#!/usr/bin/env python3
'IPython support for fonttools'
__all__ = ['displayGlyphs', 'loadFont', 'displayText', 'displayRaw']
from fontTools import ttLib
from fontTools.pens.basePen import BasePen
from fontTools.misc import arrayTools
from IPython.display import SVG, HTML
from defcon import Font
from ufo2ft import compileTTF
class SVGPen(BasePen) :
def __init__(self, glyphSet, scale=1.0) :
super(SVGPen, self).__init__(glyphSet);
self.__commands = []
self.__scale = scale
def __str__(self) :
return " ".join(self.__commands)
def scale(self, pt) :
return ((pt[0] or 0) * self.__scale, (pt[1] or 0) * self.__scale)
def _moveTo(self, pt):
self.__commands.append("M {0[0]} {0[1]}".format(self.scale(pt)))
def _lineTo(self, pt):
self.__commands.append("L {0[0]} {0[1]}".format(self.scale(pt)))
def _curveToOne(self, pt1, pt2, pt3) :
self.__commands.append("C {0[0]} {0[1]} {1[0]} {1[1]} {2[0]} {2[1]}".format(self.scale(pt1), self.scale(pt2), self.scale(pt3)))
def _closePath(self) :
self.__commands.append("Z")
def clear(self) :
self.__commands = []
def _svgheader():
return '''
\n'
return SVG(data=res)
#return res
def displayText(f, text, features = [], lang=None, dir="", script="", shapers="", size=0):
import harfbuzz
glyphs = harfbuzz.shape_text(f, text, features, lang, dir, script, shapers)
gnames = []
points = []
x = 0
y = 0
for g in glyphs:
gnames.append(f.getGlyphName(g.gid))
points.append((x+g.offset[0], y+g.offset[1]))
x += g.advance[0]
y += g.advance[1]
if size == 0:
scale = None
else:
upem = f['head'].unitsPerEm
scale = 4. * size / (upem * 3.)
return displayGlyphs(f, gnames, points, scale=scale)
def displayRaw(text):
# res = "
"+text.encode('utf-8')+""
res = u""+text+u"
"
return HTML(data=res)