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"
24 #pragma message disable nosimpint
25 #include "wx/vms_x_fix.h"
29 #pragma message enable nosimpint
35 #include "wx/string.h"
36 #include "wx/utils.h" // for wxGetDisplay()
37 #include "wx/settings.h"
38 #include "wx/gdicmn.h"
41 #include "wx/fontutil.h" // for wxNativeFontInfo
42 #include "wx/tokenzr.h"
43 #include "wx/motif/private.h"
45 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // For every wxFont, there must be a font for each display and scale requested.
52 // So these objects are stored in wxFontRefData::m_fonts
53 class wxXFont
: public wxObject
59 #if !wxMOTIF_NEW_FONT_HANDLING
60 WXFontStructPtr m_fontStruct
; // XFontStruct
62 #if !wxMOTIF_USE_RENDER_TABLE && !wxMOTIF_NEW_FONT_HANDLING
63 WXFontList m_fontList
; // Motif XmFontList
64 #else // if wxUSE_RENDER_TABLE
65 WXRenderTable m_renderTable
; // Motif XmRenderTable
66 WXRendition m_rendition
; // Motif XmRendition
68 WXDisplay
* m_display
; // XDisplay
69 int m_scale
; // Scale * 100
72 class wxFontRefData
: public wxGDIRefData
77 wxFontRefData(int size
= wxDEFAULT
,
78 int family
= wxDEFAULT
,
79 int style
= wxDEFAULT
,
80 int weight
= wxDEFAULT
,
81 bool underlined
= false,
82 const wxString
& faceName
= wxEmptyString
,
83 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
)
85 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
88 wxFontRefData(const wxFontRefData
& data
)
90 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
91 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
97 // common part of all ctors
103 const wxString
& faceName
,
104 wxFontEncoding encoding
);
113 wxFontEncoding m_encoding
;
115 wxNativeFontInfo m_nativeFontInfo
;
117 // A list of wxXFonts
121 // ============================================================================
123 // ============================================================================
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
131 #if !wxMOTIF_NEW_FONT_HANDLING
132 m_fontStruct
= (WXFontStructPtr
) 0;
134 #if !wxMOTIF_USE_RENDER_TABLE && !wxMOTIF_NEW_FONT_HANDLING
135 m_fontList
= (WXFontList
) 0;
136 #else // if wxMOTIF_USE_RENDER_TABLE
137 m_renderTable
= (WXRenderTable
) 0;
138 m_rendition
= (WXRendition
) 0;
140 m_display
= (WXDisplay
*) 0;
146 #if !wxMOTIF_USE_RENDER_TABLE
148 XmFontListFree ((XmFontList
) m_fontList
);
150 #else // if wxUSE_RENDER_TABLE
152 XmRenderTableFree ((XmRenderTable
) m_renderTable
);
153 m_renderTable
= NULL
;
156 // TODO: why does freeing the font produce a segv???
157 // Note that XFreeFont wasn't called in wxWin 1.68 either.
158 // MBN: probably some interaction with fonts being still
159 // in use in some widgets...
160 // XFontStruct* fontStruct = (XFontStruct*) m_fontStruct;
161 // XFreeFont((Display*) m_display, fontStruct);
164 // ----------------------------------------------------------------------------
166 // ----------------------------------------------------------------------------
168 void wxFontRefData::Init(int pointSize
,
173 const wxString
& faceName
,
174 wxFontEncoding encoding
)
176 if (family
== wxDEFAULT
)
181 m_faceName
= faceName
;
183 if (style
== wxDEFAULT
)
188 if (weight
== wxDEFAULT
)
193 if (pointSize
== wxDEFAULT
)
196 m_pointSize
= pointSize
;
198 m_underlined
= underlined
;
199 m_encoding
= encoding
;
202 wxFontRefData::~wxFontRefData()
204 wxList::compatibility_iterator node
= m_fonts
.GetFirst();
207 wxXFont
* f
= (wxXFont
*) node
->GetData();
209 node
= node
->GetNext();
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 wxFont::wxFont(const wxNativeFontInfo
& info
)
220 (void)Create(info
.GetXFontName());
223 bool wxFont::Create(int pointSize
,
228 const wxString
& faceName
,
229 wxFontEncoding encoding
)
232 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
233 underlined
, faceName
, encoding
);
238 bool wxFont::Create(const wxString
& fontname
, wxFontEncoding enc
)
242 *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
246 m_refData
= new wxFontRefData();
248 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(fontname
); // X font name
252 wxStringTokenizer
tn( fontname
, wxT("-") );
254 tn
.GetNextToken(); // skip initial empty token
255 tn
.GetNextToken(); // foundry
258 M_FONTDATA
->m_faceName
= tn
.GetNextToken(); // family
260 tmp
= tn
.GetNextToken().MakeUpper(); // weight
261 if (tmp
== wxT("BOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
262 if (tmp
== wxT("BLACK")) M_FONTDATA
->m_weight
= wxBOLD
;
263 if (tmp
== wxT("EXTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
264 if (tmp
== wxT("DEMIBOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
265 if (tmp
== wxT("ULTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
267 if (tmp
== wxT("LIGHT")) M_FONTDATA
->m_weight
= wxLIGHT
;
268 if (tmp
== wxT("THIN")) M_FONTDATA
->m_weight
= wxLIGHT
;
270 tmp
= tn
.GetNextToken().MakeUpper(); // slant
271 if (tmp
== wxT("I")) M_FONTDATA
->m_style
= wxITALIC
;
272 if (tmp
== wxT("O")) M_FONTDATA
->m_style
= wxITALIC
;
274 tn
.GetNextToken(); // set width
275 tn
.GetNextToken(); // add. style
276 tn
.GetNextToken(); // pixel size
278 tmp
= tn
.GetNextToken(); // pointsize
281 long num
= wxStrtol (tmp
.c_str(), (wxChar
**) NULL
, 10);
282 M_FONTDATA
->m_pointSize
= (int)(num
/ 10);
285 tn
.GetNextToken(); // x-res
286 tn
.GetNextToken(); // y-res
288 tmp
= tn
.GetNextToken().MakeUpper(); // spacing
291 M_FONTDATA
->m_family
= wxMODERN
;
292 else if (M_FONTDATA
->m_faceName
== wxT("TIMES"))
293 M_FONTDATA
->m_family
= wxROMAN
;
294 else if (M_FONTDATA
->m_faceName
== wxT("HELVETICA"))
295 M_FONTDATA
->m_family
= wxSWISS
;
296 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDATYPEWRITER"))
297 M_FONTDATA
->m_family
= wxTELETYPE
;
298 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDA"))
299 M_FONTDATA
->m_family
= wxDECORATIVE
;
300 else if (M_FONTDATA
->m_faceName
== wxT("UTOPIA"))
301 M_FONTDATA
->m_family
= wxSCRIPT
;
303 tn
.GetNextToken(); // avg width
305 // deal with font encoding
306 M_FONTDATA
->m_encoding
= enc
;
307 if ( M_FONTDATA
->m_encoding
== wxFONTENCODING_SYSTEM
)
309 wxString registry
= tn
.GetNextToken().MakeUpper(),
310 encoding
= tn
.GetNextToken().MakeUpper();
312 if ( registry
== _T("ISO8859") )
315 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
317 M_FONTDATA
->m_encoding
=
318 (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
321 else if ( registry
== _T("MICROSOFT") )
324 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
326 M_FONTDATA
->m_encoding
=
327 (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
330 else if ( registry
== _T("KOI8") )
332 M_FONTDATA
->m_encoding
= wxFONTENCODING_KOI8
;
334 //else: unknown encoding - may be give a warning here?
345 // ----------------------------------------------------------------------------
346 // change the font attributes
347 // ----------------------------------------------------------------------------
349 void wxFont::Unshare()
351 // Don't change shared data
354 m_refData
= new wxFontRefData();
358 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
364 void wxFont::SetPointSize(int pointSize
)
368 M_FONTDATA
->m_pointSize
= pointSize
;
369 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
372 void wxFont::SetFamily(int family
)
376 M_FONTDATA
->m_family
= family
;
377 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
380 void wxFont::SetStyle(int style
)
384 M_FONTDATA
->m_style
= style
;
385 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
388 void wxFont::SetWeight(int weight
)
392 M_FONTDATA
->m_weight
= weight
;
393 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
396 bool wxFont::SetFaceName(const wxString
& faceName
)
400 M_FONTDATA
->m_faceName
= faceName
;
401 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
403 return wxFontBase::SetFaceName(faceName
);
406 void wxFont::SetUnderlined(bool underlined
)
410 M_FONTDATA
->m_underlined
= underlined
;
413 void wxFont::SetEncoding(wxFontEncoding encoding
)
417 M_FONTDATA
->m_encoding
= encoding
;
418 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
421 void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
425 M_FONTDATA
->m_nativeFontInfo
= info
;
428 // ----------------------------------------------------------------------------
429 // query font attributes
430 // ----------------------------------------------------------------------------
432 int wxFont::GetPointSize() const
434 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
436 return M_FONTDATA
->m_pointSize
;
439 wxString
wxFont::GetFaceName() const
441 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid font") );
443 return M_FONTDATA
->m_faceName
;
446 int wxFont::GetFamily() const
448 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
450 return M_FONTDATA
->m_family
;
453 int wxFont::GetStyle() const
455 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
457 return M_FONTDATA
->m_style
;
460 int wxFont::GetWeight() const
462 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
464 return M_FONTDATA
->m_weight
;
467 bool wxFont::GetUnderlined() const
469 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
471 return M_FONTDATA
->m_underlined
;
474 wxFontEncoding
wxFont::GetEncoding() const
476 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
478 return M_FONTDATA
->m_encoding
;
481 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
483 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
485 if(M_FONTDATA
->m_nativeFontInfo
.GetXFontName().empty())
488 return &(M_FONTDATA
->m_nativeFontInfo
);
491 // ----------------------------------------------------------------------------
492 // real implementation
493 // ----------------------------------------------------------------------------
495 // Find an existing, or create a new, XFontStruct
496 // based on this wxFont and the given scale. Append the
497 // font to list in the private data for future reference.
498 wxXFont
* wxFont::GetInternalFont(double scale
, WXDisplay
* display
) const
501 return (wxXFont
*)NULL
;
503 long intScale
= long(scale
* 100.0 + 0.5); // key for wxXFont
504 int pointSize
= (M_FONTDATA
->m_pointSize
* 10 * intScale
) / 100;
506 // search existing fonts first
507 wxList::compatibility_iterator node
= M_FONTDATA
->m_fonts
.GetFirst();
510 wxXFont
* f
= (wxXFont
*) node
->GetData();
511 if ((!display
|| (f
->m_display
== display
)) && (f
->m_scale
== intScale
))
513 node
= node
->GetNext();
516 // not found, create a new one
518 XFontStruct
*font
= (XFontStruct
*)
519 wxLoadQueryNearestFont(pointSize
,
520 M_FONTDATA
->m_family
,
522 M_FONTDATA
->m_weight
,
523 M_FONTDATA
->m_underlined
,
525 M_FONTDATA
->m_encoding
,
530 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
532 return (wxXFont
*) NULL
;
535 wxXFont
* f
= new wxXFont
;
536 #if wxMOTIF_NEW_FONT_HANDLING
537 XFreeFont( (Display
*) display
, font
);
539 f
->m_fontStruct
= (WXFontStructPtr
)font
;
541 f
->m_display
= ( display
? display
: wxGetDisplay() );
542 f
->m_scale
= intScale
;
544 #if wxMOTIF_USE_RENDER_TABLE
545 XmRendition rendition
;
546 XmRenderTable renderTable
;
550 #if wxMOTIF_NEW_FONT_HANDLING
551 wxChar
* fontSpec
= wxStrdup( xFontSpec
.c_str() );
552 XtSetArg( args
[count
], XmNfontName
, fontSpec
); ++count
;
553 XtSetArg( args
[count
], XmNfontType
, XmFONT_IS_FONTSET
); ++count
;
555 XtSetArg( args
[count
], XmNfont
, font
); ++count
;
557 XtSetArg( args
[count
], XmNunderlineType
,
558 GetUnderlined() ? XmSINGLE_LINE
: XmNO_LINE
); ++count
;
559 rendition
= XmRenditionCreate( XmGetXmDisplay( (Display
*)f
->m_display
),
562 renderTable
= XmRenderTableAddRenditions( NULL
, &rendition
, 1,
565 f
->m_renderTable
= (WXRenderTable
)renderTable
;
566 f
->m_rendition
= (WXRendition
)rendition
;
567 wxASSERT( f
->m_renderTable
!= NULL
);
568 #else // if !wxMOTIF_USE_RENDER_TABLE
569 f
->m_fontList
= XmFontListCreate ((XFontStruct
*) font
, XmSTRING_DEFAULT_CHARSET
);
570 wxASSERT( f
->m_fontList
!= NULL
);
573 M_FONTDATA
->m_fonts
.Append(f
);
578 #if !wxMOTIF_NEW_FONT_HANDLING
580 WXFontStructPtr
wxFont::GetFontStruct(double scale
, WXDisplay
* display
) const
582 wxXFont
* f
= GetInternalFont(scale
, display
);
584 return (f
? f
->m_fontStruct
: (WXFontStructPtr
) 0);
589 #if !wxMOTIF_USE_RENDER_TABLE
591 WXFontList
wxFont::GetFontList(double scale
, WXDisplay
* display
) const
593 wxXFont
* f
= GetInternalFont(scale
, display
);
595 return (f
? f
->m_fontList
: (WXFontList
) 0);
598 #else // if wxMOTIF_USE_RENDER_TABLE
600 WXRenderTable
wxFont::GetRenderTable(WXDisplay
* display
) const
602 wxXFont
* f
= GetInternalFont(1.0, display
);
604 return (f
? f
->m_renderTable
: (WXRenderTable
) 0);
607 #endif // wxMOTIF_USE_RENDER_TABLE
609 WXFontType
wxFont::GetFontType(WXDisplay
* display
) const
611 #if wxMOTIF_USE_RENDER_TABLE
612 return Ok() ? GetRenderTable(display
) : NULL
;
614 return Ok() ? GetFontList(1.0, display
) : NULL
;
618 WXFontType
wxFont::GetFontTypeC(WXDisplay
* display
) const
620 #if wxMOTIF_USE_RENDER_TABLE
621 return Ok() ? GetRenderTable(display
) : NULL
;
623 return Ok() ? XmFontListCopy( (XmFontList
)GetFontList(1.0, display
) ) : NULL
;
627 /*static*/ WXString
wxFont::GetFontTag()
629 #if wxMOTIF_USE_RENDER_TABLE
630 return (WXString
)XmNrenderTable
;
632 return (WXString
)XmNfontList
;
636 #if wxMOTIF_NEW_FONT_HANDLING
638 WXFontSet
wxFont::GetFontSet(double scale
, WXDisplay
* display
) const
640 wxXFont
* f
= GetInternalFont(scale
, display
);
642 if( !f
) return (WXFontSet
) 0;
647 XtSetArg( args
[count
], XmNfont
, 0 ); ++count
;
648 XmRenditionRetrieve( (XmRendition
) f
->m_rendition
, args
, count
);
650 return (WXFontSet
) args
[0].value
;
653 void wxGetTextExtent(WXDisplay
* display
, const wxFont
& font
, double scale
,
655 int* width
, int* height
, int* ascent
, int* descent
)
657 XRectangle ink
, logical
;
658 WXFontSet fset
= font
.GetFontSet(scale
, display
);
660 XmbTextExtents( (XFontSet
)fset
, str
.c_str(), str
.length(), &ink
, &logical
);
662 if( width
) *width
= logical
.width
;
663 if( height
) *height
= logical
.height
;
664 if( ascent
) *ascent
= -logical
.y
;
665 if( descent
) *descent
= logical
.height
+ logical
.y
;
668 #else // if !wxMOTIF_NEW_FONT_HANDLING
670 void wxGetTextExtent(WXDisplay
* display
, const wxFont
& font
,
671 double scale
, const wxString
& str
,
672 int* width
, int* height
, int* ascent
, int* descent
)
674 WXFontStructPtr pFontStruct
= font
.GetFontStruct(scale
, display
);
676 int direction
, ascent2
, descent2
;
678 int slen
= str
.length();
680 XTextExtents((XFontStruct
*) pFontStruct
, (char*) str
.c_str(), slen
,
681 &direction
, &ascent2
, &descent2
, &overall
);
684 *width
= (overall
.width
);
686 *height
= (ascent2
+ descent2
);
693 #endif // !wxMOTIF_NEW_FONT_HANDLING