def _writeBbox(font, svgFontFaceAttrib): """ >>> from defcon import Font >>> font = Font() >>> svgFontFaceAttrib = {} >>> _writeBbox(font, svgFontFaceAttrib) >>> svgFontFaceAttrib {'bbox': '0 0 0 0'} >>> font.newGlyph("A") >>> glyph = font["A"] >>> pen = glyph.getPen() >>> pen.moveTo((-10, -10)) >>> pen.lineTo((-10, 10)) >>> pen.lineTo((10, 10)) >>> pen.lineTo((10, -10)) >>> pen.closePath() >>> _writeBbox(font, svgFontFaceAttrib) >>> svgFontFaceAttrib {'bbox': '-10 -10 10 10'} """ rect = font.bounds if rect is None: rect = (0, 0, 0, 0) rect = [valueToString(i) for i in rect] svgFontFaceAttrib["bbox"] = " ".join(rect)
def _writeHorizAdvX(glyph, attrib): """ >>> from defcon import Glyph >>> attrib = {} >>> glyph = Glyph() >>> glyph.width = 100 >>> _writeHorizAdvX(glyph, attrib) >>> attrib {'horiz-adv-x': '100'} """ assert glyph.width >= 0 attrib["horiz-adv-x"] = valueToString(glyph.width)
def _writeDescent(font, svgFontFaceAttrib): """ >>> from defcon import Font >>> font = Font() >>> svgFontFaceAttrib = {} >>> _writeDescent(font, svgFontFaceAttrib) >>> svgFontFaceAttrib {} >>> font.info.descender = -250 >>> _writeDescent(font, svgFontFaceAttrib) >>> svgFontFaceAttrib {'descent': '-250'} """ if font.info.descender is not None: svgFontFaceAttrib["descent"] = valueToString(font.info.descender)
def _writeXHeight(font, svgFontFaceAttrib): """ >>> from defcon import Font >>> font = Font() >>> svgFontFaceAttrib = {} >>> _writeXHeight(font, svgFontFaceAttrib) >>> svgFontFaceAttrib {} >>> font.info.xHeight = 400 >>> _writeXHeight(font, svgFontFaceAttrib) >>> svgFontFaceAttrib {'x-height': '400'} """ if font.info.xHeight is not None: svgFontFaceAttrib["x-height"] = valueToString(font.info.xHeight)
def _writeCapHeight(font, svgFontFaceAttrib): """ >>> from defcon import Font >>> font = Font() >>> svgFontFaceAttrib = {} >>> _writeCapHeight(font, svgFontFaceAttrib) >>> svgFontFaceAttrib {} >>> font.info.capHeight = 750 >>> _writeCapHeight(font, svgFontFaceAttrib) >>> svgFontFaceAttrib {'cap-height': '750'} """ if font.info.capHeight is not None: svgFontFaceAttrib["cap-height"] = valueToString(font.info.capHeight)
def _writeUnitsPerEm(font, svgFontFaceAttrib): """ >>> from defcon import Font >>> font = Font() >>> svgFontFaceAttrib = {} >>> _writeUnitsPerEm(font, svgFontFaceAttrib) >>> svgFontFaceAttrib {} >>> font.info.unitsPerEm = 1000 >>> _writeUnitsPerEm(font, svgFontFaceAttrib) >>> svgFontFaceAttrib {'units-per-em': '1000'} """ if font.info.unitsPerEm is not None: svgFontFaceAttrib["units-per-em"] = valueToString(font.info.unitsPerEm)
def _writeFontWeight(font, svgFontFaceAttrib): """ >>> from defcon import Font >>> font = Font() >>> svgFontFaceAttrib = {} >>> _writeFontWeight(font, svgFontFaceAttrib) >>> svgFontFaceAttrib {} >>> font.info.openTypeOS2WeightClass = 100 >>> _writeFontWeight(font, svgFontFaceAttrib) >>> svgFontFaceAttrib {'font-weight': '100'} """ if font.info.openTypeOS2WeightClass is not None: svgFontFaceAttrib["font-weight"] = valueToString(font.info.openTypeOS2WeightClass)
def _writeFontWeight(font, svgFontFaceAttrib): """ >>> from defcon import Font >>> font = Font() >>> svgFontFaceAttrib = {} >>> _writeFontWeight(font, svgFontFaceAttrib) >>> svgFontFaceAttrib {} >>> font.info.openTypeOS2WeightClass = 100 >>> _writeFontWeight(font, svgFontFaceAttrib) >>> svgFontFaceAttrib {'font-weight': '100'} """ if font.info.openTypeOS2WeightClass is not None: svgFontFaceAttrib["font-weight"] = valueToString( font.info.openTypeOS2WeightClass)
def _writeDefaultMissingGlyphAttrib(font): """ >>> from defcon import Font >>> font = Font() >>> _writeDefaultMissingGlyphAttrib(font) {'horiz-adv-x': '500'} >>> font.info.unitsPerEm = 2048 >>> _writeDefaultMissingGlyphAttrib(font) {'horiz-adv-x': '1024'} >>> font.info.postscriptDefaultWidthX = 250 >>> _writeDefaultMissingGlyphAttrib(font) {'horiz-adv-x': '250'} """ if font.info.postscriptDefaultWidthX is not None: width = font.info.postscriptDefaultWidthX elif not font.info.unitsPerEm: width = 500 else: width = int(font.info.unitsPerEm * .5) return {"horiz-adv-x" : valueToString(width)}
def _lineTo(self, pt): """ # duplicate point >>> pen = SVGPathPen(None) >>> pen.moveTo((10, 10)) >>> pen.lineTo((10, 10)) >>> pen._commands ['M10 10'] # vertical line >>> pen = SVGPathPen(None) >>> pen.moveTo((10, 10)) >>> pen.lineTo((10, 0)) >>> pen._commands ['M10 10', 'V0'] # horizontal line >>> pen = SVGPathPen(None) >>> pen.moveTo((10, 10)) >>> pen.lineTo((0, 10)) >>> pen._commands ['M10 10', 'H0'] # basic >>> pen = SVGPathPen(None) >>> pen.lineTo((70, 80)) >>> pen._commands ['L70 80'] # basic following a moveto >>> pen = SVGPathPen(None) >>> pen.moveTo((0, 0)) >>> pen.lineTo((10, 10)) >>> pen._commands ['M0 0', ' 10 10'] """ x, y = pt # duplicate point if x == self._lastX and y == self._lastY: return # vertical line elif x == self._lastX: cmd = "V" pts = valueToString(y) # horizontal line elif y == self._lastY: cmd = "H" pts = valueToString(x) # previous was a moveto elif self._lastCommand == "M": cmd = None pts = " " + pointToString(pt) # basic else: cmd = "L" pts = pointToString(pt) # write the string t = "" if cmd: t += cmd self._lastCommand = cmd t += pts self._commands.append(t) # store for future reference self._lastX, self._lastY = pt