]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/font.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "font.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 static char *wx_font_family
[] = {
24 "wxDEFAULT", "wxDECORATIVE", "wxMODERN", "wxROMAN", "wxSCRIPT",
25 "wxSWISS", "wxTELETYPE",
27 static char *wx_font_style
[] = {
28 "wxDEFAULT", "wxNORMAL", "wxSLANT", "wxITALIC",
30 static char *wx_font_weight
[] = {
31 "wxDEFAULT", "wxNORMAL", "wxBOLD", "wxLIGHT",
34 extern wxFontNameDirectory
*wxTheFontNameDirectory
;
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 class wxFontRefData
: public wxObjectRefData
47 wxList m_scaled_xfonts
;
49 int m_family
, m_style
, m_weight
;
60 wxFontRefData::wxFontRefData(void) : m_scaled_xfonts(wxKEY_INTEGER
)
62 m_byXFontName
= FALSE
;
73 wxFontRefData::~wxFontRefData(void)
75 wxNode
*node
= m_scaled_xfonts
.First();
78 GdkFont
*font
= (GdkFont
*)node
->Data();
79 wxNode
*next
= node
->Next();
80 gdk_font_unref( font
);
88 if (m_font
) gdk_font_unref( m_font
);
91 //-----------------------------------------------------------------------------
93 #define M_FONTDATA ((wxFontRefData *)m_refData)
95 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
99 if (wxTheFontList
) wxTheFontList
->Append( this );
102 wxFont::wxFont( char *xFontName
)
104 if (!xFontName
) return;
106 m_refData
= new wxFontRefData();
108 M_FONTDATA
->m_byXFontName
= TRUE
;
109 M_FONTDATA
->m_font
= gdk_font_load( xFontName
);
112 wxFont::wxFont(int PointSize
, int FontIdOrFamily
, int Style
, int Weight
,
113 bool Underlined
, const char* Face
)
115 m_refData
= new wxFontRefData();
117 if ((M_FONTDATA
->m_faceName
= (Face
) ? copystring(Face
) : (char*)NULL
) )
119 M_FONTDATA
->m_fontId
= wxTheFontNameDirectory
->FindOrCreateFontId( Face
, FontIdOrFamily
);
120 M_FONTDATA
->m_family
= wxTheFontNameDirectory
->GetFamily( FontIdOrFamily
);
124 M_FONTDATA
->m_fontId
= FontIdOrFamily
;
125 M_FONTDATA
->m_family
= wxTheFontNameDirectory
->GetFamily( FontIdOrFamily
);
127 M_FONTDATA
->m_style
= Style
;
128 M_FONTDATA
->m_weight
= Weight
;
129 M_FONTDATA
->m_pointSize
= PointSize
;
130 M_FONTDATA
->m_underlined
= Underlined
;
132 if (wxTheFontList
) wxTheFontList
->Append( this );
135 wxFont::wxFont(int PointSize
, const char *Face
, int Family
, int Style
,
136 int Weight
, bool Underlined
)
138 m_refData
= new wxFontRefData();
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
;
148 if (wxTheFontList
) wxTheFontList
->Append( this );
151 wxFont::wxFont( const wxFont
& font
)
156 wxFont::wxFont( const wxFont
* font
)
159 if (font
) Ref( *font
);
162 wxFont::~wxFont(void)
164 if (wxTheFontList
) wxTheFontList
->DeleteObject( this );
167 wxFont
& wxFont::operator = ( const wxFont
& font
)
169 if (*this == font
) return (*this);
174 bool wxFont::operator == ( const wxFont
& font
)
176 return m_refData
== font
.m_refData
;
179 bool wxFont::operator != ( const wxFont
& font
)
181 return m_refData
!= font
.m_refData
;
186 return (m_refData
!= NULL
);
189 int wxFont::GetPointSize(void) const
191 return M_FONTDATA
->m_pointSize
;
194 wxString
wxFont::GetFaceString(void) const
196 wxString s
= wxTheFontNameDirectory
->GetFontName( M_FONTDATA
->m_fontId
);
200 wxString
wxFont::GetFaceName(void) const
202 wxString s
= wxTheFontNameDirectory
->GetFontName( M_FONTDATA
->m_fontId
);
206 int wxFont::GetFamily(void) const
208 return M_FONTDATA
->m_family
;
211 wxString
wxFont::GetFamilyString(void) const
213 wxString s
= wx_font_family
[M_FONTDATA
->m_family
];
217 int wxFont::GetFontId(void) const
219 return M_FONTDATA
->m_fontId
; // stub
222 int wxFont::GetStyle(void) const
224 return M_FONTDATA
->m_style
;
227 wxString
wxFont::GetStyleString(void) const
229 wxString s
= wx_font_style
[M_FONTDATA
->m_style
];
233 int wxFont::GetWeight(void) const
235 return M_FONTDATA
->m_weight
;
238 wxString
wxFont::GetWeightString(void) const
240 wxString s
= wx_font_weight
[M_FONTDATA
->m_weight
];
244 bool wxFont::GetUnderlined(void) const
246 return M_FONTDATA
->m_underlined
;
249 //-----------------------------------------------------------------------------
250 // get internal representation of font
251 //-----------------------------------------------------------------------------
253 // local help function
254 static GdkFont
*wxLoadQueryNearestFont(int point_size
, int fontid
,
255 int style
, int weight
,
258 GdkFont
*wxFont::GetInternalFont(float scale
)
260 if (M_FONTDATA
->m_byXFontName
) return M_FONTDATA
->m_font
;
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
;
266 wxNode
*node
= M_FONTDATA
->m_scaled_xfonts
.Find(int_scale
);
269 font
= (GdkFont
*)node
->Data();
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
);
278 printf("could not load any font");
279 // wxError("could not load any font", "wxFont");
283 //-----------------------------------------------------------------------------
284 // local utilities to find a X font
285 //-----------------------------------------------------------------------------
287 static GdkFont
*wxLoadQueryFont(int point_size
, int fontid
, int style
,
288 int weight
, bool WXUNUSED(underlined
))
291 char *name
= wxTheFontNameDirectory
->GetScreenName( fontid
, weight
, style
);
294 name
= "-*-*-*-*-*-*-*-%d-*-*-*-*-*-*";
295 sprintf(buffer
, name
, point_size
);
297 return gdk_font_load( buffer
);
300 static GdkFont
*wxLoadQueryNearestFont(int point_size
, int fontid
,
301 int style
, int weight
,
306 font
= wxLoadQueryFont( point_size
, fontid
, style
, weight
, underlined
);
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));
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
,
326 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
332 //-----------------------------------------------------------------------------
333 // face names and index functions
334 //-----------------------------------------------------------------------------
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",
349 "AfmItalic", "${AfmSlant}",
353 "AfmHelvetica", "Helv",
354 "AfmCourier", "Cour",
356 "Afm___", "${AfmTimes,$[weight],$[style]}",
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}",
364 "AfmSwiss__", "${AfmHelvetica}${Afm$[weight]}${Afm$[style]}",
365 "AfmModern__", "${AfmCourier}${Afm$[weight]}${Afm$[style]}",
367 "AfmTeletype__", "${AfmModern,$[weight],$[style]}",
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",
379 #if WX_NORMALIZED_PS_FONTS
380 "PostScript___", "${PostScriptTimes,$[weight],$[style]}",
382 "PostScriptRoman__", "${PostScriptTimes,$[weight],$[style]}",
383 "PostScript___", "LucidaSans${PostScript$[weight]$[style]}",
386 "PostScriptTimesMedium", "",
387 "PostScriptTimesLight", "",
388 "PostScriptTimesBold", "Bold",
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",
396 "PostScriptSwiss__", "Helvetica${PostScript$[weight]$[style]}",
397 "PostScriptModern__", "Courier${PostScript$[weight]$[style]}",
399 "PostScriptTeletype__", "${PostScriptModern,$[weight],$[style]}",
401 #if !WX_NORMALIZED_PS_FONTS
402 "PostScriptScript__", "Zapf-Chancery-MediumItalic",
405 "ScreenMedium", "medium",
406 "ScreenBold", "bold",
407 "ScreenLight", "light",
408 "ScreenStraight", "r",
412 "ScreenDefaultBase", "misc-fixed",
413 "ScreenRomanBase", "*-times",
414 "ScreenDecorativeBase", "*-helvetica",
415 "ScreenModernBase", "*-courier",
416 "ScreenTeletypeBase", "*-lucidatypewriter",
417 "ScreenSwissBase", "*-lucida",
418 "ScreenScriptBase", "*-zapfchancery",
420 "ScreenStdSuffix", "-${Screen$[weight]}-${Screen$[style]}"
421 "-normal-*-*-%d-*-*-*-*-*-*",
424 "-${ScreenDefaultBase}${ScreenStdSuffix}",
426 "-${ScreenRomanBase}${ScreenStdSuffix}",
427 "ScreenDecorative__",
428 "-${ScreenDecorativeBase}${ScreenStdSuffix}",
430 "-${ScreenModernBase}${ScreenStdSuffix}",
432 "-${ScreenTeletypeBase}${ScreenStdSuffix}",
434 "-${ScreenSwissBase}${ScreenStdSuffix}",
436 "-${ScreenScriptBase}${ScreenStdSuffix}",
440 enum {wxWEIGHT_NORMAL
, wxWEIGHT_BOLD
, wxWEIGHT_LIGHT
, wxNUM_WEIGHTS
};
441 enum {wxSTYLE_NORMAL
, wxSTYLE_ITALIC
, wxSTYLE_SLANT
, wxNUM_STYLES
};
443 static int WCoordinate(int w
)
446 case wxBOLD
: return wxWEIGHT_BOLD
;
447 case wxLIGHT
: return wxWEIGHT_LIGHT
;
449 default: return wxWEIGHT_NORMAL
;
453 static int SCoordinate(int s
)
456 case wxITALIC
: return wxSTYLE_ITALIC
;
457 case wxSLANT
: return wxSTYLE_SLANT
;
459 default: return wxSTYLE_NORMAL
;
463 //-----------------------------------------------------------------------------
465 //-----------------------------------------------------------------------------
471 inline char *GetName(int weight
, int style
)
473 return ( map
[WCoordinate(weight
)] [SCoordinate(style
)] );
476 char *map
[wxNUM_WEIGHTS
][wxNUM_STYLES
];
477 void Initialize(const char *, const char *);
481 #define wxGetResource(a, b, c) 0
484 static void SearchResource(const char *prefix
, const char **names
, int count
, char **v
)
487 char resource
[1024], **defaults
, *internal
;
494 for (i
= 0; i
< k
; i
++) {
495 strcpy(resource
, prefix
);
496 for (j
= 0; j
< count
; j
++) {
498 strcat(resource
, names
[j
]);
500 strcat(resource
, "_");
502 if (wxGetResource(wxAPP_CLASS
, (char *)resource
, v
))
505 defaults
= font_defaults
;
507 if (!strcmp(*defaults
, resource
)) {
508 internal
= defaults
[1];
516 *v
= copystring(internal
);
519 wxSuffixMap::~wxSuffixMap(void)
523 for (k
= 0; k
< wxNUM_WEIGHTS
; ++k
)
524 for (j
= 0; j
< wxNUM_STYLES
; ++j
)
531 void wxSuffixMap::Initialize(const char *resname
, const char *devresname
)
533 const char *weight
, *style
;
536 const char *names
[3];
538 for (k
= 0; k
< wxNUM_WEIGHTS
; k
++) {
540 case wxWEIGHT_NORMAL
: weight
= "Medium"; break;
541 case wxWEIGHT_LIGHT
: weight
= "Light"; break;
543 default: weight
= "Bold";
545 for (j
= 0; j
< wxNUM_STYLES
; j
++) {
547 case wxSTYLE_NORMAL
: style
= "Straight"; break;
548 case wxSTYLE_ITALIC
: style
= "Italic"; break;
550 default: style
= "Slant";
556 SearchResource(devresname
, names
, 3, &v
);
558 /* Expand macros in the found string: */
560 int len
, closer
= 0, startpos
= 0;
562 len
= (v
? strlen(v
) : 0);
563 for (i
= 0; i
< len
; i
++) {
564 if (v
[i
] == '$' && ((v
[i
+1] == '[') || (v
[i
+1] == '{'))) {
566 closer
= (v
[i
+1] == '[') ? ']' : '}';
568 } else if (v
[i
] == closer
) {
570 const char *r
= NULL
; bool delete_r
= FALSE
;
573 name
= v
+ startpos
+ 2;
580 for (i
= 0, count
= 1; name
[i
]; i
++)
586 names
= new char*[count
];
588 for (i
= 0, count
= 1; i
< len
; i
++)
589 if (name
[i
] == ',') {
590 names
[count
++] = name
+ i
+ 1;
594 SearchResource("", (const char **)names
, count
, (char **)&r
);
599 for (i
= 0; i
< len
; i
++)
603 printf("Bad resource name \"%s\" in font lookup\n", name
);
605 } else if (!strcmp(name
, "weight")) {
607 } else if (!strcmp(name
, "style")) {
609 } else if (!strcmp(name
, "family")) {
613 printf("Bad font macro name \"%s\"\n", name
);
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
);
630 /* We have a final value: */
636 //-----------------------------------------------------------------------------
638 //-----------------------------------------------------------------------------
640 class wxFontNameItem
: public wxObject
{
641 DECLARE_DYNAMIC_CLASS(wxFontNameItem
)
643 wxFontNameItem(const char *name
, int id
, int family
);
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
;}
654 void Dump(ostream
& str
);
660 wxSuffixMap screen
, printing
, afm
;
664 IMPLEMENT_ABSTRACT_CLASS(wxFontNameItem
, wxObject
)
666 wxFontNameItem::wxFontNameItem(const char *Name
, int Id
, int Family
)
668 name
= copystring(Name
);
672 screen
. Initialize(name
, "Screen");
673 printing
.Initialize(name
, "PostScript");
674 afm
. Initialize(name
, "Afm");
677 wxFontNameItem::~wxFontNameItem(void)
685 void wxFontNameItem::Dump(ostream
& str
)
687 str
<< "wxFontNameItem(" << name
<< ")";
691 //-----------------------------------------------------------------------------
693 //-----------------------------------------------------------------------------
695 IMPLEMENT_DYNAMIC_CLASS(wxFontNameDirectory
, wxObject
)
697 wxFontNameDirectory::wxFontNameDirectory(void)
699 table
= new wxHashTable(wxKEY_INTEGER
, 20);
703 wxFontNameDirectory::~wxFontNameDirectory()
705 // Cleanup wxFontNameItems allocated
707 wxNode
*node
= table
->Next();
709 wxFontNameItem
*item
= (wxFontNameItem
*)node
->Data();
711 node
= table
->Next();
716 int wxFontNameDirectory::GetNewFontId(void)
718 return (nextFontId
--);
721 void wxFontNameDirectory::Initialize()
723 Initialize(wxDEFAULT
, wxDEFAULT
, "Default");
724 Initialize(wxDECORATIVE
, wxDECORATIVE
, "Decorative");
725 Initialize(wxROMAN
, wxROMAN
, "Roman");
726 Initialize(wxMODERN
, wxMODERN
, "Modern");
727 Initialize(wxTELETYPE
, wxTELETYPE
, "Teletype");
728 Initialize(wxSWISS
, wxSWISS
, "Swiss");
729 Initialize(wxSCRIPT
, wxSCRIPT
, "Script");
732 void wxFontNameDirectory::Initialize(int fontid
, int family
, const char *resname
)
734 char *fam
, resource
[256];
736 sprintf(resource
, "Family%s", resname
);
737 SearchResource((const char *)resource
, NULL
, 0, (char **)&fam
);
739 if (!strcmp(fam
, "Default")) family
= wxDEFAULT
;
740 else if (!strcmp(fam
, "Roman")) family
= wxROMAN
;
741 else if (!strcmp(fam
, "Decorative")) family
= wxDECORATIVE
;
742 else if (!strcmp(fam
, "Modern")) family
= wxMODERN
;
743 else if (!strcmp(fam
, "Teletype")) family
= wxTELETYPE
;
744 else if (!strcmp(fam
, "Swiss")) family
= wxSWISS
;
745 else if (!strcmp(fam
, "Script")) family
= wxSCRIPT
;
746 delete[] fam
; // free resource
748 table
->Put(fontid
, new wxFontNameItem(resname
, fontid
, family
));
751 int wxFontNameDirectory::FindOrCreateFontId(const char *name
, int family
)
755 // font exists -> return id
756 if ( (id
= GetFontId(name
)) ) return id
;
758 Initialize(id
=GetNewFontId(), family
, name
);
762 char *wxFontNameDirectory::GetScreenName(int fontid
, int weight
, int style
)
764 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
); // find font
766 return item
->GetScreenName(weight
, style
);
767 // font does not exist
771 char *wxFontNameDirectory::GetPostScriptName(int fontid
, int weight
, int style
)
773 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
); // find font
775 return item
->GetPostScriptName(weight
, style
);
776 // font does not exist
780 char *wxFontNameDirectory::GetAFMName(int fontid
, int weight
, int style
)
782 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
); // find font
784 return item
->GetAFMName(weight
, style
);
785 // font does not exist
789 char *wxFontNameDirectory::GetFontName(int fontid
)
791 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
); // find font
793 return item
->GetName();
794 // font does not exist
798 int wxFontNameDirectory::GetFontId(const char *name
)
804 while ( (node
= table
->Next()) ) {
805 wxFontNameItem
*item
= (wxFontNameItem
*)node
->Data();
806 if (!strcmp(name
, item
->name
))
809 // font does not exist
813 int wxFontNameDirectory::GetFamily(int fontid
)
815 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
);
819 // font does not exist