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

Add metrics to transformations filter #238

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
58 changes: 26 additions & 32 deletions Lib/ufo2ft/filters/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ def start(self):
if self.options.Origin not in self.Origin:
raise ValueError("%r is not a valid Origin value"
% self.options.Origin)
metrics = dict()
options_to_ufo_metrics = dict(
Width="width",
LSB="leftMargin",
RSB="rightMargin",
Height="height",
TSB="topMargin",
BSB="bottomMargin",
VerticalOrigin="verticalOrigin",
)
for option, attr in options_to_ufo_metrics.items():
value = getattr(self.options, option)
if value is not None:
metrics[attr] = value
self.metrics = metrics

def get_origin_height(self, font, origin):
if origin is self.Origin.BASELINE:
Expand All @@ -100,23 +115,6 @@ def set_context(self, font, glyphSet):

origin_height = self.get_origin_height(font, self.options.Origin)

# Metrics
ctx.metrics = dict()
if self.options.Width:
ctx.metrics["width"] = self.options.Width
if self.options.RSB:
ctx.metrics["rightMargin"] = self.options.RSB
if self.options.LSB:
ctx.metrics["leftMargin"] = self.options.LSB
if self.options.Height:
ctx.metrics["height"] = self.options.Height
if self.options.TSB:
ctx.metrics["topMargin"] = self.options.TSB
if self.options.BSB:
ctx.metrics["bottomMargin"] = self.options.BSB
if self.options.VerticalOrigin:
ctx.metrics["verticalOrigin"] = self.options.VerticalOrigin

m = Identity
dx, dy = self.options.OffsetX, self.options.OffsetY
if dx != 0 or dy != 0:
Expand All @@ -143,7 +141,7 @@ def set_context(self, font, glyphSet):
return ctx

def filter(self, glyph, isComponent=False):
metrics = self.context.metrics
metrics = self.metrics
matrix = self.context.matrix
if ((not metrics and matrix == Identity) or
not (glyph or glyph.components or glyph.anchors)):
Expand All @@ -164,20 +162,16 @@ def filter(self, glyph, isComponent=False):
modified.add(base_name)

if not isComponent:
for attr in ["height", "width",
"leftMargin", "rightMargin",
"topMargin", "bottomMargin",
"verticalOrigin"]:
if attr in metrics:
value = getattr(glyph, attr)
if value is not None:
value += metrics.get(attr)
setattr(glyph, attr, value)
else:
logger.warning(
"Cannot add %i to undefined %s in %s",
metrics.get(attr), attr, glyph.name
)
for attr, value in metrics.items():
current_value = getattr(glyph, attr)
if current_value is not None:
value += current_value
setattr(glyph, attr, value)
else:
logger.warning(
"Cannot add %i to undefined %s in %s",
value, attr, glyph.name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor suggestion: you don’t need to increment value +=current_value, just setattr(glyph, attr, current_value+value)

)

rec = RecordingPen()
glyph.draw(rec)
Expand Down