#![cfg(feature = "debug-control")]
use anyrender::render_to_buffer;
use anyrender_vello_cpu::VelloCpuImageRenderer;
use blitz_dom::{Document as _, DocumentConfig};
use blitz_paint::paint_scene;
use blitz_script::ScriptDocument;
use blitz_traits::shell::{ColorScheme, Viewport};
const HTML: &str = r#"<!doctype html><html><body style="margin:0;background:#fff">
<div style="width:200px;height:24px;font:16px/24px monospace;color:#000">Hgjy</div>
<div style="width:400px;height:376px;background:#fff"></div>
</body></html>"#;
const CSS_WIDTH: u32 = 400;
const CSS_HEIGHT: u32 = 400;
const LINE_HEIGHT: u32 = 24;
fn ink_below_the_line(layout_scale: f32, paint_scale: f64) -> usize {
let mut document = ScriptDocument::from_html(HTML, DocumentConfig::default());
let (width, height) = (
(f64::from(CSS_WIDTH) * paint_scale).round() as u32,
(f64::from(CSS_HEIGHT) * paint_scale).round() as u32,
);
{
let mut inner = document.inner_mut();
inner.set_viewport(Viewport::new(
(f64::from(CSS_WIDTH) * f64::from(layout_scale)).round() as u32,
(f64::from(CSS_HEIGHT) * f64::from(layout_scale)).round() as u32,
layout_scale,
ColorScheme::Light,
));
inner.resolve(0.0);
}
let mut inner = document.inner_mut();
let buffer = render_to_buffer::<VelloCpuImageRenderer, _>(
|scene| paint_scene(scene, &mut inner, paint_scale, width, height, 0, 0),
width,
height,
);
let first_clear_row = ((f64::from(LINE_HEIGHT) * paint_scale).round() as u32) + 2;
let mut ink = 0;
for y in first_clear_row..height {
for x in 0..width {
let offset = ((y * width + x) * 4) as usize;
if buffer[offset] < 128 {
ink += 1;
}
}
}
ink
}
#[test]
fn text_stays_inside_its_box_at_scale_one() {
assert_eq!(
ink_below_the_line(1.0, 1.0),
0,
"the fixture itself overflows, so the other cases prove nothing"
);
}
#[test]
fn text_stays_inside_its_box_when_the_document_is_laid_out_at_two() {
assert_eq!(
ink_below_the_line(2.0, 2.0),
0,
"painting at the document's own scale must keep glyphs in their boxes"
);
}
#[test]
fn a_mismatched_paint_scale_does_not_make_text_overflow() {
assert_eq!(ink_below_the_line(2.0, 1.0), 0);
}
#[test]
fn text_laid_out_for_a_wider_viewport_paints_outside_the_narrow_one() {
const WIDE: u32 = 800;
const NARROW: u32 = 300;
const PARAGRAPH: &str = r#"<!doctype html><html><body style="margin:0;background:#fff">
<div style="width:95%;font:16px/24px monospace;color:#000">
Hgjy Hgjy Hgjy Hgjy Hgjy Hgjy Hgjy Hgjy Hgjy Hgjy Hgjy Hgjy
</div></body></html>"#;
let ink_right_of_the_box = |viewport_width: u32| {
let mut document = ScriptDocument::from_html(PARAGRAPH, DocumentConfig::default());
{
let mut inner = document.inner_mut();
inner.set_viewport(Viewport::new(viewport_width, 400, 1.0, ColorScheme::Light));
inner.resolve(0.0);
}
let mut inner = document.inner_mut();
let buffer = render_to_buffer::<VelloCpuImageRenderer, _>(
|scene| paint_scene(scene, &mut inner, 1.0, NARROW, 400, 0, 0),
NARROW,
400,
);
let mut ink = 0;
for y in 0..400u32 {
for x in 290..NARROW {
let offset = ((y * NARROW + x) * 4) as usize;
if buffer[offset] < 128 {
ink += 1;
}
}
}
ink
};
assert_eq!(
ink_right_of_the_box(NARROW),
0,
"a document laid out for the size being painted must stay inside it"
);
assert!(
ink_right_of_the_box(WIDE) > 0,
"laying out at {WIDE} and painting at {NARROW} should demonstrate the \
mismatch this test exists to describe; if it no longer does, the \
fixture stopped being sensitive to viewport width"
);
}