Skip to content

Commit

Permalink
core-text: Implement CTRunGetTypographicBounds.
Browse files Browse the repository at this point in the history
  • Loading branch information
vorporeal committed May 1, 2024
1 parent dec5ede commit 9b24547
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions core-text/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

0 comments on commit 9b24547

Please sign in to comment.