]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: font.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
6 | // Id: | |
7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "font.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/font.h" | |
16 | #include "wx/utils.h" | |
17 | #include <strings.h> | |
18 | ||
19 | //----------------------------------------------------------------------------- | |
20 | // local data | |
21 | //----------------------------------------------------------------------------- | |
22 | ||
23 | static char *wx_font_family [] = { | |
24 | "wxDEFAULT", "wxDECORATIVE", "wxMODERN", "wxROMAN", "wxSCRIPT", | |
25 | "wxSWISS", "wxTELETYPE", | |
26 | }; | |
27 | static char *wx_font_style [] = { | |
28 | "wxDEFAULT", "wxNORMAL", "wxSLANT", "wxITALIC", | |
29 | }; | |
30 | static char *wx_font_weight [] = { | |
31 | "wxDEFAULT", "wxNORMAL", "wxBOLD", "wxLIGHT", | |
32 | }; | |
33 | ||
34 | extern wxFontNameDirectory wxTheFontNameDirectory; | |
35 | ||
36 | //----------------------------------------------------------------------------- | |
37 | // wxFont | |
38 | //----------------------------------------------------------------------------- | |
39 | ||
40 | class wxFontRefData: public wxObjectRefData | |
41 | { | |
42 | public: | |
43 | ||
44 | wxFontRefData(void); | |
45 | ~wxFontRefData(void); | |
46 | ||
47 | wxList m_scaled_xfonts; | |
48 | int m_pointSize; | |
49 | int m_family, m_style, m_weight; | |
50 | bool m_underlined; | |
51 | int m_fontId; | |
52 | char* m_faceName; | |
53 | ||
54 | bool m_byXFontName; | |
55 | GdkFont *m_font; | |
56 | ||
57 | friend wxFont; | |
58 | }; | |
59 | ||
60 | wxFontRefData::wxFontRefData(void) : m_scaled_xfonts(wxKEY_INTEGER) | |
61 | { | |
62 | m_byXFontName = FALSE; | |
63 | m_pointSize = -1; | |
64 | m_family = -1; | |
65 | m_style = -1; | |
66 | m_weight = -1; | |
67 | m_underlined = FALSE; | |
68 | m_fontId = 0; | |
69 | m_faceName = NULL; | |
70 | m_font = NULL; | |
71 | }; | |
72 | ||
73 | wxFontRefData::~wxFontRefData(void) | |
74 | { | |
75 | wxNode *node = m_scaled_xfonts.First(); | |
76 | while (node) | |
77 | { | |
78 | GdkFont *font = (GdkFont*)node->Data(); | |
79 | wxNode *next = node->Next(); | |
80 | gdk_font_unref( font ); | |
81 | node = next; | |
82 | }; | |
83 | if (m_faceName) | |
84 | { | |
85 | delete m_faceName; | |
86 | m_faceName = NULL; | |
87 | }; | |
88 | if (m_font) gdk_font_unref( m_font ); | |
89 | }; | |
90 | ||
91 | //----------------------------------------------------------------------------- | |
92 | ||
93 | #define M_FONTDATA ((wxFontRefData *)m_refData) | |
94 | ||
95 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) | |
96 | ||
97 | wxFont::wxFont(void) | |
98 | { | |
99 | if (wxTheFontList) wxTheFontList->Append( this ); | |
100 | }; | |
101 | ||
102 | wxFont::wxFont( char *xFontName ) | |
103 | { | |
104 | if (!xFontName) return; | |
105 | ||
106 | m_refData = new wxFontRefData(); | |
107 | ||
108 | M_FONTDATA->m_byXFontName = TRUE; | |
109 | M_FONTDATA->m_font = gdk_font_load( xFontName ); | |
110 | }; | |
111 | ||
112 | wxFont::wxFont(int PointSize, int FontIdOrFamily, int Style, int Weight, | |
113 | bool Underlined, const char* Face) | |
114 | { | |
115 | m_refData = new wxFontRefData(); | |
116 | ||
117 | if ((M_FONTDATA->m_faceName = (Face) ? copystring(Face) : (char*)NULL) ) | |
118 | { | |
119 | M_FONTDATA->m_fontId = wxTheFontNameDirectory.FindOrCreateFontId( Face, FontIdOrFamily ); | |
120 | M_FONTDATA->m_family = wxTheFontNameDirectory.GetFamily( FontIdOrFamily ); | |
121 | } | |
122 | else | |
123 | { | |
124 | M_FONTDATA->m_fontId = FontIdOrFamily; | |
125 | M_FONTDATA->m_family = wxTheFontNameDirectory.GetFamily( FontIdOrFamily ); | |
126 | }; | |
127 | M_FONTDATA->m_style = Style; | |
128 | M_FONTDATA->m_weight = Weight; | |
129 | M_FONTDATA->m_pointSize = PointSize; | |
130 | M_FONTDATA->m_underlined = Underlined; | |
131 | ||
132 | if (wxTheFontList) wxTheFontList->Append( this ); | |
133 | }; | |
134 | ||
135 | wxFont::wxFont(int PointSize, const char *Face, int Family, int Style, | |
136 | int Weight, bool Underlined) | |
137 | { | |
138 | m_refData = new wxFontRefData(); | |
139 | ||
140 | M_FONTDATA->m_fontId = wxTheFontNameDirectory.FindOrCreateFontId( Face, Family ); | |
141 | M_FONTDATA->m_faceName = (Face) ? copystring(Face) : (char*)NULL; | |
142 | M_FONTDATA->m_family = wxTheFontNameDirectory.GetFamily( M_FONTDATA->m_fontId ); | |
143 | M_FONTDATA->m_style = Style; | |
144 | M_FONTDATA->m_weight = Weight; | |
145 | M_FONTDATA->m_pointSize = PointSize; | |
146 | M_FONTDATA->m_underlined = Underlined; | |
147 | ||
148 | if (wxTheFontList) wxTheFontList->Append( this ); | |
149 | }; | |
150 | ||
151 | wxFont::wxFont( const wxFont& font ) | |
152 | { | |
153 | Ref( font ); | |
154 | }; | |
155 | ||
156 | wxFont::wxFont( const wxFont* font ) | |
157 | { | |
158 | UnRef(); | |
159 | if (font) Ref( *font ); | |
160 | }; | |
161 | ||
162 | wxFont::~wxFont(void) | |
163 | { | |
164 | if (wxTheFontList) wxTheFontList->DeleteObject( this ); | |
165 | }; | |
166 | ||
167 | wxFont& wxFont::operator = ( const wxFont& font ) | |
168 | { | |
169 | if (*this == font) return (*this); | |
170 | Ref( font ); | |
171 | return *this; | |
172 | }; | |
173 | ||
174 | bool wxFont::operator == ( const wxFont& font ) | |
175 | { | |
176 | return m_refData == font.m_refData; | |
177 | }; | |
178 | ||
179 | bool wxFont::operator != ( const wxFont& font ) | |
180 | { | |
181 | return m_refData != font.m_refData; | |
182 | }; | |
183 | ||
184 | bool wxFont::Ok() | |
185 | { | |
186 | return (m_refData != NULL); | |
187 | }; | |
188 | ||
189 | int wxFont::GetPointSize(void) const | |
190 | { | |
191 | return M_FONTDATA->m_pointSize; | |
192 | }; | |
193 | ||
194 | wxString wxFont::GetFaceString(void) const | |
195 | { | |
196 | wxString s = wxTheFontNameDirectory.GetFontName( M_FONTDATA->m_fontId ); | |
197 | return s; | |
198 | }; | |
199 | ||
200 | wxString wxFont::GetFaceName(void) const | |
201 | { | |
202 | wxString s = wxTheFontNameDirectory.GetFontName( M_FONTDATA->m_fontId ); | |
203 | return s; | |
204 | }; | |
205 | ||
206 | int wxFont::GetFamily(void) const | |
207 | { | |
208 | return M_FONTDATA->m_family; | |
209 | }; | |
210 | ||
211 | wxString wxFont::GetFamilyString(void) const | |
212 | { | |
213 | wxString s = wx_font_family[M_FONTDATA->m_family]; | |
214 | return s; | |
215 | }; | |
216 | ||
217 | int wxFont::GetFontId(void) const | |
218 | { | |
219 | return M_FONTDATA->m_fontId; // stub | |
220 | }; | |
221 | ||
222 | int wxFont::GetStyle(void) const | |
223 | { | |
224 | return M_FONTDATA->m_style; | |
225 | }; | |
226 | ||
227 | wxString wxFont::GetStyleString(void) const | |
228 | { | |
229 | wxString s = wx_font_style[M_FONTDATA->m_style]; | |
230 | return s; | |
231 | }; | |
232 | ||
233 | int wxFont::GetWeight(void) const | |
234 | { | |
235 | return M_FONTDATA->m_weight; | |
236 | }; | |
237 | ||
238 | wxString wxFont::GetWeightString(void) const | |
239 | { | |
240 | wxString s = wx_font_weight[M_FONTDATA->m_weight]; | |
241 | return s; | |
242 | }; | |
243 | ||
244 | bool wxFont::GetUnderlined(void) const | |
245 | { | |
246 | return M_FONTDATA->m_underlined; | |
247 | }; | |
248 | ||
249 | //----------------------------------------------------------------------------- | |
250 | // get internal representation of font | |
251 | //----------------------------------------------------------------------------- | |
252 | ||
253 | // local help function | |
254 | static GdkFont *wxLoadQueryNearestFont(int point_size, int fontid, | |
255 | int style, int weight, | |
256 | bool underlined); | |
257 | ||
258 | GdkFont *wxFont::GetInternalFont(float scale) | |
259 | { | |
260 | if (M_FONTDATA->m_byXFontName) return M_FONTDATA->m_font; | |
261 | ||
262 | long int_scale = long(scale * 100.0 + 0.5); // key for fontlist | |
263 | int point_scale = (M_FONTDATA->m_pointSize * 10 * int_scale) / 100; | |
264 | GdkFont *font = NULL; | |
265 | ||
266 | wxNode *node = M_FONTDATA->m_scaled_xfonts.Find(int_scale); | |
267 | if (node) | |
268 | { | |
269 | font = (GdkFont*)node->Data(); | |
270 | } | |
271 | else | |
272 | { | |
273 | font = wxLoadQueryNearestFont( point_scale, M_FONTDATA->m_fontId, M_FONTDATA->m_style, | |
274 | M_FONTDATA->m_weight, M_FONTDATA->m_underlined ); | |
275 | M_FONTDATA->m_scaled_xfonts.Append( int_scale, (wxObject*)font ); | |
276 | }; | |
277 | if (!font) | |
278 | printf("could not load any font"); | |
279 | // wxError("could not load any font", "wxFont"); | |
280 | return font; | |
281 | }; | |
282 | ||
283 | //----------------------------------------------------------------------------- | |
284 | // local utilities to find a X font | |
285 | //----------------------------------------------------------------------------- | |
286 | ||
287 | static GdkFont *wxLoadQueryFont(int point_size, int fontid, int style, | |
288 | int weight, bool WXUNUSED(underlined)) | |
289 | { | |
290 | char buffer[512]; | |
291 | char *name = wxTheFontNameDirectory.GetScreenName( fontid, weight, style ); | |
292 | ||
293 | if (!name) | |
294 | name = "-*-*-*-*-*-*-*-%d-*-*-*-*-*-*"; | |
295 | sprintf(buffer, name, point_size); | |
296 | ||
297 | return gdk_font_load( buffer ); | |
298 | } | |
299 | ||
300 | static GdkFont *wxLoadQueryNearestFont(int point_size, int fontid, | |
301 | int style, int weight, | |
302 | bool underlined) | |
303 | { | |
304 | GdkFont *font; | |
305 | ||
306 | font = wxLoadQueryFont( point_size, fontid, style, weight, underlined ); | |
307 | ||
308 | if (!font) { | |
309 | // search up and down by stepsize 10 | |
310 | int max_size = point_size + 20 * (1 + (point_size/180)); | |
311 | int min_size = point_size - 20 * (1 + (point_size/180)); | |
312 | int i; | |
313 | ||
314 | // Search for smaller size (approx.) | |
315 | for (i=point_size-10; !font && i >= 10 && i >= min_size; i -= 10) | |
316 | font = wxLoadQueryFont(i, fontid, style, weight, underlined); | |
317 | // Search for larger size (approx.) | |
318 | for (i=point_size+10; !font && i <= max_size; i += 10) | |
319 | font = wxLoadQueryFont(i, fontid, style, weight, underlined); | |
320 | // Try default family | |
321 | if (!font && fontid != wxDEFAULT) | |
322 | font = wxLoadQueryFont(point_size, wxDEFAULT, style, | |
323 | weight, underlined); | |
324 | // Bogus font | |
325 | if (!font) | |
326 | font = wxLoadQueryFont(120, wxDEFAULT, wxNORMAL, wxNORMAL, | |
327 | underlined); | |
328 | } | |
329 | return font; | |
330 | } | |
331 | ||
332 | //----------------------------------------------------------------------------- | |
333 | // face names and index functions | |
334 | //----------------------------------------------------------------------------- | |
335 | ||
336 | static char *font_defaults[] = { | |
337 | "FamilyDefault", "Default", | |
338 | "FamilyRoman", "Roman", | |
339 | "FamilyDecorative", "Decorative", | |
340 | "FamilyModern", "Modern", | |
341 | "FamilyTeletype", "Teletype", | |
342 | "FamilySwiss", "Swiss", | |
343 | "FamilyScript", "Script", | |
344 | ||
345 | "AfmMedium", "", | |
346 | "AfmBold", "Bo", | |
347 | "AfmLight", "", | |
348 | "AfmStraight", "", | |
349 | "AfmItalic", "${AfmSlant}", | |
350 | "AfmSlant", "O", | |
351 | "AfmRoman", "Ro", | |
352 | "AfmTimes", "Times", | |
353 | "AfmHelvetica", "Helv", | |
354 | "AfmCourier", "Cour", | |
355 | ||
356 | "Afm___", "${AfmTimes,$[weight],$[style]}", | |
357 | ||
358 | "AfmTimes__", "${AfmTimes}${Afm$[weight]}${Afm$[style]}", | |
359 | "AfmTimesMediumStraight", "${AfmTimes}${AfmRoman}", | |
360 | "AfmTimesLightStraight", "${AfmTimes}${AfmRoman}", | |
361 | "AfmTimes_Italic", "${AfmTimes}$[weight]${AfmItalic}", | |
362 | "AfmTimes_Slant", "${AfmTimes}$[weight]${AfmItalic}", | |
363 | ||
364 | "AfmSwiss__", "${AfmHelvetica}${Afm$[weight]}${Afm$[style]}", | |
365 | "AfmModern__", "${AfmCourier}${Afm$[weight]}${Afm$[style]}", | |
366 | ||
367 | "AfmTeletype__", "${AfmModern,$[weight],$[style]}", | |
368 | ||
369 | "PostScriptMediumStraight", "", | |
370 | "PostScriptMediumItalic", "-Oblique", | |
371 | "PostScriptMediumSlant", "-Oblique", | |
372 | "PostScriptLightStraight", "", | |
373 | "PostScriptLightItalic", "-Oblique", | |
374 | "PostScriptLightSlant", "-Oblique", | |
375 | "PostScriptBoldStraight", "-Bold", | |
376 | "PostScriptBoldItalic", "-BoldOblique", | |
377 | "PostScriptBoldSlant", "-BoldOblique", | |
378 | ||
379 | #if WX_NORMALIZED_PS_FONTS | |
380 | "PostScript___", "${PostScriptTimes,$[weight],$[style]}", | |
381 | #else | |
382 | "PostScriptRoman__", "${PostScriptTimes,$[weight],$[style]}", | |
383 | "PostScript___", "LucidaSans${PostScript$[weight]$[style]}", | |
384 | #endif | |
385 | ||
386 | "PostScriptTimesMedium", "", | |
387 | "PostScriptTimesLight", "", | |
388 | "PostScriptTimesBold", "Bold", | |
389 | ||
390 | "PostScriptTimes__", "Times${PostScript$[weight]$[style]}", | |
391 | "PostScriptTimesMediumStraight", "Times-Roman", | |
392 | "PostScriptTimesLightStraight", "Times-Roman", | |
393 | "PostScriptTimes_Slant", "Times-${PostScriptTimes$[weight]}Italic", | |
394 | "PostScriptTimes_Italic", "Times-${PostScriptTimes$[weight]}Italic", | |
395 | ||
396 | "PostScriptSwiss__", "Helvetica${PostScript$[weight]$[style]}", | |
397 | "PostScriptModern__", "Courier${PostScript$[weight]$[style]}", | |
398 | ||
399 | "PostScriptTeletype__", "${PostScriptModern,$[weight],$[style]}", | |
400 | ||
401 | #if !WX_NORMALIZED_PS_FONTS | |
402 | "PostScriptScript__", "Zapf-Chancery-MediumItalic", | |
403 | #endif | |
404 | ||
405 | "ScreenMedium", "medium", | |
406 | "ScreenBold", "bold", | |
407 | "ScreenLight", "light", | |
408 | "ScreenStraight", "r", | |
409 | "ScreenItalic", "i", | |
410 | "ScreenSlant", "o", | |
411 | ||
412 | "ScreenDefaultBase", "misc-fixed", | |
413 | "ScreenRomanBase", "*-times", | |
414 | "ScreenDecorativeBase", "*-helvetica", | |
415 | "ScreenModernBase", "*-courier", | |
416 | "ScreenTeletypeBase", "*-lucidatypewriter", | |
417 | "ScreenSwissBase", "*-lucida", | |
418 | "ScreenScriptBase", "*-zapfchancery", | |
419 | ||
420 | "ScreenStdSuffix", "-${Screen$[weight]}-${Screen$[style]}" | |
421 | "-normal-*-*-%d-*-*-*-*-*-*", | |
422 | ||
423 | "Screen___", | |
424 | "-${ScreenDefaultBase}${ScreenStdSuffix}", | |
425 | "ScreenRoman__", | |
426 | "-${ScreenRomanBase}${ScreenStdSuffix}", | |
427 | "ScreenDecorative__", | |
428 | "-${ScreenDecorativeBase}${ScreenStdSuffix}", | |
429 | "ScreenModern__", | |
430 | "-${ScreenModernBase}${ScreenStdSuffix}", | |
431 | "ScreenTeletype__", | |
432 | "-${ScreenTeletypeBase}${ScreenStdSuffix}", | |
433 | "ScreenSwiss__", | |
434 | "-${ScreenSwissBase}${ScreenStdSuffix}", | |
435 | "ScreenScript__", | |
436 | "-${ScreenScriptBase}${ScreenStdSuffix}", | |
437 | NULL | |
438 | }; | |
439 | ||
440 | enum {wxWEIGHT_NORMAL, wxWEIGHT_BOLD, wxWEIGHT_LIGHT, wxNUM_WEIGHTS}; | |
441 | enum {wxSTYLE_NORMAL, wxSTYLE_ITALIC, wxSTYLE_SLANT, wxNUM_STYLES}; | |
442 | ||
443 | static int WCoordinate(int w) | |
444 | { | |
445 | switch (w) { | |
446 | case wxBOLD: return wxWEIGHT_BOLD; | |
447 | case wxLIGHT: return wxWEIGHT_LIGHT; | |
448 | case wxNORMAL: | |
449 | default: return wxWEIGHT_NORMAL; | |
450 | } | |
451 | } | |
452 | ||
453 | static int SCoordinate(int s) | |
454 | { | |
455 | switch (s) { | |
456 | case wxITALIC: return wxSTYLE_ITALIC; | |
457 | case wxSLANT: return wxSTYLE_SLANT; | |
458 | case wxNORMAL: | |
459 | default: return wxSTYLE_NORMAL; | |
460 | } | |
461 | } | |
462 | ||
463 | //----------------------------------------------------------------------------- | |
464 | // wxSuffixMap | |
465 | //----------------------------------------------------------------------------- | |
466 | ||
467 | class wxSuffixMap { | |
468 | public: | |
469 | ~wxSuffixMap(void); | |
470 | ||
471 | inline char *GetName(int weight, int style) | |
472 | { | |
473 | return ( map [WCoordinate(weight)] [SCoordinate(style)] ); | |
474 | } | |
475 | ||
476 | char *map[wxNUM_WEIGHTS][wxNUM_STYLES]; | |
477 | void Initialize(const char *, const char *); | |
478 | }; | |
479 | ||
480 | //#if !USE_RESOURCES | |
481 | #define wxGetResource(a, b, c) 0 | |
482 | //#endif | |
483 | ||
484 | static void SearchResource(const char *prefix, const char **names, int count, char **v) | |
485 | { | |
486 | int k, i, j; | |
487 | char resource[1024], **defaults, *internal; | |
488 | ||
489 | k = 1 << count; | |
490 | ||
491 | *v = NULL; | |
492 | internal = NULL; | |
493 | ||
494 | for (i = 0; i < k; i++) { | |
495 | strcpy(resource, prefix); | |
496 | for (j = 0; j < count; j++) { | |
497 | if (!(i & (1 << j))) | |
498 | strcat(resource, names[j]); | |
499 | else | |
500 | strcat(resource, "_"); | |
501 | } | |
502 | if (wxGetResource(wxAPP_CLASS, (char *)resource, v)) | |
503 | return; | |
504 | if (!internal) { | |
505 | defaults = font_defaults; | |
506 | while (*defaults) { | |
507 | if (!strcmp(*defaults, resource)) { | |
508 | internal = defaults[1]; | |
509 | break; | |
510 | } | |
511 | defaults += 2; | |
512 | } | |
513 | } | |
514 | } | |
515 | if (internal) | |
516 | *v = copystring(internal); | |
517 | } | |
518 | ||
519 | wxSuffixMap::~wxSuffixMap(void) | |
520 | { | |
521 | int k, j; | |
522 | ||
523 | for (k = 0; k < wxNUM_WEIGHTS; ++k) | |
524 | for (j = 0; j < wxNUM_STYLES; ++j) | |
525 | if (map[k][j]) { | |
526 | delete[] map[k][j]; | |
527 | map[k][j] = NULL; | |
528 | } | |
529 | } | |
530 | ||
531 | void wxSuffixMap::Initialize(const char *resname, const char *devresname) | |
532 | { | |
533 | const char *weight, *style; | |
534 | char *v; | |
535 | int i, j, k; | |
536 | const char *names[3]; | |
537 | ||
538 | for (k = 0; k < wxNUM_WEIGHTS; k++) { | |
539 | switch (k) { | |
540 | case wxWEIGHT_NORMAL: weight = "Medium"; break; | |
541 | case wxWEIGHT_LIGHT: weight = "Light"; break; | |
542 | case wxWEIGHT_BOLD: | |
543 | default: weight = "Bold"; | |
544 | } | |
545 | for (j = 0; j < wxNUM_STYLES; j++) { | |
546 | switch (j) { | |
547 | case wxSTYLE_NORMAL: style = "Straight"; break; | |
548 | case wxSTYLE_ITALIC: style = "Italic"; break; | |
549 | case wxSTYLE_SLANT: | |
550 | default: style = "Slant"; | |
551 | } | |
552 | names[0] = resname; | |
553 | names[1] = weight; | |
554 | names[2] = style; | |
555 | ||
556 | SearchResource(devresname, names, 3, &v); | |
557 | ||
558 | /* Expand macros in the found string: */ | |
559 | found: | |
560 | int len, closer = 0, startpos = 0; | |
561 | ||
562 | len = (v ? strlen(v) : 0); | |
563 | for (i = 0; i < len; i++) { | |
564 | if (v[i] == '$' && ((v[i+1] == '[') || (v[i+1] == '{'))) { | |
565 | startpos = i; | |
566 | closer = (v[i+1] == '[') ? ']' : '}'; | |
567 | ++i; | |
568 | } else if (v[i] == closer) { | |
569 | int newstrlen; | |
570 | const char *r = NULL; bool delete_r = FALSE; | |
571 | char *name; | |
572 | ||
573 | name = v + startpos + 2; | |
574 | v[i] = 0; | |
575 | ||
576 | if (closer == '}') { | |
577 | int i, count, len; | |
578 | char **names; | |
579 | ||
580 | for (i = 0, count = 1; name[i]; i++) | |
581 | if (name[i] == ',') | |
582 | count++; | |
583 | ||
584 | len = i; | |
585 | ||
586 | names = new char*[count]; | |
587 | names[0] = name; | |
588 | for (i = 0, count = 1; i < len; i++) | |
589 | if (name[i] == ',') { | |
590 | names[count++] = name + i + 1; | |
591 | name[i] = 0; | |
592 | } | |
593 | ||
594 | SearchResource("", (const char **)names, count, (char **)&r); | |
595 | delete_r = (r != 0); | |
596 | delete[] names; | |
597 | ||
598 | if (!r) { | |
599 | for (i = 0; i < len; i++) | |
600 | if (!name[i]) | |
601 | name[i] = ','; | |
602 | r = ""; | |
603 | printf("Bad resource name \"%s\" in font lookup\n", name); | |
604 | } | |
605 | } else if (!strcmp(name, "weight")) { | |
606 | r = weight; | |
607 | } else if (!strcmp(name, "style")) { | |
608 | r = style; | |
609 | } else if (!strcmp(name, "family")) { | |
610 | r = resname; | |
611 | } else { | |
612 | r = ""; | |
613 | printf("Bad font macro name \"%s\"\n", name); | |
614 | } | |
615 | ||
616 | // add r to v | |
617 | newstrlen = strlen(r); | |
618 | char *naya = new char[startpos + newstrlen + len - i]; | |
619 | memcpy(naya, v, startpos); | |
620 | memcpy(naya + startpos, r, newstrlen); | |
621 | memcpy(naya + startpos + newstrlen, v + i + 1, len - i); | |
622 | if (delete_r) | |
623 | delete[] (char*)r; | |
624 | delete[] v; | |
625 | v = naya; | |
626 | ||
627 | goto found; | |
628 | } | |
629 | } | |
630 | /* We have a final value: */ | |
631 | map[k][j] = v; | |
632 | } | |
633 | } | |
634 | } | |
635 | ||
636 | //----------------------------------------------------------------------------- | |
637 | // wxFontNameItem | |
638 | //----------------------------------------------------------------------------- | |
639 | ||
640 | class wxFontNameItem : public wxObject { | |
641 | DECLARE_DYNAMIC_CLASS(wxFontNameItem) | |
642 | public: | |
643 | wxFontNameItem(const char *name, int id, int family); | |
644 | ~wxFontNameItem(); | |
645 | ||
646 | inline char* GetScreenName(int w, int s) {return screen.GetName(w, s);} | |
647 | inline char* GetPostScriptName(int w, int s) {return printing.GetName(w, s);} | |
648 | inline char* GetAFMName(int w, int s) {return afm.GetName(w, s);} | |
649 | inline char* GetName(void) {return name;} | |
650 | inline int GetFamily(void) {return family;} | |
651 | inline int GetId(void) {return id;} | |
652 | inline bool IsRoman(void) {return isroman;} | |
653 | #if WXDEBUG | |
654 | void Dump(ostream& str); | |
655 | #endif | |
656 | ||
657 | int id; | |
658 | int family; | |
659 | char *name; | |
660 | wxSuffixMap screen, printing, afm; | |
661 | bool isroman; | |
662 | }; | |
663 | ||
664 | IMPLEMENT_ABSTRACT_CLASS(wxFontNameItem, wxObject) | |
665 | ||
666 | wxFontNameItem::wxFontNameItem(const char *Name, int Id, int Family) | |
667 | { | |
668 | name = copystring(Name); | |
669 | id = Id; | |
670 | family = Family; | |
671 | ||
672 | screen. Initialize(name, "Screen"); | |
673 | printing.Initialize(name, "PostScript"); | |
674 | afm. Initialize(name, "Afm"); | |
675 | } | |
676 | ||
677 | wxFontNameItem::~wxFontNameItem(void) | |
678 | { | |
679 | if (name) | |
680 | delete[] name; | |
681 | name = NULL; | |
682 | } | |
683 | ||
684 | #if WXDEBUG | |
685 | void wxFontNameItem::Dump(ostream& str) | |
686 | { | |
687 | str << "wxFontNameItem(" << name << ")"; | |
688 | } | |
689 | #endif | |
690 | ||
691 | //----------------------------------------------------------------------------- | |
692 | // wxFontDirectory | |
693 | //----------------------------------------------------------------------------- | |
694 | ||
695 | IMPLEMENT_DYNAMIC_CLASS(wxFontNameDirectory, wxObject) | |
696 | ||
697 | wxFontNameDirectory::wxFontNameDirectory(void) | |
698 | { | |
699 | table = new wxHashTable(wxKEY_INTEGER, 20); | |
700 | nextFontId = -1; | |
701 | Initialize(); | |
702 | } | |
703 | ||
704 | wxFontNameDirectory::~wxFontNameDirectory() | |
705 | { | |
706 | // Cleanup wxFontNameItems allocated | |
707 | table->BeginFind(); | |
708 | wxNode *node = table->Next(); | |
709 | while (node) { | |
710 | wxFontNameItem *item = (wxFontNameItem*)node->Data(); | |
711 | delete item; | |
712 | node = table->Next(); | |
713 | } | |
714 | delete table; | |
715 | } | |
716 | ||
717 | int wxFontNameDirectory::GetNewFontId(void) | |
718 | { | |
719 | return (nextFontId--); | |
720 | } | |
721 | ||
722 | void wxFontNameDirectory::Initialize() | |
723 | { | |
724 | Initialize(wxDEFAULT, wxDEFAULT, "Default"); | |
725 | Initialize(wxDECORATIVE, wxDECORATIVE, "Decorative"); | |
726 | Initialize(wxROMAN, wxROMAN, "Roman"); | |
727 | Initialize(wxMODERN, wxMODERN, "Modern"); | |
728 | Initialize(wxTELETYPE, wxTELETYPE, "Teletype"); | |
729 | Initialize(wxSWISS, wxSWISS, "Swiss"); | |
730 | Initialize(wxSCRIPT, wxSCRIPT, "Script"); | |
731 | } | |
732 | ||
733 | void wxFontNameDirectory::Initialize(int fontid, int family, const char *resname) | |
734 | { | |
735 | char *fam, resource[256]; | |
736 | ||
737 | sprintf(resource, "Family%s", resname); | |
738 | SearchResource((const char *)resource, NULL, 0, (char **)&fam); | |
739 | if (fam) { | |
740 | if (!strcmp(fam, "Default")) family = wxDEFAULT; | |
741 | else if (!strcmp(fam, "Roman")) family = wxROMAN; | |
742 | else if (!strcmp(fam, "Decorative")) family = wxDECORATIVE; | |
743 | else if (!strcmp(fam, "Modern")) family = wxMODERN; | |
744 | else if (!strcmp(fam, "Teletype")) family = wxTELETYPE; | |
745 | else if (!strcmp(fam, "Swiss")) family = wxSWISS; | |
746 | else if (!strcmp(fam, "Script")) family = wxSCRIPT; | |
747 | delete[] fam; // free resource | |
748 | } | |
749 | table->Put(fontid, new wxFontNameItem(resname, fontid, family)); | |
750 | } | |
751 | ||
752 | int wxFontNameDirectory::FindOrCreateFontId(const char *name, int family) | |
753 | { | |
754 | int id; | |
755 | ||
756 | // font exists -> return id | |
757 | if ( (id = GetFontId(name)) ) return id; | |
758 | // create new font | |
759 | Initialize(id=GetNewFontId(), family, name); | |
760 | return id; | |
761 | } | |
762 | ||
763 | char *wxFontNameDirectory::GetScreenName(int fontid, int weight, int style) | |
764 | { | |
765 | wxFontNameItem *item = (wxFontNameItem*)table->Get(fontid); // find font | |
766 | if (item) | |
767 | return item->GetScreenName(weight, style); | |
768 | // font does not exist | |
769 | return NULL; | |
770 | } | |
771 | ||
772 | char *wxFontNameDirectory::GetPostScriptName(int fontid, int weight, int style) | |
773 | { | |
774 | wxFontNameItem *item = (wxFontNameItem*)table->Get(fontid); // find font | |
775 | if (item) | |
776 | return item->GetPostScriptName(weight, style); | |
777 | // font does not exist | |
778 | return NULL; | |
779 | } | |
780 | ||
781 | char *wxFontNameDirectory::GetAFMName(int fontid, int weight, int style) | |
782 | { | |
783 | wxFontNameItem *item = (wxFontNameItem *)table->Get(fontid); // find font | |
784 | if (item) | |
785 | return item->GetAFMName(weight, style); | |
786 | // font does not exist | |
787 | return NULL; | |
788 | } | |
789 | ||
790 | char *wxFontNameDirectory::GetFontName(int fontid) | |
791 | { | |
792 | wxFontNameItem *item = (wxFontNameItem *)table->Get(fontid); // find font | |
793 | if (item) | |
794 | return item->GetName(); | |
795 | // font does not exist | |
796 | return NULL; | |
797 | } | |
798 | ||
799 | int wxFontNameDirectory::GetFontId(const char *name) | |
800 | { | |
801 | wxNode *node; | |
802 | ||
803 | table->BeginFind(); | |
804 | ||
805 | while ( (node = table->Next()) ) { | |
806 | wxFontNameItem *item = (wxFontNameItem*)node->Data(); | |
807 | if (!strcmp(name, item->name)) | |
808 | return item->id; | |
809 | } | |
810 | // font does not exist | |
811 | return 0; | |
812 | } | |
813 | ||
814 | int wxFontNameDirectory::GetFamily(int fontid) | |
815 | { | |
816 | wxFontNameItem *item = (wxFontNameItem *)table->Get(fontid); | |
817 | ||
818 | if (item) | |
819 | return item->family; | |
820 | // font does not exist | |
821 | return wxDEFAULT; | |
822 | } |