1use crate::{
2 color::Color,
3 fonts::{FontId, GlyphId, Metrics, Properties},
4 geometry::{
5 rect::{RectF, RectI},
6 transform2d::Transform2F,
7 vector::{vec2f, vec2i, Vector2F},
8 },
9 platform,
10 text_layout::{Glyph, LineLayout, Run},
11};
12use cocoa::appkit::{CGFloat, CGPoint};
13use core_foundation::{
14 array::CFIndex,
15 attributed_string::{CFAttributedStringRef, CFMutableAttributedString},
16 base::{CFRange, TCFType},
17 number::CFNumber,
18 string::CFString,
19};
20use core_graphics::{
21 base::CGGlyph, color_space::CGColorSpace, context::CGContext, geometry::CGAffineTransform,
22};
23use core_text::{line::CTLine, string_attributes::kCTFontAttributeName};
24use font_kit::{canvas::RasterizationOptions, hinting::HintingOptions, source::SystemSource};
25use parking_lot::RwLock;
26use std::{cell::RefCell, char, cmp, convert::TryFrom, ffi::c_void};
27
28#[allow(non_upper_case_globals)]
29const kCGImageAlphaOnly: u32 = 7;
30
31pub struct FontSystem(RwLock<FontSystemState>);
32
33struct FontSystemState {
34 source: SystemSource,
35 fonts: Vec<font_kit::font::Font>,
36}
37
38impl FontSystem {
39 pub fn new() -> Self {
40 Self(RwLock::new(FontSystemState {
41 source: SystemSource::new(),
42 fonts: Vec::new(),
43 }))
44 }
45}
46
47impl platform::FontSystem for FontSystem {
48 fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>> {
49 self.0.write().load_family(name)
50 }
51
52 fn select_font(&self, font_ids: &[FontId], properties: &Properties) -> anyhow::Result<FontId> {
53 self.0.read().select_font(font_ids, properties)
54 }
55
56 fn font_metrics(&self, font_id: FontId) -> Metrics {
57 self.0.read().font_metrics(font_id)
58 }
59
60 fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF> {
61 self.0.read().typographic_bounds(font_id, glyph_id)
62 }
63
64 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
65 self.0.read().glyph_for_char(font_id, ch)
66 }
67
68 fn rasterize_glyph(
69 &self,
70 font_id: FontId,
71 font_size: f32,
72 glyph_id: GlyphId,
73 subpixel_shift: Vector2F,
74 scale_factor: f32,
75 ) -> Option<(RectI, Vec<u8>)> {
76 self.0
77 .read()
78 .rasterize_glyph(font_id, font_size, glyph_id, subpixel_shift, scale_factor)
79 }
80
81 fn layout_line(
82 &self,
83 text: &str,
84 font_size: f32,
85 runs: &[(usize, FontId, Color)],
86 ) -> LineLayout {
87 self.0.read().layout_line(text, font_size, runs)
88 }
89
90 fn wrap_line(&self, text: &str, font_id: FontId, font_size: f32, width: f32) -> Vec<usize> {
91 self.0.read().wrap_line(text, font_id, font_size, width)
92 }
93}
94
95impl FontSystemState {
96 fn load_family(&mut self, name: &str) -> anyhow::Result<Vec<FontId>> {
97 let mut font_ids = Vec::new();
98 for font in self.source.select_family_by_name(name)?.fonts() {
99 let font = font.load()?;
100 font_ids.push(FontId(self.fonts.len()));
101 self.fonts.push(font);
102 }
103 Ok(font_ids)
104 }
105
106 fn select_font(&self, font_ids: &[FontId], properties: &Properties) -> anyhow::Result<FontId> {
107 let candidates = font_ids
108 .iter()
109 .map(|font_id| self.fonts[font_id.0].properties())
110 .collect::<Vec<_>>();
111 let idx = font_kit::matching::find_best_match(&candidates, properties)?;
112 Ok(font_ids[idx])
113 }
114
115 fn font_metrics(&self, font_id: FontId) -> Metrics {
116 self.fonts[font_id.0].metrics()
117 }
118
119 fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF> {
120 Ok(self.fonts[font_id.0].typographic_bounds(glyph_id)?)
121 }
122
123 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
124 self.fonts[font_id.0].glyph_for_char(ch)
125 }
126
127 fn rasterize_glyph(
128 &self,
129 font_id: FontId,
130 font_size: f32,
131 glyph_id: GlyphId,
132 subpixel_shift: Vector2F,
133 scale_factor: f32,
134 ) -> Option<(RectI, Vec<u8>)> {
135 let font = &self.fonts[font_id.0];
136 let scale = Transform2F::from_scale(scale_factor);
137 let bounds = font
138 .raster_bounds(
139 glyph_id,
140 font_size,
141 scale,
142 HintingOptions::None,
143 RasterizationOptions::GrayscaleAa,
144 )
145 .ok()?;
146
147 if bounds.width() == 0 || bounds.height() == 0 {
148 None
149 } else {
150 // Make room for subpixel variants.
151 let bounds = RectI::new(bounds.origin(), bounds.size() + vec2i(1, 1));
152 let mut pixels = vec![0; bounds.width() as usize * bounds.height() as usize];
153 let cx = CGContext::create_bitmap_context(
154 Some(pixels.as_mut_ptr() as *mut _),
155 bounds.width() as usize,
156 bounds.height() as usize,
157 8,
158 bounds.width() as usize,
159 &CGColorSpace::create_device_gray(),
160 kCGImageAlphaOnly,
161 );
162
163 // Move the origin to bottom left and account for scaling, this
164 // makes drawing text consistent with the font-kit's raster_bounds.
165 cx.translate(0.0, bounds.height() as CGFloat);
166 let transform = scale.translate(-bounds.origin().to_f32());
167 cx.set_text_matrix(&CGAffineTransform {
168 a: transform.matrix.m11() as CGFloat,
169 b: -transform.matrix.m21() as CGFloat,
170 c: -transform.matrix.m12() as CGFloat,
171 d: transform.matrix.m22() as CGFloat,
172 tx: transform.vector.x() as CGFloat,
173 ty: -transform.vector.y() as CGFloat,
174 });
175
176 cx.set_font(&font.native_font().copy_to_CGFont());
177 cx.set_font_size(font_size as CGFloat);
178 cx.show_glyphs_at_positions(
179 &[glyph_id as CGGlyph],
180 &[CGPoint::new(
181 (subpixel_shift.x() / scale_factor) as CGFloat,
182 (subpixel_shift.y() / scale_factor) as CGFloat,
183 )],
184 );
185
186 Some((bounds, pixels))
187 }
188 }
189
190 fn layout_line(
191 &self,
192 text: &str,
193 font_size: f32,
194 runs: &[(usize, FontId, Color)],
195 ) -> LineLayout {
196 let font_id_attr_name = CFString::from_static_string("zed_font_id");
197
198 // Construct the attributed string, converting UTF8 ranges to UTF16 ranges.
199 let mut string = CFMutableAttributedString::new();
200 {
201 string.replace_str(&CFString::new(text), CFRange::init(0, 0));
202 let utf16_line_len = string.char_len() as usize;
203
204 let last_run: RefCell<Option<(usize, FontId)>> = Default::default();
205 let font_runs = runs
206 .iter()
207 .filter_map(|(len, font_id, _)| {
208 let mut last_run = last_run.borrow_mut();
209 if let Some((last_len, last_font_id)) = last_run.as_mut() {
210 if font_id == last_font_id {
211 *last_len += *len;
212 None
213 } else {
214 let result = (*last_len, *last_font_id);
215 *last_len = *len;
216 *last_font_id = *font_id;
217 Some(result)
218 }
219 } else {
220 *last_run = Some((*len, *font_id));
221 None
222 }
223 })
224 .chain(std::iter::from_fn(|| last_run.borrow_mut().take()));
225
226 let mut ix_converter = StringIndexConverter::new(text);
227 for (run_len, font_id) in font_runs {
228 let utf8_end = ix_converter.utf8_ix + run_len;
229 let utf16_start = ix_converter.utf16_ix;
230
231 if utf16_start >= utf16_line_len {
232 break;
233 }
234
235 ix_converter.advance_to_utf8_ix(utf8_end);
236 let utf16_end = cmp::min(ix_converter.utf16_ix, utf16_line_len);
237
238 let cf_range =
239 CFRange::init(utf16_start as isize, (utf16_end - utf16_start) as isize);
240 let font = &self.fonts[font_id.0];
241 unsafe {
242 string.set_attribute(
243 cf_range,
244 kCTFontAttributeName,
245 &font.native_font().clone_with_font_size(font_size as f64),
246 );
247 string.set_attribute(
248 cf_range,
249 font_id_attr_name.as_concrete_TypeRef(),
250 &CFNumber::from(font_id.0 as i64),
251 );
252 }
253
254 if utf16_end == utf16_line_len {
255 break;
256 }
257 }
258 }
259
260 // Retrieve the glyphs from the shaped line, converting UTF16 offsets to UTF8 offsets.
261 let line = CTLine::new_with_attributed_string(string.as_concrete_TypeRef());
262
263 let mut runs = Vec::new();
264 for run in line.glyph_runs().into_iter() {
265 let font_id = FontId(
266 run.attributes()
267 .unwrap()
268 .get(&font_id_attr_name)
269 .downcast::<CFNumber>()
270 .unwrap()
271 .to_i64()
272 .unwrap() as usize,
273 );
274
275 let mut ix_converter = StringIndexConverter::new(text);
276 let mut glyphs = Vec::new();
277 for ((glyph_id, position), glyph_utf16_ix) in run
278 .glyphs()
279 .iter()
280 .zip(run.positions().iter())
281 .zip(run.string_indices().iter())
282 {
283 let glyph_utf16_ix = usize::try_from(*glyph_utf16_ix).unwrap();
284 ix_converter.advance_to_utf16_ix(glyph_utf16_ix);
285 glyphs.push(Glyph {
286 id: *glyph_id as GlyphId,
287 position: vec2f(position.x as f32, position.y as f32),
288 index: ix_converter.utf8_ix,
289 });
290 }
291
292 runs.push(Run { font_id, glyphs })
293 }
294
295 let typographic_bounds = line.get_typographic_bounds();
296 LineLayout {
297 width: typographic_bounds.width as f32,
298 ascent: typographic_bounds.ascent as f32,
299 descent: typographic_bounds.descent as f32,
300 runs,
301 font_size,
302 len: text.len(),
303 }
304 }
305
306 fn wrap_line(&self, text: &str, font_id: FontId, font_size: f32, width: f32) -> Vec<usize> {
307 let mut string = CFMutableAttributedString::new();
308 string.replace_str(&CFString::new(text), CFRange::init(0, 0));
309 let cf_range = CFRange::init(0 as isize, text.encode_utf16().count() as isize);
310 let font = &self.fonts[font_id.0];
311 unsafe {
312 string.set_attribute(
313 cf_range,
314 kCTFontAttributeName,
315 &font.native_font().clone_with_font_size(font_size as f64),
316 );
317
318 let typesetter = CTTypesetterCreateWithAttributedString(string.as_concrete_TypeRef());
319 let mut ix_converter = StringIndexConverter::new(text);
320 let mut break_indices = Vec::new();
321 while ix_converter.utf8_ix < text.len() {
322 let utf16_len = CTTypesetterSuggestLineBreak(
323 typesetter,
324 ix_converter.utf16_ix as isize,
325 width as f64,
326 ) as usize;
327 ix_converter.advance_to_utf16_ix(ix_converter.utf16_ix + utf16_len);
328 if ix_converter.utf8_ix >= text.len() {
329 break;
330 }
331 break_indices.push(ix_converter.utf8_ix as usize);
332 }
333 break_indices
334 }
335 }
336}
337
338#[derive(Clone)]
339struct StringIndexConverter<'a> {
340 text: &'a str,
341 utf8_ix: usize,
342 utf16_ix: usize,
343}
344
345impl<'a> StringIndexConverter<'a> {
346 fn new(text: &'a str) -> Self {
347 Self {
348 text,
349 utf8_ix: 0,
350 utf16_ix: 0,
351 }
352 }
353
354 fn advance_to_utf8_ix(&mut self, utf8_target: usize) {
355 for (ix, c) in self.text[self.utf8_ix..].char_indices() {
356 if self.utf8_ix + ix >= utf8_target {
357 self.utf8_ix += ix;
358 return;
359 }
360 self.utf16_ix += c.len_utf16();
361 }
362 self.utf8_ix = self.text.len();
363 }
364
365 fn advance_to_utf16_ix(&mut self, utf16_target: usize) {
366 for (ix, c) in self.text[self.utf8_ix..].char_indices() {
367 if self.utf16_ix >= utf16_target {
368 self.utf8_ix += ix;
369 return;
370 }
371 self.utf16_ix += c.len_utf16();
372 }
373 self.utf8_ix = self.text.len();
374 }
375}
376
377#[repr(C)]
378pub struct __CFTypesetter(c_void);
379
380pub type CTTypesetterRef = *const __CFTypesetter;
381
382#[link(name = "CoreText", kind = "framework")]
383extern "C" {
384 fn CTTypesetterCreateWithAttributedString(string: CFAttributedStringRef) -> CTTypesetterRef;
385
386 fn CTTypesetterSuggestLineBreak(
387 typesetter: CTTypesetterRef,
388 start_index: CFIndex,
389 width: f64,
390 ) -> CFIndex;
391}
392
393#[cfg(test)]
394mod tests {
395 use crate::MutableAppContext;
396
397 use super::*;
398 use font_kit::properties::{Style, Weight};
399 use platform::FontSystem as _;
400
401 #[crate::test(self, retries = 5)]
402 fn test_layout_str(_: &mut MutableAppContext) {
403 // This is failing intermittently on CI and we don't have time to figure it out
404 let fonts = FontSystem::new();
405 let menlo = fonts.load_family("Menlo").unwrap();
406 let menlo_regular = fonts.select_font(&menlo, &Properties::new()).unwrap();
407 let menlo_italic = fonts
408 .select_font(&menlo, &Properties::new().style(Style::Italic))
409 .unwrap();
410 let menlo_bold = fonts
411 .select_font(&menlo, &Properties::new().weight(Weight::BOLD))
412 .unwrap();
413 assert_ne!(menlo_regular, menlo_italic);
414 assert_ne!(menlo_regular, menlo_bold);
415 assert_ne!(menlo_italic, menlo_bold);
416
417 let line = fonts.layout_line(
418 "hello world",
419 16.0,
420 &[
421 (2, menlo_bold, Default::default()),
422 (4, menlo_italic, Default::default()),
423 (5, menlo_regular, Default::default()),
424 ],
425 );
426 assert_eq!(line.runs.len(), 3);
427 assert_eq!(line.runs[0].font_id, menlo_bold);
428 assert_eq!(line.runs[0].glyphs.len(), 2);
429 assert_eq!(line.runs[1].font_id, menlo_italic);
430 assert_eq!(line.runs[1].glyphs.len(), 4);
431 assert_eq!(line.runs[2].font_id, menlo_regular);
432 assert_eq!(line.runs[2].glyphs.len(), 5);
433 }
434
435 #[test]
436 fn test_glyph_offsets() -> anyhow::Result<()> {
437 let fonts = FontSystem::new();
438 let zapfino = fonts.load_family("Zapfino")?;
439 let zapfino_regular = fonts.select_font(&zapfino, &Properties::new())?;
440 let menlo = fonts.load_family("Menlo")?;
441 let menlo_regular = fonts.select_font(&menlo, &Properties::new())?;
442
443 let text = "This is, mπre πr less, Zapfino!π";
444 let line = fonts.layout_line(
445 text,
446 16.0,
447 &[
448 (9, zapfino_regular, Color::default()),
449 (13, menlo_regular, Color::default()),
450 (text.len() - 22, zapfino_regular, Color::default()),
451 ],
452 );
453 assert_eq!(
454 line.runs
455 .iter()
456 .flat_map(|r| r.glyphs.iter())
457 .map(|g| g.index)
458 .collect::<Vec<_>>(),
459 vec![0, 2, 4, 5, 7, 8, 9, 10, 14, 15, 16, 17, 21, 22, 23, 24, 26, 27, 28, 29, 36, 37],
460 );
461 Ok(())
462 }
463
464 #[test]
465 #[ignore]
466 fn test_rasterize_glyph() {
467 use std::{fs::File, io::BufWriter, path::Path};
468
469 let fonts = FontSystem::new();
470 let font_ids = fonts.load_family("Fira Code").unwrap();
471 let font_id = fonts.select_font(&font_ids, &Default::default()).unwrap();
472 let glyph_id = fonts.glyph_for_char(font_id, 'G').unwrap();
473
474 const VARIANTS: usize = 1;
475 for i in 0..VARIANTS {
476 let variant = i as f32 / VARIANTS as f32;
477 let (bounds, bytes) = fonts
478 .rasterize_glyph(font_id, 16.0, glyph_id, vec2f(variant, variant), 2.)
479 .unwrap();
480
481 let name = format!("/Users/as-cii/Desktop/twog-{}.png", i);
482 let path = Path::new(&name);
483 let file = File::create(path).unwrap();
484 let ref mut w = BufWriter::new(file);
485
486 let mut encoder = png::Encoder::new(w, bounds.width() as u32, bounds.height() as u32);
487 encoder.set_color(png::ColorType::Grayscale);
488 encoder.set_depth(png::BitDepth::Eight);
489 let mut writer = encoder.write_header().unwrap();
490 writer.write_image_data(&bytes).unwrap();
491 }
492 }
493
494 #[test]
495 fn test_wrap_line() {
496 let fonts = FontSystem::new();
497 let font_ids = fonts.load_family("Helvetica").unwrap();
498 let font_id = fonts.select_font(&font_ids, &Default::default()).unwrap();
499
500 let line = "one two three four five\n";
501 let wrap_boundaries = fonts.wrap_line(line, font_id, 16., 64.0);
502 assert_eq!(wrap_boundaries, &["one two ".len(), "one two three ".len()]);
503
504 let line = "aaa Ξ±Ξ±Ξ± βββ πππ\n";
505 let wrap_boundaries = fonts.wrap_line(line, font_id, 16., 64.0);
506 assert_eq!(
507 wrap_boundaries,
508 &["aaa Ξ±Ξ±Ξ± ".len(), "aaa Ξ±Ξ±Ξ± βββ ".len(),]
509 );
510 }
511
512 #[test]
513 fn test_layout_line_bom_char() {
514 let fonts = FontSystem::new();
515 let font_ids = fonts.load_family("Helvetica").unwrap();
516 let font_id = fonts.select_font(&font_ids, &Default::default()).unwrap();
517
518 let line = "\u{feff}";
519 let layout = fonts.layout_line(line, 16., &[(line.len(), font_id, Default::default())]);
520 assert_eq!(layout.len, line.len());
521 assert!(layout.runs.is_empty());
522
523 let line = "a\u{feff}b";
524 let layout = fonts.layout_line(line, 16., &[(line.len(), font_id, Default::default())]);
525 assert_eq!(layout.len, line.len());
526 assert_eq!(layout.runs.len(), 1);
527 assert_eq!(layout.runs[0].glyphs.len(), 2);
528 assert_eq!(layout.runs[0].glyphs[0].id, 68); // a
529 // There's no glyph for \u{feff}
530 assert_eq!(layout.runs[0].glyphs[1].id, 69); // b
531 }
532}