Auto merge of #223 - mehcode:feature/attributed-string, r=jrmuizel

Add CF(Mutable)AttributedString and a couple helpers

Just declared stuff that I'm using. Copied the basic template from CFString.

----

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/223)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo
2018-07-06 16:47:27 -04:00
committed by GitHub
4 changed files with 137 additions and 0 deletions
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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;
}
+1
View File
@@ -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;
+79
View File
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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<T: TCFType>(&mut self, range: CFRange, name: CFStringRef, value: T) {
unsafe {
CFAttributedStringSetAttribute(
self.0, range, name, value.as_CFTypeRef());
}
}
}
+1
View File
@@ -162,6 +162,7 @@ macro_rules! impl_CFComparison {
}
pub mod array;
pub mod attributed_string;
pub mod base;
pub mod boolean;
pub mod data;