X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fbase%2Frs_font.cpp;h=a7156cedb5446ae93f61104473ded43a342c1539;hb=20cce16e98fc9b052c5862efa6394a285971e846;hp=cd81bb939a9520730242a51ef4b3dfb29fcff2b0;hpb=16ce54abf01ca3032e42a5bb11a4afcf9014dcca;p=architektonas diff --git a/src/base/rs_font.cpp b/src/base/rs_font.cpp index cd81bb9..a7156ce 100644 --- a/src/base/rs_font.cpp +++ b/src/base/rs_font.cpp @@ -3,7 +3,9 @@ // Part of the Architektonas Project // Originally part of QCad Community Edition by Andrew Mustun // Extensively rewritten and refactored by James L. Hammons -// (C) 2010 Underground Software +// Portions copyright (C) 2001-2003 RibbonSoft +// Copyright (C) 2010 Underground Software +// See the README and GPLv2 files for licensing and warranty information // // JLH = James L. Hammons // @@ -25,14 +27,10 @@ * @param owner true if the font owns the letters (blocks). Otherwise * the letters will be deleted when the font is deleted. */ -RS_Font::RS_Font(const QString & fileName, bool owner): letterList(owner) +RS_Font::RS_Font(const QString & fn, bool owner): letterList(owner), fileName(fn), + encoding(""), loaded(false), letterSpacing(3.0), wordSpacing(6.75), + lineSpacingFactor(1.0) { - this->fileName = fileName; - encoding = ""; - loaded = false; - letterSpacing = 3.0; - wordSpacing = 6.75; - lineSpacingFactor = 1.0; } /** @return the fileName of this font. */ @@ -119,161 +117,145 @@ bool RS_Font::loadFont() } // Open cxf file: - QFile f(path); + QFile file(path); - if (!f.open(QIODevice::ReadOnly)) + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Font::loadFont: Cannot open font file: %s", - path.toLatin1().data()); + path.toAscii().data()); return false; } - else - { - RS_DEBUG->print("RS_Font::loadFont: Successfully opened font file: %s", path.toLatin1().data()); - } - QTextStream ts(&f); - QString line; + RS_DEBUG->print("RS_Font::loadFont: Successfully opened font file: %s", path.toAscii().data()); + + QTextStream ts(&file); // Read line by line until we find a new letter: - while (!f.atEnd()) +//I think this is wrong... We're mixing classes here... +//AND THAT WAS THE PROBLEM!!! +// while (!file.atEnd()) + while (!ts.atEnd()) { - line = ts.readLine(); + QString line = ts.readLine(); if (line.isEmpty()) continue; // Read font settings: if (line.at(0) == '#') - { -#if 0 -// QStringList lst = QStringList::split(':', line.right(line.length() - 1)); - QStringList lst = line.right(line.length() - 1).split(":"); - QStringList::Iterator it3 = lst.begin(); - -// QString identifier = (*it3).stripWhiteSpace(); - QString identifier = (*it3); - identifier = identifier.simplified().toLower(); - it3++; -// QString value = (*it3).stripWhiteSpace(); - QString value = (*it3); - value = value.simplified().toLower(); -#else - QStringList list = line.right(line.length() - 1).split(":"); - QString identifier = "", value = ""; - - if (list.size() >= 1) - identifier = list[0].simplified().toLower(); - - if (list.size() >= 2) - value = list[1].simplified();//.toLower(); -#endif - - if (identifier == "letterspacing") - letterSpacing = value.toDouble(); - else if (identifier == "wordspacing") - wordSpacing = value.toDouble(); - else if (identifier == "linespacingfactor") - lineSpacingFactor = value.toDouble(); - else if (identifier == "author") - authors.append(value); - else if (identifier == "name") - names.append(value); - else if (identifier == "encoding") - { - ts.setCodec(QTextCodec::codecForName(value.toAscii())); - encoding = value; - } - } + ParseIdentifier(ts, line); // Add another letter to this font: else if (line.at(0) == '[') - { - // uniode character: - QChar ch; + ParseCharacter(ts, line); + } - // read unicode: - QRegExp regexp("[0-9A-Fa-f]{4,4}"); -// regexp.search(line); - regexp.indexIn(line); - QString cap = regexp.cap(); + file.close(); + loaded = true; + RS_DEBUG->print("RS_Font::loadFont OK"); - if (!cap.isNull()) - { - int uCode = cap.toInt(NULL, 16); - ch = QChar(uCode); - } - // read UTF8 (qcad 1 compatibility) -// else if (line.find(']') >= 3) - else if (line.indexOf(']') >= 3) - { -// int i = line.find(']'); - int i = line.indexOf(']'); - QString mid = line.mid(1, i - 1); - ch = QString::fromUtf8(mid.toLatin1()).at(0); - } - // read normal ascii character: - else - { - ch = line.at(1); - } + return true; +} - // create new letter: - RS_FontChar * letter = new RS_FontChar(NULL, ch, Vector(0.0, 0.0)); +void RS_Font::ParseIdentifier(QTextStream & ts, QString line) +{ + QStringList list = line.mid(1).split(":"); + QString identifier = "", value = ""; + + if (list.size() >= 1) + identifier = list[0].simplified().toLower(); + + if (list.size() >= 2) + value = list[1].simplified(); + + if (identifier == "letterspacing") + letterSpacing = value.toDouble(); + else if (identifier == "wordspacing") + wordSpacing = value.toDouble(); + else if (identifier == "linespacingfactor") + lineSpacingFactor = value.toDouble(); + else if (identifier == "author") + authors.append(value); + else if (identifier == "name") + names.append(value); + else if (identifier == "encoding") + { + ts.setCodec(QTextCodec::codecForName(value.toAscii())); + encoding = value; + } +} - // Read entities of this letter: - line = ts.readLine(); +void RS_Font::ParseCharacter(QTextStream & ts, QString line) +{ + // Unicode character: + QChar ch; - while (!line.isEmpty()) - { - QString coordsStr = line.right(line.length() - 2); - QStringList coords = coordsStr.split(","); - QStringList::Iterator it2 = coords.begin(); - - // Line: - if (line.at(0) == 'L') - { - double x1 = (*it2++).toDouble(); - double y1 = (*it2++).toDouble(); - double x2 = (*it2++).toDouble(); - double y2 = (*it2).toDouble(); - - RS_LineData ld(Vector(x1, y1), Vector(x2, y2)); - RS_Line * line = new RS_Line(letter, ld); - line->setPen(RS_Pen(RS2::FlagInvalid)); - line->setLayer(NULL); - letter->addEntity(line); - } - // Arc: - else if (line.at(0) == 'A') - { - double cx = (*it2++).toDouble(); - double cy = (*it2++).toDouble(); - double r = (*it2++).toDouble(); - double a1 = (*it2++).toDouble() / ARAD; - double a2 = (*it2).toDouble() / ARAD; - bool reversed = (line.at(1) == 'R'); - - RS_ArcData ad(Vector(cx, cy), r, a1, a2, reversed); - RS_Arc * arc = new RS_Arc(letter, ad); - arc->setPen(RS_Pen(RS2::FlagInvalid)); - arc->setLayer(NULL); - letter->addEntity(arc); - } - - line = ts.readLine(); - } + // Read unicode: + QRegExp regexp("[0-9A-Fa-f]{4,4}"); + regexp.indexIn(line); + QString cap = regexp.cap(); - letter->calculateBorders(); - letterList.add(letter); - } + if (!cap.isNull()) + { + int uCode = cap.toInt(NULL, 16); + ch = QChar(uCode); + } + // Read UTF8 (QCad 1 compatibility) + else if (line.indexOf(']') >= 3) + { + int i = line.indexOf(']'); + QString mid = line.mid(1, i - 1); + ch = QString::fromUtf8(mid.toAscii()).at(0); + } + // Read normal ascii character: + else + { + ch = line.at(1); } - f.close(); - loaded = true; + // Create new letter: + RS_FontChar * letter = new RS_FontChar(NULL, ch, Vector(0.0, 0.0)); + // Read entities of this letter: + line = ts.readLine(); - RS_DEBUG->print("RS_Font::loadFont OK"); + while (!line.isEmpty()) + { + QStringList coords = line.mid(2).split(","); + QStringList::Iterator it2 = coords.begin(); - return true; + if (line.at(0) == 'L') // Line + { + double x1 = (*it2++).toDouble(); + double y1 = (*it2++).toDouble(); + double x2 = (*it2++).toDouble(); + double y2 = (*it2).toDouble(); + + RS_LineData ld(Vector(x1, y1), Vector(x2, y2)); + RS_Line * line = new RS_Line(letter, ld); + line->setPen(RS_Pen(RS2::FlagInvalid)); + line->setLayer(NULL); + letter->addEntity(line); + } + else if (line.at(0) == 'A') // Arc + { + double cx = (*it2++).toDouble(); + double cy = (*it2++).toDouble(); + double r = (*it2++).toDouble(); + double a1 = (*it2++).toDouble() / ARAD; + double a2 = (*it2).toDouble() / ARAD; + bool reversed = (line.at(1) == 'R'); + + RS_ArcData ad(Vector(cx, cy), r, a1, a2, reversed); + RS_Arc * arc = new RS_Arc(letter, ad); + arc->setPen(RS_Pen(RS2::FlagInvalid)); + arc->setLayer(NULL); + letter->addEntity(arc); + } + + line = ts.readLine(); + } + + letter->calculateBorders(); + letterList.add(letter); } // Wrappers for block list (letters) functions