Closed Jasper-Bekkers closed 1 year ago
Hmm, weird, i tried running your code and there was no image generated.. (image.data was just empty)
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.
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.
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;
}
}
}
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
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.
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 ☺️
Fixed by #30
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;