Lines
100 %
Functions
Branches
use super::*;
use crate::spec::{ChartKind, Series, SeriesPoint};
/// The SVG test cannot cover this: SVGBackend emits `<text>` nodes and uses
/// the font only for layout, while BitMapBackend RASTERISES glyphs through
/// ab_glyph. The bars alone leave opaque pixels, so compare against the same
/// chart with an empty title: the caption's glyphs must add some.
#[test]
fn render_png_rasterises_label_glyphs() {
let opaque = |spec: &ChartSpec| -> usize {
let png = render_png(spec, KittyOpts::default())
.expect("render_png returned None — drawing failed, not merely unstyled");
let img = image::load_from_memory(&png).expect("not a decodable PNG");
img.to_rgba8().pixels().filter(|p| p.0[3] > 0xc0).count()
};
let titled = ChartSpec {
title: "A long chart title that needs many glyphs".to_string(),
..sample(ChartKind::Bar)
let untitled = ChartSpec {
title: String::new(),
let (with, without) = (opaque(&titled), opaque(&untitled));
assert!(
with > without + 100,
"caption added {} opaque pixels — its glyphs were not rasterised",
with.saturating_sub(without)
);
}
fn sample(kind: ChartKind) -> ChartSpec {
ChartSpec {
title: "Test".to_string(),
kind,
x_label: "X".to_string(),
y_label: "Y".to_string(),
series: vec![Series {
label: "A".to_string(),
commodity_symbol: "USD".to_string(),
points: vec![
SeriesPoint {
x: "Jan".to_string(),
y_num: 100,
y_denom: 1,
},
x: "Feb".to_string(),
y_num: 200,
],
}],
notes: vec![],
fn render_png_produces_valid_png_magic_bytes() {
let png = render_png(
&sample(ChartKind::Line),
KittyOpts {
width_px: 320,
height_px: 200,
..KittyOpts::default()
)
.expect("bitmap render should succeed");
assert_eq!(
&png[..8],
&[0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a],
"PNG magic bytes mismatch"
fn render_png_has_no_background() {
let png = render_png(&sample(ChartKind::Bar), KittyOpts::default()).expect("render");
let img = image::load_from_memory(&png).expect("png").to_rgba8();
let last = (img.width() - 1, img.height() - 1);
assert_eq!(img.get_pixel(0, 0).0[3], 0, "top-left corner is painted");
img.get_pixel(last.0, last.1).0[3],
0,
"bottom-right corner is painted"
fn render_png_on_a_background_is_opaque_in_that_colour() {
let opts = KittyOpts {
background: Some([0x00, 0x00, 0xaa]),
let png = render_png(&sample(ChartKind::Bar), opts).expect("render");
assert_eq!(img.get_pixel(0, 0).0, [0x00, 0x00, 0xaa, 0xff]);
assert_eq!(img.get_pixel(last.0, last.1).0, [0x00, 0x00, 0xaa, 0xff]);
img.pixels().all(|p| p.0[3] == 0xff),
"a pixel is translucent"
fn render_png_text_height_grows_the_glyphs() {
let ink = |text_px: u32| -> usize {
text_px,
background: Some([0xff; 3]),
series: vec![],
let png = render_png(&titled, opts).expect("render");
img.pixels().filter(|p| p.0[0] < 0xe0).count()
assert!(ink(22) > ink(11), "larger text left no more ink");
fn ink_contrasts_with_the_background() {
assert_eq!(ink(None), NEUTRAL);
assert_eq!(ink(Some([0, 0, 0xaa])), RGBColor(235, 235, 235));
assert_eq!(ink(Some([0xf0, 0xf0, 0xf0])), RGBColor(30, 30, 30));
fn render_png_on_blue_draws_light_ink_not_grey() {
background: Some([0, 0, 0xaa]),
img.pixels().any(|p| p.0[0] > 0xc8 && p.0[1] > 0xc8),
"no light ink"
fn unblend_recovers_colour_and_alpha() {
let half_red = unblend(&[255, 128, 128], &[128, 0, 0]);
assert_eq!(half_red, [255, 0, 0, 128]);
assert_eq!(unblend(&[255; 3], &[0; 3]), [0, 0, 0, 0]);
assert_eq!(unblend(&[10, 20, 30], &[10, 20, 30]), [10, 20, 30, 255]);
fn encode_png_apc_round_trips_payload() {
let png = b"\x89PNG\r\n\x1a\nsome-fake-payload";
let framed = encode_png_apc(png);
let payload: String = framed
.split("\x1b\\")
.filter(|s| !s.is_empty())
.map(|chunk| {
let semi = chunk.find(';').expect("semicolon separator");
&chunk[semi + 1..]
})
.collect();
let decoded = BASE64.decode(payload).expect("decoded base64");
assert_eq!(decoded, png);
fn encode_png_apc_chunks_large_payload() {
// Produce a PNG whose base64 encoding is > 4096 chars so
// chunking logic is exercised.
let png: Vec<u8> = (0..4000_u32).flat_map(u32::to_le_bytes).collect();
let framed = encode_png_apc(&png);
let start_markers = framed.matches("\x1b_G").count();
start_markers >= 2,
"expected at least 2 APC chunks, got {start_markers}"
assert!(framed.contains("m=1"), "intermediate chunks must set m=1");
assert!(framed.contains("m=0"), "final chunk must set m=0");
fn encode_png_apc_handles_empty_input() {
assert!(encode_png_apc(&[]).is_empty());