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/27/2010 Added this text. :-)
15 #include "rs_fontlist.h"
18 #include "rs_entity.h"
19 #include "rs_system.h"
21 //[DONE] #warning "!!! NEED TO FIX ITERATORS IN THIS CLASS !!!"
23 RS_FontList * RS_FontList::uniqueInstance = NULL;
26 * Default constructor.
28 RS_FontList::RS_FontList(): fontIterator(fonts)
31 //#warning "!!! Need to deal with setAutoDelete() Qt3->Qt4 !!!"
32 // fonts.setAutoDelete(true);
33 //fontListListeners.setAutoDelete(false);
36 /*static*/ RS_FontList * RS_FontList::instance()
38 if (uniqueInstance == NULL)
39 uniqueInstance = new RS_FontList();
41 return uniqueInstance;
44 RS_FontList::~RS_FontList()
50 * Initializes the font list by creating empty RS_Font
51 * objects, one for each font that could be found.
53 void RS_FontList::init()
55 RS_DEBUG->print("RS_FontList::initFonts");
57 QStringList list = RS_SYSTEM->getFontList();
58 // Q3Dict<char> added; //used to remeber added fonts (avoid duplication)
59 QMultiHash<QString, char *> added; //used to remeber added fonts (avoid duplication)
61 for(QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
63 RS_DEBUG->print("font: %s:", (*it).toLatin1().data());
64 //printf("RS_FontList::init(): font: %s:\n", (*it).toLatin1().data());
68 // if (!added[fi.baseName()])
69 if (added.value(fi.baseName()) == 0)
71 RS_Font * font = new RS_Font(fi.baseName());
73 added.insert(fi.baseName(), (char *)1);
76 RS_DEBUG->print("base: %s", fi.baseName().toLatin1().data());
81 * Removes all fonts in the fontlist.
83 void RS_FontList::clearFonts()
86 while (!fonts.isEmpty())
87 delete fonts.takeFirst();
90 int RS_FontList::countFonts()
96 * Removes a font from the list.
97 * Listeners are notified after the font was removed from
98 * the list but before it gets deleted.
100 void RS_FontList::removeFont(RS_Font * font)
102 RS_DEBUG->print("RS_FontList::removeFont()");
104 // here the font is removed from the list but not deleted
105 // fonts.remove(font);
106 // Here we have to delete this ourselves, because there is no AutoDelete
108 int i = fonts.indexOf(font);
111 delete fonts.takeAt(i);
113 //for (uint i=0; i<fontListListeners.count(); ++i) {
114 // RS_FontListListener* l = fontListListeners.at(i);
115 // l->fontRemoved(font);
120 * @return Pointer to the font with the given name or
121 * \p NULL if no such font was found. The font will be loaded into
122 * memory if it's not already.
124 RS_Font * RS_FontList::requestFont(const QString & name)
126 RS_DEBUG->print("RS_FontList::requestFont %s", name.toLatin1().data());
128 QString name2 = name.toLower();
129 RS_Font * foundFont = NULL;
131 // QCad 1 compatibility:
132 if (name2.contains('#') && name2.contains('_'))
133 // name2 = name2.left(name2.find('_'));
134 name2 = name2.left(name2.indexOf('_'));
135 else if (name2.contains('#'))
136 // name2 = name2.left(name2.find('#'));
137 name2 = name2.left(name2.indexOf('#'));
139 RS_DEBUG->print("name2: %s", name2.toLatin1().data());
141 // Search our list of available fonts:
142 // for(RS_Font * f=fonts.first(); f!=NULL; f=fonts.next())
143 for(int i=0; i<fonts.size(); i++)
145 RS_Font * f = fonts[i];
147 if (f->getFileName() == name2)
149 // Make sure this font is loaded into memory:
156 if (foundFont == NULL && name != "standard")
157 foundFont = requestFont("standard");
162 //! @return First font of the list.
163 RS_Font * RS_FontList::firstFont()
165 // return fonts.first();
166 // fontIterator.toFront();
167 // return fontIterator.next();
169 fontIterator = fonts;
170 return (fontIterator.hasNext() ? fontIterator.next() : NULL);
174 * @return Next font from the list after
175 * calling firstFont() or nextFont().
177 RS_Font * RS_FontList::nextFont()
179 // return fonts.next();
180 // return fontIterator.next();
181 return (fontIterator.hasNext() ? fontIterator.next() : NULL);
185 * Dumps the fonts to stdout.
187 std::ostream & operator<<(std::ostream & os, RS_FontList & l)
189 os << "Fontlist: \n";
191 for(RS_Font * f=l.firstFont(); f!=NULL; f=l.nextFont())