gpui-fast 0.1.20

gpui: GPUI with gpui-fast rendering and layout optimizations
Documentation
// Modified by Longbridge for gpui-fast.
//! Tests of resolving a font when neither it nor any fallback loads. See
//! [`crate::fast::text`].

use std::{borrow::Cow, sync::Arc};

use anyhow::anyhow;

use crate::{
    Bounds, DevicePixels, Font, FontId, FontMetrics, FontRun, GlyphId, LineLayout, NoopTextSystem,
    Pixels, PlatformTextSystem, RenderGlyphParams, Result, Size, TextRenderingMode, TextSystem,
    font,
};

/// The UI font, the only one the system loads.
const UI: FontId = FontId(0);

/// A system whose fonts all fail to load but the UI font's, as on a Mac whose
/// fonts on disk all have traits CoreText can't read.
struct OnlyUiFont;

impl PlatformTextSystem for OnlyUiFont {
    fn add_fonts(&self, _: Vec<Cow<'static, [u8]>>) -> Result<()> {
        Ok(())
    }

    fn all_font_names(&self) -> Vec<String> {
        Vec::new()
    }

    fn font_id(&self, font: &Font) -> Result<FontId> {
        if font.family.as_ref() == "UI" {
            Ok(UI)
        } else {
            Err(anyhow!("no font found"))
        }
    }

    fn font_metrics(&self, font_id: FontId) -> FontMetrics {
        NoopTextSystem.font_metrics(font_id)
    }

    fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Bounds<f32>> {
        NoopTextSystem.typographic_bounds(font_id, glyph_id)
    }

    fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>> {
        NoopTextSystem.advance(font_id, glyph_id)
    }

    fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
        NoopTextSystem.glyph_for_char(font_id, ch)
    }

    fn glyph_raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
        NoopTextSystem.glyph_raster_bounds(params)
    }

    fn rasterize_glyph(
        &self,
        params: &RenderGlyphParams,
        raster_bounds: Bounds<DevicePixels>,
    ) -> Result<(Size<DevicePixels>, Vec<u8>)> {
        NoopTextSystem.rasterize_glyph(params, raster_bounds)
    }

    fn layout_line(&self, text: &str, font_size: Pixels, runs: &[FontRun]) -> LineLayout {
        NoopTextSystem.layout_line(text, font_size, runs)
    }

    fn recommended_rendering_mode(&self, _: FontId, _: Pixels) -> TextRenderingMode {
        TextRenderingMode::Grayscale
    }
}

#[test]
fn a_font_with_no_fallback_that_loads_takes_a_font_that_resolved() {
    let text_system = TextSystem::new(Arc::new(OnlyUiFont));
    assert_eq!(text_system.resolve_font(&font("UI")), UI);

    let mut menlo = font("Menlo");
    menlo.features = crate::FontFeatures::disable_ligatures();
    assert_eq!(text_system.resolve_font(&menlo), UI);
    assert_eq!(text_system.resolve_font(&menlo), UI, "and keeps it");
}

#[test]
#[should_panic(expected = "failed to resolve font 'Menlo'")]
fn a_font_with_nothing_resolved_before_it_still_panics() {
    let text_system = TextSystem::new(Arc::new(OnlyUiFont));
    text_system.resolve_font(&font("Menlo"));
}