3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 05/28/2010 Added this text. :-)
18 #include "rs_fontchar.h"
20 #include "rs_system.h"
25 * @param owner true if the font owns the letters (blocks). Otherwise
26 * the letters will be deleted when the font is deleted.
28 RS_Font::RS_Font(const QString & fileName, bool owner): letterList(owner)
30 this->fileName = fileName;
35 lineSpacingFactor = 1.0;
38 /** @return the fileName of this font. */
39 QString RS_Font::getFileName() const
44 /** @return the encoding of this font. */
45 QString RS_Font::getEncoding() const
50 /** @return the alternative names of this font. */
51 const QStringList & RS_Font::getNames() const
56 /** @return the author(s) of this font. */
57 const QStringList & RS_Font::getAuthors() const
62 /** @return Default letter spacing for this font */
63 double RS_Font::getLetterSpacing()
68 /** @return Default word spacing for this font */
69 double RS_Font::getWordSpacing()
74 /** @return Default line spacing factor for this font */
75 double RS_Font::getLineSpacingFactor()
77 return lineSpacingFactor;
81 * Loads the font into memory.
83 * @retval true font was already loaded or is loaded now.
84 * @retval false font could not be loaded.
86 bool RS_Font::loadFont()
88 RS_DEBUG->print("RS_Font::loadFont");
95 // Search for the appropriate font if we have only the name of the font:
96 if (!fileName.toLower().contains(".cxf"))
98 QStringList fonts = RS_SYSTEM->getFontList();
101 for(QStringList::Iterator it=fonts.begin(); it!=fonts.end(); it++)
103 if (QFileInfo(*it).baseName().toLower() == fileName.toLower())
110 // We have the full path of the font:
114 // No font paths found:
117 RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Font::loadFont: No fonts available.");
124 if (!f.open(QIODevice::ReadOnly))
126 RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Font::loadFont: Cannot open font file: %s",
127 path.toLatin1().data());
132 RS_DEBUG->print("RS_Font::loadFont: Successfully opened font file: %s", path.toLatin1().data());
138 // Read line by line until we find a new letter:
141 line = ts.readLine();
146 // Read font settings:
147 if (line.at(0) == '#')
150 // QStringList lst = QStringList::split(':', line.right(line.length() - 1));
151 QStringList lst = line.right(line.length() - 1).split(":");
152 QStringList::Iterator it3 = lst.begin();
154 // QString identifier = (*it3).stripWhiteSpace();
155 QString identifier = (*it3);
156 identifier = identifier.simplified().toLower();
158 // QString value = (*it3).stripWhiteSpace();
159 QString value = (*it3);
160 value = value.simplified().toLower();
162 QStringList list = line.right(line.length() - 1).split(":");
163 QString identifier = "", value = "";
165 if (list.size() >= 1)
166 identifier = list[0].simplified().toLower();
168 if (list.size() >= 2)
169 value = list[1].simplified();//.toLower();
172 if (identifier == "letterspacing")
173 letterSpacing = value.toDouble();
174 else if (identifier == "wordspacing")
175 wordSpacing = value.toDouble();
176 else if (identifier == "linespacingfactor")
177 lineSpacingFactor = value.toDouble();
178 else if (identifier == "author")
179 authors.append(value);
180 else if (identifier == "name")
182 else if (identifier == "encoding")
184 ts.setCodec(QTextCodec::codecForName(value.toAscii()));
188 // Add another letter to this font:
189 else if (line.at(0) == '[')
195 QRegExp regexp("[0-9A-Fa-f]{4,4}");
196 // regexp.search(line);
197 regexp.indexIn(line);
198 QString cap = regexp.cap();
202 int uCode = cap.toInt(NULL, 16);
205 // read UTF8 (qcad 1 compatibility)
206 // else if (line.find(']') >= 3)
207 else if (line.indexOf(']') >= 3)
209 // int i = line.find(']');
210 int i = line.indexOf(']');
211 QString mid = line.mid(1, i - 1);
212 ch = QString::fromUtf8(mid.toLatin1()).at(0);
214 // read normal ascii character:
220 // create new letter:
221 RS_FontChar * letter = new RS_FontChar(NULL, ch, Vector(0.0, 0.0));
223 // Read entities of this letter:
224 line = ts.readLine();
226 while (!line.isEmpty())
228 QString coordsStr = line.right(line.length() - 2);
229 QStringList coords = coordsStr.split(",");
230 QStringList::Iterator it2 = coords.begin();
233 if (line.at(0) == 'L')
235 double x1 = (*it2++).toDouble();
236 double y1 = (*it2++).toDouble();
237 double x2 = (*it2++).toDouble();
238 double y2 = (*it2).toDouble();
240 RS_LineData ld(Vector(x1, y1), Vector(x2, y2));
241 RS_Line * line = new RS_Line(letter, ld);
242 line->setPen(RS_Pen(RS2::FlagInvalid));
243 line->setLayer(NULL);
244 letter->addEntity(line);
247 else if (line.at(0) == 'A')
249 double cx = (*it2++).toDouble();
250 double cy = (*it2++).toDouble();
251 double r = (*it2++).toDouble();
252 double a1 = (*it2++).toDouble() / ARAD;
253 double a2 = (*it2).toDouble() / ARAD;
254 bool reversed = (line.at(1) == 'R');
256 RS_ArcData ad(Vector(cx, cy), r, a1, a2, reversed);
257 RS_Arc * arc = new RS_Arc(letter, ad);
258 arc->setPen(RS_Pen(RS2::FlagInvalid));
260 letter->addEntity(arc);
263 line = ts.readLine();
266 letter->calculateBorders();
267 letterList.add(letter);
274 RS_DEBUG->print("RS_Font::loadFont OK");
279 // Wrappers for block list (letters) functions
280 RS_BlockList * RS_Font::getLetterList()
285 RS_Block * RS_Font::findLetter(const QString & name)
287 return letterList.find(name);
290 uint RS_Font::countLetters()
292 return letterList.count();
295 RS_Block * RS_Font::letterAt(uint i)
297 return letterList.at(i);
301 * Dumps the fonts data to stdout.
303 std::ostream & operator<<(std::ostream & os, const RS_Font & f)
305 os << " Font file name: " << f.getFileName().toLatin1().data() << "\n";
306 //<< (RS_BlockList&)f << "\n";