]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/font.cpp
2152854752a24da066716e1d87d51cafb713cf87
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", "wxROMAN", "wxSCRIPT",
25 "wxSWISS", "wxMODERN", "wxTELETYPE",
29 static char *wx_font_style [] = {
30 "wxDEFAULT", "wxNORMAL", "wxSLANT", "wxITALIC",
33 static char *wx_font_weight [] = {
34 "wxDEFAULT", "wxNORMAL", "wxBOLD", "wxLIGHT",
38 extern wxFontNameDirectory
*wxTheFontNameDirectory
;
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 class wxFontRefData
: public wxObjectRefData
51 wxList m_scaled_xfonts
;
53 int m_family
, m_style
, m_weight
;
64 wxFontRefData::wxFontRefData(void) : m_scaled_xfonts(wxKEY_INTEGER
)
66 m_byXFontName
= FALSE
;
73 m_faceName
= (char *) NULL
;
74 m_font
= (GdkFont
*) NULL
;
77 wxFontRefData::~wxFontRefData(void)
79 wxNode
*node
= m_scaled_xfonts
.First();
82 GdkFont
*font
= (GdkFont
*)node
->Data();
83 wxNode
*next
= node
->Next();
84 gdk_font_unref( font
);
90 m_faceName
= (char *) NULL
;
92 if (m_font
) gdk_font_unref( m_font
);
95 //-----------------------------------------------------------------------------
97 #define M_FONTDATA ((wxFontRefData *)m_refData)
99 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
103 if (wxTheFontList
) wxTheFontList
->Append( this );
106 wxFont::wxFont( char *xFontName
)
108 if (!xFontName
) return;
110 m_refData
= new wxFontRefData();
112 M_FONTDATA
->m_byXFontName
= TRUE
;
113 M_FONTDATA
->m_font
= gdk_font_load( xFontName
);
116 wxFont::wxFont(int PointSize
, int FontIdOrFamily
, int Style
, int Weight
,
117 bool Underlined
, const char* Face
)
119 m_refData
= new wxFontRefData();
121 if ((M_FONTDATA
->m_faceName
= (Face
) ? copystring(Face
) : (char*)NULL
) )
123 M_FONTDATA
->m_fontId
= wxTheFontNameDirectory
->FindOrCreateFontId( Face
, FontIdOrFamily
);
124 M_FONTDATA
->m_family
= wxTheFontNameDirectory
->GetFamily( FontIdOrFamily
);
128 M_FONTDATA
->m_fontId
= FontIdOrFamily
;
129 M_FONTDATA
->m_family
= wxTheFontNameDirectory
->GetFamily( FontIdOrFamily
);
131 M_FONTDATA
->m_style
= Style
;
132 M_FONTDATA
->m_weight
= Weight
;
133 M_FONTDATA
->m_pointSize
= PointSize
;
134 M_FONTDATA
->m_underlined
= Underlined
;
136 if (wxTheFontList
) wxTheFontList
->Append( this );
139 wxFont::wxFont(int PointSize
, const char *Face
, int Family
, int Style
,
140 int Weight
, bool Underlined
)
142 m_refData
= new wxFontRefData();
144 M_FONTDATA
->m_fontId
= wxTheFontNameDirectory
->FindOrCreateFontId( Face
, Family
);
145 M_FONTDATA
->m_faceName
= (Face
) ? copystring(Face
) : (char*)NULL
;
146 M_FONTDATA
->m_family
= wxTheFontNameDirectory
->GetFamily( M_FONTDATA
->m_fontId
);
147 M_FONTDATA
->m_style
= Style
;
148 M_FONTDATA
->m_weight
= Weight
;
149 M_FONTDATA
->m_pointSize
= PointSize
;
150 M_FONTDATA
->m_underlined
= Underlined
;
152 if (wxTheFontList
) wxTheFontList
->Append( this );
155 wxFont::wxFont( const wxFont
& font
)
159 if (wxTheFontList
) wxTheFontList
->Append( this );
162 wxFont::wxFont( const wxFont
* font
)
165 if (font
) Ref( *font
);
167 if (wxTheFontList
) wxTheFontList
->Append( this );
170 wxFont::~wxFont(void)
172 if (wxTheFontList
) wxTheFontList
->DeleteObject( this );
175 wxFont
& wxFont::operator = ( const wxFont
& font
)
177 if (*this == font
) return (*this);
182 bool wxFont::operator == ( const wxFont
& font
)
184 return m_refData
== font
.m_refData
;
187 bool wxFont::operator != ( const wxFont
& font
)
189 return m_refData
!= font
.m_refData
;
194 return (m_refData
!= NULL
);
197 int wxFont::GetPointSize(void) const
199 return M_FONTDATA
->m_pointSize
;
202 wxString
wxFont::GetFaceString(void) const
204 wxString s
= wxTheFontNameDirectory
->GetFontName( M_FONTDATA
->m_fontId
);
208 wxString
wxFont::GetFaceName(void) const
210 wxString s
= wxTheFontNameDirectory
->GetFontName( M_FONTDATA
->m_fontId
);
214 int wxFont::GetFamily(void) const
216 return M_FONTDATA
->m_family
;
219 wxString
wxFont::GetFamilyString(void) const
221 wxString s
= wx_font_family
[M_FONTDATA
->m_family
- wxDEFAULT
];
225 int wxFont::GetFontId(void) const
227 return M_FONTDATA
->m_fontId
; // stub
230 int wxFont::GetStyle(void) const
232 return M_FONTDATA
->m_style
;
235 wxString
wxFont::GetStyleString(void) const
237 switch (M_FONTDATA
->m_style
)
241 return wxString("wxNORMAL");
245 return wxString("wxSLANT");
249 return wxString("wxITALIC");
254 return wxString("wxDEFAULT");
257 return wxString("wxDEFAULT");
260 int wxFont::GetWeight(void) const
262 return M_FONTDATA
->m_weight
;
265 wxString
wxFont::GetWeightString(void) const
267 switch (M_FONTDATA
->m_weight
)
271 return wxString("wxNORMAL");
275 return wxString("wxBOLD");
279 return wxString("wxLIGHT");
284 return wxString("wxDEFAULT");
287 return wxString("wxDEFAULT");
290 bool wxFont::GetUnderlined(void) const
292 return M_FONTDATA
->m_underlined
;
295 //-----------------------------------------------------------------------------
296 // get internal representation of font
297 //-----------------------------------------------------------------------------
299 // local help function
300 static GdkFont
*wxLoadQueryNearestFont(int point_size
, int fontid
,
301 int style
, int weight
,
304 GdkFont
*wxFont::GetInternalFont(float scale
) const
306 if (M_FONTDATA
->m_byXFontName
) return M_FONTDATA
->m_font
;
308 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
309 int point_scale
= (M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100;
310 GdkFont
*font
= (GdkFont
*) NULL
;
312 wxNode
*node
= M_FONTDATA
->m_scaled_xfonts
.Find(int_scale
);
315 font
= (GdkFont
*)node
->Data();
319 font
= wxLoadQueryNearestFont( point_scale
, M_FONTDATA
->m_fontId
, M_FONTDATA
->m_style
,
320 M_FONTDATA
->m_weight
, M_FONTDATA
->m_underlined
);
321 M_FONTDATA
->m_scaled_xfonts
.Append( int_scale
, (wxObject
*)font
);
324 printf("could not load any font");
325 // wxError("could not load any font", "wxFont");
329 //-----------------------------------------------------------------------------
330 // local utilities to find a X font
331 //-----------------------------------------------------------------------------
333 static GdkFont
*wxLoadQueryFont(int point_size
, int fontid
, int style
,
334 int weight
, bool WXUNUSED(underlined
))
337 char *name
= wxTheFontNameDirectory
->GetScreenName( fontid
, weight
, style
);
340 name
= "-*-*-*-*-*-*-*-%d-*-*-*-*-*-*";
341 sprintf(buffer
, name
, point_size
);
343 return gdk_font_load( buffer
);
346 static GdkFont
*wxLoadQueryNearestFont(int point_size
, int fontid
,
347 int style
, int weight
,
352 font
= wxLoadQueryFont( point_size
, fontid
, style
, weight
, underlined
);
355 // search up and down by stepsize 10
356 int max_size
= point_size
+ 20 * (1 + (point_size
/180));
357 int min_size
= point_size
- 20 * (1 + (point_size
/180));
360 // Search for smaller size (approx.)
361 for (i
=point_size
-10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10)
362 font
= wxLoadQueryFont(i
, fontid
, style
, weight
, underlined
);
363 // Search for larger size (approx.)
364 for (i
=point_size
+10; !font
&& i
<= max_size
; i
+= 10)
365 font
= wxLoadQueryFont(i
, fontid
, style
, weight
, underlined
);
366 // Try default family
367 if (!font
&& fontid
!= wxDEFAULT
)
368 font
= wxLoadQueryFont(point_size
, wxDEFAULT
, style
,
372 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
378 //-----------------------------------------------------------------------------
379 // face names and index functions
380 //-----------------------------------------------------------------------------
382 static char *font_defaults
[] = {
383 "FamilyDefault", "Default",
384 "FamilyRoman", "Roman",
385 "FamilyDecorative", "Decorative",
386 "FamilyModern", "Modern",
387 "FamilyTeletype", "Teletype",
388 "FamilySwiss", "Swiss",
389 "FamilyScript", "Script",
395 "AfmItalic", "${AfmSlant}",
399 "AfmHelvetica", "Helv",
400 "AfmCourier", "Cour",
402 "Afm___", "${AfmTimes,$[weight],$[style]}",
404 "AfmTimes__", "${AfmTimes}${Afm$[weight]}${Afm$[style]}",
405 "AfmTimesMediumStraight", "${AfmTimes}${AfmRoman}",
406 "AfmTimesLightStraight", "${AfmTimes}${AfmRoman}",
407 "AfmTimes_Italic", "${AfmTimes}$[weight]${AfmItalic}",
408 "AfmTimes_Slant", "${AfmTimes}$[weight]${AfmItalic}",
410 "AfmSwiss__", "${AfmHelvetica}${Afm$[weight]}${Afm$[style]}",
411 "AfmModern__", "${AfmCourier}${Afm$[weight]}${Afm$[style]}",
413 "AfmTeletype__", "${AfmModern,$[weight],$[style]}",
415 "PostScriptMediumStraight", "",
416 "PostScriptMediumItalic", "-Oblique",
417 "PostScriptMediumSlant", "-Oblique",
418 "PostScriptLightStraight", "",
419 "PostScriptLightItalic", "-Oblique",
420 "PostScriptLightSlant", "-Oblique",
421 "PostScriptBoldStraight", "-Bold",
422 "PostScriptBoldItalic", "-BoldOblique",
423 "PostScriptBoldSlant", "-BoldOblique",
425 #if WX_NORMALIZED_PS_FONTS
426 "PostScript___", "${PostScriptTimes,$[weight],$[style]}",
428 "PostScriptRoman__", "${PostScriptTimes,$[weight],$[style]}",
429 "PostScript___", "LucidaSans${PostScript$[weight]$[style]}",
432 "PostScriptTimesMedium", "",
433 "PostScriptTimesLight", "",
434 "PostScriptTimesBold", "Bold",
436 "PostScriptTimes__", "Times${PostScript$[weight]$[style]}",
437 "PostScriptTimesMediumStraight", "Times-Roman",
438 "PostScriptTimesLightStraight", "Times-Roman",
439 "PostScriptTimes_Slant", "Times-${PostScriptTimes$[weight]}Italic",
440 "PostScriptTimes_Italic", "Times-${PostScriptTimes$[weight]}Italic",
442 "PostScriptSwiss__", "Helvetica${PostScript$[weight]$[style]}",
443 "PostScriptModern__", "Courier${PostScript$[weight]$[style]}",
445 "PostScriptTeletype__", "${PostScriptModern,$[weight],$[style]}",
447 #if !WX_NORMALIZED_PS_FONTS
448 "PostScriptScript__", "Zapf-Chancery-MediumItalic",
451 "ScreenMedium", "medium",
452 "ScreenBold", "bold",
453 "ScreenLight", "light",
454 "ScreenStraight", "r",
458 "ScreenDefaultBase", "misc-fixed",
459 "ScreenRomanBase", "*-times",
460 "ScreenDecorativeBase", "*-helvetica",
461 "ScreenModernBase", "*-courier",
462 "ScreenTeletypeBase", "*-lucidatypewriter",
463 "ScreenSwissBase", "*-lucida",
464 "ScreenScriptBase", "*-zapfchancery",
466 "ScreenStdSuffix", "-${Screen$[weight]}-${Screen$[style]}"
467 "-normal-*-*-%d-*-*-*-*-*-*",
470 "-${ScreenDefaultBase}${ScreenStdSuffix}",
472 "-${ScreenRomanBase}${ScreenStdSuffix}",
473 "ScreenDecorative__",
474 "-${ScreenDecorativeBase}${ScreenStdSuffix}",
476 "-${ScreenModernBase}${ScreenStdSuffix}",
478 "-${ScreenTeletypeBase}${ScreenStdSuffix}",
480 "-${ScreenSwissBase}${ScreenStdSuffix}",
482 "-${ScreenScriptBase}${ScreenStdSuffix}",
486 enum {wxWEIGHT_NORMAL
, wxWEIGHT_BOLD
, wxWEIGHT_LIGHT
, wxNUM_WEIGHTS
};
487 enum {wxSTYLE_NORMAL
, wxSTYLE_ITALIC
, wxSTYLE_SLANT
, wxNUM_STYLES
};
489 static int WCoordinate(int w
)
492 case wxBOLD
: return wxWEIGHT_BOLD
;
493 case wxLIGHT
: return wxWEIGHT_LIGHT
;
495 default: return wxWEIGHT_NORMAL
;
499 static int SCoordinate(int s
)
502 case wxITALIC
: return wxSTYLE_ITALIC
;
503 case wxSLANT
: return wxSTYLE_SLANT
;
505 default: return wxSTYLE_NORMAL
;
509 //-----------------------------------------------------------------------------
511 //-----------------------------------------------------------------------------
517 inline char *GetName(int weight
, int style
)
519 return ( map
[WCoordinate(weight
)] [SCoordinate(style
)] );
522 char *map
[wxNUM_WEIGHTS
][wxNUM_STYLES
];
523 void Initialize(const char *, const char *);
527 #define wxGetResource(a, b, c) 0
530 static void SearchResource(const char *prefix
, const char **names
, int count
, char **v
)
533 char resource
[1024], **defaults
, *internal
;
538 internal
= (char *) NULL
;
540 for (i
= 0; i
< k
; i
++) {
541 strcpy(resource
, prefix
);
542 for (j
= 0; j
< count
; j
++) {
544 strcat(resource
, names
[j
]);
546 strcat(resource
, "_");
548 if (wxGetResource(wxAPP_CLASS
, (char *)resource
, v
))
551 defaults
= font_defaults
;
553 if (!strcmp(*defaults
, resource
)) {
554 internal
= defaults
[1];
562 *v
= copystring(internal
);
565 wxSuffixMap::~wxSuffixMap(void)
569 for (k
= 0; k
< wxNUM_WEIGHTS
; ++k
)
570 for (j
= 0; j
< wxNUM_STYLES
; ++j
)
573 map
[k
][j
] = (char *) NULL
;
577 void wxSuffixMap::Initialize(const char *resname
, const char *devresname
)
579 const char *weight
, *style
;
582 const char *names
[3];
584 for (k
= 0; k
< wxNUM_WEIGHTS
; k
++) {
586 case wxWEIGHT_NORMAL
: weight
= "Medium"; break;
587 case wxWEIGHT_LIGHT
: weight
= "Light"; break;
589 default: weight
= "Bold";
591 for (j
= 0; j
< wxNUM_STYLES
; j
++) {
593 case wxSTYLE_NORMAL
: style
= "Straight"; break;
594 case wxSTYLE_ITALIC
: style
= "Italic"; break;
596 default: style
= "Slant";
602 SearchResource(devresname
, names
, 3, &v
);
604 /* Expand macros in the found string: */
606 int len
, closer
= 0, startpos
= 0;
608 len
= (v
? strlen(v
) : 0);
609 for (i
= 0; i
< len
; i
++) {
610 if (v
[i
] == '$' && ((v
[i
+1] == '[') || (v
[i
+1] == '{'))) {
612 closer
= (v
[i
+1] == '[') ? ']' : '}';
614 } else if (v
[i
] == closer
) {
616 const char *r
= (char *) NULL
; bool delete_r
= FALSE
;
619 name
= v
+ startpos
+ 2;
626 for (i
= 0, count
= 1; name
[i
]; i
++)
632 names
= new char*[count
];
634 for (i
= 0, count
= 1; i
< len
; i
++)
635 if (name
[i
] == ',') {
636 names
[count
++] = name
+ i
+ 1;
640 SearchResource("", (const char **)names
, count
, (char **)&r
);
645 for (i
= 0; i
< len
; i
++)
649 printf("Bad resource name \"%s\" in font lookup\n", name
);
651 } else if (!strcmp(name
, "weight")) {
653 } else if (!strcmp(name
, "style")) {
655 } else if (!strcmp(name
, "family")) {
659 printf("Bad font macro name \"%s\"\n", name
);
663 newstrlen
= strlen(r
);
664 char *naya
= new char[startpos
+ newstrlen
+ len
- i
];
665 memcpy(naya
, v
, startpos
);
666 memcpy(naya
+ startpos
, r
, newstrlen
);
667 memcpy(naya
+ startpos
+ newstrlen
, v
+ i
+ 1, len
- i
);
676 /* We have a final value: */
682 //-----------------------------------------------------------------------------
684 //-----------------------------------------------------------------------------
686 class wxFontNameItem
: public wxObject
{
687 DECLARE_DYNAMIC_CLASS(wxFontNameItem
)
689 wxFontNameItem(const char *name
, int id
, int family
);
692 inline char* GetScreenName(int w
, int s
) {return screen
.GetName(w
, s
);}
693 inline char* GetPostScriptName(int w
, int s
) {return printing
.GetName(w
, s
);}
694 inline char* GetAFMName(int w
, int s
) {return afm
.GetName(w
, s
);}
695 inline char* GetName(void) {return name
;}
696 inline int GetFamily(void) {return family
;}
697 inline int GetId(void) {return id
;}
698 inline bool IsRoman(void) {return isroman
;}
700 void Dump(ostream
& str
);
706 wxSuffixMap screen
, printing
, afm
;
710 IMPLEMENT_ABSTRACT_CLASS(wxFontNameItem
, wxObject
)
712 wxFontNameItem::wxFontNameItem(const char *Name
, int Id
, int Family
)
714 name
= copystring(Name
);
718 screen
. Initialize(name
, "Screen");
719 printing
.Initialize(name
, "PostScript");
720 afm
. Initialize(name
, "Afm");
723 wxFontNameItem::~wxFontNameItem(void)
727 name
= (char *) NULL
;
731 void wxFontNameItem::Dump(ostream
& str
)
733 str
<< "wxFontNameItem(" << name
<< ")";
737 //-----------------------------------------------------------------------------
739 //-----------------------------------------------------------------------------
741 IMPLEMENT_DYNAMIC_CLASS(wxFontNameDirectory
, wxObject
)
743 wxFontNameDirectory::wxFontNameDirectory(void)
745 table
= new wxHashTable(wxKEY_INTEGER
, 20);
749 wxFontNameDirectory::~wxFontNameDirectory()
751 // Cleanup wxFontNameItems allocated
753 wxNode
*node
= table
->Next();
755 wxFontNameItem
*item
= (wxFontNameItem
*)node
->Data();
757 node
= table
->Next();
762 int wxFontNameDirectory::GetNewFontId(void)
764 return (nextFontId
--);
767 void wxFontNameDirectory::Initialize()
769 Initialize(wxDEFAULT
, wxDEFAULT
, "Default");
770 Initialize(wxDECORATIVE
, wxDECORATIVE
, "Decorative");
771 Initialize(wxROMAN
, wxROMAN
, "Roman");
772 Initialize(wxMODERN
, wxMODERN
, "Modern");
773 Initialize(wxTELETYPE
, wxTELETYPE
, "Teletype");
774 Initialize(wxSWISS
, wxSWISS
, "Swiss");
775 Initialize(wxSCRIPT
, wxSCRIPT
, "Script");
778 void wxFontNameDirectory::Initialize(int fontid
, int family
, const char *resname
)
780 char *fam
, resource
[256];
782 sprintf(resource
, "Family%s", resname
);
783 SearchResource((const char *)resource
, (const char **) NULL
, 0, (char **)&fam
);
785 if (!strcmp(fam
, "Default")) family
= wxDEFAULT
;
786 else if (!strcmp(fam
, "Roman")) family
= wxROMAN
;
787 else if (!strcmp(fam
, "Decorative")) family
= wxDECORATIVE
;
788 else if (!strcmp(fam
, "Modern")) family
= wxMODERN
;
789 else if (!strcmp(fam
, "Teletype")) family
= wxTELETYPE
;
790 else if (!strcmp(fam
, "Swiss")) family
= wxSWISS
;
791 else if (!strcmp(fam
, "Script")) family
= wxSCRIPT
;
792 delete[] fam
; // free resource
794 table
->Put(fontid
, new wxFontNameItem(resname
, fontid
, family
));
797 int wxFontNameDirectory::FindOrCreateFontId(const char *name
, int family
)
801 // font exists -> return id
802 if ( (id
= GetFontId(name
)) ) return id
;
804 Initialize(id
=GetNewFontId(), family
, name
);
808 char *wxFontNameDirectory::GetScreenName(int fontid
, int weight
, int style
)
810 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
); // find font
812 return item
->GetScreenName(weight
, style
);
813 // font does not exist
814 return (char *) NULL
;
817 char *wxFontNameDirectory::GetPostScriptName(int fontid
, int weight
, int style
)
819 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
); // find font
821 return item
->GetPostScriptName(weight
, style
);
822 // font does not exist
823 return (char *) NULL
;
826 char *wxFontNameDirectory::GetAFMName(int fontid
, int weight
, int style
)
828 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
); // find font
830 return item
->GetAFMName(weight
, style
);
831 // font does not exist
832 return (char *) NULL
;
835 char *wxFontNameDirectory::GetFontName(int fontid
)
837 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
); // find font
839 return item
->GetName();
840 // font does not exist
841 return (char *) NULL
;
844 int wxFontNameDirectory::GetFontId(const char *name
)
850 while ( (node
= table
->Next()) ) {
851 wxFontNameItem
*item
= (wxFontNameItem
*)node
->Data();
852 if (!strcmp(name
, item
->name
))
855 // font does not exist
859 int wxFontNameDirectory::GetFamily(int fontid
)
861 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
);
865 // font does not exist