fonts.rs

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