Skip to content

Commit 65638a2

Browse files
committed
Docs: Add TOC, improve text on custom character sets.
1 parent 39a4a1c commit 65638a2

File tree

1 file changed

+76
-24
lines changed

1 file changed

+76
-24
lines changed

FONT_TO_PY.md

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
1-
# 1. font_to_py.py
1+
# font_to_py.py Creation of Python font files
2+
3+
# 0. Contents
4+
5+
1. [Introdction](./FONT_TO_PY.md#1-introduction) Creating Python fonts.
6+
1.1 [Revision history](./FONT_TO_PY.md#11-revision-history)
7+
2. [Dependencies](./FONT_TO_PY.md#2-dependencies)
8+
3. [Usage](./FONT_TO_PY.md#3-usage)
9+
3.1 [Arguments](./FONT_TO_PY.md#31-arguments)
10+
     3.1.1 [Mandatory positional arguments](./FONT_TO_PY.md#311-mandatory-positional-arguments)
11+
     3.1.2 [Optional arguments](./FONT_TO_PY.md#312-optional-arguments)
12+
     3.1.3 [The height arg](./FONT_TO_PY.md#313-the-height-arg)
13+
3.2 [The font file](./FONT_TO_PY.md#32-the-font-file) How to use the font file.
14+
4. [Python font files](./FONT_TO_PY.md#4-python-font-files) Python font file format.
15+
5. [Binary font files](./FONT_TO_PY.md#5-binary-font-files) Binary font file format.
16+
6. [Dependencies links and licence](./FONT_TO_PY.md#6-dependencies-links-and-licence)
17+
[Appendix 1 RAM utilisation Test Results](./FONT_TO_PY.md#appendix-1-ram-utilisation-test-results)
18+
[Appendix 2 Recent improvements](./FONT_TO_PY.md#appendix-2-recent-improvements)
19+
[Appendix 3 Testing](./FONT_TO_PY.md#appendix-testing) A desktop utility to check fonts.
20+
[Appendix 4 Custom character sets](./FONT_TO_PY.md#appendix-4-custom-character-sets) E.g. non-English character sets.
21+
[Appendix 5 Iteration](./FONT_TO_PY.md#appendix-5-iteration) Enabling a font to support iteration.
22+
23+
# 1. Introduction
224

325
This PC utility converts an industry standard font file to Python source code.
426

@@ -42,6 +64,7 @@ Python files produced are interchangeable with those from prior versions: the
4264
API is unchanged.
4365

4466
###### [Main README](./README.md)
67+
###### [Contents](./FONT_TO_PY.md#0-contents)
4568

4669
# 2. Dependencies
4770

@@ -80,7 +103,7 @@ $ font_to_py.py -k extended FreeSans.ttf 23 my_extended_font.py
80103
```
81104
## 3.1 Arguments
82105

83-
### 3.1.1 Mandatory positional arguments:
106+
### 3.1.1 Mandatory positional arguments
84107

85108
1. Font file path. Must be a ttf or otf file.
86109
2. Height in pixels. In the case of `bdf` or `pcf` files a height of 0 should
@@ -89,7 +112,7 @@ $ font_to_py.py -k extended FreeSans.ttf 23 my_extended_font.py
89112
binary font). A warning is output if the output filename does not have a .py
90113
extension as the creation of a binary font file may not be intended.
91114

92-
### 3.1.2 Optional arguments:
115+
### 3.1.2 Optional arguments
93116

94117
* -f or --fixed If specified, all characters will have the same width. By
95118
default fonts are assumed to be variable pitch.
@@ -104,18 +127,14 @@ $ font_to_py.py -k extended FreeSans.ttf 23 my_extended_font.py
104127
* -l or --largest Ordinal value of largest character to be stored. Default 126.
105128
* -e or --errchar Ordinal value of character to be rendered if an attempt is
106129
made to display an out-of-range character. Default 63 (ord("?")).
107-
* -i or --iterate Specialist use. See below.
108-
* -b or --binary Create a binary font file. See below.
130+
* -i or --iterate Specialist use. See [Appendix 5](./FONT_TO_PY.md#appendix-5-iteration).
131+
* -b or --binary Create a binary font file. See [Binary font files](./FONT_TO_PY.md#5-binary-font-files).
109132
* -c or --charset Option to restrict the characters in the font to a specific
110133
set. See below.
111134
* -k or --charset_file Obtain the character set from a file. Typical use is
112-
for alternative character sets such as Cyrillic: the file must contain the
113-
character set to be included. An example file is `cyrillic`. Another is
114-
`extended` which adds unicode characters `°μπωϕθαβγδλΩ` to those in the
115-
original ASCII set of printable characters. At risk of stating the obvious
116-
this will only produce useful results if the source font file includes all
117-
specified glyphs. Charset files, and any specific documentation, may be found
118-
in the `charsets` directory.
135+
for alternative character sets such as Cyrillic. Please see
136+
[Appendix 4](./FONT_TO_PY.md#appendix-4-custom-character-sets) for details of
137+
creation of custom character sets.
119138

120139
The -c option may be used to reduce the size of the font file by limiting the
121140
character set. If the font file is frozen as bytecode this will not reduce RAM
@@ -147,13 +166,7 @@ achieved, use an `otf` font. I have successfully created Cyrillic and extended
147166
fonts from a `ttf`, so I suspect the issue may be source fonts lacking the
148167
required glyphs.
149168

150-
### 3.1.3 The -i or --iterate arg
151-
152-
This is for specialist applications; it causes a generator function `glyphs` to
153-
be included in the Python font file. A generator instantiated with this will
154-
yield `bitmap`, `height`, and `width` for every glyph in the font.
155-
156-
### 3.1.4 The height arg
169+
### 3.1.3 The height arg
157170

158171
In the case of scalable `ttf` or `otf` source files the specified height is a
159172
target. The algorithm gets as close to the target height as possible (usually
@@ -178,6 +191,8 @@ to render strings on demand. A practical example may be studied
178191
[here](./writer/writer_demo.py).
179192
The detailed layout of the Python file may be seen [here](./writer/DRIVERS.md).
180193

194+
###### [Contents](./FONT_TO_PY.md#0-contents)
195+
181196
# 4. Python font files
182197

183198
Users of the `Writer` or `CWriter` classes or of
@@ -231,7 +246,9 @@ An alternative implementation of binary fonts may be found in
231246
[this repo](https://github.com/antirez/microfont). It provides for rotated
232247
rendering to a `FrameBuffer`.
233248

234-
# 6. Dependencies, links and licence
249+
###### [Contents](./FONT_TO_PY.md#0-contents)
250+
251+
# 6. Dependencies links and licence
235252

236253
The code is released under the MIT licence. The `font_to_py.py` utility
237254
requires Python 3.2 or later.
@@ -290,6 +307,8 @@ With a font of height 20 pixels RAM saving was an order of magnitude. The
290307
saving will be greater if larger fonts are used as RAM usage is independent of
291308
the array sizes.
292309

310+
###### [Contents](./FONT_TO_PY.md#0-contents)
311+
293312
# Appendix 2: Recent improvements
294313

295314
The representation of non-contiguous character sets such as the `extended` set
@@ -305,11 +324,12 @@ keeps the code small and efficient for the common (default) case.
305324
Larger character sets are assumed to be sparse and the emitted code uses an
306325
index optimised for sparse values and a binary search algorithm.
307326

308-
# Appendix 3: font_test.py
327+
# Appendix 3: Testing
309328

310-
This enables a Python font file to be described and rendered at the command
311-
prompt. It provides a useful way of checking unknown font files. Compatibility
312-
with files created by old versions of `font_to_py` is not guaranteed.
329+
The file `font_test.py` enables a Python font file to be described and rendered
330+
at the command prompt. It provides a useful way of checking unknown font files.
331+
Compatibility with files created by old versions of `font_to_py` is not
332+
guaranteed.
313333

314334
It runs under Python 3.2 or above. Given a font `myfont.py` the following will
315335
render the supplied string (assuming that `font_test.py` has executable
@@ -356,3 +376,35 @@ Start char " " (ord 32) end char "~" (ord 126)
356376
...................................................
357377
...................................................
358378
```
379+
###### [Contents](./FONT_TO_PY.md#0-contents)
380+
381+
# Appendix 4 Custom character sets
382+
383+
A common requirement is to create Python fonts containing a specific set of
384+
glyphs - typically for non-English languages, or simply to include additional
385+
symbols such as `°`. This is achieved by creating a "charset" file containing the
386+
entire set of required glyphs. Python fonts are then created using the `-k`
387+
option, specifying the charset file as follows:
388+
```shell
389+
font_to_py.py FreeSans.ttf 20 freesans_cyr_20.py -k cyrillic
390+
font_to_py.py -x -k extended FreeSans.ttf 17 font10.py
391+
```
392+
Example charsets may be found in the `charsets` directory, a non-English example
393+
being `cyrillic`. The `extended` file adds unicode characters `°μπωϕθαβγδλΩ` to
394+
those in the original ASCII set of printable characters. This might be edited to
395+
produce a custom version.
396+
397+
At risk of stating the obvious, for this process to succeed the source font file
398+
must include all specified glyphs.
399+
400+
Submissions of charset files, particularly for non-English languages, are
401+
welcome.
402+
403+
# Appendix 5 Iteration
404+
405+
The `-i` or `--iterate` arg is for specialist applications; it causes a
406+
generator function `glyphs` to be included in the Python font file. A generator
407+
instantiated with this will yield `bitmap`, `height`, and `width` for every
408+
glyph in the font.
409+
410+
###### [Contents](./FONT_TO_PY.md#0-contents)

0 commit comments

Comments
 (0)