summaryrefslogtreecommitdiffstats
path: root/lib/silfont/ipython.py
blob: 150aa979c44fa4ceb8a8b53149acd798af0311a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/python
'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 '''<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
'''

def _bbox(f, gnames, points, scale=1):
    gset = f.glyphSet
    bbox = (0, 0, 0, 0)
    for i, gname in enumerate(gnames):
        if hasattr(points, '__len__') and i == len(points):
            points.append((bbox[2] / scale, 0))
        pt = points[i] if i < len(points) else (0, 0)
        g = gset[gname]._glyph
        if g is None or not hasattr(g, 'xMin') :
            gbox = (0, 0, 0, 0)
        else :
            gbox = (g.xMin * scale, g.yMin * scale, g.xMax * scale, g.yMax * scale)
        bbox = arrayTools.unionRect(bbox, arrayTools.offsetRect(gbox, pt[0] * scale, pt[1] * scale))
    return bbox

glyphsetcount = 0
def _defglyphs(f, gnames, scale=1):
    global glyphsetcount
    glyphsetcount += 1
    gset = f.glyphSet
    p = SVGPen(gset, scale)
    res = "<defs><g>\n"
    for gname in sorted(set(gnames)):
        res += '<symbol overflow="visible" id="{}_{}">\n'.format(gname, glyphsetcount)
        g = gset[gname]
        p.clear()
        g.draw(p)
        res += '<path style="stroke:none;" d="' + str(p) + '"/>\n</symbol>\n'
    res += "</g></defs>\n"
    return res

def loadFont(fname):
    if fname.lower().endswith(".ufo"):
        ufo = Font(fname)
        f = compileTTF(ufo)
    else:
        f = ttLib.TTFont(fname)
    return f

def displayGlyphs(f, gnames, points=None, scale=None):
    if not hasattr(gnames, '__len__') or isinstance(gnames, basestring):
        gnames = [gnames]
    if not hasattr(points, '__len__'):
        points = []
    if not hasattr(f, 'glyphSet'):
        f.glyphSet = f.getGlyphSet()
    res = _svgheader()
    if points is None:
        points = []
    bbox = _bbox(f, gnames, points, scale or 1)
    maxh = 100.
    height = bbox[3] - (bbox[1] if bbox[1] < 0 else 0)
    if scale is None and height > maxh:
        scale = maxh / height
        bbox = [x  * scale for x in bbox]
    res += _defglyphs(f, gnames, scale)
    res += '<g id="surface1" transform="matrix(1,0,0,-1,{},{})">\n'.format(-bbox[0], bbox[3])
    res += '  <rect x="{}" y="{}" width="{}" height="{}" style="fill:white;stroke:none"/>\n'.format(
        bbox[0], bbox[1], bbox[2]-bbox[0], bbox[3])
    res += '  <g style="fill:black">\n'
    for i, gname in enumerate(gnames):
        pt = points[i] if i < len(points) else (0, 0)
        res += '    <use xlink:href="#{0}_{3}" x="{1}" y="{2}"/>\n'.format(gname, pt[0] * scale, pt[1] * scale, glyphsetcount)
    res += '  </g></g>\n</svg>\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 = "<html><body>"+text.encode('utf-8')+"</body></html>"
    res = u"<html><body><p>"+text+u"</p></body></html>"
    return HTML(data=res)