1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/font.cpp
3 // Purpose: wxFont class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
26 #pragma message disable nosimpint
27 #include "wx/vms_x_fix.h"
31 #pragma message enable nosimpint
34 #include "wx/string.h"
36 #include "wx/gdicmn.h"
37 #include "wx/utils.h" // for wxGetDisplay()
38 #include "wx/fontutil.h" // for wxNativeFontInfo
39 #include "wx/tokenzr.h"
40 #include "wx/settings.h"
41 #include "wx/motif/private.h"
43 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // For every wxFont, there must be a font for each display and scale requested.
50 // So these objects are stored in wxFontRefData::m_fonts
51 class wxXFont
: public wxObject
57 #if !wxMOTIF_NEW_FONT_HANDLING
58 WXFontStructPtr m_fontStruct
; // XFontStruct
60 #if !wxMOTIF_USE_RENDER_TABLE && !wxMOTIF_NEW_FONT_HANDLING
61 WXFontList m_fontList
; // Motif XmFontList
62 #else // if wxUSE_RENDER_TABLE
63 WXRenderTable m_renderTable
; // Motif XmRenderTable
64 WXRendition m_rendition
; // Motif XmRendition
66 WXDisplay
* m_display
; // XDisplay
67 int m_scale
; // Scale * 100
70 class wxFontRefData
: public wxGDIRefData
75 wxFontRefData(int size
= wxDEFAULT
,
76 int family
= wxDEFAULT
,
77 int style
= wxDEFAULT
,
78 int weight
= wxDEFAULT
,
79 bool underlined
= false,
80 const wxString
& faceName
= wxEmptyString
,
81 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
)
83 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
86 wxFontRefData(const wxFontRefData
& data
)
88 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
89 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
95 // common part of all ctors
101 const wxString
& faceName
,
102 wxFontEncoding encoding
);
111 wxFontEncoding m_encoding
;
113 wxNativeFontInfo m_nativeFontInfo
;
115 // A list of wxXFonts
119 // ============================================================================
121 // ============================================================================
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
129 #if !wxMOTIF_NEW_FONT_HANDLING
130 m_fontStruct
= (WXFontStructPtr
) 0;
132 #if !wxMOTIF_USE_RENDER_TABLE && !wxMOTIF_NEW_FONT_HANDLING
133 m_fontList
= (WXFontList
) 0;
134 #else // if wxMOTIF_USE_RENDER_TABLE
135 m_renderTable
= (WXRenderTable
) 0;
136 m_rendition
= (WXRendition
) 0;
138 m_display
= (WXDisplay
*) 0;
144 #if !wxMOTIF_USE_RENDER_TABLE
146 XmFontListFree ((XmFontList
) m_fontList
);
148 #else // if wxUSE_RENDER_TABLE
150 XmRenderTableFree ((XmRenderTable
) m_renderTable
);
151 m_renderTable
= NULL
;
154 // TODO: why does freeing the font produce a segv???
155 // Note that XFreeFont wasn't called in wxWin 1.68 either.
156 // MBN: probably some interaction with fonts being still
157 // in use in some widgets...
158 // XFontStruct* fontStruct = (XFontStruct*) m_fontStruct;
159 // XFreeFont((Display*) m_display, fontStruct);
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 void wxFontRefData::Init(int pointSize
,
171 const wxString
& faceName
,
172 wxFontEncoding encoding
)
174 if (family
== wxDEFAULT
)
179 m_faceName
= faceName
;
181 if (style
== wxDEFAULT
)
186 if (weight
== wxDEFAULT
)
191 if (pointSize
== wxDEFAULT
)
194 m_pointSize
= pointSize
;
196 m_underlined
= underlined
;
197 m_encoding
= encoding
;
200 wxFontRefData::~wxFontRefData()
202 wxList::compatibility_iterator node
= m_fonts
.GetFirst();
205 wxXFont
* f
= (wxXFont
*) node
->GetData();
207 node
= node
->GetNext();
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
216 wxFont::wxFont(const wxNativeFontInfo
& info
)
218 (void)Create(info
.GetXFontName());
221 bool wxFont::Create(int pointSize
,
226 const wxString
& faceName
,
227 wxFontEncoding encoding
)
230 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
231 underlined
, faceName
, encoding
);
236 bool wxFont::Create(const wxString
& fontname
, wxFontEncoding enc
)
240 *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
244 m_refData
= new wxFontRefData();
246 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(fontname
); // X font name
250 wxStringTokenizer
tn( fontname
, wxT("-") );
252 tn
.GetNextToken(); // skip initial empty token
253 tn
.GetNextToken(); // foundry
256 M_FONTDATA
->m_faceName
= tn
.GetNextToken(); // family
258 tmp
= tn
.GetNextToken().MakeUpper(); // weight
259 if (tmp
== wxT("BOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
260 if (tmp
== wxT("BLACK")) M_FONTDATA
->m_weight
= wxBOLD
;
261 if (tmp
== wxT("EXTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
262 if (tmp
== wxT("DEMIBOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
263 if (tmp
== wxT("ULTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
265 if (tmp
== wxT("LIGHT")) M_FONTDATA
->m_weight
= wxLIGHT
;
266 if (tmp
== wxT("THIN")) M_FONTDATA
->m_weight
= wxLIGHT
;
268 tmp
= tn
.GetNextToken().MakeUpper(); // slant
269 if (tmp
== wxT("I")) M_FONTDATA
->m_style
= wxITALIC
;
270 if (tmp
== wxT("O")) M_FONTDATA
->m_style
= wxITALIC
;
272 tn
.GetNextToken(); // set width
273 tn
.GetNextToken(); // add. style
274 tn
.GetNextToken(); // pixel size
276 tmp
= tn
.GetNextToken(); // pointsize
279 long num
= wxStrtol (tmp
.c_str(), (wxChar
**) NULL
, 10);
280 M_FONTDATA
->m_pointSize
= (int)(num
/ 10);
283 tn
.GetNextToken(); // x-res
284 tn
.GetNextToken(); // y-res
286 tmp
= tn
.GetNextToken().MakeUpper(); // spacing
289 M_FONTDATA
->m_family
= wxMODERN
;
290 else if (M_FONTDATA
->m_faceName
== wxT("TIMES"))
291 M_FONTDATA
->m_family
= wxROMAN
;
292 else if (M_FONTDATA
->m_faceName
== wxT("HELVETICA"))
293 M_FONTDATA
->m_family
= wxSWISS
;
294 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDATYPEWRITER"))
295 M_FONTDATA
->m_family
= wxTELETYPE
;
296 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDA"))
297 M_FONTDATA
->m_family
= wxDECORATIVE
;
298 else if (M_FONTDATA
->m_faceName
== wxT("UTOPIA"))
299 M_FONTDATA
->m_family
= wxSCRIPT
;
301 tn
.GetNextToken(); // avg width
303 // deal with font encoding
304 M_FONTDATA
->m_encoding
= enc
;
305 if ( M_FONTDATA
->m_encoding
== wxFONTENCODING_SYSTEM
)
307 wxString registry
= tn
.GetNextToken().MakeUpper(),
308 encoding
= tn
.GetNextToken().MakeUpper();
310 if ( registry
== _T("ISO8859") )
313 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
315 M_FONTDATA
->m_encoding
=
316 (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
319 else if ( registry
== _T("MICROSOFT") )
322 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
324 M_FONTDATA
->m_encoding
=
325 (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
328 else if ( registry
== _T("KOI8") )
330 M_FONTDATA
->m_encoding
= wxFONTENCODING_KOI8
;
332 //else: unknown encoding - may be give a warning here?
343 // ----------------------------------------------------------------------------
344 // change the font attributes
345 // ----------------------------------------------------------------------------
347 void wxFont::Unshare()
349 // Don't change shared data
352 m_refData
= new wxFontRefData();
356 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
362 void wxFont::SetPointSize(int pointSize
)
366 M_FONTDATA
->m_pointSize
= pointSize
;
367 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
370 void wxFont::SetFamily(int family
)
374 M_FONTDATA
->m_family
= family
;
375 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
378 void wxFont::SetStyle(int style
)
382 M_FONTDATA
->m_style
= style
;
383 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
386 void wxFont::SetWeight(int weight
)
390 M_FONTDATA
->m_weight
= weight
;
391 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
394 void wxFont::SetFaceName(const wxString
& faceName
)
398 M_FONTDATA
->m_faceName
= faceName
;
399 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
402 void wxFont::SetUnderlined(bool underlined
)
406 M_FONTDATA
->m_underlined
= underlined
;
409 void wxFont::SetEncoding(wxFontEncoding encoding
)
413 M_FONTDATA
->m_encoding
= encoding
;
414 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
417 void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
421 M_FONTDATA
->m_nativeFontInfo
= info
;
424 // ----------------------------------------------------------------------------
425 // query font attributes
426 // ----------------------------------------------------------------------------
428 int wxFont::GetPointSize() const
430 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
432 return M_FONTDATA
->m_pointSize
;
435 wxString
wxFont::GetFaceName() const
437 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
439 return M_FONTDATA
->m_faceName
;
442 int wxFont::GetFamily() const
444 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
446 return M_FONTDATA
->m_family
;
449 int wxFont::GetStyle() const
451 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
453 return M_FONTDATA
->m_style
;
456 int wxFont::GetWeight() const
458 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
460 return M_FONTDATA
->m_weight
;
463 bool wxFont::GetUnderlined() const
465 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
467 return M_FONTDATA
->m_underlined
;
470 wxFontEncoding
wxFont::GetEncoding() const
472 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
474 return M_FONTDATA
->m_encoding
;
477 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
479 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
481 if(M_FONTDATA
->m_nativeFontInfo
.GetXFontName().IsEmpty())
484 return &(M_FONTDATA
->m_nativeFontInfo
);
487 // ----------------------------------------------------------------------------
488 // real implementation
489 // ----------------------------------------------------------------------------
491 // Find an existing, or create a new, XFontStruct
492 // based on this wxFont and the given scale. Append the
493 // font to list in the private data for future reference.
494 wxXFont
* wxFont::GetInternalFont(double scale
, WXDisplay
* display
) const
497 return (wxXFont
*)NULL
;
499 long intScale
= long(scale
* 100.0 + 0.5); // key for wxXFont
500 int pointSize
= (M_FONTDATA
->m_pointSize
* 10 * intScale
) / 100;
502 // search existing fonts first
503 wxList::compatibility_iterator node
= M_FONTDATA
->m_fonts
.GetFirst();
506 wxXFont
* f
= (wxXFont
*) node
->GetData();
507 if ((!display
|| (f
->m_display
== display
)) && (f
->m_scale
== intScale
))
509 node
= node
->GetNext();
512 // not found, create a new one
514 XFontStruct
*font
= (XFontStruct
*)
515 wxLoadQueryNearestFont(pointSize
,
516 M_FONTDATA
->m_family
,
518 M_FONTDATA
->m_weight
,
519 M_FONTDATA
->m_underlined
,
521 M_FONTDATA
->m_encoding
,
526 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
528 return (wxXFont
*) NULL
;
531 wxXFont
* f
= new wxXFont
;
532 #if wxMOTIF_NEW_FONT_HANDLING
533 XFreeFont( (Display
*) display
, font
);
535 f
->m_fontStruct
= (WXFontStructPtr
)font
;
537 f
->m_display
= ( display
? display
: wxGetDisplay() );
538 f
->m_scale
= intScale
;
540 #if wxMOTIF_USE_RENDER_TABLE
541 XmRendition rendition
;
542 XmRenderTable renderTable
;
546 #if wxMOTIF_NEW_FONT_HANDLING
547 wxChar
* fontSpec
= wxStrdup( xFontSpec
.c_str() );
548 XtSetArg( args
[count
], XmNfontName
, fontSpec
); ++count
;
549 XtSetArg( args
[count
], XmNfontType
, XmFONT_IS_FONTSET
); ++count
;
551 XtSetArg( args
[count
], XmNfont
, font
); ++count
;
553 XtSetArg( args
[count
], XmNunderlineType
,
554 GetUnderlined() ? XmSINGLE_LINE
: XmNO_LINE
); ++count
;
555 rendition
= XmRenditionCreate( XmGetXmDisplay( (Display
*)f
->m_display
),
558 renderTable
= XmRenderTableAddRenditions( NULL
, &rendition
, 1,
561 f
->m_renderTable
= (WXRenderTable
)renderTable
;
562 f
->m_rendition
= (WXRendition
)rendition
;
563 wxASSERT( f
->m_renderTable
!= NULL
);
564 #else // if !wxMOTIF_USE_RENDER_TABLE
565 f
->m_fontList
= XmFontListCreate ((XFontStruct
*) font
, XmSTRING_DEFAULT_CHARSET
);
566 wxASSERT( f
->m_fontList
!= NULL
);
569 M_FONTDATA
->m_fonts
.Append(f
);
574 #if !wxMOTIF_NEW_FONT_HANDLING
576 WXFontStructPtr
wxFont::GetFontStruct(double scale
, WXDisplay
* display
) const
578 wxXFont
* f
= GetInternalFont(scale
, display
);
580 return (f
? f
->m_fontStruct
: (WXFontStructPtr
) 0);
585 #if !wxMOTIF_USE_RENDER_TABLE
587 WXFontList
wxFont::GetFontList(double scale
, WXDisplay
* display
) const
589 wxXFont
* f
= GetInternalFont(scale
, display
);
591 return (f
? f
->m_fontList
: (WXFontList
) 0);
594 #else // if wxMOTIF_USE_RENDER_TABLE
596 WXRenderTable
wxFont::GetRenderTable(WXDisplay
* display
) const
598 wxXFont
* f
= GetInternalFont(1.0, display
);
600 return (f
? f
->m_renderTable
: (WXRenderTable
) 0);
603 #endif // wxMOTIF_USE_RENDER_TABLE
605 WXFontType
wxFont::GetFontType(WXDisplay
* display
) const
607 #if wxMOTIF_USE_RENDER_TABLE
608 return Ok() ? GetRenderTable(display
) : NULL
;
610 return Ok() ? GetFontList(1.0, display
) : NULL
;
614 WXFontType
wxFont::GetFontTypeC(WXDisplay
* display
) const
616 #if wxMOTIF_USE_RENDER_TABLE
617 return Ok() ? GetRenderTable(display
) : NULL
;
619 return Ok() ? XmFontListCopy( (XmFontList
)GetFontList(1.0, display
) ) : NULL
;
623 /*static*/ WXString
wxFont::GetFontTag()
625 #if wxMOTIF_USE_RENDER_TABLE
626 return (WXString
)XmNrenderTable
;
628 return (WXString
)XmNfontList
;
632 #if wxMOTIF_NEW_FONT_HANDLING
634 WXFontSet
wxFont::GetFontSet(double scale
, WXDisplay
* display
) const
636 wxXFont
* f
= GetInternalFont(scale
, display
);
638 if( !f
) return (WXFontSet
) 0;
643 XtSetArg( args
[count
], XmNfont
, 0 ); ++count
;
644 XmRenditionRetrieve( (XmRendition
) f
->m_rendition
, args
, count
);
646 return (WXFontSet
) args
[0].value
;
649 void wxGetTextExtent(WXDisplay
* display
, const wxFont
& font
, double scale
,
651 int* width
, int* height
, int* ascent
, int* descent
)
653 XRectangle ink
, logical
;
654 WXFontSet fset
= font
.GetFontSet(scale
, display
);
656 XmbTextExtents( (XFontSet
)fset
, str
.c_str(), str
.length(), &ink
, &logical
);
658 if( width
) *width
= logical
.width
;
659 if( height
) *height
= logical
.height
;
660 if( ascent
) *ascent
= -logical
.y
;
661 if( descent
) *descent
= logical
.height
+ logical
.y
;
664 #else // if !wxMOTIF_NEW_FONT_HANDLING
666 void wxGetTextExtent(WXDisplay
* display
, const wxFont
& font
,
667 double scale
, const wxString
& str
,
668 int* width
, int* height
, int* ascent
, int* descent
)
670 WXFontStructPtr pFontStruct
= font
.GetFontStruct(scale
, display
);
672 int direction
, ascent2
, descent2
;
674 int slen
= str
.Len();
676 XTextExtents((XFontStruct
*) pFontStruct
, (char*) str
.c_str(), slen
,
677 &direction
, &ascent2
, &descent2
, &overall
);
680 *width
= (overall
.width
);
682 *height
= (ascent2
+ descent2
);
689 #endif // !wxMOTIF_NEW_FONT_HANDLING