]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/font.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "font.h"
17 #include "wx/gdicmn.h"
18 #include "wx/tokenzr.h"
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
28 extern wxFontNameDirectory *wxTheFontNameDirectory;
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 class wxFontRefData
: public wxObjectRefData
40 wxFontRefData( const wxFontRefData
& data
);
43 wxList m_scaled_xfonts
;
45 int m_family
, m_style
, m_weight
;
55 wxFontRefData::wxFontRefData() : m_scaled_xfonts(wxKEY_INTEGER
)
57 m_byXFontName
= FALSE
;
63 m_font
= (GdkFont
*) NULL
;
66 wxFontRefData::wxFontRefData( const wxFontRefData
& data
) : m_scaled_xfonts(wxKEY_INTEGER
)
68 m_byXFontName
= FALSE
;
69 m_pointSize
= data
.m_pointSize
;
70 m_family
= data
.m_family
;
71 m_style
= data
.m_style
;
72 m_weight
= data
.m_weight
;
73 m_underlined
= data
.m_underlined
;
74 m_faceName
= data
.m_faceName
;
75 m_font
= (GdkFont
*) NULL
;
76 if (data
.m_font
) m_font
= gdk_font_ref( data
.m_font
);
79 wxFontRefData::~wxFontRefData()
81 wxNode
*node
= m_scaled_xfonts
.First();
84 GdkFont
*font
= (GdkFont
*)node
->Data();
85 wxNode
*next
= node
->Next();
86 gdk_font_unref( font
);
89 if (m_font
) gdk_font_unref( m_font
);
92 //-----------------------------------------------------------------------------
94 #define M_FONTDATA ((wxFontRefData *)m_refData)
96 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
100 if (wxTheFontList
) wxTheFontList
->Append( this );
103 wxFont::wxFont( GdkFont
*font
, char *xFontName
)
105 if (!xFontName
) return;
107 m_refData
= new wxFontRefData();
109 // M_FONTDATA->m_byXFontName = TRUE;
110 M_FONTDATA
->m_font
= font
;
114 wxString
fontname( xFontName
);
115 wxStringTokenizer
tn( fontname
, _T("-") );
117 tn
.GetNextToken(); // foundry
119 M_FONTDATA
->m_faceName
= tn
.GetNextToken(); // courier
121 tmp
= tn
.GetNextToken().MakeUpper();
122 if (tmp
== _T("BOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
124 tmp
= tn
.GetNextToken().MakeUpper();
125 if (tmp
== _T("I")) M_FONTDATA
->m_style
= wxITALIC
;
126 if (tmp
== _T("O")) M_FONTDATA
->m_style
= wxITALIC
;
128 tn
.GetNextToken(); // set width
129 tn
.GetNextToken(); // ?
130 tn
.GetNextToken(); // pixel size
132 tmp
= tn
.GetNextToken(); // pointsize
133 int num
= wxStrtol (tmp
.c_str(), (wxChar
**) NULL
, 10);
134 M_FONTDATA
->m_pointSize
= num
/ 10;
136 tn
.GetNextToken(); // x-res
137 tn
.GetNextToken(); // y-res
139 tmp
= tn
.GetNextToken().MakeUpper();
140 if (tmp
== _T("M")) M_FONTDATA
->m_family
= wxMODERN
;
141 else if (M_FONTDATA
->m_faceName
== _T("TIMES")) M_FONTDATA
->m_family
= wxROMAN
;
142 else if (M_FONTDATA
->m_faceName
== _T("HELVETICA")) M_FONTDATA
->m_family
= wxSWISS
;
143 else if (M_FONTDATA
->m_faceName
== _T("LUCIDATYPEWRITER")) M_FONTDATA
->m_family
= wxTELETYPE
;
144 else if (M_FONTDATA
->m_faceName
== _T("LUCIDA")) M_FONTDATA
->m_family
= wxDECORATIVE
;
145 else if (M_FONTDATA
->m_faceName
== _T("UTOPIA")) M_FONTDATA
->m_family
= wxSCRIPT
;
148 wxFont::wxFont( int pointSize
, int family
, int style
, int weight
, bool underlined
, const wxString
& face
)
150 m_refData
= new wxFontRefData();
152 if (family
== wxDEFAULT
)
153 M_FONTDATA
->m_family
= wxSWISS
;
155 M_FONTDATA
->m_family
= family
;
157 if (!face
.IsEmpty()) M_FONTDATA
->m_faceName
= face
;
159 if (style
== wxDEFAULT
)
160 M_FONTDATA
->m_style
= wxNORMAL
;
162 M_FONTDATA
->m_style
= style
;
164 if (weight
== wxDEFAULT
)
165 M_FONTDATA
->m_weight
= wxNORMAL
;
167 M_FONTDATA
->m_weight
= weight
;
169 if (pointSize
== wxDEFAULT
)
170 M_FONTDATA
->m_pointSize
= 12;
172 M_FONTDATA
->m_pointSize
= pointSize
;
174 M_FONTDATA
->m_underlined
= underlined
;
176 if (wxTheFontList
) wxTheFontList
->Append( this );
180 wxFont::wxFont( const wxFont
& font
)
184 if (wxTheFontList
) wxTheFontList
->Append( this );
189 if (wxTheFontList
) wxTheFontList
->DeleteObject( this );
192 wxFont
& wxFont::operator = ( const wxFont
& font
)
194 if (*this == font
) return (*this);
199 bool wxFont::operator == ( const wxFont
& font
) const
201 return m_refData
== font
.m_refData
;
204 bool wxFont::operator != ( const wxFont
& font
) const
206 return m_refData
!= font
.m_refData
;
209 bool wxFont::Ok() const
211 return (m_refData
!= NULL
);
214 int wxFont::GetPointSize() const
216 wxCHECK_MSG( Ok(), 0, _T("invalid font") );
218 return M_FONTDATA
->m_pointSize
;
221 wxString
wxFont::GetFaceName() const
223 wxCHECK_MSG( Ok(), _T(""), _T("invalid font") );
225 return M_FONTDATA
->m_faceName
;
228 int wxFont::GetFamily() const
230 wxCHECK_MSG( Ok(), 0, _T("invalid font") );
232 return M_FONTDATA
->m_family
;
235 wxString
wxFont::GetFamilyString() const
237 wxCHECK_MSG( Ok(), _T("wxDEFAULT"), _T("invalid font") );
239 switch (M_FONTDATA
->m_family
)
241 case wxDECORATIVE
: return wxString(_T("wxDECORATIVE"));
242 case wxROMAN
: return wxString(_T("wxROMAN"));
243 case wxSCRIPT
: return wxString(_T("wxSCRIPT"));
244 case wxSWISS
: return wxString(_T("wxSWISS"));
245 case wxMODERN
: return wxString(_T("wxMODERN"));
246 case wxTELETYPE
: return wxString(_T("wxTELETYPE"));
247 default: return _T("wxDEFAULT");
253 int wxFont::GetStyle() const
255 wxCHECK_MSG( Ok(), 0, _T("invalid font") );
257 return M_FONTDATA
->m_style
;
260 wxString
wxFont::GetStyleString() const
262 wxCHECK_MSG( Ok(), _T("wxDEFAULT"), _T("invalid font") );
264 switch (M_FONTDATA
->m_style
)
266 case wxNORMAL
: return wxString(_T("wxNORMAL"));
267 case wxSLANT
: return wxString(_T("wxSLANT"));
268 case wxITALIC
: return wxString(_T("wxITALIC"));
269 default: return wxString(_T("wxDEFAULT"));
272 return wxString(_T("wxDEFAULT"));
275 int wxFont::GetWeight() const
277 wxCHECK_MSG( Ok(), 0, _T("invalid font") );
279 return M_FONTDATA
->m_weight
;
282 wxString
wxFont::GetWeightString() const
284 wxCHECK_MSG( Ok(), _T("wxDEFAULT"), _T("invalid font") );
286 switch (M_FONTDATA
->m_weight
)
288 case wxNORMAL
: return wxString(_T("wxNORMAL"));
289 case wxBOLD
: return wxString(_T("wxBOLD"));
290 case wxLIGHT
: return wxString(_T("wxLIGHT"));
291 default: return wxString(_T("wxDEFAULT"));
294 return wxString(_T("wxDEFAULT"));
297 bool wxFont::GetUnderlined() const
299 wxCHECK_MSG( Ok(), FALSE
, _T("invalid font") );
301 return M_FONTDATA
->m_underlined
;
304 void wxFont::Unshare()
308 m_refData
= new wxFontRefData();
312 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
318 void wxFont::SetPointSize(int pointSize
)
322 M_FONTDATA
->m_pointSize
= pointSize
;
325 void wxFont::SetFamily(int family
)
329 M_FONTDATA
->m_family
= family
;
332 void wxFont::SetStyle(int style
)
336 M_FONTDATA
->m_style
= style
;
339 void wxFont::SetWeight(int weight
)
343 M_FONTDATA
->m_weight
= weight
;
346 void wxFont::SetFaceName(const wxString
& faceName
)
350 M_FONTDATA
->m_faceName
= faceName
;
353 void wxFont::SetUnderlined(bool underlined
)
357 M_FONTDATA
->m_underlined
= underlined
;
360 //-----------------------------------------------------------------------------
361 // get internal representation of font
362 //-----------------------------------------------------------------------------
364 static GdkFont
*wxLoadQueryNearestFont( int point_size
, int family
, int style
, int weight
,
365 bool underlined
, const wxString
&facename
);
367 GdkFont
*wxFont::GetInternalFont( float scale
) const
371 wxFAIL_MSG( _T("invalid font") );
372 return (GdkFont
*) NULL
;
375 /* short cut if the special X font constructor has been used */
376 if (M_FONTDATA
->m_byXFontName
) return M_FONTDATA
->m_font
;
378 long int_scale
= long(scale
* 100.0 + 0.5); /* key for fontlist */
379 int point_scale
= (M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100;
380 GdkFont
*font
= (GdkFont
*) NULL
;
382 wxNode
*node
= M_FONTDATA
->m_scaled_xfonts
.Find(int_scale
);
385 font
= (GdkFont
*)node
->Data();
390 if ((int_scale == 100) &&
391 (M_FONTDATA->m_family == wxSWISS) &&
392 (M_FONTDATA->m_style == wxNORMAL) &&
393 (M_FONTDATA->m_pointSize == 12) &&
394 (M_FONTDATA->m_weight == wxNORMAL) &&
395 (M_FONTDATA->m_underlined == FALSE))
397 font = gdk_font_load( "-adobe-helvetica-medium-r-normal--*-120-*-*-*-*-*-*" );
402 font
= wxLoadQueryNearestFont( point_scale
, M_FONTDATA
->m_family
, M_FONTDATA
->m_style
,
403 M_FONTDATA
->m_weight
, M_FONTDATA
->m_underlined
, M_FONTDATA
->m_faceName
);
405 M_FONTDATA
->m_scaled_xfonts
.Append( int_scale
, (wxObject
*)font
);
410 wxLogError(_T("could not load any font"));
416 //-----------------------------------------------------------------------------
417 // local utilities to find a X font
418 //-----------------------------------------------------------------------------
420 static GdkFont
*wxLoadQueryFont( int pointSize
, int family
, int style
, int weight
,
421 bool underlined
, const wxString
&facename
)
423 wxChar
*xfamily
= (wxChar
*) NULL
;
424 wxChar
*xstyle
= (wxChar
*) NULL
;
425 wxChar
*xweight
= (wxChar
*) NULL
;
429 case wxDECORATIVE
: xfamily
= _T("lucida"); break;
430 case wxROMAN
: xfamily
= _T("times"); break;
431 case wxMODERN
: xfamily
= _T("courier"); break;
432 case wxSWISS
: xfamily
= _T("helvetica"); break;
433 case wxTELETYPE
: xfamily
= _T("lucidatypewriter"); break;
434 case wxSCRIPT
: xfamily
= _T("utopia"); break;
435 default: xfamily
= _T("*");
438 if (!facename
.IsEmpty())
440 wxSprintf( wxBuffer
, _T("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"), facename
.c_str() );
441 GdkFont
*test
= gdk_font_load( wxConvCurrent
->cWX2MB(wxBuffer
) );
444 gdk_font_unref( test
);
445 xfamily
= WXSTRINGCAST facename
;
451 case wxITALIC
: xstyle
= _T("i"); break;
452 case wxSLANT
: xstyle
= _T("o"); break;
453 case wxNORMAL
: xstyle
= _T("r"); break;
454 default: xstyle
= _T("*"); break;
458 case wxBOLD
: xweight
= _T("bold"); break;
460 case wxNORMAL
: xweight
= _T("medium"); break;
461 default: xweight
= _T("*"); break;
464 wxSprintf( wxBuffer
, _T("-*-%s-%s-%s-normal-*-*-%d-*-*-*-*-*-*"),
465 xfamily
, xweight
, xstyle
, pointSize
);
467 return gdk_font_load( wxConvCurrent
->cWX2MB(wxBuffer
) );
470 static GdkFont
*wxLoadQueryNearestFont( int point_size
, int family
, int style
, int weight
,
471 bool underlined
, const wxString
&facename
)
473 GdkFont
*font
= wxLoadQueryFont( point_size
, family
, style
, weight
, underlined
, facename
);
477 /* search up and down by stepsize 10 */
478 int max_size
= point_size
+ 20 * (1 + (point_size
/180));
479 int min_size
= point_size
- 20 * (1 + (point_size
/180));
483 /* Search for smaller size (approx.) */
484 for (i
=point_size
-10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10)
485 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
, facename
);
487 /* Search for larger size (approx.) */
488 for (i
=point_size
+10; !font
&& i
<= max_size
; i
+= 10)
489 font
= wxLoadQueryFont( i
, family
, style
, weight
, underlined
, facename
);
491 /* Try default family */
492 if (!font
&& family
!= wxDEFAULT
)
493 font
= wxLoadQueryFont( point_size
, wxDEFAULT
, style
, weight
, underlined
, facename
);
497 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
, underlined
, facename
);
505 //-----------------------------------------------------------------------------
506 // face names and index functions
507 //-----------------------------------------------------------------------------
509 static char *font_defaults[] = {
510 "FamilyDefault", "Default",
511 "FamilyRoman", "Roman",
512 "FamilyDecorative", "Decorative",
513 "FamilyModern", "Modern",
514 "FamilyTeletype", "Teletype",
515 "FamilySwiss", "Swiss",
516 "FamilyScript", "Script",
522 "AfmItalic", "${AfmSlant}",
526 "AfmHelvetica", "Helv",
527 "AfmCourier", "Cour",
529 "Afm___", "${AfmTimes,$[weight],$[style]}",
531 "AfmTimes__", "${AfmTimes}${Afm$[weight]}${Afm$[style]}",
532 "AfmTimesMediumStraight", "${AfmTimes}${AfmRoman}",
533 "AfmTimesLightStraight", "${AfmTimes}${AfmRoman}",
534 "AfmTimes_Italic", "${AfmTimes}$[weight]${AfmItalic}",
535 "AfmTimes_Slant", "${AfmTimes}$[weight]${AfmItalic}",
537 "AfmSwiss__", "${AfmHelvetica}${Afm$[weight]}${Afm$[style]}",
538 "AfmModern__", "${AfmCourier}${Afm$[weight]}${Afm$[style]}",
540 "AfmTeletype__", "${AfmModern,$[weight],$[style]}",
542 "PostScriptMediumStraight", "",
543 "PostScriptMediumItalic", "-Oblique",
544 "PostScriptMediumSlant", "-Oblique",
545 "PostScriptLightStraight", "",
546 "PostScriptLightItalic", "-Oblique",
547 "PostScriptLightSlant", "-Oblique",
548 "PostScriptBoldStraight", "-Bold",
549 "PostScriptBoldItalic", "-BoldOblique",
550 "PostScriptBoldSlant", "-BoldOblique",
552 #if WX_NORMALIZED_PS_FONTS
553 "PostScript___", "${PostScriptTimes,$[weight],$[style]}",
555 "PostScriptRoman__", "${PostScriptTimes,$[weight],$[style]}",
556 "PostScript___", "LucidaSans${PostScript$[weight]$[style]}",
559 "PostScriptTimesMedium", "",
560 "PostScriptTimesLight", "",
561 "PostScriptTimesBold", "Bold",
563 "PostScriptTimes__", "Times${PostScript$[weight]$[style]}",
564 "PostScriptTimesMediumStraight", "Times-Roman",
565 "PostScriptTimesLightStraight", "Times-Roman",
566 "PostScriptTimes_Slant", "Times-${PostScriptTimes$[weight]}Italic",
567 "PostScriptTimes_Italic", "Times-${PostScriptTimes$[weight]}Italic",
569 "PostScriptSwiss__", "Helvetica${PostScript$[weight]$[style]}",
570 "PostScriptModern__", "Courier${PostScript$[weight]$[style]}",
572 "PostScriptTeletype__", "${PostScriptModern,$[weight],$[style]}",
574 #if !WX_NORMALIZED_PS_FONTS
575 "PostScriptScript__", "Zapf-Chancery-MediumItalic",
578 "ScreenMedium", "medium",
579 "ScreenBold", "bold",
580 "ScreenLight", "light",
581 "ScreenStraight", "r",
585 "ScreenDefaultBase", "*-times",
587 "ScreenRomanBase", "*-times",
588 "ScreenDecorativeBase", "*-helvetica",
589 "ScreenModernBase", "*-courier",
590 "ScreenTeletypeBase", "*-lucidatypewriter",
591 "ScreenSwissBase", "*-lucida",
592 "ScreenScriptBase", "*-zapfchancery",
594 "ScreenStdSuffix", "-${Screen$[weight]}-${Screen$[style]}"
595 "-normal-*-*-%d-*-*-*-*-*-*",
598 "-${ScreenDefaultBase}${ScreenStdSuffix}",
600 "-${ScreenRomanBase}${ScreenStdSuffix}",
601 "ScreenDecorative__",
602 "-${ScreenDecorativeBase}${ScreenStdSuffix}",
604 "-${ScreenModernBase}${ScreenStdSuffix}",
606 "-${ScreenTeletypeBase}${ScreenStdSuffix}",
608 "-${ScreenSwissBase}${ScreenStdSuffix}",
610 "-${ScreenScriptBase}${ScreenStdSuffix}",
614 enum {wxWEIGHT_NORMAL, wxWEIGHT_BOLD, wxWEIGHT_LIGHT, wxNUM_WEIGHTS};
615 enum {wxSTYLE_NORMAL, wxSTYLE_ITALIC, wxSTYLE_SLANT, wxNUM_STYLES};
617 static int WCoordinate(int w)
621 case wxBOLD: return wxWEIGHT_BOLD;
622 case wxLIGHT: return wxWEIGHT_LIGHT;
624 default: return wxWEIGHT_NORMAL;
628 static int SCoordinate(int s)
632 case wxITALIC: return wxSTYLE_ITALIC;
633 case wxSLANT: return wxSTYLE_SLANT;
635 default: return wxSTYLE_NORMAL;
639 //-----------------------------------------------------------------------------
641 //-----------------------------------------------------------------------------
648 inline char *GetName(int weight, int style)
650 return ( map [WCoordinate(weight)] [SCoordinate(style)] );
653 char *map[wxNUM_WEIGHTS][wxNUM_STYLES];
654 void Initialize(const char *, const char *);
657 static void SearchResource(const char *prefix, const char **names, int count, char **v)
660 char resource[1024], **defaults, *internal;
665 internal = (char *) NULL;
667 for (i = 0; i < k; i++)
669 strcpy(resource, prefix);
670 for (j = 0; j < count; j++)
672 // upon failure to find a matching fontname
673 // in the default fonts above, we substitute more
674 // and more values by _ so that at last ScreenMyFontBoldNormal
675 // would turn into Screen___ and this will then get
676 // converted to -${ScreenDefaultBase}${ScreenStdSuffix}
679 strcat(resource, names[j]);
681 strcat(resource, "_");
684 // we previously search the Xt-resources here
688 defaults = font_defaults;
691 if (!strcmp(*defaults, resource))
693 internal = defaults[1];
703 if ((strcmp(internal,"-${ScreenDefaultBase}${ScreenStdSuffix}") == 0) &&
704 (strcmp(names[0], "Default") != 0))
706 // we did not find any font name in the standard list.
707 // this can (hopefully does) mean that someone supplied
708 // the facename in the wxFont constructor so we insert
711 strcpy( resource,"-*-" ); // any producer
712 strcat( resource, names[0] ); // facename
713 strcat( resource, "${ScreenStdSuffix}" ); // add size params later on
714 *v = copystring(resource);
718 *v = copystring(internal);
723 wxSuffixMap::~wxSuffixMap()
727 for (k = 0; k < wxNUM_WEIGHTS; ++k)
728 for (j = 0; j < wxNUM_STYLES; ++j)
732 map[k][j] = (char *) NULL;
736 void wxSuffixMap::Initialize(const char *resname, const char *devresname)
738 const char *weight, *style;
741 const char *names[3];
743 for (k = 0; k < wxNUM_WEIGHTS; k++)
747 case wxWEIGHT_NORMAL: weight = "Medium"; break;
748 case wxWEIGHT_LIGHT: weight = "Light"; break;
750 default: weight = "Bold";
752 for (j = 0; j < wxNUM_STYLES; j++)
756 case wxSTYLE_NORMAL: style = "Straight"; break;
757 case wxSTYLE_ITALIC: style = "Italic"; break;
759 default: style = "Slant";
765 SearchResource(devresname, names, 3, &v);
767 // Expand macros in the found string:
769 int len, closer = 0, startpos = 0;
771 len = (v ? strlen(v) : 0);
772 for (i = 0; i < len; i++)
774 if (v[i] == '$' && ((v[i+1] == '[') || (v[i+1] == '{')))
777 closer = (v[i+1] == '[') ? ']' : '}';
780 else if (v[i] == closer)
783 const char *r = (char *) NULL; bool delete_r = FALSE;
786 name = v + startpos + 2;
794 for (i = 0, count = 1; name[i]; i++)
800 names = new char*[count];
802 for (i = 0, count = 1; i < len; i++)
805 names[count++] = name + i + 1;
809 SearchResource("", (const char **)names, count, (char **)&r);
815 for (i = 0; i < len; i++)
819 wxLogError( "Bad resource name in font lookup." );
821 } else if (!strcmp(name, "weight")) {
823 } else if (!strcmp(name, "style")) {
825 } else if (!strcmp(name, "family")) {
829 wxLogError( "Bad font macro name." );
833 newstrlen = strlen(r);
834 char *naya = new char[startpos + newstrlen + len - i];
835 memcpy(naya, v, startpos);
836 memcpy(naya + startpos, r, newstrlen);
837 memcpy(naya + startpos + newstrlen, v + i + 1, len - i);
846 // We have a final value:
852 //-----------------------------------------------------------------------------
854 //-----------------------------------------------------------------------------
856 class wxFontNameItem : public wxObject
858 DECLARE_DYNAMIC_CLASS(wxFontNameItem)
860 wxFontNameItem(const char *name, int id, int family);
863 inline char* GetScreenName(int w, int s) {return screen.GetName(w, s);}
864 inline char* GetPostScriptName(int w, int s) {return printing.GetName(w, s);}
865 inline char* GetAFMName(int w, int s) {return afm.GetName(w, s);}
866 inline char* GetName() {return name;}
867 inline int GetFamily() {return family;}
868 inline int GetId() {return id;}
869 inline bool IsRoman() {return isroman;}
870 #if defined(__WXDEBUG__)
871 void Dump(ostream& str);
877 wxSuffixMap screen, printing, afm;
881 IMPLEMENT_ABSTRACT_CLASS(wxFontNameItem, wxObject)
883 wxFontNameItem::wxFontNameItem(const char *Name, int Id, int Family)
885 name = copystring(Name);
889 screen. Initialize(name, "Screen");
890 printing.Initialize(name, "PostScript");
891 afm. Initialize(name, "Afm");
894 wxFontNameItem::~wxFontNameItem()
898 name = (char *) NULL;
901 #if defined(__WXDEBUG__)
902 void wxFontNameItem::Dump(ostream& str)
904 str << "wxFontNameItem(" << name << ")";
908 //-----------------------------------------------------------------------------
910 //-----------------------------------------------------------------------------
912 IMPLEMENT_DYNAMIC_CLASS(wxFontNameDirectory, wxObject)
914 wxFontNameDirectory::wxFontNameDirectory()
916 table = new wxHashTable(wxKEY_INTEGER, 20);
920 wxFontNameDirectory::~wxFontNameDirectory()
922 // Cleanup wxFontNameItems allocated
924 wxNode *node = table->Next();
927 wxFontNameItem *item = (wxFontNameItem*)node->Data();
929 node = table->Next();
934 int wxFontNameDirectory::GetNewFontId()
936 return (nextFontId--);
939 void wxFontNameDirectory::Initialize()
941 Initialize(wxDEFAULT, wxDEFAULT, "Default");
942 Initialize(wxDECORATIVE, wxDECORATIVE, "Decorative");
943 Initialize(wxROMAN, wxROMAN, "Roman");
944 Initialize(wxMODERN, wxMODERN, "Modern");
945 Initialize(wxTELETYPE, wxTELETYPE, "Teletype");
946 Initialize(wxSWISS, wxSWISS, "Swiss");
947 Initialize(wxSCRIPT, wxSCRIPT, "Script");
950 void wxFontNameDirectory::Initialize(int fontid, int family, const char *resname)
952 char *fam, resource[256];
954 sprintf(resource, "Family%s", resname);
955 SearchResource((const char *)resource, (const char **) NULL, 0, (char **)&fam);
959 if (!strcmp(fam, "Default")) family = wxDEFAULT;
960 else if (!strcmp(fam, "Roman")) family = wxROMAN;
961 else if (!strcmp(fam, "Decorative")) family = wxDECORATIVE;
962 else if (!strcmp(fam, "Modern")) family = wxMODERN;
963 else if (!strcmp(fam, "Teletype")) family = wxTELETYPE;
964 else if (!strcmp(fam, "Swiss")) family = wxSWISS;
965 else if (!strcmp(fam, "Script")) family = wxSCRIPT;
966 delete[] fam; // free resource
968 table->Put(fontid, new wxFontNameItem(resname, fontid, family));
971 int wxFontNameDirectory::FindOrCreateFontId(const char *name, int family)
975 // font exists -> return id
976 if ( (id = GetFontId(name)) ) return id;
979 Initialize(id=GetNewFontId(), family, name);
983 char *wxFontNameDirectory::GetScreenName(int fontid, int weight, int style)
985 wxFontNameItem *item = (wxFontNameItem*)table->Get(fontid); // find font
987 return item->GetScreenName(weight, style);
989 // font does not exist
990 return (char *) NULL;
993 char *wxFontNameDirectory::GetPostScriptName(int fontid, int weight, int style)
995 wxFontNameItem *item = (wxFontNameItem*)table->Get(fontid); // find font
997 return item->GetPostScriptName(weight, style);
999 // font does not exist
1000 return (char *) NULL;
1003 char *wxFontNameDirectory::GetAFMName(int fontid, int weight, int style)
1005 wxFontNameItem *item = (wxFontNameItem *)table->Get(fontid); // find font
1007 return item->GetAFMName(weight, style);
1008 // font does not exist
1009 return (char *) NULL;
1012 char *wxFontNameDirectory::GetFontName(int fontid)
1014 wxFontNameItem *item = (wxFontNameItem *)table->Get(fontid); // find font
1016 return item->GetName();
1018 // font does not exist
1019 return (char *) NULL;
1022 int wxFontNameDirectory::GetFontId(const char *name)
1028 while ( (node = table->Next()) )
1030 wxFontNameItem *item = (wxFontNameItem*)node->Data();
1031 if (!strcmp(name, item->name))
1035 // font does not exist
1039 int wxFontNameDirectory::GetFamily(int fontid)
1041 wxFontNameItem *item = (wxFontNameItem *)table->Get(fontid);
1044 return item->family;
1046 // font does not exist