Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core-text: Implement CTRunGetTypographicBounds. #675

Merged
merged 1 commit into from
May 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}