| 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 font_descriptor; |
| 13 |
use font_descriptor::{CTFontDescriptor, CTFontDescriptorRef, CTFontOrientation}; |
| 14 |
use font_descriptor::{CTFontSymbolicTraits, CTFontTraits, SymbolicTraitAccessors, TraitAccessors}; |
| 15 |
use font_manager::create_font_descriptor; |
| 16 |
|
| 17 |
use core_foundation::array::{CFArray, CFArrayRef}; |
| 18 |
use core_foundation::base::{CFIndex, CFOptionFlags, CFType, CFTypeID, CFTypeRef, TCFType}; |
| 19 |
use core_foundation::data::{CFData, CFDataRef}; |
| 20 |
use core_foundation::dictionary::{CFDictionary, CFDictionaryRef}; |
| 21 |
use core_foundation::number::CFNumber; |
| 22 |
use core_foundation::string::{CFString, CFStringRef, UniChar}; |
| 23 |
use core_foundation::url::{CFURLRef, CFURL}; |
| 24 |
use core_graphics::base::CGFloat; |
| 25 |
use core_graphics::context::CGContext; |
| 26 |
use core_graphics::font::{CGFont, CGGlyph}; |
| 27 |
use core_graphics::geometry::{CGAffineTransform, CGPoint, CGRect, CGSize}; |
| 28 |
use core_graphics::path::CGPath; |
| 29 |
|
| 30 |
use foreign_types::ForeignType; |
| 31 |
use libc::{self, size_t}; |
| 32 |
use std::os::raw::c_void; |
| 33 |
use std::ptr; |
| 34 |
|
| 35 |
type CGContextRef = *mut <CGContext as ForeignType>::CType; |
| 36 |
type CGFontRef = *mut <CGFont as ForeignType>::CType; |
| 37 |
type CGPathRef = *mut <CGPath as ForeignType>::CType; |
| 38 |
|
| 39 |
pub type CTFontUIFontType = u32; |
| 40 |
// kCTFontNoFontType: CTFontUIFontType = -1; |
| 41 |
pub const kCTFontUserFontType: CTFontUIFontType = 0; |
| 42 |
pub const kCTFontUserFixedPitchFontType: CTFontUIFontType = 1; |
| 43 |
pub const kCTFontSystemFontType: CTFontUIFontType = 2; |
| 44 |
pub const kCTFontEmphasizedSystemFontType: CTFontUIFontType = 3; |
| 45 |
pub const kCTFontSmallSystemFontType: CTFontUIFontType = 4; |
| 46 |
pub const kCTFontSmallEmphasizedSystemFontType: CTFontUIFontType = 5; |
| 47 |
pub const kCTFontMiniSystemFontType: CTFontUIFontType = 6; |
| 48 |
pub const kCTFontMiniEmphasizedSystemFontType: CTFontUIFontType = 7; |
| 49 |
pub const kCTFontViewsFontType: CTFontUIFontType = 8; |
| 50 |
pub const kCTFontApplicationFontType: CTFontUIFontType = 9; |
| 51 |
pub const kCTFontLabelFontType: CTFontUIFontType = 10; |
| 52 |
pub const kCTFontMenuTitleFontType: CTFontUIFontType = 11; |
| 53 |
pub const kCTFontMenuItemFontType: CTFontUIFontType = 12; |
| 54 |
pub const kCTFontMenuItemMarkFontType: CTFontUIFontType = 13; |
| 55 |
pub const kCTFontMenuItemCmdKeyFontType: CTFontUIFontType = 14; |
| 56 |
pub const kCTFontWindowTitleFontType: CTFontUIFontType = 15; |
| 57 |
pub const kCTFontPushButtonFontType: CTFontUIFontType = 16; |
| 58 |
pub const kCTFontUtilityWindowTitleFontType: CTFontUIFontType = 17; |
| 59 |
pub const kCTFontAlertHeaderFontType: CTFontUIFontType = 18; |
| 60 |
pub const kCTFontSystemDetailFontType: CTFontUIFontType = 19; |
| 61 |
pub const kCTFontEmphasizedSystemDetailFontType: CTFontUIFontType = 20; |
| 62 |
pub const kCTFontToolbarFontType: CTFontUIFontType = 21; |
| 63 |
pub const kCTFontSmallToolbarFontType: CTFontUIFontType = 22; |
| 64 |
pub const kCTFontMessageFontType: CTFontUIFontType = 23; |
| 65 |
pub const kCTFontPaletteFontType: CTFontUIFontType = 24; |
| 66 |
pub const kCTFontToolTipFontType: CTFontUIFontType = 25; |
| 67 |
pub const kCTFontControlContentFontType: CTFontUIFontType = 26; |
| 68 |
|
| 69 |
pub type CTFontTableTag = u32; |
| 70 |
// TODO: create bindings for enum with 'chars' values |
| 71 |
|
| 72 |
pub type CTFontTableOptions = u32; |
| 73 |
pub const kCTFontTableOptionsNoOptions: CTFontTableOptions = 0; |
| 74 |
pub const kCTFontTableOptionsExcludeSynthetic: CTFontTableOptions = 1 << 0; |
| 75 |
|
| 76 |
pub type CTFontOptions = CFOptionFlags; |
| 77 |
pub const kCTFontOptionsDefault: CTFontOptions = 0; |
| 78 |
pub const kCTFontOptionsPreventAutoActivation: CTFontOptions = 1 << 0; |
| 79 |
pub const kCTFontOptionsPreferSystemFont: CTFontOptions = 1 << 2; |
| 80 |
|
| 81 |
pub enum CTFontNameSpecifier { |
| 82 |
Copyright, |
| 83 |
Family, |
| 84 |
SubFamily, |
| 85 |
Style, |
| 86 |
Unique, |
| 87 |
Full, |
| 88 |
Version, |
| 89 |
PostScript, |
| 90 |
Trademark, |
| 91 |
Manufacturer, |
| 92 |
Designer, |
| 93 |
Description, |
| 94 |
VendorURL, |
| 95 |
DesignerURL, |
| 96 |
License, |
| 97 |
LicenseURL, |
| 98 |
SampleText, |
| 99 |
PostScriptCID, |
| 100 |
} |
| 101 |
|
| 102 |
impl From<CTFontNameSpecifier> for CFStringRef { |
| 103 |
fn from(val: CTFontNameSpecifier) -> Self { |
| 104 |
unsafe { |
| 105 |
match val { |
| 106 |
CTFontNameSpecifier::Copyright => kCTFontCopyrightNameKey, |
| 107 |
CTFontNameSpecifier::Family => kCTFontFamilyNameKey, |
| 108 |
CTFontNameSpecifier::SubFamily => kCTFontSubFamilyNameKey, |
| 109 |
CTFontNameSpecifier::Style => kCTFontStyleNameKey, |
| 110 |
CTFontNameSpecifier::Unique => kCTFontUniqueNameKey, |
| 111 |
CTFontNameSpecifier::Full => kCTFontFullNameKey, |
| 112 |
CTFontNameSpecifier::Version => kCTFontVersionNameKey, |
| 113 |
CTFontNameSpecifier::PostScript => kCTFontPostScriptNameKey, |
| 114 |
CTFontNameSpecifier::Trademark => kCTFontTrademarkNameKey, |
| 115 |
CTFontNameSpecifier::Manufacturer => kCTFontManufacturerNameKey, |
| 116 |
CTFontNameSpecifier::Designer => kCTFontDesignerNameKey, |
| 117 |
CTFontNameSpecifier::Description => kCTFontDescriptionNameKey, |
| 118 |
CTFontNameSpecifier::VendorURL => kCTFontVendorURLNameKey, |
| 119 |
CTFontNameSpecifier::DesignerURL => kCTFontDesignerURLNameKey, |
| 120 |
CTFontNameSpecifier::License => kCTFontLicenseNameKey, |
| 121 |
CTFontNameSpecifier::LicenseURL => kCTFontLicenseURLNameKey, |
| 122 |
CTFontNameSpecifier::SampleText => kCTFontSampleTextNameKey, |
| 123 |
CTFontNameSpecifier::PostScriptCID => kCTFontPostScriptCIDNameKey, |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
#[repr(C)] |
| 130 |
pub struct __CTFont(c_void); |
| 131 |
|
| 132 |
pub type CTFontRef = *const __CTFont; |
| 133 |
|
| 134 |
declare_TCFType! { |
| 135 |
CTFont, CTFontRef |
| 136 |
} |
| 137 |
impl_TCFType!(CTFont, CTFontRef, CTFontGetTypeID); |
| 138 |
impl_CFTypeDescription!(CTFont); |
| 139 |
|
| 140 |
unsafe impl Send for CTFont {} |
| 141 |
unsafe impl Sync for CTFont {} |
| 142 |
|
| 143 |
pub fn new_from_CGFont(cgfont: &CGFont, pt_size: f64) -> CTFont { |
| 144 |
unsafe { |
| 145 |
let font_ref = CTFontCreateWithGraphicsFont( |
| 146 |
cgfont.as_ptr() as *mut _, |
| 147 |
pt_size as CGFloat, |
| 148 |
ptr::null(), |
| 149 |
ptr::null(), |
| 150 |
); |
| 151 |
CTFont::wrap_under_create_rule(font_ref) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
pub fn new_from_CGFont_with_variations( |
| 156 |
cgfont: &CGFont, |
| 157 |
pt_size: f64, |
| 158 |
variations: &CFDictionary<CFString, CFNumber>, |
| 159 |
) -> CTFont { |
| 160 |
unsafe { |
| 161 |
let font_desc = font_descriptor::new_from_variations(variations); |
| 162 |
let font_ref = CTFontCreateWithGraphicsFont( |
| 163 |
cgfont.as_ptr() as *mut _, |
| 164 |
pt_size as CGFloat, |
| 165 |
ptr::null(), |
| 166 |
font_desc.as_concrete_TypeRef(), |
| 167 |
); |
| 168 |
CTFont::wrap_under_create_rule(font_ref) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
pub fn new_from_descriptor(desc: &CTFontDescriptor, pt_size: f64) -> CTFont { |
| 173 |
unsafe { |
| 174 |
let font_ref = CTFontCreateWithFontDescriptor( |
| 175 |
desc.as_concrete_TypeRef(), |
| 176 |
pt_size as CGFloat, |
| 177 |
ptr::null(), |
| 178 |
); |
| 179 |
CTFont::wrap_under_create_rule(font_ref) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
pub fn new_from_descriptor_and_options( |
| 184 |
desc: &CTFontDescriptor, |
| 185 |
pt_size: f64, |
| 186 |
options: CTFontOptions, |
| 187 |
) -> CTFont { |
| 188 |
unsafe { |
| 189 |
let font_ref = CTFontCreateWithFontDescriptorAndOptions( |
| 190 |
desc.as_concrete_TypeRef(), |
| 191 |
pt_size as CGFloat, |
| 192 |
ptr::null(), |
| 193 |
options, |
| 194 |
); |
| 195 |
CTFont::wrap_under_create_rule(font_ref) |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
pub fn new_from_buffer(buffer: &[u8]) -> Result<CTFont, ()> { |
| 200 |
let ct_font_descriptor = create_font_descriptor(buffer)?; |
| 201 |
Ok(new_from_descriptor(&ct_font_descriptor, 16.0)) |
| 202 |
} |
| 203 |
|
| 204 |
pub fn new_from_name(name: &str, pt_size: f64) -> Result<CTFont, ()> { |
| 205 |
unsafe { |
| 206 |
let name: CFString = name.parse().unwrap(); |
| 207 |
let font_ref = |
| 208 |
CTFontCreateWithName(name.as_concrete_TypeRef(), pt_size as CGFloat, ptr::null()); |
| 209 |
if font_ref.is_null() { |
| 210 |
Err(()) |
| 211 |
} else { |
| 212 |
Ok(CTFont::wrap_under_create_rule(font_ref)) |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
pub fn new_from_name_and_options( |
| 218 |
name: &str, |
| 219 |
pt_size: f64, |
| 220 |
options: CTFontOptions, |
| 221 |
) -> Result<CTFont, ()> { |
| 222 |
unsafe { |
| 223 |
let name: CFString = name.parse().unwrap(); |
| 224 |
let font_ref = CTFontCreateWithNameAndOptions( |
| 225 |
name.as_concrete_TypeRef(), |
| 226 |
pt_size as CGFloat, |
| 227 |
ptr::null(), |
| 228 |
options, |
| 229 |
); |
| 230 |
if font_ref.is_null() { |
| 231 |
Err(()) |
| 232 |
} else { |
| 233 |
Ok(CTFont::wrap_under_create_rule(font_ref)) |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
pub fn new_ui_font_for_language( |
| 239 |
ui_type: CTFontUIFontType, |
| 240 |
size: f64, |
| 241 |
language: Option<CFString>, |
| 242 |
) -> CTFont { |
| 243 |
unsafe { |
| 244 |
let font_ref = CTFontCreateUIFontForLanguage( |
| 245 |
ui_type, |
| 246 |
size, |
| 247 |
language |
| 248 |
.as_ref() |
| 249 |
.map(|x| x.as_concrete_TypeRef()) |
| 250 |
.unwrap_or(std::ptr::null()), |
| 251 |
); |
| 252 |
if font_ref.is_null() { |
| 253 |
// CTFontCreateUIFontForLanguage can fail, but is unlikely to do so during |
| 254 |
// normal usage (if you pass a bad ui_type it will). To make things more |
| 255 |
// convenient, just panic if it fails. |
| 256 |
panic!(); |
| 257 |
} else { |
| 258 |
CTFont::wrap_under_create_rule(font_ref) |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
impl CTFont { |
| 264 |
// Properties |
| 265 |
pub fn symbolic_traits(&self) -> CTFontSymbolicTraits { |
| 266 |
unsafe { CTFontGetSymbolicTraits(self.0) } |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
impl CTFont { |
| 271 |
// Creation methods |
| 272 |
pub fn copy_to_CGFont(&self) -> CGFont { |
| 273 |
unsafe { |
| 274 |
let cgfont_ref = CTFontCopyGraphicsFont(self.0, ptr::null_mut()); |
| 275 |
CGFont::from_ptr(cgfont_ref as *mut _) |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
pub fn copy_descriptor(&self) -> CTFontDescriptor { |
| 280 |
unsafe { |
| 281 |
let desc = CTFontCopyFontDescriptor(self.0); |
| 282 |
CTFontDescriptor::wrap_under_create_rule(desc) |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
pub fn clone_with_font_size(&self, size: f64) -> CTFont { |
| 287 |
unsafe { |
| 288 |
let font_ref = |
| 289 |
CTFontCreateCopyWithAttributes(self.0, size as CGFloat, ptr::null(), ptr::null()); |
| 290 |
CTFont::wrap_under_create_rule(font_ref) |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
pub fn clone_with_symbolic_traits( |
| 295 |
&self, |
| 296 |
trait_value: CTFontSymbolicTraits, |
| 297 |
trait_mask: CTFontSymbolicTraits, |
| 298 |
) -> Option<CTFont> { |
| 299 |
unsafe { |
| 300 |
let font_ref = CTFontCreateCopyWithSymbolicTraits( |
| 301 |
self.0, |
| 302 |
0.0, |
| 303 |
ptr::null(), |
| 304 |
trait_value, |
| 305 |
trait_mask, |
| 306 |
); |
| 307 |
if font_ref.is_null() { |
| 308 |
None |
| 309 |
} else { |
| 310 |
Some(CTFont::wrap_under_create_rule(font_ref)) |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
// Names |
| 316 |
pub fn get_string_by_name_key(&self, name_key: CTFontNameSpecifier) -> Option<String> { |
| 317 |
unsafe { |
| 318 |
let result = CTFontCopyName(self.as_concrete_TypeRef(), name_key.into()); |
| 319 |
if result.is_null() { |
| 320 |
None |
| 321 |
} else { |
| 322 |
Some(CFString::wrap_under_create_rule(result).to_string()) |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
pub fn family_name(&self) -> String { |
| 328 |
let value = self.get_string_by_name_key(CTFontNameSpecifier::Family); |
| 329 |
value.expect("Fonts should always have a family name.") |
| 330 |
} |
| 331 |
|
| 332 |
pub fn face_name(&self) -> String { |
| 333 |
let value = self.get_string_by_name_key(CTFontNameSpecifier::SubFamily); |
| 334 |
value.expect("Fonts should always have a face name.") |
| 335 |
} |
| 336 |
|
| 337 |
pub fn unique_name(&self) -> String { |
| 338 |
let value = self.get_string_by_name_key(CTFontNameSpecifier::Unique); |
| 339 |
value.expect("Fonts should always have a unique name.") |
| 340 |
} |
| 341 |
|
| 342 |
pub fn postscript_name(&self) -> String { |
| 343 |
let value = self.get_string_by_name_key(CTFontNameSpecifier::PostScript); |
| 344 |
value.expect("Fonts should always have a PostScript name.") |
| 345 |
} |
| 346 |
|
| 347 |
pub fn display_name(&self) -> String { |
| 348 |
let value = self.get_string_by_name_key(CTFontNameSpecifier::Full); |
| 349 |
value.expect("Fonts should always have a PostScript name.") |
| 350 |
} |
| 351 |
|
| 352 |
pub fn style_name(&self) -> String { |
| 353 |
let value = self.get_string_by_name_key(CTFontNameSpecifier::Style); |
| 354 |
value.expect("Fonts should always have a style name.") |
| 355 |
} |
| 356 |
|
| 357 |
pub fn all_traits(&self) -> CTFontTraits { |
| 358 |
unsafe { CTFontTraits::wrap_under_create_rule(CTFontCopyTraits(self.0)) } |
| 359 |
} |
| 360 |
|
| 361 |
// Font metrics |
| 362 |
pub fn ascent(&self) -> CGFloat { |
| 363 |
unsafe { CTFontGetAscent(self.0) } |
| 364 |
} |
| 365 |
|
| 366 |
pub fn descent(&self) -> CGFloat { |
| 367 |
unsafe { CTFontGetDescent(self.0) } |
| 368 |
} |
| 369 |
|
| 370 |
pub fn underline_thickness(&self) -> CGFloat { |
| 371 |
unsafe { CTFontGetUnderlineThickness(self.0) } |
| 372 |
} |
| 373 |
|
| 374 |
pub fn underline_position(&self) -> CGFloat { |
| 375 |
unsafe { CTFontGetUnderlinePosition(self.0) } |
| 376 |
} |
| 377 |
|
| 378 |
pub fn slant_angle(&self) -> CGFloat { |
| 379 |
unsafe { CTFontGetSlantAngle(self.0) } |
| 380 |
} |
| 381 |
|
| 382 |
pub fn cap_height(&self) -> CGFloat { |
| 383 |
unsafe { CTFontGetCapHeight(self.0) } |
| 384 |
} |
| 385 |
|
| 386 |
pub fn bounding_box(&self) -> CGRect { |
| 387 |
unsafe { CTFontGetBoundingBox(self.0) } |
| 388 |
} |
| 389 |
|
| 390 |
pub fn leading(&self) -> CGFloat { |
| 391 |
unsafe { CTFontGetLeading(self.0) } |
| 392 |
} |
| 393 |
|
| 394 |
pub fn units_per_em(&self) -> libc::c_uint { |
| 395 |
unsafe { CTFontGetUnitsPerEm(self.0) } |
| 396 |
} |
| 397 |
|
| 398 |
pub fn x_height(&self) -> CGFloat { |
| 399 |
unsafe { CTFontGetXHeight(self.0) } |
| 400 |
} |
| 401 |
|
| 402 |
pub fn pt_size(&self) -> CGFloat { |
| 403 |
unsafe { CTFontGetSize(self.0) } |
| 404 |
} |
| 405 |
|
| 406 |
pub fn get_glyph_with_name(&self, glyph_name: &str) -> CGGlyph { |
| 407 |
let glyph_name = CFString::new(glyph_name); |
| 408 |
unsafe { CTFontGetGlyphWithName(self.0, glyph_name.as_concrete_TypeRef()) } |
| 409 |
} |
| 410 |
|
| 411 |
pub unsafe fn get_glyphs_for_characters( |
| 412 |
&self, |
| 413 |
characters: *const UniChar, |
| 414 |
glyphs: *mut CGGlyph, |
| 415 |
count: CFIndex, |
| 416 |
) -> bool { |
| 417 |
CTFontGetGlyphsForCharacters(self.0, characters, glyphs, count) |
| 418 |
} |
| 419 |
|
| 420 |
pub unsafe fn get_advances_for_glyphs( |
| 421 |
&self, |
| 422 |
orientation: CTFontOrientation, |
| 423 |
glyphs: *const CGGlyph, |
| 424 |
advances: *mut CGSize, |
| 425 |
count: CFIndex, |
| 426 |
) -> f64 { |
| 427 |
CTFontGetAdvancesForGlyphs(self.0, orientation, glyphs, advances, count) |
| 428 |
} |
| 429 |
|
| 430 |
pub unsafe fn get_vertical_translations_for_glyphs( |
| 431 |
&self, |
| 432 |
orientation: CTFontOrientation, |
| 433 |
glyphs: *const CGGlyph, |
| 434 |
translations: *mut CGSize, |
| 435 |
count: CFIndex, |
| 436 |
) { |
| 437 |
CTFontGetVerticalTranslationsForGlyphs(self.0, orientation, glyphs, translations, count) |
| 438 |
} |
| 439 |
|
| 440 |
pub fn get_font_table(&self, tag: u32) -> Option<CFData> { |
| 441 |
unsafe { |
| 442 |
let result = CTFontCopyTable( |
| 443 |
self.0, |
| 444 |
tag as CTFontTableTag, |
| 445 |
kCTFontTableOptionsExcludeSynthetic, |
| 446 |
); |
| 447 |
if result.is_null() { |
| 448 |
None |
| 449 |
} else { |
| 450 |
Some(CFData::wrap_under_create_rule(result)) |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
pub fn get_available_font_tables(&self) -> Option<CFArray<CTFontTableTag>> { |
| 456 |
unsafe { |
| 457 |
let result = CTFontCopyAvailableTables(self.0, kCTFontTableOptionsExcludeSynthetic); |
| 458 |
if result.is_null() { |
| 459 |
None |
| 460 |
} else { |
| 461 |
Some(TCFType::wrap_under_create_rule(result)) |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
pub fn get_bounding_rects_for_glyphs( |
| 467 |
&self, |
| 468 |
orientation: CTFontOrientation, |
| 469 |
glyphs: &[CGGlyph], |
| 470 |
) -> CGRect { |
| 471 |
unsafe { |
| 472 |
CTFontGetBoundingRectsForGlyphs( |
| 473 |
self.as_concrete_TypeRef(), |
| 474 |
orientation, |
| 475 |
glyphs.as_ptr(), |
| 476 |
ptr::null_mut(), |
| 477 |
glyphs.len() as CFIndex, |
| 478 |
) |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
pub fn draw_glyphs(&self, glyphs: &[CGGlyph], positions: &[CGPoint], context: CGContext) { |
| 483 |
assert_eq!(glyphs.len(), positions.len()); |
| 484 |
unsafe { |
| 485 |
CTFontDrawGlyphs( |
| 486 |
self.as_concrete_TypeRef(), |
| 487 |
glyphs.as_ptr(), |
| 488 |
positions.as_ptr(), |
| 489 |
glyphs.len() as size_t, |
| 490 |
context.as_ptr(), |
| 491 |
) |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
pub fn url(&self) -> Option<CFURL> { |
| 496 |
unsafe { |
| 497 |
let result = CTFontCopyAttribute(self.0, kCTFontURLAttribute); |
| 498 |
if result.is_null() { |
| 499 |
None |
| 500 |
} else { |
| 501 |
Some(CFURL::wrap_under_create_rule(result as CFURLRef)) |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
pub fn get_variation_axes(&self) -> Option<CFArray<CFDictionary<CFString, CFType>>> { |
| 507 |
unsafe { |
| 508 |
let axes = CTFontCopyVariationAxes(self.0); |
| 509 |
if axes.is_null() { |
| 510 |
return None; |
| 511 |
} |
| 512 |
Some(TCFType::wrap_under_create_rule(axes)) |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
pub fn create_path_for_glyph( |
| 517 |
&self, |
| 518 |
glyph: CGGlyph, |
| 519 |
matrix: &CGAffineTransform, |
| 520 |
) -> Result<CGPath, ()> { |
| 521 |
unsafe { |
| 522 |
let path = CTFontCreatePathForGlyph(self.0, glyph, matrix); |
| 523 |
if path.is_null() { |
| 524 |
Err(()) |
| 525 |
} else { |
| 526 |
Ok(CGPath::from_ptr(path)) |
| 527 |
} |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
#[inline] |
| 532 |
pub fn glyph_count(&self) -> CFIndex { |
| 533 |
unsafe { CTFontGetGlyphCount(self.0) } |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
// Helper methods |
| 538 |
pub fn debug_font_names(font: &CTFont) { |
| 539 |
fn get_key(font: &CTFont, key: CTFontNameSpecifier) -> String { |
| 540 |
font.get_string_by_name_key(key).unwrap() |
| 541 |
} |
| 542 |
|
| 543 |
println!( |
| 544 |
"kCTFontFamilyNameKey: {}", |
| 545 |
get_key(font, CTFontNameSpecifier::Family) |
| 546 |
); |
| 547 |
println!( |
| 548 |
"kCTFontSubFamilyNameKey: {}", |
| 549 |
get_key(font, CTFontNameSpecifier::SubFamily) |
| 550 |
); |
| 551 |
println!( |
| 552 |
"kCTFontStyleNameKey: {}", |
| 553 |
get_key(font, CTFontNameSpecifier::Style) |
| 554 |
); |
| 555 |
println!( |
| 556 |
"kCTFontUniqueNameKey: {}", |
| 557 |
get_key(font, CTFontNameSpecifier::Unique) |
| 558 |
); |
| 559 |
println!( |
| 560 |
"kCTFontFullNameKey: {}", |
| 561 |
get_key(font, CTFontNameSpecifier::Full) |
| 562 |
); |
| 563 |
println!( |
| 564 |
"kCTFontPostScriptNameKey: {}", |
| 565 |
get_key(font, CTFontNameSpecifier::PostScript) |
| 566 |
); |
| 567 |
} |
| 568 |
|
| 569 |
pub fn debug_font_traits(font: &CTFont) { |
| 570 |
let sym = font.symbolic_traits(); |
| 571 |
println!("kCTFontItalicTrait: {}", sym.is_italic()); |
| 572 |
println!("kCTFontBoldTrait: {}", sym.is_bold()); |
| 573 |
println!("kCTFontExpandedTrait: {}", sym.is_expanded()); |
| 574 |
println!("kCTFontCondensedTrait: {}", sym.is_condensed()); |
| 575 |
println!("kCTFontMonoSpaceTrait: {}", sym.is_monospace()); |
| 576 |
|
| 577 |
let traits = font.all_traits(); |
| 578 |
println!("kCTFontWeightTrait: {}", traits.normalized_weight()); |
| 579 |
println!("kCTFontWidthTrait: {}", traits.normalized_width()); |
| 580 |
// println!("kCTFontSlantTrait: {}", traits.normalized_slant()); |
| 581 |
} |
| 582 |
|
| 583 |
#[cfg(feature = "mountainlion")] |
| 584 |
pub fn cascade_list_for_languages( |
| 585 |
font: &CTFont, |
| 586 |
language_pref_list: &CFArray<CFString>, |
| 587 |
) -> CFArray<CTFontDescriptor> { |
| 588 |
unsafe { |
| 589 |
let font_collection_ref = CTFontCopyDefaultCascadeListForLanguages( |
| 590 |
font.as_concrete_TypeRef(), |
| 591 |
language_pref_list.as_concrete_TypeRef(), |
| 592 |
); |
| 593 |
CFArray::wrap_under_create_rule(font_collection_ref) |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
#[link(name = "CoreText", kind = "framework")] |
| 598 |
extern "C" { |
| 599 |
/* |
| 600 |
* CTFont.h |
| 601 |
*/ |
| 602 |
|
| 603 |
/* Name Specifier Constants */ |
| 604 |
static kCTFontCopyrightNameKey: CFStringRef; |
| 605 |
static kCTFontFamilyNameKey: CFStringRef; |
| 606 |
static kCTFontSubFamilyNameKey: CFStringRef; |
| 607 |
static kCTFontStyleNameKey: CFStringRef; |
| 608 |
static kCTFontUniqueNameKey: CFStringRef; |
| 609 |
static kCTFontFullNameKey: CFStringRef; |
| 610 |
static kCTFontVersionNameKey: CFStringRef; |
| 611 |
static kCTFontPostScriptNameKey: CFStringRef; |
| 612 |
static kCTFontTrademarkNameKey: CFStringRef; |
| 613 |
static kCTFontManufacturerNameKey: CFStringRef; |
| 614 |
static kCTFontDesignerNameKey: CFStringRef; |
| 615 |
static kCTFontDescriptionNameKey: CFStringRef; |
| 616 |
static kCTFontVendorURLNameKey: CFStringRef; |
| 617 |
static kCTFontDesignerURLNameKey: CFStringRef; |
| 618 |
static kCTFontLicenseNameKey: CFStringRef; |
| 619 |
static kCTFontLicenseURLNameKey: CFStringRef; |
| 620 |
static kCTFontSampleTextNameKey: CFStringRef; |
| 621 |
static kCTFontPostScriptCIDNameKey: CFStringRef; |
| 622 |
|
| 623 |
#[cfg(test)] |
| 624 |
static kCTFontVariationAxisIdentifierKey: CFStringRef; |
| 625 |
//static kCTFontVariationAxisMinimumValueKey: CFStringRef; |
| 626 |
#[cfg(test)] |
| 627 |
static kCTFontVariationAxisMaximumValueKey: CFStringRef; |
| 628 |
//static kCTFontVariationAxisDefaultValueKey: CFStringRef; |
| 629 |
//static kCTFontVariationAxisNameKey: CFStringRef; |
| 630 |
|
| 631 |
//static kCTFontFeatureTypeIdentifierKey: CFStringRef; |
| 632 |
//static kCTFontFeatureTypeNameKey: CFStringRef; |
| 633 |
//static kCTFontFeatureTypeExclusiveKey: CFStringRef; |
| 634 |
//static kCTFontFeatureTypeSelectorsKey: CFStringRef; |
| 635 |
//static kCTFontFeatureSelectorIdentifierKey: CFStringRef; |
| 636 |
//static kCTFontFeatureSelectorNameKey: CFStringRef; |
| 637 |
//static kCTFontFeatureSelectorDefaultKey: CFStringRef; |
| 638 |
//static kCTFontFeatureSelectorSettingKey: CFStringRef; |
| 639 |
|
| 640 |
static kCTFontURLAttribute: CFStringRef; |
| 641 |
|
| 642 |
// N.B. Unlike most Cocoa bindings, this extern block is organized according |
| 643 |
// to the documentation's Functions By Task listing, because there so many functions. |
| 644 |
|
| 645 |
/* Creating Fonts */ |
| 646 |
fn CTFontCreateWithName( |
| 647 |
name: CFStringRef, |
| 648 |
size: CGFloat, |
| 649 |
matrix: *const CGAffineTransform, |
| 650 |
) -> CTFontRef; |
| 651 |
fn CTFontCreateWithNameAndOptions( |
| 652 |
name: CFStringRef, |
| 653 |
size: CGFloat, |
| 654 |
matrix: *const CGAffineTransform, |
| 655 |
options: CTFontOptions, |
| 656 |
) -> CTFontRef; |
| 657 |
fn CTFontCreateWithFontDescriptor( |
| 658 |
descriptor: CTFontDescriptorRef, |
| 659 |
size: CGFloat, |
| 660 |
matrix: *const CGAffineTransform, |
| 661 |
) -> CTFontRef; |
| 662 |
fn CTFontCreateWithFontDescriptorAndOptions( |
| 663 |
descriptor: CTFontDescriptorRef, |
| 664 |
size: CGFloat, |
| 665 |
matrix: *const CGAffineTransform, |
| 666 |
options: CTFontOptions, |
| 667 |
) -> CTFontRef; |
| 668 |
fn CTFontCreateUIFontForLanguage( |
| 669 |
uiType: CTFontUIFontType, |
| 670 |
size: CGFloat, |
| 671 |
language: CFStringRef, |
| 672 |
) -> CTFontRef; |
| 673 |
fn CTFontCreateCopyWithAttributes( |
| 674 |
font: CTFontRef, |
| 675 |
size: CGFloat, |
| 676 |
matrix: *const CGAffineTransform, |
| 677 |
attributes: CTFontDescriptorRef, |
| 678 |
) -> CTFontRef; |
| 679 |
fn CTFontCreateCopyWithSymbolicTraits( |
| 680 |
font: CTFontRef, |
| 681 |
size: CGFloat, |
| 682 |
matrix: *const CGAffineTransform, |
| 683 |
symTraitValue: CTFontSymbolicTraits, |
| 684 |
symTraitMask: CTFontSymbolicTraits, |
| 685 |
) -> CTFontRef; |
| 686 |
//fn CTFontCreateCopyWithFamily |
| 687 |
//fn CTFontCreateForString |
| 688 |
|
| 689 |
/* Getting Font Data */ |
| 690 |
fn CTFontCopyFontDescriptor(font: CTFontRef) -> CTFontDescriptorRef; |
| 691 |
fn CTFontCopyAttribute(font: CTFontRef, attribute: CFStringRef) -> CFTypeRef; |
| 692 |
fn CTFontGetSize(font: CTFontRef) -> CGFloat; |
| 693 |
//fn CTFontGetMatrix |
| 694 |
fn CTFontGetSymbolicTraits(font: CTFontRef) -> CTFontSymbolicTraits; |
| 695 |
fn CTFontCopyTraits(font: CTFontRef) -> CFDictionaryRef; |
| 696 |
|
| 697 |
/* Getting Font Names */ |
| 698 |
//fn CTFontCopyPostScriptName(font: CTFontRef) -> CFStringRef; |
| 699 |
//fn CTFontCopyFamilyName(font: CTFontRef) -> CFStringRef; |
| 700 |
//fn CTFontCopyFullName(font: CTFontRef) -> CFStringRef; |
| 701 |
//fn CTFontCopyDisplayName(font: CTFontRef) -> CFStringRef; |
| 702 |
fn CTFontCopyName(font: CTFontRef, nameKey: CFStringRef) -> CFStringRef; |
| 703 |
//fn CTFontCopyLocalizedName(font: CTFontRef, nameKey: CFStringRef, |
| 704 |
// language: *CFStringRef) -> CFStringRef; |
| 705 |
#[cfg(feature = "mountainlion")] |
| 706 |
fn CTFontCopyDefaultCascadeListForLanguages( |
| 707 |
font: CTFontRef, |
| 708 |
languagePrefList: CFArrayRef, |
| 709 |
) -> CFArrayRef; |
| 710 |
|
| 711 |
/* Working With Encoding */ |
| 712 |
//fn CTFontCopyCharacterSet |
| 713 |
//fn CTFontGetStringEncoding |
| 714 |
//fn CTFontCopySupportedLanguages |
| 715 |
|
| 716 |
/* Getting Font Metrics */ |
| 717 |
fn CTFontGetAscent(font: CTFontRef) -> CGFloat; |
| 718 |
fn CTFontGetDescent(font: CTFontRef) -> CGFloat; |
| 719 |
fn CTFontGetLeading(font: CTFontRef) -> CGFloat; |
| 720 |
fn CTFontGetUnitsPerEm(font: CTFontRef) -> libc::c_uint; |
| 721 |
fn CTFontGetGlyphCount(font: CTFontRef) -> CFIndex; |
| 722 |
fn CTFontGetBoundingBox(font: CTFontRef) -> CGRect; |
| 723 |
fn CTFontGetUnderlinePosition(font: CTFontRef) -> CGFloat; |
| 724 |
fn CTFontGetUnderlineThickness(font: CTFontRef) -> CGFloat; |
| 725 |
fn CTFontGetSlantAngle(font: CTFontRef) -> CGFloat; |
| 726 |
fn CTFontGetCapHeight(font: CTFontRef) -> CGFloat; |
| 727 |
fn CTFontGetXHeight(font: CTFontRef) -> CGFloat; |
| 728 |
|
| 729 |
/* Getting Glyph Data */ |
| 730 |
fn CTFontCreatePathForGlyph( |
| 731 |
font: CTFontRef, |
| 732 |
glyph: CGGlyph, |
| 733 |
matrix: *const CGAffineTransform, |
| 734 |
) -> CGPathRef; |
| 735 |
fn CTFontGetGlyphWithName(font: CTFontRef, glyphName: CFStringRef) -> CGGlyph; |
| 736 |
fn CTFontGetBoundingRectsForGlyphs( |
| 737 |
font: CTFontRef, |
| 738 |
orientation: CTFontOrientation, |
| 739 |
glyphs: *const CGGlyph, |
| 740 |
boundingRects: *mut CGRect, |
| 741 |
count: CFIndex, |
| 742 |
) -> CGRect; |
| 743 |
fn CTFontGetAdvancesForGlyphs( |
| 744 |
font: CTFontRef, |
| 745 |
orientation: CTFontOrientation, |
| 746 |
glyphs: *const CGGlyph, |
| 747 |
advances: *mut CGSize, |
| 748 |
count: CFIndex, |
| 749 |
) -> libc::c_double; |
| 750 |
fn CTFontGetVerticalTranslationsForGlyphs( |
| 751 |
font: CTFontRef, |
| 752 |
orientation: CTFontOrientation, |
| 753 |
glyphs: *const CGGlyph, |
| 754 |
translations: *mut CGSize, |
| 755 |
count: CFIndex, |
| 756 |
); |
| 757 |
|
| 758 |
/* Working With Font Variations */ |
| 759 |
fn CTFontCopyVariationAxes(font: CTFontRef) -> CFArrayRef; |
| 760 |
//fn CTFontCopyVariation |
| 761 |
|
| 762 |
/* Getting Font Features */ |
| 763 |
//fn CTFontCopyFeatures |
| 764 |
//fn CTFontCopyFeatureSettings |
| 765 |
|
| 766 |
/* Working with Glyphs */ |
| 767 |
fn CTFontGetGlyphsForCharacters( |
| 768 |
font: CTFontRef, |
| 769 |
characters: *const UniChar, |
| 770 |
glyphs: *mut CGGlyph, |
| 771 |
count: CFIndex, |
| 772 |
) -> bool; |
| 773 |
fn CTFontDrawGlyphs( |
| 774 |
font: CTFontRef, |
| 775 |
glyphs: *const CGGlyph, |
| 776 |
positions: *const CGPoint, |
| 777 |
count: size_t, |
| 778 |
context: CGContextRef, |
| 779 |
); |
| 780 |
//fn CTFontGetLigatureCaretPositions |
| 781 |
|
| 782 |
/* Converting Fonts */ |
| 783 |
fn CTFontCopyGraphicsFont(font: CTFontRef, attributes: *mut CTFontDescriptorRef) -> CGFontRef; |
| 784 |
fn CTFontCreateWithGraphicsFont( |
| 785 |
graphicsFont: CGFontRef, |
| 786 |
size: CGFloat, |
| 787 |
matrix: *const CGAffineTransform, |
| 788 |
attributes: CTFontDescriptorRef, |
| 789 |
) -> CTFontRef; |
| 790 |
//fn CTFontGetPlatformFont |
| 791 |
//fn CTFontCreateWithPlatformFont |
| 792 |
//fn CTFontCreateWithQuickdrawInstance |
| 793 |
|
| 794 |
/* Getting Font Table Data */ |
| 795 |
fn CTFontCopyAvailableTables(font: CTFontRef, options: CTFontTableOptions) -> CFArrayRef; |
| 796 |
fn CTFontCopyTable( |
| 797 |
font: CTFontRef, |
| 798 |
table: CTFontTableTag, |
| 799 |
options: CTFontTableOptions, |
| 800 |
) -> CFDataRef; |
| 801 |
|
| 802 |
fn CTFontGetTypeID() -> CFTypeID; |
| 803 |
} |
| 804 |
|
| 805 |
#[test] |
| 806 |
fn copy_font() { |
| 807 |
use std::io::Read; |
| 808 |
let mut f = std::fs::File::open("/System/Library/Fonts/ZapfDingbats.ttf").unwrap(); |
| 809 |
let mut font_data = Vec::new(); |
| 810 |
f.read_to_end(&mut font_data).unwrap(); |
| 811 |
let desc = crate::font_manager::create_font_descriptor(&font_data).unwrap(); |
| 812 |
let font = new_from_descriptor(&desc, 12.); |
| 813 |
drop(desc); |
| 814 |
let desc = font.copy_descriptor(); |
| 815 |
drop(font); |
| 816 |
let font = new_from_descriptor(&desc, 14.); |
| 817 |
assert_eq!(font.family_name(), "Zapf Dingbats"); |
| 818 |
} |
| 819 |
|
| 820 |
#[cfg(test)] |
| 821 |
fn macos_version() -> (i32, i32, i32) { |
| 822 |
use std::io::Read; |
| 823 |
|
| 824 |
// This is the same approach that Firefox uses for detecting versions |
| 825 |
let file = "/System/Library/CoreServices/SystemVersion.plist"; |
| 826 |
let mut f = std::fs::File::open(file).unwrap(); |
| 827 |
let mut system_version_data = Vec::new(); |
| 828 |
f.read_to_end(&mut system_version_data).unwrap(); |
| 829 |
|
| 830 |
use core_foundation::propertylist; |
| 831 |
let (list, _) = propertylist::create_with_data( |
| 832 |
core_foundation::data::CFData::from_buffer(&system_version_data), |
| 833 |
propertylist::kCFPropertyListImmutable, |
| 834 |
) |
| 835 |
.unwrap(); |
| 836 |
let k = unsafe { propertylist::CFPropertyList::wrap_under_create_rule(list) }; |
| 837 |
|
| 838 |
let dict = unsafe { |
| 839 |
std::mem::transmute::<_, CFDictionary<CFType, CFType>>( |
| 840 |
k.downcast::<CFDictionary>().unwrap(), |
| 841 |
) |
| 842 |
}; |
| 843 |
|
| 844 |
let version = dict |
| 845 |
.find(&CFString::new("ProductVersion").as_CFType()) |
| 846 |
.as_ref() |
| 847 |
.unwrap() |
| 848 |
.downcast::<CFString>() |
| 849 |
.unwrap() |
| 850 |
.to_string(); |
| 851 |
|
| 852 |
match version |
| 853 |
.split('.') |
| 854 |
.map(|x| x.parse().unwrap()) |
| 855 |
.collect::<Vec<_>>()[..] |
| 856 |
{ |
| 857 |
[a, b, c] => (a, b, c), |
| 858 |
[a, b] => (a, b, 0), |
| 859 |
_ => panic!(), |
| 860 |
} |
| 861 |
} |
| 862 |
|
| 863 |
#[test] |
| 864 |
fn copy_system_font() { |
| 865 |
use crate::*; |
| 866 |
|
| 867 |
let small = new_ui_font_for_language(kCTFontSystemDetailFontType, 19., None); |
| 868 |
let big = small.clone_with_font_size(20.); |
| 869 |
|
| 870 |
// ensure that we end up with different fonts for the different sizes before 10.15 |
| 871 |
if macos_version() < (10, 15, 0) { |
| 872 |
assert_ne!(big.url(), small.url()); |
| 873 |
} else { |
| 874 |
assert_eq!(big.url(), small.url()); |
| 875 |
} |
| 876 |
|
| 877 |
let ps = small.postscript_name(); |
| 878 |
let desc = small.copy_descriptor(); |
| 879 |
|
| 880 |
// check that we can construct a new vesion by descriptor |
| 881 |
let ctfont = new_from_descriptor(&desc, 20.); |
| 882 |
assert_eq!(big.postscript_name(), ctfont.postscript_name()); |
| 883 |
|
| 884 |
// check that we can construct a new version by attributes |
| 885 |
let attr = desc.attributes(); |
| 886 |
let desc_from_attr = font_descriptor::new_from_attributes(&attr); |
| 887 |
let font_from_attr = new_from_descriptor(&desc_from_attr, 19.); |
| 888 |
assert_eq!(font_from_attr.postscript_name(), small.postscript_name()); |
| 889 |
|
| 890 |
// on newer versions of macos we can't construct by name anymore |
| 891 |
if macos_version() < (10, 13, 0) { |
| 892 |
let ui_font_by_name = new_from_name(&small.postscript_name(), 19.).unwrap(); |
| 893 |
assert_eq!(ui_font_by_name.postscript_name(), small.postscript_name()); |
| 894 |
|
| 895 |
let ui_font_by_name = new_from_name(&small.postscript_name(), 20.).unwrap(); |
| 896 |
assert_eq!(ui_font_by_name.postscript_name(), small.postscript_name()); |
| 897 |
|
| 898 |
let ui_font_by_name = new_from_name(&big.postscript_name(), 20.).unwrap(); |
| 899 |
assert_eq!(ui_font_by_name.postscript_name(), big.postscript_name()); |
| 900 |
} |
| 901 |
|
| 902 |
// but we can still construct the CGFont by name |
| 903 |
let cgfont = CGFont::from_name(&CFString::new(&ps)).unwrap(); |
| 904 |
let cgfont = new_from_CGFont(&cgfont, 0.); |
| 905 |
println!("{:?}", cgfont); |
| 906 |
let desc = cgfont.copy_descriptor(); |
| 907 |
let matching = unsafe { |
| 908 |
crate::font_descriptor::CTFontDescriptorCreateMatchingFontDescriptor( |
| 909 |
desc.as_concrete_TypeRef(), |
| 910 |
std::ptr::null(), |
| 911 |
) |
| 912 |
}; |
| 913 |
let matching = unsafe { CTFontDescriptor::wrap_under_create_rule(matching) }; |
| 914 |
|
| 915 |
println!("{:?}", cgfont.copy_descriptor()); |
| 916 |
assert!(desc |
| 917 |
.attributes() |
| 918 |
.find(CFString::from_static_string("NSFontSizeAttribute")) |
| 919 |
.is_some()); |
| 920 |
|
| 921 |
println!("{:?}", matching); |
| 922 |
println!( |
| 923 |
"{:?}", |
| 924 |
matching |
| 925 |
.attributes() |
| 926 |
.find(CFString::from_static_string("NSFontSizeAttribute")) |
| 927 |
); |
| 928 |
|
| 929 |
assert!(matching |
| 930 |
.attributes() |
| 931 |
.find(CFString::from_static_string("NSFontSizeAttribute")) |
| 932 |
.is_none()); |
| 933 |
|
| 934 |
assert_eq!(small.postscript_name(), cgfont.postscript_name()); |
| 935 |
} |
| 936 |
|
| 937 |
// Tests what happens when variations have values not inbetween min and max |
| 938 |
#[test] |
| 939 |
fn out_of_range_variations() { |
| 940 |
use crate::*; |
| 941 |
|
| 942 |
let small = new_ui_font_for_language(kCTFontSystemDetailFontType, 19., None); |
| 943 |
|
| 944 |
let axes = small.get_variation_axes(); |
| 945 |
if macos_version() < (10, 12, 0) { |
| 946 |
assert!(axes.is_none()); |
| 947 |
return; |
| 948 |
} |
| 949 |
let axes = axes.unwrap(); |
| 950 |
let mut vals = Vec::new(); |
| 951 |
dbg!(&axes); |
| 952 |
for axis in axes.iter() { |
| 953 |
let tag = axis |
| 954 |
.find(unsafe { kCTFontVariationAxisIdentifierKey }) |
| 955 |
.unwrap() |
| 956 |
.downcast::<CFNumber>() |
| 957 |
.unwrap() |
| 958 |
.to_i64() |
| 959 |
.unwrap(); |
| 960 |
let max = axis |
| 961 |
.find(unsafe { kCTFontVariationAxisMaximumValueKey }) |
| 962 |
.unwrap() |
| 963 |
.downcast::<CFNumber>() |
| 964 |
.unwrap() |
| 965 |
.to_f64() |
| 966 |
.unwrap(); |
| 967 |
vals.push((CFNumber::from(tag), CFNumber::from(max + 1.))); |
| 968 |
} |
| 969 |
let vals_dict = CFDictionary::from_CFType_pairs(&vals); |
| 970 |
let variation_attribute = |
| 971 |
unsafe { CFString::wrap_under_get_rule(font_descriptor::kCTFontVariationAttribute) }; |
| 972 |
let attrs_dict = CFDictionary::from_CFType_pairs(&[(variation_attribute.clone(), vals_dict)]); |
| 973 |
let ct_var_font_desc = small |
| 974 |
.copy_descriptor() |
| 975 |
.create_copy_with_attributes(attrs_dict.to_untyped()) |
| 976 |
.unwrap(); |
| 977 |
let variation_font = crate::font::new_from_descriptor(&ct_var_font_desc, 19.); |
| 978 |
let var_desc = variation_font.copy_descriptor(); |
| 979 |
let var_attrs = var_desc.attributes(); |
| 980 |
dbg!(&var_attrs); |
| 981 |
// attributes greater than max are dropped on macOS <= 11 |
| 982 |
// on macOS 12 they seem to be preserved as is. |
| 983 |
let var_attrs = var_attrs.find(variation_attribute); |
| 984 |
if macos_version() >= (12, 0, 0) && macos_version() < (13, 0, 0) { |
| 985 |
let var_attrs = var_attrs.unwrap().downcast::<CFDictionary>().unwrap(); |
| 986 |
assert!(!var_attrs.is_empty()); |
| 987 |
let var_attrs: CFDictionary<CFType, CFType> = unsafe { std::mem::transmute(var_attrs) }; |
| 988 |
// attributes greater than max remain |
| 989 |
for axis in axes.iter() { |
| 990 |
let tag = axis |
| 991 |
.find(unsafe { kCTFontVariationAxisIdentifierKey }) |
| 992 |
.unwrap(); |
| 993 |
let max = axis |
| 994 |
.find(unsafe { kCTFontVariationAxisMaximumValueKey }) |
| 995 |
.unwrap() |
| 996 |
.downcast::<CFNumber>() |
| 997 |
.unwrap() |
| 998 |
.to_f64() |
| 999 |
.unwrap(); |
| 1000 |
let val = var_attrs |
| 1001 |
.find(tag.clone()) |
| 1002 |
.unwrap() |
| 1003 |
.downcast::<CFNumber>() |
| 1004 |
.unwrap() |
| 1005 |
.to_f64() |
| 1006 |
.unwrap(); |
| 1007 |
assert_eq!(val, max + 1.); |
| 1008 |
} |
| 1009 |
} else if macos_version() >= (10, 15, 0) { |
| 1010 |
assert!(var_attrs.is_none()); |
| 1011 |
} else { |
| 1012 |
let var_attrs = var_attrs.unwrap().downcast::<CFDictionary>().unwrap(); |
| 1013 |
assert!(var_attrs.is_empty()); |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
#[test] |
| 1018 |
fn equal_descriptor_different_font() { |
| 1019 |
use crate::*; |
| 1020 |
|
| 1021 |
let variation_attribute = |
| 1022 |
unsafe { CFString::wrap_under_get_rule(font_descriptor::kCTFontVariationAttribute) }; |
| 1023 |
let size_attribute = |
| 1024 |
unsafe { CFString::wrap_under_get_rule(font_descriptor::kCTFontSizeAttribute) }; |
| 1025 |
|
| 1026 |
let sys_font = new_ui_font_for_language(kCTFontSystemDetailFontType, 19., None); |
| 1027 |
|
| 1028 |
// but we can still construct the CGFont by name |
| 1029 |
let create_vars = |desc| { |
| 1030 |
let vals: Vec<(CFNumber, CFNumber)> = |
| 1031 |
vec![(CFNumber::from(0x6f70737a), CFNumber::from(17.))]; |
| 1032 |
let vals_dict = CFDictionary::from_CFType_pairs(&vals); |
| 1033 |
let attrs_dict = |
| 1034 |
CFDictionary::from_CFType_pairs(&[(variation_attribute.clone(), vals_dict)]); |
| 1035 |
let size_attrs_dict = |
| 1036 |
CFDictionary::from_CFType_pairs(&[(size_attribute.clone(), CFNumber::from(120.))]); |
| 1037 |
dbg!(&desc); |
| 1038 |
let from_font_desc = new_from_descriptor(&desc, 120.).copy_descriptor(); |
| 1039 |
let resized_font_desc = desc |
| 1040 |
.create_copy_with_attributes(size_attrs_dict.to_untyped()) |
| 1041 |
.unwrap(); |
| 1042 |
if macos_version() >= (11, 0, 0) { |
| 1043 |
assert_eq!(from_font_desc, resized_font_desc); |
| 1044 |
} else { |
| 1045 |
// we won't have a name if we're using system font desc |
| 1046 |
if from_font_desc |
| 1047 |
.attributes() |
| 1048 |
.find(unsafe { font_descriptor::kCTFontNameAttribute }) |
| 1049 |
.is_none() |
| 1050 |
{ |
| 1051 |
// it's surprising that desc's are the not equal but the attributes are |
| 1052 |
assert_ne!(from_font_desc, resized_font_desc); |
| 1053 |
assert_eq!( |
| 1054 |
from_font_desc.attributes().to_untyped(), |
| 1055 |
resized_font_desc.attributes().to_untyped() |
| 1056 |
); |
| 1057 |
} else if macos_version() >= (10, 13, 0) { |
| 1058 |
// this is unsurprising |
| 1059 |
assert_ne!(from_font_desc, resized_font_desc); |
| 1060 |
assert_ne!( |
| 1061 |
from_font_desc.attributes().to_untyped(), |
| 1062 |
resized_font_desc.attributes().to_untyped() |
| 1063 |
); |
| 1064 |
} else { |
| 1065 |
assert_ne!(from_font_desc, resized_font_desc); |
| 1066 |
assert_eq!( |
| 1067 |
from_font_desc.attributes().to_untyped(), |
| 1068 |
resized_font_desc.attributes().to_untyped() |
| 1069 |
); |
| 1070 |
} |
| 1071 |
} |
| 1072 |
|
| 1073 |
let from_font_desc = from_font_desc |
| 1074 |
.create_copy_with_attributes(attrs_dict.to_untyped()) |
| 1075 |
.unwrap(); |
| 1076 |
let resized_font_desc = resized_font_desc |
| 1077 |
.create_copy_with_attributes(attrs_dict.to_untyped()) |
| 1078 |
.unwrap(); |
| 1079 |
(from_font_desc, resized_font_desc) |
| 1080 |
}; |
| 1081 |
|
| 1082 |
// setting the variation works properly if we use system font desc |
| 1083 |
let (from_font_desc, resized_font_desc) = create_vars(sys_font.copy_descriptor()); |
| 1084 |
assert_eq!(from_font_desc, resized_font_desc); |
| 1085 |
assert!(resized_font_desc |
| 1086 |
.attributes() |
| 1087 |
.find(variation_attribute.clone()) |
| 1088 |
.is_some()); |
| 1089 |
|
| 1090 |
// but doesn't if we refer to it by name |
| 1091 |
let ps = sys_font.postscript_name(); |
| 1092 |
let cgfont = CGFont::from_name(&CFString::new(&ps)).unwrap(); |
| 1093 |
let ctfont = new_from_CGFont(&cgfont, 0.); |
| 1094 |
|
| 1095 |
let (from_font_desc, resized_font_desc) = create_vars(ctfont.copy_descriptor()); |
| 1096 |
if macos_version() >= (10, 15, 0) { |
| 1097 |
assert_ne!(from_font_desc, resized_font_desc); |
| 1098 |
} |
| 1099 |
|
| 1100 |
if macos_version() >= (10, 13, 0) { |
| 1101 |
assert!(from_font_desc |
| 1102 |
.attributes() |
| 1103 |
.find(variation_attribute.clone()) |
| 1104 |
.is_some()); |
| 1105 |
if macos_version() >= (11, 0, 0) { |
| 1106 |
assert!(resized_font_desc |
| 1107 |
.attributes() |
| 1108 |
.find(variation_attribute) |
| 1109 |
.is_none()); |
| 1110 |
} else { |
| 1111 |
assert!(resized_font_desc |
| 1112 |
.attributes() |
| 1113 |
.find(variation_attribute) |
| 1114 |
.is_some()); |
| 1115 |
}; |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
#[test] |
| 1120 |
fn system_font_variation() { |
| 1121 |
use crate::*; |
| 1122 |
|
| 1123 |
let small = new_ui_font_for_language(kCTFontSystemDetailFontType, 19., None); |
| 1124 |
|
| 1125 |
// but we can still construct the CGFont by name |
| 1126 |
let ps = small.postscript_name(); |
| 1127 |
let cgfont = CGFont::from_name(&CFString::new(&ps)).unwrap(); |
| 1128 |
let cgfont = new_from_CGFont(&cgfont, 0.); |
| 1129 |
let desc = cgfont.copy_descriptor(); |
| 1130 |
|
| 1131 |
let vals: Vec<(CFNumber, CFNumber)> = |
| 1132 |
vec![(CFNumber::from(0x6f70737a /* opsz */), CFNumber::from(17.))]; |
| 1133 |
let vals_dict = CFDictionary::from_CFType_pairs(&vals); |
| 1134 |
let variation_attribute = |
| 1135 |
unsafe { CFString::wrap_under_get_rule(font_descriptor::kCTFontVariationAttribute) }; |
| 1136 |
let attrs_dict = CFDictionary::from_CFType_pairs(&[(variation_attribute, vals_dict)]); |
| 1137 |
let ct_var_font_desc = desc |
| 1138 |
.create_copy_with_attributes(attrs_dict.to_untyped()) |
| 1139 |
.unwrap(); |
| 1140 |
let attrs = ct_var_font_desc.attributes(); |
| 1141 |
let var_attr = attrs.find(CFString::from_static_string("NSCTFontVariationAttribute")); |
| 1142 |
if macos_version() >= (11, 0, 0) { |
| 1143 |
// the variation goes away |
| 1144 |
assert!(var_attr.is_none()); |
| 1145 |
} else { |
| 1146 |
assert!(var_attr.is_some()); |
| 1147 |
} |
| 1148 |
|
| 1149 |
dbg!(ct_var_font_desc); |
| 1150 |
} |
| 1151 |
|
| 1152 |
#[test] |
| 1153 |
fn ui_font() { |
| 1154 |
// pass some garbagey inputs |
| 1155 |
new_ui_font_for_language( |
| 1156 |
kCTFontSystemDetailFontType, |
| 1157 |
10000009., |
| 1158 |
Some(CFString::from("Gofld")), |
| 1159 |
); |
| 1160 |
} |