+++ /dev/null
-// font.cpp
-//
-// Part of the Architektonas Project
-// Originally part of QCad Community Edition by Andrew Mustun
-// Extensively rewritten and refactored by James L. Hammons
-// 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 <jlhamm@acm.org>
-//
-// Who When What
-// --- ---------- -----------------------------------------------------------
-// JLH 05/28/2010 Added this text. :-)
-//
-
-#include "font.h"
-
-#include "color.h"
-#include "fontchar.h"
-#include "mathextra.h"
-#include "system.h"
-
-/**
- * Constructor.
- *
- * @param owner true if the font owns the letters (blocks). Otherwise
- * the letters will be deleted when the font is deleted.
- */
-Font::Font(const QString & fn, bool owner): letterList(owner), fileName(fn),
- encoding(""), loaded(false), letterSpacing(3.0), wordSpacing(6.75),
- lineSpacingFactor(1.0)
-{
-}
-
-/** @return the fileName of this font. */
-QString Font::getFileName() const
-{
- return fileName;
-}
-
-/** @return the encoding of this font. */
-QString Font::getEncoding() const
-{
- return encoding;
-}
-
-/** @return the alternative names of this font. */
-const QStringList & Font::getNames() const
-{
- return names;
-}
-
-/** @return the author(s) of this font. */
-const QStringList & Font::getAuthors() const
-{
- return authors;
-}
-
-/** @return Default letter spacing for this font */
-double Font::getLetterSpacing()
-{
- return letterSpacing;
-}
-
-/** @return Default word spacing for this font */
-double Font::getWordSpacing()
-{
- return wordSpacing;
-}
-
-/** @return Default line spacing factor for this font */
-double Font::getLineSpacingFactor()
-{
- return lineSpacingFactor;
-}
-
-/**
- * Loads the font into memory.
- *
- * @retval true font was already loaded or is loaded now.
- * @retval false font could not be loaded.
- */
-bool Font::loadFont()
-{
- DEBUG->print("Font::loadFont");
-
- if (loaded)
- return true;
-
- QString path;
-
- // Search for the appropriate font if we have only the name of the font:
- if (!fileName.toLower().contains(".cxf"))
- {
- QStringList fonts = SYSTEM->getFontList();
- QFileInfo file;
-
- for(QStringList::Iterator it=fonts.begin(); it!=fonts.end(); it++)
- {
- if (QFileInfo(*it).baseName().toLower() == fileName.toLower())
- {
- path = *it;
- break;
- }
- }
- }
- // We have the full path of the font:
- else
- path = fileName;
-
- // No font paths found:
- if (path.isEmpty())
- {
- DEBUG->print(Debug::D_WARNING, "Font::loadFont: No fonts available.");
- return false;
- }
-
- // Open cxf file:
- QFile file(path);
-
- if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
- {
- DEBUG->print(Debug::D_WARNING, "Font::loadFont: Cannot open font file: %s",
- path.toAscii().data());
- return false;
- }
-
- DEBUG->print("Font::loadFont: Successfully opened font file: %s", path.toAscii().data());
-
- QTextStream ts(&file);
-
- // Read line by line until we find a new letter:
-//I think this is wrong... We're mixing classes here...
-//AND THAT WAS THE PROBLEM!!!
-// while (!file.atEnd())
- while (!ts.atEnd())
- {
- QString line = ts.readLine();
-
- if (line.isEmpty())
- continue;
-
- // Read font settings:
- if (line.at(0) == '#')
- ParseIdentifier(ts, line);
- // Add another letter to this font:
- else if (line.at(0) == '[')
- ParseCharacter(ts, line);
- }
-
- file.close();
- loaded = true;
- DEBUG->print("Font::loadFont OK");
-
- return true;
-}
-
-void 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;
- }
-}
-
-void Font::ParseCharacter(QTextStream & ts, QString line)
-{
- // Unicode character:
- QChar ch;
-
- // Read unicode:
- QRegExp regexp("[0-9A-Fa-f]{4,4}");
- regexp.indexIn(line);
- QString cap = regexp.cap();
-
- 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);
- }
-
- // Create new letter:
- FontChar * letter = new FontChar(NULL, ch, Vector(0.0, 0.0));
- // Read entities of this letter:
- line = ts.readLine();
-
- while (!line.isEmpty())
- {
- QStringList coords = line.mid(2).split(",");
- QStringList::Iterator it2 = coords.begin();
-
- if (line.at(0) == 'L') // Line
- {
- double x1 = (*it2++).toDouble();
- double y1 = (*it2++).toDouble();
- double x2 = (*it2++).toDouble();
- double y2 = (*it2).toDouble();
-
- LineData ld(Vector(x1, y1), Vector(x2, y2));
- Line * line = new Line(letter, ld);
- line->setPen(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');
-
- ArcData ad(Vector(cx, cy), r, a1, a2, reversed);
- Arc * arc = new Arc(letter, ad);
- arc->setPen(Pen(RS2::FlagInvalid));
- arc->setLayer(NULL);
- letter->addEntity(arc);
- }
-
- line = ts.readLine();
- }
-
- letter->calculateBorders();
- letterList.add(letter);
-}
-
-// Wrappers for block list (letters) functions
-BlockList * Font::getLetterList()
-{
- return &letterList;
-}
-
-Block * Font::findLetter(const QString & name)
-{
- return letterList.find(name);
-}
-
-uint Font::countLetters()
-{
- return letterList.count();
-}
-
-Block * Font::letterAt(uint i)
-{
- return letterList.at(i);
-}
-
-/**
- * Dumps the fonts data to stdout.
- */
-std::ostream & operator<<(std::ostream & os, const Font & f)
-{
- os << " Font file name: " << f.getFileName().toLatin1().data() << "\n";
- //<< (BlockList&)f << "\n";
- return os;
-}