]>
Commit | Line | Data |
---|---|---|
a24aff65 | 1 | ///////////////////////////////////////////////////////////////////////////// |
8898456d | 2 | // Name: src/cocoa/font.cpp |
a24aff65 DE |
3 | // Purpose: wxFont class |
4 | // Author: AUTHOR | |
5 | // Modified by: | |
6 | // Created: ??/??/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) AUTHOR | |
8898456d | 9 | // Licence: wxWindows licence |
a24aff65 DE |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
51146826 DE |
12 | /*! @file font.mm |
13 | @disucssion | |
14 | Cocoa has three classes which interact to form the font system: | |
15 | ||
16 | NSFont: Represents a specific font (e.g. Times-Bold) at a specific size | |
17 | with specific attributes. That is, it's basically like this class. | |
18 | Notably, it doesn't hold an underlined flag, so this class does. | |
19 | Available on all OS X versions. | |
20 | ||
21 | NSFontManager: Fairly broad controller class which ties together the | |
22 | model (NSFont) with the view (NSFontPanel). We are ignoring NSFontPanel | |
23 | in this discussion. NSFontManager is actually a little broader than just | |
24 | a controller though. It's also the traditional way of mutating fonts | |
25 | and asking for a font with a certain family and certain attributes. | |
26 | For example, you can use NSFont's factor methods to create a font named | |
27 | @"Times-Roman" then use NSFontManager to imbue attributes like italic and | |
28 | bold. You might do this if, for instance, you already have times at the | |
29 | Roman weight but want to make it bold. | |
30 | ||
31 | Alternatively, you can use NSFontManager to ask for a font in the @"Times" | |
32 | family with the bold attribute. | |
33 | ||
34 | NSFontManager is available on all OS X versions. | |
35 | ||
36 | NSFontDescriptor: Added in OS X 10.3. Prior to this there was no specific | |
37 | class to represent all of the attributes of a font. Instead, a regular | |
38 | NSDictionary was used with a set of well-defined keys. Unfortunately, | |
39 | there was no method accepting the attributes dictionary, only a method | |
40 | to retrieve it from an NSFont. That meant that in order to create | |
41 | a new font by imbueing certain attributes like Bold one would need | |
42 | to use the specific method in NSFontManager to do so. | |
43 | ||
44 | The NSFontDescriptor class, on the other hand, has factory methods which | |
45 | can create a new font descriptor with an attributes dictionary as well | |
46 | as mutate (by copy) an existing font descriptor using attributes from | |
47 | an attributes dictionary. | |
48 | ||
49 | In theory, most of what can be done using NSFontDescriptor can just as | |
50 | well be done without it. NSFontDescriptor is basically just a shell | |
51 | around an NSMutableDictionary with well-defined keys. | |
52 | ||
53 | ||
54 | Getting back to the broad overview, font matching is one of the weaker points | |
55 | in Cocoa's API and NSFontDescriptor is the easier to use solution. | |
56 | ||
57 | That said, it's not impossible to implement font matching without it. For instance, | |
58 | if you have a family name and want to list available variants (e.g. Bold, italic, | |
59 | underlined) then you can ask NSFontManager for availableMembersOfFontFamily:. | |
60 | ||
61 | The problem is that you can either match on family name or on font attributes | |
62 | but not on both. To match both you have to do one then the other. | |
63 | NSFontDescriptor allows you to get a list of fonts matching both a family name | |
64 | and a particular set of attributes. Furthermore, the attributes instead of | |
65 | being flags as in NSFontManager are instead well-defined keys in a dictionary. | |
66 | ||
67 | The only way to get that behavior without NSFontManager is to pare down the | |
68 | list as much as possible using the classic NSFontManager methods and then | |
69 | to instantiate each font in the list and match on each font's afmDictionary. | |
70 | ||
71 | A reasonable guess is that that's probably more or less exactly what | |
72 | NSFontDescriptor does internally. | |
73 | */ | |
8898456d WS |
74 | #include "wx/wxprec.h" |
75 | ||
48a1108e WS |
76 | #include "wx/font.h" |
77 | ||
8898456d WS |
78 | #ifndef WX_PRECOMP |
79 | #include "wx/string.h" | |
dd05139a | 80 | #include "wx/gdicmn.h" |
8898456d WS |
81 | #endif |
82 | ||
151b7b73 | 83 | #include "wx/fontutil.h" |
9c6e197f | 84 | #include "wx/encinfo.h" |
a24aff65 | 85 | |
51146826 DE |
86 | #include "wx/cocoa/string.h" |
87 | #include "wx/cocoa/private/fontfactory.h" | |
88 | #include "wx/cocoa/autorelease.h" | |
89 | ||
90 | #include <AppKit/NSFont.h> | |
91 | #include <AppKit/NSFontManager.h> | |
92 | ||
93 | // Helper methods for NSFont/wxNativeFontInfo | |
94 | static NSFont* GetNSFontForNativeFontInfo(const wxNativeFontInfo &info); | |
95 | static void UpdateNativeFontInfoWithNSFont(wxNativeFontInfo &info, NSFont *cocoaNSFont); | |
96 | static wxNativeFontInfo MakeNativeFontInfoForNSFont(NSFont *cocoaNSFont, bool underlined = false); | |
97 | static wxNativeFontInfo MakeNativeFontInfo(int size, int family, int style, int weight, bool underlined, const wxString& faceName, wxFontEncoding encoding); | |
98 | ||
99 | /*! @discussion | |
100 | Due to 2.8 ABI compatibility concerns we probably don't want to change wxNativeFontInfo | |
101 | although this may be unfounded because this class is supposed to be internal as is | |
102 | wxNativeFontInfo so anyone who subclassed it or created one without going through | |
103 | wxFont should expect what they get (i.e. horrible breakage) | |
104 | There's a concern that wxFontRefData was in the public header when 2.8 shipped so | |
105 | it's possible that someone did subclass it to get better font behavior. | |
106 | ||
107 | For right now, the plan is to write it strictly ABI compatible with 2.8 and eventually | |
108 | to enhance it in trunk to accurately represent font attributes as Cocoa sees them. | |
109 | ||
110 | I'd like to let at least one 2.8 release go out the door and get feedback as to whether | |
111 | this is going to be a problem or not. If so, we'll keep it strictly ABI compatible. | |
112 | If not, we'll update it. | |
113 | */ | |
99d21c0e DE |
114 | class WXDLLEXPORT wxFontRefData: public wxGDIRefData |
115 | { | |
116 | friend class WXDLLIMPEXP_FWD_CORE wxFont; | |
117 | public: | |
118 | wxFontRefData() | |
51146826 DE |
119 | : m_cocoaNSFont(nil) |
120 | , m_info(MakeNativeFontInfo(10, wxDEFAULT, wxNORMAL, wxNORMAL, FALSE, | |
121 | wxT("Geneva"), wxFONTENCODING_DEFAULT)) | |
99d21c0e | 122 | { |
51146826 | 123 | CreateNSFontAndUpdateInfo(); |
99d21c0e DE |
124 | } |
125 | ||
126 | wxFontRefData(const wxFontRefData& data) | |
151b7b73 | 127 | : wxGDIRefData() |
51146826 | 128 | , m_cocoaNSFont([data.m_cocoaNSFont retain]) |
151b7b73 | 129 | , m_info(data.m_info) |
99d21c0e | 130 | { |
99d21c0e DE |
131 | } |
132 | ||
51146826 DE |
133 | wxFontRefData(NSFont *cocoaNSFont, bool underlined) |
134 | : wxGDIRefData() | |
135 | , m_cocoaNSFont([cocoaNSFont retain]) | |
136 | , m_info(MakeNativeFontInfoForNSFont(m_cocoaNSFont, underlined)) | |
137 | { | |
138 | } | |
139 | ||
05725592 DE |
140 | wxFontRefData(const wxNativeFontInfo& info) |
141 | : wxGDIRefData() | |
51146826 | 142 | , m_cocoaNSFont(nil) |
05725592 | 143 | , m_info(info) |
51146826 DE |
144 | { |
145 | CreateNSFontAndUpdateInfo(); | |
146 | } | |
05725592 | 147 | |
99d21c0e DE |
148 | wxFontRefData(int size, |
149 | int family, | |
150 | int style, | |
151 | int weight, | |
152 | bool underlined, | |
153 | const wxString& faceName, | |
154 | wxFontEncoding encoding) | |
51146826 DE |
155 | : m_cocoaNSFont(nil) |
156 | , m_info(MakeNativeFontInfo(size, family, style, weight, underlined, faceName, encoding)) | |
99d21c0e | 157 | { |
51146826 | 158 | CreateNSFontAndUpdateInfo(); |
99d21c0e DE |
159 | } |
160 | ||
161 | virtual ~wxFontRefData(); | |
162 | protected: | |
51146826 DE |
163 | /*! @abstract common part of some ctors |
164 | @discussion | |
165 | This is a leftover of sorts from the old stub code. | |
166 | FIXME: Remove from trunk | |
167 | */ | |
99d21c0e DE |
168 | void Init(int size, |
169 | int family, | |
170 | int style, | |
171 | int weight, | |
172 | bool underlined, | |
173 | const wxString& faceName, | |
174 | wxFontEncoding encoding); | |
175 | ||
51146826 DE |
176 | /*! @discussion |
177 | Uses the native font info to create an NSFont and then updates that info with | |
178 | the attributes of the font. This is necessary because, for example, a font | |
179 | can be created with an empty faceName in which case some concrete typeface must | |
180 | be chosen. | |
181 | We choose to handle this by first initializing the wxNativeFontInfo with the | |
182 | properties as given by the user and then creating the NSFont and updating | |
183 | the wxNativeFontInfo to match the NSFont. | |
184 | */ | |
185 | void CreateNSFontAndUpdateInfo() | |
186 | { | |
187 | wxAutoNSAutoreleasePool pool; | |
188 | [m_cocoaNSFont release]; | |
189 | m_cocoaNSFont = [GetNSFontForNativeFontInfo(m_info) retain]; | |
190 | UpdateNativeFontInfoWithNSFont(m_info, m_cocoaNSFont); | |
191 | } | |
192 | ||
99d21c0e | 193 | // font characterstics |
51146826 | 194 | NSFont *m_cocoaNSFont; |
151b7b73 DE |
195 | wxNativeFontInfo m_info; |
196 | ||
99d21c0e DE |
197 | public: |
198 | }; | |
151b7b73 | 199 | |
51146826 DE |
200 | NSString *GetFamilyName(wxFontFamily family) |
201 | { | |
202 | switch(family) | |
203 | { | |
204 | case wxFONTFAMILY_DEFAULT: | |
205 | default: | |
206 | return @"Times"; | |
207 | case wxFONTFAMILY_DECORATIVE: | |
208 | case wxFONTFAMILY_ROMAN: | |
209 | case wxFONTFAMILY_SCRIPT: | |
210 | return @"Times"; | |
211 | case wxFONTFAMILY_SWISS: | |
212 | return @"Lucida Grande"; | |
213 | case wxFONTFAMILY_MODERN: | |
214 | case wxFONTFAMILY_TELETYPE: | |
215 | return @"Monaco"; | |
216 | }; | |
217 | } | |
218 | // Returns an NSFont given the native font info | |
219 | // NOTE: It is not considered alloc'd (i.e. this is a get method not an alloc/new method) | |
220 | static NSFont* GetNSFontForNativeFontInfo(const wxNativeFontInfo &info) | |
221 | { | |
222 | if(!info.faceName.empty()) | |
223 | { | |
224 | NSFont *font = [NSFont fontWithName:wxNSStringWithWxString(info.faceName) size:info.pointSize]; | |
225 | // TODO: use NSFontManager to mix in the weights and whatnot | |
226 | if(font != nil) | |
227 | return font; | |
228 | // To err or not to err? | |
229 | } | |
230 | // No font with that face name or no face name | |
231 | ||
232 | NSFontTraitMask cocoaTraits = 0; | |
233 | int cocoaWeight = 5; | |
234 | NSFont *font = [[NSFontManager sharedFontManager] fontWithFamily:GetFamilyName(info.family) traits:cocoaTraits weight:cocoaWeight size:info.pointSize]; | |
235 | return font; | |
236 | } | |
237 | ||
238 | /*! @discussion | |
239 | Updates all fields of @a info except for underlined which is not a property of NSFont. | |
240 | */ | |
241 | static void UpdateNativeFontInfoWithNSFont(wxNativeFontInfo &info, NSFont *cocoaNSFont) | |
242 | { | |
243 | info.pointSize = [cocoaNSFont pointSize]; | |
244 | ||
245 | // FIXME: We could maybe improve on this? | |
246 | info.family = wxFONTFAMILY_DEFAULT; | |
247 | ||
248 | // FIXME: italicAngle might indicate a slanted rather than truly italic font? | |
249 | info.style = [cocoaNSFont italicAngle] == 0.0?wxFONTSTYLE_NORMAL:wxFONTSTYLE_ITALIC; | |
250 | ||
251 | int cocoaWeight = [[NSFontManager sharedFontManager] weightOfFont:cocoaNSFont]; | |
252 | if(cocoaWeight < 5) | |
253 | info.weight = wxFONTWEIGHT_LIGHT; | |
254 | else if(cocoaWeight < 9) | |
255 | info.weight = wxFONTWEIGHT_NORMAL; | |
256 | else | |
257 | info.weight = wxFONTWEIGHT_BOLD; | |
258 | ||
259 | // FIXME: Is this right? I think so. | |
260 | info.faceName = wxStringWithNSString([cocoaNSFont fontName]); | |
261 | ||
262 | // TODO: Translate NSStringEncoding to wxFontEncoding | |
263 | info.encoding = wxFONTENCODING_SYSTEM; | |
264 | } | |
265 | ||
266 | /*! @discussion | |
267 | Creates a new generic wxNativeFontInfo from an NSFont and an underlined flag. | |
268 | Uses UpdateNativeFontInfoWithNSFont to do the work and sets the underlined field | |
269 | of wxNativeFontInfo to the @a underlined argument. | |
270 | */ | |
271 | static wxNativeFontInfo MakeNativeFontInfoForNSFont(NSFont *cocoaNSFont, bool underlined) | |
272 | { | |
273 | wxNativeFontInfo info; | |
274 | UpdateNativeFontInfoWithNSFont(info, cocoaNSFont); | |
275 | ||
276 | // NSFont are never underlined.. that's a function of the drawing system | |
277 | info.underlined = underlined; | |
278 | ||
279 | return info; | |
280 | } | |
281 | ||
282 | //#include "_font_test_2_8_abi_compat.h" | |
283 | ||
a24aff65 | 284 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) |
a24aff65 | 285 | |
51146826 | 286 | static wxNativeFontInfo MakeNativeFontInfo(int size, int family, int style, int weight, bool underlined, const wxString& faceName, wxFontEncoding encoding) |
a24aff65 | 287 | { |
51146826 | 288 | wxNativeFontInfo m_info; // NOTE: not an i-var despite name |
151b7b73 DE |
289 | m_info.pointSize = size; |
290 | m_info.family = static_cast<wxFontFamily>(family); | |
291 | m_info.style = static_cast<wxFontStyle>(style); | |
292 | m_info.weight = static_cast<wxFontWeight>(weight); | |
293 | m_info.underlined = underlined; | |
294 | m_info.faceName = faceName; | |
295 | m_info.encoding = encoding; | |
51146826 DE |
296 | return m_info; |
297 | } | |
298 | ||
299 | void wxFontRefData::Init(int size, int family, int style, int weight, bool underlined, const wxString& faceName, wxFontEncoding encoding) | |
300 | { | |
301 | m_info = MakeNativeFontInfo(size, family, style, weight, underlined, faceName, encoding); | |
a24aff65 DE |
302 | } |
303 | ||
304 | wxFontRefData::~wxFontRefData() | |
305 | { | |
51146826 DE |
306 | [m_cocoaNSFont release]; |
307 | m_cocoaNSFont = nil; | |
a24aff65 DE |
308 | // TODO: delete font data |
309 | } | |
310 | ||
68c95704 | 311 | #define M_FONTDATA ((wxFontRefData*)m_refData) |
873fd4af | 312 | |
51146826 DE |
313 | wxFont wxCocoaFontFactory::InstanceForNSFont(WX_NSFont cocoaFont, bool underlined) |
314 | { | |
315 | return wxFont(new wxFontRefData(cocoaFont, underlined)); | |
316 | } | |
317 | ||
318 | bool wxFont::Create(wxFontRefData *refData) | |
319 | { | |
320 | UnRef(); | |
321 | m_refData = refData; | |
322 | ||
323 | return m_refData != NULL; | |
324 | } | |
325 | ||
05725592 | 326 | bool wxFont::Create(const wxNativeFontInfo& nativeFontInfo) |
a24aff65 | 327 | { |
05725592 DE |
328 | UnRef(); |
329 | m_refData = new wxFontRefData(nativeFontInfo); | |
330 | ||
331 | return true; | |
a24aff65 DE |
332 | } |
333 | ||
334 | void wxFont::SetEncoding(wxFontEncoding) | |
335 | { | |
336 | } | |
337 | ||
338 | wxFontEncoding wxFont::GetEncoding() const | |
339 | { | |
340 | return wxFontEncoding(); | |
341 | } | |
342 | ||
343 | int wxFont::GetPointSize() const | |
344 | { | |
05725592 DE |
345 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
346 | return M_FONTDATA->m_info.pointSize; | |
a24aff65 DE |
347 | } |
348 | ||
349 | bool wxFont::GetUnderlined() const | |
350 | { | |
e7e97a59 | 351 | if(M_FONTDATA) |
151b7b73 | 352 | return M_FONTDATA->m_info.underlined; |
e7e97a59 DE |
353 | else |
354 | return false; | |
a24aff65 DE |
355 | } |
356 | ||
357 | int wxFont::GetStyle() const | |
358 | { | |
05725592 DE |
359 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
360 | return M_FONTDATA->m_info.style; | |
a24aff65 DE |
361 | } |
362 | ||
363 | int wxFont::GetFamily() const | |
364 | { | |
05725592 DE |
365 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
366 | return M_FONTDATA->m_info.family; | |
a24aff65 DE |
367 | } |
368 | ||
369 | int wxFont::GetWeight() const | |
370 | { | |
05725592 DE |
371 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
372 | return M_FONTDATA->m_info.weight; | |
a24aff65 DE |
373 | } |
374 | ||
3bf5a59b VZ |
375 | const wxNativeFontInfo *wxFont::GetNativeFontInfo() const |
376 | { | |
151b7b73 DE |
377 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
378 | return &M_FONTDATA->m_info; | |
3bf5a59b VZ |
379 | } |
380 | ||
a24aff65 DE |
381 | bool wxFont::Create(int pointSize, int family, int style, int weight, bool underlined, const wxString& faceName, wxFontEncoding encoding) |
382 | { | |
383 | UnRef(); | |
151b7b73 | 384 | m_refData = new wxFontRefData(pointSize, family, style, weight, underlined, faceName, encoding); |
a24aff65 DE |
385 | |
386 | RealizeResource(); | |
387 | ||
8898456d | 388 | return true; |
a24aff65 DE |
389 | } |
390 | ||
391 | wxFont::~wxFont() | |
392 | { | |
a24aff65 DE |
393 | } |
394 | ||
395 | bool wxFont::RealizeResource() | |
396 | { | |
397 | // TODO: create the font (if there is a native font object) | |
8898456d | 398 | return false; |
a24aff65 DE |
399 | } |
400 | ||
401 | void wxFont::Unshare() | |
402 | { | |
8898456d WS |
403 | // Don't change shared data |
404 | if (!m_refData) | |
a24aff65 | 405 | { |
8898456d WS |
406 | m_refData = new wxFontRefData(); |
407 | } | |
a24aff65 DE |
408 | else |
409 | { | |
8898456d WS |
410 | wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData); |
411 | UnRef(); | |
412 | m_refData = ref; | |
413 | } | |
a24aff65 DE |
414 | } |
415 | ||
416 | void wxFont::SetPointSize(int pointSize) | |
417 | { | |
418 | Unshare(); | |
419 | ||
151b7b73 | 420 | M_FONTDATA->m_info.pointSize = pointSize; |
a24aff65 DE |
421 | |
422 | RealizeResource(); | |
423 | } | |
424 | ||
425 | void wxFont::SetFamily(int family) | |
426 | { | |
427 | Unshare(); | |
428 | ||
151b7b73 | 429 | M_FONTDATA->m_info.family = static_cast<wxFontFamily>(family); |
a24aff65 DE |
430 | |
431 | RealizeResource(); | |
432 | } | |
433 | ||
434 | void wxFont::SetStyle(int style) | |
435 | { | |
436 | Unshare(); | |
437 | ||
151b7b73 | 438 | M_FONTDATA->m_info.style = static_cast<wxFontStyle>(style); |
a24aff65 DE |
439 | |
440 | RealizeResource(); | |
441 | } | |
442 | ||
443 | void wxFont::SetWeight(int weight) | |
444 | { | |
445 | Unshare(); | |
446 | ||
151b7b73 | 447 | M_FONTDATA->m_info.weight = static_cast<wxFontWeight>(weight); |
a24aff65 DE |
448 | |
449 | RealizeResource(); | |
450 | } | |
451 | ||
85ab460e | 452 | bool wxFont::SetFaceName(const wxString& faceName) |
a24aff65 DE |
453 | { |
454 | Unshare(); | |
455 | ||
151b7b73 | 456 | M_FONTDATA->m_info.faceName = faceName; |
a24aff65 DE |
457 | |
458 | RealizeResource(); | |
85ab460e VZ |
459 | |
460 | return wxFontBase::SetFaceName(faceName); | |
a24aff65 DE |
461 | } |
462 | ||
463 | void wxFont::SetUnderlined(bool underlined) | |
464 | { | |
465 | Unshare(); | |
466 | ||
151b7b73 | 467 | M_FONTDATA->m_info.underlined = underlined; |
a24aff65 DE |
468 | |
469 | RealizeResource(); | |
470 | } | |
471 | ||
472 | /* New font system */ | |
473 | wxString wxFont::GetFaceName() const | |
474 | { | |
b0c0a393 | 475 | wxString str; |
a24aff65 | 476 | if (M_FONTDATA) |
151b7b73 | 477 | str = M_FONTDATA->m_info.faceName; |
a24aff65 DE |
478 | return str; |
479 | } | |
480 | ||
481 | // vim:sts=4:sw=4:et |