Created
October 1, 2019 13:14
-
-
Save typemytype/52de64953b7486ae9090f19525e22c9b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| from fontMath.mathGlyph import MathGlyph, _expandImage | |
| def translate(self, pt): | |
| x, y = pt | |
| copiedGlyph = self.copyWithoutMathSubObjects() | |
| # width | |
| copiedGlyph.width = self.width + x | |
| # height | |
| copiedGlyph.height = self.height + y | |
| # contours | |
| copiedGlyph.contours = [] | |
| if self.contours: | |
| result = [] | |
| for contour in self.contours: | |
| resultPoints = [] | |
| for point in contour["points"]: | |
| segmentType, (ptx, pty), smooth, name, identifier = point | |
| resultPoints.append((segmentType, (ptx + x, pty + y), smooth, name, identifier)) | |
| result.append(dict(identifier=contour["identifier"], points=resultPoints)) | |
| copiedGlyph.contours = result | |
| # components | |
| copiedGlyph.components = [] | |
| if self.components: | |
| result = [] | |
| for component in self.components: | |
| component = dict(component) | |
| xx, xy, yx, yy, tx, ty = component["transformation"] | |
| component["transformation"] = xx, xy, yx, yy, tx + x, ty + y | |
| result.append(component) | |
| copiedGlyph.components = result | |
| # anchors | |
| copiedGlyph.anchors = [] | |
| if self.anchors: | |
| result = [] | |
| for anchor in self.anchors: | |
| anchor = dict(anchor) | |
| anchor["x"] += x | |
| anchor["y"] += y | |
| result.append(anchor) | |
| opiedGlyph.anchors = result | |
| # guidelines | |
| copiedGlyph.guidelines = [] | |
| if self.guidelines: | |
| result = [] | |
| for guideline in self.guidelines: | |
| guideline = dict(guideline) | |
| guideline["x"] += x | |
| guideline["y"] += y | |
| result.append(guideline) | |
| copiedGlyph.guidelines = result | |
| # image | |
| copiedGlyph.image = _expandImage(self.image) | |
| if self.image: | |
| copiedGlyph.image = image = _expandImage(None) | |
| xx, xy, yx, yy, tx, ty = image["transformation"] | |
| image["transformation"] = xx, xy, yx, yy, tx + x, ty + y | |
| else: | |
| copiedGlyph.image = _expandImage(None) | |
| return copiedGlyph | |
| ### | |
| from defcon import Glyph | |
| from fontPens.digestPointPen import DigestPointPen | |
| dummy = Glyph() | |
| pen = dummy.getPen() | |
| pen.moveTo((100, 100)) | |
| pen.lineTo((200, 100)) | |
| pen.lineTo((200, 200)) | |
| pen.closePath() | |
| g = MathGlyph(dummy) | |
| r = translate(g, (10, 10)) | |
| p = DigestPointPen() | |
| r.drawPoints(p) | |
| print(p.getDigest()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment