fonts.rs

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