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