]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/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",
28 static char *wx_font_style
[] = {
29 "wxDEFAULT", "wxNORMAL", "wxSLANT", "wxITALIC",
32 static char *wx_font_weight
[] = {
33 "wxDEFAULT", "wxNORMAL", "wxBOLD", "wxLIGHT",
36 extern wxFontNameDirectory
*wxTheFontNameDirectory
;
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 class wxFontRefData
: public wxObjectRefData
49 wxList m_scaled_xfonts
;
51 int m_family
, m_style
, m_weight
;
62 wxFontRefData::wxFontRefData(void) : m_scaled_xfonts(wxKEY_INTEGER
)
64 m_byXFontName
= FALSE
;
71 m_faceName
= (char *) NULL
;
72 m_font
= (GdkFont
*) NULL
;
75 wxFontRefData::~wxFontRefData(void)
77 wxNode
*node
= m_scaled_xfonts
.First();
80 GdkFont
*font
= (GdkFont
*)node
->Data();
81 wxNode
*next
= node
->Next();
82 gdk_font_unref( font
);
88 m_faceName
= (char *) NULL
;
90 if (m_font
) gdk_font_unref( m_font
);
93 //-----------------------------------------------------------------------------
95 #define M_FONTDATA ((wxFontRefData *)m_refData)
97 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
101 if (wxTheFontList
) wxTheFontList
->Append( this );
104 wxFont::wxFont( char *xFontName
)
106 if (!xFontName
) return;
108 m_refData
= new wxFontRefData();
110 M_FONTDATA
->m_byXFontName
= TRUE
;
111 M_FONTDATA
->m_font
= gdk_font_load( xFontName
);
114 wxFont::wxFont(int PointSize
, int FontIdOrFamily
, int Style
, int Weight
,
115 bool Underlined
, const char* Face
)
117 m_refData
= new wxFontRefData();
119 if ((M_FONTDATA
->m_faceName
= (Face
) ? copystring(Face
) : (char*)NULL
) )
121 M_FONTDATA
->m_fontId
= wxTheFontNameDirectory
->FindOrCreateFontId( Face
, FontIdOrFamily
);
122 M_FONTDATA
->m_family
= wxTheFontNameDirectory
->GetFamily( FontIdOrFamily
);
126 M_FONTDATA
->m_fontId
= FontIdOrFamily
;
127 M_FONTDATA
->m_family
= wxTheFontNameDirectory
->GetFamily( FontIdOrFamily
);
129 M_FONTDATA
->m_style
= Style
;
130 M_FONTDATA
->m_weight
= Weight
;
131 M_FONTDATA
->m_pointSize
= PointSize
;
132 M_FONTDATA
->m_underlined
= Underlined
;
134 if (wxTheFontList
) wxTheFontList
->Append( this );
137 wxFont::wxFont(int PointSize
, const char *Face
, int Family
, int Style
,
138 int Weight
, bool Underlined
)
140 m_refData
= new wxFontRefData();
142 M_FONTDATA
->m_fontId
= wxTheFontNameDirectory
->FindOrCreateFontId( Face
, Family
);
143 M_FONTDATA
->m_faceName
= (Face
) ? copystring(Face
) : (char*)NULL
;
144 M_FONTDATA
->m_family
= wxTheFontNameDirectory
->GetFamily( M_FONTDATA
->m_fontId
);
145 M_FONTDATA
->m_style
= Style
;
146 M_FONTDATA
->m_weight
= Weight
;
147 M_FONTDATA
->m_pointSize
= PointSize
;
148 M_FONTDATA
->m_underlined
= Underlined
;
150 if (wxTheFontList
) wxTheFontList
->Append( this );
153 wxFont::wxFont( const wxFont
& font
)
157 if (wxTheFontList
) wxTheFontList
->Append( this );
160 wxFont::wxFont( const wxFont
* font
)
163 if (font
) Ref( *font
);
165 if (wxTheFontList
) wxTheFontList
->Append( this );
168 wxFont::~wxFont(void)
170 if (wxTheFontList
) wxTheFontList
->DeleteObject( this );
173 wxFont
& wxFont::operator = ( const wxFont
& font
)
175 if (*this == font
) return (*this);
180 bool wxFont::operator == ( const wxFont
& font
)
182 return m_refData
== font
.m_refData
;
185 bool wxFont::operator != ( const wxFont
& font
)
187 return m_refData
!= font
.m_refData
;
192 return (m_refData
!= NULL
);
195 int wxFont::GetPointSize(void) const
197 return M_FONTDATA
->m_pointSize
;
200 wxString
wxFont::GetFaceString(void) const
202 wxString s
= wxTheFontNameDirectory
->GetFontName( M_FONTDATA
->m_fontId
);
206 wxString
wxFont::GetFaceName(void) const
208 wxString s
= wxTheFontNameDirectory
->GetFontName( M_FONTDATA
->m_fontId
);
212 int wxFont::GetFamily(void) const
214 return M_FONTDATA
->m_family
;
217 wxString
wxFont::GetFamilyString(void) const
219 wxString s
= wx_font_family
[M_FONTDATA
->m_family
];
223 int wxFont::GetFontId(void) const
225 return M_FONTDATA
->m_fontId
; // stub
228 int wxFont::GetStyle(void) const
230 return M_FONTDATA
->m_style
;
233 wxString
wxFont::GetStyleString(void) const
235 wxString s
= wx_font_style
[M_FONTDATA
->m_style
];
239 int wxFont::GetWeight(void) const
241 return M_FONTDATA
->m_weight
;
244 wxString
wxFont::GetWeightString(void) const
246 wxString s
= wx_font_weight
[M_FONTDATA
->m_weight
];
250 bool wxFont::GetUnderlined(void) const
252 return M_FONTDATA
->m_underlined
;
255 //-----------------------------------------------------------------------------
256 // get internal representation of font
257 //-----------------------------------------------------------------------------
259 // local help function
260 static GdkFont
*wxLoadQueryNearestFont(int point_size
, int fontid
,
261 int style
, int weight
,
264 GdkFont
*wxFont::GetInternalFont(float scale
) const
266 if (M_FONTDATA
->m_byXFontName
) return M_FONTDATA
->m_font
;
268 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
269 int point_scale
= (M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100;
270 GdkFont
*font
= (GdkFont
*) NULL
;
272 wxNode
*node
= M_FONTDATA
->m_scaled_xfonts
.Find(int_scale
);
275 font
= (GdkFont
*)node
->Data();
279 font
= wxLoadQueryNearestFont( point_scale
, M_FONTDATA
->m_fontId
, M_FONTDATA
->m_style
,
280 M_FONTDATA
->m_weight
, M_FONTDATA
->m_underlined
);
281 M_FONTDATA
->m_scaled_xfonts
.Append( int_scale
, (wxObject
*)font
);
284 printf("could not load any font");
285 // wxError("could not load any font", "wxFont");
289 //-----------------------------------------------------------------------------
290 // local utilities to find a X font
291 //-----------------------------------------------------------------------------
293 static GdkFont
*wxLoadQueryFont(int point_size
, int fontid
, int style
,
294 int weight
, bool WXUNUSED(underlined
))
297 char *name
= wxTheFontNameDirectory
->GetScreenName( fontid
, weight
, style
);
300 name
= "-*-*-*-*-*-*-*-%d-*-*-*-*-*-*";
301 sprintf(buffer
, name
, point_size
);
303 return gdk_font_load( buffer
);
306 static GdkFont
*wxLoadQueryNearestFont(int point_size
, int fontid
,
307 int style
, int weight
,
312 font
= wxLoadQueryFont( point_size
, fontid
, style
, weight
, underlined
);
315 // search up and down by stepsize 10
316 int max_size
= point_size
+ 20 * (1 + (point_size
/180));
317 int min_size
= point_size
- 20 * (1 + (point_size
/180));
320 // Search for smaller size (approx.)
321 for (i
=point_size
-10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10)
322 font
= wxLoadQueryFont(i
, fontid
, style
, weight
, underlined
);
323 // Search for larger size (approx.)
324 for (i
=point_size
+10; !font
&& i
<= max_size
; i
+= 10)
325 font
= wxLoadQueryFont(i
, fontid
, style
, weight
, underlined
);
326 // Try default family
327 if (!font
&& fontid
!= wxDEFAULT
)
328 font
= wxLoadQueryFont(point_size
, wxDEFAULT
, style
,
332 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
338 //-----------------------------------------------------------------------------
339 // face names and index functions
340 //-----------------------------------------------------------------------------
342 static char *font_defaults
[] = {
343 "FamilyDefault", "Default",
344 "FamilyRoman", "Roman",
345 "FamilyDecorative", "Decorative",
346 "FamilyModern", "Modern",
347 "FamilyTeletype", "Teletype",
348 "FamilySwiss", "Swiss",
349 "FamilyScript", "Script",
355 "AfmItalic", "${AfmSlant}",
359 "AfmHelvetica", "Helv",
360 "AfmCourier", "Cour",
362 "Afm___", "${AfmTimes,$[weight],$[style]}",
364 "AfmTimes__", "${AfmTimes}${Afm$[weight]}${Afm$[style]}",
365 "AfmTimesMediumStraight", "${AfmTimes}${AfmRoman}",
366 "AfmTimesLightStraight", "${AfmTimes}${AfmRoman}",
367 "AfmTimes_Italic", "${AfmTimes}$[weight]${AfmItalic}",
368 "AfmTimes_Slant", "${AfmTimes}$[weight]${AfmItalic}",
370 "AfmSwiss__", "${AfmHelvetica}${Afm$[weight]}${Afm$[style]}",
371 "AfmModern__", "${AfmCourier}${Afm$[weight]}${Afm$[style]}",
373 "AfmTeletype__", "${AfmModern,$[weight],$[style]}",
375 "PostScriptMediumStraight", "",
376 "PostScriptMediumItalic", "-Oblique",
377 "PostScriptMediumSlant", "-Oblique",
378 "PostScriptLightStraight", "",
379 "PostScriptLightItalic", "-Oblique",
380 "PostScriptLightSlant", "-Oblique",
381 "PostScriptBoldStraight", "-Bold",
382 "PostScriptBoldItalic", "-BoldOblique",
383 "PostScriptBoldSlant", "-BoldOblique",
385 #if WX_NORMALIZED_PS_FONTS
386 "PostScript___", "${PostScriptTimes,$[weight],$[style]}",
388 "PostScriptRoman__", "${PostScriptTimes,$[weight],$[style]}",
389 "PostScript___", "LucidaSans${PostScript$[weight]$[style]}",
392 "PostScriptTimesMedium", "",
393 "PostScriptTimesLight", "",
394 "PostScriptTimesBold", "Bold",
396 "PostScriptTimes__", "Times${PostScript$[weight]$[style]}",
397 "PostScriptTimesMediumStraight", "Times-Roman",
398 "PostScriptTimesLightStraight", "Times-Roman",
399 "PostScriptTimes_Slant", "Times-${PostScriptTimes$[weight]}Italic",
400 "PostScriptTimes_Italic", "Times-${PostScriptTimes$[weight]}Italic",
402 "PostScriptSwiss__", "Helvetica${PostScript$[weight]$[style]}",
403 "PostScriptModern__", "Courier${PostScript$[weight]$[style]}",
405 "PostScriptTeletype__", "${PostScriptModern,$[weight],$[style]}",
407 #if !WX_NORMALIZED_PS_FONTS
408 "PostScriptScript__", "Zapf-Chancery-MediumItalic",
411 "ScreenMedium", "medium",
412 "ScreenBold", "bold",
413 "ScreenLight", "light",
414 "ScreenStraight", "r",
418 "ScreenDefaultBase", "misc-fixed",
419 "ScreenRomanBase", "*-times",
420 "ScreenDecorativeBase", "*-helvetica",
421 "ScreenModernBase", "*-courier",
422 "ScreenTeletypeBase", "*-lucidatypewriter",
423 "ScreenSwissBase", "*-lucida",
424 "ScreenScriptBase", "*-zapfchancery",
426 "ScreenStdSuffix", "-${Screen$[weight]}-${Screen$[style]}"
427 "-normal-*-*-%d-*-*-*-*-*-*",
430 "-${ScreenDefaultBase}${ScreenStdSuffix}",
432 "-${ScreenRomanBase}${ScreenStdSuffix}",
433 "ScreenDecorative__",
434 "-${ScreenDecorativeBase}${ScreenStdSuffix}",
436 "-${ScreenModernBase}${ScreenStdSuffix}",
438 "-${ScreenTeletypeBase}${ScreenStdSuffix}",
440 "-${ScreenSwissBase}${ScreenStdSuffix}",
442 "-${ScreenScriptBase}${ScreenStdSuffix}",
446 enum {wxWEIGHT_NORMAL
, wxWEIGHT_BOLD
, wxWEIGHT_LIGHT
, wxNUM_WEIGHTS
};
447 enum {wxSTYLE_NORMAL
, wxSTYLE_ITALIC
, wxSTYLE_SLANT
, wxNUM_STYLES
};
449 static int WCoordinate(int w
)
452 case wxBOLD
: return wxWEIGHT_BOLD
;
453 case wxLIGHT
: return wxWEIGHT_LIGHT
;
455 default: return wxWEIGHT_NORMAL
;
459 static int SCoordinate(int s
)
462 case wxITALIC
: return wxSTYLE_ITALIC
;
463 case wxSLANT
: return wxSTYLE_SLANT
;
465 default: return wxSTYLE_NORMAL
;
469 //-----------------------------------------------------------------------------
471 //-----------------------------------------------------------------------------
477 inline char *GetName(int weight
, int style
)
479 return ( map
[WCoordinate(weight
)] [SCoordinate(style
)] );
482 char *map
[wxNUM_WEIGHTS
][wxNUM_STYLES
];
483 void Initialize(const char *, const char *);
487 #define wxGetResource(a, b, c) 0
490 static void SearchResource(const char *prefix
, const char **names
, int count
, char **v
)
493 char resource
[1024], **defaults
, *internal
;
498 internal
= (char *) NULL
;
500 for (i
= 0; i
< k
; i
++) {
501 strcpy(resource
, prefix
);
502 for (j
= 0; j
< count
; j
++) {
504 strcat(resource
, names
[j
]);
506 strcat(resource
, "_");
508 if (wxGetResource(wxAPP_CLASS
, (char *)resource
, v
))
511 defaults
= font_defaults
;
513 if (!strcmp(*defaults
, resource
)) {
514 internal
= defaults
[1];
522 *v
= copystring(internal
);
525 wxSuffixMap::~wxSuffixMap(void)
529 for (k
= 0; k
< wxNUM_WEIGHTS
; ++k
)
530 for (j
= 0; j
< wxNUM_STYLES
; ++j
)
533 map
[k
][j
] = (char *) NULL
;
537 void wxSuffixMap::Initialize(const char *resname
, const char *devresname
)
539 const char *weight
, *style
;
542 const char *names
[3];
544 for (k
= 0; k
< wxNUM_WEIGHTS
; k
++) {
546 case wxWEIGHT_NORMAL
: weight
= "Medium"; break;
547 case wxWEIGHT_LIGHT
: weight
= "Light"; break;
549 default: weight
= "Bold";
551 for (j
= 0; j
< wxNUM_STYLES
; j
++) {
553 case wxSTYLE_NORMAL
: style
= "Straight"; break;
554 case wxSTYLE_ITALIC
: style
= "Italic"; break;
556 default: style
= "Slant";
562 SearchResource(devresname
, names
, 3, &v
);
564 /* Expand macros in the found string: */
566 int len
, closer
= 0, startpos
= 0;
568 len
= (v
? strlen(v
) : 0);
569 for (i
= 0; i
< len
; i
++) {
570 if (v
[i
] == '$' && ((v
[i
+1] == '[') || (v
[i
+1] == '{'))) {
572 closer
= (v
[i
+1] == '[') ? ']' : '}';
574 } else if (v
[i
] == closer
) {
576 const char *r
= (char *) NULL
; bool delete_r
= FALSE
;
579 name
= v
+ startpos
+ 2;
586 for (i
= 0, count
= 1; name
[i
]; i
++)
592 names
= new char*[count
];
594 for (i
= 0, count
= 1; i
< len
; i
++)
595 if (name
[i
] == ',') {
596 names
[count
++] = name
+ i
+ 1;
600 SearchResource("", (const char **)names
, count
, (char **)&r
);
605 for (i
= 0; i
< len
; i
++)
609 printf("Bad resource name \"%s\" in font lookup\n", name
);
611 } else if (!strcmp(name
, "weight")) {
613 } else if (!strcmp(name
, "style")) {
615 } else if (!strcmp(name
, "family")) {
619 printf("Bad font macro name \"%s\"\n", name
);
623 newstrlen
= strlen(r
);
624 char *naya
= new char[startpos
+ newstrlen
+ len
- i
];
625 memcpy(naya
, v
, startpos
);
626 memcpy(naya
+ startpos
, r
, newstrlen
);
627 memcpy(naya
+ startpos
+ newstrlen
, v
+ i
+ 1, len
- i
);
636 /* We have a final value: */
642 //-----------------------------------------------------------------------------
644 //-----------------------------------------------------------------------------
646 class wxFontNameItem
: public wxObject
{
647 DECLARE_DYNAMIC_CLASS(wxFontNameItem
)
649 wxFontNameItem(const char *name
, int id
, int family
);
652 inline char* GetScreenName(int w
, int s
) {return screen
.GetName(w
, s
);}
653 inline char* GetPostScriptName(int w
, int s
) {return printing
.GetName(w
, s
);}
654 inline char* GetAFMName(int w
, int s
) {return afm
.GetName(w
, s
);}
655 inline char* GetName(void) {return name
;}
656 inline int GetFamily(void) {return family
;}
657 inline int GetId(void) {return id
;}
658 inline bool IsRoman(void) {return isroman
;}
660 void Dump(ostream
& str
);
666 wxSuffixMap screen
, printing
, afm
;
670 IMPLEMENT_ABSTRACT_CLASS(wxFontNameItem
, wxObject
)
672 wxFontNameItem::wxFontNameItem(const char *Name
, int Id
, int Family
)
674 name
= copystring(Name
);
678 screen
. Initialize(name
, "Screen");
679 printing
.Initialize(name
, "PostScript");
680 afm
. Initialize(name
, "Afm");
683 wxFontNameItem::~wxFontNameItem(void)
687 name
= (char *) NULL
;
691 void wxFontNameItem::Dump(ostream
& str
)
693 str
<< "wxFontNameItem(" << name
<< ")";
697 //-----------------------------------------------------------------------------
699 //-----------------------------------------------------------------------------
701 IMPLEMENT_DYNAMIC_CLASS(wxFontNameDirectory
, wxObject
)
703 wxFontNameDirectory::wxFontNameDirectory(void)
705 table
= new wxHashTable(wxKEY_INTEGER
, 20);
709 wxFontNameDirectory::~wxFontNameDirectory()
711 // Cleanup wxFontNameItems allocated
713 wxNode
*node
= table
->Next();
715 wxFontNameItem
*item
= (wxFontNameItem
*)node
->Data();
717 node
= table
->Next();
722 int wxFontNameDirectory::GetNewFontId(void)
724 return (nextFontId
--);
727 void wxFontNameDirectory::Initialize()
729 Initialize(wxDEFAULT
, wxDEFAULT
, "Default");
730 Initialize(wxDECORATIVE
, wxDECORATIVE
, "Decorative");
731 Initialize(wxROMAN
, wxROMAN
, "Roman");
732 Initialize(wxMODERN
, wxMODERN
, "Modern");
733 Initialize(wxTELETYPE
, wxTELETYPE
, "Teletype");
734 Initialize(wxSWISS
, wxSWISS
, "Swiss");
735 Initialize(wxSCRIPT
, wxSCRIPT
, "Script");
738 void wxFontNameDirectory::Initialize(int fontid
, int family
, const char *resname
)
740 char *fam
, resource
[256];
742 sprintf(resource
, "Family%s", resname
);
743 SearchResource((const char *)resource
, (const char **) NULL
, 0, (char **)&fam
);
745 if (!strcmp(fam
, "Default")) family
= wxDEFAULT
;
746 else if (!strcmp(fam
, "Roman")) family
= wxROMAN
;
747 else if (!strcmp(fam
, "Decorative")) family
= wxDECORATIVE
;
748 else if (!strcmp(fam
, "Modern")) family
= wxMODERN
;
749 else if (!strcmp(fam
, "Teletype")) family
= wxTELETYPE
;
750 else if (!strcmp(fam
, "Swiss")) family
= wxSWISS
;
751 else if (!strcmp(fam
, "Script")) family
= wxSCRIPT
;
752 delete[] fam
; // free resource
754 table
->Put(fontid
, new wxFontNameItem(resname
, fontid
, family
));
757 int wxFontNameDirectory::FindOrCreateFontId(const char *name
, int family
)
761 // font exists -> return id
762 if ( (id
= GetFontId(name
)) ) return id
;
764 Initialize(id
=GetNewFontId(), family
, name
);
768 char *wxFontNameDirectory::GetScreenName(int fontid
, int weight
, int style
)
770 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
); // find font
772 return item
->GetScreenName(weight
, style
);
773 // font does not exist
774 return (char *) NULL
;
777 char *wxFontNameDirectory::GetPostScriptName(int fontid
, int weight
, int style
)
779 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
); // find font
781 return item
->GetPostScriptName(weight
, style
);
782 // font does not exist
783 return (char *) NULL
;
786 char *wxFontNameDirectory::GetAFMName(int fontid
, int weight
, int style
)
788 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
); // find font
790 return item
->GetAFMName(weight
, style
);
791 // font does not exist
792 return (char *) NULL
;
795 char *wxFontNameDirectory::GetFontName(int fontid
)
797 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
); // find font
799 return item
->GetName();
800 // font does not exist
801 return (char *) NULL
;
804 int wxFontNameDirectory::GetFontId(const char *name
)
810 while ( (node
= table
->Next()) ) {
811 wxFontNameItem
*item
= (wxFontNameItem
*)node
->Data();
812 if (!strcmp(name
, item
->name
))
815 // font does not exist
819 int wxFontNameDirectory::GetFamily(int fontid
)
821 wxFontNameItem
*item
= (wxFontNameItem
*)table
->Get(fontid
);
825 // font does not exist