Skip to content

Commit ee04992

Browse files
committed
add comment
1 parent 7e5640b commit ee04992

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Cargo.lock
22
target/
3+
.idea
34

core-text/src/run.rs

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10-
use std::borrow::Cow;
11-
use std::os::raw::c_void;
12-
use std::slice;
13-
use core_foundation::base::{CFIndex, CFTypeID, TCFType, CFType, CFRange};
10+
use core_foundation::base::{CFIndex, CFRange, CFType, CFTypeID, TCFType};
1411
use core_foundation::dictionary::{CFDictionary, CFDictionaryRef};
1512
use core_foundation::string::CFString;
13+
use core_graphics::base::CGFloat;
1614
use core_graphics::font::CGGlyph;
1715
use core_graphics::geometry::CGPoint;
18-
use core_graphics::base::{CGFloat};
16+
use std::borrow::Cow;
17+
use std::os::raw::c_void;
18+
use std::slice;
1919

2020
use crate::line::TypographicBounds;
2121

@@ -41,9 +41,7 @@ impl CTRun {
4141
}
4242
}
4343
pub fn glyph_count(&self) -> CFIndex {
44-
unsafe {
45-
CTRunGetGlyphCount(self.0)
46-
}
44+
unsafe { CTRunGetGlyphCount(self.0) }
4745
}
4846

4947
pub fn glyphs(&self) -> Cow<[CGGlyph]> {
@@ -91,12 +89,26 @@ impl CTRun {
9189
let mut descent = 0.0;
9290
let mut leading = 0.0;
9391
unsafe {
94-
let range = CFRange {
95-
location: 0,
96-
length: 0,
97-
};
98-
let width = CTRunGetTypographicBounds(self.as_concrete_TypeRef(), range, &mut ascent, &mut descent, &mut leading);
99-
TypographicBounds { width, ascent, descent, leading }
92+
// The portion of the run to calculate the typographic bounds for. By setting this to 0,
93+
// CoreText will measure the bounds from start to end, see https://developer.apple.com/documentation/coretext/1510569-ctrungettypographicbounds?language=objc.
94+
let range = CFRange {
95+
location: 0,
96+
length: 0,
97+
};
98+
99+
let width = CTRunGetTypographicBounds(
100+
self.as_concrete_TypeRef(),
101+
range,
102+
&mut ascent,
103+
&mut descent,
104+
&mut leading,
105+
);
106+
TypographicBounds {
107+
width,
108+
ascent,
109+
descent,
110+
leading,
111+
}
100112
}
101113
}
102114

@@ -124,21 +136,30 @@ impl CTRun {
124136
#[test]
125137
fn create_runs() {
126138
use core_foundation::attributed_string::CFMutableAttributedString;
127-
use string_attributes::*;
128-
use line::*;
129139
use font;
140+
use line::*;
141+
use string_attributes::*;
130142
let mut string = CFMutableAttributedString::new();
131143
string.replace_str(&CFString::new("Food"), CFRange::init(0, 0));
132144
let len = string.char_len();
133145
unsafe {
134-
string.set_attribute(CFRange::init(0, len), kCTFontAttributeName, &font::new_from_name("Helvetica", 16.).unwrap());
146+
string.set_attribute(
147+
CFRange::init(0, len),
148+
kCTFontAttributeName,
149+
&font::new_from_name("Helvetica", 16.).unwrap(),
150+
);
135151
}
136152
let line = CTLine::new_with_attributed_string(string.as_concrete_TypeRef());
137153
let runs = line.glyph_runs();
138154
assert_eq!(runs.len(), 1);
139155
for run in runs.iter() {
140156
assert_eq!(run.glyph_count(), 4);
141-
let font = run.attributes().unwrap().get(CFString::new("NSFont")).downcast::<font::CTFont>().unwrap();
157+
let font = run
158+
.attributes()
159+
.unwrap()
160+
.get(CFString::new("NSFont"))
161+
.downcast::<font::CTFont>()
162+
.unwrap();
142163
assert_eq!(font.pt_size(), 16.);
143164

144165
let positions = run.positions();
@@ -156,7 +177,7 @@ fn create_runs() {
156177
}
157178

158179
#[link(name = "CoreText", kind = "framework")]
159-
extern {
180+
extern "C" {
160181
fn CTRunGetTypeID() -> CFTypeID;
161182
fn CTRunGetAttributes(run: CTRunRef) -> CFDictionaryRef;
162183
fn CTRunGetGlyphCount(run: CTRunRef) -> CFIndex;
@@ -167,5 +188,11 @@ extern {
167188
fn CTRunGetGlyphsPtr(run: CTRunRef) -> *const CGGlyph;
168189
fn CTRunGetGlyphs(run: CTRunRef, range: CFRange, buffer: *const CGGlyph);
169190

170-
fn CTRunGetTypographicBounds(line: CTRunRef, range: CFRange, ascent: *mut CGFloat, descent: *mut CGFloat, leading: *mut CGFloat) -> CGFloat;
191+
fn CTRunGetTypographicBounds(
192+
line: CTRunRef,
193+
range: CFRange,
194+
ascent: *mut CGFloat,
195+
descent: *mut CGFloat,
196+
leading: *mut CGFloat,
197+
) -> CGFloat;
171198
}

0 commit comments

Comments
 (0)