]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/font.cpp
ae4ccd07ffe8c6beae3fe89eb18bfd3e09d75260
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFont class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "font.h"
17 #include "wx/string.h"
19 #include "wx/gdicmn.h"
24 #if !USE_SHARED_LIBRARIES
25 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
28 wxFontRefData::wxFontRefData(): m_fontsByScale(wxKEY_INTEGER
)
39 wxFontRefData::wxFontRefData(const wxFontRefData
& data
): m_fontsByScale(wxKEY_INTEGER
)
41 m_style
= data
.m_style
;
42 m_pointSize
= data
.m_pointSize
;
43 m_family
= data
.m_family
;
44 m_style
= data
.m_style
;
45 m_weight
= data
.m_weight
;
46 m_underlined
= data
.m_underlined
;
47 m_faceName
= data
.m_faceName
;
49 // Don't have to copy actual fonts, because they'll be created
53 wxFontRefData::~wxFontRefData()
55 wxNode
* node
= m_fontsByScale
.First();
58 XFontStruct
* fontStruct
= (XFontStruct
*) node
->Data();
59 // TODO: why does freeing the font produce a segv???
60 // Commenting it out will result in memory leaks, and
61 // maybe X resource problems, who knows...
62 // XFreeFont((Display*) wxGetDisplay, fontStruct);
65 m_fontsByScale
.Clear();
71 wxTheFontList
->Append(this);
74 wxFont::wxFont(int pointSize
, int family
, int style
, int weight
, bool underlined
, const wxString
& faceName
)
76 Create(pointSize
, family
, style
, weight
, underlined
, faceName
);
79 wxTheFontList
->Append(this);
82 bool wxFont::Create(int pointSize
, int family
, int style
, int weight
, bool underlined
, const wxString
& faceName
)
85 m_refData
= new wxFontRefData
;
87 M_FONTDATA
->m_family
= family
;
88 M_FONTDATA
->m_style
= style
;
89 M_FONTDATA
->m_weight
= weight
;
90 M_FONTDATA
->m_pointSize
= pointSize
;
91 M_FONTDATA
->m_underlined
= underlined
;
92 M_FONTDATA
->m_faceName
= faceName
;
102 wxTheFontList
->DeleteObject(this);
105 bool wxFont::RealizeResource()
107 // TODO: create the font (if there is a native font object)
111 void wxFont::Unshare()
113 // Don't change shared data
116 m_refData
= new wxFontRefData();
120 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
126 void wxFont::SetPointSize(int pointSize
)
130 M_FONTDATA
->m_pointSize
= pointSize
;
135 void wxFont::SetFamily(int family
)
139 M_FONTDATA
->m_family
= family
;
144 void wxFont::SetStyle(int style
)
148 M_FONTDATA
->m_style
= style
;
153 void wxFont::SetWeight(int weight
)
157 M_FONTDATA
->m_weight
= weight
;
162 void wxFont::SetFaceName(const wxString
& faceName
)
166 M_FONTDATA
->m_faceName
= faceName
;
171 void wxFont::SetUnderlined(bool underlined
)
175 M_FONTDATA
->m_underlined
= underlined
;
180 wxString
wxFont::GetFamilyString() const
186 fam
= "wxDECORATIVE";
210 /* New font system */
211 wxString
wxFont::GetFaceName() const
215 str
= M_FONTDATA
->m_faceName
;
219 wxString
wxFont::GetStyleString() const
237 wxString
wxFont::GetWeightString() const
255 // Find an existing, or create a new, XFontStruct
256 // based on this wxFont and the given scale. Append the
257 // font to list in the private data for future reference.
258 WXFontStructPtr
wxFont::FindOrCreateFontStruct(double scale
)
263 long intScale
= long(scale
* 100.0 + 0.5); // key for fontlist
264 int pointSize
= (M_FONTDATA
->m_pointSize
* 10 * intScale
) / 100;
266 wxNode
* node
= M_FONTDATA
->m_fontsByScale
.Find(intScale
);
269 return (WXFontStructPtr
) node
->Data();
272 WXFontStructPtr font
= LoadQueryFont(pointSize
, M_FONTDATA
->m_family
,
273 M_FONTDATA
->m_style
, M_FONTDATA
->m_weight
, M_FONTDATA
->m_underlined
);
277 // search up and down by stepsize 10
278 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
279 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
282 // Search for smaller size (approx.)
283 for (i
=pointSize
-10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10)
284 font
= LoadQueryFont(i
, M_FONTDATA
->m_family
, M_FONTDATA
->m_style
, M_FONTDATA
->m_weight
, M_FONTDATA
->m_underlined
);
285 // Search for larger size (approx.)
286 for (i
=pointSize
+10; !font
&& i
<= max_size
; i
+= 10)
287 font
= LoadQueryFont(i
, M_FONTDATA
->m_family
, M_FONTDATA
->m_style
, M_FONTDATA
->m_weight
, M_FONTDATA
->m_underlined
);
288 // Try default family
289 if (!font
&& M_FONTDATA
->m_family
!= wxDEFAULT
)
290 font
= LoadQueryFont(pointSize
, wxDEFAULT
, M_FONTDATA
->m_style
,
291 M_FONTDATA
->m_weight
, M_FONTDATA
->m_underlined
);
294 font
= LoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
295 M_FONTDATA
->m_underlined
);
296 wxASSERT_MSG( (font
!= (XFontStruct
*) NULL
), "Could not allocate even a default font -- something is wrong." );
300 M_FONTDATA
->m_fontsByScale
.Append(intScale
, (wxObject
*) font
);
301 return (WXFontStructPtr
) font
;
306 WXFontStructPtr
wxFont::LoadQueryFont(int pointSize
, int family
, int style
,
307 int weight
, bool underlined
)
314 case wxDECORATIVE
: xfamily
= "lucida";
316 case wxROMAN
: xfamily
= "times";
318 case wxMODERN
: xfamily
= "courier";
320 case wxSWISS
: xfamily
= "lucida";
323 default: xfamily
= "*";
327 case wxITALIC
: xstyle
= "i";
329 case wxSLANT
: xstyle
= "o";
331 case wxNORMAL
: xstyle
= "r";
333 default: xstyle
= "*";
338 case wxBOLD
: xweight
= "bold";
341 case wxNORMAL
: xweight
= "medium";
343 default: xweight
= "*";
347 sprintf(wxBuffer
, "-*-%s-%s-%s-normal-*-*-%d-*-*-*-*-*-*",
348 xfamily
, xweight
, xstyle
, pointSize
);
350 Display
*dpy
= (Display
*) wxGetDisplay();
351 XFontStruct
* font
= XLoadQueryFont(dpy
, wxBuffer
);
353 return (WXFontStructPtr
) font
;