This adds a simple test of the MarginPen object. Basically to make sure the update to new style object doesn't cause any problems. Can't tell because AngledMarginPen seems to have other internal problems.
git-svn-id: http://svn.robofab.com/branches/ufo3k@569 b5fa9d6c-a76f-4ffd-b3cb-f825fc41095c
This commit is contained in:
parent
ab3c6bca2f
commit
8c864d3171
@ -1,129 +1,144 @@
|
|||||||
from robofab.world import RFont
|
from robofab.world import RFont
|
||||||
from fontTools.pens.basePen import BasePen
|
from fontTools.pens.basePen import BasePen
|
||||||
from robofab.misc.arrayTools import updateBounds, pointInRect, unionRect
|
from robofab.misc.arrayTools import updateBounds, pointInRect, unionRect
|
||||||
from robofab.misc.bezierTools import calcCubicBounds, calcQuadraticBounds
|
from robofab.misc.bezierTools import calcCubicBounds, calcQuadraticBounds
|
||||||
from robofab.pens.filterPen import _estimateCubicCurveLength, _getCubicPoint
|
from robofab.pens.filterPen import _estimateCubicCurveLength, _getCubicPoint
|
||||||
import math
|
import math
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = ["AngledMarginPen", "getAngledMargins",
|
__all__ = ["AngledMarginPen", "getAngledMargins",
|
||||||
"setAngledLeftMargin", "setAngledRightMargin",
|
"setAngledLeftMargin", "setAngledRightMargin",
|
||||||
"centerAngledMargins"]
|
"centerAngledMargins"]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class AngledMarginPen(BasePen):
|
class AngledMarginPen(BasePen):
|
||||||
"""
|
"""
|
||||||
Pen to calculate the margins according to a slanted coordinate system. Slant angle comes from font.info.italicAngle.
|
Pen to calculate the margins according to a slanted coordinate system. Slant angle comes from font.info.italicAngle.
|
||||||
|
|
||||||
- this pen works on the on-curve points, and approximates the distance to curves.
|
- this pen works on the on-curve points, and approximates the distance to curves.
|
||||||
- results will be float.
|
- results will be float.
|
||||||
- when used in FontLab, the resulting margins may be slightly different from the values originally set, due to rounding errors.
|
- when used in FontLab, the resulting margins may be slightly different from the values originally set, due to rounding errors.
|
||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
|
|
||||||
- similar to what RoboFog used to do.
|
- similar to what RoboFog used to do.
|
||||||
- RoboFog had a special attribute for "italicoffset", horizontal shift of all glyphs. This is missing in Robofab.
|
- RoboFog had a special attribute for "italicoffset", horizontal shift of all glyphs. This is missing in Robofab.
|
||||||
"""
|
"""
|
||||||
def __init__(self, glyphSet, width, italicAngle):
|
def __init__(self, glyphSet, width, italicAngle):
|
||||||
BasePen.__init__(self, glyphSet)
|
BasePen.__init__(self, glyphSet)
|
||||||
self.width = width
|
self.width = width
|
||||||
self._angle = math.radians(90+italicAngle)
|
self._angle = math.radians(90+italicAngle)
|
||||||
self.maxSteps = 100
|
self.maxSteps = 100
|
||||||
self.margin = None
|
self.margin = None
|
||||||
self._left = None
|
self._left = None
|
||||||
self._right = None
|
self._right = None
|
||||||
self._start = None
|
self._start = None
|
||||||
self.currentPt = None
|
self.currentPt = None
|
||||||
|
|
||||||
def _getAngled(self, pt):
|
def _getAngled(self, pt):
|
||||||
r = (g.width + (pt[1] / math.tan(self._angle)))-pt[0]
|
print "_getAngled", pt
|
||||||
l = pt[0]-((pt[1] / math.tan(self._angle)))
|
r = (self.width + (pt[1] / math.tan(self._angle)))-pt[0]
|
||||||
if self._right is None:
|
l = pt[0]-((pt[1] / math.tan(self._angle)))
|
||||||
self._right = r
|
if self._right is None:
|
||||||
else:
|
self._right = r
|
||||||
self._right = min(self._right, r)
|
else:
|
||||||
if self._left is None:
|
self._right = min(self._right, r)
|
||||||
self._left = l
|
if self._left is None:
|
||||||
else:
|
self._left = l
|
||||||
self._left = min(self._left, l)
|
else:
|
||||||
#print pt, l, r
|
self._left = min(self._left, l)
|
||||||
self.margin = self._left, self._right
|
self.margin = self._left, self._right
|
||||||
|
|
||||||
def _moveTo(self, pt):
|
def _moveTo(self, pt):
|
||||||
self._start = self.currentPt = pt
|
self._start = self.currentPt = pt
|
||||||
|
|
||||||
def _addMoveTo(self):
|
def _addMoveTo(self):
|
||||||
if self._start is None:
|
if self._start is None:
|
||||||
return
|
return
|
||||||
self._start = self.currentPt = None
|
self._start = self.currentPt = None
|
||||||
|
|
||||||
def _lineTo(self, pt):
|
def _lineTo(self, pt):
|
||||||
self._addMoveTo()
|
self._addMoveTo()
|
||||||
self._getAngled(pt)
|
print "_lineTo"
|
||||||
|
self._getAngled(pt)
|
||||||
def _curveToOne(self, pt1, pt2, pt3):
|
|
||||||
step = 1.0/self.maxSteps
|
def _curveToOne(self, pt1, pt2, pt3):
|
||||||
factors = range(0, self.maxSteps+1)
|
step = 1.0/self.maxSteps
|
||||||
for i in factors:
|
factors = range(0, self.maxSteps+1)
|
||||||
pt = _getCubicPoint(i*step, self.currentPt, pt1, pt2, pt3)
|
for i in factors:
|
||||||
self._getAngled(pt)
|
print "_curveToOne", i
|
||||||
self.currentPt = pt3
|
pt = _getCubicPoint(i*step, self.currentPt, pt1, pt2, pt3)
|
||||||
|
self._getAngled(pt)
|
||||||
def _qCurveToOne(self, bcp, pt):
|
self.currentPt = pt3
|
||||||
self._addMoveTo()
|
|
||||||
# add curve tracing magic here.
|
def _qCurveToOne(self, bcp, pt):
|
||||||
self._getAngled(pt)
|
self._addMoveTo()
|
||||||
self.currentPt = pt3
|
# add curve tracing magic here.
|
||||||
|
print "_qCurveToOne"
|
||||||
def getAngledMargins(glyph, font):
|
self._getAngled(pt)
|
||||||
"""Convenience function, returns the angled margins for this glyph. Adjusted for font.info.italicAngle."""
|
self.currentPt = pt3
|
||||||
pen = AngledMarginPen(font, glyph.width, font.info.italicAngle)
|
|
||||||
glyph.draw(pen)
|
def getAngledMargins(glyph, font):
|
||||||
return pen.margin
|
"""Convenience function, returns the angled margins for this glyph. Adjusted for font.info.italicAngle."""
|
||||||
|
pen = AngledMarginPen(font, glyph.width, font.info.italicAngle)
|
||||||
def setAngledLeftMargin(glyph, font, value):
|
glyph.draw(pen)
|
||||||
"""Convenience function, sets the left angled margin to value. Adjusted for font.info.italicAngle."""
|
return pen.margin
|
||||||
pen = AngledMarginPen(font, glyph.width, font.info.italicAngle)
|
|
||||||
g.draw(pen)
|
def setAngledLeftMargin(glyph, font, value):
|
||||||
isLeft, isRight = pen.margin
|
"""Convenience function, sets the left angled margin to value. Adjusted for font.info.italicAngle."""
|
||||||
glyph.leftMargin += value-isLeft
|
pen = AngledMarginPen(font, glyph.width, font.info.italicAngle)
|
||||||
|
g.draw(pen)
|
||||||
def setAngledRightMargin(glyph, font, value):
|
isLeft, isRight = pen.margin
|
||||||
"""Convenience function, sets the right angled margin to value. Adjusted for font.info.italicAngle."""
|
glyph.leftMargin += value-isLeft
|
||||||
pen = AngledMarginPen(font, glyph.width, font.info.italicAngle)
|
|
||||||
g.draw(pen)
|
def setAngledRightMargin(glyph, font, value):
|
||||||
isLeft, isRight = pen.margin
|
"""Convenience function, sets the right angled margin to value. Adjusted for font.info.italicAngle."""
|
||||||
glyph.rightMargin += value-isRight
|
pen = AngledMarginPen(font, glyph.width, font.info.italicAngle)
|
||||||
|
g.draw(pen)
|
||||||
def centerAngledMargins(glyph, font):
|
isLeft, isRight = pen.margin
|
||||||
"""Convenience function, centers the glyph on angled margins."""
|
glyph.rightMargin += value-isRight
|
||||||
pen = AngledMarginPen(font, glyph.width, font.info.italicAngle)
|
|
||||||
g.draw(pen)
|
def centerAngledMargins(glyph, font):
|
||||||
isLeft, isRight = pen.margin
|
"""Convenience function, centers the glyph on angled margins."""
|
||||||
setAngledLeftMargin(glyph, font, (isLeft+isRight)*.5)
|
pen = AngledMarginPen(font, glyph.width, font.info.italicAngle)
|
||||||
setAngledRightMargin(glyph, font, (isLeft+isRight)*.5)
|
g.draw(pen)
|
||||||
|
isLeft, isRight = pen.margin
|
||||||
def guessItalicOffset(glyph, font):
|
setAngledLeftMargin(glyph, font, (isLeft+isRight)*.5)
|
||||||
"""Guess the italic offset based on the margins of a symetric glyph.
|
setAngledRightMargin(glyph, font, (isLeft+isRight)*.5)
|
||||||
For instance H or I.
|
|
||||||
"""
|
def guessItalicOffset(glyph, font):
|
||||||
l, r = getAngledMargins(glyph, font)
|
"""Guess the italic offset based on the margins of a symetric glyph.
|
||||||
return l - (l+r)*.5
|
For instance H or I.
|
||||||
|
"""
|
||||||
|
l, r = getAngledMargins(glyph, font)
|
||||||
if __name__ == "__main__":
|
return l - (l+r)*.5
|
||||||
|
|
||||||
# example for FontLab, with a glyph open.
|
|
||||||
from robofab.world import CurrentFont, CurrentGlyph
|
if __name__ == "__main__":
|
||||||
g = CurrentGlyph()
|
|
||||||
f = CurrentFont()
|
def makeTestGlyph():
|
||||||
|
# make a simple glyph that we can test the pens with.
|
||||||
print "margins!", getAngledMargins(g, f)
|
from robofab.objects.objectsRF import RGlyph
|
||||||
# set the angled margin to a value
|
testGlyph = RGlyph()
|
||||||
m = 50
|
testGlyph.name = "testGlyph"
|
||||||
setAngledLeftMargin(g, f, m)
|
testGlyph.width = 1000
|
||||||
setAngledRightMargin(g, f, m)
|
pen = testGlyph.getPen()
|
||||||
g.update()
|
pen.moveTo((100, 100))
|
||||||
|
pen.lineTo((900, 100))
|
||||||
|
pen.lineTo((900, 800))
|
||||||
|
pen.lineTo((100, 800))
|
||||||
|
# a curve
|
||||||
|
pen.curveTo((120, 700), (120, 300), (100, 100))
|
||||||
|
pen.closePath()
|
||||||
|
return testGlyph
|
||||||
|
|
||||||
|
def angledMarginPenTest():
|
||||||
|
testGlyph = makeTestGlyph()
|
||||||
|
glyphSet = {}
|
||||||
|
testPen = AngledMarginPen(glyphSet, width=0, italicAngle=10)
|
||||||
|
testGlyph.draw(testPen)
|
||||||
|
print testPen.margin
|
||||||
|
|
||||||
|
angledMarginPenTest()
|
Loading…
x
Reference in New Issue
Block a user