Skip to content

Commit 9c4dc9b

Browse files
committed
core-text: Implement CTRunGetTypographicBounds.
1 parent dec5ede commit 9c4dc9b

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

core-text/src/run.rs

+37
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use std::borrow::Cow;
1616
use std::os::raw::c_void;
1717
use std::slice;
1818

19+
use crate::line::TypographicBounds;
20+
1921
#[repr(C)]
2022
pub struct __CTRun(c_void);
2123

@@ -81,6 +83,34 @@ impl CTRun {
8183
}
8284
}
8385

86+
pub fn get_typographic_bounds(&self) -> TypographicBounds {
87+
let mut ascent = 0.0;
88+
let mut descent = 0.0;
89+
let mut leading = 0.0;
90+
unsafe {
91+
// The portion of the run to calculate the typographic bounds for. By setting this to 0,
92+
// CoreText will measure the bounds from start to end, see https://developer.apple.com/documentation/coretext/1510569-ctrungettypographicbounds?language=objc.
93+
let range = CFRange {
94+
location: 0,
95+
length: 0,
96+
};
97+
98+
let width = CTRunGetTypographicBounds(
99+
self.as_concrete_TypeRef(),
100+
range,
101+
&mut ascent,
102+
&mut descent,
103+
&mut leading,
104+
);
105+
TypographicBounds {
106+
width,
107+
ascent,
108+
descent,
109+
leading,
110+
}
111+
}
112+
}
113+
84114
pub fn string_indices(&self) -> Cow<[CFIndex]> {
85115
unsafe {
86116
// CTRunGetStringIndicesPtr can return null under some not understood circumstances.
@@ -156,4 +186,11 @@ extern "C" {
156186
fn CTRunGetStringIndices(run: CTRunRef, range: CFRange, buffer: *const CFIndex);
157187
fn CTRunGetGlyphsPtr(run: CTRunRef) -> *const CGGlyph;
158188
fn CTRunGetGlyphs(run: CTRunRef, range: CFRange, buffer: *const CGGlyph);
189+
fn CTRunGetTypographicBounds(
190+
line: CTRunRef,
191+
range: CFRange,
192+
ascent: *mut CGFloat,
193+
descent: *mut CGFloat,
194+
leading: *mut CGFloat,
195+
) -> CGFloat;
159196
}

0 commit comments

Comments
 (0)