37 lines
1.5 KiB
Python
37 lines
1.5 KiB
Python
"""Extract notation glyph bounds from the shipped OTF, in staff-space units."""
|
|
import argparse
|
|
from pathlib import Path
|
|
|
|
from fontTools.pens.boundsPen import BoundsPen
|
|
from fontTools.ttLib import TTFont
|
|
|
|
def generate(font_path, output):
|
|
with TTFont(font_path) as font:
|
|
glyphs, cmap = font.getGlyphSet(), font.getBestCmap()
|
|
units = font['head'].unitsPerEm / 4
|
|
ranges = [(0xE050, 0xE06F), (0xE080, 0xE09F), (0xE0A0, 0xE0A5),
|
|
(0xE101, 0xE102), (0xE1E7, 0xE1E8), (0xE240, 0xE250),
|
|
(0xE260, 0xE270), (0xE4E0, 0xE4EE)]
|
|
records = []
|
|
for start, end in ranges:
|
|
for codepoint in range(start, end):
|
|
if codepoint not in cmap:
|
|
continue
|
|
pen = BoundsPen(glyphs)
|
|
glyphs[cmap[codepoint]].draw(pen)
|
|
if pen.bounds:
|
|
box = ', '.join(f'{v / units:.8f}F' for v in pen.bounds)
|
|
records.append(f' {{{codepoint}, {{{box}}}}},')
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
output.write_text('#pragma once\n#include <array>\n#include <cstdint>\nnamespace vamp_zero::font_data {\n'
|
|
'struct glyph { std::int32_t codepoint; std::array<float, 4> bounds; };\n'
|
|
f'inline constexpr std::array<glyph, {len(records)}> glyphs = {{{{\n'
|
|
+ '\n'.join(records) + '\n}};\n}\n', encoding='ascii')
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--font', type=Path, required=True)
|
|
parser.add_argument('--output', type=Path, required=True)
|
|
args = parser.parse_args()
|
|
generate(args.font, args.output)
|