Fix transparent text in PNG export (#3444)

This commit is contained in:
frozolotl 2024-02-20 14:03:25 +01:00 committed by GitHub
parent d8aba7c4ff
commit ee2128d115
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 22 deletions

View File

@ -465,12 +465,7 @@ fn render_outline_glyph(
write_bitmap(canvas, &bitmap, &state, sampler)?; write_bitmap(canvas, &bitmap, &state, sampler)?;
} }
Paint::Solid(color) => { Paint::Solid(color) => {
write_bitmap( write_bitmap(canvas, &bitmap, &state, to_sk_color_u8(*color).premultiply())?;
canvas,
&bitmap,
&state,
to_sk_color_u8_without_alpha(*color).premultiply(),
)?;
} }
Paint::Pattern(pattern) => { Paint::Pattern(pattern) => {
let pixmap = render_pattern_frame(&state, pattern); let pixmap = render_pattern_frame(&state, pattern);
@ -548,7 +543,8 @@ fn write_bitmap<S: PaintSampler>(
let color = sampler.sample((x as _, y as _)); let color = sampler.sample((x as _, y as _));
let color = bytemuck::cast(color); let color = bytemuck::cast(color);
let pi = (y * cw + x) as usize; let pi = (y * cw + x) as usize;
if cov == 255 { // Fast path if color is opaque.
if cov == u8::MAX && color & 0xFF == 0xFF {
pixels[pi] = color; pixels[pi] = color;
continue; continue;
} }
@ -771,17 +767,6 @@ fn scaled_texture(image: &Image, w: u32, h: u32) -> Option<Arc<sk::Pixmap>> {
trait PaintSampler: Copy { trait PaintSampler: Copy {
/// Sample the color at the `pos` in the pixmap. /// Sample the color at the `pos` in the pixmap.
fn sample(self, pos: (u32, u32)) -> sk::PremultipliedColorU8; fn sample(self, pos: (u32, u32)) -> sk::PremultipliedColorU8;
/// Write the sampler to a pixmap.
fn write_to_pixmap(self, canvas: &mut sk::Pixmap) {
let width = canvas.width();
for x in 0..canvas.width() {
for y in 0..canvas.height() {
let color = self.sample((x, y));
canvas.pixels_mut()[(y * width + x) as usize] = color;
}
}
}
} }
impl PaintSampler for sk::PremultipliedColorU8 { impl PaintSampler for sk::PremultipliedColorU8 {
@ -835,7 +820,7 @@ impl PaintSampler for GradientSampler<'_> {
self.transform_to_parent.map_point(&mut point); self.transform_to_parent.map_point(&mut point);
// Sample the gradient // Sample the gradient
to_sk_color_u8_without_alpha(self.gradient.sample_at( to_sk_color_u8(self.gradient.sample_at(
(point.x, point.y), (point.x, point.y),
(self.container_size.x.to_f32(), self.container_size.y.to_f32()), (self.container_size.x.to_f32(), self.container_size.y.to_f32()),
)) ))
@ -1027,9 +1012,9 @@ fn to_sk_color(color: Color) -> sk::Color {
sk::Color::from_rgba8(r, g, b, a) sk::Color::from_rgba8(r, g, b, a)
} }
fn to_sk_color_u8_without_alpha(color: Color) -> sk::ColorU8 { fn to_sk_color_u8(color: Color) -> sk::ColorU8 {
let [r, g, b, _] = color.to_rgb().to_vec4_u8(); let [r, g, b, a] = color.to_rgb().to_vec4_u8();
sk::ColorU8::from_rgba(r, g, b, 255) sk::ColorU8::from_rgba(r, g, b, a)
} }
fn to_sk_line_cap(cap: LineCap) -> sk::LineCap { fn to_sk_line_cap(cap: LineCap) -> sk::LineCap {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View File

@ -30,6 +30,12 @@ Emoji: 🐪, 🌋, 🏞
This is #text(rgb("FA644B"))[way more] colorful. This is #text(rgb("FA644B"))[way more] colorful.
] ]
// Transparency.
#block(fill: green)[
#set text(fill: rgb("FF000080"))
This text is transparent.
]
// Disable font fallback beyond the user-specified list. // Disable font fallback beyond the user-specified list.
// Without disabling, New Computer Modern Math would come to the rescue. // Without disabling, New Computer Modern Math would come to the rescue.
#set text(font: ("PT Sans", "Twitter Color Emoji"), fallback: false) #set text(font: ("PT Sans", "Twitter Color Emoji"), fallback: false)