Skip to content

Commit 570ef9c

Browse files
authored
Merge pull request #2 from warpdotdev/aloke/testing_ct_run
Expose the typographic bounds as a function within the CoreText Run
2 parents e3ba932 + ee04992 commit 570ef9c

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Cargo.lock
22
target/
3+
.idea
34

core-text/src/run.rs

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10-
use std::borrow::Cow;
11-
use std::os::raw::c_void;
12-
use std::slice;
13-
use core_foundation::base::{CFIndex, CFTypeID, TCFType, CFType, CFRange};
10+
use core_foundation::base::{CFIndex, CFRange, CFType, CFTypeID, TCFType};
1411
use core_foundation::dictionary::{CFDictionary, CFDictionaryRef};
1512
use core_foundation::string::CFString;
13+
use core_graphics::base::CGFloat;
1614
use core_graphics::font::CGGlyph;
1715
use core_graphics::geometry::CGPoint;
16+
use std::borrow::Cow;
17+
use std::os::raw::c_void;
18+
use std::slice;
19+
20+
use crate::line::TypographicBounds;
1821

1922
#[repr(C)]
2023
pub struct __CTRun(c_void);
@@ -38,9 +41,7 @@ impl CTRun {
3841
}
3942
}
4043
pub fn glyph_count(&self) -> CFIndex {
41-
unsafe {
42-
CTRunGetGlyphCount(self.0)
43-
}
44+
unsafe { CTRunGetGlyphCount(self.0) }
4445
}
4546

4647
pub fn glyphs(&self) -> Cow<[CGGlyph]> {
@@ -83,6 +84,34 @@ impl CTRun {
8384
}
8485
}
8586

87+
pub fn get_typographic_bounds(&self) -> TypographicBounds {
88+
let mut ascent = 0.0;
89+
let mut descent = 0.0;
90+
let mut leading = 0.0;
91+
unsafe {
92+
// The portion of the run to calculate the typographic bounds for. By setting this to 0,
93+
// CoreText will measure the bounds from start to end, see https://developer.apple.com/documentation/coretext/1510569-ctrungettypographicbounds?language=objc.
94+
let range = CFRange {
95+
location: 0,
96+
length: 0,
97+
};
98+
99+
let width = CTRunGetTypographicBounds(
100+
self.as_concrete_TypeRef(),
101+
range,
102+
&mut ascent,
103+
&mut descent,
104+
&mut leading,
105+
);
106+
TypographicBounds {
107+
width,
108+
ascent,
109+
descent,
110+
leading,
111+
}
112+
}
113+
}
114+
86115
pub fn string_indices(&self) -> Cow<[CFIndex]> {
87116
unsafe {
88117
// CTRunGetStringIndicesPtr can return null under some not understood circumstances.
@@ -107,21 +136,30 @@ impl CTRun {
107136
#[test]
108137
fn create_runs() {
109138
use core_foundation::attributed_string::CFMutableAttributedString;
110-
use string_attributes::*;
111-
use line::*;
112139
use font;
140+
use line::*;
141+
use string_attributes::*;
113142
let mut string = CFMutableAttributedString::new();
114143
string.replace_str(&CFString::new("Food"), CFRange::init(0, 0));
115144
let len = string.char_len();
116145
unsafe {
117-
string.set_attribute(CFRange::init(0, len), kCTFontAttributeName, &font::new_from_name("Helvetica", 16.).unwrap());
146+
string.set_attribute(
147+
CFRange::init(0, len),
148+
kCTFontAttributeName,
149+
&font::new_from_name("Helvetica", 16.).unwrap(),
150+
);
118151
}
119152
let line = CTLine::new_with_attributed_string(string.as_concrete_TypeRef());
120153
let runs = line.glyph_runs();
121154
assert_eq!(runs.len(), 1);
122155
for run in runs.iter() {
123156
assert_eq!(run.glyph_count(), 4);
124-
let font = run.attributes().unwrap().get(CFString::new("NSFont")).downcast::<font::CTFont>().unwrap();
157+
let font = run
158+
.attributes()
159+
.unwrap()
160+
.get(CFString::new("NSFont"))
161+
.downcast::<font::CTFont>()
162+
.unwrap();
125163
assert_eq!(font.pt_size(), 16.);
126164

127165
let positions = run.positions();
@@ -139,7 +177,7 @@ fn create_runs() {
139177
}
140178

141179
#[link(name = "CoreText", kind = "framework")]
142-
extern {
180+
extern "C" {
143181
fn CTRunGetTypeID() -> CFTypeID;
144182
fn CTRunGetAttributes(run: CTRunRef) -> CFDictionaryRef;
145183
fn CTRunGetGlyphCount(run: CTRunRef) -> CFIndex;
@@ -149,4 +187,12 @@ extern {
149187
fn CTRunGetStringIndices(run: CTRunRef, range: CFRange, buffer: *const CFIndex);
150188
fn CTRunGetGlyphsPtr(run: CTRunRef) -> *const CGGlyph;
151189
fn CTRunGetGlyphs(run: CTRunRef, range: CFRange, buffer: *const CGGlyph);
190+
191+
fn CTRunGetTypographicBounds(
192+
line: CTRunRef,
193+
range: CFRange,
194+
ascent: *mut CGFloat,
195+
descent: *mut CGFloat,
196+
leading: *mut CGFloat,
197+
) -> CGFloat;
152198
}

0 commit comments

Comments
 (0)