simoncozens / fonttools-rs

A Rust OpenType manipulation library
Apache License 2.0
38 stars 6 forks source link

Remove overlap result not the best #70

Open ctrlcctrlv opened 2 years ago

ctrlcctrlv commented 2 years ago

image Input was FRBAmericanCursive-400-Regular.zip

…it's fast and wrong though, as opposed to slow and wrong as fontmake often is (though not this wrong)!

ctrlcctrlv commented 2 years ago

Strangely the contours are still in the TTF, just wrong winding direction. I wonder, is this yet another manifestation of @matthewblanchard's Skia bug?

ctrlcctrlv commented 2 years ago

Just an update. This is what it looks like post-№497140 (@MatthewBlanchard—Changed aswinding to use the signed area of the polygon defined by the contour's control points instead of the derivative of the left edge to determine winding direction.)

image image

Much better, but not perfect.

To achieve this result I applied this patch to @simoncozens code:

diff --git a/crates/fonttools-cli/src/bin/ttf-remove-overlap.rs b/crates/fonttools-cli/src/bin/ttf-remove-overlap.rs
index d402dbb..53bd5e1 100644
--- a/crates/fonttools-cli/src/bin/ttf-remove-overlap.rs
+++ b/crates/fonttools-cli/src/bin/ttf-remove-overlap.rs
@@ -1,8 +1,7 @@
 use fonttools::tables::glyf::{Glyph, Point};
-use fonttools::tag;
 use fonttools_cli::{open_font, read_args, save_font};

-use skia_safe::{simplify, Path};
+use skia_safe::{Path, PathOp};

 fn remove_overlap(g: &mut Glyph) {
     if g.has_components() || g.is_empty() {
@@ -46,7 +45,7 @@ fn remove_overlap(g: &mut Glyph) {
         }
         path.close();
     }
-    if let Some(newpath) = simplify(&path) {
+    if let Some(Some(newpath)) = path.op(&path, PathOp::Union).map(|p| p.as_winding()) {
         g.contours = skia_to_glyf(newpath);
     }
 }

And compiled as:

SKIA_USE_SYSTEM_LIBRARIES=1 SKIA_LIBRARY_SEARCH_PATH=/opt/lib/ SKIA_SOURCE_DIR=$PWD/../../../rust-skia/skia-bindings/skia/ SKIA_BUILD_DEFINES="`cat /opt/lib/skia-defines.txt`" cargo -vvv build --release