Skip to content

Instantly share code, notes, and snippets.

@ryanbugden
Last active April 6, 2023 20:35
Show Gist options
  • Select an option

  • Save ryanbugden/eb6a3eab6507b535c8c62ddad22d8f23 to your computer and use it in GitHub Desktop.

Select an option

Save ryanbugden/eb6a3eab6507b535c8c62ddad22d8f23 to your computer and use it in GitHub Desktop.
Run this to cut the glyph off at y=0 and either x-height or cap-height, depending on the glyph.
# menuTitle: Slice Top and Bottom of Glyph - EZUI
import ezui
from mojo.tools import IntersectGlyphWithLine
import math
oob = 1000 # out-of-bounds distance
def intersectContourAtHeight(g, height):
slicer = g.getRepresentation(
"doodle.Beam",
beam=((-oob, height), (g.width + oob, height)),
canHaveComponent=True
)
slicer.sliceGlyph(roundValue=True, addIntersectionsOnly=False)
# if the height is low, delete all the low points
if height <= 0:
for c in g.contours:
if c.bounds[3] == height:
g.removeContour(c)
# if slice didn't make distinct contours, get rid of points
for pt in c.points:
if pt.y < height:
c.removePoint(pt)
# if the height is high, delete all the high points
else:
for c in g.contours:
if c.bounds[1] == height:
g.removeContour(c)
for pt in c.points:
if pt.y > height:
c.removePoint(pt)
class SliceGlyph(ezui.WindowController):
def build(self):
content = """
( ) Ascender @topCut
(X) Cap-Height
( ) X-Height
--------
(X) Baseline @bottomCut
( ) Descender
--------
(Slice Glyph) @commit
"""
self.w = ezui.EZWindow(
content=content,
title="Slice Off",
size=(140, 'auto'),
controller=self
)
def started(self):
self.w.open()
def commitCallback(self, sender):
f, g = CurrentFont(), CurrentGlyph()
top = {
0: f.info.ascender,
1: f.info.capHeight,
2: f.info.xHeight,
}
bottom = {
0: 0,
1: f.info.descender
}
heights = [top[self.w.getItemValues()['topCut']], bottom[self.w.getItemValues()['bottomCut']]]
with g.undo(f"Slice Glyph {g.name}"):
for height in heights:
intersectContourAtHeight(g, height)
g.changed()
SliceGlyph()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment