Skip to content

Instantly share code, notes, and snippets.

@cockscomb
Created April 23, 2013 11:12

Revisions

  1. cockscomb created this gist Apr 23, 2013.
    30 changes: 30 additions & 0 deletions remove-empty-glyphs.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/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)
    59 changes: 59 additions & 0 deletions unicode-range-maker.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    #!/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