fonts.rs

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