RazrFalcon / tiny-skia

A tiny Skia subset ported to Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hairline stroking at half-pixel offset + `LineCap::Square` produces artifacts

RazrFalcon opened this issue · comments

use tiny_skia::*;

fn main() {
    let mut paint = Paint::default();
    paint.set_color_rgba8(255, 0, 0, 255);
    paint.anti_alias = true;

    let path = {
        let mut pb = PathBuilder::new();
        pb.move_to(2.5, 1.5);
        pb.line_to(5.5, 1.5);
        pb.line_to(5.5, 5.5);
        pb.line_to(2.5, 5.5);
        pb.close();
        pb.finish().unwrap()
    };

    let mut stroke = Stroke::default();
    stroke.width = 1.0;
    stroke.line_cap = LineCap::Square;

    let mut pixmap = Pixmap::new(7, 7).unwrap();
    pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None);
    pixmap.save_png("image.png").unwrap();
}

produces (8x upscale, actual/expected):

image
image2

This issue can be "fixed" by not using LineCap::Square, by using larger stroke width (which would simply disable hairline stroking) and by closing the path manually, without relying on ClosePath/Z.

The Skia itself produces similar results, but not quite the same. Not sure if this is actually a Skia bug or an intentional behaviour.