Files
vectorcraft/crates/render/src/tests/bitmapglyphs.rs
T
dotCooCoo 5fe1391a35 Preferences: image smoothing reaches type rasters, pattern tiles and placed documents
With File Handling › Display Bitmaps as Anti-aliased Images (or its Pixel
Preview setting) off, images inside pattern tiles and placed documents now
show hard-edged pixels, and the tile or placed bitmap is drawn without
smoothing too. Tiles and placed bitmaps made with and without smoothing are
kept apart, and a raster kept for type with color bitmap glyphs is made
again when the setting changes.

- Tests: a 2x2 checker scaled up in a pattern tile, a placed document and
  the canvas shows only its own colors with smoothing off and greys with it
  on; type rasters change their inputs with the setting.
- Docs: docs/mcp.md and the preference help no longer note pattern tiles
  as an exception.
2026-12-04 01:31:56 -08:00

120 lines
5.9 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Colour bitmap glyphs (emoji): drawn as their images, which the type's fill doesn't paint.
use vectorcraft_color::{Color, Paint};
use vectorcraft_doc::text::{CharStyle, TextObject};
use vectorcraft_doc::{Node, NodeKind};
use vectorcraft_geom::Point;
use vectorcraft_text::test_fonts::{COLOUR_CHAR, COLOUR_LARGE, colour_bitmap_family, colour_bitmap_font};
use crate::*;
/// 100 pt [`COLOUR_CHAR`] in the colour bitmap test font (`sbix` or `CBDT`), filled blue, its
/// baseline at (20, 120), on a white 160×160 page.
fn render(sbix: bool, opts: RenderOptions) -> Rendered {
let _adding = crate::tests_fontchange::adding_fonts();
vectorcraft_text::FontDb::global().add_font(colour_bitmap_font(sbix).unwrap());
let mut d = Document::new(160.0, 160.0);
let st = CharStyle {
font_family: colour_bitmap_family(sbix).into(),
size: 100.0,
fill: Paint::solid(Color::rgb(0.0, 0.0, 1.0)),
..Default::default()
};
let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(TextObject::point(Point::new(20.0, 120.0), &COLOUR_CHAR.to_string(), st))));
let l = d.layers[0].id;
d.insert(Some(l), 0, n).unwrap();
Renderer::new().render(&d, 160, 160, Affine::IDENTITY, &RenderOptions { background: Some([255, 255, 255, 255]), ..opts })
}
#[test]
fn a_bitmap_glyph_draws_its_largest_strike_not_the_fill() {
for sbix in [true, false] {
let img = render(sbix, RenderOptions::default());
// Inside the em square the image covers (the CBDT one from 75 above the baseline to 25
// below, the sbix one from 87.2 above to 12.8 below), away from its edges.
assert_eq!(img.pixel(70, 80), COLOUR_LARGE, "sbix {sbix}");
let blue = (0..160).flat_map(|y| (0..160).map(move |x| (x, y))).any(|(x, y)| img.pixel(x, y)[2] > 200 && img.pixel(x, y)[0] < 50);
assert!(!blue, "the fill doesn't paint the glyph (sbix {sbix})");
}
}
#[test]
fn outline_mode_draws_a_bitmap_glyph_as_its_box() {
let img = render(false, RenderOptions { outline: true, ..Default::default() });
assert_eq!(img.pixel(70, 80), [255, 255, 255, 255], "no pixels inside");
// The box's left edge at x 32.5 (the bearing of 2 px of 6.25 pt), from y 45 to 145.
assert!((31..=34).any(|x| img.pixel(x, 100)[0] < 128), "the box's edge shows");
}
/// 100 pt [`COLOUR_CHAR`] in the `CBDT` test font with `effects`, on a white 160×160 page.
fn render_with(effects: Vec<vectorcraft_doc::Effect>) -> Rendered {
let _adding = crate::tests_fontchange::adding_fonts();
vectorcraft_text::FontDb::global().add_font(colour_bitmap_font(false).unwrap());
let mut d = Document::new(160.0, 160.0);
let st = CharStyle { font_family: colour_bitmap_family(false).into(), size: 100.0, ..Default::default() };
let mut n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(TextObject::point(Point::new(20.0, 120.0), &COLOUR_CHAR.to_string(), st))));
let mut appearance = (*n.appearance).clone();
appearance.effects = effects;
n.appearance = appearance.into();
let l = d.layers[0].id;
d.insert(Some(l), 0, n).unwrap();
Renderer::new().render(&d, 160, 160, Affine::IDENTITY, &RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() })
}
fn effect(id: &str, params: serde_json::Value) -> vectorcraft_doc::Effect {
vectorcraft_doc::Effect { id: id.into(), params, visible: true }
}
/// Type with a geometry effect is drawn as its outlines: its emoji go with them, as images that
/// hold their own pixels (here moved 40 pt by a Transform effect).
#[test]
fn bitmap_glyphs_go_with_type_that_a_geometry_effect_reshapes() {
let img = render_with(vec![effect("distort.transform", serde_json::json!({"moveH": 40}))]);
assert_eq!(img.pixel(110, 80), COLOUR_LARGE, "moved with the type");
assert_eq!(img.pixel(50, 80), [255, 255, 255, 255], "not where it was");
}
/// Bitmap glyphs are sampled as images are (File Handling › Display Bitmaps as Anti-aliased Images,
/// #10712), so a raster kept for type, such as a blur's, is made again when image smoothing changes.
#[test]
fn rasters_of_type_follow_image_smoothing() {
let _adding = crate::tests_fontchange::adding_fonts();
vectorcraft_text::FontDb::global().add_font(colour_bitmap_font(false).unwrap());
let mut d = Document::new(160.0, 160.0);
let st = CharStyle { font_family: colour_bitmap_family(false).into(), size: 100.0, ..Default::default() };
let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(TextObject::point(Point::new(20.0, 120.0), &COLOUR_CHAR.to_string(), st))));
let l = d.layers[0].id;
d.insert(Some(l), 0, n.clone()).unwrap();
let symbols = d.symbol_boxes().quick();
let mut r = Renderer::new();
let mut inputs = |smooth_images: bool| {
let opts = RenderOptions { smooth_images, ..Default::default() };
let f = Frame {
mt: false,
patch: None,
doc: &d,
symbols: &symbols,
view: Affine::IDENTITY,
visible: vectorcraft_geom::Rect::new(0.0, 0.0, 160.0, 160.0),
px: 1.0,
opts: &opts,
ink: crate::ink::Ink::Display,
output: Affine::IDENTITY,
plates: None,
};
r.raster_inputs(&f, &n, crate::fx::Reads::of(&n))
};
let smoothed = inputs(true);
assert!(inputs(true) == smoothed, "the same settings: the same inputs");
assert!(inputs(false) != smoothed, "image smoothing changed: the raster is made again");
}
/// A colour adjustment leaves the emoji of reshaped type in their colours, as it does those of
/// type, and as exports and Expand Appearance draw them.
#[test]
fn a_colour_adjustment_leaves_the_emoji_of_reshaped_type_as_they_are() {
let moved = effect("distort.transform", serde_json::json!({"moveH": 40}));
let img = render_with(vec![moved, effect("adjust.hueSaturation", serde_json::json!({"hue": 120}))]);
assert_eq!(img.pixel(110, 80), COLOUR_LARGE);
}