text_system.rs

  1use crate::{
  2    Bounds, DevicePixels, Font, FontFeatures, FontId, FontMetrics, FontRun, FontStyle, FontWeight,
  3    GlyphId, LineLayout, Pixels, PlatformTextSystem, Point, RenderGlyphParams, SUBPIXEL_VARIANTS,
  4    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 = params
278            .subpixel_variant
279            .map(|v| v as f32 / (SUBPIXEL_VARIANTS as f32 * params.scale_factor));
280        let image = self
281            .swash_cache
282            .get_image(
283                &mut self.font_system,
284                CacheKey::new(
285                    font.id(),
286                    params.glyph_id.0 as u16,
287                    (params.font_size * params.scale_factor).into(),
288                    (subpixel_shift.x, subpixel_shift.y.trunc()),
289                    cosmic_text::CacheKeyFlags::empty(),
290                )
291                .0,
292            )
293            .clone()
294            .with_context(|| format!("no image for {params:?} in font {font:?}"))?;
295        Ok(Bounds {
296            origin: point(image.placement.left.into(), (-image.placement.top).into()),
297            size: size(image.placement.width.into(), image.placement.height.into()),
298        })
299    }
300
301    #[profiling::function]
302    fn rasterize_glyph(
303        &mut self,
304        params: &RenderGlyphParams,
305        glyph_bounds: Bounds<DevicePixels>,
306    ) -> Result<(Size<DevicePixels>, Vec<u8>)> {
307        if glyph_bounds.size.width.0 == 0 || glyph_bounds.size.height.0 == 0 {
308            anyhow::bail!("glyph bounds are empty");
309        } else {
310            let bitmap_size = glyph_bounds.size;
311            let font = &self.loaded_fonts[params.font_id.0].font;
312            let subpixel_shift = params
313                .subpixel_variant
314                .map(|v| v as f32 / (SUBPIXEL_VARIANTS as f32 * params.scale_factor));
315            let mut image = self
316                .swash_cache
317                .get_image(
318                    &mut self.font_system,
319                    CacheKey::new(
320                        font.id(),
321                        params.glyph_id.0 as u16,
322                        (params.font_size * params.scale_factor).into(),
323                        (subpixel_shift.x, subpixel_shift.y.trunc()),
324                        cosmic_text::CacheKeyFlags::empty(),
325                    )
326                    .0,
327                )
328                .clone()
329                .with_context(|| format!("no image for {params:?} in font {font:?}"))?;
330
331            if params.is_emoji {
332                // Convert from RGBA to BGRA.
333                for pixel in image.data.chunks_exact_mut(4) {
334                    pixel.swap(0, 2);
335                }
336            }
337
338            Ok((bitmap_size, image.data))
339        }
340    }
341
342    /// This is used when cosmic_text has chosen a fallback font instead of using the requested
343    /// font, typically to handle some unicode characters. When this happens, `loaded_fonts` may not
344    /// yet have an entry for this fallback font, and so one is added.
345    ///
346    /// Note that callers shouldn't use this `FontId` somewhere that will retrieve the corresponding
347    /// `LoadedFont.features`, as it will have an arbitrarily chosen or empty value. The only
348    /// current use of this field is for the *input* of `layout_line`, and so it's fine to use
349    /// `font_id_for_cosmic_id` when computing the *output* of `layout_line`.
350    fn font_id_for_cosmic_id(&mut self, id: cosmic_text::fontdb::ID) -> FontId {
351        if let Some(ix) = self
352            .loaded_fonts
353            .iter()
354            .position(|loaded_font| loaded_font.font.id() == id)
355        {
356            FontId(ix)
357        } else {
358            let font = self.font_system.get_font(id).unwrap();
359            let face = self.font_system.db().face(id).unwrap();
360
361            let font_id = FontId(self.loaded_fonts.len());
362            self.loaded_fonts.push(LoadedFont {
363                font,
364                features: CosmicFontFeatures::new(),
365                is_known_emoji_font: check_is_known_emoji_font(&face.post_script_name),
366            });
367
368            font_id
369        }
370    }
371
372    #[profiling::function]
373    fn layout_line(&mut self, text: &str, font_size: Pixels, font_runs: &[FontRun]) -> LineLayout {
374        let mut attrs_list = AttrsList::new(&Attrs::new());
375        let mut offs = 0;
376        for run in font_runs {
377            let loaded_font = self.loaded_font(run.font_id);
378            let font = self.font_system.db().face(loaded_font.font.id()).unwrap();
379
380            attrs_list.add_span(
381                offs..(offs + run.len),
382                &Attrs::new()
383                    .metadata(run.font_id.0)
384                    .family(Family::Name(&font.families.first().unwrap().0))
385                    .stretch(font.stretch)
386                    .style(font.style)
387                    .weight(font.weight)
388                    .font_features(loaded_font.features.clone()),
389            );
390            offs += run.len;
391        }
392
393        let line = ShapeLine::new(
394            &mut self.font_system,
395            text,
396            &attrs_list,
397            cosmic_text::Shaping::Advanced,
398            4,
399        );
400        let mut layout_lines = Vec::with_capacity(1);
401        line.layout_to_buffer(
402            &mut self.scratch,
403            font_size.0,
404            None, // We do our own wrapping
405            cosmic_text::Wrap::None,
406            None,
407            &mut layout_lines,
408            None,
409        );
410        let layout = layout_lines.first().unwrap();
411
412        let mut runs: Vec<ShapedRun> = Vec::new();
413        for glyph in &layout.glyphs {
414            let mut font_id = FontId(glyph.metadata);
415            let mut loaded_font = self.loaded_font(font_id);
416            if loaded_font.font.id() != glyph.font_id {
417                font_id = self.font_id_for_cosmic_id(glyph.font_id);
418                loaded_font = self.loaded_font(font_id);
419            }
420            let is_emoji = loaded_font.is_known_emoji_font;
421
422            // HACK: Prevent crash caused by variation selectors.
423            if glyph.glyph_id == 3 && is_emoji {
424                continue;
425            }
426
427            let shaped_glyph = ShapedGlyph {
428                id: GlyphId(glyph.glyph_id as u32),
429                position: point(glyph.x.into(), glyph.y.into()),
430                index: glyph.start,
431                is_emoji,
432            };
433
434            if let Some(last_run) = runs
435                .last_mut()
436                .filter(|last_run| last_run.font_id == font_id)
437            {
438                last_run.glyphs.push(shaped_glyph);
439            } else {
440                runs.push(ShapedRun {
441                    font_id,
442                    glyphs: vec![shaped_glyph],
443                });
444            }
445        }
446
447        LineLayout {
448            font_size,
449            width: layout.w.into(),
450            ascent: layout.max_ascent.into(),
451            descent: layout.max_descent.into(),
452            runs,
453            len: text.len(),
454        }
455    }
456}
457
458impl TryFrom<&FontFeatures> for CosmicFontFeatures {
459    type Error = anyhow::Error;
460
461    fn try_from(features: &FontFeatures) -> Result<Self> {
462        let mut result = CosmicFontFeatures::new();
463        for feature in features.0.iter() {
464            let name_bytes: [u8; 4] = feature
465                .0
466                .as_bytes()
467                .try_into()
468                .context("Incorrect feature flag format")?;
469
470            let tag = cosmic_text::FeatureTag::new(&name_bytes);
471
472            result.set(tag, feature.1);
473        }
474        Ok(result)
475    }
476}
477
478impl From<RectF> for Bounds<f32> {
479    fn from(rect: RectF) -> Self {
480        Bounds {
481            origin: point(rect.origin_x(), rect.origin_y()),
482            size: size(rect.width(), rect.height()),
483        }
484    }
485}
486
487impl From<RectI> for Bounds<DevicePixels> {
488    fn from(rect: RectI) -> Self {
489        Bounds {
490            origin: point(DevicePixels(rect.origin_x()), DevicePixels(rect.origin_y())),
491            size: size(DevicePixels(rect.width()), DevicePixels(rect.height())),
492        }
493    }
494}
495
496impl From<Vector2I> for Size<DevicePixels> {
497    fn from(value: Vector2I) -> Self {
498        size(value.x().into(), value.y().into())
499    }
500}
501
502impl From<RectI> for Bounds<i32> {
503    fn from(rect: RectI) -> Self {
504        Bounds {
505            origin: point(rect.origin_x(), rect.origin_y()),
506            size: size(rect.width(), rect.height()),
507        }
508    }
509}
510
511impl From<Point<u32>> for Vector2I {
512    fn from(size: Point<u32>) -> Self {
513        Vector2I::new(size.x as i32, size.y as i32)
514    }
515}
516
517impl From<Vector2F> for Size<f32> {
518    fn from(vec: Vector2F) -> Self {
519        size(vec.x(), vec.y())
520    }
521}
522
523impl From<FontWeight> for cosmic_text::Weight {
524    fn from(value: FontWeight) -> Self {
525        cosmic_text::Weight(value.0 as u16)
526    }
527}
528
529impl From<FontStyle> for cosmic_text::Style {
530    fn from(style: FontStyle) -> Self {
531        match style {
532            FontStyle::Normal => cosmic_text::Style::Normal,
533            FontStyle::Italic => cosmic_text::Style::Italic,
534            FontStyle::Oblique => cosmic_text::Style::Oblique,
535        }
536    }
537}
538
539fn font_into_properties(font: &crate::Font) -> font_kit::properties::Properties {
540    font_kit::properties::Properties {
541        style: match font.style {
542            crate::FontStyle::Normal => font_kit::properties::Style::Normal,
543            crate::FontStyle::Italic => font_kit::properties::Style::Italic,
544            crate::FontStyle::Oblique => font_kit::properties::Style::Oblique,
545        },
546        weight: font_kit::properties::Weight(font.weight.0),
547        stretch: Default::default(),
548    }
549}
550
551fn face_info_into_properties(
552    face_info: &cosmic_text::fontdb::FaceInfo,
553) -> font_kit::properties::Properties {
554    font_kit::properties::Properties {
555        style: match face_info.style {
556            cosmic_text::Style::Normal => font_kit::properties::Style::Normal,
557            cosmic_text::Style::Italic => font_kit::properties::Style::Italic,
558            cosmic_text::Style::Oblique => font_kit::properties::Style::Oblique,
559        },
560        // both libs use the same values for weight
561        weight: font_kit::properties::Weight(face_info.weight.0.into()),
562        stretch: match face_info.stretch {
563            cosmic_text::Stretch::Condensed => font_kit::properties::Stretch::CONDENSED,
564            cosmic_text::Stretch::Expanded => font_kit::properties::Stretch::EXPANDED,
565            cosmic_text::Stretch::ExtraCondensed => font_kit::properties::Stretch::EXTRA_CONDENSED,
566            cosmic_text::Stretch::ExtraExpanded => font_kit::properties::Stretch::EXTRA_EXPANDED,
567            cosmic_text::Stretch::Normal => font_kit::properties::Stretch::NORMAL,
568            cosmic_text::Stretch::SemiCondensed => font_kit::properties::Stretch::SEMI_CONDENSED,
569            cosmic_text::Stretch::SemiExpanded => font_kit::properties::Stretch::SEMI_EXPANDED,
570            cosmic_text::Stretch::UltraCondensed => font_kit::properties::Stretch::ULTRA_CONDENSED,
571            cosmic_text::Stretch::UltraExpanded => font_kit::properties::Stretch::ULTRA_EXPANDED,
572        },
573    }
574}
575
576fn check_is_known_emoji_font(postscript_name: &str) -> bool {
577    // TODO: Include other common emoji fonts
578    postscript_name == "NotoColorEmoji"
579}