]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: font.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
a81258be | 5 | // Id: $Id$ |
c801d85f | 6 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem |
0c5d3e1c | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
0c5d3e1c VZ |
10 | // ============================================================================ |
11 | // declarations | |
12 | // ============================================================================ | |
13 | ||
14 | // ---------------------------------------------------------------------------- | |
15 | // headers | |
16 | // ---------------------------------------------------------------------------- | |
17 | ||
c801d85f | 18 | #ifdef __GNUG__ |
0c5d3e1c | 19 | #pragma implementation "font.h" |
c801d85f KB |
20 | #endif |
21 | ||
22 | #include "wx/font.h" | |
23 | #include "wx/utils.h" | |
5705323e | 24 | #include "wx/log.h" |
4cb122de | 25 | #include "wx/gdicmn.h" |
8636aed8 | 26 | #include "wx/tokenzr.h" |
0c5d3e1c | 27 | |
c801d85f KB |
28 | #include <strings.h> |
29 | ||
83624f79 RR |
30 | #include "gdk/gdk.h" |
31 | ||
0c5d3e1c VZ |
32 | // ---------------------------------------------------------------------------- |
33 | // wxFontRefData | |
34 | // ---------------------------------------------------------------------------- | |
35 | ||
36 | class wxFontRefData : public wxObjectRefData | |
c801d85f | 37 | { |
8bbe427f | 38 | public: |
0c5d3e1c VZ |
39 | wxFontRefData(int size = wxDEFAULT, |
40 | int family = wxDEFAULT, | |
41 | int style = wxDEFAULT, | |
42 | int weight = wxDEFAULT, | |
43 | bool underlined = FALSE, | |
44 | const wxString& faceName = wxEmptyString, | |
f35c2659 | 45 | wxFontEncoding encoding = wxFONTENCODING_DEFAULT); |
358fc25c | 46 | wxFontRefData( const wxFontRefData& data ); |
0c5d3e1c VZ |
47 | virtual ~wxFontRefData(); |
48 | ||
49 | protected: | |
50 | // common part of all ctors | |
51 | void Init(int pointSize, | |
52 | int family, | |
53 | int style, | |
54 | int weight, | |
55 | bool underlined, | |
56 | const wxString& faceName, | |
57 | wxFontEncoding encoding); | |
58 | ||
59 | private: | |
f35c2659 RR |
60 | wxList m_scaled_xfonts; |
61 | int m_pointSize; | |
62 | int m_family, | |
63 | m_style, | |
64 | m_weight; | |
65 | bool m_underlined; | |
66 | wxString m_faceName; | |
67 | wxFontEncoding m_encoding; | |
8bbe427f | 68 | |
c801d85f KB |
69 | friend wxFont; |
70 | }; | |
71 | ||
0c5d3e1c VZ |
72 | // ============================================================================ |
73 | // implementation | |
74 | // ============================================================================ | |
75 | ||
76 | // ---------------------------------------------------------------------------- | |
77 | // wxFontRefData | |
78 | // ---------------------------------------------------------------------------- | |
79 | ||
80 | void wxFontRefData::Init(int pointSize, | |
81 | int family, | |
82 | int style, | |
83 | int weight, | |
84 | bool underlined, | |
85 | const wxString& faceName, | |
86 | wxFontEncoding encoding) | |
8bbe427f | 87 | { |
0c5d3e1c VZ |
88 | if (family == wxDEFAULT) |
89 | m_family = wxSWISS; | |
90 | else | |
91 | m_family = family; | |
92 | ||
93 | m_faceName = faceName; | |
94 | ||
95 | if (style == wxDEFAULT) | |
96 | m_style = wxNORMAL; | |
97 | else | |
98 | m_style = style; | |
99 | ||
100 | if (weight == wxDEFAULT) | |
101 | m_weight = wxNORMAL; | |
102 | else | |
103 | m_weight = weight; | |
104 | ||
105 | if (pointSize == wxDEFAULT) | |
106 | m_pointSize = 12; | |
107 | else | |
108 | m_pointSize = pointSize; | |
109 | ||
110 | m_underlined = underlined; | |
111 | m_encoding = encoding; | |
8bbe427f VZ |
112 | } |
113 | ||
0c5d3e1c | 114 | wxFontRefData::wxFontRefData( const wxFontRefData& data ) |
f35c2659 | 115 | : m_scaled_xfonts(wxKEY_INTEGER) |
358fc25c | 116 | { |
0c5d3e1c | 117 | Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight, |
f35c2659 RR |
118 | data.m_underlined, data.m_faceName, data.m_encoding); |
119 | } | |
0c5d3e1c | 120 | |
f35c2659 RR |
121 | wxFontRefData::wxFontRefData(int size, int family, int style, |
122 | int weight, bool underlined, const wxString& faceName, wxFontEncoding encoding ) | |
123 | : m_scaled_xfonts(wxKEY_INTEGER) | |
124 | { | |
125 | Init(size, family, style, weight, | |
126 | underlined, faceName, encoding); | |
358fc25c RR |
127 | } |
128 | ||
8bbe427f VZ |
129 | wxFontRefData::~wxFontRefData() |
130 | { | |
131 | wxNode *node = m_scaled_xfonts.First(); | |
132 | while (node) | |
133 | { | |
134 | GdkFont *font = (GdkFont*)node->Data(); | |
135 | wxNode *next = node->Next(); | |
136 | gdk_font_unref( font ); | |
137 | node = next; | |
138 | } | |
0c5d3e1c | 139 | } |
c801d85f | 140 | |
0c5d3e1c VZ |
141 | // ---------------------------------------------------------------------------- |
142 | // wxFont | |
143 | // ---------------------------------------------------------------------------- | |
c801d85f KB |
144 | |
145 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) | |
146 | ||
0c5d3e1c | 147 | void wxFont::Init() |
c801d85f | 148 | { |
0c5d3e1c VZ |
149 | if (wxTheFontList) |
150 | wxTheFontList->Append( this ); | |
ff7b1510 | 151 | } |
c801d85f | 152 | |
5e0201ea | 153 | wxFont::wxFont( GdkFont *WXUNUSED(font), char *xFontName ) |
c801d85f | 154 | { |
0c5d3e1c VZ |
155 | if (!xFontName) |
156 | return; | |
157 | ||
0c5d3e1c | 158 | Init(); |
8bbe427f VZ |
159 | |
160 | m_refData = new wxFontRefData(); | |
161 | ||
8636aed8 | 162 | wxString tmp; |
284b4c88 | 163 | |
8636aed8 | 164 | wxString fontname( xFontName ); |
223d09f6 | 165 | wxStringTokenizer tn( fontname, wxT("-") ); |
284b4c88 | 166 | |
8636aed8 | 167 | tn.GetNextToken(); // foundry |
284b4c88 | 168 | |
8636aed8 RR |
169 | M_FONTDATA->m_faceName = tn.GetNextToken(); // courier |
170 | ||
171 | tmp = tn.GetNextToken().MakeUpper(); | |
223d09f6 | 172 | if (tmp == wxT("BOLD")) M_FONTDATA->m_weight = wxBOLD; |
30760ce7 RR |
173 | if (tmp == wxT("BLACK")) M_FONTDATA->m_weight = wxBOLD; |
174 | if (tmp == wxT("EXTRABOLD")) M_FONTDATA->m_weight = wxBOLD; | |
175 | if (tmp == wxT("DEMIBOLD")) M_FONTDATA->m_weight = wxBOLD; | |
176 | if (tmp == wxT("ULTRABOLD")) M_FONTDATA->m_weight = wxBOLD; | |
177 | ||
178 | if (tmp == wxT("LIGHT")) M_FONTDATA->m_weight = wxLIGHT; | |
179 | if (tmp == wxT("THIN")) M_FONTDATA->m_weight = wxLIGHT; | |
180 | ||
8636aed8 | 181 | tmp = tn.GetNextToken().MakeUpper(); |
223d09f6 KB |
182 | if (tmp == wxT("I")) M_FONTDATA->m_style = wxITALIC; |
183 | if (tmp == wxT("O")) M_FONTDATA->m_style = wxITALIC; | |
284b4c88 | 184 | |
8636aed8 RR |
185 | tn.GetNextToken(); // set width |
186 | tn.GetNextToken(); // ? | |
187 | tn.GetNextToken(); // pixel size | |
284b4c88 | 188 | |
8636aed8 RR |
189 | tmp = tn.GetNextToken(); // pointsize |
190 | int num = wxStrtol (tmp.c_str(), (wxChar **) NULL, 10); | |
191 | M_FONTDATA->m_pointSize = num / 10; | |
284b4c88 | 192 | |
8636aed8 RR |
193 | tn.GetNextToken(); // x-res |
194 | tn.GetNextToken(); // y-res | |
284b4c88 | 195 | |
8636aed8 | 196 | tmp = tn.GetNextToken().MakeUpper(); |
223d09f6 KB |
197 | if (tmp == wxT("M")) M_FONTDATA->m_family = wxMODERN; |
198 | else if (M_FONTDATA->m_faceName == wxT("TIMES")) M_FONTDATA->m_family = wxROMAN; | |
199 | else if (M_FONTDATA->m_faceName == wxT("HELVETICA")) M_FONTDATA->m_family = wxSWISS; | |
200 | else if (M_FONTDATA->m_faceName == wxT("LUCIDATYPEWRITER")) M_FONTDATA->m_family = wxTELETYPE; | |
201 | else if (M_FONTDATA->m_faceName == wxT("LUCIDA")) M_FONTDATA->m_family = wxDECORATIVE; | |
202 | else if (M_FONTDATA->m_faceName == wxT("UTOPIA")) M_FONTDATA->m_family = wxSCRIPT; | |
ff7b1510 | 203 | } |
c801d85f | 204 | |
0c5d3e1c VZ |
205 | bool wxFont::Create( int pointSize, |
206 | int family, | |
207 | int style, | |
208 | int weight, | |
209 | bool underlined, | |
210 | const wxString& face, | |
211 | wxFontEncoding encoding ) | |
8bbe427f | 212 | { |
0c5d3e1c VZ |
213 | m_refData = new wxFontRefData(pointSize, family, style, weight, |
214 | underlined, face, encoding); | |
8bbe427f | 215 | |
0c5d3e1c | 216 | return TRUE; |
ff7b1510 | 217 | } |
c801d85f | 218 | |
0c5d3e1c | 219 | void wxFont::Unshare() |
8bbe427f | 220 | { |
0c5d3e1c VZ |
221 | if (!m_refData) |
222 | { | |
223 | m_refData = new wxFontRefData(); | |
224 | } | |
225 | else | |
226 | { | |
227 | wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData); | |
228 | UnRef(); | |
229 | m_refData = ref; | |
230 | } | |
ff7b1510 | 231 | } |
c801d85f | 232 | |
8bbe427f | 233 | wxFont::~wxFont() |
c801d85f | 234 | { |
0c5d3e1c VZ |
235 | if (wxTheFontList) |
236 | wxTheFontList->DeleteObject( this ); | |
ff7b1510 | 237 | } |
c801d85f | 238 | |
0c5d3e1c VZ |
239 | // ---------------------------------------------------------------------------- |
240 | // accessors | |
241 | // ---------------------------------------------------------------------------- | |
c801d85f | 242 | |
8bbe427f | 243 | int wxFont::GetPointSize() const |
c801d85f | 244 | { |
223d09f6 | 245 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
8bbe427f VZ |
246 | |
247 | return M_FONTDATA->m_pointSize; | |
ff7b1510 | 248 | } |
c801d85f | 249 | |
8bbe427f | 250 | wxString wxFont::GetFaceName() const |
c801d85f | 251 | { |
223d09f6 | 252 | wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") ); |
8bbe427f | 253 | |
36b3b54a | 254 | return M_FONTDATA->m_faceName; |
ff7b1510 | 255 | } |
c801d85f | 256 | |
8bbe427f | 257 | int wxFont::GetFamily() const |
c801d85f | 258 | { |
223d09f6 | 259 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
8bbe427f VZ |
260 | |
261 | return M_FONTDATA->m_family; | |
ff7b1510 | 262 | } |
c801d85f | 263 | |
8bbe427f | 264 | int wxFont::GetStyle() const |
c801d85f | 265 | { |
223d09f6 | 266 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
d84eb083 | 267 | |
8bbe427f | 268 | return M_FONTDATA->m_style; |
ff7b1510 | 269 | } |
c801d85f | 270 | |
8bbe427f | 271 | int wxFont::GetWeight() const |
c801d85f | 272 | { |
223d09f6 | 273 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
8bbe427f VZ |
274 | |
275 | return M_FONTDATA->m_weight; | |
276 | } | |
277 | ||
8bbe427f VZ |
278 | bool wxFont::GetUnderlined() const |
279 | { | |
223d09f6 | 280 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); |
8bbe427f VZ |
281 | |
282 | return M_FONTDATA->m_underlined; | |
ff7b1510 | 283 | } |
c801d85f | 284 | |
0c5d3e1c VZ |
285 | |
286 | wxFontEncoding wxFont::GetEncoding() const | |
358fc25c | 287 | { |
223d09f6 | 288 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); |
0c5d3e1c VZ |
289 | |
290 | return M_FONTDATA->m_encoding; | |
358fc25c RR |
291 | } |
292 | ||
0c5d3e1c VZ |
293 | // ---------------------------------------------------------------------------- |
294 | // change font attributes | |
295 | // ---------------------------------------------------------------------------- | |
296 | ||
358fc25c RR |
297 | void wxFont::SetPointSize(int pointSize) |
298 | { | |
299 | Unshare(); | |
300 | ||
301 | M_FONTDATA->m_pointSize = pointSize; | |
302 | } | |
303 | ||
304 | void wxFont::SetFamily(int family) | |
305 | { | |
306 | Unshare(); | |
307 | ||
308 | M_FONTDATA->m_family = family; | |
309 | } | |
310 | ||
311 | void wxFont::SetStyle(int style) | |
312 | { | |
313 | Unshare(); | |
314 | ||
315 | M_FONTDATA->m_style = style; | |
316 | } | |
317 | ||
318 | void wxFont::SetWeight(int weight) | |
319 | { | |
320 | Unshare(); | |
321 | ||
322 | M_FONTDATA->m_weight = weight; | |
323 | } | |
324 | ||
325 | void wxFont::SetFaceName(const wxString& faceName) | |
326 | { | |
327 | Unshare(); | |
328 | ||
329 | M_FONTDATA->m_faceName = faceName; | |
330 | } | |
331 | ||
332 | void wxFont::SetUnderlined(bool underlined) | |
333 | { | |
334 | Unshare(); | |
335 | ||
336 | M_FONTDATA->m_underlined = underlined; | |
337 | } | |
338 | ||
0c5d3e1c VZ |
339 | void wxFont::SetEncoding(wxFontEncoding encoding) |
340 | { | |
341 | Unshare(); | |
c801d85f | 342 | |
0c5d3e1c VZ |
343 | M_FONTDATA->m_encoding = encoding; |
344 | } | |
345 | ||
346 | // ---------------------------------------------------------------------------- | |
347 | // get internal representation of font | |
348 | // ---------------------------------------------------------------------------- | |
c801d85f | 349 | |
36b3b54a | 350 | GdkFont *wxFont::GetInternalFont( float scale ) const |
c801d85f | 351 | { |
8bbe427f VZ |
352 | if (!Ok()) |
353 | { | |
223d09f6 | 354 | wxFAIL_MSG( wxT("invalid font") ); |
0c5d3e1c | 355 | |
8bbe427f VZ |
356 | return (GdkFont*) NULL; |
357 | } | |
358 | ||
36b3b54a | 359 | long int_scale = long(scale * 100.0 + 0.5); /* key for fontlist */ |
8bbe427f VZ |
360 | int point_scale = (M_FONTDATA->m_pointSize * 10 * int_scale) / 100; |
361 | GdkFont *font = (GdkFont *) NULL; | |
362 | ||
363 | wxNode *node = M_FONTDATA->m_scaled_xfonts.Find(int_scale); | |
364 | if (node) | |
365 | { | |
366 | font = (GdkFont*)node->Data(); | |
367 | } | |
368 | else | |
369 | { | |
0c5d3e1c | 370 | #if 0 |
8bbe427f VZ |
371 | if ((int_scale == 100) && |
372 | (M_FONTDATA->m_family == wxSWISS) && | |
373 | (M_FONTDATA->m_style == wxNORMAL) && | |
374 | (M_FONTDATA->m_pointSize == 12) && | |
375 | (M_FONTDATA->m_weight == wxNORMAL) && | |
376 | (M_FONTDATA->m_underlined == FALSE)) | |
377 | { | |
378 | font = gdk_font_load( "-adobe-helvetica-medium-r-normal--*-120-*-*-*-*-*-*" ); | |
379 | } | |
380 | else | |
0c5d3e1c | 381 | #endif // 0 |
8bbe427f | 382 | { |
0c5d3e1c VZ |
383 | font = wxLoadQueryNearestFont( point_scale, |
384 | M_FONTDATA->m_family, | |
385 | M_FONTDATA->m_style, | |
386 | M_FONTDATA->m_weight, | |
387 | M_FONTDATA->m_underlined, | |
388 | M_FONTDATA->m_faceName, | |
389 | M_FONTDATA->m_encoding ); | |
8bbe427f | 390 | } |
0c5d3e1c | 391 | |
8bbe427f VZ |
392 | M_FONTDATA->m_scaled_xfonts.Append( int_scale, (wxObject*)font ); |
393 | } | |
284b4c88 | 394 | |
8bbe427f | 395 | if (!font) |
36b3b54a | 396 | { |
223d09f6 | 397 | wxLogError(wxT("could not load any font")); |
36b3b54a | 398 | } |
284b4c88 | 399 | |
8bbe427f | 400 | return font; |
ff7b1510 | 401 | } |
c801d85f | 402 |