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