text_system.rs

  1use crate::{
  2    Bounds, DevicePixels, Font, FontFeatures, FontId, FontMetrics, FontRun, FontStyle, FontWeight,
  3    GlyphId, LineLayout, Pixels, PlatformTextSystem, Point, RenderGlyphParams, SUBPIXEL_VARIANTS_X,
  4    SUBPIXEL_VARIANTS_Y, ShapedGlyph, ShapedRun, SharedString, Size, TextRenderingMode, point,
  5    size,
  6};
  7use anyhow::{Context as _, Ok, Result};
  8use collections::HashMap;
  9use cosmic_text::{
 10    Attrs, AttrsList, Family, Font as CosmicTextFont, FontFeatures as CosmicFontFeatures,
 11    FontSystem, ShapeBuffer, ShapeLine,
 12};
 13
 14use itertools::Itertools;
 15use parking_lot::RwLock;
 16use pathfinder_geometry::{
 17    rect::{RectF, RectI},
 18    vector::{Vector2F, Vector2I},
 19};
 20use smallvec::SmallVec;
 21use std::{borrow::Cow, sync::Arc};
 22use swash::{
 23    scale::{Render, ScaleContext, Source, StrikeWith},
 24    zeno::{Format, Transform, Vector},
 25};
 26
 27pub(crate) struct CosmicTextSystem(RwLock<CosmicTextSystemState>);
 28
 29#[derive(Debug, Clone, PartialEq, Eq, Hash)]
 30struct FontKey {
 31    family: SharedString,
 32    features: FontFeatures,
 33}
 34
 35impl FontKey {
 36    fn new(family: SharedString, features: FontFeatures) -> Self {
 37        Self { family, features }
 38    }
 39}
 40
 41struct CosmicTextSystemState {
 42    font_system: FontSystem,
 43    scratch: ShapeBuffer,
 44    swash_scale_context: ScaleContext,
 45    /// Contains all already loaded fonts, including all faces. Indexed by `FontId`.
 46    loaded_fonts: Vec<LoadedFont>,
 47    /// Caches the `FontId`s associated with a specific family to avoid iterating the font database
 48    /// for every font face in a family.
 49    font_ids_by_family_cache: HashMap<FontKey, SmallVec<[FontId; 4]>>,
 50}
 51
 52struct LoadedFont {
 53    font: Arc<CosmicTextFont>,
 54    features: CosmicFontFeatures,
 55    is_known_emoji_font: bool,
 56}
 57
 58impl CosmicTextSystem {
 59    pub(crate) fn new() -> Self {
 60        // todo(linux) make font loading non-blocking
 61        let mut font_system = FontSystem::new();
 62
 63        Self(RwLock::new(CosmicTextSystemState {
 64            font_system,
 65            scratch: ShapeBuffer::default(),
 66            swash_scale_context: ScaleContext::new(),
 67            loaded_fonts: Vec::new(),
 68            font_ids_by_family_cache: HashMap::default(),
 69        }))
 70    }
 71}
 72
 73impl Default for CosmicTextSystem {
 74    fn default() -> Self {
 75        Self::new()
 76    }
 77}
 78
 79impl PlatformTextSystem for CosmicTextSystem {
 80    fn add_fonts(&self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()> {
 81        self.0.write().add_fonts(fonts)
 82    }
 83
 84    fn all_font_names(&self) -> Vec<String> {
 85        let mut result = self
 86            .0
 87            .read()
 88            .font_system
 89            .db()
 90            .faces()
 91            .filter_map(|face| face.families.first().map(|family| family.0.clone()))
 92            .collect_vec();
 93        result.sort();
 94        result.dedup();
 95        result
 96    }
 97
 98    fn font_id(&self, font: &Font) -> Result<FontId> {
 99        // todo(linux): Do we need to use CosmicText's Font APIs? Can we consolidate this to use font_kit?
100        let mut state = self.0.write();
101        let key = FontKey::new(font.family.clone(), font.features.clone());
102        let candidates = if let Some(font_ids) = state.font_ids_by_family_cache.get(&key) {
103            font_ids.as_slice()
104        } else {
105            let font_ids = state.load_family(&font.family, &font.features)?;
106            state.font_ids_by_family_cache.insert(key.clone(), font_ids);
107            state.font_ids_by_family_cache[&key].as_ref()
108        };
109
110        // todo(linux) ideally we would make fontdb's `find_best_match` pub instead of using font-kit here
111        let candidate_properties = candidates
112            .iter()
113            .map(|font_id| {
114                let database_id = state.loaded_font(*font_id).font.id();
115                let face_info = state.font_system.db().face(database_id).expect("");
116                face_info_into_properties(face_info)
117            })
118            .collect::<SmallVec<[_; 4]>>();
119
120        let ix =
121            font_kit::matching::find_best_match(&candidate_properties, &font_into_properties(font))
122                .context("requested font family contains no font matching the other parameters")?;
123
124        Ok(candidates[ix])
125    }
126
127    fn font_metrics(&self, font_id: FontId) -> FontMetrics {
128        let metrics = self
129            .0
130            .read()
131            .loaded_font(font_id)
132            .font
133            .as_swash()
134            .metrics(&[]);
135
136        FontMetrics {
137            units_per_em: metrics.units_per_em as u32,
138            ascent: metrics.ascent,
139            descent: -metrics.descent, // todo(linux) confirm this is correct
140            line_gap: metrics.leading,
141            underline_position: metrics.underline_offset,
142            underline_thickness: metrics.stroke_size,
143            cap_height: metrics.cap_height,
144            x_height: metrics.x_height,
145            // todo(linux): Compute this correctly
146            bounding_box: Bounds {
147                origin: point(0.0, 0.0),
148                size: size(metrics.max_width, metrics.ascent + metrics.descent),
149            },
150        }
151    }
152
153    fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Bounds<f32>> {
154        let lock = self.0.read();
155        let glyph_metrics = lock.loaded_font(font_id).font.as_swash().glyph_metrics(&[]);
156        let glyph_id = glyph_id.0 as u16;
157        // todo(linux): Compute this correctly
158        // see https://github.com/servo/font-kit/blob/master/src/loaders/freetype.rs#L614-L620
159        Ok(Bounds {
160            origin: point(0.0, 0.0),
161            size: size(
162                glyph_metrics.advance_width(glyph_id),
163                glyph_metrics.advance_height(glyph_id),
164            ),
165        })
166    }
167
168    fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>> {
169        self.0.read().advance(font_id, glyph_id)
170    }
171
172    fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
173        self.0.read().glyph_for_char(font_id, ch)
174    }
175
176    fn glyph_raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
177        self.0.write().raster_bounds(params)
178    }
179
180    fn rasterize_glyph(
181        &self,
182        params: &RenderGlyphParams,
183        raster_bounds: Bounds<DevicePixels>,
184    ) -> Result<(Size<DevicePixels>, Vec<u8>)> {
185        self.0.write().rasterize_glyph(params, raster_bounds)
186    }
187
188    fn layout_line(&self, text: &str, font_size: Pixels, runs: &[FontRun]) -> LineLayout {
189        self.0.write().layout_line(text, font_size, runs)
190    }
191
192    fn recommended_rendering_mode(
193        &self,
194        _font_id: FontId,
195        _font_size: Pixels,
196    ) -> TextRenderingMode {
197        // Ideally, we'd use fontconfig to read the user preference.
198        TextRenderingMode::Subpixel
199    }
200}
201
202impl CosmicTextSystemState {
203    fn loaded_font(&self, font_id: FontId) -> &LoadedFont {
204        &self.loaded_fonts[font_id.0]
205    }
206
207    #[profiling::function]
208    fn add_fonts(&mut self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()> {
209        let db = self.font_system.db_mut();
210        for bytes in fonts {
211            match bytes {
212                Cow::Borrowed(embedded_font) => {
213                    db.load_font_data(embedded_font.to_vec());
214                }
215                Cow::Owned(bytes) => {
216                    db.load_font_data(bytes);
217                }
218            }
219        }
220        Ok(())
221    }
222
223    #[profiling::function]
224    fn load_family(
225        &mut self,
226        name: &str,
227        features: &FontFeatures,
228    ) -> Result<SmallVec<[FontId; 4]>> {
229        // TODO: Determine the proper system UI font.
230        let name = crate::text_system::font_name_with_fallbacks(name, "IBM Plex Sans");
231
232        let families = self
233            .font_system
234            .db()
235            .faces()
236            .filter(|face| face.families.iter().any(|family| *name == family.0))
237            .map(|face| (face.id, face.post_script_name.clone()))
238            .collect::<SmallVec<[_; 4]>>();
239
240        let mut loaded_font_ids = SmallVec::new();
241        for (font_id, postscript_name) in families {
242            let font = self
243                .font_system
244                .get_font(font_id)
245                .context("Could not load font")?;
246
247            // HACK: To let the storybook run and render Windows caption icons. We should actually do better font fallback.
248            let allowed_bad_font_names = [
249                "SegoeFluentIcons", // NOTE: Segoe fluent icons postscript name is inconsistent
250                "Segoe Fluent Icons",
251            ];
252
253            if font.as_swash().charmap().map('m') == 0
254                && !allowed_bad_font_names.contains(&postscript_name.as_str())
255            {
256                self.font_system.db_mut().remove_face(font.id());
257                continue;
258            };
259
260            let font_id = FontId(self.loaded_fonts.len());
261            loaded_font_ids.push(font_id);
262            self.loaded_fonts.push(LoadedFont {
263                font,
264                features: features.try_into()?,
265                is_known_emoji_font: check_is_known_emoji_font(&postscript_name),
266            });
267        }
268
269        Ok(loaded_font_ids)
270    }
271
272    fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>> {
273        let glyph_metrics = self.loaded_font(font_id).font.as_swash().glyph_metrics(&[]);
274        Ok(Size {
275            width: glyph_metrics.advance_width(glyph_id.0 as u16),
276            height: glyph_metrics.advance_height(glyph_id.0 as u16),
277        })
278    }
279
280    fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
281        let glyph_id = self.loaded_font(font_id).font.as_swash().charmap().map(ch);
282        if glyph_id == 0 {
283            None
284        } else {
285            Some(GlyphId(glyph_id.into()))
286        }
287    }
288
289    fn raster_bounds(&mut self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
290        let image = self.render_glyph_image(params)?;
291        Ok(Bounds {
292            origin: point(image.placement.left.into(), (-image.placement.top).into()),
293            size: size(image.placement.width.into(), image.placement.height.into()),
294        })
295    }
296
297    #[profiling::function]
298    fn rasterize_glyph(
299        &mut self,
300        params: &RenderGlyphParams,
301        glyph_bounds: Bounds<DevicePixels>,
302    ) -> Result<(Size<DevicePixels>, Vec<u8>)> {
303        if glyph_bounds.size.width.0 == 0 || glyph_bounds.size.height.0 == 0 {
304            anyhow::bail!("glyph bounds are empty");
305        }
306
307        let mut image = self.render_glyph_image(params)?;
308        let bitmap_size = glyph_bounds.size;
309        match image.content {
310            swash::scale::image::Content::Color | swash::scale::image::Content::SubpixelMask => {
311                // Convert from RGBA to BGRA.
312                for pixel in image.data.chunks_exact_mut(4) {
313                    pixel.swap(0, 2);
314                }
315                Ok((bitmap_size, image.data))
316            }
317            swash::scale::image::Content::Mask => Ok((bitmap_size, image.data)),
318        }
319    }
320
321    fn render_glyph_image(
322        &mut self,
323        params: &RenderGlyphParams,
324    ) -> Result<swash::scale::image::Image> {
325        let loaded_font = &self.loaded_fonts[params.font_id.0];
326        let font_ref = loaded_font.font.as_swash();
327        let pixel_size = params.font_size.0;
328
329        let subpixel_offset = Vector::new(
330            params.subpixel_variant.x as f32 / SUBPIXEL_VARIANTS_X as f32 / params.scale_factor,
331            params.subpixel_variant.y as f32 / SUBPIXEL_VARIANTS_Y as f32 / params.scale_factor,
332        );
333
334        let mut scaler = self
335            .swash_scale_context
336            .builder(font_ref)
337            .size(pixel_size)
338            .hint(true)
339            .build();
340
341        let sources: &[Source] = if params.is_emoji {
342            &[
343                Source::ColorOutline(0),
344                Source::ColorBitmap(StrikeWith::BestFit),
345                Source::Outline,
346            ]
347        } else {
348            &[Source::Outline]
349        };
350
351        let mut renderer = Render::new(sources);
352        renderer.transform(Some(Transform {
353            xx: params.scale_factor,
354            xy: 0.0,
355            yx: 0.0,
356            yy: params.scale_factor,
357            x: 0.0,
358            y: 0.0,
359        }));
360
361        if params.subpixel_rendering {
362            // There seems to be a bug in Swash where the B and R values are swapped.
363            renderer
364                .format(Format::subpixel_bgra())
365                .offset(subpixel_offset);
366        } else {
367            renderer.format(Format::Alpha).offset(subpixel_offset);
368        }
369
370        let glyph_id: u16 = params.glyph_id.0.try_into()?;
371        renderer
372            .render(&mut scaler, glyph_id)
373            .with_context(|| format!("unable to render glyph via swash for {params:?}"))
374    }
375
376    /// This is used when cosmic_text has chosen a fallback font instead of using the requested
377    /// font, typically to handle some unicode characters. When this happens, `loaded_fonts` may not
378    /// yet have an entry for this fallback font, and so one is added.
379    ///
380    /// Note that callers shouldn't use this `FontId` somewhere that will retrieve the corresponding
381    /// `LoadedFont.features`, as it will have an arbitrarily chosen or empty value. The only
382    /// current use of this field is for the *input* of `layout_line`, and so it's fine to use
383    /// `font_id_for_cosmic_id` when computing the *output* of `layout_line`.
384    fn font_id_for_cosmic_id(&mut self, id: cosmic_text::fontdb::ID) -> FontId {
385        if let Some(ix) = self
386            .loaded_fonts
387            .iter()
388            .position(|loaded_font| loaded_font.font.id() == id)
389        {
390            FontId(ix)
391        } else {
392            let font = self.font_system.get_font(id).unwrap();
393            let face = self.font_system.db().face(id).unwrap();
394
395            let font_id = FontId(self.loaded_fonts.len());
396            self.loaded_fonts.push(LoadedFont {
397                font,
398                features: CosmicFontFeatures::new(),
399                is_known_emoji_font: check_is_known_emoji_font(&face.post_script_name),
400            });
401
402            font_id
403        }
404    }
405
406    #[profiling::function]
407    fn layout_line(&mut self, text: &str, font_size: Pixels, font_runs: &[FontRun]) -> LineLayout {
408        let mut attrs_list = AttrsList::new(&Attrs::new());
409        let mut offs = 0;
410        for run in font_runs {
411            let loaded_font = self.loaded_font(run.font_id);
412            let font = self.font_system.db().face(loaded_font.font.id()).unwrap();
413
414            attrs_list.add_span(
415                offs..(offs + run.len),
416                &Attrs::new()
417                    .metadata(run.font_id.0)
418                    .family(Family::Name(&font.families.first().unwrap().0))
419                    .stretch(font.stretch)
420                    .style(font.style)
421                    .weight(font.weight)
422                    .font_features(loaded_font.features.clone()),
423            );
424            offs += run.len;
425        }
426
427        let line = ShapeLine::new(
428            &mut self.font_system,
429            text,
430            &attrs_list,
431            cosmic_text::Shaping::Advanced,
432            4,
433        );
434        let mut layout_lines = Vec::with_capacity(1);
435        line.layout_to_buffer(
436            &mut self.scratch,
437            font_size.0,
438            None, // We do our own wrapping
439            cosmic_text::Wrap::None,
440            None,
441            &mut layout_lines,
442            None,
443        );
444        let layout = layout_lines.first().unwrap();
445
446        let mut runs: Vec<ShapedRun> = Vec::new();
447        for glyph in &layout.glyphs {
448            let mut font_id = FontId(glyph.metadata);
449            let mut loaded_font = self.loaded_font(font_id);
450            if loaded_font.font.id() != glyph.font_id {
451                font_id = self.font_id_for_cosmic_id(glyph.font_id);
452                loaded_font = self.loaded_font(font_id);
453            }
454            let is_emoji = loaded_font.is_known_emoji_font;
455
456            // HACK: Prevent crash caused by variation selectors.
457            if glyph.glyph_id == 3 && is_emoji {
458                continue;
459            }
460
461            let shaped_glyph = ShapedGlyph {
462                id: GlyphId(glyph.glyph_id as u32),
463                position: point(glyph.x.into(), glyph.y.into()),
464                index: glyph.start,
465                is_emoji,
466            };
467
468            if let Some(last_run) = runs
469                .last_mut()
470                .filter(|last_run| last_run.font_id == font_id)
471            {
472                last_run.glyphs.push(shaped_glyph);
473            } else {
474                runs.push(ShapedRun {
475                    font_id,
476                    glyphs: vec![shaped_glyph],
477                });
478            }
479        }
480
481        LineLayout {
482            font_size,
483            width: layout.w.into(),
484            ascent: layout.max_ascent.into(),
485            descent: layout.max_descent.into(),
486            runs,
487            len: text.len(),
488        }
489    }
490}
491
492impl TryFrom<&FontFeatures> for CosmicFontFeatures {
493    type Error = anyhow::Error;
494
495    fn try_from(features: &FontFeatures) -> Result<Self> {
496        let mut result = CosmicFontFeatures::new();
497        for feature in features.0.iter() {
498            let name_bytes: [u8; 4] = feature
499                .0
500                .as_bytes()
501                .try_into()
502                .context("Incorrect feature flag format")?;
503
504            let tag = cosmic_text::FeatureTag::new(&name_bytes);
505
506            result.set(tag, feature.1);
507        }
508        Ok(result)
509    }
510}
511
512impl From<RectF> for Bounds<f32> {
513    fn from(rect: RectF) -> Self {
514        Bounds {
515            origin: point(rect.origin_x(), rect.origin_y()),
516            size: size(rect.width(), rect.height()),
517        }
518    }
519}
520
521impl From<RectI> for Bounds<DevicePixels> {
522    fn from(rect: RectI) -> Self {
523        Bounds {
524            origin: point(DevicePixels(rect.origin_x()), DevicePixels(rect.origin_y())),
525            size: size(DevicePixels(rect.width()), DevicePixels(rect.height())),
526        }
527    }
528}
529
530impl From<Vector2I> for Size<DevicePixels> {
531    fn from(value: Vector2I) -> Self {
532        size(value.x().into(), value.y().into())
533    }
534}
535
536impl From<RectI> for Bounds<i32> {
537    fn from(rect: RectI) -> Self {
538        Bounds {
539            origin: point(rect.origin_x(), rect.origin_y()),
540            size: size(rect.width(), rect.height()),
541        }
542    }
543}
544
545impl From<Point<u32>> for Vector2I {
546    fn from(size: Point<u32>) -> Self {
547        Vector2I::new(size.x as i32, size.y as i32)
548    }
549}
550
551impl From<Vector2F> for Size<f32> {
552    fn from(vec: Vector2F) -> Self {
553        size(vec.x(), vec.y())
554    }
555}
556
557impl From<FontWeight> for cosmic_text::Weight {
558    fn from(value: FontWeight) -> Self {
559        cosmic_text::Weight(value.0 as u16)
560    }
561}
562
563impl From<FontStyle> for cosmic_text::Style {
564    fn from(style: FontStyle) -> Self {
565        match style {
566            FontStyle::Normal => cosmic_text::Style::Normal,
567            FontStyle::Italic => cosmic_text::Style::Italic,
568            FontStyle::Oblique => cosmic_text::Style::Oblique,
569        }
570    }
571}
572
573fn font_into_properties(font: &crate::Font) -> font_kit::properties::Properties {
574    font_kit::properties::Properties {
575        style: match font.style {
576            crate::FontStyle::Normal => font_kit::properties::Style::Normal,
577            crate::FontStyle::Italic => font_kit::properties::Style::Italic,
578            crate::FontStyle::Oblique => font_kit::properties::Style::Oblique,
579        },
580        weight: font_kit::properties::Weight(font.weight.0),
581        stretch: Default::default(),
582    }
583}
584
585fn face_info_into_properties(
586    face_info: &cosmic_text::fontdb::FaceInfo,
587) -> font_kit::properties::Properties {
588    font_kit::properties::Properties {
589        style: match face_info.style {
590            cosmic_text::Style::Normal => font_kit::properties::Style::Normal,
591            cosmic_text::Style::Italic => font_kit::properties::Style::Italic,
592            cosmic_text::Style::Oblique => font_kit::properties::Style::Oblique,
593        },
594        // both libs use the same values for weight
595        weight: font_kit::properties::Weight(face_info.weight.0.into()),
596        stretch: match face_info.stretch {
597            cosmic_text::Stretch::Condensed => font_kit::properties::Stretch::CONDENSED,
598            cosmic_text::Stretch::Expanded => font_kit::properties::Stretch::EXPANDED,
599            cosmic_text::Stretch::ExtraCondensed => font_kit::properties::Stretch::EXTRA_CONDENSED,
600            cosmic_text::Stretch::ExtraExpanded => font_kit::properties::Stretch::EXTRA_EXPANDED,
601            cosmic_text::Stretch::Normal => font_kit::properties::Stretch::NORMAL,
602            cosmic_text::Stretch::SemiCondensed => font_kit::properties::Stretch::SEMI_CONDENSED,
603            cosmic_text::Stretch::SemiExpanded => font_kit::properties::Stretch::SEMI_EXPANDED,
604            cosmic_text::Stretch::UltraCondensed => font_kit::properties::Stretch::ULTRA_CONDENSED,
605            cosmic_text::Stretch::UltraExpanded => font_kit::properties::Stretch::ULTRA_EXPANDED,
606        },
607    }
608}
609
610fn check_is_known_emoji_font(postscript_name: &str) -> bool {
611    // TODO: Include other common emoji fonts
612    postscript_name == "NotoColorEmoji"
613}