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
2
24
3
25
This PC utility converts an industry standard font file to Python source code.
4
26
@@ -42,6 +64,7 @@ Python files produced are interchangeable with those from prior versions: the
42
64
API is unchanged.
43
65
44
66
###### [ Main README] ( ./README.md )
67
+ ###### [ Contents] ( ./FONT_TO_PY.md#0-contents )
45
68
46
69
# 2. Dependencies
47
70
@@ -80,7 +103,7 @@ $ font_to_py.py -k extended FreeSans.ttf 23 my_extended_font.py
80
103
```
81
104
## 3.1 Arguments
82
105
83
- ### 3.1.1 Mandatory positional arguments:
106
+ ### 3.1.1 Mandatory positional arguments
84
107
85
108
1 . Font file path. Must be a ttf or otf file.
86
109
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
89
112
binary font). A warning is output if the output filename does not have a .py
90
113
extension as the creation of a binary font file may not be intended.
91
114
92
- ### 3.1.2 Optional arguments:
115
+ ### 3.1.2 Optional arguments
93
116
94
117
* -f or --fixed If specified, all characters will have the same width. By
95
118
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
104
127
* -l or --largest Ordinal value of largest character to be stored. Default 126.
105
128
* -e or --errchar Ordinal value of character to be rendered if an attempt is
106
129
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 ) .
109
132
* -c or --charset Option to restrict the characters in the font to a specific
110
133
set. See below.
111
134
* -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.
119
138
120
139
The -c option may be used to reduce the size of the font file by limiting the
121
140
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
147
166
fonts from a ` ttf ` , so I suspect the issue may be source fonts lacking the
148
167
required glyphs.
149
168
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
157
170
158
171
In the case of scalable ` ttf ` or ` otf ` source files the specified height is a
159
172
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
178
191
[ here] ( ./writer/writer_demo.py ) .
179
192
The detailed layout of the Python file may be seen [ here] ( ./writer/DRIVERS.md ) .
180
193
194
+ ###### [ Contents] ( ./FONT_TO_PY.md#0-contents )
195
+
181
196
# 4. Python font files
182
197
183
198
Users of the ` Writer ` or ` CWriter ` classes or of
@@ -231,7 +246,9 @@ An alternative implementation of binary fonts may be found in
231
246
[ this repo] ( https://github.com/antirez/microfont ) . It provides for rotated
232
247
rendering to a ` FrameBuffer ` .
233
248
234
- # 6. Dependencies, links and licence
249
+ ###### [ Contents] ( ./FONT_TO_PY.md#0-contents )
250
+
251
+ # 6. Dependencies links and licence
235
252
236
253
The code is released under the MIT licence. The ` font_to_py.py ` utility
237
254
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
290
307
saving will be greater if larger fonts are used as RAM usage is independent of
291
308
the array sizes.
292
309
310
+ ###### [ Contents] ( ./FONT_TO_PY.md#0-contents )
311
+
293
312
# Appendix 2: Recent improvements
294
313
295
314
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.
305
324
Larger character sets are assumed to be sparse and the emitted code uses an
306
325
index optimised for sparse values and a binary search algorithm.
307
326
308
- # Appendix 3: font_test.py
327
+ # Appendix 3: Testing
309
328
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.
313
333
314
334
It runs under Python 3.2 or above. Given a font ` myfont.py ` the following will
315
335
render the supplied string (assuming that ` font_test.py ` has executable
@@ -356,3 +376,35 @@ Start char " " (ord 32) end char "~" (ord 126)
356
376
...................................................
357
377
...................................................
358
378
```
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