-
Notifications
You must be signed in to change notification settings - Fork 27
/
emoji_builder.py
executable file
·496 lines (401 loc) · 14.6 KB
/
emoji_builder.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/usr/bin/python
#
# Copyright 2013 Google, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Google Author(s): Behdad Esfahbod, Stuart Gill
#
import sys, struct, StringIO
from png import PNG
def div (a, b):
return int (round (a / float (b)))
class FontMetrics:
def __init__ (self, upem, ascent, descent):
self.upem = upem
self.ascent = ascent
self.descent = descent
class StrikeMetrics:
def __init__ (self, font_metrics, advance, bitmap_width, bitmap_height):
self.width = bitmap_width # in pixels
self.height = bitmap_height # in pixels
self.advance = advance # in font units
self.x_ppem = self.y_ppem = div (bitmap_width * font_metrics.upem, advance)
class GlyphMap:
def __init__ (self, glyph, offset, image_format):
self.glyph = glyph
self.offset = offset
self.image_format = image_format
# Based on http://www.microsoft.com/typography/otspec/ebdt.htm
class CBDT:
def __init__ (self, font_metrics, options = (), stream = None):
self.stream = stream if stream != None else bytearray ()
self.options = options
self.font_metrics = font_metrics
self.base_offset = 0
self.base_offset = self.tell ()
def tell (self):
return len (self.stream) - self.base_offset
def write (self, data):
self.stream.extend (data)
def data (self):
return self.stream
def write_header (self):
self.write (struct.pack (">L", 0x00020000)) # FIXED version
def start_strike (self, strike_metrics):
self.strike_metrics = strike_metrics
self.glyph_maps = []
def write_glyphs (self, glyphs, glyph_filenames, image_format):
write_func = self.image_write_func (image_format)
for glyph in glyphs:
img_file = glyph_filenames[glyph]
offset = self.tell ()
write_func (PNG (img_file))
self.glyph_maps.append (GlyphMap (glyph, offset, image_format))
def end_strike (self):
self.glyph_maps.append (GlyphMap (None, self.tell (), None))
glyph_maps = self.glyph_maps
del self.glyph_maps
del self.strike_metrics
return glyph_maps
def write_smallGlyphMetrics (self, width, height):
ascent = self.font_metrics.ascent
descent = self.font_metrics.descent
upem = self.font_metrics.upem
y_ppem = self.strike_metrics.y_ppem
x_bearing = 0
# center vertically
line_height = (ascent + descent) * y_ppem / float (upem)
line_ascent = ascent * y_ppem / float (upem)
y_bearing = int (round (line_ascent - .5 * (line_height - height)))
advance = width
# smallGlyphMetrics
# Type Name
# BYTE height
# BYTE width
# CHAR BearingX
# CHAR BearingY
# BYTE Advance
self.write (struct.pack ("BBbbB",
height, width,
x_bearing, y_bearing,
advance))
def write_format1 (self, png):
import cairo
img = cairo.ImageSurface.create_from_png (png.stream ())
if img.get_format () != cairo.FORMAT_ARGB32:
raise Exception ("Expected FORMAT_ARGB32, but image has format %d" % img.get_format ())
width = img.get_width ()
height = img.get_height ()
stride = img.get_stride ()
data = img.get_data ()
self.write_smallGlyphMetrics (width, height)
if sys.byteorder == "little" and stride == width * 4:
# Sweet. Data is in desired format, ship it!
self.write (data)
return
# Unexpected stride or endianness, do it the slow way
offset = 0
for y in range (height):
for x in range (width):
pixel = data[offset + 4 * x: offset + 4 * (x + 1)]
# Convert to little endian
pixel = struct.pack ("<I", struct.unpack ("@I", pixel)[0])
self.write (pixel)
offset += stride
png_allowed_chunks = ["IHDR", "PLTE", "tRNS", "sRGB", "IDAT", "IEND"]
def write_format17 (self, png):
width, height = png.get_size ()
if 'keep_chunks' not in self.options:
png = png.filter_chunks (self.png_allowed_chunks)
self.write_smallGlyphMetrics (width, height)
png_data = png.data ()
# ULONG data length
self.write (struct.pack(">L", len (png_data)))
self.write (png_data)
def image_write_func (self, image_format):
if image_format == 1: return self.write_format1
if image_format == 17: return self.write_format17
return None
# Based on http://www.microsoft.com/typography/otspec/eblc.htm
class CBLC:
def __init__ (self, font_metrics, options = (), stream = None):
self.stream = stream if stream != None else bytearray ()
self.streams = []
self.options = options
self.font_metrics = font_metrics
self.base_offset = 0
self.base_offset = self.tell ()
def tell (self):
return len (self.stream) - self.base_offset
def write (self, data):
self.stream.extend (data)
def data (self):
return self.stream
def push_stream (self, stream):
self.streams.append (self.stream)
self.stream = stream
def pop_stream (self):
stream = self.stream
self.stream = self.streams.pop ()
return stream
def write_header (self):
self.write (struct.pack (">L", 0x00020000)) # FIXED version
def start_strikes (self, num_strikes):
self.num_strikes = num_strikes
self.write (struct.pack (">L", self.num_strikes)) # ULONG numSizes
self.bitmapSizeTables = bytearray ()
self.otherTables = bytearray ()
def write_strike (self, strike_metrics, glyph_maps):
self.strike_metrics = strike_metrics
self.write_bitmapSizeTable (glyph_maps)
del self.strike_metrics
def end_strikes (self):
self.write (self.bitmapSizeTables)
self.write (self.otherTables)
del self.bitmapSizeTables
del self.otherTables
def write_sbitLineMetrics_hori (self):
ascent = self.font_metrics.ascent
descent = self.font_metrics.descent
upem = self.font_metrics.upem
y_ppem = self.strike_metrics.y_ppem
# sbitLineMetrics
# Type Name
# CHAR ascender
# CHAR descender
# BYTE widthMax
# CHAR caretSlopeNumerator
# CHAR caretSlopeDenominator
# CHAR caretOffset
# CHAR minOriginSB
# CHAR minAdvanceSB
# CHAR maxBeforeBL
# CHAR minAfterBL
# CHAR pad1
# CHAR pad2
line_height = div ((ascent + descent) * y_ppem, upem)
ascent = div (ascent * y_ppem, upem)
descent = - (line_height - ascent)
self.write (struct.pack ("bbBbbbbbbbbb",
ascent, descent,
self.strike_metrics.width,
0, 0, 0,
0, 0, 0, 0, # TODO
0, 0))
def write_sbitLineMetrics_vert (self):
self.write_sbitLineMetrics_hori () # XXX
def write_indexSubTable1 (self, glyph_maps):
image_format = glyph_maps[0].image_format
self.write (struct.pack(">H", 1)) # USHORT indexFormat
self.write (struct.pack(">H", image_format)) # USHORT imageFormat
imageDataOffset = glyph_maps[0].offset
self.write (struct.pack(">L", imageDataOffset)) # ULONG imageDataOffset
for gmap in glyph_maps[:-1]:
self.write (struct.pack(">L", gmap.offset - imageDataOffset)) # ULONG offsetArray
assert gmap.image_format == image_format
self.write (struct.pack(">L", glyph_maps[-1].offset - imageDataOffset))
def write_bitmapSizeTable (self, glyph_maps):
# count number of ranges
count = 1
start = glyph_maps[0].glyph
last_glyph = start
last_image_format = glyph_maps[0].image_format
for gmap in glyph_maps[1:-1]:
if last_glyph + 1 != gmap.glyph or last_image_format != gmap.image_format:
count += 1
last_glyph = gmap.glyph
last_image_format = gmap.image_format
headersLen = count * 8
headers = bytearray ()
subtables = bytearray ()
start = glyph_maps[0].glyph
start_id = 0
last_glyph = start
last_image_format = glyph_maps[0].image_format
last_id = 0
for gmap in glyph_maps[1:-1]:
if last_glyph + 1 != gmap.glyph or last_image_format != gmap.image_format:
headers.extend (struct.pack(">HHL", start, last_glyph, headersLen + len (subtables)))
self.push_stream (subtables)
self.write_indexSubTable1 (glyph_maps[start_id:last_id+2])
self.pop_stream ()
start = gmap.glyph
start_id = last_id + 1
last_glyph = gmap.glyph
last_image_format = gmap.image_format
last_id += 1
headers.extend (struct.pack(">HHL", start, last_glyph, headersLen + len (subtables)))
self.push_stream (subtables)
self.write_indexSubTable1 (glyph_maps[start_id:last_id+2])
self.pop_stream ()
indexTablesSize = len (headers) + len (subtables)
numberOfIndexSubTables = count
bitmapSizeTableSize = 48 * self.num_strikes
indexSubTableArrayOffset = 8 + bitmapSizeTableSize + len (self.otherTables)
self.push_stream (self.bitmapSizeTables)
# bitmapSizeTable
# Type Name Description
# ULONG indexSubTableArrayOffset offset to index subtable from beginning of CBLC.
self.write (struct.pack(">L", indexSubTableArrayOffset))
# ULONG indexTablesSize number of bytes in corresponding index subtables and array
self.write (struct.pack(">L", indexTablesSize))
# ULONG numberOfIndexSubTables an index subtable for each range or format change
self.write (struct.pack(">L", numberOfIndexSubTables))
# ULONG colorRef not used; set to 0.
self.write (struct.pack(">L", 0))
# sbitLineMetrics hori line metrics for text rendered horizontally
self.write_sbitLineMetrics_hori ()
self.write_sbitLineMetrics_vert ()
# sbitLineMetrics vert line metrics for text rendered vertically
# USHORT startGlyphIndex lowest glyph index for this size
self.write (struct.pack(">H", glyph_maps[0].glyph))
# USHORT endGlyphIndex highest glyph index for this size
self.write (struct.pack(">H", glyph_maps[-2].glyph))
# BYTE ppemX horizontal pixels per Em
self.write (struct.pack(">B", self.strike_metrics.x_ppem))
# BYTE ppemY vertical pixels per Em
self.write (struct.pack(">B", self.strike_metrics.y_ppem))
# BYTE bitDepth the Microsoft rasterizer v.1.7 or greater supports the
# following bitDepth values, as described below: 1, 2, 4, and 8.
self.write (struct.pack(">B", 32))
# CHAR flags vertical or horizontal (see bitmapFlags)
self.write (struct.pack(">b", 0x01))
self.pop_stream ()
self.push_stream (self.otherTables)
self.write (headers)
self.write (subtables)
self.pop_stream ()
def main (argv):
import glob
from fontTools import ttx, ttLib
options = []
option_map = {
"-V": "verbose",
"-O": "keep_outlines",
"-U": "uncompressed",
"-C": "keep_chunks",
}
for key, value in option_map.items ():
if key in argv:
options.append (value)
argv.remove (key)
if len (argv) < 4:
print >>sys.stderr, """
Usage:
emoji_builder.py [-V] [-O] [-U] [-A] font.ttf out-font.ttf strike-prefix...
This will search for files that have strike-prefix followed
by a hex number, and end in ".png". For example, if strike-prefix
is "icons/uni", then files with names like "icons/uni1f4A9.png" will
be loaded. All images for the same strike should have the same size
for best results.
If multiple strike-prefix parameters are provided, multiple
strikes will be embedded, in the order provided.
The script then embeds color bitmaps in the font, for characters
that the font already supports, and writes the new font out.
If -V is given, verbose mode is enabled.
If -U is given, uncompressed images are stored (imageFormat=1).
By default, PNG images are stored (imageFormat=17).
If -O is given, the outline tables ('glyf', 'CFF ') and
related tables are NOT dropped from the font.
By default they are dropped.
If -C is given, unused chunks (color profile, etc) are NOT
dropped from the PNG images when embedding.
By default they are dropped.
"""
sys.exit (1)
font_file = argv[1]
out_file = argv[2]
img_prefixes = argv[3:]
del argv
def add_font_table (font, tag, data):
tab = ttLib.tables.DefaultTable.DefaultTable (tag)
tab.data = str(data)
font[tag] = tab
def drop_outline_tables (font):
for tag in ['cvt ', 'fpgm', 'glyf', 'loca', 'prep', 'CFF ', 'VORG']:
try:
del font[tag]
except KeyError:
pass
print
font = ttx.TTFont (font_file)
print "Loaded font '%s'." % font_file
font_metrics = FontMetrics (font['head'].unitsPerEm,
font['hhea'].ascent,
-font['hhea'].descent)
print "Font metrics: upem=%d ascent=%d descent=%d." % \
(font_metrics.upem, font_metrics.ascent, font_metrics.descent)
glyph_metrics = font['hmtx'].metrics
unicode_cmap = font['cmap'].getcmap (3, 10)
if not unicode_cmap:
unicode_cmap = font['cmap'].getcmap (3, 1)
if not unicode_cmap:
raise Exception ("Failed to find a Unicode cmap.")
image_format = 1 if 'uncompressed' in options else 17
ebdt = CBDT (font_metrics, options)
ebdt.write_header ()
eblc = CBLC (font_metrics, options)
eblc.write_header ()
eblc.start_strikes (len (img_prefixes))
for img_prefix in img_prefixes:
print
img_files = {}
glb = "%s*.png" % img_prefix
print "Looking for images matching '%s'." % glb
for img_file in glob.glob (glb):
uchar = int (img_file[len (img_prefix):-4], 16)
img_files[uchar] = img_file
if not img_files:
raise Exception ("No image files found in '%s'." % glb)
print "Found images for %d characters in '%s'." % (len (img_files), glb)
glyph_imgs = {}
advance = width = height = 0
for uchar, img_file in img_files.items ():
if uchar in unicode_cmap.cmap:
glyph_name = unicode_cmap.cmap[uchar]
glyph_id = font.getGlyphID (glyph_name)
glyph_imgs[glyph_id] = img_file
if "verbose" in options:
print "Matched U+%04X: id=%d name=%s image=%s" % (uchar, glyph_id, glyph_name, img_file)
advance += glyph_metrics[glyph_name][0]
w, h = PNG (img_file).get_size ()
width += w
height += h
glyphs = sorted (glyph_imgs.keys ())
if not glyphs:
raise Exception ("No common characteres found between font and '%s'." % glb)
print "Embedding images for %d glyphs for this strike." % len (glyphs)
advance, width, height = (div (x, len (glyphs)) for x in (advance, width, height))
strike_metrics = StrikeMetrics (font_metrics, advance, width, height)
print "Strike ppem set to %d." % (strike_metrics.y_ppem)
ebdt.start_strike (strike_metrics)
ebdt.write_glyphs (glyphs, glyph_imgs, image_format)
glyph_maps = ebdt.end_strike ()
eblc.write_strike (strike_metrics, glyph_maps)
print
ebdt = ebdt.data ()
add_font_table (font, 'CBDT', ebdt)
print "CBDT table synthesized: %d bytes." % len (ebdt)
eblc.end_strikes ()
eblc = eblc.data ()
add_font_table (font, 'CBLC', eblc)
print "CBLC table synthesized: %d bytes." % len (eblc)
print
if 'keep_outlines' not in options:
drop_outline_tables (font)
print "Dropped outline ('glyf', 'CFF ') and related tables."
font.save (out_file)
print "Output font '%s' generated." % out_file
if __name__ == '__main__':
main (sys.argv)