-
Notifications
You must be signed in to change notification settings - Fork 11
/
demo.py
73 lines (65 loc) · 2.23 KB
/
demo.py
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
from AppKit import *
from fontTools.pens.cocoaPen import CocoaPen
from compositor import Font
# a simple function that implements path caching
def getCachedNSBezierPath(glyph, font):
if not hasattr(glyph, "nsBezierPath"):
pen = CocoaPen(font)
glyph.draw(pen)
glyph.nsBezierPath = pen.path
return glyph.nsBezierPath
# a path to a font
fontPath = aPathToYourFont
# a path to save the image to
imagePath = "demo.tiff"
# setup the layout engine
font = Font(fontPath)
# turn the aalt feature on so that we get any alternates
font.setFeatureState("aalt", True)
# process some text
glyphRecords = font.process(u"HERE IS SOME TEXT!")
# calculate the image size
pointSize = 50.0
offset = 20
scale = pointSize / font.info.unitsPerEm
imageWidth = sum([font[record.glyphName].width + record.xAdvance for record in glyphRecords]) * scale
imageWidth = int(round(imageWidth))
imageWidth += offset * 2
imageHeight = pointSize + (offset * 2)
# setup the image
image = NSImage.alloc().initWithSize_((imageWidth, imageHeight))
image.lockFocus()
# fill it with white
NSColor.whiteColor().set()
NSRectFill(((0, 0), (imageWidth, imageHeight)))
# offset and set the scale
transform = NSAffineTransform.transform()
transform.translateXBy_yBy_(offset, offset)
transform.scaleBy_(scale)
transform.translateXBy_yBy_(0, abs(font.info.descender))
transform.concat()
# iterate over the glyph records
for record in glyphRecords:
glyph = font[record.glyphName]
# shift for x and y placement
transform = NSAffineTransform.transform()
transform.translateXBy_yBy_(record.xPlacement, record.yPlacement)
transform.concat()
# if alternates are present, switch the color
if record.alternates:
NSColor.redColor().set()
# otherwise, set the color to black
else:
NSColor.blackColor().set()
# get a NSBezierPath for the glyph and fill it
path = getCachedNSBezierPath(glyph, font)
path.fill()
# shift for the next glyph
transform = NSAffineTransform.transform()
transform.translateXBy_yBy_(record.xAdvance + glyph.width, -record.yPlacement)
transform.concat()
# release the image
image.unlockFocus()
# write the image to disk
tiff = image.TIFFRepresentation()
tiff.writeToFile_atomically_(imagePath, False)