Git Product home page Git Product logo

chinese-hershey-font's Introduction

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

chinese-hershey-font's People

Contributors

lingdong- avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

chinese-hershey-font's Issues

Font license

The software repository here says MIT license. Is that also the license for the font files?

Edit: Maybe update documentation to be clear.

Settings

What settings did you use to create the files in dist? The default values?

json file seems wrong

4E00 is the chinese 一
it should has two points
but in your json file so many points
"U+4E00":[[[0.0, 0.89], [0.89, 0.89], [1.02, 0.97]], [[0.06, 0.89], [0.255, 0.93], [0.405, 0.93], [0.555, 0.93], [0.705, 0.93], [0.9, 0.97]], [[0.93, 0.89], [0.735, 0.93], [0.585, 0.93], [0.435, 0.93], [0.285, 0.93], [0.15, 0.93], [-0.03, 0.97]]]

i try to draw these points can not get 一
am i did some thing wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.