def extract(self):
basename_ = os.path.splitext(os.path.basename(self.filename))[0]
glyph_widths = {}
for cwdh in self.cwdh_sections:
for index in range(cwdh['start'], cwdh['end'] + 1):
glyph_widths[index] = cwdh['data'][index - cwdh['start']]
glyph_mapping = {}
for cmap in self.cmap_sections:
if cmap['type'] == MAPPING_DIRECT:
for code in range(cmap['start'], cmap['end']):
glyph_mapping[unichr(code)] = code - cmap['start'] + cmap['indexOffset']
elif cmap['type'] == MAPPING_TABLE:
for code in range(cmap['start'], cmap['end']):
index = cmap['indexTable'][code - cmap['start']]
if index != 0xFFFF:
glyph_mapping[unichr(code)] = index
elif cmap['type'] == MAPPING_SCAN:
for code in cmap['entries'].keys():
glyph_mapping[code] = cmap['entries'][code]
# save JSON manifest
json_file_ = open('%s_manifest.json' % basename_, 'w')
json_file_.write(json.dumps({
'fontInfo': self.font_info,
'textureInfo': {
'glyph': self.tglp['glyph'],
'sheetCount': self.tglp['sheetCount'],
'sheetInfo': {
'cols': self.tglp['sheet']['cols'],
'rows': self.tglp['sheet']['rows'],
'width': self.tglp['sheet']['width'],
'height': self.tglp['sheet']['height'],
'colorFormat': PIXEL_FORMATS[self.tglp['sheet']['format']]
}
},
'glyphWidths': glyph_widths,
'glyphMap': glyph_mapping
}, indent=2, sort_keys=True))
json_file_.close()
# save sheet bitmaps
for i in range(self.tglp['sheetCount']):
sheet = self.tglp['sheets'][i]
width = sheet['width']
height = sheet['height']
png_data = []
for y in range(height):
row = []
for x in range(width):
for color in sheet['data'][x + (y * width)]:
row.append(color)
png_data.append(row)
file_ = open('%s_sheet%d.png' % (basename_, i), 'wb')
writer = png.Writer(width, height, alpha=True)
writer.write(file_, png_data)
file_.close()
评论列表
文章目录