core-text: Implement CTRunGetTypographicBounds. (#675)

This commit is contained in:
David Stern
2024-05-22 22:05:58 -04:00
committed by GitHub
parent fb8fd65eef
commit 32368020b6
+38
View File
@@ -10,12 +10,15 @@
use core_foundation::base::{CFIndex, CFRange, CFType, CFTypeID, TCFType};
use core_foundation::dictionary::{CFDictionary, CFDictionaryRef};
use core_foundation::string::CFString;
use core_graphics::base::CGFloat;
use core_graphics::font::CGGlyph;
use core_graphics::geometry::CGPoint;
use std::borrow::Cow;
use std::os::raw::c_void;
use std::slice;
use crate::line::TypographicBounds;
#[repr(C)]
pub struct __CTRun(c_void);
@@ -81,6 +84,34 @@ impl CTRun {
}
}
pub fn get_typographic_bounds(&self) -> TypographicBounds {
let mut ascent = 0.0;
let mut descent = 0.0;
let mut leading = 0.0;
unsafe {
// The portion of the run to calculate the typographic bounds for. By setting this to 0,
// CoreText will measure the bounds from start to end, see https://developer.apple.com/documentation/coretext/1510569-ctrungettypographicbounds?language=objc.
let range = CFRange {
location: 0,
length: 0,
};
let width = CTRunGetTypographicBounds(
self.as_concrete_TypeRef(),
range,
&mut ascent,
&mut descent,
&mut leading,
);
TypographicBounds {
width,
ascent,
descent,
leading,
}
}
}
pub fn string_indices(&self) -> Cow<[CFIndex]> {
unsafe {
// CTRunGetStringIndicesPtr can return null under some not understood circumstances.
@@ -156,4 +187,11 @@ extern "C" {
fn CTRunGetStringIndices(run: CTRunRef, range: CFRange, buffer: *const CFIndex);
fn CTRunGetGlyphsPtr(run: CTRunRef) -> *const CGGlyph;
fn CTRunGetGlyphs(run: CTRunRef, range: CFRange, buffer: *const CGGlyph);
fn CTRunGetTypographicBounds(
line: CTRunRef,
range: CFRange,
ascent: *mut CGFloat,
descent: *mut CGFloat,
leading: *mut CGFloat,
) -> CGFloat;
}