From bab39948b11d3ff144d52eec4fc9508dbfbfc3f4 Mon Sep 17 00:00:00 2001 From: Colin Rofls Date: Sat, 9 May 2020 13:21:33 -0400 Subject: [PATCH] Add CTFrame::get_lines This is a bit unsatisfactory; there doesn't seem to be a memory safe way to get access to the lines without an intermediate allocation. --- core-text/src/frame.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/core-text/src/frame.rs b/core-text/src/frame.rs index 3336f2c..d1d0607 100644 --- a/core-text/src/frame.rs +++ b/core-text/src/frame.rs @@ -9,8 +9,10 @@ use std::os::raw::c_void; use core_foundation::base::{CFTypeID, TCFType}; +use core_foundation::array::{CFArrayRef, CFArray}; use core_graphics::context::{CGContext, CGContextRef}; use foreign_types::{ForeignType, ForeignTypeRef}; +use crate::line::CTLine; #[repr(C)] pub struct __CTFrame(c_void); @@ -24,6 +26,18 @@ impl_TCFType!(CTFrame, CTFrameRef, CTFrameGetTypeID); impl_CFTypeDescription!(CTFrame); impl CTFrame { + /// Returns an owned copy of the underlying lines. + /// + /// Each line is retained, and will remain valid past the life of this `CTFrame`. + pub fn get_lines(&self) -> Vec { + unsafe { + let array_ref = CTFrameGetLines(self.as_concrete_TypeRef()); + // not strictly correct but saves an unnecessary retain call + let array: CFArray = CFArray::wrap_under_create_rule(array_ref); + array.iter().map(|l| CTLine::wrap_under_get_rule(l.as_concrete_TypeRef())).collect() + } + } + pub fn draw(&self, context: &CGContextRef) { unsafe { CTFrameDraw(self.as_concrete_TypeRef(), context.as_ptr()); @@ -34,5 +48,6 @@ impl CTFrame { #[link(name = "CoreText", kind = "framework")] extern { fn CTFrameGetTypeID() -> CFTypeID; + fn CTFrameGetLines(frame: CTFrameRef) -> CFArrayRef; fn CTFrameDraw(frame: CTFrameRef, context: *mut ::CType); -} \ No newline at end of file +}