Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🤖 and 🪵 are rendered incorrectly it would seem #26

Closed
Jasper-Bekkers opened this issue Mar 20, 2022 · 7 comments
Closed

🤖 and 🪵 are rendered incorrectly it would seem #26

Jasper-Bekkers opened this issue Mar 20, 2022 · 7 comments

Comments

@Jasper-Bekkers
Copy link
Contributor

Repro case

It looks like both of these render partially (I haven't tried many more, however 🔥 from the documentation seems to render correctly). The robot-man seems to have his mouth cut off, and the log seems to have the top cut off.

Since this is my very first time diving into this library, I may well be doing something wrong. I've attached the repro of what I'm trying to do.

I've also attached the rendered image below;

use swash::scale::ScaleContext;
use swash::scale::StrikeWith;
use swash::scale::*;
use zeno::Vector;

fn main() {
    use swash::FontRef;

    let font_path = "C:/Windows/Fonts/seguiemj.ttf";

    let font_data = std::fs::read(font_path).ok().unwrap();
    let font = FontRef::from_index(&font_data, 0).unwrap();

    let mut context = ScaleContext::new();
    let mut scaler = context.builder(font).hint(true).build();
    let glyph_id = font.charmap().map('🤖');
    let image = Render::new(&[
        Source::ColorBitmap(StrikeWith::ExactSize),
        Source::ColorOutline(1),
        Source::Outline,
    ])
    .format(zeno::Format::Subpixel)
    .offset(Vector::new(0.0, 0.0))
    .render(&mut scaler, glyph_id)
    .unwrap();

    dbg!(image.placement);

    let img =
        ::image::RgbaImage::from_raw(image.placement.width, image.placement.height, image.data)
            .unwrap();
    img.save_with_format("stuff.png", ::image::ImageFormat::Png);
}

stuff

@rice7th
Copy link

rice7th commented Nov 13, 2022

Hmm, weird, i tried running your code and there was no image generated.. (image.data was just empty)

@Jasper-Bekkers
Copy link
Contributor Author

That is indeed quite odd; I'm running with the following Cargo.toml

[package]
name = "swash-test"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
swash = "0.1.4"
zeno = "0.2"
image = "0.24"

I'm also running on Windows (as you can see by the font path), maybe that makes a difference? I just re-ran the code hoping it was potentially a version issue, but I'm getting a clean stuff.png image generated which looks identical to the image I've attached here.

@Jasper-Bekkers
Copy link
Contributor Author

Jasper-Bekkers commented Nov 15, 2022

Digging in a bit, it's starting to look like some of the scaling code is incorrectly calculating the size of the image for colored outlines.

swash/src/scale/mod.rs

Lines 914 to 921 in d3f8bbf

if i == 0 {
base_x = placement.left;
base_y = placement.top;
base_w = placement.width;
base_h = placement.height;
image.data.resize((base_w * base_h * 4) as usize, 0);
image.placement = placement;
}

I don't think it's correct to take the very first placement as the final image's total size, I have some local modifications that calculate the sizes and offsets beforehand but it's not quite correct yet (and to be a bit faster would need some minor changes to zeno to make some functions public). I'll play around with this some more to see if I can figure out the correct logic, though having a spec handy to compare to would be nice, so if anyone has a relevant link for me I'd love to take a look.

@dfrg My current changes look like this; they still don't fully calculate the bounds correctly for either the 🪵 or the 🤖 emoji's but it's overall quite a bit better then before; I would love your input on this since I suspect some of the bounds calculations / offset calculations aren't fully correct yet.

Source::ColorOutline(palette_index) => {
                    if !scaler.has_color_outlines() {
                        continue;
                    }
                    scaler.state.outline.clear();
                    if scaler.scale_color_outline_impl(glyph_id) {
                        let font = &scaler.font;
                        let proxy = &scaler.proxy;
                        let state = &mut scaler.state;
                        let scratch = &mut state.scratch0;
                        let rcx = &mut state.rcx;
                        let outline = &mut state.outline;
                        // Cool effect, but probably not generally desirable.
                        // Maybe expose a separate option?
                        // if self.embolden != 0. {
                        //     outline.embolden(self.embolden, self.embolden);
                        // }
                        if let Some(transform) = &self.transform {
                            outline.transform(transform);
                        }
                        let palette = proxy.color.palette(font, *palette_index);
                        let mut base_x = i32::MAX;
                        let mut base_y = 0i32;
                        let mut base_w = 0u32;
                        let mut base_h = 0u32;
                        let mut ok = true;
                        for i in 0..outline.len() {
                            let layer = match outline.get(i) {
                                Some(layer) => layer,
                                _ => {
                                    ok = false;
                                    break;
                                }
                            };
                            scratch.clear();
                            let placement = Mask::with_scratch(layer.path(), rcx)
                                .origin(Origin::BottomLeft)
                                .style(self.style)
                                .render_offset(self.offset)
                                .inspect(|fmt, w, h| {
                                    scratch.resize(fmt.buffer_size(w, h), 0);
                                })
                                .render_into(&mut scratch[..], None); // todo: don't need to rasterize twice just to get the placement, zeno has a helper function
                            base_x = base_x.min(placement.left);
                            base_y = base_y.max(placement.top);

                            base_w = base_w.max(placement.width);
                            base_h = base_h.max(placement.height);
                            image.data.resize((base_w * base_h * 4) as usize, 0);
                        }

                        for i in 0..outline.len() {
                            let layer = match outline.get(i) {
                                Some(layer) => layer,
                                _ => {
                                    ok = false;
                                    break;
                                }
                            };
                            scratch.clear();
                            let placement = Mask::with_scratch(layer.path(), rcx)
                                .origin(Origin::BottomLeft)
                                .style(self.style)
                                .render_offset(self.offset)
                                .inspect(|fmt, w, h| {
                                    scratch.resize(fmt.buffer_size(w, h), 0);
                                })
                                .render_into(&mut scratch[..], None);
                                //base_x = placement.left;
                                //base_y = placement.top;
                            //if i == 0 {
                                // base_w = placement.width;
                                // base_h = placement.height;
                                image.data.resize((base_w * base_h * 4) as usize, 0);
                                image.placement = placement;
                                image.placement.width = base_w;
                                image.placement.height = base_h;
                            //}
                            dbg!(placement);
                            let color = layer
                                .color_index()
                                .map(|i| palette.map(|p| p.get(i)))
                                .flatten()
                                .unwrap_or(self.foreground);
                            bitmap::blit(
                                &scratch[..],
                                placement.width,
                                placement.height,
                                placement.left.wrapping_sub(base_x),
                                base_y.wrapping_sub(placement.top),
                                color,
                                &mut image.data,
                                base_w,
                                base_h,
                            );
                        }
                        if ok {
                            image.source = Source::ColorOutline(*palette_index);
                            image.content = Content::Color;
                            return true;
                        }
                    }
                }

@Jasper-Bekkers
Copy link
Contributor Author

Checking it a bit further, it seems like either implementation is out of spec for ignoring the clipbox; https://learn.microsoft.com/en-us/typography/opentype/spec/colr#metrics-and-boundedness-of-color-glyphs-using-version-1-formats

@dfrg
Copy link
Owner

dfrg commented Nov 16, 2022

Hi Jasper! I’ll take a look at this soon. One note is that ClipBox is specifically for COLRv1 which swash does not currently support. When this was written, I believe the base layer was supposed to determine image boundaries for the full glyph.

In any case, I’ll dig into those particular glyphs and see if I can find out what’s going on. In the worst case, we take the union of the bounding box of all layers and that should give us appropriate dimensions.

@Jasper-Bekkers
Copy link
Contributor Author

Jasper-Bekkers commented Nov 16, 2022

In any case, I’ll dig into those particular glyphs and see if I can find out what’s going on. In the worst case, we take the union of the bounding box of all layers and that should give us appropriate dimensions.

The PR that I filed does this already (I hope correctly). Thanks for taking a look ☺️

@Jasper-Bekkers
Copy link
Contributor Author

Fixed by #30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants