Skip to content

Instantly share code, notes, and snippets.

@cockscomb
Created April 23, 2013 11:12
Show Gist options
  • Save cockscomb/5442744 to your computer and use it in GitHub Desktop.
Save cockscomb/5442744 to your computer and use it in GitHub Desktop.
Webフォントに使用するフォントから空のグリフを除くためのfontforgeスクリプティングインターフェース用のPythonスクリプト
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import fontforge
import argparse
def remove_empty_glyphs(input, output):
font = fontforge.open(input)
code_points = []
for glyph in font.glyphs():
# 実際には glyph のデータが空になっていることがある
if glyph.left_side_bearing != 0.0 and glyph.right_side_bearing != 0.0:
pass
else:
font.removeGlyph(glyph)
font.generate(output)
font.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Make unicode-range from font.')
parser.add_argument('input', nargs=1, help='path to input font file')
parser.add_argument('output', nargs=1, help='path to output font file')
args = parser.parse_args()
input_file_path = args.input[0]
output_file_path = args.output[0]
remove_empty_glyphs(input_file_path, output_file_path)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import fontforge
import argparse
def _code_points_from_font(filepath):
font = fontforge.open(filepath)
code_points = []
for glyph in font.glyphs():
point = glyph.unicode
# unicode が見つからないとき -1
# 実際には glyph のデータが空になっていることがあるから side_bearing が 0.0 のものを除く
if point != -1 and glyph.left_side_bearing != 0.0 and glyph.right_side_bearing != 0.0:
code_points.append(point)
return sorted(code_points)
def _code_ranges_from_code_points(code_points):
code_ranges = []
last_point = code_points[0]
code_ranges.append([last_point])
for point in code_points:
if point - last_point > 1 and point != last_point:
# 連続していない
code_ranges[-1].append(last_point)
code_ranges.append([point])
else:
# 連続している
pass
last_point = point
return code_ranges
def unicode_range_from_font(filepath):
code_points = _code_points_from_font(filepath)
code_ranges = _code_ranges_from_code_points(code_points)
unicode_ranges = []
for code_range in code_ranges:
if len(code_range) > 1:
unicode_ranges.append('U+{0:X}-{1:X}'.format(code_range[0], code_range[-1]))
elif len(code_range) == 1:
unicode_ranges.append('U+{0:X}'.format(code_range[0]))
unicode_range = ', '.join(unicode_ranges)
unicode_range = 'unicode-range: {0};'.format(unicode_range)
return unicode_range
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Make unicode-range from font.')
parser.add_argument('font', nargs=1, help='path to font file')
filepath = parser.parse_args().font[0]
unicode_range = unicode_range_from_font(filepath)
print unicode_range
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment