Last active
January 25, 2024 16:48
-
-
Save ryanbugden/7df9d90036ec716bebf0fc2e39731555 to your computer and use it in GitHub Desktop.
Honor the italic angle when scaling disproportionately.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# menuTitle: Scale While Slanted | |
import ezui | |
from mojo.extensions import getExtensionDefault, setExtensionDefault | |
from fontTools.misc.transform import Transform | |
from math import radians | |
EXTENSION_KEY = "com.ryanbugden.scaleWhileSlanted.settings" | |
EXTENSION_DEFAULTS = { | |
"scaleFactor": 0.5, | |
"radios": 0, | |
} | |
class ScaleWhileSlanted(ezui.WindowController): | |
def build(self): | |
content = """ | |
* TwoColumnForm @form0 | |
> : Scale Factor: | |
> [_0.5_] @scaleFactor | |
--- | |
* TwoColumnForm @form1 | |
> : Span: | |
> (X) Current Glyph @spanRadios | |
> ( ) Selected Glyphs | |
--- | |
* Tabs @tabs | |
> * Tab: Vertical | |
>> * TwoColumnForm @alignForm0 | |
>>> : Origin: | |
>>> (X) Top @orientVertRadios | |
>>> ( ) Center | |
>>> ( ) Bottom | |
> * Tab: Horizontal | |
>> * TwoColumnForm @alignForm1 | |
>>> : Origin: | |
>>> (X) Left @orientHorzRadios | |
>>> ( ) Center | |
>>> ( ) Right | |
--- | |
(Scale ) @scaleButton | |
""" | |
title_col_width = 80 | |
item_col_width = 130 | |
field_width = 40 | |
button_width = title_col_width + item_col_width + 20 | |
descriptionData = dict( | |
form0=dict( | |
titleColumnWidth=title_col_width, | |
itemColumnWidth=item_col_width, | |
), | |
form1=dict( | |
titleColumnWidth=title_col_width, | |
itemColumnWidth=item_col_width, | |
), | |
alignForm0=dict( | |
titleColumnWidth=title_col_width - 18, | |
itemColumnWidth=item_col_width, | |
), | |
alignForm1=dict( | |
titleColumnWidth=title_col_width - 18, | |
itemColumnWidth=item_col_width, | |
), | |
scaleFactor=dict( | |
valueWidth=field_width, | |
valueType='float', | |
), | |
scaleButton=dict( | |
width="fill", | |
), | |
) | |
self.w = ezui.EZPanel( | |
title="Scale While Slanted", | |
content=content, | |
descriptionData=descriptionData, | |
controller=self | |
) | |
self.w.getNSWindow().setTitlebarAppearsTransparent_(True) | |
self.w.setItemValues(getExtensionDefault(EXTENSION_KEY, fallback=EXTENSION_DEFAULTS)) | |
self.tabs = self.w.getItem("tabs") | |
def started(self): | |
self.w.open() | |
def destroy(self): | |
setExtensionDefault(EXTENSION_KEY, self.w.getItemValues()) | |
def scaleButtonCallback(self, sender): | |
sel_tab_id = self.tabs.getSelectedTab() | |
form_entry = self.w.getItem("alignForm" + sel_tab_id[-1]).getItemValues() | |
self.span = self.w.getItem("spanRadios").get() | |
self.scale_val = self.w.getItem("scaleFactor").get() | |
print(form_entry) | |
origins = { | |
"orientVertRadios": { | |
0: 'top', | |
1: 'center', | |
2: 'bottom' | |
}, | |
"orientHorzRadios": { | |
0: 'left', | |
1: 'center', | |
2: 'right' | |
}, | |
} | |
origin = origins[list(form_entry.keys())[0]][list(form_entry.values())[0]] | |
orientation = list(form_entry.keys())[0] | |
if orientation == "orientVertRadios": | |
scale = (1, self.scale_val) | |
elif orientation == "orientHorzRadios": | |
scale = (self.scale_val, 1) | |
self.scale_glyph(origin, scale) | |
def scale_glyph(self, origin, scale): | |
self.f = CurrentFont() | |
skew_val = 0 | |
if self.f.info.italicAngle: | |
skew_val = radians(-self.f.info.italicAngle) | |
if self.span == 0: | |
self.glyphs = [CurrentGlyph()] | |
else: | |
self.glyphs = [self.f[g_name] for g_name in self.f.selectedGlyphNames] | |
offset_key = 'com.typemytype.robofont.italicSlantOffset' | |
for glyph in self.glyphs: | |
if len(self.glyphs) > 1: | |
contours = [c for c in glyph.contours] | |
elif len(self.glyphs) == 1 and glyph.selectedContours: | |
contours = glyph.selectedContours | |
elif len(self.glyphs) == 1 and glyph.selectedContours == (): | |
contours = [c for c in glyph.contours] | |
else: | |
print("Performing the slanted scale operation on 0 glyphs!") | |
break | |
# # If it’s only current glyph, only transform the selected contours | |
# if len(self.glyphs) == 1: | |
# if glyph.selectedContours: | |
# contours = glyph.selectedContours | |
hold_g = RGlyph() | |
for c in contours: | |
hold_g.appendContour(c) | |
if hold_g: | |
with glyph.undo(f"Skewle contours in {glyph.name}"): | |
min_x = hold_g.leftMargin # min([pt.x for pt in c.points for c in hold_g.contours]) # contours[0].bounds[0] | |
max_x = hold_g.width - hold_g.rightMargin # contours[0].bounds[0] | |
sel_width = hold_g.width | |
min_y = hold_g.bounds[1] # 0 # contours[0].bounds[1] | |
max_y = hold_g.bounds[3] | |
if offset_key in self.f.lib.keys(): | |
# Try to decide whether to offset the transformation, based on whether the bottom-left-most pt == min | |
flag = False | |
for c in hold_g.contours: | |
for pt in c.points: | |
if pt.y == min_y and pt.x == min_x: | |
flag = True | |
break | |
if flag == False: | |
min_x += self.f.lib[offset_key] | |
print(f"Changing the offset of min_x plus {self.f.lib[offset_key]}") | |
origins = { | |
"left": (min_x, (min_y + max_y)/2), | |
"center": ((min_x + max_x)/2, (min_y + max_y)/2), | |
"right": (max_x, (min_y + max_y)/2), | |
"top": ((min_x + max_x)/2, max_y), | |
"bottom": ((min_x + max_x)/2, min_y), | |
} | |
og_x, og_y = origins[origin] | |
t = Transform() | |
t = t.translate(og_x, og_y) | |
t = t.skew(skew_val, 0) | |
t = t.scale(*scale) | |
t = t.skew(-skew_val, 0) | |
t = t.translate(-og_x, -og_y) | |
for c in hold_g.contours: | |
c.transformBy(t) | |
for c in contours: | |
glyph.removeContour(c) | |
glyph.appendGlyph(hold_g) | |
glyph.changed() | |
ScaleWhileSlanted() |
Oh, I see, cleaner this way, thanks @typemytype !
Having weird results with Transform(), but will take a deeper look and make a revision here.
have you tried making a version that work vertically?
have you tried making a version that work vertically?
No, but I'll give it a shot this week
@okay-type Updated, with EZUI! Only uses (left, 0) as origin point for now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
doesnt work with bigger numbers:
fix by first un-skew by the italic angle, add the scale and re-skew by by the inverted italic angle