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 // ----------------------------------------------------------------------------
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
61 WXFontList m_fontList
; // Motif XmFontList
62 #else // if wxMOTIF_USE_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 wxFontFamily family
= wxFONTFAMILY_DEFAULT
,
77 wxFontStyle style
= wxFONTSTYLE_NORMAL
,
78 wxFontWeight weight
= wxFONTWEIGHT_NORMAL
,
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
);
92 virtual ~wxFontRefData();
95 // common part of all ctors
101 const wxString
& faceName
,
102 wxFontEncoding encoding
);
106 wxFontFamily m_family
;
108 wxFontWeight m_weight
;
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
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 wxMOTIF_USE_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
)
175 m_family
= wxFONTFAMILY_SWISS
;
179 m_faceName
= faceName
;
181 if (style
== wxDEFAULT
)
182 m_style
= wxFONTSTYLE_NORMAL
;
186 if (weight
== wxDEFAULT
)
187 m_weight
= wxFONTWEIGHT_NORMAL
;
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 #define M_FONTDATA ((wxFontRefData*)m_refData)
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
= wxFONTWEIGHT_BOLD
;
262 if (tmp
== wxT("BLACK")) M_FONTDATA
->m_weight
= wxFONTWEIGHT_BOLD
;
263 if (tmp
== wxT("EXTRABOLD")) M_FONTDATA
->m_weight
= wxFONTWEIGHT_BOLD
;
264 if (tmp
== wxT("DEMIBOLD")) M_FONTDATA
->m_weight
= wxFONTWEIGHT_BOLD
;
265 if (tmp
== wxT("ULTRABOLD")) M_FONTDATA
->m_weight
= wxFONTWEIGHT_BOLD
;
267 if (tmp
== wxT("LIGHT")) M_FONTDATA
->m_weight
= wxFONTWEIGHT_LIGHT
;
268 if (tmp
== wxT("THIN")) M_FONTDATA
->m_weight
= wxFONTWEIGHT_LIGHT
;
270 tmp
= tn
.GetNextToken().MakeUpper(); // slant
271 if (tmp
== wxT("I")) M_FONTDATA
->m_style
= wxFONTSTYLE_ITALIC
;
272 if (tmp
== wxT("O")) M_FONTDATA
->m_style
= wxFONTSTYLE_ITALIC
;
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
.mb_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
= wxFONTFAMILY_MODERN
;
292 else if (M_FONTDATA
->m_faceName
== wxT("TIMES"))
293 M_FONTDATA
->m_family
= wxFONTFAMILY_ROMAN
;
294 else if (M_FONTDATA
->m_faceName
== wxT("HELVETICA"))
295 M_FONTDATA
->m_family
= wxFONTFAMILY_SWISS
;
296 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDATYPEWRITER"))
297 M_FONTDATA
->m_family
= wxFONTFAMILY_TELETYPE
;
298 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDA"))
299 M_FONTDATA
->m_family
= wxFONTFAMILY_DECORATIVE
;
300 else if (M_FONTDATA
->m_faceName
== wxT("UTOPIA"))
301 M_FONTDATA
->m_family
= wxFONTFAMILY_SCRIPT
;
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
== wxT("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
== wxT("MICROSOFT") )
324 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
326 M_FONTDATA
->m_encoding
=
327 (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
330 else if ( registry
== wxT("KOI8") )
332 M_FONTDATA
->m_encoding
= wxFONTENCODING_KOI8
;
334 //else: unknown encoding - may be give a warning here?
345 wxGDIRefData
*wxFont::CreateGDIRefData() const
347 return new wxFontRefData
;
350 wxGDIRefData
*wxFont::CloneGDIRefData(const wxGDIRefData
*data
) const
352 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));
355 // ----------------------------------------------------------------------------
356 // change the font attributes
357 // ----------------------------------------------------------------------------
359 void wxFont::Unshare()
361 // Don't change shared data
364 m_refData
= new wxFontRefData();
368 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
374 void wxFont::SetPointSize(int pointSize
)
378 M_FONTDATA
->m_pointSize
= pointSize
;
379 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
382 void wxFont::SetFamily(wxFontFamily family
)
386 M_FONTDATA
->m_family
= family
;
387 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
390 void wxFont::SetStyle(wxFontStyle style
)
394 M_FONTDATA
->m_style
= style
;
395 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
398 void wxFont::SetWeight(wxFontWeight weight
)
402 M_FONTDATA
->m_weight
= weight
;
403 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
406 bool wxFont::SetFaceName(const wxString
& faceName
)
410 M_FONTDATA
->m_faceName
= faceName
;
411 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
413 return wxFontBase::SetFaceName(faceName
);
416 void wxFont::SetUnderlined(bool underlined
)
420 M_FONTDATA
->m_underlined
= underlined
;
423 void wxFont::SetEncoding(wxFontEncoding encoding
)
427 M_FONTDATA
->m_encoding
= encoding
;
428 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
431 void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
435 M_FONTDATA
->m_nativeFontInfo
= info
;
438 // ----------------------------------------------------------------------------
439 // query font attributes
440 // ----------------------------------------------------------------------------
442 int wxFont::GetPointSize() const
444 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
446 return M_FONTDATA
->m_pointSize
;
449 wxString
wxFont::GetFaceName() const
451 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid font") );
453 return M_FONTDATA
->m_faceName
;
456 wxFontFamily
wxFont::DoGetFamily() const
458 return M_FONTDATA
->m_family
;
461 wxFontStyle
wxFont::GetStyle() const
463 wxCHECK_MSG( Ok(), wxFONTSTYLE_MAX
, wxT("invalid font") );
465 return M_FONTDATA
->m_style
;
468 wxFontWeight
wxFont::GetWeight() const
470 wxCHECK_MSG( Ok(), wxFONTWEIGHT_MAX
, wxT("invalid font") );
472 return M_FONTDATA
->m_weight
;
475 bool wxFont::GetUnderlined() const
477 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
479 return M_FONTDATA
->m_underlined
;
482 wxFontEncoding
wxFont::GetEncoding() const
484 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
486 return M_FONTDATA
->m_encoding
;
489 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
491 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
493 if(M_FONTDATA
->m_nativeFontInfo
.GetXFontName().empty())
496 return &(M_FONTDATA
->m_nativeFontInfo
);
499 // ----------------------------------------------------------------------------
500 // real implementation
501 // ----------------------------------------------------------------------------
503 // Find an existing, or create a new, XFontStruct
504 // based on this wxFont and the given scale. Append the
505 // font to list in the private data for future reference.
506 wxXFont
* wxFont::GetInternalFont(double scale
, WXDisplay
* display
) const
511 long intScale
= long(scale
* 100.0 + 0.5); // key for wxXFont
512 int pointSize
= (M_FONTDATA
->m_pointSize
* 10 * intScale
) / 100;
514 // search existing fonts first
515 wxList::compatibility_iterator node
= M_FONTDATA
->m_fonts
.GetFirst();
518 wxXFont
* f
= (wxXFont
*) node
->GetData();
519 if ((!display
|| (f
->m_display
== display
)) && (f
->m_scale
== intScale
))
521 node
= node
->GetNext();
524 // not found, create a new one
526 XFontStruct
*font
= (XFontStruct
*)
527 wxLoadQueryNearestFont(pointSize
,
528 M_FONTDATA
->m_family
,
530 M_FONTDATA
->m_weight
,
531 M_FONTDATA
->m_underlined
,
533 M_FONTDATA
->m_encoding
,
538 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
543 wxXFont
* f
= new wxXFont
;
544 #if wxMOTIF_NEW_FONT_HANDLING
545 XFreeFont( (Display
*) display
, font
);
547 f
->m_fontStruct
= (WXFontStructPtr
)font
;
549 f
->m_display
= ( display
? display
: wxGetDisplay() );
550 f
->m_scale
= intScale
;
552 #if wxMOTIF_USE_RENDER_TABLE
553 XmRendition rendition
;
554 XmRenderTable renderTable
;
558 #if wxMOTIF_NEW_FONT_HANDLING
559 char* fontSpec
= wxStrdup(xFontSpec
.mb_str());
560 XtSetArg( args
[count
], XmNfontName
, fontSpec
); ++count
;
561 XtSetArg( args
[count
], XmNfontType
, XmFONT_IS_FONTSET
); ++count
;
563 XtSetArg( args
[count
], XmNfont
, font
); ++count
;
565 XtSetArg( args
[count
], XmNunderlineType
,
566 GetUnderlined() ? XmSINGLE_LINE
: XmNO_LINE
); ++count
;
567 rendition
= XmRenditionCreate( XmGetXmDisplay( (Display
*)f
->m_display
),
570 renderTable
= XmRenderTableAddRenditions( NULL
, &rendition
, 1,
573 f
->m_renderTable
= (WXRenderTable
)renderTable
;
574 f
->m_rendition
= (WXRendition
)rendition
;
575 wxASSERT( f
->m_renderTable
!= NULL
);
576 #else // if !wxMOTIF_USE_RENDER_TABLE
577 f
->m_fontList
= XmFontListCreate ((XFontStruct
*) font
, XmSTRING_DEFAULT_CHARSET
);
578 wxASSERT( f
->m_fontList
!= NULL
);
581 M_FONTDATA
->m_fonts
.Append(f
);
586 #if !wxMOTIF_NEW_FONT_HANDLING
588 WXFontStructPtr
wxFont::GetFontStruct(double scale
, WXDisplay
* display
) const
590 wxXFont
* f
= GetInternalFont(scale
, display
);
592 return (f
? f
->m_fontStruct
: (WXFontStructPtr
) 0);
597 #if !wxMOTIF_USE_RENDER_TABLE
599 WXFontList
wxFont::GetFontList(double scale
, WXDisplay
* display
) const
601 wxXFont
* f
= GetInternalFont(scale
, display
);
603 return (f
? f
->m_fontList
: (WXFontList
) 0);
606 #else // if wxMOTIF_USE_RENDER_TABLE
608 WXRenderTable
wxFont::GetRenderTable(WXDisplay
* display
) const
610 wxXFont
* f
= GetInternalFont(1.0, display
);
612 return (f
? f
->m_renderTable
: (WXRenderTable
) 0);
615 #endif // wxMOTIF_USE_RENDER_TABLE
617 WXFontType
wxFont::GetFontType(WXDisplay
* display
) const
619 #if wxMOTIF_USE_RENDER_TABLE
620 return Ok() ? GetRenderTable(display
) : NULL
;
622 return Ok() ? GetFontList(1.0, display
) : NULL
;
626 WXFontType
wxFont::GetFontTypeC(WXDisplay
* display
) const
628 #if wxMOTIF_USE_RENDER_TABLE
629 return Ok() ? GetRenderTable(display
) : NULL
;
631 return Ok() ? XmFontListCopy( (XmFontList
)GetFontList(1.0, display
) ) : NULL
;
635 /*static*/ WXString
wxFont::GetFontTag()
637 #if wxMOTIF_USE_RENDER_TABLE
638 return (WXString
)XmNrenderTable
;
640 return (WXString
)XmNfontList
;
644 #if wxMOTIF_USE_RENDER_TABLE
646 WXFontSet
wxFont::GetFontSet(double scale
, WXDisplay
* display
) const
648 wxXFont
* f
= GetInternalFont(scale
, display
);
650 if( !f
) return (WXFontSet
) 0;
655 XtSetArg( args
[count
], XmNfont
, 0 ); ++count
;
656 XmRenditionRetrieve( (XmRendition
) f
->m_rendition
, args
, count
);
658 return (WXFontSet
) args
[0].value
;
661 void wxGetTextExtent(WXDisplay
* display
, const wxFont
& font
, double scale
,
663 int* width
, int* height
, int* ascent
, int* descent
)
665 XRectangle ink
, logical
;
666 WXFontSet fset
= font
.GetFontSet(scale
, display
);
668 XmbTextExtents( (XFontSet
)fset
, str
.mb_str(), str
.length(), &ink
, &logical
);
670 if( width
) *width
= logical
.width
;
671 if( height
) *height
= logical
.height
;
672 if( ascent
) *ascent
= -logical
.y
;
673 if( descent
) *descent
= logical
.height
+ logical
.y
;
676 #else // if !wxMOTIF_USE_RENDER_TABLE
678 void wxGetTextExtent(WXDisplay
* display
, const wxFont
& font
,
679 double scale
, const wxString
& str
,
680 int* width
, int* height
, int* ascent
, int* descent
)
682 WXFontStructPtr pFontStruct
= font
.GetFontStruct(scale
, display
);
684 int direction
, ascent2
, descent2
;
686 int slen
= str
.length();
688 XTextExtents((XFontStruct
*) pFontStruct
,
689 const_cast<char*>((const char *)str
.mb_str()), slen
,
690 &direction
, &ascent2
, &descent2
, &overall
);
693 *width
= (overall
.width
);
695 *height
= (ascent2
+ descent2
);
702 #endif // !wxMOTIF_USE_RENDER_TABLE