|
10 | 10 | use core_foundation::base::{CFIndex, CFRange, CFType, CFTypeID, TCFType};
|
11 | 11 | use core_foundation::dictionary::{CFDictionary, CFDictionaryRef};
|
12 | 12 | use core_foundation::string::CFString;
|
| 13 | +use core_graphics::base::CGFloat; |
13 | 14 | use core_graphics::font::CGGlyph;
|
14 | 15 | use core_graphics::geometry::CGPoint;
|
15 | 16 | use std::borrow::Cow;
|
16 | 17 | use std::os::raw::c_void;
|
17 | 18 | use std::slice;
|
18 | 19 |
|
| 20 | +use crate::line::TypographicBounds; |
| 21 | + |
19 | 22 | #[repr(C)]
|
20 | 23 | pub struct __CTRun(c_void);
|
21 | 24 |
|
@@ -81,6 +84,34 @@ impl CTRun {
|
81 | 84 | }
|
82 | 85 | }
|
83 | 86 |
|
| 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 | + |
84 | 115 | pub fn string_indices(&self) -> Cow<[CFIndex]> {
|
85 | 116 | unsafe {
|
86 | 117 | // CTRunGetStringIndicesPtr can return null under some not understood circumstances.
|
@@ -156,4 +187,11 @@ extern "C" {
|
156 | 187 | fn CTRunGetStringIndices(run: CTRunRef, range: CFRange, buffer: *const CFIndex);
|
157 | 188 | fn CTRunGetGlyphsPtr(run: CTRunRef) -> *const CGGlyph;
|
158 | 189 | 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; |
159 | 197 | }
|
0 commit comments