Use NoopTextSystem during tests (#28607)

This should allow tests to be more similar across platforms.

Release Notes:

- N/A
This commit is contained in:
Conrad Irwin 2025-04-11 16:26:41 -06:00 committed by GitHub
parent 97a9a5de10
commit 5994ac5cec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 97 additions and 54 deletions

View file

@ -37,9 +37,10 @@ use crate::{
DEFAULT_WINDOW_SIZE, DevicePixels, DispatchEventResult, Font, FontId, FontMetrics, FontRun,
ForegroundExecutor, GlyphId, GpuSpecs, ImageSource, Keymap, LineLayout, Pixels, PlatformInput,
Point, RenderGlyphParams, RenderImage, RenderImageParams, RenderSvgParams, ScaledPixels, Scene,
SharedString, Size, SvgRenderer, SvgSize, Task, TaskLabel, Window, point,
ShapedGlyph, ShapedRun, SharedString, Size, SvgRenderer, SvgSize, Task, TaskLabel, Window,
point, px, size,
};
use anyhow::{Result, anyhow};
use anyhow::Result;
use async_task::Runnable;
use futures::channel::oneshot;
use image::codecs::gif::GifDecoder;
@ -532,40 +533,105 @@ impl PlatformTextSystem for NoopTextSystem {
Vec::new()
}
fn font_id(&self, descriptor: &Font) -> Result<FontId> {
Err(anyhow!("No font found for {:?}", descriptor))
fn font_id(&self, _descriptor: &Font) -> Result<FontId> {
return Ok(FontId(1));
}
fn font_metrics(&self, _font_id: FontId) -> FontMetrics {
unimplemented!()
FontMetrics {
units_per_em: 1000,
ascent: 1025.0,
descent: -275.0,
line_gap: 0.0,
underline_position: -95.0,
underline_thickness: 60.0,
cap_height: 698.0,
x_height: 516.0,
bounding_box: Bounds {
origin: Point {
x: -260.0,
y: -245.0,
},
size: Size {
width: 1501.0,
height: 1364.0,
},
},
}
}
fn typographic_bounds(&self, font_id: FontId, _glyph_id: GlyphId) -> Result<Bounds<f32>> {
Err(anyhow!("No font found for {:?}", font_id))
fn typographic_bounds(&self, _font_id: FontId, _glyph_id: GlyphId) -> Result<Bounds<f32>> {
Ok(Bounds {
origin: Point { x: 54.0, y: 0.0 },
size: size(392.0, 528.0),
})
}
fn advance(&self, font_id: FontId, _glyph_id: GlyphId) -> Result<Size<f32>> {
Err(anyhow!("No font found for {:?}", font_id))
fn advance(&self, _font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>> {
Ok(size(600.0 * glyph_id.0 as f32, 0.0))
}
fn glyph_for_char(&self, _font_id: FontId, _ch: char) -> Option<GlyphId> {
None
fn glyph_for_char(&self, _font_id: FontId, ch: char) -> Option<GlyphId> {
Some(GlyphId(ch.len_utf16() as u32))
}
fn glyph_raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
Err(anyhow!("No font found for {:?}", params))
fn glyph_raster_bounds(&self, _params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
Ok(Default::default())
}
fn rasterize_glyph(
&self,
params: &RenderGlyphParams,
_raster_bounds: Bounds<DevicePixels>,
_params: &RenderGlyphParams,
raster_bounds: Bounds<DevicePixels>,
) -> Result<(Size<DevicePixels>, Vec<u8>)> {
Err(anyhow!("No font found for {:?}", params))
Ok((raster_bounds.size, Vec::new()))
}
fn layout_line(&self, _text: &str, _font_size: Pixels, _runs: &[FontRun]) -> LineLayout {
unimplemented!()
fn layout_line(&self, text: &str, font_size: Pixels, _runs: &[FontRun]) -> LineLayout {
let mut position = px(0.);
let metrics = self.font_metrics(FontId(0));
let em_width = font_size
* self
.advance(FontId(0), self.glyph_for_char(FontId(0), 'm').unwrap())
.unwrap()
.width
/ metrics.units_per_em as f32;
let mut glyphs = SmallVec::default();
for (ix, c) in text.char_indices() {
if let Some(glyph) = self.glyph_for_char(FontId(0), c) {
glyphs.push(ShapedGlyph {
id: glyph,
position: point(position, px(0.)),
index: ix,
is_emoji: glyph.0 == 2,
});
if glyph.0 == 2 {
position += em_width * 2.0;
} else {
position += em_width;
}
} else {
position += em_width
}
}
let mut runs = Vec::default();
if glyphs.len() > 0 {
runs.push(ShapedRun {
font_id: FontId(0),
glyphs,
});
} else {
position = px(0.);
}
LineLayout {
font_size,
width: position,
ascent: font_size * (metrics.ascent / metrics.units_per_em as f32),
descent: font_size * (metrics.descent / metrics.units_per_em as f32),
runs,
len: text.len(),
}
}
}