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