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, 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 * params.scale_factor)
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        if params.subpixel_rendering {
353            // There seems to be a bug in Swash where the B and R values are swapped.
354            renderer
355                .format(Format::subpixel_bgra())
356                .offset(subpixel_offset);
357        } else {
358            renderer.format(Format::Alpha).offset(subpixel_offset);
359        }
360
361        let glyph_id: u16 = params.glyph_id.0.try_into()?;
362        renderer
363            .render(&mut scaler, glyph_id)
364            .with_context(|| format!("unable to render glyph via swash for {params:?}"))
365    }
366
367    /// This is used when cosmic_text has chosen a fallback font instead of using the requested
368    /// font, typically to handle some unicode characters. When this happens, `loaded_fonts` may not
369    /// yet have an entry for this fallback font, and so one is added.
370    ///
371    /// Note that callers shouldn't use this `FontId` somewhere that will retrieve the corresponding
372    /// `LoadedFont.features`, as it will have an arbitrarily chosen or empty value. The only
373    /// current use of this field is for the *input* of `layout_line`, and so it's fine to use
374    /// `font_id_for_cosmic_id` when computing the *output* of `layout_line`.
375    fn font_id_for_cosmic_id(&mut self, id: cosmic_text::fontdb::ID) -> FontId {
376        if let Some(ix) = self
377            .loaded_fonts
378            .iter()
379            .position(|loaded_font| loaded_font.font.id() == id)
380        {
381            FontId(ix)
382        } else {
383            let font = self.font_system.get_font(id).unwrap();
384            let face = self.font_system.db().face(id).unwrap();
385
386            let font_id = FontId(self.loaded_fonts.len());
387            self.loaded_fonts.push(LoadedFont {
388                font,
389                features: CosmicFontFeatures::new(),
390                is_known_emoji_font: check_is_known_emoji_font(&face.post_script_name),
391            });
392
393            font_id
394        }
395    }
396
397    #[profiling::function]
398    fn layout_line(&mut self, text: &str, font_size: Pixels, font_runs: &[FontRun]) -> LineLayout {
399        let mut attrs_list = AttrsList::new(&Attrs::new());
400        let mut offs = 0;
401        for run in font_runs {
402            let loaded_font = self.loaded_font(run.font_id);
403            let font = self.font_system.db().face(loaded_font.font.id()).unwrap();
404
405            attrs_list.add_span(
406                offs..(offs + run.len),
407                &Attrs::new()
408                    .metadata(run.font_id.0)
409                    .family(Family::Name(&font.families.first().unwrap().0))
410                    .stretch(font.stretch)
411                    .style(font.style)
412                    .weight(font.weight)
413                    .font_features(loaded_font.features.clone()),
414            );
415            offs += run.len;
416        }
417
418        let line = ShapeLine::new(
419            &mut self.font_system,
420            text,
421            &attrs_list,
422            cosmic_text::Shaping::Advanced,
423            4,
424        );
425        let mut layout_lines = Vec::with_capacity(1);
426        line.layout_to_buffer(
427            &mut self.scratch,
428            font_size.0,
429            None, // We do our own wrapping
430            cosmic_text::Wrap::None,
431            None,
432            &mut layout_lines,
433            None,
434        );
435        let layout = layout_lines.first().unwrap();
436
437        let mut runs: Vec<ShapedRun> = Vec::new();
438        for glyph in &layout.glyphs {
439            let mut font_id = FontId(glyph.metadata);
440            let mut loaded_font = self.loaded_font(font_id);
441            if loaded_font.font.id() != glyph.font_id {
442                font_id = self.font_id_for_cosmic_id(glyph.font_id);
443                loaded_font = self.loaded_font(font_id);
444            }
445            let is_emoji = loaded_font.is_known_emoji_font;
446
447            // HACK: Prevent crash caused by variation selectors.
448            if glyph.glyph_id == 3 && is_emoji {
449                continue;
450            }
451
452            let shaped_glyph = ShapedGlyph {
453                id: GlyphId(glyph.glyph_id as u32),
454                position: point(glyph.x.into(), glyph.y.into()),
455                index: glyph.start,
456                is_emoji,
457            };
458
459            if let Some(last_run) = runs
460                .last_mut()
461                .filter(|last_run| last_run.font_id == font_id)
462            {
463                last_run.glyphs.push(shaped_glyph);
464            } else {
465                runs.push(ShapedRun {
466                    font_id,
467                    glyphs: vec![shaped_glyph],
468                });
469            }
470        }
471
472        LineLayout {
473            font_size,
474            width: layout.w.into(),
475            ascent: layout.max_ascent.into(),
476            descent: layout.max_descent.into(),
477            runs,
478            len: text.len(),
479        }
480    }
481}
482
483impl TryFrom<&FontFeatures> for CosmicFontFeatures {
484    type Error = anyhow::Error;
485
486    fn try_from(features: &FontFeatures) -> Result<Self> {
487        let mut result = CosmicFontFeatures::new();
488        for feature in features.0.iter() {
489            let name_bytes: [u8; 4] = feature
490                .0
491                .as_bytes()
492                .try_into()
493                .context("Incorrect feature flag format")?;
494
495            let tag = cosmic_text::FeatureTag::new(&name_bytes);
496
497            result.set(tag, feature.1);
498        }
499        Ok(result)
500    }
501}
502
503impl From<RectF> for Bounds<f32> {
504    fn from(rect: RectF) -> Self {
505        Bounds {
506            origin: point(rect.origin_x(), rect.origin_y()),
507            size: size(rect.width(), rect.height()),
508        }
509    }
510}
511
512impl From<RectI> for Bounds<DevicePixels> {
513    fn from(rect: RectI) -> Self {
514        Bounds {
515            origin: point(DevicePixels(rect.origin_x()), DevicePixels(rect.origin_y())),
516            size: size(DevicePixels(rect.width()), DevicePixels(rect.height())),
517        }
518    }
519}
520
521impl From<Vector2I> for Size<DevicePixels> {
522    fn from(value: Vector2I) -> Self {
523        size(value.x().into(), value.y().into())
524    }
525}
526
527impl From<RectI> for Bounds<i32> {
528    fn from(rect: RectI) -> Self {
529        Bounds {
530            origin: point(rect.origin_x(), rect.origin_y()),
531            size: size(rect.width(), rect.height()),
532        }
533    }
534}
535
536impl From<Point<u32>> for Vector2I {
537    fn from(size: Point<u32>) -> Self {
538        Vector2I::new(size.x as i32, size.y as i32)
539    }
540}
541
542impl From<Vector2F> for Size<f32> {
543    fn from(vec: Vector2F) -> Self {
544        size(vec.x(), vec.y())
545    }
546}
547
548impl From<FontWeight> for cosmic_text::Weight {
549    fn from(value: FontWeight) -> Self {
550        cosmic_text::Weight(value.0 as u16)
551    }
552}
553
554impl From<FontStyle> for cosmic_text::Style {
555    fn from(style: FontStyle) -> Self {
556        match style {
557            FontStyle::Normal => cosmic_text::Style::Normal,
558            FontStyle::Italic => cosmic_text::Style::Italic,
559            FontStyle::Oblique => cosmic_text::Style::Oblique,
560        }
561    }
562}
563
564fn font_into_properties(font: &crate::Font) -> font_kit::properties::Properties {
565    font_kit::properties::Properties {
566        style: match font.style {
567            crate::FontStyle::Normal => font_kit::properties::Style::Normal,
568            crate::FontStyle::Italic => font_kit::properties::Style::Italic,
569            crate::FontStyle::Oblique => font_kit::properties::Style::Oblique,
570        },
571        weight: font_kit::properties::Weight(font.weight.0),
572        stretch: Default::default(),
573    }
574}
575
576fn face_info_into_properties(
577    face_info: &cosmic_text::fontdb::FaceInfo,
578) -> font_kit::properties::Properties {
579    font_kit::properties::Properties {
580        style: match face_info.style {
581            cosmic_text::Style::Normal => font_kit::properties::Style::Normal,
582            cosmic_text::Style::Italic => font_kit::properties::Style::Italic,
583            cosmic_text::Style::Oblique => font_kit::properties::Style::Oblique,
584        },
585        // both libs use the same values for weight
586        weight: font_kit::properties::Weight(face_info.weight.0.into()),
587        stretch: match face_info.stretch {
588            cosmic_text::Stretch::Condensed => font_kit::properties::Stretch::CONDENSED,
589            cosmic_text::Stretch::Expanded => font_kit::properties::Stretch::EXPANDED,
590            cosmic_text::Stretch::ExtraCondensed => font_kit::properties::Stretch::EXTRA_CONDENSED,
591            cosmic_text::Stretch::ExtraExpanded => font_kit::properties::Stretch::EXTRA_EXPANDED,
592            cosmic_text::Stretch::Normal => font_kit::properties::Stretch::NORMAL,
593            cosmic_text::Stretch::SemiCondensed => font_kit::properties::Stretch::SEMI_CONDENSED,
594            cosmic_text::Stretch::SemiExpanded => font_kit::properties::Stretch::SEMI_EXPANDED,
595            cosmic_text::Stretch::UltraCondensed => font_kit::properties::Stretch::ULTRA_CONDENSED,
596            cosmic_text::Stretch::UltraExpanded => font_kit::properties::Stretch::ULTRA_EXPANDED,
597        },
598    }
599}
600
601fn check_is_known_emoji_font(postscript_name: &str) -> bool {
602    // TODO: Include other common emoji fonts
603    postscript_name == "NotoColorEmoji"
604}