summaryrefslogtreecommitdiffstats
path: root/third_party/rust/core-text/src/font_descriptor.rs
blob: e5506d2162f1cf3584bea2f215f5735a3a7ded63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright 2013 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(non_upper_case_globals)]

use core_foundation::array::CFArrayRef;
use core_foundation::base::{CFType, CFTypeID, CFTypeRef, TCFType};
use core_foundation::dictionary::{CFDictionary, CFDictionaryRef};
use core_foundation::number::{CFNumber, CFNumberRef};
use core_foundation::set::CFSetRef;
use core_foundation::string::{CFString, CFStringRef};
use core_foundation::url::{CFURLRef, CFURL};
use core_graphics::base::CGFloat;

use std::os::raw::c_void;
use std::path::PathBuf;

/*
* CTFontTraits.h
*/
// actually, these are extern enums
pub type CTFontFormat = u32;
pub const kCTFontFormatUnrecognized: CTFontFormat = 0;
pub const kCTFontFormatOpenTypePostScript: CTFontFormat = 1;
pub const kCTFontFormatOpenTypeTrueType: CTFontFormat = 2;
pub const kCTFontFormatTrueType: CTFontFormat = 3;
pub const kCTFontFormatPostScript: CTFontFormat = 4;
pub const kCTFontFormatBitmap: CTFontFormat = 5;

pub const kCTFontClassMaskShift: u32 = 28;

pub type CTFontSymbolicTraits = u32;
pub const kCTFontItalicTrait: CTFontSymbolicTraits = 1 << 0;
pub const kCTFontBoldTrait: CTFontSymbolicTraits = 1 << 1;
pub const kCTFontExpandedTrait: CTFontSymbolicTraits = 1 << 5;
pub const kCTFontCondensedTrait: CTFontSymbolicTraits = 1 << 6;
pub const kCTFontMonoSpaceTrait: CTFontSymbolicTraits = 1 << 10;
pub const kCTFontVerticalTrait: CTFontSymbolicTraits = 1 << 11;
pub const kCTFontUIOptimizedTrait: CTFontSymbolicTraits = 1 << 12;
pub const kCTFontColorGlyphsTrait: CTFontSymbolicTraits = 1 << 13;
pub const kCTFontClassMaskTrait: CTFontSymbolicTraits = 15 << kCTFontClassMaskShift;

pub trait SymbolicTraitAccessors {
    fn is_italic(&self) -> bool;
    fn is_bold(&self) -> bool;
    fn is_expanded(&self) -> bool;
    fn is_condensed(&self) -> bool;
    fn is_monospace(&self) -> bool;
    fn is_vertical(&self) -> bool;
}

impl SymbolicTraitAccessors for CTFontSymbolicTraits {
    fn is_italic(&self) -> bool {
        (*self & kCTFontItalicTrait) != 0
    }
    fn is_bold(&self) -> bool {
        (*self & kCTFontBoldTrait) != 0
    }
    fn is_expanded(&self) -> bool {
        (*self & kCTFontExpandedTrait) != 0
    }
    fn is_condensed(&self) -> bool {
        (*self & kCTFontCondensedTrait) != 0
    }
    fn is_monospace(&self) -> bool {
        (*self & kCTFontMonoSpaceTrait) != 0
    }
    fn is_vertical(&self) -> bool {
        (*self & kCTFontVerticalTrait) != 0
    }
}

pub type CTFontStylisticClass = u32;
pub const kCTFontUnknownClass: CTFontStylisticClass = 0 << kCTFontClassMaskShift;
pub const kCTFontOldStyleSerifsClass: CTFontStylisticClass = 1 << kCTFontClassMaskShift;
pub const kCTFontTransitionalSerifsClass: CTFontStylisticClass = 2 << kCTFontClassMaskShift;
pub const kCTFontModernSerifsClass: CTFontStylisticClass = 3 << kCTFontClassMaskShift;
pub const kCTFontClarendonSerifsClass: CTFontStylisticClass = 4 << kCTFontClassMaskShift;
pub const kCTFontSlabSerifsClass: CTFontStylisticClass = 5 << kCTFontClassMaskShift;
pub const kCTFontFreeformSerifsClass: CTFontStylisticClass = 7 << kCTFontClassMaskShift;
pub const kCTFontSansSerifClass: CTFontStylisticClass = 8 << kCTFontClassMaskShift;
pub const kCTFontOrnamentalsClass: CTFontStylisticClass = 9 << kCTFontClassMaskShift;
pub const kCTFontScriptsClass: CTFontStylisticClass = 10 << kCTFontClassMaskShift;
pub const kCTFontSymbolicClass: CTFontStylisticClass = 12 << kCTFontClassMaskShift;

pub trait StylisticClassAccessors {
    fn is_serif(&self) -> bool;
    fn is_sans_serif(&self) -> bool;
    fn is_script(&self) -> bool;
    fn is_fantasy(&self) -> bool;
    fn is_symbols(&self) -> bool;
}

impl StylisticClassAccessors for CTFontStylisticClass {
    fn is_serif(&self) -> bool {
        let any_serif_class = kCTFontOldStyleSerifsClass
            | kCTFontTransitionalSerifsClass
            | kCTFontModernSerifsClass
            | kCTFontClarendonSerifsClass
            | kCTFontSlabSerifsClass
            | kCTFontFreeformSerifsClass;

        (*self & any_serif_class) != 0
    }

    fn is_sans_serif(&self) -> bool {
        (*self & kCTFontSansSerifClass) != 0
    }

    fn is_script(&self) -> bool {
        (*self & kCTFontScriptsClass) != 0
    }

    fn is_fantasy(&self) -> bool {
        (*self & kCTFontOrnamentalsClass) != 0
    }

    fn is_symbols(&self) -> bool {
        (*self & kCTFontSymbolicClass) != 0
    }
}

pub type CTFontAttributes = CFDictionary;

pub type CTFontTraits = CFDictionary<CFString, CFType>;

pub trait TraitAccessors {
    fn symbolic_traits(&self) -> CTFontSymbolicTraits;
    fn normalized_weight(&self) -> f64;
    fn normalized_width(&self) -> f64;
    fn normalized_slant(&self) -> f64;
}

trait TraitAccessorPrivate {
    fn extract_number_for_key(&self, key: CFStringRef) -> CFNumber;
}

impl TraitAccessorPrivate for CTFontTraits {
    fn extract_number_for_key(&self, key: CFStringRef) -> CFNumber {
        let cftype = self.get(key);
        cftype.downcast::<CFNumber>().unwrap()
    }
}

impl TraitAccessors for CTFontTraits {
    fn symbolic_traits(&self) -> CTFontSymbolicTraits {
        unsafe {
            let number = self.extract_number_for_key(kCTFontSymbolicTrait);
            number.to_i64().unwrap() as u32
        }
    }

    fn normalized_weight(&self) -> f64 {
        unsafe {
            let number = self.extract_number_for_key(kCTFontWeightTrait);
            number.to_f64().unwrap()
        }
    }

    fn normalized_width(&self) -> f64 {
        unsafe {
            let number = self.extract_number_for_key(kCTFontWidthTrait);
            number.to_f64().unwrap()
        }
    }

    fn normalized_slant(&self) -> f64 {
        unsafe {
            let number = self.extract_number_for_key(kCTFontSlantTrait);
            number.to_f64().unwrap()
        }
    }
}

/*
* CTFontDescriptor.h
*/
pub type CTFontOrientation = u32;
pub const kCTFontDefaultOrientation: CTFontOrientation = 0; // deprecated since macOS 10.11, iOS 9
pub const kCTFontOrientationDefault: CTFontOrientation = 0; // introduced in macOS 10.8, iOS 6
pub const kCTFontHorizontalOrientation: CTFontOrientation = 1; // deprecated since macOS 10.11, iOS 9
pub const kCTFontOrientationHorizontal: CTFontOrientation = 1; // introduced in macOS 10.8, iOS 6
pub const kCTFontVerticalOrientation: CTFontOrientation = 2; // deprecated since macOS 10.11, iOS 9
pub const kCTFontOrientationVertical: CTFontOrientation = 2; // introduced in macOS 10.8, iOS 6

pub type CTFontPriority = u32;
pub const kCTFontPrioritySystem: CTFontPriority = 10000;
pub const kCTFontPriorityNetwork: CTFontPriority = 20000;
pub const kCTFontPriorityComputer: CTFontPriority = 30000;
pub const kCTFontPriorityUser: CTFontPriority = 40000;
pub const kCTFontPriorityDynamic: CTFontPriority = 50000;
pub const kCTFontPriorityProcess: CTFontPriority = 60000;

#[repr(C)]
pub struct __CTFontDescriptor(c_void);

pub type CTFontDescriptorRef = *const __CTFontDescriptor;

declare_TCFType! {
    CTFontDescriptor, CTFontDescriptorRef
}
impl_TCFType!(
    CTFontDescriptor,
    CTFontDescriptorRef,
    CTFontDescriptorGetTypeID
);
impl_CFTypeDescription!(CTFontDescriptor);

// "Font objects (CTFont, CTFontDescriptor, and associated objects) can be used
//  simultaneously by multiple operations, work queues, or threads."
unsafe impl Send for CTFontDescriptor {}
unsafe impl Sync for CTFontDescriptor {}

impl CTFontDescriptor {
    fn get_string_attribute(&self, attribute: CFStringRef) -> Option<String> {
        unsafe {
            let value = CTFontDescriptorCopyAttribute(self.0, attribute);
            if value.is_null() {
                return None;
            }

            let value = CFType::wrap_under_create_rule(value);
            assert!(value.instance_of::<CFString>());
            let s = CFString::wrap_under_get_rule(value.as_CFTypeRef() as CFStringRef);
            Some(s.to_string())
        }
    }
}

impl CTFontDescriptor {
    pub fn family_name(&self) -> String {
        unsafe {
            let value = self.get_string_attribute(kCTFontFamilyNameAttribute);
            value.expect("A font must have a non-null family name.")
        }
    }

    pub fn font_name(&self) -> String {
        unsafe {
            let value = self.get_string_attribute(kCTFontNameAttribute);
            value.expect("A font must have a non-null name.")
        }
    }

    pub fn style_name(&self) -> String {
        unsafe {
            let value = self.get_string_attribute(kCTFontStyleNameAttribute);
            value.expect("A font must have a non-null style name.")
        }
    }

    pub fn display_name(&self) -> String {
        unsafe {
            let value = self.get_string_attribute(kCTFontDisplayNameAttribute);
            value.expect("A font must have a non-null display name.")
        }
    }

    pub fn font_format(&self) -> Option<CTFontFormat> {
        unsafe {
            let value = CTFontDescriptorCopyAttribute(self.0, kCTFontFormatAttribute);
            if value.is_null() {
                return None;
            }

            let value = CFType::wrap_under_create_rule(value);
            assert!(value.instance_of::<CFNumber>());
            let format = CFNumber::wrap_under_get_rule(value.as_CFTypeRef() as CFNumberRef);
            format.to_i32().map(|x| x as CTFontFormat)
        }
    }

    pub fn font_path(&self) -> Option<PathBuf> {
        unsafe {
            let value = CTFontDescriptorCopyAttribute(self.0, kCTFontURLAttribute);
            if value.is_null() {
                return None;
            }

            let value = CFType::wrap_under_create_rule(value);
            assert!(value.instance_of::<CFURL>());
            let url = CFURL::wrap_under_get_rule(value.as_CFTypeRef() as CFURLRef);
            url.to_path()
        }
    }

    pub fn traits(&self) -> CTFontTraits {
        unsafe {
            let value = CTFontDescriptorCopyAttribute(self.0, kCTFontTraitsAttribute);
            assert!(!value.is_null());
            let value = CFType::wrap_under_create_rule(value);
            assert!(value.instance_of::<CFDictionary>());
            CFDictionary::wrap_under_get_rule(value.as_CFTypeRef() as CFDictionaryRef)
        }
    }

    pub fn create_copy_with_attributes(&self, attr: CFDictionary) -> Result<CTFontDescriptor, ()> {
        unsafe {
            let desc = CTFontDescriptorCreateCopyWithAttributes(
                self.as_concrete_TypeRef(),
                attr.as_concrete_TypeRef(),
            );
            if desc.is_null() {
                return Err(());
            }
            Ok(CTFontDescriptor::wrap_under_create_rule(desc))
        }
    }

    pub fn attributes(&self) -> CFDictionary<CFString, CFType> {
        unsafe {
            let attrs = CTFontDescriptorCopyAttributes(self.as_concrete_TypeRef());
            CFDictionary::wrap_under_create_rule(attrs)
        }
    }
}

pub fn new_from_attributes(attributes: &CFDictionary<CFString, CFType>) -> CTFontDescriptor {
    unsafe {
        let result: CTFontDescriptorRef =
            CTFontDescriptorCreateWithAttributes(attributes.as_concrete_TypeRef());
        CTFontDescriptor::wrap_under_create_rule(result)
    }
}

pub fn new_from_variations(variations: &CFDictionary<CFString, CFNumber>) -> CTFontDescriptor {
    unsafe {
        let var_key = CFString::wrap_under_get_rule(kCTFontVariationAttribute);
        let var_val = CFType::wrap_under_get_rule(variations.as_CFTypeRef());
        let attributes = CFDictionary::from_CFType_pairs(&[(var_key, var_val)]);
        new_from_attributes(&attributes)
    }
}

pub fn new_from_postscript_name(name: &CFString) -> CTFontDescriptor {
    unsafe {
        let result: CTFontDescriptorRef =
            CTFontDescriptorCreateWithNameAndSize(name.as_concrete_TypeRef(), 0.0);
        CTFontDescriptor::wrap_under_create_rule(result)
    }
}

pub fn debug_descriptor(desc: &CTFontDescriptor) {
    println!("family: {}", desc.family_name());
    println!("name: {}", desc.font_name());
    println!("style: {}", desc.style_name());
    println!("display: {}", desc.display_name());
    println!("path: {:?}", desc.font_path());
    desc.show();
}

extern "C" {
    /*
     * CTFontTraits.h
     */

    // font trait constants
    pub static kCTFontSymbolicTrait: CFStringRef;
    pub static kCTFontWeightTrait: CFStringRef;
    pub static kCTFontWidthTrait: CFStringRef;
    pub static kCTFontSlantTrait: CFStringRef;

    /*
     * CTFontDescriptor.h
     */

    // font attribute constants. Note that the name-related attributes
    // here are somewhat flaky. Servo creates CTFont instances and
    // then uses CTFontCopyName to get more fine-grained names.
    pub static kCTFontURLAttribute: CFStringRef; // value: CFURLRef
    pub static kCTFontNameAttribute: CFStringRef; // value: CFStringRef
    pub static kCTFontDisplayNameAttribute: CFStringRef; // value: CFStringRef
    pub static kCTFontFamilyNameAttribute: CFStringRef; // value: CFStringRef
    pub static kCTFontStyleNameAttribute: CFStringRef; // value: CFStringRef
    pub static kCTFontTraitsAttribute: CFStringRef;
    pub static kCTFontVariationAttribute: CFStringRef;
    pub static kCTFontSizeAttribute: CFStringRef;
    pub static kCTFontMatrixAttribute: CFStringRef;
    pub static kCTFontCascadeListAttribute: CFStringRef;
    pub static kCTFontCharacterSetAttribute: CFStringRef;
    pub static kCTFontLanguagesAttribute: CFStringRef;
    pub static kCTFontBaselineAdjustAttribute: CFStringRef;
    pub static kCTFontMacintoshEncodingsAttribute: CFStringRef;
    pub static kCTFontFeaturesAttribute: CFStringRef;
    pub static kCTFontFeatureSettingsAttribute: CFStringRef;
    pub static kCTFontFixedAdvanceAttribute: CFStringRef;
    pub static kCTFontOrientationAttribute: CFStringRef;
    pub static kCTFontFormatAttribute: CFStringRef;
    pub static kCTFontRegistrationScopeAttribute: CFStringRef;
    pub static kCTFontPriorityAttribute: CFStringRef;
    pub static kCTFontEnabledAttribute: CFStringRef;

    pub fn CTFontDescriptorCopyAttribute(
        descriptor: CTFontDescriptorRef,
        attribute: CFStringRef,
    ) -> CFTypeRef;
    pub fn CTFontDescriptorCopyAttributes(descriptor: CTFontDescriptorRef) -> CFDictionaryRef;
    pub fn CTFontDescriptorCopyLocalizedAttribute(
        descriptor: CTFontDescriptorRef,
        attribute: CFStringRef,
        language: *mut CFStringRef,
    ) -> CFTypeRef;
    pub fn CTFontDescriptorCreateCopyWithAttributes(
        original: CTFontDescriptorRef,
        attributes: CFDictionaryRef,
    ) -> CTFontDescriptorRef;
    pub fn CTFontDescriptorCreateCopyWithFeature(
        original: CTFontDescriptorRef,
        featureTypeIdentifier: CFNumberRef,
        featureSelectorIdentifier: CFNumberRef,
    ) -> CTFontDescriptorRef;
    pub fn CTFontDescriptorCreateCopyWithVariation(
        original: CTFontDescriptorRef,
        variationIdentifier: CFNumberRef,
        variationValue: CGFloat,
    ) -> CTFontDescriptorRef;
    pub fn CTFontDescriptorCreateMatchingFontDescriptor(
        descriptor: CTFontDescriptorRef,
        mandatoryAttributes: CFSetRef,
    ) -> CTFontDescriptorRef;
    pub fn CTFontDescriptorCreateWithAttributes(attributes: CFDictionaryRef)
        -> CTFontDescriptorRef;
    pub fn CTFontDescriptorCreateWithNameAndSize(
        name: CFStringRef,
        size: CGFloat,
    ) -> CTFontDescriptorRef;
    pub fn CTFontDescriptorGetTypeID() -> CFTypeID;
}

extern "C" {
    pub fn CTFontDescriptorCreateMatchingFontDescriptors(
        descriptor: CTFontDescriptorRef,
        mandatoryAttributes: CFSetRef,
    ) -> CFArrayRef;
}