Debian Codegraph @

third_party/rust/core-text/src/font_descriptor.rs

raw
1 // Copyright 2013 The Servo Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![allow(non_upper_case_globals)]
11
12 use core_foundation::array::CFArrayRef;
13 use core_foundation::base::{CFType, CFTypeID, CFTypeRef, TCFType};
14 use core_foundation::dictionary::{CFDictionary, CFDictionaryRef};
15 use core_foundation::number::{CFNumber, CFNumberRef};
16 use core_foundation::set::CFSetRef;
17 use core_foundation::string::{CFString, CFStringRef};
18 use core_foundation::url::{CFURLRef, CFURL};
19 use core_graphics::base::CGFloat;
20
21 use std::os::raw::c_void;
22 use std::path::PathBuf;
23
24 /*
25 * CTFontTraits.h
26 */
27 // actually, these are extern enums
28 pub type CTFontFormat = u32;
29 pub const kCTFontFormatUnrecognized: CTFontFormat = 0;
30 pub const kCTFontFormatOpenTypePostScript: CTFontFormat = 1;
31 pub const kCTFontFormatOpenTypeTrueType: CTFontFormat = 2;
32 pub const kCTFontFormatTrueType: CTFontFormat = 3;
33 pub const kCTFontFormatPostScript: CTFontFormat = 4;
34 pub const kCTFontFormatBitmap: CTFontFormat = 5;
35
36 pub const kCTFontClassMaskShift: u32 = 28;
37
38 pub type CTFontSymbolicTraits = u32;
39 pub const kCTFontItalicTrait: CTFontSymbolicTraits = 1 << 0;
40 pub const kCTFontBoldTrait: CTFontSymbolicTraits = 1 << 1;
41 pub const kCTFontExpandedTrait: CTFontSymbolicTraits = 1 << 5;
42 pub const kCTFontCondensedTrait: CTFontSymbolicTraits = 1 << 6;
43 pub const kCTFontMonoSpaceTrait: CTFontSymbolicTraits = 1 << 10;
44 pub const kCTFontVerticalTrait: CTFontSymbolicTraits = 1 << 11;
45 pub const kCTFontUIOptimizedTrait: CTFontSymbolicTraits = 1 << 12;
46 pub const kCTFontColorGlyphsTrait: CTFontSymbolicTraits = 1 << 13;
47 pub const kCTFontClassMaskTrait: CTFontSymbolicTraits = 15 << kCTFontClassMaskShift;
48
49 pub trait SymbolicTraitAccessors {
50 fn is_italic(&self) -> bool;
51 fn is_bold(&self) -> bool;
52 fn is_expanded(&self) -> bool;
53 fn is_condensed(&self) -> bool;
54 fn is_monospace(&self) -> bool;
55 fn is_vertical(&self) -> bool;
56 }
57
58 impl SymbolicTraitAccessors for CTFontSymbolicTraits {
59 fn is_italic(&self) -> bool {
60 (*self & kCTFontItalicTrait) != 0
61 }
62 fn is_bold(&self) -> bool {
63 (*self & kCTFontBoldTrait) != 0
64 }
65 fn is_expanded(&self) -> bool {
66 (*self & kCTFontExpandedTrait) != 0
67 }
68 fn is_condensed(&self) -> bool {
69 (*self & kCTFontCondensedTrait) != 0
70 }
71 fn is_monospace(&self) -> bool {
72 (*self & kCTFontMonoSpaceTrait) != 0
73 }
74 fn is_vertical(&self) -> bool {
75 (*self & kCTFontVerticalTrait) != 0
76 }
77 }
78
79 pub type CTFontStylisticClass = u32;
80 pub const kCTFontUnknownClass: CTFontStylisticClass = 0 << kCTFontClassMaskShift;
81 pub const kCTFontOldStyleSerifsClass: CTFontStylisticClass = 1 << kCTFontClassMaskShift;
82 pub const kCTFontTransitionalSerifsClass: CTFontStylisticClass = 2 << kCTFontClassMaskShift;
83 pub const kCTFontModernSerifsClass: CTFontStylisticClass = 3 << kCTFontClassMaskShift;
84 pub const kCTFontClarendonSerifsClass: CTFontStylisticClass = 4 << kCTFontClassMaskShift;
85 pub const kCTFontSlabSerifsClass: CTFontStylisticClass = 5 << kCTFontClassMaskShift;
86 pub const kCTFontFreeformSerifsClass: CTFontStylisticClass = 7 << kCTFontClassMaskShift;
87 pub const kCTFontSansSerifClass: CTFontStylisticClass = 8 << kCTFontClassMaskShift;
88 pub const kCTFontOrnamentalsClass: CTFontStylisticClass = 9 << kCTFontClassMaskShift;
89 pub const kCTFontScriptsClass: CTFontStylisticClass = 10 << kCTFontClassMaskShift;
90 pub const kCTFontSymbolicClass: CTFontStylisticClass = 12 << kCTFontClassMaskShift;
91
92 pub trait StylisticClassAccessors {
93 fn is_serif(&self) -> bool;
94 fn is_sans_serif(&self) -> bool;
95 fn is_script(&self) -> bool;
96 fn is_fantasy(&self) -> bool;
97 fn is_symbols(&self) -> bool;
98 }
99
100 impl StylisticClassAccessors for CTFontStylisticClass {
101 fn is_serif(&self) -> bool {
102 let any_serif_class = kCTFontOldStyleSerifsClass
103 | kCTFontTransitionalSerifsClass
104 | kCTFontModernSerifsClass
105 | kCTFontClarendonSerifsClass
106 | kCTFontSlabSerifsClass
107 | kCTFontFreeformSerifsClass;
108
109 (*self & any_serif_class) != 0
110 }
111
112 fn is_sans_serif(&self) -> bool {
113 (*self & kCTFontSansSerifClass) != 0
114 }
115
116 fn is_script(&self) -> bool {
117 (*self & kCTFontScriptsClass) != 0
118 }
119
120 fn is_fantasy(&self) -> bool {
121 (*self & kCTFontOrnamentalsClass) != 0
122 }
123
124 fn is_symbols(&self) -> bool {
125 (*self & kCTFontSymbolicClass) != 0
126 }
127 }
128
129 pub type CTFontAttributes = CFDictionary;
130
131 pub type CTFontTraits = CFDictionary<CFString, CFType>;
132
133 pub trait TraitAccessors {
134 fn symbolic_traits(&self) -> CTFontSymbolicTraits;
135 fn normalized_weight(&self) -> f64;
136 fn normalized_width(&self) -> f64;
137 fn normalized_slant(&self) -> f64;
138 }
139
140 trait TraitAccessorPrivate {
141 fn extract_number_for_key(&self, key: CFStringRef) -> CFNumber;
142 }
143
144 impl TraitAccessorPrivate for CTFontTraits {
145 fn extract_number_for_key(&self, key: CFStringRef) -> CFNumber {
146 let cftype = self.get(key);
147 cftype.downcast::<CFNumber>().unwrap()
148 }
149 }
150
151 impl TraitAccessors for CTFontTraits {
152 fn symbolic_traits(&self) -> CTFontSymbolicTraits {
153 unsafe {
154 let number = self.extract_number_for_key(kCTFontSymbolicTrait);
155 number.to_i64().unwrap() as u32
156 }
157 }
158
159 fn normalized_weight(&self) -> f64 {
160 unsafe {
161 let number = self.extract_number_for_key(kCTFontWeightTrait);
162 number.to_f64().unwrap()
163 }
164 }
165
166 fn normalized_width(&self) -> f64 {
167 unsafe {
168 let number = self.extract_number_for_key(kCTFontWidthTrait);
169 number.to_f64().unwrap()
170 }
171 }
172
173 fn normalized_slant(&self) -> f64 {
174 unsafe {
175 let number = self.extract_number_for_key(kCTFontSlantTrait);
176 number.to_f64().unwrap()
177 }
178 }
179 }
180
181 /*
182 * CTFontDescriptor.h
183 */
184 pub type CTFontOrientation = u32;
185 pub const kCTFontDefaultOrientation: CTFontOrientation = 0; // deprecated since macOS 10.11, iOS 9
186 pub const kCTFontOrientationDefault: CTFontOrientation = 0; // introduced in macOS 10.8, iOS 6
187 pub const kCTFontHorizontalOrientation: CTFontOrientation = 1; // deprecated since macOS 10.11, iOS 9
188 pub const kCTFontOrientationHorizontal: CTFontOrientation = 1; // introduced in macOS 10.8, iOS 6
189 pub const kCTFontVerticalOrientation: CTFontOrientation = 2; // deprecated since macOS 10.11, iOS 9
190 pub const kCTFontOrientationVertical: CTFontOrientation = 2; // introduced in macOS 10.8, iOS 6
191
192 pub type CTFontPriority = u32;
193 pub const kCTFontPrioritySystem: CTFontPriority = 10000;
194 pub const kCTFontPriorityNetwork: CTFontPriority = 20000;
195 pub const kCTFontPriorityComputer: CTFontPriority = 30000;
196 pub const kCTFontPriorityUser: CTFontPriority = 40000;
197 pub const kCTFontPriorityDynamic: CTFontPriority = 50000;
198 pub const kCTFontPriorityProcess: CTFontPriority = 60000;
199
200 #[repr(C)]
201 pub struct __CTFontDescriptor(c_void);
202
203 pub type CTFontDescriptorRef = *const __CTFontDescriptor;
204
205 declare_TCFType! {
206 CTFontDescriptor, CTFontDescriptorRef
207 }
208 impl_TCFType!(
209 CTFontDescriptor,
210 CTFontDescriptorRef,
211 CTFontDescriptorGetTypeID
212 );
213 impl_CFTypeDescription!(CTFontDescriptor);
214
215 // "Font objects (CTFont, CTFontDescriptor, and associated objects) can be used
216 // simultaneously by multiple operations, work queues, or threads."
217 unsafe impl Send for CTFontDescriptor {}
218 unsafe impl Sync for CTFontDescriptor {}
219
220 impl CTFontDescriptor {
221 fn get_string_attribute(&self, attribute: CFStringRef) -> Option<String> {
222 unsafe {
223 let value = CTFontDescriptorCopyAttribute(self.0, attribute);
224 if value.is_null() {
225 return None;
226 }
227
228 let value = CFType::wrap_under_create_rule(value);
229 assert!(value.instance_of::<CFString>());
230 let s = CFString::wrap_under_get_rule(value.as_CFTypeRef() as CFStringRef);
231 Some(s.to_string())
232 }
233 }
234 }
235
236 impl CTFontDescriptor {
237 pub fn family_name(&self) -> String {
238 unsafe {
239 let value = self.get_string_attribute(kCTFontFamilyNameAttribute);
240 value.expect("A font must have a non-null family name.")
241 }
242 }
243
244 pub fn font_name(&self) -> String {
245 unsafe {
246 let value = self.get_string_attribute(kCTFontNameAttribute);
247 value.expect("A font must have a non-null name.")
248 }
249 }
250
251 pub fn style_name(&self) -> String {
252 unsafe {
253 let value = self.get_string_attribute(kCTFontStyleNameAttribute);
254 value.expect("A font must have a non-null style name.")
255 }
256 }
257
258 pub fn display_name(&self) -> String {
259 unsafe {
260 let value = self.get_string_attribute(kCTFontDisplayNameAttribute);
261 value.expect("A font must have a non-null display name.")
262 }
263 }
264
265 pub fn font_format(&self) -> Option<CTFontFormat> {
266 unsafe {
267 let value = CTFontDescriptorCopyAttribute(self.0, kCTFontFormatAttribute);
268 if value.is_null() {
269 return None;
270 }
271
272 let value = CFType::wrap_under_create_rule(value);
273 assert!(value.instance_of::<CFNumber>());
274 let format = CFNumber::wrap_under_get_rule(value.as_CFTypeRef() as CFNumberRef);
275 format.to_i32().map(|x| x as CTFontFormat)
276 }
277 }
278
279 pub fn font_path(&self) -> Option<PathBuf> {
280 unsafe {
281 let value = CTFontDescriptorCopyAttribute(self.0, kCTFontURLAttribute);
282 if value.is_null() {
283 return None;
284 }
285
286 let value = CFType::wrap_under_create_rule(value);
287 assert!(value.instance_of::<CFURL>());
288 let url = CFURL::wrap_under_get_rule(value.as_CFTypeRef() as CFURLRef);
289 url.to_path()
290 }
291 }
292
293 pub fn traits(&self) -> CTFontTraits {
294 unsafe {
295 let value = CTFontDescriptorCopyAttribute(self.0, kCTFontTraitsAttribute);
296 assert!(!value.is_null());
297 let value = CFType::wrap_under_create_rule(value);
298 assert!(value.instance_of::<CFDictionary>());
299 CFDictionary::wrap_under_get_rule(value.as_CFTypeRef() as CFDictionaryRef)
300 }
301 }
302
303 pub fn create_copy_with_attributes(&self, attr: CFDictionary) -> Result<CTFontDescriptor, ()> {
304 unsafe {
305 let desc = CTFontDescriptorCreateCopyWithAttributes(
306 self.as_concrete_TypeRef(),
307 attr.as_concrete_TypeRef(),
308 );
309 if desc.is_null() {
310 return Err(());
311 }
312 Ok(CTFontDescriptor::wrap_under_create_rule(desc))
313 }
314 }
315
316 pub fn attributes(&self) -> CFDictionary<CFString, CFType> {
317 unsafe {
318 let attrs = CTFontDescriptorCopyAttributes(self.as_concrete_TypeRef());
319 CFDictionary::wrap_under_create_rule(attrs)
320 }
321 }
322 }
323
324 pub fn new_from_attributes(attributes: &CFDictionary<CFString, CFType>) -> CTFontDescriptor {
325 unsafe {
326 let result: CTFontDescriptorRef =
327 CTFontDescriptorCreateWithAttributes(attributes.as_concrete_TypeRef());
328 CTFontDescriptor::wrap_under_create_rule(result)
329 }
330 }
331
332 pub fn new_from_variations(variations: &CFDictionary<CFString, CFNumber>) -> CTFontDescriptor {
333 unsafe {
334 let var_key = CFString::wrap_under_get_rule(kCTFontVariationAttribute);
335 let var_val = CFType::wrap_under_get_rule(variations.as_CFTypeRef());
336 let attributes = CFDictionary::from_CFType_pairs(&[(var_key, var_val)]);
337 new_from_attributes(&attributes)
338 }
339 }
340
341 pub fn new_from_postscript_name(name: &CFString) -> CTFontDescriptor {
342 unsafe {
343 let result: CTFontDescriptorRef =
344 CTFontDescriptorCreateWithNameAndSize(name.as_concrete_TypeRef(), 0.0);
345 CTFontDescriptor::wrap_under_create_rule(result)
346 }
347 }
348
349 pub fn debug_descriptor(desc: &CTFontDescriptor) {
350 println!("family: {}", desc.family_name());
351 println!("name: {}", desc.font_name());
352 println!("style: {}", desc.style_name());
353 println!("display: {}", desc.display_name());
354 println!("path: {:?}", desc.font_path());
355 desc.show();
356 }
357
358 extern "C" {
359 /*
360 * CTFontTraits.h
361 */
362
363 // font trait constants
364 pub static kCTFontSymbolicTrait: CFStringRef;
365 pub static kCTFontWeightTrait: CFStringRef;
366 pub static kCTFontWidthTrait: CFStringRef;
367 pub static kCTFontSlantTrait: CFStringRef;
368
369 /*
370 * CTFontDescriptor.h
371 */
372
373 // font attribute constants. Note that the name-related attributes
374 // here are somewhat flaky. Servo creates CTFont instances and
375 // then uses CTFontCopyName to get more fine-grained names.
376 pub static kCTFontURLAttribute: CFStringRef; // value: CFURLRef
377 pub static kCTFontNameAttribute: CFStringRef; // value: CFStringRef
378 pub static kCTFontDisplayNameAttribute: CFStringRef; // value: CFStringRef
379 pub static kCTFontFamilyNameAttribute: CFStringRef; // value: CFStringRef
380 pub static kCTFontStyleNameAttribute: CFStringRef; // value: CFStringRef
381 pub static kCTFontTraitsAttribute: CFStringRef;
382 pub static kCTFontVariationAttribute: CFStringRef;
383 pub static kCTFontSizeAttribute: CFStringRef;
384 pub static kCTFontMatrixAttribute: CFStringRef;
385 pub static kCTFontCascadeListAttribute: CFStringRef;
386 pub static kCTFontCharacterSetAttribute: CFStringRef;
387 pub static kCTFontLanguagesAttribute: CFStringRef;
388 pub static kCTFontBaselineAdjustAttribute: CFStringRef;
389 pub static kCTFontMacintoshEncodingsAttribute: CFStringRef;
390 pub static kCTFontFeaturesAttribute: CFStringRef;
391 pub static kCTFontFeatureSettingsAttribute: CFStringRef;
392 pub static kCTFontFixedAdvanceAttribute: CFStringRef;
393 pub static kCTFontOrientationAttribute: CFStringRef;
394 pub static kCTFontFormatAttribute: CFStringRef;
395 pub static kCTFontRegistrationScopeAttribute: CFStringRef;
396 pub static kCTFontPriorityAttribute: CFStringRef;
397 pub static kCTFontEnabledAttribute: CFStringRef;
398
399 pub fn CTFontDescriptorCopyAttribute(
400 descriptor: CTFontDescriptorRef,
401 attribute: CFStringRef,
402 ) -> CFTypeRef;
403 pub fn CTFontDescriptorCopyAttributes(descriptor: CTFontDescriptorRef) -> CFDictionaryRef;
404 pub fn CTFontDescriptorCopyLocalizedAttribute(
405 descriptor: CTFontDescriptorRef,
406 attribute: CFStringRef,
407 language: *mut CFStringRef,
408 ) -> CFTypeRef;
409 pub fn CTFontDescriptorCreateCopyWithAttributes(
410 original: CTFontDescriptorRef,
411 attributes: CFDictionaryRef,
412 ) -> CTFontDescriptorRef;
413 pub fn CTFontDescriptorCreateCopyWithFeature(
414 original: CTFontDescriptorRef,
415 featureTypeIdentifier: CFNumberRef,
416 featureSelectorIdentifier: CFNumberRef,
417 ) -> CTFontDescriptorRef;
418 pub fn CTFontDescriptorCreateCopyWithVariation(
419 original: CTFontDescriptorRef,
420 variationIdentifier: CFNumberRef,
421 variationValue: CGFloat,
422 ) -> CTFontDescriptorRef;
423 pub fn CTFontDescriptorCreateMatchingFontDescriptor(
424 descriptor: CTFontDescriptorRef,
425 mandatoryAttributes: CFSetRef,
426 ) -> CTFontDescriptorRef;
427 pub fn CTFontDescriptorCreateWithAttributes(attributes: CFDictionaryRef)
428 -> CTFontDescriptorRef;
429 pub fn CTFontDescriptorCreateWithNameAndSize(
430 name: CFStringRef,
431 size: CGFloat,
432 ) -> CTFontDescriptorRef;
433 pub fn CTFontDescriptorGetTypeID() -> CFTypeID;
434 }
435
436 extern "C" {
437 pub fn CTFontDescriptorCreateMatchingFontDescriptors(
438 descriptor: CTFontDescriptorRef,
439 mandatoryAttributes: CFSetRef,
440 ) -> CFArrayRef;
441 }