Skip to content

Commit 3236802

Browse files
authoredMay 23, 2024··
core-text: Implement CTRunGetTypographicBounds. (#675)
1 parent fb8fd65 commit 3236802

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
 

‎core-text/src/run.rs

+38
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
use core_foundation::base::{CFIndex, CFRange, CFType, CFTypeID, TCFType};
1111
use core_foundation::dictionary::{CFDictionary, CFDictionaryRef};
1212
use core_foundation::string::CFString;
13+
use core_graphics::base::CGFloat;
1314
use core_graphics::font::CGGlyph;
1415
use core_graphics::geometry::CGPoint;
1516
use std::borrow::Cow;
1617
use std::os::raw::c_void;
1718
use std::slice;
1819

20+
use crate::line::TypographicBounds;
21+
1922
#[repr(C)]
2023
pub struct __CTRun(c_void);
2124

@@ -81,6 +84,34 @@ impl CTRun {
8184
}
8285
}
8386

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+
84115
pub fn string_indices(&self) -> Cow<[CFIndex]> {
85116
unsafe {
86117
// CTRunGetStringIndicesPtr can return null under some not understood circumstances.
@@ -156,4 +187,11 @@ extern "C" {
156187
fn CTRunGetStringIndices(run: CTRunRef, range: CFRange, buffer: *const CFIndex);
157188
fn CTRunGetGlyphsPtr(run: CTRunRef) -> *const CGGlyph;
158189
fn CTRunGetGlyphs(run: CTRunRef, range: CFRange, buffer: *const CGGlyph);
190+
fn CTRunGetTypographicBounds(
191+
line: CTRunRef,
192+
range: CFRange,
193+
ascent: *mut CGFloat,
194+
descent: *mut CGFloat,
195+
leading: *mut CGFloat,
196+
) -> CGFloat;
159197
}

0 commit comments

Comments
 (0)
Please sign in to comment.