File size: 6,089 Bytes
83882fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
---
license: mit
language:
  - en
  - zh
pretty_name: FELDoc-Atomic - single-character rendering tool for rare-CJK evaluation sets
tags:
  - ocr
  - cjk
  - rare-characters
  - data-generation
  - synthetic-data
  - rendering
---

# FELDoc-Atomic — single-char renderer for rare-CJK evaluation sets

**中文说明见下半部分 | Chinese documentation follows the English section below.**

---

## English

`render_single_chars.py` renders a list of Chinese characters (txt, several
characters per line) into a **one image per character** evaluation set —
designed for rare-CJK scenarios. It originated from an ancient-book OCR /
tokenizer evaluation-set construction workflow.

### Why this tool exists

1. **Rare characters lack glyphs** — common simplified-Chinese fonts cover
   CJK Extension B/C/D (4-byte UTF-8) poorly; naive rendering yields tofu
   blocks. Pass multiple font files (e.g. plane-split large fonts) and each
   character is routed to the first font whose cmap covers it.
2. **Silent failures** — some glyphs render blank even when present. The tool
   counts ink pixels per image; below a threshold it treats the image as
   blank/tofu, retries with the next font, and only if all fonts fail records
   the character in a failure list. It never quietly emits bad images.

### What "FELDoc-Atomic" means

The name describes the unit of work this tool targets: atomic single-character
images — the building blocks of FELDoc-style fine-grained OCR evaluation
sets. ("FELDoc" = Fine-grained Evaluation of Layout-aware Document OCR.)

### Install

Python ≥ 3.9.

```bash
pip install -r src/requirements.txt   # Pillow>=10.0, fonttools>=4.40
```

### Usage

```bash
# multiple txt inputs -> one subdirectory per input under --out
python src/render_single_chars.py token2.txt token3.txt token4.txt ^
    --out ./images ^
    --font font_plane00.ttf font_plane02.ttf font_plane15.ttf font_plane16.ttf

# single txt -> flat output directory, prefix distinguishes batches
python src/render_single_chars.py chars.txt --out ./images --flat --prefix 2token ^
    --font myfont.ttf
```

`.ttc` font collections are supported: every face in a collection is routed
separately (labels appear as `name.ttf#i` in `annotations.csv`).

### Options

| Option | Default | Description |
|---|---|---|
| `inputs` | (required) | input txt files; each non-whitespace char on each line is rendered; auto-dedup preserving order |
| `--out` | `images` | output root |
| `--font` | (required) | font file(s), priority = order given (e.g. plane-split font stacks) |
| `--size` | `128` | canvas size (px) |
| `--font-px` | `100` | initial font size; auto-shrinks if the glyph overflows the canvas |
| `--min-ink` | `40` | blank-detection threshold (dark pixel count) |
| `--flat` | off | render everything into `--out` without per-input subdirectories |
| `--prefix` | `char` | filename prefix in `--flat` mode |

### Output

- `images/<group>/<prefix>_<index:04d>_<char>.png` — 128×128 grayscale,
  white background, black glyph, centered
- `images/annotations.csv` — per-image annotation (`group, filename, char,
  codepoint, font`), UTF-8 with BOM (opens cleanly in Excel)

### Rendering specification (defaults)

- 128×128 PNG, 8-bit grayscale; background 255 (white), foreground 0 (black)
- initial font size 100 px, `anchor=mm` geometric centering
- glyph bounding box overflows the canvas → font size shrinks by 6 px per step
  (floor 20 px)
- ink pixels (grayscale < 200) below 40 → treated as blank, next font tried

### Fonts — NOT distributed here

**No font files are included in this repository.** The original in-house
workflow used commercial multi-plane CJK fonts (Zhonghua Book Company Song
ti), which must not be redistributed. Use any fonts you have the rights to;
free wide-coverage alternatives include **Noto Sans CJK / Noto Serif CJK
(OFL)**, **Han Sans / Han Serif (OFL)** and **GNU Unifont**. The renderer
works with any `.ttf`/`.ttc` fonts you supply, and plane-split font stacks
can be given in priority order via repeated `--font` arguments.

## License

MIT (see `src/LICENSE`).

---

## 中文说明

`render_single_chars.py` 把汉字列表(txt,一行若干字)批量渲染成"一图一字"的评测图片集,专为 CJK 生僻字场景设计。源自古籍 OCR / tokenizer 评测集构建工作流。

### 为什么需要它

1. **生僻字缺字形**——简体常用字体对 CJK 扩展 B/C/D 区覆盖率极低,直接渲染会得到"豆腐块"。支持传入多个字体文件(如按 Unicode 平面拆分的字库),按 cmap 查询自动路由到第一个有该字形的字体。
2. **静默失败**——有些字形即使存在也可能渲染成空白。对每张图做墨水像素统计,低于阈值判为空白/tofu,自动换下一个字体重试,仍失败则记入失败清单,绝不悄悄产出坏图。

### 名称含义

FELDoc-Atomic 指本工具的工作粒度:原子级单字图——构建 FELDoc 式细粒度 OCR 评测集的基本单元。

### 安装

Python ≥ 3.9。

```bash
pip install -r src/requirements.txt   # Pillow>=10.0, fonttools>=4.40
```

### 用法

```bash
# 多个 txt → 输出目录下同名子目录(每组一个文件夹)
python src/render_single_chars.py token2.txt token3.txt token4.txt ^
    --out ./images ^
    --font 字库00平面.ttf 字库02平面.ttf 字库15平面.ttf 字库16平面.ttf

# 单个 txt → 平铺一个目录,用前缀区分
python src/render_single_chars.py chars.txt --out ./images --flat --prefix 2token ^
    --font myfont.ttf
```

`.ttc` 字体集合已支持:集合内每个 face 单独路由(`annotations.csv` 中标注为 `name.ttf#i`)。

参数、输出格式、渲染规格与英文部分表格一致。

### 关于字体——本仓库不含任何字体

原工作流使用的商业多平面大字库(中华书局宋体)**不可再分发,因此不入包**。请使用你有权使用的字体;开源大字库可选 Noto Sans CJK / Noto Serif CJK(OFL)、GNU Unifont 等。

## 许可

MIT(见 `src/LICENSE`)。