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 #if wxCHECK_MOTIF_VERSION( 2, 0 ) && !wxCHECK_LESSTIF()
46 #define wxUSE_RENDER_TABLE 1
48 #define wxUSE_RENDER_TABLE 0
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // For every wxFont, there must be a font for each display and scale requested.
56 // So these objects are stored in wxFontRefData::m_fonts
57 class wxXFont
: public wxObject
63 #if !wxMOTIF_NEW_FONT_HANDLING
64 WXFontStructPtr m_fontStruct
; // XFontStruct
66 #if !wxUSE_RENDER_TABLE && !wxMOTIF_NEW_FONT_HANDLING
67 WXFontList m_fontList
; // Motif XmFontList
68 #else // if wxUSE_RENDER_TABLE
69 WXRenderTable m_renderTable
; // Motif XmRenderTable
70 WXRendition m_rendition
; // Motif XmRendition
72 WXDisplay
* m_display
; // XDisplay
73 int m_scale
; // Scale * 100
76 class wxFontRefData
: public wxGDIRefData
81 wxFontRefData(int size
= wxDEFAULT
,
82 int family
= wxDEFAULT
,
83 int style
= wxDEFAULT
,
84 int weight
= wxDEFAULT
,
85 bool underlined
= false,
86 const wxString
& faceName
= wxEmptyString
,
87 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
)
89 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
92 wxFontRefData(const wxFontRefData
& data
)
94 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
95 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
101 // common part of all ctors
107 const wxString
& faceName
,
108 wxFontEncoding encoding
);
117 wxFontEncoding m_encoding
;
119 wxNativeFontInfo m_nativeFontInfo
;
121 // A list of wxXFonts
125 // ============================================================================
127 // ============================================================================
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
135 #if !wxMOTIF_NEW_FONT_HANDLING
136 m_fontStruct
= (WXFontStructPtr
) 0;
138 #if !wxUSE_RENDER_TABLE && !wxMOTIF_NEW_FONT_HANDLING
139 m_fontList
= (WXFontList
) 0;
140 #else // if wxUSE_RENDER_TABLE
141 m_renderTable
= (WXRenderTable
) 0;
142 m_rendition
= (WXRendition
) 0;
144 m_display
= (WXDisplay
*) 0;
150 #if !wxUSE_RENDER_TABLE
152 XmFontListFree ((XmFontList
) m_fontList
);
154 #else // if wxUSE_RENDER_TABLE
156 XmRenderTableFree ((XmRenderTable
) m_renderTable
);
157 m_renderTable
= NULL
;
160 // TODO: why does freeing the font produce a segv???
161 // Note that XFreeFont wasn't called in wxWin 1.68 either.
162 // MBN: probably some interaction with fonts being still
163 // in use in some widgets...
164 // XFontStruct* fontStruct = (XFontStruct*) m_fontStruct;
165 // XFreeFont((Display*) m_display, fontStruct);
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 void wxFontRefData::Init(int pointSize
,
177 const wxString
& faceName
,
178 wxFontEncoding encoding
)
180 if (family
== wxDEFAULT
)
185 m_faceName
= faceName
;
187 if (style
== wxDEFAULT
)
192 if (weight
== wxDEFAULT
)
197 if (pointSize
== wxDEFAULT
)
200 m_pointSize
= pointSize
;
202 m_underlined
= underlined
;
203 m_encoding
= encoding
;
206 wxFontRefData::~wxFontRefData()
208 wxList::compatibility_iterator node
= m_fonts
.GetFirst();
211 wxXFont
* f
= (wxXFont
*) node
->GetData();
213 node
= node
->GetNext();
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
222 wxFont::wxFont(const wxNativeFontInfo
& info
)
226 (void)Create(info
.GetXFontName());
233 bool wxFont::Create(int pointSize
,
238 const wxString
& faceName
,
239 wxFontEncoding encoding
)
242 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
243 underlined
, faceName
, encoding
);
250 bool wxFont::Create(const wxString
& fontname
, wxFontEncoding enc
)
254 *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
258 m_refData
= new wxFontRefData();
260 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(fontname
); // X font name
264 wxStringTokenizer
tn( fontname
, wxT("-") );
266 tn
.GetNextToken(); // skip initial empty token
267 tn
.GetNextToken(); // foundry
270 M_FONTDATA
->m_faceName
= tn
.GetNextToken(); // family
272 tmp
= tn
.GetNextToken().MakeUpper(); // weight
273 if (tmp
== wxT("BOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
274 if (tmp
== wxT("BLACK")) M_FONTDATA
->m_weight
= wxBOLD
;
275 if (tmp
== wxT("EXTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
276 if (tmp
== wxT("DEMIBOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
277 if (tmp
== wxT("ULTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
279 if (tmp
== wxT("LIGHT")) M_FONTDATA
->m_weight
= wxLIGHT
;
280 if (tmp
== wxT("THIN")) M_FONTDATA
->m_weight
= wxLIGHT
;
282 tmp
= tn
.GetNextToken().MakeUpper(); // slant
283 if (tmp
== wxT("I")) M_FONTDATA
->m_style
= wxITALIC
;
284 if (tmp
== wxT("O")) M_FONTDATA
->m_style
= wxITALIC
;
286 tn
.GetNextToken(); // set width
287 tn
.GetNextToken(); // add. style
288 tn
.GetNextToken(); // pixel size
290 tmp
= tn
.GetNextToken(); // pointsize
293 long num
= wxStrtol (tmp
.c_str(), (wxChar
**) NULL
, 10);
294 M_FONTDATA
->m_pointSize
= (int)(num
/ 10);
297 tn
.GetNextToken(); // x-res
298 tn
.GetNextToken(); // y-res
300 tmp
= tn
.GetNextToken().MakeUpper(); // spacing
303 M_FONTDATA
->m_family
= wxMODERN
;
304 else if (M_FONTDATA
->m_faceName
== wxT("TIMES"))
305 M_FONTDATA
->m_family
= wxROMAN
;
306 else if (M_FONTDATA
->m_faceName
== wxT("HELVETICA"))
307 M_FONTDATA
->m_family
= wxSWISS
;
308 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDATYPEWRITER"))
309 M_FONTDATA
->m_family
= wxTELETYPE
;
310 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDA"))
311 M_FONTDATA
->m_family
= wxDECORATIVE
;
312 else if (M_FONTDATA
->m_faceName
== wxT("UTOPIA"))
313 M_FONTDATA
->m_family
= wxSCRIPT
;
315 tn
.GetNextToken(); // avg width
317 // deal with font encoding
318 M_FONTDATA
->m_encoding
= enc
;
319 if ( M_FONTDATA
->m_encoding
== wxFONTENCODING_SYSTEM
)
321 wxString registry
= tn
.GetNextToken().MakeUpper(),
322 encoding
= tn
.GetNextToken().MakeUpper();
324 if ( registry
== _T("ISO8859") )
327 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
329 M_FONTDATA
->m_encoding
=
330 (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
333 else if ( registry
== _T("MICROSOFT") )
336 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
338 M_FONTDATA
->m_encoding
=
339 (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
342 else if ( registry
== _T("KOI8") )
344 M_FONTDATA
->m_encoding
= wxFONTENCODING_KOI8
;
346 //else: unknown encoding - may be give a warning here?
357 // ----------------------------------------------------------------------------
358 // change the font attributes
359 // ----------------------------------------------------------------------------
361 void wxFont::Unshare()
363 // Don't change shared data
366 m_refData
= new wxFontRefData();
370 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
376 void wxFont::SetPointSize(int pointSize
)
380 M_FONTDATA
->m_pointSize
= pointSize
;
381 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
386 void wxFont::SetFamily(int family
)
390 M_FONTDATA
->m_family
= family
;
391 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
396 void wxFont::SetStyle(int style
)
400 M_FONTDATA
->m_style
= style
;
401 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
406 void wxFont::SetWeight(int weight
)
410 M_FONTDATA
->m_weight
= weight
;
411 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
416 void wxFont::SetFaceName(const wxString
& faceName
)
420 M_FONTDATA
->m_faceName
= faceName
;
421 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
426 void wxFont::SetUnderlined(bool underlined
)
430 M_FONTDATA
->m_underlined
= underlined
;
435 void wxFont::SetEncoding(wxFontEncoding encoding
)
439 M_FONTDATA
->m_encoding
= encoding
;
440 M_FONTDATA
->m_nativeFontInfo
.GetXFontName().Clear(); // invalid now
445 void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
449 M_FONTDATA
->m_nativeFontInfo
= info
;
452 // ----------------------------------------------------------------------------
453 // query font attributes
454 // ----------------------------------------------------------------------------
456 int wxFont::GetPointSize() const
458 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
460 return M_FONTDATA
->m_pointSize
;
463 wxString
wxFont::GetFaceName() const
465 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
467 return M_FONTDATA
->m_faceName
;
470 int wxFont::GetFamily() const
472 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
474 return M_FONTDATA
->m_family
;
477 int wxFont::GetStyle() const
479 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
481 return M_FONTDATA
->m_style
;
484 int wxFont::GetWeight() const
486 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
488 return M_FONTDATA
->m_weight
;
491 bool wxFont::GetUnderlined() const
493 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
495 return M_FONTDATA
->m_underlined
;
498 wxFontEncoding
wxFont::GetEncoding() const
500 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
502 return M_FONTDATA
->m_encoding
;
505 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
507 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
509 if(M_FONTDATA
->m_nativeFontInfo
.GetXFontName().IsEmpty())
512 return &(M_FONTDATA
->m_nativeFontInfo
);
515 // ----------------------------------------------------------------------------
516 // real implementation
517 // ----------------------------------------------------------------------------
519 // Find an existing, or create a new, XFontStruct
520 // based on this wxFont and the given scale. Append the
521 // font to list in the private data for future reference.
522 wxXFont
* wxFont::GetInternalFont(double scale
, WXDisplay
* display
) const
525 return (wxXFont
*)NULL
;
527 long intScale
= long(scale
* 100.0 + 0.5); // key for wxXFont
528 int pointSize
= (M_FONTDATA
->m_pointSize
* 10 * intScale
) / 100;
530 // search existing fonts first
531 wxList::compatibility_iterator node
= M_FONTDATA
->m_fonts
.GetFirst();
534 wxXFont
* f
= (wxXFont
*) node
->GetData();
535 if ((!display
|| (f
->m_display
== display
)) && (f
->m_scale
== intScale
))
537 node
= node
->GetNext();
540 // not found, create a new one
542 XFontStruct
*font
= (XFontStruct
*)
543 wxLoadQueryNearestFont(pointSize
,
544 M_FONTDATA
->m_family
,
546 M_FONTDATA
->m_weight
,
547 M_FONTDATA
->m_underlined
,
549 M_FONTDATA
->m_encoding
,
554 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
556 return (wxXFont
*) NULL
;
559 wxXFont
* f
= new wxXFont
;
560 #if wxMOTIF_NEW_FONT_HANDLING
561 XFreeFont( (Display
*) display
, font
);
563 f
->m_fontStruct
= (WXFontStructPtr
)font
;
565 f
->m_display
= ( display
? display
: wxGetDisplay() );
566 f
->m_scale
= intScale
;
568 #if wxUSE_RENDER_TABLE
569 XmRendition rendition
;
570 XmRenderTable renderTable
;
574 #if wxMOTIF_NEW_FONT_HANDLING
575 wxChar
* fontSpec
= wxStrdup( xFontSpec
.c_str() );
576 XtSetArg( args
[count
], XmNfontName
, fontSpec
); ++count
;
577 XtSetArg( args
[count
], XmNfontType
, XmFONT_IS_FONTSET
); ++count
;
579 XtSetArg( args
[count
], XmNfont
, font
); ++count
;
581 XtSetArg( args
[count
], XmNunderlineType
,
582 GetUnderlined() ? XmSINGLE_LINE
: XmNO_LINE
); ++count
;
583 rendition
= XmRenditionCreate( XmGetXmDisplay( (Display
*)f
->m_display
),
586 renderTable
= XmRenderTableAddRenditions( NULL
, &rendition
, 1,
589 f
->m_renderTable
= (WXRenderTable
)renderTable
;
590 f
->m_rendition
= (WXRendition
)rendition
;
591 wxASSERT( f
->m_renderTable
!= NULL
);
592 #else // if !wxUSE_RENDER_TABLE
593 f
->m_fontList
= XmFontListCreate ((XFontStruct
*) font
, XmSTRING_DEFAULT_CHARSET
);
594 wxASSERT( f
->m_fontList
!= NULL
);
597 M_FONTDATA
->m_fonts
.Append(f
);
602 #if !wxMOTIF_NEW_FONT_HANDLING
604 WXFontStructPtr
wxFont::GetFontStruct(double scale
, WXDisplay
* display
) const
606 wxXFont
* f
= GetInternalFont(scale
, display
);
608 return (f
? f
->m_fontStruct
: (WXFontStructPtr
) 0);
611 WXFontList
wxFont::GetFontList(double scale
, WXDisplay
* display
) const
613 #if !wxUSE_RENDER_TABLE
614 wxXFont
* f
= GetInternalFont(scale
, display
);
616 return (f
? f
->m_fontList
: (WXFontList
) 0);
622 #endif // !wxMOTIF_NEW_FONT_HANDLING
624 // declared in the header, can't use wxUSE_RENDER_TABLE
625 #if wxCHECK_MOTIF_VERSION( 2, 0 )
627 WXRenderTable
wxFont::GetRenderTable(WXDisplay
* display
) const
629 #if wxUSE_RENDER_TABLE
630 wxXFont
* f
= GetInternalFont(1.0, display
);
632 return (f
? f
->m_renderTable
: (WXFontList
) 0);
638 #endif // wxCHECK_MOTIF_VERSION( 2, 0 )
640 WXFontType
wxFont::GetFontType(WXDisplay
* display
) const
642 #if wxUSE_RENDER_TABLE
643 return Ok() ? GetRenderTable(display
) : NULL
;
645 return Ok() ? GetFontList(1.0, display
) : NULL
;
649 WXFontType
wxFont::GetFontTypeC(WXDisplay
* display
) const
651 #if wxUSE_RENDER_TABLE
652 return Ok() ? GetRenderTable(display
) : NULL
;
654 return Ok() ? XmFontListCopy( (XmFontList
)GetFontList(1.0, display
) ) : NULL
;
658 /*static*/ WXString
wxFont::GetFontTag()
660 #if wxUSE_RENDER_TABLE
661 return (WXString
)XmNrenderTable
;
663 return (WXString
)XmNfontList
;
667 #if wxMOTIF_NEW_FONT_HANDLING
669 WXFontSet
wxFont::GetFontSet(double scale
, WXDisplay
* display
) const
671 wxXFont
* f
= GetInternalFont(scale
, display
);
673 if( !f
) return (WXFontSet
) 0;
678 XtSetArg( args
[count
], XmNfont
, 0 ); ++count
;
679 XmRenditionRetrieve( (XmRendition
) f
->m_rendition
, args
, count
);
681 return (WXFontSet
) args
[0].value
;
684 void wxGetTextExtent(WXDisplay
* display
, const wxFont
& font
, double scale
,
686 int* width
, int* height
, int* ascent
, int* descent
)
688 XRectangle ink
, logical
;
689 WXFontSet fset
= font
.GetFontSet(scale
, display
);
691 XmbTextExtents( (XFontSet
)fset
, str
.c_str(), str
.length(), &ink
, &logical
);
693 if( width
) *width
= logical
.width
;
694 if( height
) *height
= logical
.height
;
695 if( ascent
) *ascent
= -logical
.y
;
696 if( descent
) *descent
= logical
.height
+ logical
.y
;
699 #else // if !wxMOTIF_NEW_FONT_HANDLING
701 void wxGetTextExtent(WXDisplay
* display
, const wxFont
& font
,
702 double scale
, const wxString
& str
,
703 int* width
, int* height
, int* ascent
, int* descent
)
705 WXFontStructPtr pFontStruct
= font
.GetFontStruct(scale
, display
);
707 int direction
, ascent2
, descent2
;
709 int slen
= str
.Len();
711 XTextExtents((XFontStruct
*) pFontStruct
, (char*) str
.c_str(), slen
,
712 &direction
, &ascent2
, &descent2
, &overall
);
715 *width
= (overall
.width
);
717 *height
= (ascent2
+ descent2
);
724 #endif // !wxMOTIF_NEW_FONT_HANDLING