pop-os / cosmic-text

Pure Rust multi-line text handling

Home Page:https://pop-os.github.io/cosmic-text/cosmic_text/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A case of erroneous glyph placement by the swash scaler

MoSal opened this issue · comments

Font

Kawkab Mono v0.501

Glyphs Affected

Bold ع and غ

Analysis

Applying this patch first:

diff --git a/src/swash.rs b/src/swash.rs
index 7c35ce9f5..ee704ce27 100644
--- a/src/swash.rs
+++ b/src/swash.rs
@@ -37,7 +37,7 @@ fn swash_image(
     let offset = Vector::new(cache_key.x_bin.as_float(), cache_key.y_bin.as_float());
 
     // Select our source order
-    Render::new(&[
+    let mut ret = Render::new(&[
         // Color outline with the first palette
         Source::ColorOutline(0),
         // Color bitmap with best fit selection mode
@@ -58,7 +58,8 @@ fn swash_image(
         None
     })
     // Render the image
-    .render(&mut scaler, cache_key.glyph_id)
+    .render(&mut scaler, cache_key.glyph_id);
+    dbg!(ret.as_ref().map(|img| img.placement));
 }
 
 fn swash_outline_commands(

We can see this erroneous placement:

[src/swash.rs:62:5] ret.as_ref().map(|img| img.placement) = Some(
    Placement {
        left: 1,
        top: -2412,
        width: 48,
        height: 89,
    },
)

As you can see, the top value is way off.

I added this temporary workaround to make sure that glyphs are at least in the general vicinity of where they're supposed to be:

ret.as_mut().map(|img| if img.placement.top < img.placement.height as i32 * -1 {
     img.placement.top = (img.placement.height / 2) as i32;
});

Hopefully a proper fix will materialize.

Bug appears to be in swash's CFF scaler.

With swash's new-cff branch:

[src/swash.rs:62:5] ret.as_ref().map(|img| img.placement) = Some(
    Placement {
        left: 1,
        top: -364,
        width: 48,
        height: 89,
    },
)

So still wrong, but less so.

Thanks for reporting. I’ll take a look at this.

Nice catch! This was a bug in CFF hinting (interestingly, the bug was different but in the same exact code for both the old and new CFF scalers). I pushed a fix, merged the new CFF scaler and released swash 0.1.9.

Please let me know if this fixes things on your end.

Please let me know if this fixes things on your end.

It does. Thank you for your timely fix.