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

[checkoutlinesufo] improve overlap removal #1146

Merged
merged 3 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions python/afdko/checkoutlinesufo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from shutil import copy2
import sys
import textwrap
import warnings

import booleanOperations.booleanGlyph
import defcon
Expand All @@ -21,6 +22,7 @@

from afdko import ufotools
from afdko.ufotools import (
thresholdAttrGlyph,
kProcessedGlyphsLayer as PROCD_GLYPHS_LAYER,
kProcessedGlyphsLayerName as PROCD_GLYPHS_LAYER_NAME,
)
Expand Down Expand Up @@ -118,6 +120,8 @@ def save(self):
To achieve this, the code below hacks ufoLib to surgically save
only the processed layer.
This hack is only performed if the original UFO is format 2.

NOTE: this is deprecated and will be removed from AFDKO.
"""
fmt_ver = {
1: UFOFormatVersion.FORMAT_1_0,
Expand All @@ -134,6 +138,14 @@ def save(self):
# Override the UFO's formatVersion. This disguises a UFO2 to
# be seen as UFO3 by ufoLib, thus enabling it to write the
# layer without raising an error.
warn_txt = ("Using a ‘hybrid’ UFO2-as-UFO3 is deprecated and "
"will be removed from AFDKO by the end of 2020. "
"This behavior (hack) was primarily to support "
"older versions of RoboFont which did not support "
"UFO3/layers. RoboFont 3 now supports UFO3 so the "
"hack is no longer required. Please update your "
"toolchain as needed.")
warnings.warn(warn_txt, category=FutureWarning)
writer._formatVersion = UFOFormatVersion.FORMAT_3_0

glyph_set = writer.getGlyphSet(
Expand Down Expand Up @@ -910,6 +922,8 @@ def restore_contour_order(fixed_glyph, original_contours):
num_contours = len(new_list)
# Check each extreme for a match.
if num_contours > 0:
# ctr_starts tracks start points per contour
ctr_starts = {n: [] for n in range(num_contours)}
for i in range(num_contours):
ci, contour = new_list[i]
max_p = contour.maxP
Expand All @@ -933,7 +947,12 @@ def restore_contour_order(fixed_glyph, original_contours):
if (point.x == old_start_point.x) \
and (point.y == old_start_point.y) \
and point.segmentType is not None:
assert not(ctr_starts[ci]), "Can't happen: duplicated start point!" # noqa: E501
# if the above assertion fails, it means
# coincident points were not correctly
# removed prior to this method.
contour.setStartPoint(pi)
ctr_starts[ci].append(pi)

break
if matched:
Expand Down Expand Up @@ -1075,7 +1094,14 @@ def run(args=None):
for point in contour:
point.x = int(round(point.x))
point.y = int(round(point.y))

# JH May 2020: remove overlap can leave some coincident points
# we use thresholdAttrGlyph (modified thresholdPen) to remove them
# prior to restore_contour_order.
thresholdAttrGlyph(fixed_glyph, 1)

restore_contour_order(fixed_glyph, original_contours)

# The following is needed when the script is called from another
# script with Popen():
sys.stdout.flush()
Expand Down
43 changes: 43 additions & 0 deletions python/afdko/ufotools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import re
from collections import OrderedDict

from fontPens.thresholdPen import ThresholdPen
from fontTools.misc import etree as ET
from fontTools.misc import plistlib
from fontTools.pens.recordingPen import RecordingPen
from fontTools.ufoLib import UFOReader

from psautohint.ufoFont import norm_float
Expand Down Expand Up @@ -1081,3 +1083,44 @@ def makeUFOFMNDB(srcFontPath):
with open(fmndbPath, "w") as fp:
fp.write(data)
return fmndbPath


def thresholdAttrGlyph(aGlyph, threshold=0):
"""
Like fontPens.thresholdPen.thresholdGlyph, but preserves some glyph- and
point-level attributes that are not preserved by that method.
"""
# preserve glyph-level attributes
attrnames = ['anchors']
attrs = {k: getattr(aGlyph, k, None) for k in attrnames if hasattr(aGlyph, k)} # noqa: E501

# preserve Point.smooth attributes
smoothed = {(ci, (p.x, p.y)): p for ci, p in PointIterator(aGlyph) if p.smooth} # noqa: E501

# filter with ThresholdPen into recording pen
recorder = RecordingPen()
filterpen = ThresholdPen(recorder, threshold)
aGlyph.draw(filterpen)
aGlyph.clear()
recorder.replay(aGlyph.getPen())

# restore Point.smooth attributes
for ci, p in PointIterator(aGlyph):
if (ci, (p.x, p.y)) in smoothed:
p.smooth = True

# restore glyph-level attributes
for k, v in attrs.items():
setattr(aGlyph, k, v)

return aGlyph


class PointIterator(object):
def __init__(self, aGlyph):
self.glyph = aGlyph

def __iter__(self):
for ci, c in enumerate(self.glyph._contours):
for p in c._points:
yield (ci, p)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
'cedillacmb': ['a71d363ec57fde7208302f0545e6b327b02dd7eda5a926469ee80871685ad160427712cc9b9a4ff64786d00da1d881ceb2add6f10de8ec568e0f283d843d8d09', ['checkOutlines']],
'hashMapVersion': (1, 0),
's': ['52f431644e8deae043be02ec76aae1c6bf40bf86ceb639ec3bb317e28c04a74ebabbb41d5c17395ee979ec4fe17b65b428767c33d6e71ff1dee10ee94326d98e', ['checkOutlines']],
'scedilla': ['8894db48ec0b378095f53bb76eb19f5cdc1416c3926381d8d9230b8505c54f7fac4656cd8fc35bd9e9088b3b680bd0f7cbc6f51a0c9db39f2a6af12fb156c722', ['checkOutlines']],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>ascender</key>
<integer>750</integer>
<key>capHeight</key>
<integer>750</integer>
<key>descender</key>
<integer>-250</integer>
<key>guidelines</key>
<array/>
<key>postscriptBlueValues</key>
<array/>
<key>postscriptFamilyBlues</key>
<array/>
<key>postscriptFamilyOtherBlues</key>
<array/>
<key>postscriptOtherBlues</key>
<array/>
<key>postscriptStemSnapH</key>
<array/>
<key>postscriptStemSnapV</key>
<array/>
<key>unitsPerEm</key>
<integer>1000</integer>
<key>xHeight</key>
<integer>500</integer>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict/>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>color</key>
<string>0.5,1,0,0.7</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version='1.0' encoding='UTF-8'?>
<glyph name="cedillacmb" format="2">
<unicode hex="0327"/>
<outline>
<contour>
<point x="-25" y="-86" type="line"/>
<point x="24" y="-93"/>
<point x="47" y="-107"/>
<point x="47" y="-138" type="curve" smooth="yes"/>
<point x="47" y="-166"/>
<point x="28" y="-184"/>
<point x="-11" y="-184" type="curve" smooth="yes"/>
<point x="-29" y="-184"/>
<point x="-43" y="-183"/>
<point x="-59" y="-181" type="curve"/>
<point x="-66" y="-211" type="line"/>
<point x="-53" y="-215"/>
<point x="-32" y="-220"/>
<point x="-4" y="-220" type="curve" smooth="yes"/>
<point x="69" y="-220"/>
<point x="107" y="-186"/>
<point x="107" y="-140" type="curve" smooth="yes"/>
<point x="107" y="-92"/>
<point x="66" y="-66"/>
<point x="16" y="-58" type="curve"/>
<point x="24" y="3" type="line"/>
<point x="-12" y="3" type="line"/>
</contour>
</outline>
</glyph>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>cedillacmb</key>
<string>cedillacmb.glif</string>
<key>s</key>
<string>s.glif</string>
<key>scedilla</key>
<string>scedilla.glif</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version='1.0' encoding='UTF-8'?>
<glyph name="s" format="2">
<advance width="489"/>
<unicode hex="0073"/>
<outline>
<contour>
<point x="230" y="-14" type="curve" smooth="yes"/>
<point x="377" y="-14"/>
<point x="444" y="57"/>
<point x="444" y="135" type="curve" smooth="yes"/>
<point x="444" y="201"/>
<point x="404" y="258"/>
<point x="296" y="287" type="curve" smooth="yes"/>
<point x="251" y="299" type="line" smooth="yes"/>
<point x="181" y="318"/>
<point x="155" y="344"/>
<point x="155" y="385" type="curve" smooth="yes"/>
<point x="155" y="428"/>
<point x="190" y="451"/>
<point x="257" y="451" type="curve" smooth="yes"/>
<point x="281" y="451"/>
<point x="304" y="446"/>
<point x="327" y="436" type="curve"/>
<point x="343" y="352" type="line"/>
<point x="409" y="352" type="line"/>
<point x="414" y="468" type="line"/>
<point x="366" y="496"/>
<point x="319" y="511"/>
<point x="258" y="511" type="curve" smooth="yes"/>
<point x="128" y="511"/>
<point x="61" y="443"/>
<point x="61" y="365" type="curve" smooth="yes"/>
<point x="61" y="291"/>
<point x="112" y="244"/>
<point x="196" y="220" type="curve" smooth="yes"/>
<point x="253" y="204" type="line" smooth="yes"/>
<point x="320" y="185"/>
<point x="348" y="160"/>
<point x="348" y="114" type="curve" smooth="yes"/>
<point x="348" y="73"/>
<point x="310" y="46"/>
<point x="230" y="46" type="curve" smooth="yes"/>
<point x="197" y="46"/>
<point x="168" y="50"/>
<point x="143" y="59" type="curve"/>
<point x="122" y="151" type="line"/>
<point x="53" y="151" type="line"/>
<point x="57" y="26" type="line"/>
<point x="113" y="-1"/>
<point x="163" y="-14"/>
</contour>
</outline>
</glyph>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version='1.0' encoding='UTF-8'?>
<glyph name="scedilla" format="2">
<advance width="433"/>
<unicode hex="015F"/>
<outline>
<contour>
<point x="230" y="-14" type="line" smooth="yes"/>
<point x="377" y="-14"/>
<point x="444" y="57"/>
<point x="444" y="135" type="curve" smooth="yes"/>
<point x="444" y="201"/>
<point x="404" y="258"/>
<point x="296" y="287" type="curve" smooth="yes"/>
<point x="251" y="299" type="line" smooth="yes"/>
<point x="181" y="318"/>
<point x="155" y="344"/>
<point x="155" y="385" type="curve" smooth="yes"/>
<point x="155" y="428"/>
<point x="190" y="451"/>
<point x="257" y="451" type="curve" smooth="yes"/>
<point x="281" y="451"/>
<point x="304" y="446"/>
<point x="327" y="436" type="curve"/>
<point x="343" y="352" type="line"/>
<point x="409" y="352" type="line"/>
<point x="414" y="468" type="line"/>
<point x="366" y="496"/>
<point x="319" y="511"/>
<point x="258" y="511" type="curve" smooth="yes"/>
<point x="128" y="511"/>
<point x="61" y="443"/>
<point x="61" y="365" type="curve" smooth="yes"/>
<point x="61" y="291"/>
<point x="112" y="244"/>
<point x="196" y="220" type="curve" smooth="yes"/>
<point x="253" y="204" type="line" smooth="yes"/>
<point x="320" y="185"/>
<point x="348" y="160"/>
<point x="348" y="114" type="curve" smooth="yes"/>
<point x="348" y="73"/>
<point x="310" y="46"/>
<point x="230" y="46" type="curve" smooth="yes"/>
<point x="197" y="46"/>
<point x="168" y="50"/>
<point x="143" y="59" type="curve"/>
<point x="122" y="151" type="line"/>
<point x="53" y="151" type="line"/>
<point x="57" y="26" type="line"/>
<point x="102" y="4"/>
<point x="144" y="-9"/>
<point x="194" y="-13" type="curve"/>
<point x="183" y="-86" type="line"/>
<point x="232" y="-93"/>
<point x="255" y="-107"/>
<point x="255" y="-138" type="curve" smooth="yes"/>
<point x="255" y="-166"/>
<point x="236" y="-184"/>
<point x="197" y="-184" type="curve" smooth="yes"/>
<point x="179" y="-184"/>
<point x="165" y="-183"/>
<point x="149" y="-181" type="curve"/>
<point x="142" y="-211" type="line"/>
<point x="155" y="-215"/>
<point x="176" y="-220"/>
<point x="204" y="-220" type="curve" smooth="yes"/>
<point x="277" y="-220"/>
<point x="315" y="-186"/>
<point x="315" y="-140" type="curve" smooth="yes"/>
<point x="315" y="-92"/>
<point x="274" y="-66"/>
<point x="224" y="-58" type="curve"/>
</contour>
</outline>
</glyph>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<glyph name="cedillacmb" format="2">
<unicode hex="0327"/>
<outline>
<contour>
<point x="-25" y="-86" type="line"/>
<point x="24" y="-93"/>
<point x="47" y="-107"/>
<point x="47" y="-138" type="curve" smooth="yes"/>
<point x="47" y="-166"/>
<point x="28" y="-184"/>
<point x="-11" y="-184" type="curve" smooth="yes"/>
<point x="-29" y="-184"/>
<point x="-43" y="-183"/>
<point x="-59" y="-181" type="curve"/>
<point x="-66" y="-211" type="line"/>
<point x="-53" y="-215"/>
<point x="-32" y="-220"/>
<point x="-4" y="-220" type="curve" smooth="yes"/>
<point x="69" y="-220"/>
<point x="107" y="-186"/>
<point x="107" y="-140" type="curve" smooth="yes"/>
<point x="107" y="-85"/>
<point x="53" y="-59"/>
<point x="-6" y="-56" type="curve"/>
<point x="14" y="-77" type="line"/>
<point x="24" y="3" type="line"/>
<point x="-12" y="3" type="line"/>
</contour>
</outline>
<anchor name="_baseLC" x="0" y="0"/>
</glyph>
Loading