From 1d369075c3195ef83b193b0a1610a049efe85206 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Fri, 6 Jul 2018 10:24:54 -0700 Subject: [PATCH] Add CF(Mutable)AttributedString and a couple helpers --- core-foundation-sys/src/attributed_string.rs | 56 ++++++++++++++ core-foundation-sys/src/lib.rs | 1 + core-foundation/src/attributed_string.rs | 79 ++++++++++++++++++++ core-foundation/src/lib.rs | 1 + 4 files changed, 137 insertions(+) create mode 100644 core-foundation-sys/src/attributed_string.rs create mode 100644 core-foundation/src/attributed_string.rs diff --git a/core-foundation-sys/src/attributed_string.rs b/core-foundation-sys/src/attributed_string.rs new file mode 100644 index 0000000..ecdffe6 --- /dev/null +++ b/core-foundation-sys/src/attributed_string.rs @@ -0,0 +1,56 @@ +// 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 base::{CFAllocatorRef, CFTypeRef, CFIndex, CFRange, CFTypeID}; +use string::CFStringRef; +use dictionary::CFDictionaryRef; + +#[repr(C)] +pub struct __CFAttributedString(c_void); + +pub type CFAttributedStringRef = *const __CFAttributedString; +pub type CFMutableAttributedStringRef = *const __CFAttributedString; + +extern { + /* CFAttributedString */ + + pub fn CFAttributedStringCreate( + allocator: CFAllocatorRef, + str: CFStringRef, + attributes: CFDictionaryRef, + ) -> CFAttributedStringRef; + + pub fn CFAttributedStringGetLength(astr: CFAttributedStringRef) -> CFIndex; + + pub fn CFAttributedStringGetTypeID() -> CFTypeID; + + /* CFMutableAttributedString */ + + pub fn CFAttributedStringCreateMutableCopy( + allocator: CFAllocatorRef, max_length: CFIndex, astr: CFAttributedStringRef + ) -> CFMutableAttributedStringRef; + + pub fn CFAttributedStringCreateMutable( + allocator: CFAllocatorRef, + max_length: CFIndex, + ) -> CFMutableAttributedStringRef; + + pub fn CFAttributedStringReplaceString( + astr: CFMutableAttributedStringRef, range: CFRange, replacement: CFStringRef); + + pub fn CFAttributedStringSetAttribute( + astr: CFMutableAttributedStringRef, + range: CFRange, + attr_name: CFStringRef, + value: CFTypeRef, + ); + + pub fn CFMutableAttributedStringGetTypeID() -> CFTypeID; +} diff --git a/core-foundation-sys/src/lib.rs b/core-foundation-sys/src/lib.rs index 21888d7..e03cddb 100644 --- a/core-foundation-sys/src/lib.rs +++ b/core-foundation-sys/src/lib.rs @@ -11,6 +11,7 @@ #![cfg_attr(all(feature="mac_os_10_7_support", feature="mac_os_10_8_features"), feature(linkage))] // back-compat requires weak linkage pub mod array; +pub mod attributed_string; pub mod base; pub mod bundle; pub mod data; diff --git a/core-foundation/src/attributed_string.rs b/core-foundation/src/attributed_string.rs new file mode 100644 index 0000000..dea20fc --- /dev/null +++ b/core-foundation/src/attributed_string.rs @@ -0,0 +1,79 @@ +// 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. + +pub use core_foundation_sys::attributed_string::*; + +use base::TCFType; +use core_foundation_sys::base::{CFIndex, CFRange, kCFAllocatorDefault}; +use std::ptr::null; +use string::{CFString, CFStringRef}; + +declare_TCFType!{ + CFAttributedString, CFAttributedStringRef +} +impl_TCFType!(CFAttributedString, CFAttributedStringRef, CFAttributedStringGetTypeID); + +impl CFAttributedString { + #[inline] + pub fn new(string: &CFString) -> Self { + unsafe { + let astr_ref = CFAttributedStringCreate( + kCFAllocatorDefault, string.as_concrete_TypeRef(), null()); + + CFAttributedString::wrap_under_create_rule(astr_ref) + } + } + + #[inline] + pub fn char_len(&self) -> CFIndex { + unsafe { + CFAttributedStringGetLength(self.0) + } + } +} + +declare_TCFType!{ + CFMutableAttributedString, CFMutableAttributedStringRef +} +impl_TCFType!(CFMutableAttributedString, CFMutableAttributedStringRef, CFMutableAttributedStringGetTypeID); + +impl CFMutableAttributedString { + #[inline] + pub fn new() -> Self { + unsafe { + let astr_ref = CFAttributedStringCreateMutable( + kCFAllocatorDefault, 0); + + CFMutableAttributedString::wrap_under_create_rule(astr_ref) + } + } + + #[inline] + pub fn char_len(&self) -> CFIndex { + unsafe { + CFAttributedStringGetLength(self.0) + } + } + + #[inline] + pub fn replace_str(&mut self, string: &CFString, range: CFRange) { + unsafe { + CFAttributedStringReplaceString( + self.0, range, string.as_concrete_TypeRef()); + } + } + + #[inline] + pub fn set_attribute(&mut self, range: CFRange, name: CFStringRef, value: T) { + unsafe { + CFAttributedStringSetAttribute( + self.0, range, name, value.as_CFTypeRef()); + } + } +} diff --git a/core-foundation/src/lib.rs b/core-foundation/src/lib.rs index 34cc3fc..02ad188 100644 --- a/core-foundation/src/lib.rs +++ b/core-foundation/src/lib.rs @@ -162,6 +162,7 @@ macro_rules! impl_CFComparison { } pub mod array; +pub mod attributed_string; pub mod base; pub mod boolean; pub mod data;