]>
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 extern wxFontNameDirectory
*wxTheFontNameDirectory
;
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 class wxFontRefData
: public wxObjectRefData
36 wxList m_scaled_xfonts
;
38 int m_family
, m_style
, m_weight
;
49 wxFontRefData::wxFontRefData(void) : m_scaled_xfonts(wxKEY_INTEGER
)
51 m_byXFontName
= FALSE
;
58 m_faceName
= (char *) NULL
;
59 m_font
= (GdkFont
*) NULL
;
62 wxFontRefData::~wxFontRefData(void)
64 wxNode
*node
= m_scaled_xfonts
.First();
67 GdkFont
*font
= (GdkFont
*)node
->Data();
68 wxNode
*next
= node
->Next();
69 gdk_font_unref( font
);
75 m_faceName
= (char *) NULL
;
77 if (m_font
) gdk_font_unref( m_font
);
80 //-----------------------------------------------------------------------------
82 #define M_FONTDATA ((wxFontRefData *)m_refData)
84 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
88 if (wxTheFontList
) wxTheFontList
->Append( this );
91 wxFont::wxFont( char *xFontName
)
93 if (!xFontName
) return;
95 m_refData
= new wxFontRefData();
97 M_FONTDATA
->m_byXFontName
= TRUE
;
98 M_FONTDATA
->m_font
= gdk_font_load( xFontName
);
101 wxFont::wxFont(int PointSize
, int FontIdOrFamily
, int Style
, int Weight
,
102 bool Underlined
, const char* Face
)
104 m_refData
= new wxFontRefData();
106 if ((M_FONTDATA
->m_faceName
= (Face
) ? copystring(Face
) : (char*)NULL
) )
108 M_FONTDATA
->m_fontId
= wxTheFontNameDirectory
->FindOrCreateFontId( Face
, FontIdOrFamily
);
109 M_FONTDATA
->m_family
= wxTheFontNameDirectory
->GetFamily( FontIdOrFamily
);
113 M_FONTDATA
->m_fontId
= FontIdOrFamily
;
114 M_FONTDATA
->m_family
= wxTheFontNameDirectory
->GetFamily( FontIdOrFamily
);
116 if (Style
== wxDEFAULT
) Style
= wxSWISS
;
117 M_FONTDATA
->m_style
= Style
;
118 if (Weight
== wxDEFAULT
) Weight
= wxNORMAL
;
119 M_FONTDATA
->m_weight
= Weight
;
120 if (PointSize
== wxDEFAULT
) PointSize
= 10;
121 M_FONTDATA
->m_pointSize
= PointSize
;
122 M_FONTDATA
->m_underlined
= Underlined
;
124 if (wxTheFontList
) wxTheFontList
->Append( this );
127 wxFont::wxFont(int PointSize
, const char *Face
, int Family
, int Style
,
128 int Weight
, bool Underlined
)
130 m_refData
= new wxFontRefData();
132 M_FONTDATA
->m_fontId
= wxTheFontNameDirectory
->FindOrCreateFontId( Face
, Family
);
133 M_FONTDATA
->m_faceName
= (Face
) ? copystring(Face
) : (char*)NULL
;
134 M_FONTDATA
->m_family
= wxTheFontNameDirectory
->GetFamily( M_FONTDATA
->m_fontId
);
135 M_FONTDATA
->m_style
= Style
;
136 M_FONTDATA
->m_weight
= Weight
;
137 M_FONTDATA
->m_pointSize
= PointSize
;
138 M_FONTDATA
->m_underlined
= Underlined
;
140 if (wxTheFontList
) wxTheFontList
->Append( this );
143 wxFont::wxFont( const wxFont
& font
)
147 if (wxTheFontList
) wxTheFontList
->Append( this );
150 wxFont::wxFont( const wxFont
* font
)
153 if (font
) Ref( *font
);
155 if (wxTheFontList
) wxTheFontList
->Append( this );
158 wxFont::~wxFont(void)
160 if (wxTheFontList
) wxTheFontList
->DeleteObject( this );
163 wxFont
& wxFont::operator = ( const wxFont
& font
)
165 if (*this == font
) return (*this);
170 bool wxFont::operator == ( const wxFont
& font
)
172 return m_refData
== font
.m_refData
;
175 bool wxFont::operator != ( const wxFont
& font
)
177 return m_refData
!= font
.m_refData
;
180 bool wxFont::Ok() const
184 wxFAIL_MSG( "invalid font" );
191 int wxFont::GetPointSize(void) const
195 return M_FONTDATA
->m_pointSize
;
198 wxString
wxFont::GetFaceString(void) const
200 if (!Ok()) return "";
202 wxString s
= wxTheFontNameDirectory
->GetFontName( M_FONTDATA
->m_fontId
);
206 wxString
wxFont::GetFaceName(void) const
208 if (!Ok()) return "";
210 wxString s
= wxTheFontNameDirectory
->GetFontName( M_FONTDATA
->m_fontId
);
214 int wxFont::GetFamily(void) const
218 return M_FONTDATA
->m_family
;
221 wxString
wxFont::GetFamilyString(void) const
223 if (!Ok()) return "wxDEFAULT";
225 switch (M_FONTDATA
->m_family
)
227 case wxDECORATIVE
: return wxString("wxDECORATIVE");
228 case wxROMAN
: return wxString("wxROMAN");
229 case wxSCRIPT
: return wxString("wxSCRIPT");
230 case wxSWISS
: return wxString("wxSWISS");
231 case wxMODERN
: return wxString("wxMODERN");
232 case wxTELETYPE
: return wxString("wxTELETYPE");
233 default: return "wxDEFAULT";
239 int wxFont::GetFontId(void) const
243 return M_FONTDATA
->m_fontId
; // stub
246 int wxFont::GetStyle(void) const
250 return M_FONTDATA
->m_style
;
253 wxString
wxFont::GetStyleString(void) const
255 if (!Ok()) return "wxDEFAULT";
257 switch (M_FONTDATA
->m_style
)
259 case wxNORMAL
: return wxString("wxNORMAL");
260 case wxSLANT
: return wxString("wxSLANT");
261 case wxITALIC
: return wxString("wxITALIC");
262 default: return wxString("wxDEFAULT");
265 return wxString("wxDEFAULT");
268 int wxFont::GetWeight(void) const
272 return M_FONTDATA
->m_weight
;
275 wxString
wxFont::GetWeightString(void) const
277 if (!Ok()) return "wxDEFAULT";
279 switch (M_FONTDATA
->m_weight
)
281 case wxNORMAL
: return wxString("wxNORMAL");
282 case wxBOLD
: return wxString("wxBOLD");
283 case wxLIGHT
: return wxString("wxLIGHT");
284 default: return wxString("wxDEFAULT");
287 return wxString("wxDEFAULT");
290 bool wxFont::GetUnderlined(void) const
292 if (!Ok()) return FALSE
;
294 return M_FONTDATA
->m_underlined
;
297 //-----------------------------------------------------------------------------
298 // get internal representation of font
299 //-----------------------------------------------------------------------------
301 // local help function
302 static GdkFont
*wxLoadQueryNearestFont(int point_size
, int fontid
,
303 int style
, int weight
,
306 GdkFont
*wxFont::GetInternalFont(float scale
) const
308 if (M_FONTDATA
->m_byXFontName
) return M_FONTDATA
->m_font
;
310 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
311 int point_scale
= (M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100;
312 GdkFont
*font
= (GdkFont
*) NULL
;
314 wxNode
*node
= M_FONTDATA
->m_scaled_xfonts
.Find(int_scale
);
317 font
= (GdkFont
*)node
->Data();
321 font
= wxLoadQueryNearestFont( point_scale
, M_FONTDATA
->m_fontId
, M_FONTDATA
->m_style
,
322 M_FONTDATA
->m_weight
, M_FONTDATA
->m_underlined
);
323 M_FONTDATA
->m_scaled_xfonts
.Append( int_scale
, (wxObject
*)font
);
326 printf("could not load any font");
327 // wxError("could not load any font", "wxFont");
331 //-----------------------------------------------------------------------------
332 // local utilities to find a X font
333 //-----------------------------------------------------------------------------
335 static GdkFont
*wxLoadQueryFont(int point_size
, int fontid
, int style
,
336 int weight
, bool WXUNUSED(underlined
))
339 char *name
= wxTheFontNameDirectory
->GetScreenName( fontid
, weight
, style
);
342 name
= "-*-*-*-*-*-*-*-%d-*-*-*-*-*-*";
343 sprintf(buffer
, name
, point_size
);
345 return gdk_font_load( buffer
);
348 static GdkFont
*wxLoadQueryNearestFont(int point_size
, int fontid
,
349 int style
, int weight
,
354 font
= wxLoadQueryFont( point_size
, fontid
, style
, weight
, underlined
);
357 // search up and down by stepsize 10
358 int max_size
= point_size
+ 20 * (1 + (point_size
/180));
359 int min_size
= point_size
- 20 * (1 + (point_size
/180));
362 // Search for smaller size (approx.)
363 for (i
=point_size
-10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10)
364 font
= wxLoadQueryFont(i
, fontid
, style
, weight
, underlined
);
365 // Search for larger size (approx.)
366 for (i
=point_size
+10; !font
&& i
<= max_size
; i
+= 10)
367 font
= wxLoadQueryFont(i
, fontid
, style
, weight
, underlined
);
368 // Try default family
369 if (!font
&& fontid
!= wxDEFAULT
)
370 font
= wxLoadQueryFont(point_size
, wxDEFAULT
, style
,
374 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
380 //-----------------------------------------------------------------------------
381 // face names and index functions
382 //-----------------------------------------------------------------------------
384 static char *font_defaults
[] = {
385 "FamilyDefault", "Default",
386 "FamilyRoman", "Roman",
387 "FamilyDecorative", "Decorative",
388 "FamilyModern", "Modern",
389 "FamilyTeletype", "Teletype",
390 "FamilySwiss", "Swiss",
391 "FamilyScript", "Script",
397 "AfmItalic", "${AfmSlant}",
401 "AfmHelvetica", "Helv",
402 "AfmCourier", "Cour",
404 "Afm___", "${AfmTimes,$[weight],$[style]}",
406 "AfmTimes__", "${AfmTimes}${Afm$[weight]}${Afm$[style]}",
407 "AfmTimesMediumStraight", "${AfmTimes}${AfmRoman}",
408 "AfmTimesLightStraight", "${AfmTimes}${AfmRoman}",
409 "AfmTimes_Italic", "${AfmTimes}$[weight]${AfmItalic}",
410 "AfmTimes_Slant", "${AfmTimes}$[weight]${AfmItalic}",
412 "AfmSwiss__", "${AfmHelvetica}${Afm$[weight]}${Afm$[style]}",
413 "AfmModern__", "${AfmCourier}${Afm$[weight]}${Afm$[style]}",
415 "AfmTeletype__", "${AfmModern,$[weight],$[style]}",
417 "PostScriptMediumStraight", "",
418 "PostScriptMediumItalic", "-Oblique",
419 "PostScriptMediumSlant", "-Oblique",
420 "PostScriptLightStraight", "",
421 "PostScriptLightItalic", "-Oblique",
422 "PostScriptLightSlant", "-Oblique",
423 "PostScriptBoldStraight", "-Bold",
424 "PostScriptBoldItalic", "-BoldOblique",
425 "PostScriptBoldSlant", "-BoldOblique",
427 #if WX_NORMALIZED_PS_FONTS
428 "PostScript___", "${PostScriptTimes,$[weight],$[style]}",
430 "PostScriptRoman__", "${PostScriptTimes,$[weight],$[style]}",
431 "PostScript___", "LucidaSans${PostScript$[weight]$[style]}",
434 "PostScriptTimesMedium", "",
435 "PostScriptTimesLight", "",
436 "PostScriptTimesBold", "Bold",
438 "PostScriptTimes__", "Times${PostScript$[weight]$[style]}",
439 "PostScriptTimesMediumStraight", "Times-Roman",
440 "PostScriptTimesLightStraight", "Times-Roman",
441 "PostScriptTimes_Slant", "Times-${PostScriptTimes$[weight]}Italic",
442 "PostScriptTimes_Italic", "Times-${PostScriptTimes$[weight]}Italic",
444 "PostScriptSwiss__", "Helvetica${PostScript$[weight]$[style]}",
445 "PostScriptModern__", "Courier${PostScript$[weight]$[style]}",
447 "PostScriptTeletype__", "${PostScriptModern,$[weight],$[style]}",
449 #if !WX_NORMALIZED_PS_FONTS
450 "PostScriptScript__", "Zapf-Chancery-MediumItalic",
453 "ScreenMedium", "medium",
454 "ScreenBold", "bold",
455 "ScreenLight", "light",
456 "ScreenStraight", "r",
460 "ScreenDefaultBase", "misc-fixed",
461 "ScreenRomanBase", "*-times",
462 "ScreenDecorativeBase", "*-helvetica",
463 "ScreenModernBase", "*-courier",
464 "ScreenTeletypeBase", "*-lucidatypewriter",
465 "ScreenSwissBase", "*-lucida",
466 "ScreenScriptBase", "*-zapfchancery",
468 "ScreenStdSuffix", "-${Screen$[weight]}-${Screen$[style]}"
469 "-normal-*-*-%d-*-*-*-*-*-*",
472 "-${ScreenDefaultBase}${ScreenStdSuffix}",
474 "-${ScreenRomanBase}${ScreenStdSuffix}",
475 "ScreenDecorative__",
476 "-${ScreenDecorativeBase}${ScreenStdSuffix}",
478 "-${ScreenModernBase}${ScreenStdSuffix}",
480 "-${ScreenTeletypeBase}${ScreenStdSuffix}",
482 "-${ScreenSwissBase}${ScreenStdSuffix}",
484 "-${ScreenScriptBase}${ScreenStdSuffix}",
488 enum {wxWEIGHT_NORMAL
, wxWEIGHT_BOLD
, wxWEIGHT_LIGHT
, wxNUM_WEIGHTS
};
489 enum {wxSTYLE_NORMAL
, wxSTYLE_ITALIC
, wxSTYLE_SLANT
, wxNUM_STYLES
};
491 static int WCoordinate(int w
)
494 case wxBOLD
: return wxWEIGHT_BOLD
;
495 case wxLIGHT
: return wxWEIGHT_LIGHT
;
497 default: return wxWEIGHT_NORMAL
;
501 static int SCoordinate(int s
)
504 case wxITALIC
: return wxSTYLE_ITALIC
;
505 case wxSLANT
: return wxSTYLE_SLANT
;
507 default: return wxSTYLE_NORMAL
;
511 //-----------------------------------------------------------------------------
513 //-----------------------------------------------------------------------------
519 inline char *GetName(int weight
, int style
)
521 return ( map
[WCoordinate(weight
)] [SCoordinate(style
)] );
524 char *map
[wxNUM_WEIGHTS
][wxNUM_STYLES
];
525 void Initialize(const char *, const char *);
529 #define wxGetResource(a, b, c) 0
532 static void SearchResource(const char *prefix
, const char **names
, int count
, char **v
)
535 char resource
[1024], **defaults
, *internal
;
540 internal
= (char *) NULL
;
542 for (i
= 0; i
< k
; i
++) {
543 strcpy(resource
, prefix
);
544 for (j
= 0; j
< count
; j
++) {
546 strcat(resource
, names
[j
]);
548 strcat(resource
, "_");
550 if (wxGetResource(wxAPP_CLASS
, (char *)resource
, v
))
553 defaults
= font_defaults
;
555 if (!strcmp(*defaults
, resource
)) {
556 internal
= defaults
[1];
564 *v
= copystring(internal
);
567 wxSuffixMap::~wxSuffixMap(void)
571 for (k
= 0; k
< wxNUM_WEIGHTS
; ++k
)
572 for (j
= 0; j
< wxNUM_STYLES
; ++j
)
575 map
[k
][j
] = (char *) NULL
;
579 void wxSuffixMap::Initialize(const char *resname
, const char *devresname
)
581 const char *weight
, *style
;
584 const char *names
[3];
586 for (k
= 0; k
< wxNUM_WEIGHTS
; k
++) {
588 case wxWEIGHT_NORMAL
: weight
= "Medium"; break;
589 case wxWEIGHT_LIGHT
: weight
= "Light"; break;
591 default: weight
= "Bold";
593 for (j
= 0; j
< wxNUM_STYLES
; j
++) {
595 case wxSTYLE_NORMAL
: style
= "Straight"; break;
596 case wxSTYLE_ITALIC
: style
= "Italic"; break;
598 default: style
= "Slant";
604 SearchResource(devresname
, names
, 3, &v
);
606 /* Expand macros in the found string: */
608 int len
, closer
= 0, startpos
= 0;
610 len
= (v
? strlen(v
) : 0);
611 for (i
= 0; i
< len
; i
++) {
612 if (v
[i
] == '$' && ((v
[i
+1] == '[') || (v
[i
+1] == '{'))) {
614 closer
= (v
[i
+1] == '[') ? ']' : '}';
616 } else if (v
[i
] == closer
) {
618 const char *r
= (char *) NULL
; bool delete_r
= FALSE
;
621 name
= v
+ startpos
+ 2;
628 for (i
= 0, count
= 1; name
[i
]; i
++)
634 names
= new char*[count
];
636 for (i
= 0, count
= 1; i
< len
; i
++)
637 if (name
[i
] == ',') {
638 names
[count
++] = name
+ i
+ 1;
642 SearchResource("", (const char **)names
, count
, (char **)&r
);
647 for (i
= 0; i
< len
; i
++)
651 printf("Bad resource name \"%s\" in font lookup\n", name
);
653 } else if (!strcmp(name
, "weight")) {
655 } else if (!strcmp(name
, "style")) {
657 } else if (!strcmp(name
, "family")) {
661 printf("Bad font macro name \"%s\"\n", name
);
665 newstrlen
= strlen(r
);
666 char *naya
= new char[startpos
+ newstrlen
+ len
- i
];
667 memcpy(naya
, v
, startpos
);
668 memcpy(naya
+ startpos
, r
, newstrlen
);
669 memcpy(naya
+ startpos
+ newstrlen
, v
+ i
+ 1, len
- i
);
678 /* We have a final value: */
684 //-----------------------------------------------------------------------------
686 //-----------------------------------------------------------------------------
688 class wxFontNameItem
: public wxObject
{
689 DECLARE_DYNAMIC_CLASS(wxFontNameItem
)
691 wxFontNameItem(const char *name
, int id
, int family
);
694 inline char* GetScreenName(int w
, int s
) {return screen
.GetName(w
, s
);}
695 inline char* GetPostScriptName(int w
, int s
) {return printing
.GetName(w
, s
);}
696 inline char* GetAFMName(int w
, int s
) {return afm
.GetName(w
, s
);}
697 inline char* GetName(void) {return name
;}
698 inline int GetFamily(void) {return family
;}
699 inline int GetId(void) {return id
;}
700 inline bool IsRoman(void) {return isroman
;}
702 void Dump(ostream
& str
);
708 wxSuffixMap screen
, printing
, afm
;
712 IMPLEMENT_ABSTRACT_CLASS(wxFontNameItem
, wxObject
)
714 wxFontNameItem::wxFontNameItem(const char *Name
, int Id
, int Family
)
716 name
= copystring(Name
);
720 screen
. Initialize(name
, "Screen");
721 printing
.Initialize(name
, "PostScript");
722 afm
. Initialize(name
, "Afm");
725 wxFontNameItem::~wxFontNameItem(void)
729 name
= (char *) NULL
;
733 void wxFontNameItem::Dump(ostream
& str
)
735 str
<< "wxFontNameItem(" << name
<< ")";
739 //-----------------------------------------------------------------------------
741 //-----------------------------------------------------------------------------
743 IMPLEMENT_DYNAMIC_CLASS(wxFontNameDirectory
, wxObject
)
745 wxFontNameDirectory::wxFontNameDirectory(void)
747 table
= new wxHashTable(wxKEY_INTEGER
, 20);
751 wxFontNameDirectory::~wxFontNameDirectory()
753 // Cleanup wxFontNameItems allocated
755 wxNode
*node
= table
->Next();
757 wxFontNameItem
*item
= (wxFontNameItem
*)node
->Data();
759 node
= table
->Next();
764 int wxFontNameDirectory::GetNewFontId(void)
766 return (nextFontId
--);
769 void wxFontNameDirectory::Initialize()
771 Initialize(wxDEFAULT
, wxDEFAULT
, "Default");
772 Initialize(wxDECORATIVE
, wxDECORATIVE
, "Decorative");
773 Initialize(wxROMAN
, wxROMAN
, "Roman");
774 Initialize(wxMODERN
, wxMODERN
, "Modern");
775 Initialize(wxTELETYPE
, wxTELETYPE
, "Teletype");
776 Initialize(wxSWISS
, wxSWISS
, "Swiss");
777 Initialize(wxSCRIPT
, wxSCRIPT
, "Script");
780 void wxFontNameDirectory::Initialize(int fontid
, int family
, const char *resname
)
782 char *fam
, resource
[256];
784 sprintf(resource
, "Family%s", resname
);
785 SearchResource((const char *)resource
, (const char **) NULL
, 0, (char **)&fam
);
787 if (!strcmp(fam
, "Default")) family
= wxDEFAULT
;
788 else if (!strcmp(fam
, "Roman")) family
= wxROMAN
;
789 else if (!strcmp(fam
, "Decorative")) family
= wxDECORATIVE
;
790 else if (!strcmp(fam
, "Modern")) family
= wxMODERN
;
791 else if (!strcmp(fam
, "Teletype")) family
= wxTELETYPE
;
792 else if (!strcmp(fam
, "Swiss")) family
= wxSWISS
;
793 else if (!strcmp(fam
, "Script")) family
= wxSCRIPT
;
794 delete[] fam
; // free resource
796 table
->Put(fontid
, new wxFontNameItem(resname
, fontid
, family
));
799 int wxFontNameDirectory::FindOrCreateFontId(const char *name
, int family
)
803 // font exists -> return id
804 if ( (id
= GetFontId(name
)) ) return id
;
806 Initialize(id
=GetNewFontId(), family
, name
);
810 char *wxFontNameDirectory::GetScreenName(int fontid
, int weight
, int style
)
812 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
); // find font
814 return item
->GetScreenName(weight
, style
);
815 // font does not exist
816 return (char *) NULL
;
819 char *wxFontNameDirectory::GetPostScriptName(int fontid
, int weight
, int style
)
821 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
); // find font
823 return item
->GetPostScriptName(weight
, style
);
824 // font does not exist
825 return (char *) NULL
;
828 char *wxFontNameDirectory::GetAFMName(int fontid
, int weight
, int style
)
830 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
); // find font
832 return item
->GetAFMName(weight
, style
);
833 // font does not exist
834 return (char *) NULL
;
837 char *wxFontNameDirectory::GetFontName(int fontid
)
839 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
); // find font
841 return item
->GetName();
842 // font does not exist
843 return (char *) NULL
;
846 int wxFontNameDirectory::GetFontId(const char *name
)
852 while ( (node
= table
->Next()) ) {
853 wxFontNameItem
*item
= (wxFontNameItem
*)node
->Data();
854 if (!strcmp(name
, item
->name
))
857 // font does not exist
861 int wxFontNameDirectory::GetFamily(int fontid
)
863 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
);
867 // font does not exist