1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| from pathlib import Path from io import BytesIO import math
import requests from fontTools.ttLib import TTFont from PIL import Image, ImageDraw, ImageFont
BASE = "http://xx.cn" sample = "#SZVVbBNKl6_1791510247091otltag䘚䘖䘠#FontTag"
font_id = sample.split("otltag", 1)[0].lstrip("#") url = f"{BASE}/ttf/{font_id}.ttf"
response = requests.get(url, timeout=20) response.raise_for_status()
font_path = Path("water_font.ttf") font_path.write_bytes(response.content)
font = TTFont(BytesIO(response.content)) cmap = {}
for table in font["cmap"].tables: if table.isUnicode(): cmap.update(table.cmap)
items = sorted(cmap.items()) print("字形总数:", len(items))
cols = 5 cell_w = 180 cell_h = 130 page_size = 100
glyph_font = ImageFont.truetype(str(font_path), 58) label_font = ImageFont.truetype("arial.ttf", 15)
output_dir = Path("font_atlas_pages") output_dir.mkdir(exist_ok=True)
for start in range(0, len(items), page_size): page_items = items[start:start + page_size] rows = math.ceil(len(page_items) / cols)
image = Image.new( "RGB", (cols * cell_w, rows * cell_h), "white" ) draw = ImageDraw.Draw(image)
for i, (codepoint, glyph_name) in enumerate(page_items): x = (i % cols) * cell_w y = (i // cols) * cell_h
draw.rectangle( (x, y, x + cell_w - 1, y + cell_h - 1), outline="#cccccc" )
draw.text( (x + 55, y + 5), chr(codepoint), font=glyph_font, fill="black" )
draw.text( (x + 8, y + 78), f"U+{codepoint:04X}", font=label_font, fill="blue" )
draw.text( (x + 8, y + 100), f"{glyph_name} 编号:{start + i + 1}", font=label_font, fill="black" )
page_no = start // page_size + 1 output = output_dir / f"page_{page_no:02d}.png" image.save(output) print("已生成:", output.resolve())
print("全部完成。请打开 font_atlas_pages 文件夹查看图片。")
|