Skip to content

Commit d782037

Browse files
committed
More subsetting
1 parent 3946234 commit d782037

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

lib/freetypy/subset.py

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def _subset_format2(self, glyphs):
349349
name_index < numglyphs - N_BASIC_NAMES):
350350
needed_indices[name_index - N_BASIC_NAMES] = gind
351351

352-
names = []
352+
names = [b'.removed']
353353
name_index = 0
354354
i += 2 * numglyphs
355355
while i < len(content):
@@ -364,8 +364,8 @@ def _subset_format2(self, glyphs):
364364
name_index += 1
365365

366366
new_content = [content[0:36]]
367-
for i in range(numglyphs):
368-
val = new_glyph_index.get(i, 0)
367+
for i in range(1, numglyphs):
368+
val = new_glyph_index.get(i, N_BASIC_NAMES)
369369
new_content.append(struct.pack('>H', val))
370370

371371
for name in names:
@@ -379,8 +379,64 @@ def subset(self, glyphs):
379379
self.content = self._subset_format2(glyphs)
380380

381381

382+
class _HheaTable(_Table):
383+
hhea_table_struct = _BinaryStruct([
384+
('version', 'I'),
385+
('ascent', 'h'),
386+
('descent', 'h'),
387+
('lineGap', 'h'),
388+
('advanceWidthMax', 'H'),
389+
('minLeftSideBearing', 'h'),
390+
('minRightSideBearing', 'h'),
391+
('xMaxExtent', 'h'),
392+
('caretSlopeRise', 'h'),
393+
('caretSlopeRun', 'h'),
394+
('caretOffset', 'h'),
395+
('res0', 'h'),
396+
('res1', 'h'),
397+
('res2', 'h'),
398+
('res3', 'h'),
399+
('metricDataFormat', 'h'),
400+
('numOfLongHorMetrics', 'H')])
401+
402+
def __init__(self, header, content):
403+
super(_HheaTable, self).__init__(header, content)
404+
405+
self.__dict__.update(self.hhea_table_struct.unpack(content))
406+
407+
408+
class _HmtxTable(_Table):
409+
def subset(self, glyph_set, offsets, hhea):
410+
# In keeping with not changing glyph ids, we can't actually
411+
# remove entries here. However, we can set unused entries to
412+
# 0 which should aid in compression.
413+
414+
n_glyphs = len(offsets) - 1
415+
n_long_hor_metrics = hhea.numOfLongHorMetrics
416+
content = self.content
417+
418+
h_metrics = content[:n_long_hor_metrics*4]
419+
new_values = []
420+
for i in range(n_long_hor_metrics):
421+
if i in glyph_set:
422+
new_values.append(h_metrics[i*4:i*4+4])
423+
else:
424+
new_values.append(b'\0\0\0\0')
425+
426+
left_side_bearing = content[n_long_hor_metrics*4:]
427+
for i in range(n_glyphs - n_long_hor_metrics):
428+
if i + n_long_hor_metrics in glyph_set:
429+
new_values.append(left_side_bearing[i*2:i*2+2])
430+
else:
431+
new_values.append(b'\0\0')
432+
433+
self.content = b''.join(new_values)
434+
435+
382436
SPECIAL_TABLES = {
383437
b'head': _HeadTable,
438+
b'hhea': _HheaTable,
439+
b'hmtx': _HmtxTable,
384440
b'loca': _LocaTable,
385441
b'glyf': _GlyfTable,
386442
b'post': _PostTable
@@ -452,10 +508,14 @@ def subset(self, ccodes):
452508
# glyphs
453509
glyphs = self[b'glyf'].find_all_glyphs(glyphs, offsets)
454510

511+
glyph_set = set(glyphs)
512+
455513
self[b'glyf'].subset(glyphs, offsets)
456514
self[b'loca'].subset(self, glyphs, offsets)
457515
if b'post' in self._tables:
458516
self[b'post'].subset(glyphs)
517+
if b'hmtx' in self._tables and b'hhea' in self._tables:
518+
self[b'hmtx'].subset(glyph_set, offsets, self[b'hhea'])
459519

460520
def write(self, fd):
461521
self._header['numTables'] = len(self._tables)

0 commit comments

Comments
 (0)