* @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. */
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());
- }
+
+ RS_DEBUG->print("RS_Font::loadFont: Successfully opened font file: %s", path.toAscii().data());
QTextStream ts(&file);
- QString line;
// Read line by line until we find a new letter:
//I think this is wrong... We're mixing classes here...
// while (!file.atEnd())
while (!ts.atEnd())
{
- line = ts.readLine();
-//printf("\"%s\"\n", line.toAscii().data());
+ 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
-//printf("--> identifier=\"%s\", value=\"%s\"\n", identifier.toAscii().data(), value.toAscii().data());
-
- 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);
}
- file.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