| 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::{CFIndex, CFRange, CFType, CFTypeID, TCFType}; |
| 11 |
use core_foundation::dictionary::{CFDictionary, CFDictionaryRef}; |
| 12 |
use core_foundation::string::CFString; |
| 13 |
use core_graphics::font::CGGlyph; |
| 14 |
use core_graphics::geometry::CGPoint; |
| 15 |
use std::borrow::Cow; |
| 16 |
use std::os::raw::c_void; |
| 17 |
use std::slice; |
| 18 |
|
| 19 |
#[repr(C)] |
| 20 |
pub struct __CTRun(c_void); |
| 21 |
|
| 22 |
pub type CTRunRef = *const __CTRun; |
| 23 |
|
| 24 |
declare_TCFType! { |
| 25 |
CTRun, CTRunRef |
| 26 |
} |
| 27 |
impl_TCFType!(CTRun, CTRunRef, CTRunGetTypeID); |
| 28 |
impl_CFTypeDescription!(CTRun); |
| 29 |
|
| 30 |
impl CTRun { |
| 31 |
pub fn attributes(&self) -> Option<CFDictionary<CFString, CFType>> { |
| 32 |
unsafe { |
| 33 |
let attrs = CTRunGetAttributes(self.0); |
| 34 |
if attrs.is_null() { |
| 35 |
return None; |
| 36 |
} |
| 37 |
Some(TCFType::wrap_under_get_rule(attrs)) |
| 38 |
} |
| 39 |
} |
| 40 |
pub fn glyph_count(&self) -> CFIndex { |
| 41 |
unsafe { CTRunGetGlyphCount(self.0) } |
| 42 |
} |
| 43 |
|
| 44 |
pub fn glyphs(&self) -> Cow<[CGGlyph]> { |
| 45 |
unsafe { |
| 46 |
// CTRunGetGlyphsPtr can return null under some not understood circumstances. |
| 47 |
// If it does the Apple documentation tells us to allocate our own buffer and call |
| 48 |
// CTRunGetGlyphs |
| 49 |
let count = CTRunGetGlyphCount(self.0); |
| 50 |
let glyphs_ptr = CTRunGetGlyphsPtr(self.0); |
| 51 |
if !glyphs_ptr.is_null() { |
| 52 |
Cow::from(slice::from_raw_parts(glyphs_ptr, count as usize)) |
| 53 |
} else { |
| 54 |
let mut vec = Vec::with_capacity(count as usize); |
| 55 |
// "If the length of the range is set to 0, then the copy operation will continue |
| 56 |
// from the start index of the range to the end of the run" |
| 57 |
CTRunGetGlyphs(self.0, CFRange::init(0, 0), vec.as_mut_ptr()); |
| 58 |
vec.set_len(count as usize); |
| 59 |
Cow::from(vec) |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
pub fn positions(&self) -> Cow<[CGPoint]> { |
| 65 |
unsafe { |
| 66 |
// CTRunGetPositionsPtr can return null under some not understood circumstances. |
| 67 |
// If it does the Apple documentation tells us to allocate our own buffer and call |
| 68 |
// CTRunGetPositions |
| 69 |
let count = CTRunGetGlyphCount(self.0); |
| 70 |
let positions_ptr = CTRunGetPositionsPtr(self.0); |
| 71 |
if !positions_ptr.is_null() { |
| 72 |
Cow::from(slice::from_raw_parts(positions_ptr, count as usize)) |
| 73 |
} else { |
| 74 |
let mut vec = Vec::with_capacity(count as usize); |
| 75 |
// "If the length of the range is set to 0, then the copy operation will continue |
| 76 |
// from the start index of the range to the end of the run" |
| 77 |
CTRunGetPositions(self.0, CFRange::init(0, 0), vec.as_mut_ptr()); |
| 78 |
vec.set_len(count as usize); |
| 79 |
Cow::from(vec) |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
pub fn string_indices(&self) -> Cow<[CFIndex]> { |
| 85 |
unsafe { |
| 86 |
// CTRunGetStringIndicesPtr can return null under some not understood circumstances. |
| 87 |
// If it does the Apple documentation tells us to allocate our own buffer and call |
| 88 |
// CTRunGetStringIndices |
| 89 |
let count = CTRunGetGlyphCount(self.0); |
| 90 |
let indices_ptr = CTRunGetStringIndicesPtr(self.0); |
| 91 |
if !indices_ptr.is_null() { |
| 92 |
Cow::from(slice::from_raw_parts(indices_ptr, count as usize)) |
| 93 |
} else { |
| 94 |
let mut vec = Vec::with_capacity(count as usize); |
| 95 |
// "If the length of the range is set to 0, then the copy operation will continue |
| 96 |
// from the start index of the range to the end of the run" |
| 97 |
CTRunGetStringIndices(self.0, CFRange::init(0, 0), vec.as_mut_ptr()); |
| 98 |
vec.set_len(count as usize); |
| 99 |
Cow::from(vec) |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
#[test] |
| 106 |
fn create_runs() { |
| 107 |
use core_foundation::attributed_string::CFMutableAttributedString; |
| 108 |
use font; |
| 109 |
use line::*; |
| 110 |
use string_attributes::*; |
| 111 |
let mut string = CFMutableAttributedString::new(); |
| 112 |
string.replace_str(&CFString::new("Food"), CFRange::init(0, 0)); |
| 113 |
let len = string.char_len(); |
| 114 |
unsafe { |
| 115 |
string.set_attribute( |
| 116 |
CFRange::init(0, len), |
| 117 |
kCTFontAttributeName, |
| 118 |
&font::new_from_name("Helvetica", 16.).unwrap(), |
| 119 |
); |
| 120 |
} |
| 121 |
let line = CTLine::new_with_attributed_string(string.as_concrete_TypeRef()); |
| 122 |
let runs = line.glyph_runs(); |
| 123 |
assert_eq!(runs.len(), 1); |
| 124 |
for run in runs.iter() { |
| 125 |
assert_eq!(run.glyph_count(), 4); |
| 126 |
let font = run |
| 127 |
.attributes() |
| 128 |
.unwrap() |
| 129 |
.get(CFString::new("NSFont")) |
| 130 |
.downcast::<font::CTFont>() |
| 131 |
.unwrap(); |
| 132 |
assert_eq!(font.pt_size(), 16.); |
| 133 |
|
| 134 |
let positions = run.positions(); |
| 135 |
assert_eq!(positions.len(), 4); |
| 136 |
assert!(positions[0].x < positions[1].x); |
| 137 |
|
| 138 |
let glyphs = run.glyphs(); |
| 139 |
assert_eq!(glyphs.len(), 4); |
| 140 |
assert_ne!(glyphs[0], glyphs[1]); |
| 141 |
assert_eq!(glyphs[1], glyphs[2]); |
| 142 |
|
| 143 |
let indices = run.string_indices(); |
| 144 |
assert_eq!(indices.as_ref(), &[0, 1, 2, 3]); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
#[link(name = "CoreText", kind = "framework")] |
| 149 |
extern "C" { |
| 150 |
fn CTRunGetTypeID() -> CFTypeID; |
| 151 |
fn CTRunGetAttributes(run: CTRunRef) -> CFDictionaryRef; |
| 152 |
fn CTRunGetGlyphCount(run: CTRunRef) -> CFIndex; |
| 153 |
fn CTRunGetPositionsPtr(run: CTRunRef) -> *const CGPoint; |
| 154 |
fn CTRunGetPositions(run: CTRunRef, range: CFRange, buffer: *const CGPoint); |
| 155 |
fn CTRunGetStringIndicesPtr(run: CTRunRef) -> *const CFIndex; |
| 156 |
fn CTRunGetStringIndices(run: CTRunRef, range: CFRange, buffer: *const CFIndex); |
| 157 |
fn CTRunGetGlyphsPtr(run: CTRunRef) -> *const CGGlyph; |
| 158 |
fn CTRunGetGlyphs(run: CTRunRef, range: CFRange, buffer: *const CGGlyph); |
| 159 |
} |