-rw-r--r-- | library/fontdatabase.cpp | 235 |
1 files changed, 235 insertions, 0 deletions
diff --git a/library/fontdatabase.cpp b/library/fontdatabase.cpp new file mode 100644 index 0000000..50dcf3d --- a/dev/null +++ b/library/fontdatabase.cpp | |||
@@ -0,0 +1,235 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | #include "qpeapplication.h" | ||
22 | #include "fontfactoryinterface.h" | ||
23 | #include "fontdatabase.h" | ||
24 | |||
25 | #include <qpe/qlibrary.h> | ||
26 | |||
27 | #include <qfontmanager_qws.h> | ||
28 | #include <qdir.h> | ||
29 | #include <qdict.h> | ||
30 | #include <stdio.h> | ||
31 | #include <stdlib.h> | ||
32 | |||
33 | static QString fontDir() | ||
34 | { | ||
35 | QString qtdir = getenv("QTDIR"); | ||
36 | if ( qtdir.isEmpty() ) qtdir = "/usr/local/qt-embedded"; | ||
37 | return qtdir+"/lib/fonts/"; | ||
38 | } | ||
39 | |||
40 | #ifdef QT_NO_FONTDATABASE | ||
41 | static QString fontFamily( const QString& key ) | ||
42 | { | ||
43 | int u0 = key.find('_'); | ||
44 | int u1 = key.find('_',u0+1); | ||
45 | int u2 = key.find('_',u1+1); | ||
46 | QString family = key.left(u0); | ||
47 | //int pointSize = key.mid(u0+1,u1-u0-1).toInt(); | ||
48 | //int weight = key.mid(u1+1,u2-u1-1).toInt(); | ||
49 | //bool italic = key.mid(u2-1,1) == "i"; | ||
50 | // #### ignores _t and _I fields | ||
51 | return family; | ||
52 | } | ||
53 | #endif | ||
54 | |||
55 | QValueList<FontFactory> *FontDatabase::factoryList = 0; | ||
56 | |||
57 | /*! | ||
58 | \class FontDatabase fontdatabase.h | ||
59 | \brief The FontDatabase class provides information about available fonts. | ||
60 | |||
61 | Provides information about available fonts. | ||
62 | |||
63 | FontDatabase provides information about the available fonts of the | ||
64 | underlying window system. | ||
65 | |||
66 | Most often you will simply want to query the database for all font | ||
67 | families(), and their respective pointSizes(), styles() and charSets(). | ||
68 | |||
69 | Use FontDatabase rather than QFontDatabase when you may need access to | ||
70 | fonts not normally available. For example, if the freetype library and | ||
71 | Qtopia freetype plugin are installed TrueType fonts will be available | ||
72 | to your application. Font renderer plugins have greater resource | ||
73 | requirements than the system fonts so they should be used only when | ||
74 | necessary. | ||
75 | */ | ||
76 | |||
77 | /*! | ||
78 | Constructs the FontDatabase class. | ||
79 | */ | ||
80 | FontDatabase::FontDatabase() | ||
81 | #ifndef QT_NO_FONTDATABASE | ||
82 | : QFontDatabase() | ||
83 | #endif | ||
84 | { | ||
85 | if ( !factoryList ) | ||
86 | loadRenderers(); | ||
87 | } | ||
88 | |||
89 | /*! | ||
90 | Returns a list of names of all available font families. | ||
91 | */ | ||
92 | QStringList FontDatabase::families() const | ||
93 | { | ||
94 | #ifndef QT_NO_FONTDATABASE | ||
95 | return QFontDatabase::families(); | ||
96 | #else | ||
97 | QStringList list; | ||
98 | QDict<void> familyDict; | ||
99 | QDiskFont *qdf; | ||
100 | for ( qdf=qt_fontmanager->diskfonts.first(); qdf!=0; | ||
101 | qdf=qt_fontmanager->diskfonts.next()) { | ||
102 | QString familyname = qdf->name; | ||
103 | if ( !familyDict.find( familyname ) ) { | ||
104 | familyDict.insert( familyname, (void *)1 ); | ||
105 | list.append( familyname ); | ||
106 | } | ||
107 | } | ||
108 | |||
109 | QDir dir(fontDir(),"*.qpf"); | ||
110 | for (int i=0; i<(int)dir.count(); i++) { | ||
111 | QString familyname = fontFamily(dir[i]); | ||
112 | if ( !familyDict.find( familyname ) ) { | ||
113 | familyDict.insert( familyname, (void *)1 ); | ||
114 | list.append( familyname ); | ||
115 | } | ||
116 | } | ||
117 | |||
118 | return list; | ||
119 | #endif | ||
120 | } | ||
121 | |||
122 | #ifdef QT_NO_FONTDATABASE | ||
123 | /*! | ||
124 | Returns a list of standard fontsizes. | ||
125 | */ | ||
126 | QValueList<int> FontDatabase::standardSizes() | ||
127 | { | ||
128 | static int s[]={ 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, | ||
129 | 36, 48, 72, 0 }; | ||
130 | static bool first = TRUE; | ||
131 | static QValueList<int> sList; | ||
132 | if ( first ) { | ||
133 | first = FALSE; | ||
134 | int i = 0; | ||
135 | while( s[i] ) | ||
136 | sList.append( s[i++] ); | ||
137 | } | ||
138 | return sList; | ||
139 | } | ||
140 | |||
141 | #endif | ||
142 | |||
143 | /*! | ||
144 | Load any font renderer plugins that are available and make fonts that | ||
145 | the plugins can read available. | ||
146 | */ | ||
147 | void FontDatabase::loadRenderers() | ||
148 | { | ||
149 | #ifndef QT_NO_COMPONENT | ||
150 | if ( !factoryList ) | ||
151 | factoryList = new QValueList<FontFactory>; | ||
152 | |||
153 | QValueList<FontFactory>::Iterator mit; | ||
154 | for ( mit = factoryList->begin(); mit != factoryList->end(); ++mit ) { | ||
155 | qt_fontmanager->factories.setAutoDelete( false ); | ||
156 | qt_fontmanager->factories.removeRef( (*mit).factory ); | ||
157 | qt_fontmanager->factories.setAutoDelete( true ); | ||
158 | (*mit).interface->release(); | ||
159 | (*mit).library->unload(); | ||
160 | delete (*mit).library; | ||
161 | } | ||
162 | factoryList->clear(); | ||
163 | |||
164 | QString path = QPEApplication::qpeDir() + "/plugins/fontfactories"; | ||
165 | QDir dir( path, "lib*.so" ); | ||
166 | QStringList list = dir.entryList(); | ||
167 | QStringList::Iterator it; | ||
168 | for ( it = list.begin(); it != list.end(); ++it ) { | ||
169 | FontFactoryInterface *iface = 0; | ||
170 | QLibrary *lib = new QLibrary( path + "/" + *it ); | ||
171 | if ( lib->queryInterface( IID_FontFactory, (QUnknownInterface**)&iface ) == QS_OK ) { | ||
172 | FontFactory factory; | ||
173 | factory.library = lib; | ||
174 | factory.interface = iface; | ||
175 | factory.factory = factory.interface->fontFactory(); | ||
176 | factoryList->append( factory ); | ||
177 | qt_fontmanager->factories.append( factory.factory ); | ||
178 | readFonts( factory.factory ); | ||
179 | } else { | ||
180 | delete lib; | ||
181 | } | ||
182 | } | ||
183 | #endif | ||
184 | } | ||
185 | |||
186 | /*! | ||
187 | \internal | ||
188 | */ | ||
189 | void FontDatabase::readFonts( QFontFactory *factory ) | ||
190 | { | ||
191 | |||
192 | // Load in font definition file | ||
193 | QString fn = fontDir() + "fontdir"; | ||
194 | FILE* fontdef=fopen(fn.local8Bit(),"r"); | ||
195 | if(!fontdef) { | ||
196 | QCString temp=fn.local8Bit(); | ||
197 | qWarning("Cannot find font definition file %s - is $QTDIR set correctly?", | ||
198 | temp.data()); | ||
199 | return; | ||
200 | } | ||
201 | char buf[200]=""; | ||
202 | char name[200]=""; | ||
203 | char render[200]=""; | ||
204 | char file[200]=""; | ||
205 | char flags[200]=""; | ||
206 | char isitalic[10]=""; | ||
207 | fgets(buf,200,fontdef); | ||
208 | while(!feof(fontdef)) { | ||
209 | if ( buf[0] != '#' ) { | ||
210 | int weight=50; | ||
211 | int size=0; | ||
212 | flags[0]=0; | ||
213 | sscanf(buf,"%s %s %s %s %d %d %s",name,file,render,isitalic,&weight,&size,flags); | ||
214 | QString filename; | ||
215 | if ( file[0] != '/' ) | ||
216 | filename = fontDir(); | ||
217 | filename += file; | ||
218 | if ( QFile::exists(filename) ) { | ||
219 | if( factory->name() == render ) { | ||
220 | QDiskFont * qdf=new QDiskFont(factory,name,isitalic[0]=='y', | ||
221 | weight,size,flags,filename); | ||
222 | qt_fontmanager->diskfonts.append(qdf); | ||
223 | #ifndef QT_NO_FONTDATABASE | ||
224 | #if QT_VERSION >= 232 | ||
225 | QFontDatabase::qwsAddDiskFont( qdf ); | ||
226 | #endif | ||
227 | #endif | ||
228 | } | ||
229 | } | ||
230 | } | ||
231 | fgets(buf,200,fontdef); | ||
232 | } | ||
233 | fclose(fontdef); | ||
234 | } | ||
235 | |||