# dangxg/h-cat-r-synthetic-handwriten-dataset-generator architecture

> Architecture diagram of the GitHub repository dangxg/h-cat-r-synthetic-handwriten-dataset-generator, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dangxg/h-cat-r-synthetic-handwriten-dataset-generator
- Repository: https://github.com/dangxg/h-cat-r-synthetic-handwriten-dataset-generator

## Overview

#### Purpose and entry points
This is a Python CLI pipeline that produces synthetic Catalan handwriting OCR datasets. The primary entry point is `build_dataset.py`; supporting CLI stages acquire and validate font/text inputs: `download_fonts.py`, `verify_and_clean_fonts.py`, and `verify_and_clean_books.py`.

#### Input acquisition and curation
Font acquisition downloads handwriting/manuscript fonts into category-organized font directories via `download_fonts.py`. `verify_and_clean_fonts.py` is a quality gate: it checks required Catalan characters, digits, and punctuation both in font cmap tables and by actual Pillow rendering, optionally removing invalid fonts. Text curation is handled by `verify_and_clean_books.py`, which validates/cleans Catalan book text before generation.

#### Dataset construction
`build_dataset.py` reads curated texts and eligible fonts, creates text units as either five-word lines or individual words, and renders them as fixed-height, variable-width RGB PNGs. It supports style selection, font-category filtering, per-category font limits, source/output-directory overrides, and configurable train/validation proportions.

#### Parallel rendering boundary
`build_dataset.py` is designed for multiprocess image generation. The main process precomputes all render tasks and assigns unique image filenames before workers start. Workers render/write only their assigned PNGs and return metadata; the parent process serializes metadata and aggregate statistics. Work is dynamically balanced with unordered pool iteration and platform-specific multiprocessing behavior (`spawn` on Windows; `fork` on Linux/macOS).

#### Output contract
Generation writes a HuggingFace `imagefolder`-compatible directory with `train`, `validation`, and `test` splits. Each split contains PNGs plus `metadata.jsonl`; root-level `dataset_info.json` records split counts, feature schema, generation mode/style, and font totals. Metadata links every image to its transcription, font identity/category/style, source book, and mode. This contract is produced by `build_dataset.py`.

#### Runtime and external dependencies
The runtime is Python 3.8+ with Pillow for raster rendering, fontTools for font inspection, and tqdm for progress reporting. HuggingFace `datasets` is an optional downstream loader; generated data targets OCR training workflows such as HuggingFace Transformers TrOCR. External content sources are Wikisource Catalan validated books and downloadable third-party fonts.

#### Diagnostic utility
`test_font_rendering.py` independently renders either one font or an entire font directory, providing a focused visual/functional diagnostic separate from destructive font cleanup.

Main entry points: [`build_dataset.py`](https://github.com/dangxg/h-cat-r-synthetic-handwriten-dataset-generator/blob/main/build_dataset.py) (Dataset builder), [`download_fonts.py`](https://github.com/dangxg/h-cat-r-synthetic-handwriten-dataset-generator/blob/main/download_fonts.py) (Font downloader), [`verify_and_clean_fonts.py`](https://github.com/dangxg/h-cat-r-synthetic-handwriten-dataset-generator/blob/main/verify_and_clean_fonts.py) (Font quality gate), [`verify_and_clean_books.py`](https://github.com/dangxg/h-cat-r-synthetic-handwriten-dataset-generator/blob/main/verify_and_clean_books.py) (Book text curator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_inputs["Input acquisition &amp; curation"]
  node_font_sources["Third-party font sources<br/>external source"]
  node_download_fonts["Font downloader<br/>Python CLI<br/>[download_fonts.py]"]
  node_font_dirs[("Category font directories<br/>font corpus")]
  node_font_validator["Font quality gate<br/>Python CLI"]
  node_book_sources["Catalan Wikisource books<br/>external source"]
  node_book_validator["Book text curator<br/>Python CLI"]
  node_curated_texts[("Curated book texts<br/>text corpus")]
  node_font_diagnostic["Font rendering diagnostic<br/>Python CLI"]
end

subgraph group_build["Dataset generation"]
  node_dataset_builder["Dataset builder<br/>Python CLI<br/>[build_dataset.py]"]
  node_text_units["Text units<br/>generation stage"]
  node_render_task_planner["Render task planner<br/>parent-process stage"]
  node_render_workers{{"Multiprocess render workers<br/>worker pool"}}
  node_metadata_aggregator["Metadata &amp; statistics writer<br/>parent-process stage"]
end

subgraph group_output["Imagefolder output"]
  node_image_splits[("Train / validation / test PNGs<br/>imagefolder splits")]
  node_split_metadata["Split metadata.jsonl<br/>metadata manifest"]
  node_dataset_info["dataset_info.json<br/>dataset manifest"]
end

node_hf_ocr_consumers["HF OCR training consumers<br/>downstream consumer"]

node_font_sources -->|"downloads"| node_download_fonts
node_download_fonts -->|"stores by category"| node_font_dirs
node_font_dirs -->|"inspects"| node_font_validator
node_font_validator -->|"keeps eligible fonts"| node_font_dirs
node_font_dirs -.->|"diagnoses"| node_font_diagnostic
node_book_sources -->|"provides text"| node_book_validator
node_book_validator -->|"writes cleaned text"| node_curated_texts
node_font_dirs -->|"eligible fonts"| node_dataset_builder
node_curated_texts -->|"source texts"| node_dataset_builder
node_dataset_builder -->|"creates"| node_text_units
node_dataset_builder -->|"configures"| node_render_task_planner
node_text_units -->|"plans"| node_render_task_planner
node_render_task_planner -->|"unordered task dispatch"| node_render_workers
node_render_workers -->|"writes PNGs"| node_image_splits
node_render_workers -->|"returns metadata"| node_metadata_aggregator
node_metadata_aggregator -->|"writes per split"| node_split_metadata
node_metadata_aggregator -->|"writes summary"| node_dataset_info
node_image_splits -->|"imagefolder dataset"| node_hf_ocr_consumers
node_split_metadata -->|"transcriptions"| node_hf_ocr_consumers

click node_download_fonts "https://github.com/dangxg/h-cat-r-synthetic-handwriten-dataset-generator/blob/main/download_fonts.py"
click node_font_validator "https://github.com/dangxg/h-cat-r-synthetic-handwriten-dataset-generator/blob/main/verify_and_clean_fonts.py"
click node_book_validator "https://github.com/dangxg/h-cat-r-synthetic-handwriten-dataset-generator/blob/main/verify_and_clean_books.py"
click node_font_diagnostic "https://github.com/dangxg/h-cat-r-synthetic-handwriten-dataset-generator/blob/main/test_font_rendering.py"
click node_dataset_builder "https://github.com/dangxg/h-cat-r-synthetic-handwriten-dataset-generator/blob/main/build_dataset.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_font_sources,node_download_fonts,node_font_dirs,node_font_validator,node_book_sources,node_book_validator,node_curated_texts,node_font_diagnostic toneBlue
class node_dataset_builder,node_text_units,node_render_task_planner,node_render_workers,node_metadata_aggregator toneAmber
class node_image_splits,node_split_metadata,node_dataset_info toneMint
class node_hf_ocr_consumers toneNeutral
```

## Components

### Input acquisition & curation

- **Third-party font sources**: external source · Downloadable handwriting and manuscript fonts.
- [Font downloader](https://github.com/dangxg/h-cat-r-synthetic-handwriten-dataset-generator/blob/main/download_fonts.py): Python CLI · `download_fonts.py`
- **Category font directories**: font corpus
- [Font quality gate](https://github.com/dangxg/h-cat-r-synthetic-handwriten-dataset-generator/blob/main/verify_and_clean_fonts.py): Python CLI · Checks cmap coverage and Pillow rendering; may remove invalid fonts. · `verify_and_clean_fonts.py`
- **Catalan Wikisource books**: external source
- [Book text curator](https://github.com/dangxg/h-cat-r-synthetic-handwriten-dataset-generator/blob/main/verify_and_clean_books.py): Python CLI · Validates and cleans Catalan text. · `verify_and_clean_books.py`
- **Curated book texts**: text corpus
- [Font rendering diagnostic](https://github.com/dangxg/h-cat-r-synthetic-handwriten-dataset-generator/blob/main/test_font_rendering.py): Python CLI · Visually tests one font or a font directory without cleanup. · `test_font_rendering.py`

### Dataset generation

- [Dataset builder](https://github.com/dangxg/h-cat-r-synthetic-handwriten-dataset-generator/blob/main/build_dataset.py): Python CLI · Selects fonts and texts and controls split/style configuration. · `build_dataset.py`
- **Text units**: generation stage · Five-word lines or individual words.
- **Render task planner**: parent-process stage · Preassigns unique filenames and render tasks before workers start.
- **Multiprocess render workers**: worker pool · Dynamically render assigned fixed-height RGB PNGs and return metadata.
- **Metadata & statistics writer**: parent-process stage · Serializes worker metadata and aggregate generation statistics.

### Imagefolder output

- **Train / validation / test PNGs**: imagefolder splits · Fixed-height, variable-width RGB PNG images.
- **Split metadata.jsonl**: metadata manifest · Links images to transcription, font, category, style, source book, and mode.
- **dataset_info.json**: dataset manifest · Records split counts, schema, generation mode/style, and font totals.

### Other components

- **HF OCR training consumers**: downstream consumer · Optional datasets loader and TrOCR-oriented workflows.

## Connections

- Third-party font sources → Font downloader: downloads
- Font downloader → Category font directories: stores by category
- Category font directories → Font quality gate: inspects
- Font quality gate → Category font directories: keeps eligible fonts
- Category font directories → Font rendering diagnostic: diagnoses
- Catalan Wikisource books → Book text curator: provides text
- Book text curator → Curated book texts: writes cleaned text
- Category font directories → Dataset builder: eligible fonts
- Curated book texts → Dataset builder: source texts
- Dataset builder → Text units: creates
- Dataset builder → Render task planner: configures
- Text units → Render task planner: plans
- Render task planner → Multiprocess render workers: unordered task dispatch
- Multiprocess render workers → Train / validation / test PNGs: writes PNGs
- Multiprocess render workers → Metadata & statistics writer: returns metadata
- Metadata & statistics writer → Split metadata.jsonl: writes per split
- Metadata & statistics writer → dataset_info.json: writes summary
- Train / validation / test PNGs → HF OCR training consumers: imagefolder dataset
- Split metadata.jsonl → HF OCR training consumers: transcriptions
