Giter VIP home page Giter VIP logo

Comments (7)

rice7th avatar rice7th commented on August 23, 2024

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

from swash.

Jasper-Bekkers avatar Jasper-Bekkers commented on August 23, 2024

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.

from swash.

Jasper-Bekkers avatar Jasper-Bekkers commented on August 23, 2024

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;
                        }
                    }
                }

from swash.

Jasper-Bekkers avatar Jasper-Bekkers commented on August 23, 2024

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

from swash.

dfrg avatar dfrg commented on August 23, 2024

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.

from swash.

Jasper-Bekkers avatar Jasper-Bekkers commented on August 23, 2024

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 ☺️

from swash.

Jasper-Bekkers avatar Jasper-Bekkers commented on August 23, 2024

Fixed by #30

from swash.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.