(font_path: Path, codepoints: tuple[int, ...])
| 87 | |
| 88 | |
| 89 | def _process_font(font_path: Path, codepoints: tuple[int, ...]): |
| 90 | print(f"Processing {font_path.name}...") |
| 91 | |
| 92 | font_size = { |
| 93 | "unifont.otf": 16, # unifont is only 16x8 or 16x16 pixels per glyph |
| 94 | }.get(font_path.name, 200) |
| 95 | |
| 96 | data = font_path.read_bytes() |
| 97 | file_buf = rl.ffi.new("unsigned char[]", data) |
| 98 | cp_buffer = rl.ffi.new("int[]", codepoints) |
| 99 | cp_ptr = rl.ffi.cast("int *", cp_buffer) |
| 100 | glyph_count = rl.ffi.new("int *", len(codepoints)) |
| 101 | glyphs = rl.load_font_data( |
| 102 | rl.ffi.cast("unsigned char *", file_buf), len(data), font_size, cp_ptr, len(codepoints), |
| 103 | rl.FontType.FONT_DEFAULT, glyph_count |
| 104 | ) |
| 105 | if glyphs == rl.ffi.NULL: |
| 106 | raise RuntimeError("raylib failed to load font data") |
| 107 | |
| 108 | rects_ptr = rl.ffi.new("Rectangle **") |
| 109 | image = rl.gen_image_font_atlas(glyphs, rects_ptr, glyph_count[0], font_size, GLYPH_PADDING, 0) |
| 110 | if image.width == 0 or image.height == 0: |
| 111 | raise RuntimeError("raylib returned an empty atlas") |
| 112 | |
| 113 | rects = rects_ptr[0] |
| 114 | atlas_name = f"{font_path.stem}.png" |
| 115 | atlas_path = FONT_DIR / atlas_name |
| 116 | entries, line_height, base = _glyph_metrics(glyphs, rects, glyph_count[0]) |
| 117 | |
| 118 | if not rl.export_image(image, atlas_path.as_posix()): |
| 119 | raise RuntimeError("Failed to export atlas image") |
| 120 | |
| 121 | _write_bmfont(FONT_DIR / f"{font_path.stem}.fnt", font_size, font_path.stem, atlas_name, line_height, base, (image.width, image.height), entries) |
| 122 | |
| 123 | |
| 124 | def main(): |
no test coverage detected