diff options
| author | yum <yum.food.vr@gmail.com> | 2022-11-05 16:28:09 -0700 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2022-11-05 16:32:06 -0700 |
| commit | 4f2316a1493f7dd338604ca749df15ea7a40dc16 (patch) | |
| tree | 51733f7662610263765e5a6656d9c6314d9b7f53 /generate_fonts.py | |
| parent | da8e8d93813ca85a78653587ed23c90820763e69 (diff) | |
Add generate_fonts.py
Add code to generate 4k textures holding a bunch of unicode characters.
Add unicode blocks for English, Japanese, Chinese, and Korean.
Embed GNU's excellent Unifont ttf, which I use to generate these
textures. The license is included under $filename.LICENSE.
Diffstat (limited to 'generate_fonts.py')
| -rw-r--r-- | generate_fonts.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/generate_fonts.py b/generate_fonts.py new file mode 100644 index 0000000..0b9a4fd --- /dev/null +++ b/generate_fonts.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 + +from PIL import Image, ImageFont, ImageDraw + +import math + +# Use a power of 2 pixels per character so we can evenly divide the plane. +font_pixels = 64 +font = ImageFont.truetype("unifont-15.0.01.ttf", font_pixels) + +n_rows = 64 +n_cols = 128 + +def allow_range(allowlist, lo, hi): + for i in range(lo, hi + 1): + allowlist.add(i) +def ban_range(allowlist, lo, hi): + for i in range(lo, hi + 1): + allowlist.remove(i) +allowlist = set() +# ASCII +allow_range(allowlist, 32, 126) +# Latin-1 supplement +allow_range(allowlist, 0x00A1, 255) +# Latin extended-A +allow_range(allowlist, 0x0100, 0x017F) +# Latin extended-B +allow_range(allowlist, 0x0180, 0x024F) +# Spacing modifier letters +allow_range(allowlist, 0x0250, 0x02AF) +# Greek and Coptic +allow_range(allowlist, 880, 1023) +ban_range(allowlist, 0x0378, 0x03a2) +# Cyrillic +allow_range(allowlist, 0x0400, 0x04FF) +# Currency symbols +allow_range(allowlist, 0x20A0, 0x20C0) + +# Japanese +# Punctuation +allow_range(allowlist, 0x3000, 0x303F) +# Hiragana +allow_range(allowlist, 0x3041, 0x30FF) +ban_range(allowlist, 0x3097, 0x3098) +# Katakana +allow_range(allowlist, 0x30A0, 0x30FF) +# CJK Unified Ideographs (Kanji) +allow_range(allowlist, 0x4E00, 0x9FFF) +# CJK Unified Ideographs extension A (rare Kanji) +allow_range(allowlist, 0x3400, 0x4dbf) + +# Korean +allow_range(allowlist, 0x1100, 0x11FF) +allow_range(allowlist, 0xAC00, 0xD7A3) + +max_char = max(allowlist) +total_rows = math.ceil(max_char / n_cols) +print("total rows {}".format(total_rows)) +total_textures = math.ceil(total_rows / n_rows) +print("total textures {}".format(total_textures)) + +for nth_texture in range(0, total_textures): + # Create a 4K grayscale ("L") image + image = Image.new(mode="L", size=(4096,4096), color=255) + draw = ImageDraw.Draw(image) + + row_begin = nth_texture * n_rows + + for row in range(row_begin, row_begin + n_rows): + line = "" + for col in range(0, n_cols): + # Generate the unicode character for this spot. + n = row * n_cols + col + if n in allowlist: + line += chr(n) + else: + line += " " + draw.text((0, (row - row_begin) * font_pixels), line, font=font) + + image.save("font-%01d.png" % nth_texture) + |
