• Stars
    star
    123
  • Rank 281,732 (Top 6 %)
  • Language
    Python
  • License
    MIT License
  • Created over 5 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Convert Chinese Characters to Single-Line Fonts using Computer Vision

chinese-hershey-font

Convert Chinese Characters to Single-Line Fonts using Computer Vision

Single-line fonts such as the Hershey fonts are massively useful for making cool procedural graphics and for engraving. They're also arguably easier to learn for neural nets.

This tool automatically generates Chinese Single-Line fonts given a regular True Type Font file (ttf/ttc). It can output either the classic Hershey format, or a JSON file containing all the polylines.

The algorithm scans a raster rendering of a character at many different angles to find line segments that are most likely part of a stroke. It then estimates the strokes by connecting, merging, and cleaning up the line segments.

Dependencies

  • Python 2
  • PIL/Pillow (pip install pillow)

File Formats

The following are types of files this software can produce to encode single-line fonts.

Stroke Files

A JSON file with one object. The keys of the object are unicode indices of characters. Each key maps to an array of polylines. A polyline is an array of points. A point is a 2-element Array containing x and y coordinates. A coordinate is a float between 0.0 and 1.0, and (0,0) is the upper left-corner. For example:

{
  "U+4E00":[[[0.0, 0.55], [1.0, 0.55]]],
  "U+4E01":[[[0.02, 0.02], [0.99, 0.02]], [[0.51, 0.02], [0.53, 0.925], [0.31, 1.0]]]
}

The above encoding contains the first two Chinese characters in unicode, "一" and "丁".

Hershey Fonts

The Hershey fonts are a collection of vector fonts developed c. 1967 by Dr. Allen Vincent Hershey at the Naval Weapons Laboratory, originally designed to be rendered using vectors on early cathode ray tube displays. The fonts are publicly available and have few usage restrictions. Vector fonts are easily scaled and rotated in two or three dimensions; consequently the Hershey fonts have been widely used in computer graphics, computer-aided design programs, and more recently also in computer-aided manufacturing applications like laser engraving. (Wikipedia)

This link gives an overview on how to parse Hershey fonts. You can also find my own implementation at Lingdong-/p5-hershey-js.

Compared to stroke files, Hershey Fonts are many times more compact, but also has less accurate coordinates.

Usage

Pre-compiled Fonts

If you're only interested in utilizing pre-generated single-line fonts, you can grab them at the following places:

Note:

Heiti.hf.txt and Kaiti.hf.txt were originally based on proprietary macOS system fonts STHeiti and STKaiti. They're now replaced with open source alternatives.

Generating Stroke Files

If you would like to generate new single-line fonts from custom TTF/TTC files, first use the following command to generate a JSON-encoded stroke file.

python char2stroke.py build path/to/font.ttf

Optional arguments:

  --first [FIRST]
  --height [HEIGHT]
  --last [LAST]
  --ngradient [NGRADIENT]
  --output [OUTPUT]
  --strw [STRW]
  --width [WIDTH]
  • width and height determines the dimension of the raster images of characters to be scanned. The larger these numbers, the more detailed and the slower. Default is 100 for both.
  • strw is the approximate stroke width (in pixels, at given width and height). This is used when merging strokes. Default is 10.
  • first and last specifies the range of unicode characters to include. Default values are 0x4e00 and 0x9fef, which contains all the "CJK Ideograph" glyphs.
  • ngradient is the number of different gradients used to scan the image. At ngradient = 1, only strokes at 0°, 45° and 90° are scanned, while at 2, 3 and 4, slopes of atan(1/2), atan(1/3) and atan(1/4) are also included. Default is 2.
  • output: path to write the output file. When this is not specified, the program writes to stdout, and >(info) and |(info) can be used to redirect the output.

Quick Tests

Before generating large files, it is helpful to do some quick tests on small sets of characters, compare how different fonts turn out, and tweak parameters. The following command facilitate this process with side-by-side visualization of computer vision results.

python char2stroke.py test path/to/font1.ttf path/to/font2.ttf ...

Optional arguments:

  --corpus [CORPUS]
  --height [HEIGHT]
  --ngradient [NGRADIENT]
  --nsample [NSAMPLE]
  --strw [STRW]
  --width [WIDTH]
  • corpus: string to test on. Default is the text of Thousand Character Classic.
  • nsample: number of characters to be randomly picked from the corpus. Default is 8.
  • The other arguments are identical to those of build mode.

Generate Hershey Font from Stroke File

The following command generates a Hershey font from a stroke file produced in the previous step.

python tohershey.py path/to/input.json > path/to/output.hf.txt

Example

More Repositories

1

shan-shui-inf

Procedurally generated Chinese landscape painting.
HTML
5,464
star
2

fishdraw

procedurally generated fish drawings
JavaScript
2,197
star
3

qiji-font

齊伋體 - typeface from Ming Dynasty woodblock printed books
Python
1,245
star
4

rrpl

Describing Chinese Characters with Recursive Radical Packing Language (RRPL)
JavaScript
846
star
5

linedraw

Convert images to vectorized line drawings for plotters.
Python
744
star
6

wax

A tiny programming language that transpiles to C, C++, Java, TypeScript, Python, C#, Swift, Lua and WebAssembly 🚀
C
730
star
7

q5xjs

A small and fast alternative (experimental) implementation of p5.js
JavaScript
526
star
8

nonflowers

Procedurally generated paintings of nonexistent flowers.
JavaScript
503
star
9

skeleton-tracing

A new algorithm for retrieving topological skeleton as a set of polylines from binary images
C
488
star
10

cope

A modern IDE for writing classical Chinese poetry 格律诗编辑程序
JavaScript
458
star
11

ndwfc

🌊💥 N-dimensional Wave Function Collapse with infinite canvas
JavaScript
308
star
12

psvg

Programmable Scalable Vector Graphics -- drawings that draw themselves
TypeScript
297
star
13

legumes

🎼 A sheet music to polylines renderer
TypeScript
230
star
14

magic-square-poems

Discovering magic squares in Tang Dynasty poems
C
188
star
15

handpose-facemesh-demos

🎥🤟 8 minimalistic templates for tfjs mediapipe handpose and facemesh
JavaScript
185
star
16

Hermit

A man. A horse. A nature.
Python
167
star
17

Processing-Demos-for-The-Pocket-Handbook-of-Image-Processing-Algorithms

Processing Demos made when reading the book *The Pocket Handbook for Image Processing Algorithms in C*
Processing
127
star
18

edges2calligraphy

Using pix2pix to convert scribbles to Chinese calligraphy
JavaScript
114
star
19

tk-fangsong-font

剔骨仿宋: Experimental Fang Song style Chinese font
Python
111
star
20

grand-timeline

Interactive grand unified timeline of 30,800 ancient Chinese people / 古人全表
JavaScript
109
star
21

hfmath

Render LaTeX math with Hershey Fonts
TypeScript
91
star
22

wechit

WeChat in Terminal (微信终端版)
Python
88
star
23

VisionOSC

PoseOSC + FaceOSC + HandOSC + OcrOSC + CatOSC + DogOSC
Objective-C++
85
star
24

wasm-fun

Non-trivial programs in hand-written WebAssembly
WebAssembly
77
star
25

PoseOSC

📹🤸‍♂️🤾‍♀️🤺 PoseNet + OSC: send realtime human pose estimation data to your apps
JavaScript
76
star
26

ci-ren

Generative Chinese poetry
Python
74
star
27

r1b

A thermal-printer-oriented, 1-bit graphics rasterizer for 2D and 3D
C
70
star
28

squiggy

vector brushstroke library
TypeScript
63
star
29

asciimare

3D engine powered by ASCII art
Python
61
star
30

Okb.js

Procedural generation toolkit for Javascript - noises, randomness, curves, and more
HTML
58
star
31

ofxPoissonFill

Poisson filling shader for OpenFrameworks
C++
56
star
32

p5-hershey-js

p5.js Hershey Vector Font Library
JavaScript
50
star
33

pmst

🎨 Poor Man's Style Transfer - Painting an image with the style of another, without machine learning
C++
48
star
34

zdic-cli

An offline command-line interface to zdic.net dictionary (漢典)
JavaScript
47
star
35

interesting-polygon-archive

Collection of polygon data in various formats for testing computational geometry algorithms.
Processing
46
star
36

Loshu.js

A linear algebra library for JavaScript 🔢
JavaScript
46
star
37

skeletonization-js

Javascript implementation of image skeletonization
JavaScript
41
star
38

ttf2hershey

Convert True Type Fonts (.ttf) to Hershey vector fonts
Python
40
star
39

fv

An experimental approach to expressing vector math in js (tagged template literals)
JavaScript
30
star
40

t43

A tiny 3D slicer written from scratch
C
30
star
41

LingDong-

Automatically keep my Github profile README updated with a python script and Github Actions
Python
28
star
42

PContour

Processing/Java library for finding contours in binary images
HTML
27
star
43

srcsnap

screenshot-driven version tracking
JavaScript
21
star
44

dbn.js

Recreation of John Maeda's "Design By Numbers" programming environment in JavaScript
JavaScript
17
star
45

TrackpadOSC

💻👋✌️👉Send mac's multitouch trackpad read-out through OSC
Objective-C
17
star
46

xcessing

Friendly Processing-like interface to X11/Xlib in C
C
16
star
47

wax4vscode

Extension for the wax programming language in VS Code (highlight + transpile + run)
TypeScript
15
star
48

svg2pl

convert svg to polylines
C
14
star
49

lbll

tiny experimental language for limited environments
C
14
star
50

fast-many-face-detection-with-cpp-or-openframeworks-on-mac-using-neural-networks

Fast Many Face Detection with C++/OpenFrameworks on macOS using Neural Networks
C++
14
star
51

machining-projection-map

JavaScript
8
star
52

avrlass

AVR Lightweight Assembler (and disassembler)
JavaScript
6
star
53

60-212

JavaScript
5
star
54

teapot.lua

1 path tracer written in pure lua, 1 file, 0 dependencies.
Lua
5
star
55

Hello-World

Lorem Ipsum
3
star
56

cvDictUI

opencv-python tool for generating interactive GUI from any python dictionary
Python
3
star
57

lingdong

LingDong's project links
JavaScript
2
star