Debian Codegraph @

third_party/rust/core-graphics/src/font.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 use core_foundation::base::{CFRelease, CFRetain, CFType, CFTypeID, TCFType};
11 use core_foundation::array::{CFArray, CFArrayRef};
12 use core_foundation::data::{CFData, CFDataRef};
13 use core_foundation::number::CFNumber;
14 use core_foundation::string::{CFString, CFStringRef};
15 use core_foundation::dictionary::{CFDictionary, CFDictionaryRef};
16 use data_provider::CGDataProvider;
17 use geometry::CGRect;
18 use std::ptr::NonNull;
19
20 use foreign_types::ForeignType;
21
22 use libc::{c_int, size_t};
23
24 pub use core_graphics_types::base::CGGlyph;
25
26 foreign_type! {
27 #[doc(hidden)]
28 pub unsafe type CGFont: Send + Sync {
29 type CType = ::sys::CGFont;
30 fn drop = |p| CFRelease(p as *mut _);
31 fn clone = |p| CFRetain(p as *const _) as *mut _;
32 }
33 }
34
35 impl CGFont {
36 pub fn type_id() -> CFTypeID {
37 unsafe {
38 CGFontGetTypeID()
39 }
40 }
41
42 pub fn from_data_provider(provider: CGDataProvider) -> Result<CGFont, ()> {
43 unsafe {
44 let font_ref = CGFontCreateWithDataProvider(provider.as_ptr());
45 match NonNull::new(font_ref) {
46 Some(font_ref) => Ok(CGFont(font_ref)),
47 None => Err(()),
48 }
49 }
50 }
51
52 pub fn from_name(name: &CFString) -> Result<CGFont, ()> {
53 unsafe {
54 let font_ref = CGFontCreateWithFontName(name.as_concrete_TypeRef());
55 match NonNull::new(font_ref) {
56 Some(font_ref) => Ok(CGFont(font_ref)),
57 None => Err(()),
58 }
59 }
60 }
61
62 pub fn create_copy_from_variations(&self, vars: &CFDictionary<CFString, CFNumber>) -> Result<CGFont, ()> {
63 unsafe {
64 let font_ref = CGFontCreateCopyWithVariations(self.as_ptr(),
65 vars.as_concrete_TypeRef());
66 match NonNull::new(font_ref) {
67 Some(font_ref) => Ok(CGFont(font_ref)),
68 None => Err(()),
69 }
70 }
71 }
72
73 pub fn postscript_name(&self) -> CFString {
74 unsafe {
75 let string_ref = CGFontCopyPostScriptName(self.as_ptr());
76 TCFType::wrap_under_create_rule(string_ref)
77 }
78 }
79
80 pub fn get_glyph_b_boxes(&self, glyphs: &[CGGlyph], bboxes: &mut [CGRect]) -> bool {
81 unsafe {
82 assert!(bboxes.len() >= glyphs.len());
83 CGFontGetGlyphBBoxes(self.as_ptr(),
84 glyphs.as_ptr(),
85 glyphs.len(),
86 bboxes.as_mut_ptr())
87 }
88 }
89
90 pub fn get_glyph_advances(&self, glyphs: &[CGGlyph], advances: &mut [c_int]) -> bool {
91 unsafe {
92 assert!(advances.len() >= glyphs.len());
93 CGFontGetGlyphAdvances(self.as_ptr(),
94 glyphs.as_ptr(),
95 glyphs.len(),
96 advances.as_mut_ptr())
97 }
98 }
99
100 pub fn get_units_per_em(&self) -> c_int {
101 unsafe {
102 CGFontGetUnitsPerEm(self.as_ptr())
103 }
104 }
105
106 pub fn copy_table_tags(&self) -> CFArray<u32> {
107 unsafe {
108 TCFType::wrap_under_create_rule(CGFontCopyTableTags(self.as_ptr()))
109 }
110 }
111
112 pub fn copy_table_for_tag(&self, tag: u32) -> Option<CFData> {
113 let data_ref = unsafe { CGFontCopyTableForTag(self.as_ptr(), tag) };
114 if !data_ref.is_null() {
115 Some(unsafe { TCFType::wrap_under_create_rule(data_ref) })
116 } else {
117 None
118 }
119 }
120
121 pub fn copy_variations(&self) -> Option<CFDictionary<CFString, CFNumber>> {
122 let variations = unsafe { CGFontCopyVariations(self.as_ptr()) };
123 if !variations.is_null() {
124 Some(unsafe { TCFType::wrap_under_create_rule(variations) })
125 } else {
126 None
127 }
128 }
129
130 pub fn copy_variation_axes(&self) -> Option<CFArray<CFDictionary<CFString, CFType>>> {
131 let axes = unsafe { CGFontCopyVariationAxes(self.as_ptr()) };
132 if !axes.is_null() {
133 Some(unsafe { TCFType::wrap_under_create_rule(axes) })
134 } else {
135 None
136 }
137 }
138 }
139
140 #[link(name = "CoreGraphics", kind = "framework")]
141 extern {
142 // TODO: basically nothing has bindings (even commented-out) besides what we use.
143 fn CGFontCreateWithDataProvider(provider: ::sys::CGDataProviderRef) -> ::sys::CGFontRef;
144 fn CGFontCreateWithFontName(name: CFStringRef) -> ::sys::CGFontRef;
145 fn CGFontCreateCopyWithVariations(font: ::sys::CGFontRef, vars: CFDictionaryRef) -> ::sys::CGFontRef;
146 fn CGFontGetTypeID() -> CFTypeID;
147
148 fn CGFontCopyPostScriptName(font: ::sys::CGFontRef) -> CFStringRef;
149
150 // These do the same thing as CFRetain/CFRelease, except
151 // gracefully handle a NULL argument. We don't use them.
152 //fn CGFontRetain(font: ::sys::CGFontRef);
153 //fn CGFontRelease(font: ::sys::CGFontRef);
154
155 fn CGFontGetGlyphBBoxes(font: ::sys::CGFontRef,
156 glyphs: *const CGGlyph,
157 count: size_t,
158 bboxes: *mut CGRect)
159 -> bool;
160 fn CGFontGetGlyphAdvances(font: ::sys::CGFontRef,
161 glyphs: *const CGGlyph,
162 count: size_t,
163 advances: *mut c_int)
164 -> bool;
165 fn CGFontGetUnitsPerEm(font: ::sys::CGFontRef) -> c_int;
166
167 fn CGFontCopyTableTags(font: ::sys::CGFontRef) -> CFArrayRef;
168 fn CGFontCopyTableForTag(font: ::sys::CGFontRef, tag: u32) -> CFDataRef;
169 fn CGFontCopyVariations(font: ::sys::CGFontRef) -> CFDictionaryRef;
170 fn CGFontCopyVariationAxes(font: ::sys::CGFontRef) -> CFArrayRef;
171 }