]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: font.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ============================================================================ | |
11 | // declarations | |
12 | // ============================================================================ | |
13 | ||
14 | // ---------------------------------------------------------------------------- | |
15 | // headers | |
16 | // ---------------------------------------------------------------------------- | |
17 | ||
18 | #ifdef __GNUG__ | |
19 | #pragma implementation "font.h" | |
20 | #endif | |
21 | ||
22 | #include "wx/font.h" | |
23 | #include "wx/utils.h" | |
24 | #include "wx/log.h" | |
25 | #include "wx/gdicmn.h" | |
26 | #include "wx/tokenzr.h" | |
27 | ||
28 | #include <strings.h> | |
29 | ||
30 | #include "gdk/gdk.h" | |
31 | ||
32 | // ---------------------------------------------------------------------------- | |
33 | // wxFontRefData | |
34 | // ---------------------------------------------------------------------------- | |
35 | ||
36 | class wxFontRefData : public wxObjectRefData | |
37 | { | |
38 | public: | |
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, | |
45 | wxFontEncoding encoding = wxFONTENCODING_DEFAULT); | |
46 | wxFontRefData( const wxFontRefData& data ); | |
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: | |
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; | |
68 | ||
69 | friend wxFont; | |
70 | }; | |
71 | ||
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) | |
87 | { | |
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; | |
112 | } | |
113 | ||
114 | wxFontRefData::wxFontRefData( const wxFontRefData& data ) | |
115 | : m_scaled_xfonts(wxKEY_INTEGER) | |
116 | { | |
117 | Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight, | |
118 | data.m_underlined, data.m_faceName, data.m_encoding); | |
119 | } | |
120 | ||
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); | |
127 | } | |
128 | ||
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 | } | |
139 | } | |
140 | ||
141 | // ---------------------------------------------------------------------------- | |
142 | // wxFont | |
143 | // ---------------------------------------------------------------------------- | |
144 | ||
145 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) | |
146 | ||
147 | void wxFont::Init() | |
148 | { | |
149 | if (wxTheFontList) | |
150 | wxTheFontList->Append( this ); | |
151 | } | |
152 | ||
153 | wxFont::wxFont( GdkFont *WXUNUSED(font), char *xFontName ) | |
154 | { | |
155 | if (!xFontName) | |
156 | return; | |
157 | ||
158 | Init(); | |
159 | ||
160 | m_refData = new wxFontRefData(); | |
161 | ||
162 | wxString tmp; | |
163 | ||
164 | wxString fontname( xFontName ); | |
165 | wxStringTokenizer tn( fontname, wxT("-") ); | |
166 | ||
167 | tn.GetNextToken(); // foundry | |
168 | ||
169 | M_FONTDATA->m_faceName = tn.GetNextToken(); // courier | |
170 | ||
171 | tmp = tn.GetNextToken().MakeUpper(); | |
172 | if (tmp == wxT("BOLD")) M_FONTDATA->m_weight = wxBOLD; | |
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 | ||
181 | tmp = tn.GetNextToken().MakeUpper(); | |
182 | if (tmp == wxT("I")) M_FONTDATA->m_style = wxITALIC; | |
183 | if (tmp == wxT("O")) M_FONTDATA->m_style = wxITALIC; | |
184 | ||
185 | tn.GetNextToken(); // set width | |
186 | tn.GetNextToken(); // ? | |
187 | tn.GetNextToken(); // pixel size | |
188 | ||
189 | tmp = tn.GetNextToken(); // pointsize | |
190 | int num = wxStrtol (tmp.c_str(), (wxChar **) NULL, 10); | |
191 | M_FONTDATA->m_pointSize = num / 10; | |
192 | ||
193 | tn.GetNextToken(); // x-res | |
194 | tn.GetNextToken(); // y-res | |
195 | ||
196 | tmp = tn.GetNextToken().MakeUpper(); | |
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; | |
203 | } | |
204 | ||
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 ) | |
212 | { | |
213 | m_refData = new wxFontRefData(pointSize, family, style, weight, | |
214 | underlined, face, encoding); | |
215 | ||
216 | return TRUE; | |
217 | } | |
218 | ||
219 | void wxFont::Unshare() | |
220 | { | |
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 | } | |
231 | } | |
232 | ||
233 | wxFont::~wxFont() | |
234 | { | |
235 | if (wxTheFontList) | |
236 | wxTheFontList->DeleteObject( this ); | |
237 | } | |
238 | ||
239 | // ---------------------------------------------------------------------------- | |
240 | // accessors | |
241 | // ---------------------------------------------------------------------------- | |
242 | ||
243 | int wxFont::GetPointSize() const | |
244 | { | |
245 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
246 | ||
247 | return M_FONTDATA->m_pointSize; | |
248 | } | |
249 | ||
250 | wxString wxFont::GetFaceName() const | |
251 | { | |
252 | wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") ); | |
253 | ||
254 | return M_FONTDATA->m_faceName; | |
255 | } | |
256 | ||
257 | int wxFont::GetFamily() const | |
258 | { | |
259 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
260 | ||
261 | return M_FONTDATA->m_family; | |
262 | } | |
263 | ||
264 | int wxFont::GetStyle() const | |
265 | { | |
266 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
267 | ||
268 | return M_FONTDATA->m_style; | |
269 | } | |
270 | ||
271 | int wxFont::GetWeight() const | |
272 | { | |
273 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
274 | ||
275 | return M_FONTDATA->m_weight; | |
276 | } | |
277 | ||
278 | bool wxFont::GetUnderlined() const | |
279 | { | |
280 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); | |
281 | ||
282 | return M_FONTDATA->m_underlined; | |
283 | } | |
284 | ||
285 | ||
286 | wxFontEncoding wxFont::GetEncoding() const | |
287 | { | |
288 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); | |
289 | ||
290 | return M_FONTDATA->m_encoding; | |
291 | } | |
292 | ||
293 | // ---------------------------------------------------------------------------- | |
294 | // change font attributes | |
295 | // ---------------------------------------------------------------------------- | |
296 | ||
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 | ||
339 | void wxFont::SetEncoding(wxFontEncoding encoding) | |
340 | { | |
341 | Unshare(); | |
342 | ||
343 | M_FONTDATA->m_encoding = encoding; | |
344 | } | |
345 | ||
346 | // ---------------------------------------------------------------------------- | |
347 | // get internal representation of font | |
348 | // ---------------------------------------------------------------------------- | |
349 | ||
350 | GdkFont *wxFont::GetInternalFont( float scale ) const | |
351 | { | |
352 | if (!Ok()) | |
353 | { | |
354 | wxFAIL_MSG( wxT("invalid font") ); | |
355 | ||
356 | return (GdkFont*) NULL; | |
357 | } | |
358 | ||
359 | long int_scale = long(scale * 100.0 + 0.5); /* key for fontlist */ | |
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 | { | |
370 | #if 0 | |
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 | |
381 | #endif // 0 | |
382 | { | |
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 ); | |
390 | } | |
391 | ||
392 | M_FONTDATA->m_scaled_xfonts.Append( int_scale, (wxObject*)font ); | |
393 | } | |
394 | ||
395 | if (!font) | |
396 | { | |
397 | wxLogError(wxT("could not load any font")); | |
398 | } | |
399 | ||
400 | return font; | |
401 | } | |
402 |