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
)
220 (void)Create(info
.GetXFontName());
227 bool wxFont::Create(int pointSize
,
232 const wxString
& faceName
,
233 wxFontEncoding encoding
)
236 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
237 underlined
, faceName
, encoding
);
242 bool wxFont::Create(const wxString
& fontname
, wxFontEncoding enc
)
246 *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
250 m_refData
= new wxFontRefData();
252 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(fontname
); // X font name
256 wxStringTokenizer
tn( fontname
, wxT("-") );
258 tn
.GetNextToken(); // skip initial empty token
259 tn
.GetNextToken(); // foundry
262 M_FONTDATA
->m_faceName
= tn
.GetNextToken(); // family
264 tmp
= tn
.GetNextToken().MakeUpper(); // weight
265 if (tmp
== wxT("BOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
266 if (tmp
== wxT("BLACK")) M_FONTDATA
->m_weight
= wxBOLD
;
267 if (tmp
== wxT("EXTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
268 if (tmp
== wxT("DEMIBOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
269 if (tmp
== wxT("ULTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
271 if (tmp
== wxT("LIGHT")) M_FONTDATA
->m_weight
= wxLIGHT
;
272 if (tmp
== wxT("THIN")) M_FONTDATA
->m_weight
= wxLIGHT
;
274 tmp
= tn
.GetNextToken().MakeUpper(); // slant
275 if (tmp
== wxT("I")) M_FONTDATA
->m_style
= wxITALIC
;
276 if (tmp
== wxT("O")) M_FONTDATA
->m_style
= wxITALIC
;
278 tn
.GetNextToken(); // set width
279 tn
.GetNextToken(); // add. style
280 tn
.GetNextToken(); // pixel size
282 tmp
= tn
.GetNextToken(); // pointsize
285 long num
= wxStrtol (tmp
.c_str(), (wxChar
**) NULL
, 10);
286 M_FONTDATA
->m_pointSize
= (int)(num
/ 10);
289 tn
.GetNextToken(); // x-res
290 tn
.GetNextToken(); // y-res
292 tmp
= tn
.GetNextToken().MakeUpper(); // spacing
295 M_FONTDATA
->m_family
= wxMODERN
;
296 else if (M_FONTDATA
->m_faceName
== wxT("TIMES"))
297 M_FONTDATA
->m_family
= wxROMAN
;
298 else if (M_FONTDATA
->m_faceName
== wxT("HELVETICA"))
299 M_FONTDATA
->m_family
= wxSWISS
;
300 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDATYPEWRITER"))
301 M_FONTDATA
->m_family
= wxTELETYPE
;
302 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDA"))
303 M_FONTDATA
->m_family
= wxDECORATIVE
;
304 else if (M_FONTDATA
->m_faceName
== wxT("UTOPIA"))
305 M_FONTDATA
->m_family
= wxSCRIPT
;
307 tn
.GetNextToken(); // avg width
309 // deal with font encoding
310 M_FONTDATA
->m_encoding
= enc
;
311 if ( M_FONTDATA
->m_encoding
== wxFONTENCODING_SYSTEM
)
313 wxString registry
= tn
.GetNextToken().MakeUpper(),
314 encoding
= tn
.GetNextToken().MakeUpper();
316 if ( registry
== _T("ISO8859") )
319 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
321 M_FONTDATA
->m_encoding
=
322 (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
325 else if ( registry
== _T("MICROSOFT") )
328 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
330 M_FONTDATA
->m_encoding
=
331 (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
334 else if ( registry
== _T("KOI8") )
336 M_FONTDATA
->m_encoding
= wxFONTENCODING_KOI8
;
338 //else: unknown encoding - may be give a warning here?
349 // ----------------------------------------------------------------------------
350 // change the font attributes
351 // ----------------------------------------------------------------------------
353 void wxFont::Unshare()
355 // Don't change shared data
358 m_refData
= new wxFontRefData();
362 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
368 void wxFont::SetPointSize(int pointSize
)
372 M_FONTDATA
->m_pointSize
= pointSize
;
373 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
376 void wxFont::SetFamily(int family
)
380 M_FONTDATA
->m_family
= family
;
381 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
384 void wxFont::SetStyle(int style
)
388 M_FONTDATA
->m_style
= style
;
389 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
392 void wxFont::SetWeight(int weight
)
396 M_FONTDATA
->m_weight
= weight
;
397 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
400 void wxFont::SetFaceName(const wxString
& faceName
)
404 M_FONTDATA
->m_faceName
= faceName
;
405 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
408 void wxFont::SetUnderlined(bool underlined
)
412 M_FONTDATA
->m_underlined
= underlined
;
415 void wxFont::SetEncoding(wxFontEncoding encoding
)
419 M_FONTDATA
->m_encoding
= encoding
;
420 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
423 void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
427 M_FONTDATA
->m_nativeFontInfo
= info
;
430 // ----------------------------------------------------------------------------
431 // query font attributes
432 // ----------------------------------------------------------------------------
434 int wxFont::GetPointSize() const
436 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
438 return M_FONTDATA
->m_pointSize
;
441 wxString
wxFont::GetFaceName() const
443 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
445 return M_FONTDATA
->m_faceName
;
448 int wxFont::GetFamily() const
450 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
452 return M_FONTDATA
->m_family
;
455 int wxFont::GetStyle() const
457 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
459 return M_FONTDATA
->m_style
;
462 int wxFont::GetWeight() const
464 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
466 return M_FONTDATA
->m_weight
;
469 bool wxFont::GetUnderlined() const
471 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
473 return M_FONTDATA
->m_underlined
;
476 wxFontEncoding
wxFont::GetEncoding() const
478 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
480 return M_FONTDATA
->m_encoding
;
483 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
485 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
487 if(M_FONTDATA
->m_nativeFontInfo
.GetXFontName().IsEmpty())
490 return &(M_FONTDATA
->m_nativeFontInfo
);
493 // ----------------------------------------------------------------------------
494 // real implementation
495 // ----------------------------------------------------------------------------
497 // Find an existing, or create a new, XFontStruct
498 // based on this wxFont and the given scale. Append the
499 // font to list in the private data for future reference.
500 wxXFont
* wxFont::GetInternalFont(double scale
, WXDisplay
* display
) const
503 return (wxXFont
*)NULL
;
505 long intScale
= long(scale
* 100.0 + 0.5); // key for wxXFont
506 int pointSize
= (M_FONTDATA
->m_pointSize
* 10 * intScale
) / 100;
508 // search existing fonts first
509 wxList::compatibility_iterator node
= M_FONTDATA
->m_fonts
.GetFirst();
512 wxXFont
* f
= (wxXFont
*) node
->GetData();
513 if ((!display
|| (f
->m_display
== display
)) && (f
->m_scale
== intScale
))
515 node
= node
->GetNext();
518 // not found, create a new one
520 XFontStruct
*font
= (XFontStruct
*)
521 wxLoadQueryNearestFont(pointSize
,
522 M_FONTDATA
->m_family
,
524 M_FONTDATA
->m_weight
,
525 M_FONTDATA
->m_underlined
,
527 M_FONTDATA
->m_encoding
,
532 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
534 return (wxXFont
*) NULL
;
537 wxXFont
* f
= new wxXFont
;
538 #if wxMOTIF_NEW_FONT_HANDLING
539 XFreeFont( (Display
*) display
, font
);
541 f
->m_fontStruct
= (WXFontStructPtr
)font
;
543 f
->m_display
= ( display
? display
: wxGetDisplay() );
544 f
->m_scale
= intScale
;
546 #if wxMOTIF_USE_RENDER_TABLE
547 XmRendition rendition
;
548 XmRenderTable renderTable
;
552 #if wxMOTIF_NEW_FONT_HANDLING
553 wxChar
* fontSpec
= wxStrdup( xFontSpec
.c_str() );
554 XtSetArg( args
[count
], XmNfontName
, fontSpec
); ++count
;
555 XtSetArg( args
[count
], XmNfontType
, XmFONT_IS_FONTSET
); ++count
;
557 XtSetArg( args
[count
], XmNfont
, font
); ++count
;
559 XtSetArg( args
[count
], XmNunderlineType
,
560 GetUnderlined() ? XmSINGLE_LINE
: XmNO_LINE
); ++count
;
561 rendition
= XmRenditionCreate( XmGetXmDisplay( (Display
*)f
->m_display
),
564 renderTable
= XmRenderTableAddRenditions( NULL
, &rendition
, 1,
567 f
->m_renderTable
= (WXRenderTable
)renderTable
;
568 f
->m_rendition
= (WXRendition
)rendition
;
569 wxASSERT( f
->m_renderTable
!= NULL
);
570 #else // if !wxMOTIF_USE_RENDER_TABLE
571 f
->m_fontList
= XmFontListCreate ((XFontStruct
*) font
, XmSTRING_DEFAULT_CHARSET
);
572 wxASSERT( f
->m_fontList
!= NULL
);
575 M_FONTDATA
->m_fonts
.Append(f
);
580 #if !wxMOTIF_NEW_FONT_HANDLING
582 WXFontStructPtr
wxFont::GetFontStruct(double scale
, WXDisplay
* display
) const
584 wxXFont
* f
= GetInternalFont(scale
, display
);
586 return (f
? f
->m_fontStruct
: (WXFontStructPtr
) 0);
591 #if !wxMOTIF_USE_RENDER_TABLE
593 WXFontList
wxFont::GetFontList(double scale
, WXDisplay
* display
) const
595 wxXFont
* f
= GetInternalFont(scale
, display
);
597 return (f
? f
->m_fontList
: (WXFontList
) 0);
600 #else // if wxMOTIF_USE_RENDER_TABLE
602 WXRenderTable
wxFont::GetRenderTable(WXDisplay
* display
) const
604 wxXFont
* f
= GetInternalFont(1.0, display
);
606 return (f
? f
->m_renderTable
: (WXRenderTable
) 0);
609 #endif // wxMOTIF_USE_RENDER_TABLE
611 WXFontType
wxFont::GetFontType(WXDisplay
* display
) const
613 #if wxMOTIF_USE_RENDER_TABLE
614 return Ok() ? GetRenderTable(display
) : NULL
;
616 return Ok() ? GetFontList(1.0, display
) : NULL
;
620 WXFontType
wxFont::GetFontTypeC(WXDisplay
* display
) const
622 #if wxMOTIF_USE_RENDER_TABLE
623 return Ok() ? GetRenderTable(display
) : NULL
;
625 return Ok() ? XmFontListCopy( (XmFontList
)GetFontList(1.0, display
) ) : NULL
;
629 /*static*/ WXString
wxFont::GetFontTag()
631 #if wxMOTIF_USE_RENDER_TABLE
632 return (WXString
)XmNrenderTable
;
634 return (WXString
)XmNfontList
;
638 #if wxMOTIF_NEW_FONT_HANDLING
640 WXFontSet
wxFont::GetFontSet(double scale
, WXDisplay
* display
) const
642 wxXFont
* f
= GetInternalFont(scale
, display
);
644 if( !f
) return (WXFontSet
) 0;
649 XtSetArg( args
[count
], XmNfont
, 0 ); ++count
;
650 XmRenditionRetrieve( (XmRendition
) f
->m_rendition
, args
, count
);
652 return (WXFontSet
) args
[0].value
;
655 void wxGetTextExtent(WXDisplay
* display
, const wxFont
& font
, double scale
,
657 int* width
, int* height
, int* ascent
, int* descent
)
659 XRectangle ink
, logical
;
660 WXFontSet fset
= font
.GetFontSet(scale
, display
);
662 XmbTextExtents( (XFontSet
)fset
, str
.c_str(), str
.length(), &ink
, &logical
);
664 if( width
) *width
= logical
.width
;
665 if( height
) *height
= logical
.height
;
666 if( ascent
) *ascent
= -logical
.y
;
667 if( descent
) *descent
= logical
.height
+ logical
.y
;
670 #else // if !wxMOTIF_NEW_FONT_HANDLING
672 void wxGetTextExtent(WXDisplay
* display
, const wxFont
& font
,
673 double scale
, const wxString
& str
,
674 int* width
, int* height
, int* ascent
, int* descent
)
676 WXFontStructPtr pFontStruct
= font
.GetFontStruct(scale
, display
);
678 int direction
, ascent2
, descent2
;
680 int slen
= str
.Len();
682 XTextExtents((XFontStruct
*) pFontStruct
, (char*) str
.c_str(), slen
,
683 &direction
, &ascent2
, &descent2
, &overall
);
686 *width
= (overall
.width
);
688 *height
= (ascent2
+ descent2
);
695 #endif // !wxMOTIF_NEW_FONT_HANDLING