| 1 |
// Copyright 2013 The Servo Project Developers. See the COPYRIGHT |
| 2 |
// file at the top-level directory of this distribution. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 5 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 6 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 7 |
// option. This file may not be copied, modified, or distributed |
| 8 |
// except according to those terms. |
| 9 |
|
| 10 |
pub use core_foundation_sys::attributed_string::*; |
| 11 |
|
| 12 |
use crate::base::TCFType; |
| 13 |
use crate::string::{CFString, CFStringRef}; |
| 14 |
use core_foundation_sys::base::{kCFAllocatorDefault, CFIndex, CFRange}; |
| 15 |
use std::ptr::null; |
| 16 |
|
| 17 |
declare_TCFType! { |
| 18 |
CFAttributedString, CFAttributedStringRef |
| 19 |
} |
| 20 |
impl_TCFType!( |
| 21 |
CFAttributedString, |
| 22 |
CFAttributedStringRef, |
| 23 |
CFAttributedStringGetTypeID |
| 24 |
); |
| 25 |
|
| 26 |
impl CFAttributedString { |
| 27 |
#[inline] |
| 28 |
pub fn new(string: &CFString) -> Self { |
| 29 |
unsafe { |
| 30 |
let astr_ref = |
| 31 |
CFAttributedStringCreate(kCFAllocatorDefault, string.as_concrete_TypeRef(), null()); |
| 32 |
|
| 33 |
CFAttributedString::wrap_under_create_rule(astr_ref) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
#[inline] |
| 38 |
pub fn char_len(&self) -> CFIndex { |
| 39 |
unsafe { CFAttributedStringGetLength(self.0) } |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
declare_TCFType! { |
| 44 |
CFMutableAttributedString, CFMutableAttributedStringRef |
| 45 |
} |
| 46 |
impl_TCFType!( |
| 47 |
CFMutableAttributedString, |
| 48 |
CFMutableAttributedStringRef, |
| 49 |
CFAttributedStringGetTypeID |
| 50 |
); |
| 51 |
|
| 52 |
impl CFMutableAttributedString { |
| 53 |
#[inline] |
| 54 |
pub fn new() -> Self { |
| 55 |
unsafe { |
| 56 |
let astr_ref = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0); |
| 57 |
|
| 58 |
CFMutableAttributedString::wrap_under_create_rule(astr_ref) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
#[inline] |
| 63 |
pub fn char_len(&self) -> CFIndex { |
| 64 |
unsafe { CFAttributedStringGetLength(self.0) } |
| 65 |
} |
| 66 |
|
| 67 |
#[inline] |
| 68 |
pub fn replace_str(&mut self, string: &CFString, range: CFRange) { |
| 69 |
unsafe { |
| 70 |
CFAttributedStringReplaceString(self.0, range, string.as_concrete_TypeRef()); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
#[inline] |
| 75 |
pub fn set_attribute<T: TCFType>(&mut self, range: CFRange, name: CFStringRef, value: &T) { |
| 76 |
unsafe { |
| 77 |
CFAttributedStringSetAttribute(self.0, range, name, value.as_CFTypeRef()); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
impl Default for CFMutableAttributedString { |
| 83 |
fn default() -> Self { |
| 84 |
Self::new() |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
#[cfg(test)] |
| 89 |
mod tests { |
| 90 |
use super::*; |
| 91 |
|
| 92 |
#[test] |
| 93 |
fn attributed_string_type_id_comparison() { |
| 94 |
// CFMutableAttributedString TypeID must be equal to CFAttributedString TypeID. |
| 95 |
// Compilation must not fail. |
| 96 |
assert_eq!( |
| 97 |
<CFAttributedString as TCFType>::type_id(), |
| 98 |
<CFMutableAttributedString as TCFType>::type_id() |
| 99 |
); |
| 100 |
} |
| 101 |
} |