From b205c0fe7e72152bbdccc8b956ddb3aedfb0b8be Mon Sep 17 00:00:00 2001 From: Colin Rofls Date: Sat, 9 May 2020 11:40:58 -0400 Subject: [PATCH] Add CTLine::get_typographic_bounds This also adds a type, TypographicBounds, for reporting the result of this method. --- core-text/src/line.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/core-text/src/line.rs b/core-text/src/line.rs index 28e52e8..5e09bff 100644 --- a/core-text/src/line.rs +++ b/core-text/src/line.rs @@ -28,6 +28,14 @@ declare_TCFType! { impl_TCFType!(CTLine, CTLineRef, CTLineGetTypeID); impl_CFTypeDescription!(CTLine); +/// Metrics for a given line. +pub struct TypographicBounds { + pub width: CGFloat, + pub ascent: CGFloat, + pub descent: CGFloat, + pub leading: CGFloat, +} + impl CTLine { pub fn new_with_attributed_string(string: CFAttributedStringRef) -> Self { unsafe { @@ -54,6 +62,16 @@ impl CTLine { } } + pub fn get_typographic_bounds(&self) -> TypographicBounds { + let mut ascent = 0.0; + let mut descent = 0.0; + let mut leading = 0.0; + unsafe { + let width = CTLineGetTypographicBounds(self.as_concrete_TypeRef(), &mut ascent, &mut descent, &mut leading); + TypographicBounds { width, ascent, descent, leading } + } + } + pub fn get_string_index_for_position(&self, position: CGPoint) -> CFIndex { unsafe { CTLineGetStringIndexForPosition(self.as_concrete_TypeRef(), position) @@ -80,6 +98,7 @@ extern { // Measuring Lines fn CTLineGetImageBounds(line: CTLineRef, context: * const core_graphics::sys::CGContext) -> CGRect; + fn CTLineGetTypographicBounds(line: CTLineRef, ascent: *mut CGFloat, descent: *mut CGFloat, leading: *mut CGFloat) -> CGFloat; // Getting Line Positioning fn CTLineGetStringIndexForPosition(line: CTLineRef, position: CGPoint) -> CFIndex;