1use crate::{
2 fonts::{FontId, GlyphId, Metrics, Properties},
3 geometry::{
4 rect::{RectF, RectI},
5 transform2d::Transform2F,
6 vector::{vec2f, vec2i, Vector2F},
7 },
8 platform,
9 text_layout::{Glyph, LineLayout, Run, RunStyle},
10};
11use cocoa::appkit::{CGFloat, CGPoint};
12use core_foundation::{
13 array::CFIndex,
14 attributed_string::{CFAttributedStringRef, CFMutableAttributedString},
15 base::{CFRange, TCFType},
16 number::CFNumber,
17 string::CFString,
18};
19use core_graphics::{
20 base::CGGlyph, color_space::CGColorSpace, context::CGContext, geometry::CGAffineTransform,
21};
22use core_text::{line::CTLine, string_attributes::kCTFontAttributeName};
23use font_kit::{
24 canvas::RasterizationOptions, handle::Handle, hinting::HintingOptions, source::SystemSource,
25 sources::mem::MemSource,
26};
27use parking_lot::RwLock;
28use std::{cell::RefCell, char, cmp, convert::TryFrom, ffi::c_void, sync::Arc};
29
30#[allow(non_upper_case_globals)]
31const kCGImageAlphaOnly: u32 = 7;
32
33pub struct FontSystem(RwLock<FontSystemState>);
34
35struct FontSystemState {
36 memory_source: MemSource,
37 system_source: SystemSource,
38 fonts: Vec<font_kit::font::Font>,
39}
40
41impl FontSystem {
42 pub fn new() -> Self {
43 Self(RwLock::new(FontSystemState {
44 memory_source: MemSource::empty(),
45 system_source: SystemSource::new(),
46 fonts: Vec::new(),
47 }))
48 }
49}
50
51impl platform::FontSystem for FontSystem {
52 fn add_fonts(&self, fonts: &[Arc<Vec<u8>>]) -> anyhow::Result<()> {
53 self.0.write().add_fonts(fonts)
54 }
55
56 fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>> {
57 self.0.write().load_family(name)
58 }
59
60 fn select_font(&self, font_ids: &[FontId], properties: &Properties) -> anyhow::Result<FontId> {
61 self.0.read().select_font(font_ids, properties)
62 }
63
64 fn font_metrics(&self, font_id: FontId) -> Metrics {
65 self.0.read().font_metrics(font_id)
66 }
67
68 fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF> {
69 self.0.read().typographic_bounds(font_id, glyph_id)
70 }
71
72 fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<Vector2F> {
73 self.0.read().advance(font_id, glyph_id)
74 }
75
76 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
77 self.0.read().glyph_for_char(font_id, ch)
78 }
79
80 fn rasterize_glyph(
81 &self,
82 font_id: FontId,
83 font_size: f32,
84 glyph_id: GlyphId,
85 subpixel_shift: Vector2F,
86 scale_factor: f32,
87 ) -> Option<(RectI, Vec<u8>)> {
88 self.0
89 .read()
90 .rasterize_glyph(font_id, font_size, glyph_id, subpixel_shift, scale_factor)
91 }
92
93 fn layout_line(&self, text: &str, font_size: f32, runs: &[(usize, RunStyle)]) -> LineLayout {
94 self.0.read().layout_line(text, font_size, runs)
95 }
96
97 fn wrap_line(&self, text: &str, font_id: FontId, font_size: f32, width: f32) -> Vec<usize> {
98 self.0.read().wrap_line(text, font_id, font_size, width)
99 }
100}
101
102impl FontSystemState {
103 fn add_fonts(&mut self, fonts: &[Arc<Vec<u8>>]) -> anyhow::Result<()> {
104 self.memory_source.add_fonts(
105 fonts
106 .iter()
107 .map(|bytes| Handle::from_memory(bytes.clone(), 0)),
108 )?;
109 Ok(())
110 }
111
112 fn load_family(&mut self, name: &str) -> anyhow::Result<Vec<FontId>> {
113 let mut font_ids = Vec::new();
114
115 let family = self
116 .memory_source
117 .select_family_by_name(name)
118 .or_else(|_| self.system_source.select_family_by_name(name))?;
119 for font in family.fonts() {
120 let font = font.load()?;
121 font_ids.push(FontId(self.fonts.len()));
122 self.fonts.push(font);
123 }
124 Ok(font_ids)
125 }
126
127 fn select_font(&self, font_ids: &[FontId], properties: &Properties) -> anyhow::Result<FontId> {
128 let candidates = font_ids
129 .iter()
130 .map(|font_id| self.fonts[font_id.0].properties())
131 .collect::<Vec<_>>();
132 let idx = font_kit::matching::find_best_match(&candidates, properties)?;
133 Ok(font_ids[idx])
134 }
135
136 fn font_metrics(&self, font_id: FontId) -> Metrics {
137 self.fonts[font_id.0].metrics()
138 }
139
140 fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF> {
141 Ok(self.fonts[font_id.0].typographic_bounds(glyph_id)?)
142 }
143
144 fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<Vector2F> {
145 Ok(self.fonts[font_id.0].advance(glyph_id)?)
146 }
147
148 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
149 self.fonts[font_id.0].glyph_for_char(ch)
150 }
151
152 fn rasterize_glyph(
153 &self,
154 font_id: FontId,
155 font_size: f32,
156 glyph_id: GlyphId,
157 subpixel_shift: Vector2F,
158 scale_factor: f32,
159 ) -> Option<(RectI, Vec<u8>)> {
160 let font = &self.fonts[font_id.0];
161 let scale = Transform2F::from_scale(scale_factor);
162 let bounds = font
163 .raster_bounds(
164 glyph_id,
165 font_size,
166 scale,
167 HintingOptions::None,
168 RasterizationOptions::GrayscaleAa,
169 )
170 .ok()?;
171
172 if bounds.width() == 0 || bounds.height() == 0 {
173 None
174 } else {
175 // Make room for subpixel variants.
176 let bounds = RectI::new(bounds.origin(), bounds.size() + vec2i(1, 1));
177 let mut pixels = vec![0; bounds.width() as usize * bounds.height() as usize];
178 let cx = CGContext::create_bitmap_context(
179 Some(pixels.as_mut_ptr() as *mut _),
180 bounds.width() as usize,
181 bounds.height() as usize,
182 8,
183 bounds.width() as usize,
184 &CGColorSpace::create_device_gray(),
185 kCGImageAlphaOnly,
186 );
187
188 // Move the origin to bottom left and account for scaling, this
189 // makes drawing text consistent with the font-kit's raster_bounds.
190 cx.translate(0.0, bounds.height() as CGFloat);
191 let transform = scale.translate(-bounds.origin().to_f32());
192 cx.set_text_matrix(&CGAffineTransform {
193 a: transform.matrix.m11() as CGFloat,
194 b: -transform.matrix.m21() as CGFloat,
195 c: -transform.matrix.m12() as CGFloat,
196 d: transform.matrix.m22() as CGFloat,
197 tx: transform.vector.x() as CGFloat,
198 ty: -transform.vector.y() as CGFloat,
199 });
200
201 cx.set_font(&font.native_font().copy_to_CGFont());
202 cx.set_font_size(font_size as CGFloat);
203 cx.show_glyphs_at_positions(
204 &[glyph_id as CGGlyph],
205 &[CGPoint::new(
206 (subpixel_shift.x() / scale_factor) as CGFloat,
207 (subpixel_shift.y() / scale_factor) as CGFloat,
208 )],
209 );
210
211 Some((bounds, pixels))
212 }
213 }
214
215 fn layout_line(&self, text: &str, font_size: f32, runs: &[(usize, RunStyle)]) -> 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, style)| {
228 let mut last_run = last_run.borrow_mut();
229 if let Some((last_len, last_font_id)) = last_run.as_mut() {
230 if style.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 = style.font_id;
237 Some(result)
238 }
239 } else {
240 *last_run = Some((*len, style.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 super::*;
416 use crate::MutableAppContext;
417 use font_kit::properties::{Style, Weight};
418 use platform::FontSystem as _;
419
420 #[crate::test(self, retries = 5)]
421 fn test_layout_str(_: &mut MutableAppContext) {
422 // This is failing intermittently on CI and we don't have time to figure it out
423 let fonts = FontSystem::new();
424 let menlo = fonts.load_family("Menlo").unwrap();
425 let menlo_regular = RunStyle {
426 font_id: fonts.select_font(&menlo, &Properties::new()).unwrap(),
427 color: Default::default(),
428 underline: None,
429 };
430 let menlo_italic = RunStyle {
431 font_id: fonts
432 .select_font(&menlo, &Properties::new().style(Style::Italic))
433 .unwrap(),
434 color: Default::default(),
435 underline: None,
436 };
437 let menlo_bold = RunStyle {
438 font_id: fonts
439 .select_font(&menlo, &Properties::new().weight(Weight::BOLD))
440 .unwrap(),
441 color: Default::default(),
442 underline: None,
443 };
444 assert_ne!(menlo_regular, menlo_italic);
445 assert_ne!(menlo_regular, menlo_bold);
446 assert_ne!(menlo_italic, menlo_bold);
447
448 let line = fonts.layout_line(
449 "hello world",
450 16.0,
451 &[(2, menlo_bold), (4, menlo_italic), (5, menlo_regular)],
452 );
453 assert_eq!(line.runs.len(), 3);
454 assert_eq!(line.runs[0].font_id, menlo_bold.font_id);
455 assert_eq!(line.runs[0].glyphs.len(), 2);
456 assert_eq!(line.runs[1].font_id, menlo_italic.font_id);
457 assert_eq!(line.runs[1].glyphs.len(), 4);
458 assert_eq!(line.runs[2].font_id, menlo_regular.font_id);
459 assert_eq!(line.runs[2].glyphs.len(), 5);
460 }
461
462 #[test]
463 fn test_glyph_offsets() -> anyhow::Result<()> {
464 let fonts = FontSystem::new();
465 let zapfino = fonts.load_family("Zapfino")?;
466 let zapfino_regular = RunStyle {
467 font_id: fonts.select_font(&zapfino, &Properties::new())?,
468 color: Default::default(),
469 underline: None,
470 };
471 let menlo = fonts.load_family("Menlo")?;
472 let menlo_regular = RunStyle {
473 font_id: fonts.select_font(&menlo, &Properties::new())?,
474 color: Default::default(),
475 underline: None,
476 };
477
478 let text = "This is, mπre πr less, Zapfino!π";
479 let line = fonts.layout_line(
480 text,
481 16.0,
482 &[
483 (9, zapfino_regular),
484 (13, menlo_regular),
485 (text.len() - 22, zapfino_regular),
486 ],
487 );
488 assert_eq!(
489 line.runs
490 .iter()
491 .flat_map(|r| r.glyphs.iter())
492 .map(|g| g.index)
493 .collect::<Vec<_>>(),
494 vec![0, 2, 4, 5, 7, 8, 9, 10, 14, 15, 16, 17, 21, 22, 23, 24, 26, 27, 28, 29, 36, 37],
495 );
496 Ok(())
497 }
498
499 #[test]
500 #[ignore]
501 fn test_rasterize_glyph() {
502 use std::{fs::File, io::BufWriter, path::Path};
503
504 let fonts = FontSystem::new();
505 let font_ids = fonts.load_family("Fira Code").unwrap();
506 let font_id = fonts.select_font(&font_ids, &Default::default()).unwrap();
507 let glyph_id = fonts.glyph_for_char(font_id, 'G').unwrap();
508
509 const VARIANTS: usize = 1;
510 for i in 0..VARIANTS {
511 let variant = i as f32 / VARIANTS as f32;
512 let (bounds, bytes) = fonts
513 .rasterize_glyph(font_id, 16.0, glyph_id, vec2f(variant, variant), 2.)
514 .unwrap();
515
516 let name = format!("/Users/as-cii/Desktop/twog-{}.png", i);
517 let path = Path::new(&name);
518 let file = File::create(path).unwrap();
519 let ref mut w = BufWriter::new(file);
520
521 let mut encoder = png::Encoder::new(w, bounds.width() as u32, bounds.height() as u32);
522 encoder.set_color(png::ColorType::Grayscale);
523 encoder.set_depth(png::BitDepth::Eight);
524 let mut writer = encoder.write_header().unwrap();
525 writer.write_image_data(&bytes).unwrap();
526 }
527 }
528
529 #[test]
530 fn test_wrap_line() {
531 let fonts = FontSystem::new();
532 let font_ids = fonts.load_family("Helvetica").unwrap();
533 let font_id = fonts.select_font(&font_ids, &Default::default()).unwrap();
534
535 let line = "one two three four five\n";
536 let wrap_boundaries = fonts.wrap_line(line, font_id, 16., 64.0);
537 assert_eq!(wrap_boundaries, &["one two ".len(), "one two three ".len()]);
538
539 let line = "aaa Ξ±Ξ±Ξ± βββ πππ\n";
540 let wrap_boundaries = fonts.wrap_line(line, font_id, 16., 64.0);
541 assert_eq!(
542 wrap_boundaries,
543 &["aaa Ξ±Ξ±Ξ± ".len(), "aaa Ξ±Ξ±Ξ± βββ ".len(),]
544 );
545 }
546
547 #[test]
548 fn test_layout_line_bom_char() {
549 let fonts = FontSystem::new();
550 let font_ids = fonts.load_family("Helvetica").unwrap();
551 let style = RunStyle {
552 font_id: fonts.select_font(&font_ids, &Default::default()).unwrap(),
553 color: Default::default(),
554 underline: None,
555 };
556
557 let line = "\u{feff}";
558 let layout = fonts.layout_line(line, 16., &[(line.len(), style)]);
559 assert_eq!(layout.len, line.len());
560 assert!(layout.runs.is_empty());
561
562 let line = "a\u{feff}b";
563 let layout = fonts.layout_line(line, 16., &[(line.len(), style)]);
564 assert_eq!(layout.len, line.len());
565 assert_eq!(layout.runs.len(), 1);
566 assert_eq!(layout.runs[0].glyphs.len(), 2);
567 assert_eq!(layout.runs[0].glyphs[0].id, 68); // a
568 // There's no glyph for \u{feff}
569 assert_eq!(layout.runs[0].glyphs[1].id, 69); // b
570 }
571}