summaryrefslogtreecommitdiffstats
path: root/third_party/rust/core-text/src/framesetter.rs
blob: dacb540b85443bc1b851b73b7ce5d04dbc1a3a0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// 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 super::frame::{CTFrame, CTFrameRef};
use core_foundation::attributed_string::CFAttributedStringRef;
use core_foundation::base::{CFRange, CFTypeID, TCFType};
use core_foundation::dictionary::CFDictionaryRef;
use core_graphics::geometry::CGSize;
use core_graphics::path::{CGPath, CGPathRef};
use foreign_types::{ForeignType, ForeignTypeRef};
use std::os::raw::c_void;
use std::ptr::null;

#[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)
        }
    }

    /// Suggest an appropriate frame size for displaying a text range.
    ///
    /// Returns a tuple containing an appropriate size (that should be smaller
    /// than the provided constraints) as well as the range of text that fits in
    /// this frame.
    pub fn suggest_frame_size_with_constraints(
        &self,
        string_range: CFRange,
        frame_attributes: CFDictionaryRef,
        constraints: CGSize,
    ) -> (CGSize, CFRange) {
        unsafe {
            let mut fit_range = CFRange::init(0, 0);
            let size = CTFramesetterSuggestFrameSizeWithConstraints(
                self.as_concrete_TypeRef(),
                string_range,
                frame_attributes,
                constraints,
                &mut fit_range,
            );
            (size, fit_range)
        }
    }
}

#[link(name = "CoreText", kind = "framework")]
extern "C" {
    fn CTFramesetterGetTypeID() -> CFTypeID;
    fn CTFramesetterCreateWithAttributedString(string: CFAttributedStringRef) -> CTFramesetterRef;
    fn CTFramesetterCreateFrame(
        framesetter: CTFramesetterRef,
        string_range: CFRange,
        path: *mut <CGPath as ForeignType>::CType,
        attributes: *const c_void,
    ) -> CTFrameRef;
    fn CTFramesetterSuggestFrameSizeWithConstraints(
        framesetter: CTFramesetterRef,
        string_range: CFRange,
        frame_attributes: CFDictionaryRef,
        constraints: CGSize,
        fitRange: *mut CFRange,
    ) -> CGSize;
}