]> Shamusworld >> Repos - architektonas/blob - src/base/rs_fontlist.cpp
Fixed thumbnail rendering in LibraryWidget and DXF detection.
[architektonas] / src / base / rs_fontlist.cpp
1 // rs_fontlist.cpp
2 //
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 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  05/27/2010  Added this text. :-)
15 //
16
17 #include "rs_fontlist.h"
18
19 #include "rs_font.h"
20 #include "rs_entity.h"
21 #include "rs_system.h"
22
23 //[DONE] #warning "!!! NEED TO FIX ITERATORS IN THIS CLASS !!!"
24
25 RS_FontList * RS_FontList::uniqueInstance = NULL;
26
27 /**
28  * Default constructor.
29  */
30 RS_FontList::RS_FontList(): fontIterator(fonts)
31 {
32 //Dealt with here...
33 //#warning "!!! Need to deal with setAutoDelete() Qt3->Qt4 !!!"
34 //      fonts.setAutoDelete(true);
35         //fontListListeners.setAutoDelete(false);
36 }
37
38 /*static*/ RS_FontList * RS_FontList::instance()
39 {
40         if (uniqueInstance == NULL)
41                 uniqueInstance = new RS_FontList();
42
43         return uniqueInstance;
44 }
45
46 RS_FontList::~RS_FontList()
47 {
48         clearFonts();
49 }
50
51 /**
52  * Initializes the font list by creating empty RS_Font
53  * objects, one for each font that could be found.
54  */
55 void RS_FontList::init()
56 {
57         RS_DEBUG->print("RS_FontList::initFonts");
58
59         QStringList list = RS_SYSTEM->getFontList();
60 //      Q3Dict<char> added; //used to remeber added fonts (avoid duplication)
61         QMultiHash<QString, char *> added; //used to remeber added fonts (avoid duplication)
62
63         for(QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
64         {
65                 RS_DEBUG->print("font: %s:", (*it).toLatin1().data());
66 //printf("RS_FontList::init(): font: %s:\n", (*it).toLatin1().data());
67
68                 QFileInfo fi(*it);
69
70 //              if (!added[fi.baseName()])
71                 if (added.value(fi.baseName()) == 0)
72                 {
73                         RS_Font * font = new RS_Font(fi.baseName());
74                         fonts.append(font);
75                         added.insert(fi.baseName(), (char *)1);
76                 }
77
78                 RS_DEBUG->print("base: %s", fi.baseName().toLatin1().data());
79         }
80 }
81
82 /**
83  * Removes all fonts in the fontlist.
84  */
85 void RS_FontList::clearFonts()
86 {
87 //      fonts.clear();
88         while (!fonts.isEmpty())
89                 delete fonts.takeFirst();
90 }
91
92 int RS_FontList::countFonts()
93 {
94         return fonts.count();
95 }
96
97 /**
98  * Removes a font from the list.
99  * Listeners are notified after the font was removed from
100  * the list but before it gets deleted.
101  */
102 void RS_FontList::removeFont(RS_Font * font)
103 {
104         RS_DEBUG->print("RS_FontList::removeFont()");
105
106         // here the font is removed from the list but not deleted
107 //      fonts.remove(font);
108         // Here we have to delete this ourselves, because there is no AutoDelete
109         // for QLists
110         int i = fonts.indexOf(font);
111
112         if (i != -1)
113                 delete fonts.takeAt(i);
114
115         //for (uint i=0; i<fontListListeners.count(); ++i) {
116         //    RS_FontListListener* l = fontListListeners.at(i);
117         //    l->fontRemoved(font);
118         //}
119 }
120
121 /**
122  * @return Pointer to the font with the given name or
123  * \p NULL if no such font was found. The font will be loaded into
124  * memory if it's not already.
125  */
126 RS_Font * RS_FontList::requestFont(const QString & name)
127 {
128         RS_DEBUG->print("RS_FontList::requestFont %s",  name.toLatin1().data());
129
130         QString name2 = name.toLower();
131         RS_Font * foundFont = NULL;
132
133         // QCad 1 compatibility:
134         if (name2.contains('#') && name2.contains('_'))
135 //              name2 = name2.left(name2.find('_'));
136                 name2 = name2.left(name2.indexOf('_'));
137         else if (name2.contains('#'))
138 //              name2 = name2.left(name2.find('#'));
139                 name2 = name2.left(name2.indexOf('#'));
140
141         RS_DEBUG->print("name2: %s", name2.toLatin1().data());
142
143         // Search our list of available fonts:
144 //      for(RS_Font * f=fonts.first(); f!=NULL; f=fonts.next())
145         for(int i=0; i<fonts.size(); i++)
146         {
147                 RS_Font * f = fonts[i];
148
149                 if (f->getFileName() == name2)
150                 {
151                         // Make sure this font is loaded into memory:
152                         f->loadFont();
153                         foundFont = f;
154                         break;
155                 }
156         }
157
158         if (foundFont == NULL && name != "standard")
159                 foundFont = requestFont("standard");
160
161         return foundFont;
162 }
163
164 //! @return First font of the list.
165 RS_Font * RS_FontList::firstFont()
166 {
167 //      return fonts.first();
168 //      fontIterator.toFront();
169 //      return fontIterator.next();
170
171         fontIterator = fonts;
172         return (fontIterator.hasNext() ? fontIterator.next() : NULL);
173 }
174
175 /**
176  * @return Next font from the list after
177  * calling firstFont() or nextFont().
178  */
179 RS_Font * RS_FontList::nextFont()
180 {
181 //      return fonts.next();
182 //      return fontIterator.next();
183         return (fontIterator.hasNext() ? fontIterator.next() : NULL);
184 }
185
186 /**
187  * Dumps the fonts to stdout.
188  */
189 std::ostream & operator<<(std::ostream & os, RS_FontList & l)
190 {
191         os << "Fontlist: \n";
192
193         for(RS_Font * f=l.firstFont(); f!=NULL; f=l.nextFont())
194                 os << *f << "\n";
195
196         return os;
197 }