From 7ec49da39368d45537decaed93ba4ff24c7182b1 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Mon, 20 Aug 2018 16:29:06 -0700 Subject: [PATCH] Add CTFrame and CTFramesetter --- core-text/src/frame.rs | 38 ++++++++++++++++++++++++ core-text/src/framesetter.rs | 57 ++++++++++++++++++++++++++++++++++++ core-text/src/lib.rs | 2 ++ 3 files changed, 97 insertions(+) create mode 100644 core-text/src/frame.rs create mode 100644 core-text/src/framesetter.rs diff --git a/core-text/src/frame.rs b/core-text/src/frame.rs new file mode 100644 index 0000000..3336f2c --- /dev/null +++ b/core-text/src/frame.rs @@ -0,0 +1,38 @@ +// Copyright 2013 The Servo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::os::raw::c_void; +use core_foundation::base::{CFTypeID, TCFType}; +use core_graphics::context::{CGContext, CGContextRef}; +use foreign_types::{ForeignType, ForeignTypeRef}; + +#[repr(C)] +pub struct __CTFrame(c_void); + +pub type CTFrameRef = *const __CTFrame; + +declare_TCFType! { + CTFrame, CTFrameRef +} +impl_TCFType!(CTFrame, CTFrameRef, CTFrameGetTypeID); +impl_CFTypeDescription!(CTFrame); + +impl CTFrame { + pub fn draw(&self, context: &CGContextRef) { + unsafe { + CTFrameDraw(self.as_concrete_TypeRef(), context.as_ptr()); + } + } +} + +#[link(name = "CoreText", kind = "framework")] +extern { + fn CTFrameGetTypeID() -> CFTypeID; + fn CTFrameDraw(frame: CTFrameRef, context: *mut ::CType); +} \ No newline at end of file diff --git a/core-text/src/framesetter.rs b/core-text/src/framesetter.rs new file mode 100644 index 0000000..cde767c --- /dev/null +++ b/core-text/src/framesetter.rs @@ -0,0 +1,57 @@ +// Copyright 2013 The Servo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::os::raw::c_void; +use core_foundation::attributed_string::CFAttributedStringRef; +use core_foundation::base::{CFRange, CFTypeID, TCFType}; +use core_graphics::path::{CGPath, CGPathRef}; +use foreign_types::{ForeignType, ForeignTypeRef}; +use std::ptr::null; +use super::frame::{CTFrameRef, CTFrame}; + +#[repr(C)] +pub struct __CTFramesetter(c_void); + +pub type CTFramesetterRef = *const __CTFramesetter; + +declare_TCFType! { + CTFramesetter, CTFramesetterRef +} +impl_TCFType!(CTFramesetter, CTFramesetterRef, CTFramesetterGetTypeID); +impl_CFTypeDescription!(CTFramesetter); + +impl CTFramesetter { + pub fn new_with_attributed_string(string: CFAttributedStringRef) -> Self { + unsafe { + let ptr = CTFramesetterCreateWithAttributedString(string); + CTFramesetter::wrap_under_create_rule(ptr) + } + } + + pub fn create_frame(&self, string_range: CFRange, path: &CGPathRef) -> CTFrame { + unsafe { + let ptr = CTFramesetterCreateFrame( + self.as_concrete_TypeRef(), string_range, path.as_ptr(), null()); + + CTFrame::wrap_under_create_rule(ptr) + } + } +} + +#[link(name = "CoreText", kind = "framework")] +extern { + fn CTFramesetterGetTypeID() -> CFTypeID; + fn CTFramesetterCreateWithAttributedString(string: CFAttributedStringRef) -> CTFramesetterRef; + fn CTFramesetterCreateFrame( + framesetter: CTFramesetterRef, + string_range: CFRange, + path: *mut ::CType, + attributes: *const c_void, + ) -> CTFrameRef; +} \ No newline at end of file diff --git a/core-text/src/lib.rs b/core-text/src/lib.rs index 401146a..fbd0be1 100644 --- a/core-text/src/lib.rs +++ b/core-text/src/lib.rs @@ -23,3 +23,5 @@ pub mod font; pub mod font_collection; pub mod font_descriptor; pub mod font_manager; +pub mod frame; +pub mod framesetter;