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