-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathREADME.html
5685 lines (5113 loc) · 217 KB
/
README.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2020-06-19 Fri 00:41 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>‎</title>
<meta name="generator" content="Org mode" />
<meta name="author" content="Sacha Chua" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
/* Languages per Org manual */
pre.src-asymptote:before { content: 'Asymptote'; }
pre.src-awk:before { content: 'Awk'; }
pre.src-C:before { content: 'C'; }
/* pre.src-C++ doesn't work in CSS */
pre.src-clojure:before { content: 'Clojure'; }
pre.src-css:before { content: 'CSS'; }
pre.src-D:before { content: 'D'; }
pre.src-ditaa:before { content: 'ditaa'; }
pre.src-dot:before { content: 'Graphviz'; }
pre.src-calc:before { content: 'Emacs Calc'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-fortran:before { content: 'Fortran'; }
pre.src-gnuplot:before { content: 'gnuplot'; }
pre.src-haskell:before { content: 'Haskell'; }
pre.src-hledger:before { content: 'hledger'; }
pre.src-java:before { content: 'Java'; }
pre.src-js:before { content: 'Javascript'; }
pre.src-latex:before { content: 'LaTeX'; }
pre.src-ledger:before { content: 'Ledger'; }
pre.src-lisp:before { content: 'Lisp'; }
pre.src-lilypond:before { content: 'Lilypond'; }
pre.src-lua:before { content: 'Lua'; }
pre.src-matlab:before { content: 'MATLAB'; }
pre.src-mscgen:before { content: 'Mscgen'; }
pre.src-ocaml:before { content: 'Objective Caml'; }
pre.src-octave:before { content: 'Octave'; }
pre.src-org:before { content: 'Org mode'; }
pre.src-oz:before { content: 'OZ'; }
pre.src-plantuml:before { content: 'Plantuml'; }
pre.src-processing:before { content: 'Processing.js'; }
pre.src-python:before { content: 'Python'; }
pre.src-R:before { content: 'R'; }
pre.src-ruby:before { content: 'Ruby'; }
pre.src-sass:before { content: 'Sass'; }
pre.src-scheme:before { content: 'Scheme'; }
pre.src-screen:before { content: 'Gnu Screen'; }
pre.src-sed:before { content: 'Sed'; }
pre.src-sh:before { content: 'shell'; }
pre.src-sql:before { content: 'SQL'; }
pre.src-sqlite:before { content: 'SQLite'; }
/* additional languages in org.el's org-babel-load-languages alist */
pre.src-forth:before { content: 'Forth'; }
pre.src-io:before { content: 'IO'; }
pre.src-J:before { content: 'J'; }
pre.src-makefile:before { content: 'Makefile'; }
pre.src-maxima:before { content: 'Maxima'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-picolisp:before { content: 'Pico Lisp'; }
pre.src-scala:before { content: 'Scala'; }
pre.src-shell:before { content: 'Shell Script'; }
pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
/* additional language identifiers per "defun org-babel-execute"
in ob-*.el */
pre.src-cpp:before { content: 'C++'; }
pre.src-abc:before { content: 'ABC'; }
pre.src-coq:before { content: 'Coq'; }
pre.src-groovy:before { content: 'Groovy'; }
/* additional language identifiers from org-babel-shell-names in
ob-shell.el: ob-shell is the only babel language using a lambda to put
the execution function name together. */
pre.src-bash:before { content: 'bash'; }
pre.src-csh:before { content: 'csh'; }
pre.src-ash:before { content: 'ash'; }
pre.src-dash:before { content: 'dash'; }
pre.src-ksh:before { content: 'ksh'; }
pre.src-mksh:before { content: 'mksh'; }
pre.src-posh:before { content: 'posh'; }
/* Additional Emacs modes also supported by the LaTeX listings package */
pre.src-ada:before { content: 'Ada'; }
pre.src-asm:before { content: 'Assembler'; }
pre.src-caml:before { content: 'Caml'; }
pre.src-delphi:before { content: 'Delphi'; }
pre.src-html:before { content: 'HTML'; }
pre.src-idl:before { content: 'IDL'; }
pre.src-mercury:before { content: 'Mercury'; }
pre.src-metapost:before { content: 'MetaPost'; }
pre.src-modula-2:before { content: 'Modula-2'; }
pre.src-pascal:before { content: 'Pascal'; }
pre.src-ps:before { content: 'PostScript'; }
pre.src-prolog:before { content: 'Prolog'; }
pre.src-simula:before { content: 'Simula'; }
pre.src-tcl:before { content: 'tcl'; }
pre.src-tex:before { content: 'TeX'; }
pre.src-plain-tex:before { content: 'Plain TeX'; }
pre.src-verilog:before { content: 'Verilog'; }
pre.src-vhdl:before { content: 'VHDL'; }
pre.src-xml:before { content: 'XML'; }
pre.src-nxml:before { content: 'XML'; }
/* add a generic configuration mode; LaTeX export needs an additional
(add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
pre.src-conf:before { content: 'Configuration File'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.equation-container {
display: table;
text-align: center;
width: 100%;
}
.equation {
vertical-align: middle;
}
.equation-label {
display: table-cell;
text-align: right;
vertical-align: middle;
}
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
.org-svg { width: 90%; }
/*]]>*/-->
</style>
<link rel="stylesheet" type="text/css"
href="http://sachachua.com/blog/wp-content/themes/sacha-v3/foundation/css/foundation.min.css"></link>
<link rel="stylesheet" type="text/css" href="http://sachachua.com/org-export.css"></link>
<link rel="stylesheet" type="text/css" href="http://sachachua.com/blog/wp-content/themes/sacha-v3/style.css"></link>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2019 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
displayAlign: "center",
displayIndent: "0em",
"HTML-CSS": { scale: 100,
linebreaks: { automatic: "false" },
webFont: "TeX"
},
SVG: {scale: 100,
linebreaks: { automatic: "false" },
font: "TeX"},
NativeMML: {scale: 100},
TeX: { equationNumbers: {autoNumber: "AMS"},
MultLineWidth: "85%",
TagSide: "right",
TagIndent: ".8em"
}
});
</script>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_HTML"></script>
</head>
<body>
<div id="preamble" class="status">
<a name="top" id="top"></a>
</div>
<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org0603a3a">Summary</a></li>
<li><a href="#orgd6ad45a">License</a></li>
<li><a href="#orga3b5e3b">Blog post</a></li>
<li><a href="#org54699ad">Things I needed to install</a>
<ul>
<li><a href="#orgb73ca5f">Errors fixed along the way</a></li>
</ul>
</li>
<li><a href="#org9e3aca6">General font code</a>
<ul>
<li><a href="#org3793a89">Parameters and common functions</a></li>
<li><a href="#org3548ede">Generate guidelines</a></li>
<li><a href="#org9c91af5">Cut into glyphs</a></li>
<li><a href="#org6d154b7">Import SVG outlines into font</a></li>
<li><a href="#orgdcfabfa">Adjust bearings</a></li>
<li><a href="#org1738e5e">Kern the font</a></li>
<li><a href="#org352b731">Hand-tweak the glyphs</a></li>
</ul>
</li>
<li><a href="#org726a11d">Generate fonts</a>
<ul>
<li><a href="#orgda27c7c">Generate sachacHand Light</a></li>
<li><a href="#orgf6ab4bc">Generate sachacHand Regular</a></li>
<li><a href="#org1d4c18b">Generate sachacHand Bold</a></li>
</ul>
</li>
<li><a href="#org0a25f47">Variants</a>
<ul>
<li><a href="#org4a74eee">Import the glyphs for variant1 and variant2</a></li>
<li><a href="#org98dd64b">Okay, how do I space and kern the variants more efficiently?</a></li>
</ul>
</li>
<li><a href="#org842eb3a">Test the fonts</a></li>
<li><a href="#orgf144230">Ideas</a>
<ul>
<li><a href="#orgd23f781"><span class="done DONE">DONE</span> Copy glyphs from hand-edited font</a></li>
<li><a href="#org0fc5067"><span class="todo TODO">TODO</span> Alternate glyphs</a></li>
<li><a href="#org1700929"><span class="todo TODO">TODO</span> Ligatures</a></li>
<li><a href="#org498ae82"><span class="todo TODO">TODO</span> Accents</a></li>
<li><a href="#org1b0e294">Generating a zero-width version?</a></li>
<li><a href="#org6e90cc6"><span class="todo TODO">TODO</span> Make a font for A-</a></li>
</ul>
</li>
<li><a href="#org3dee60b">Extra stuff</a></li>
<li><a href="#orgf8f5f3c">Get information from my blog database</a>
<ul>
<li><a href="#orgdeb02c8">Figure out what glyphs I want based on my blog headings</a></li>
<li><a href="#org99c4320">Make test page with blog headings</a></li>
<li><a href="#orgb2da442">Check glyphs</a></li>
<li><a href="#org828e444">Look up posts with weird glyphs</a></li>
<li><a href="#org1776af3">Get frequency of pairs of characters</a></li>
<li><a href="#orgadab5a0">Copy metrics from my edited font</a></li>
<li><a href="#org4fe0a4e">Copy it to my website</a></li>
</ul>
</li>
<li><a href="#org0e74ef5">Other resources</a></li>
</ul>
</div>
</div>
<div id="outline-container-org0603a3a" class="outline-2">
<h2 id="org0603a3a">Summary</h2>
<div class="outline-text-2" id="text-org0603a3a">
<p>
I wanted to make my own handwriting font. I also wanted to be able to
generate fonts quickly from the handwriting samples I can draw on my
ta [ State ]: EDITED, shown value does not take effect until you set or save it.
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left"><a href="https://pages.sachachua.com/sachac-hand/README.html">https://pages.sachachua.com/sachac-hand/README.html</a></td>
<td class="org-left">This README as HTML</td>
</tr>
<tr>
<td class="org-left"><a href="https://github.com/sachac/sachac-hand">https://github.com/sachac/sachac-hand</a></td>
<td class="org-left">Github repo</td>
</tr>
<tr>
<td class="org-left"><a href="./files/test.html">./files/test.html</a></td>
<td class="org-left">Test pages</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="outline-container-orgd6ad45a" class="outline-2">
<h2 id="orgd6ad45a">License</h2>
</div>
<div id="outline-container-orga3b5e3b" class="outline-2">
<h2 id="orga3b5e3b">Blog post</h2>
<div class="outline-text-2" id="text-orga3b5e3b">
<p>
I wanted to make a font based on my handwriting using only free
software. It turns out that FontForge can be scripted with Python. I
know just a little about Python and even less about typography, but I
managed to hack together something that worked for me. If you're
reading this on my blog at <a href="https://sachachua.com/blog/">https://sachachua.com/blog/</a> , you'll
probably see the new font being used on the blog post titles. Whee!
</p>
<p>
My rough notes are at
<a href="https://github.com/sachac/sachac-hand/">https://github.com/sachac/sachac-hand/</a> . I wanted
to write it as a literate program using Org Babel blocks. It's not
really fully reproducible yet, but it might be a handy starting point.
The basic workflow was:
</p>
<ol class="org-ol">
<li>Generate a template using other fonts as the base.</li>
<li>Import the template into Medibang Paint on my phone and draw
letters on a different layer. (I almost forgot the letter <code>q</code>, so I
had to add it at the last minute.)</li>
<li>Export just the layer with my writing.</li>
<li>Cut the image into separate glyphs using Python and autotrace each one.</li>
<li>Import each glyph into FontForge as an SVG and a PNG.</li>
<li>Set the left side and right side bearing, overriding as needed based on a table.</li>
<li>Figure out kerning classes.</li>
<li>Hand-tweak the contours and kerning.</li>
<li>Use <code>sfnt2woff</code> to export the web font file for use on my blog, and modify the stylesheet to include it.</li>
</ol>
<p>
I really liked being able to specify kerning classes through an Org
Mode table like this:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-right" />
<col class="org-right" />
<col class="org-right" />
<col class="org-right" />
<col class="org-right" />
<col class="org-right" />
<col class="org-right" />
<col class="org-right" />
<col class="org-right" />
</colgroup>
<tbody>
<tr>
<td class="org-left"> </td>
<td class="org-right">None</td>
<td class="org-right">o,a,c,e,d,g,q,w</td>
<td class="org-right">f,t,x,v,y,z</td>
<td class="org-right">h,b,l,i,k</td>
<td class="org-right">j</td>
<td class="org-right">m,n,p,r,u</td>
<td class="org-right">s</td>
<td class="org-right">T</td>
<td class="org-right">zero</td>
</tr>
<tr>
<td class="org-left">None</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
</tr>
<tr>
<td class="org-left">f</td>
<td class="org-right">0</td>
<td class="org-right">-102</td>
<td class="org-right">-61</td>
<td class="org-right">-30</td>
<td class="org-right">0</td>
<td class="org-right">-60</td>
<td class="org-right">0</td>
<td class="org-right">-120</td>
<td class="org-right">-70</td>
</tr>
<tr>
<td class="org-left">t</td>
<td class="org-right">0</td>
<td class="org-right">-70</td>
<td class="org-right">-41</td>
<td class="org-right">-25</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">-120</td>
<td class="org-right">-10</td>
</tr>
<tr>
<td class="org-left">r</td>
<td class="org-right">0</td>
<td class="org-right">-82</td>
<td class="org-right">-41</td>
<td class="org-right">-25</td>
<td class="org-right">0</td>
<td class="org-right">-20</td>
<td class="org-right">0</td>
<td class="org-right">-120</td>
<td class="org-right">29</td>
</tr>
<tr>
<td class="org-left">k</td>
<td class="org-right">0</td>
<td class="org-right">-50</td>
<td class="org-right">-81</td>
<td class="org-right">-20</td>
<td class="org-right">0</td>
<td class="org-right">-20</td>
<td class="org-right">-48</td>
<td class="org-right">-120</td>
<td class="org-right">-79</td>
</tr>
<tr>
<td class="org-left">l</td>
<td class="org-right">0</td>
<td class="org-right">-41</td>
<td class="org-right">-50</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">-120</td>
<td class="org-right">-52</td>
</tr>
<tr>
<td class="org-left">v</td>
<td class="org-right">0</td>
<td class="org-right">-40</td>
<td class="org-right">-35</td>
<td class="org-right">-30</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">-120</td>
<td class="org-right">30</td>
</tr>
<tr>
<td class="org-left">b,o,p</td>
<td class="org-right">0</td>
<td class="org-right">-20</td>
<td class="org-right">-80</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">-120</td>
<td class="org-right">43</td>
</tr>
<tr>
<td class="org-left">a</td>
<td class="org-right">0</td>
<td class="org-right">-23</td>
<td class="org-right">-60</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">-120</td>
<td class="org-right">7</td>
</tr>
<tr>
<td class="org-left">W</td>
<td class="org-right">0</td>
<td class="org-right">-40</td>
<td class="org-right">-30</td>
<td class="org-right">-20</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">-120</td>
<td class="org-right">17</td>
</tr>
<tr>
<td class="org-left">T</td>
<td class="org-right">0</td>
<td class="org-right">-190</td>
<td class="org-right">-120</td>
<td class="org-right">-60</td>
<td class="org-right">0</td>
<td class="org-right">-130</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">-188</td>
</tr>
<tr>
<td class="org-left">F</td>
<td class="org-right">0</td>
<td class="org-right">-100</td>
<td class="org-right">-90</td>
<td class="org-right">-60</td>
<td class="org-right">0</td>
<td class="org-right">-70</td>
<td class="org-right">-100</td>
<td class="org-right">-40</td>
<td class="org-right">-166</td>
</tr>
<tr>
<td class="org-left">two</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">0</td>
<td class="org-right">-53</td>
</tr>
</tbody>
</table>
<p>
I had a hard time defining classes using the FontForge interface
because I occasionally ended up clearing my glyph selection, so it was
great being able to just edit my columns and rows.
</p>
<p>
Clearly my kerning is still very rough–no actual values for j, for
example–but it's a start. Also, I can probably figure out how to
combine this with character pair kerning and have two tables for
easier tweaking.
</p>
<p>
A- insisted on tracing my handwriting template a few times, so I might
actually be able to go through the same process to convert her
handwriting into a font. Whee!
</p>
</div>
</div>
<div id="outline-container-org54699ad" class="outline-2">
<h2 id="org54699ad">Things I needed to install</h2>
<div class="outline-text-2" id="text-org54699ad">
<p>
<code>sudo apt-get install fontforge python3-fontforge python3-numpy python3-sqlalchemy python3-pandas python3-pymysql python3-nltk woff-tools woff2 python3-yattag python3-livereload</code>
</p>
<p>
I compiled autotrace based on my fork at <a href="https://github.com/sachac/autotrace">https://github.com/sachac/autotrace</a> so that it uses Graphicsmagick instead of Imagemagick.
</p>
<p>
I also needed <code>(setenv "LD_LIBRARY_PATH" "/usr/local/lib")</code>. There are probably a bunch of other prerequisites I've forgotten to write down.
</p>
</div>
<div id="outline-container-orgb73ca5f" class="outline-3">
<h3 id="orgb73ca5f">Errors fixed along the way</h3>
<div class="outline-text-3" id="text-orgb73ca5f">
<ul class="org-ul">
<li><code>FileNotFoundError: [Errno 2] No such file or directory: '/home/sacha/.local/lib/python3.8/site-packages/aglfn/agl-aglfn/aglfn.txt'</code>
<ul class="org-ul">
<li>symlink or copy the one from /usr/share/aglfn to the right place</li>
</ul></li>
</ul>
</div>
</div>
</div>
<div id="outline-container-org9e3aca6" class="outline-2">
<h2 id="org9e3aca6">General font code</h2>
<div class="outline-text-2" id="text-org9e3aca6">
</div>
<div id="outline-container-org3793a89" class="outline-3">
<h3 id="org3793a89">Parameters and common functions</h3>
<div class="outline-text-3" id="text-org3793a89">
<div class="org-src-container">
<pre class="src src-python" id="org8e4bb43"><code><span class="org-keyword">import</span> numpy <span class="org-keyword">as</span> np</code>
<code><span class="org-keyword">import</span> pandas <span class="org-keyword">as</span> pd</code>
<code><span class="org-keyword">import</span> aglfn</code>
<code><span class="org-keyword">import</span> fontforge</code>
<code><span class="org-keyword">import</span> subprocess</code>
<code></code>
<code><span class="org-variable-name">params</span> = {<span class="org-string">'template'</span>: <span class="org-string">'template-256.png'</span>,</code>
<code> <span class="org-string">'sample_file'</span>: <span class="org-string">'sample.png'</span>,</code>
<code> <span class="org-string">'name_list'</span>: <span class="org-string">'aglfn.txt'</span>,</code>
<code> <span class="org-string">'new_font_file'</span>: <span class="org-string">'sachacHand.sfd'</span>,</code>
<code> <span class="org-string">'new_otf'</span>: <span class="org-string">'sachacHand.otf'</span>,</code>
<code> <span class="org-string">'new_font_name'</span>: <span class="org-string">'sachacHand'</span>,</code>
<code> <span class="org-string">'new_family_name'</span>: <span class="org-string">'sachacHand'</span>,</code>
<code> <span class="org-string">'new_full_name'</span>: <span class="org-string">'sachacHand'</span>,</code>
<code> <span class="org-string">'text_color'</span>: <span class="org-string">'lightgray'</span>,</code>
<code> <span class="org-string">'glyph_dir'</span>: <span class="org-string">'glyphs/'</span>,</code>
<code> <span class="org-string">'letters'</span>: <span class="org-string">'HOnodpagscebhklftijmnruwvxyzCGABRDLEFIJKMNPQSTUVWXYZ0123456789?:;-–—=!\'’"“”@/\\~_#$%&()*+,.<>[]^`{|}q'</span>,</code>
<code> <span class="org-string">'direction'</span>: <span class="org-string">'vertical'</span>,</code>
<code> <span class="org-string">'rows'</span>: 10, </code>
<code> <span class="org-string">'columns'</span>: 10, </code>
<code> <span class="org-string">'x_height'</span>: 368,</code>
<code> <span class="org-string">'em'</span>: 1000, </code>
<code> <span class="org-string">'em_width'</span>: 1000, </code>
<code> <span class="org-string">'row_padding'</span>: 0,</code>
<code> <span class="org-string">'ascent'</span>: 800, </code>
<code> <span class="org-string">'descent'</span>: 200, </code>
<code> <span class="org-string">'height'</span>: 500, </code>
<code> <span class="org-string">'width'</span>: 500, </code>
<code> <span class="org-string">'caps'</span>: 650,</code>
<code> <span class="org-string">'line_width'</span>: 3,</code>
<code> <span class="org-string">'text'</span>: <span class="org-string">"Python+FontForge+Org: I made a font based on my handwriting!"</span></code>
<code> }</code>
<code>fontforge.loadNamelist(params[<span class="org-string">'name_list'</span>])</code>
<code><span class="org-variable-name">params</span>[<span class="org-string">'font_size'</span>] = <span class="org-builtin">int</span>(params[<span class="org-string">'em'</span>])</code>
<code><span class="org-variable-name">params</span>[<span class="org-string">'baseline'</span>] = params[<span class="org-string">'em'</span>] - params[<span class="org-string">'descent'</span>]</code>
<code></code>
<code><span class="org-keyword">def</span> <span class="org-function-name">transpose_letters</span>(letters, width, height):</code>
<code> <span class="org-keyword">return</span> <span class="org-string">''</span>.join(np.reshape(<span class="org-builtin">list</span>(letters.ljust(width * height)), (height, width)).transpose().reshape(-1))</code>
<code></code>
<code><span class="org-comment-delimiter"># </span><span class="org-comment">Return glyph name of s, or s if none (possibly variant)</span></code>
<code><span class="org-keyword">def</span> <span class="org-function-name">glyph_name</span>(s):</code>
<code> <span class="org-keyword">return</span> aglfn.name(s) <span class="org-keyword">or</span> s</code>
<code></code>
<code><span class="org-keyword">def</span> <span class="org-function-name">get_glyph</span>(font, g):</code>
<code> <span class="org-variable-name">pos</span> = font.findEncodingSlot(g)</code>
<code> <span class="org-keyword">if</span> pos == -1:</code>
<code> <span class="org-keyword">return</span> font.createChar(pos, g)</code>
<code> <span class="org-keyword">else</span>:</code>
<code> <span class="org-keyword">return</span> font[pos]</code>
<code></code>
<code><span class="org-keyword">def</span> <span class="org-function-name">glyph_matrix</span>(font=<span class="org-constant">None</span>, matrix=<span class="org-constant">None</span>, letters=<span class="org-constant">None</span>, rows=0, columns=0, direction=<span class="org-string">'horizontal'</span>, **kwargs):</code>
<code> <span class="org-keyword">if</span> matrix:</code>
<code> <span class="org-keyword">if</span> <span class="org-builtin">isinstance</span>(matrix[0], <span class="org-builtin">str</span>):</code>
<code> <span class="org-comment-delimiter"># </span><span class="org-comment">Split each</span></code>
<code> <span class="org-variable-name">matrix</span> = [x.split(<span class="org-string">','</span>) <span class="org-keyword">for</span> x <span class="org-keyword">in</span> matrix]</code>
<code> <span class="org-keyword">else</span>:</code>
<code> <span class="org-variable-name">matrix</span> = matrix[:] <span class="org-comment-delimiter"># </span><span class="org-comment">copy the list</span></code>
<code> <span class="org-keyword">else</span>:</code>
<code> <span class="org-variable-name">matrix</span> = np.reshape(<span class="org-builtin">list</span>(letters.ljust(rows * columns))[0:rows * columns], (rows, columns))</code>
<code> <span class="org-keyword">if</span> direction == <span class="org-string">'vertical'</span>:</code>
<code> <span class="org-variable-name">matrix</span> = matrix.transpose()</code>
<code> <span class="org-variable-name">matrix</span> = [[glyph_name(x) <span class="org-keyword">if</span> x != <span class="org-string">'None'</span> <span class="org-keyword">else</span> <span class="org-constant">None</span> <span class="org-keyword">for</span> x <span class="org-keyword">in</span> row] <span class="org-keyword">for</span> row <span class="org-keyword">in</span> matrix]</code>
<code> <span class="org-keyword">if</span> font:</code>
<code> <span class="org-keyword">for</span> r, row <span class="org-keyword">in</span> <span class="org-builtin">enumerate</span>(matrix):</code>
<code> <span class="org-keyword">for</span> c, col <span class="org-keyword">in</span> <span class="org-builtin">enumerate</span>(row):</code>
<code> <span class="org-keyword">if</span> col <span class="org-keyword">is</span> <span class="org-constant">None</span>: <span class="org-keyword">continue</span></code>
<code> matrix[r][c] = get_glyph(font, col)</code>
<code> <span class="org-keyword">return</span> matrix</code>
<code></code>
<code><span class="org-keyword">def</span> <span class="org-function-name">glyph_filename_base</span>(glyph_name):</code>
<code> <span class="org-keyword">try</span>:</code>
<code> <span class="org-keyword">return</span> <span class="org-string">'uni%s-%s'</span> % (<span class="org-builtin">hex</span>(<span class="org-builtin">ord</span>(aglfn.to_glyph(glyph_name))).replace(<span class="org-string">'0x'</span>, <span class="org-string">''</span>).zfill(4), glyph_name)</code>
<code> <span class="org-keyword">except</span>:</code>
<code> <span class="org-keyword">return</span> glyph_name</code>
<code></code>
<code><span class="org-keyword">def</span> <span class="org-function-name">load_font</span>(params):</code>
<code> <span class="org-keyword">if</span> <span class="org-builtin">type</span>(params) == <span class="org-builtin">str</span>:</code>
<code> <span class="org-keyword">return</span> fontforge.<span class="org-builtin">open</span>(params)</code>
<code> <span class="org-keyword">else</span>:</code>
<code> <span class="org-keyword">return</span> fontforge.<span class="org-builtin">open</span>(params[<span class="org-string">'new_font_file'</span>])</code>
<code></code>
<code><span class="org-keyword">def</span> <span class="org-function-name">save_font</span>(font, font_filename=<span class="org-constant">None</span>, **kwargs):</code>
<code> <span class="org-keyword">if</span> font_filename <span class="org-keyword">is</span> <span class="org-constant">None</span>:</code>
<code> <span class="org-variable-name">font_filename</span> = font.fontname + <span class="org-string">'.sfd'</span></code>
<code> font.save(font_filename)</code>
<code> font.generate(font_filename.replace(<span class="org-string">'.sfd'</span>, <span class="org-string">'.otf'</span>))</code>
<code> subprocess.call([<span class="org-string">'sfnt2woff'</span>, font_filename.replace(<span class="org-string">'.sfd'</span>, <span class="org-string">'.otf'</span>)])</code>
<code></code>
<code><span class="org-keyword">import</span> orgbabelhelper <span class="org-keyword">as</span> ob</code>
<code><span class="org-keyword">def</span> <span class="org-function-name">out</span>(df, **kwargs):</code>
<code> <span class="org-keyword">print</span>(ob.dataframe_to_orgtable(df, **kwargs))</code>
<code></code>
</pre>
</div>
</div>
</div>
<div id="outline-container-org3548ede" class="outline-3">
<h3 id="org3548ede">Generate guidelines</h3>
<div class="outline-text-3" id="text-org3548ede">
</div>
<div id="outline-container-org687835d" class="outline-4">
<h4 id="org687835d">Code to make the template</h4>
<div class="outline-text-4" id="text-org687835d">
<div class="org-src-container">
<pre class="src src-python" id="org5cd62a8"><code><span class="org-keyword">from</span> PIL <span class="org-keyword">import</span> Image, ImageFont, ImageDraw</code>
<code></code>
<code><span class="org-comment-delimiter">#</span><span class="org-comment">LETTERS = 'abcd'</span></code>
<code><span class="org-comment-delimiter"># </span><span class="org-comment">Baseline is red</span></code>
<code><span class="org-comment-delimiter"># </span><span class="org-comment">Top of glyph is light blue</span></code>
<code><span class="org-comment-delimiter"># </span><span class="org-comment">Bottom of glyph is blue</span></code>
<code><span class="org-keyword">def</span> <span class="org-function-name">draw_letter</span>(column, row, letter, params):</code>
<code> <span class="org-variable-name">draw</span> = params[<span class="org-string">'draw'</span>]</code>
<code> <span class="org-variable-name">sized_padding</span> = <span class="org-builtin">int</span>(params[<span class="org-string">'row_padding'</span>] * params[<span class="org-string">'em'</span>] / params[<span class="org-string">'height'</span>])</code>
<code> <span class="org-variable-name">origin</span> = (column * params[<span class="org-string">'em_width'</span>], row * (params[<span class="org-string">'em'</span>] + sized_padding))</code>
<code> draw.line((origin[0], origin[1], origin[0] + params[<span class="org-string">'em_width'</span>], origin[1]), fill=<span class="org-string">'lightblue'</span>, width=params[<span class="org-string">'line_width'</span>])</code>
<code> draw.line((origin[0], origin[1], origin[0], origin[1] + params[<span class="org-string">'em'</span>]), fill=<span class="org-string">'lightgray'</span>, width=params[<span class="org-string">'line_width'</span>])</code>
<code> draw.line((origin[0], origin[1] + params[<span class="org-string">'ascent'</span>] - params[<span class="org-string">'x_height'</span>], origin[0] + params[<span class="org-string">'em_width'</span>], origin[1] + params[<span class="org-string">'ascent'</span>] - params[<span class="org-string">'x_height'</span>]), fill=<span class="org-string">'lightgray'</span>, width=params[<span class="org-string">'line_width'</span>])</code>
<code> draw.line((origin[0], origin[1] + params[<span class="org-string">'ascent'</span>], origin[0] + params[<span class="org-string">'em_width'</span>], origin[1] + params[<span class="org-string">'ascent'</span>]), fill=<span class="org-string">'red'</span>, width=params[<span class="org-string">'line_width'</span>])</code>
<code> draw.line((origin[0], origin[1] + params[<span class="org-string">'ascent'</span>] - params[<span class="org-string">'caps'</span>], origin[0] + params[<span class="org-string">'em_width'</span>], origin[1] + params[<span class="org-string">'ascent'</span>] - params[<span class="org-string">'caps'</span>]), fill=<span class="org-string">'lightgreen'</span>, width=params[<span class="org-string">'line_width'</span>])</code>
<code> draw.line((origin[0], origin[1] + params[<span class="org-string">'em'</span>], origin[0] + params[<span class="org-string">'em_width'</span>], origin[1] + params[<span class="org-string">'em'</span>]), fill=<span class="org-string">'blue'</span>, width=params[<span class="org-string">'line_width'</span>])</code>
<code> <span class="org-variable-name">width</span>, <span class="org-variable-name">height</span> = draw.textsize(letter, font=params[<span class="org-string">'font'</span>])</code>
<code> draw.text((origin[0] + (params[<span class="org-string">'em_width'</span>] - width) / 2, origin[1]), letter, font=params[<span class="org-string">'font'</span>], fill=params[<span class="org-string">'text_color'</span>])</code>
<code></code>
<code><span class="org-keyword">def</span> <span class="org-function-name">make_template</span>(params):</code>
<code> <span class="org-variable-name">sized_padding</span> = <span class="org-builtin">int</span>(params[<span class="org-string">'row_padding'</span>] * params[<span class="org-string">'em'</span>] / params[<span class="org-string">'height'</span>])</code>
<code> <span class="org-variable-name">img</span> = Image.new(<span class="org-string">'RGB'</span>, (params[<span class="org-string">'columns'</span>] * params[<span class="org-string">'em_width'</span>], params[<span class="org-string">'rows'</span>] * (params[<span class="org-string">'em'</span>] + sized_padding)), <span class="org-string">'white'</span>)</code>
<code> <span class="org-variable-name">params</span>[<span class="org-string">'draw'</span>] = ImageDraw.Draw(img)</code>
<code> <span class="org-variable-name">params</span>[<span class="org-string">'font'</span>] = ImageFont.truetype(params[<span class="org-string">'font_name'</span>], params[<span class="org-string">'font_size'</span>])</code>
<code> <span class="org-variable-name">matrix</span> = glyph_matrix(**params)</code>
<code> <span class="org-keyword">for</span> r, row <span class="org-keyword">in</span> <span class="org-builtin">enumerate</span>(matrix):</code>
<code> <span class="org-keyword">for</span> c, ch <span class="org-keyword">in</span> <span class="org-builtin">enumerate</span>(row):</code>
<code> draw_letter(c, r, ch, params)</code>
<code> img.thumbnail((params[<span class="org-string">'columns'</span>] * params[<span class="org-string">'width'</span>], params[<span class="org-string">'rows'</span>] * (params[<span class="org-string">'height'</span>] + params[<span class="org-string">'row_padding'</span>])))</code>
<code> img.save(params[<span class="org-string">'template'</span>])</code>
<code> <span class="org-keyword">return</span> params[<span class="org-string">'template'</span>]</code>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgc8dd90f" class="outline-4">
<h4 id="orgc8dd90f">Actually make the templates</h4>
<div class="outline-text-4" id="text-orgc8dd90f">
<div class="org-src-container">
<pre class="src src-python"><code><<params>></code>
<code><<def_make_template>></code>
<code><span class="org-comment-delimiter">#</span><span class="org-comment">make_template({**params, 'font_name': '/home/sacha/.fonts/Romochka.otf', 'template': 'template-romochka.png', 'row_padding': 15}) </span></code>
<code><span class="org-comment-delimiter">#</span><span class="org-comment">make_template({**params, 'font_name': '/home/sacha/.fonts/Breip.ttf', 'template': 'template-breip.png', 'row_padding': 15}) </span></code>
<code>make_template({**params, <span class="org-string">'font_name'</span>: <span class="org-string">'/home/sacha/.fonts/KGPrimaryDots.ttf'</span>, </code>
<code> <span class="org-string">'letters'</span>: <span class="org-string">'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890?:;-–—=!\'’"“”@/\\~_#$%&()*+,.<>[]^`{|}'</span>, <span class="org-string">'direction'</span>: <span class="org-string">'horizontal'</span>, <span class="org-string">'text_color'</span>: <span class="org-string">'black'</span>,</code>
<code> <span class="org-string">'template'</span>: <span class="org-string">'template-kg.png'</span>, <span class="org-string">'ascent'</span>: 800, <span class="org-string">'descent'</span>: 200, <span class="org-string">'caps'</span>: 600, <span class="org-string">'x_height'</span>: 340, <span class="org-string">'row_padding'</span>: 50}) </code>
<code>make_template({**params, <span class="org-string">'font_name'</span>: <span class="org-string">'sachacHand.otf'</span>, <span class="org-string">'template'</span>: <span class="org-string">'template-sachacHand.png'</span>, <span class="org-string">'row_padding'</span>: 50})</code>
<code><span class="org-keyword">return</span> make_template({**params, <span class="org-string">'font_name'</span>: <span class="org-string">'sachacHand.otf'</span>, </code>
<code><span class="org-string">'template'</span>: <span class="org-string">'template-sample.png'</span>, <span class="org-string">'direction'</span>: <span class="org-string">'horizontal'</span>, <span class="org-string">'rows'</span>: 4, <span class="org-string">'columns'</span>: 4, <span class="org-string">'height'</span>: 100, <span class="org-string">'width'</span>: 100, <span class="org-string">'row_padding'</span>: 100 }) </code>
</pre>
</div>
<div class="figure">
<p><img src="files/template-sample.png" alt="template-sample.png" />
</p>
</div>
</div>
</div>
</div>
<div id="outline-container-org9c91af5" class="outline-3">
<h3 id="org9c91af5">Cut into glyphs</h3>
<div class="outline-text-3" id="text-org9c91af5">
<div class="org-src-container">
<pre class="src src-python" id="org85f9070"><code><span class="org-keyword">import</span> os</code>
<code><span class="org-keyword">import</span> libxml2</code>
<code><span class="org-keyword">from</span> PIL <span class="org-keyword">import</span> Image, ImageOps</code>
<code><span class="org-keyword">import</span> subprocess</code>
<code><span class="org-keyword">def</span> <span class="org-function-name">cut_glyphs</span>(sample_file=<span class="org-string">""</span>, letters=<span class="org-string">""</span>, direction=<span class="org-string">""</span>, columns=0, rows=0, height=0, width=0, row_padding=0, glyph_dir=<span class="org-string">'glyphs'</span>, matrix=<span class="org-constant">None</span>, force=<span class="org-constant">False</span>, **kwargs):</code>
<code> <span class="org-variable-name">im</span> = Image.<span class="org-builtin">open</span>(sample_file).convert(<span class="org-string">'1'</span>)</code>
<code> <span class="org-keyword">if</span> <span class="org-keyword">not</span> os.path.exists(glyph_dir):</code>
<code> os.makedirs(glyph_dir)</code>
<code> <span class="org-variable-name">matrix</span> = glyph_matrix(matrix=matrix, letters=letters, direction=direction, columns=columns, rows=rows)</code>
<code> <span class="org-keyword">for</span> r, row <span class="org-keyword">in</span> <span class="org-builtin">enumerate</span>(matrix):</code>
<code> <span class="org-variable-name">top</span> = r * (height + row_padding)</code>
<code> <span class="org-variable-name">bottom</span> = top + height</code>
<code> <span class="org-keyword">for</span> c, ch <span class="org-keyword">in</span> <span class="org-builtin">enumerate</span>(row):</code>
<code> <span class="org-keyword">if</span> ch <span class="org-keyword">is</span> <span class="org-constant">None</span>: <span class="org-keyword">continue</span></code>
<code> <span class="org-variable-name">filename</span> = os.path.join(glyph_dir, glyph_filename_base(ch) + <span class="org-string">'.pbm'</span>)</code>
<code> <span class="org-keyword">if</span> os.path.exists(filename) <span class="org-keyword">and</span> <span class="org-keyword">not</span> force: <span class="org-keyword">continue</span></code>
<code> <span class="org-variable-name">left</span> = c * width</code>
<code> <span class="org-variable-name">right</span> = left + width</code>
<code> <span class="org-variable-name">small</span> = im.crop((left, top, right, bottom))</code>
<code> small.save(filename)</code>
<code> <span class="org-variable-name">svg</span> = filename.replace(<span class="org-string">'.pbm'</span>, <span class="org-string">'.svg'</span>)</code>
<code> <span class="org-variable-name">png</span> = filename.replace(<span class="org-string">'.pbm'</span>, <span class="org-string">'.png'</span>)</code>
<code> small.save(png)</code>
<code> subprocess.call([<span class="org-string">'autotrace'</span>, <span class="org-string">'-output-file'</span>, svg, filename])</code>
<code> <span class="org-variable-name">doc</span> = libxml2.parseFile(svg)</code>
<code> <span class="org-variable-name">root</span> = doc.children</code>
<code> <span class="org-variable-name">child</span> = root.children</code>
<code> child.<span class="org-builtin">next</span>.unlinkNode()</code>
<code> doc.saveFile(svg)</code>
</pre>
</div>
</div>
</div>
<div id="outline-container-org6d154b7" class="outline-3">
<h3 id="org6d154b7">Import SVG outlines into font</h3>
<div class="outline-text-3" id="text-org6d154b7">
<div class="org-src-container">
<pre class="src src-python" id="org9766a16"><code><span class="org-keyword">import</span> fontforge</code>
<code><span class="org-keyword">import</span> os</code>
<code><span class="org-keyword">import</span> aglfn</code>
<code></code>
<code><span class="org-keyword">def</span> <span class="org-function-name">set_up_font_info</span>(font, new_family_name=<span class="org-string">""</span>, new_font_name=<span class="org-string">""</span>, new_full_name=<span class="org-string">""</span>, em=1000, descent=200, ascent=800, **kwargs):</code>
<code> <span class="org-variable-name">font.encoding</span> = <span class="org-string">'UnicodeFull'</span></code>
<code> <span class="org-variable-name">font.fontname</span> = new_font_name</code>
<code> <span class="org-variable-name">font.familyname</span> = new_family_name</code>
<code> <span class="org-variable-name">font.fullname</span> = new_full_name</code>
<code> <span class="org-variable-name">font.em</span> = em</code>
<code> <span class="org-variable-name">font.descent</span> = descent</code>
<code> <span class="org-variable-name">font.ascent</span> = ascent</code>
<code> <span class="org-keyword">return</span> font</code>
<code></code>
<code><span class="org-keyword">def</span> <span class="org-function-name">import_glyphs</span>(font, glyph_dir=<span class="org-string">'glyphs'</span>, letters=<span class="org-constant">None</span>, columns=<span class="org-constant">None</span>, rows=<span class="org-constant">None</span>, direction=<span class="org-constant">None</span>, matrix=<span class="org-constant">None</span>, height=0, **kwargs):</code>
<code> <span class="org-variable-name">old_em</span> = font.em</code>
<code> <span class="org-variable-name">font.em</span> = height</code>
<code> <span class="org-variable-name">matrix</span> = glyph_matrix(font=font, matrix=matrix, letters=letters, columns=columns, rows=rows, direction=direction)</code>
<code> <span class="org-keyword">for</span> row <span class="org-keyword">in</span> matrix:</code>
<code> <span class="org-keyword">for</span> g <span class="org-keyword">in</span> row:</code>
<code> <span class="org-keyword">if</span> g <span class="org-keyword">is</span> <span class="org-constant">None</span>: <span class="org-keyword">continue</span></code>
<code> <span class="org-keyword">try</span>:</code>
<code> <span class="org-variable-name">base</span> = glyph_filename_base(g.glyphname)</code>
<code> <span class="org-variable-name">svg_filename</span> = os.path.join(glyph_dir, base + <span class="org-string">'.svg'</span>)</code>
<code> <span class="org-variable-name">png_filename</span> = os.path.join(glyph_dir, base + <span class="org-string">'.png'</span>)</code>
<code> g.clear()</code>
<code> g.importOutlines(png_filename)</code>
<code> g.importOutlines(svg_filename)</code>
<code> <span class="org-keyword">except</span> <span class="org-type">Exception</span> <span class="org-keyword">as</span> e:</code>
<code> <span class="org-keyword">print</span>(<span class="org-string">"Error with "</span>, g, e)</code>
<code> <span class="org-variable-name">font.em</span> = old_em</code>
<code> <span class="org-keyword">return</span> font</code>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgdcfabfa" class="outline-3">
<h3 id="orgdcfabfa">Adjust bearings</h3>
<div class="outline-text-3" id="text-orgdcfabfa">
<div class="org-src-container">
<pre class="src src-python" id="org78e85b6"><code><span class="org-keyword">import</span> re</code>
<code><span class="org-comment-delimiter"># </span><span class="org-comment">Return glyph name without .suffix</span></code>
<code><span class="org-keyword">def</span> <span class="org-function-name">glyph_base_name</span>(x):</code>
<code> <span class="org-variable-name">m</span> = re.match(r<span class="org-string">"([^.]+)\..+"</span>, x)</code>
<code> <span class="org-keyword">return</span> m.group(1) <span class="org-keyword">if</span> m <span class="org-keyword">else</span> x</code>
<code><span class="org-keyword">def</span> <span class="org-function-name">glyph_suffix</span>(x):</code>
<code> <span class="org-variable-name">m</span> = re.match(r<span class="org-string">"([^.]+)\.(.+)"</span>, x)</code>
<code> <span class="org-keyword">return</span> m.group(2) <span class="org-keyword">if</span> m <span class="org-keyword">else</span> <span class="org-string">''</span></code>
<code></code>
<code><span class="org-keyword">def</span> <span class="org-function-name">set_bearings</span>(font, bearings, **kwargs):</code>
<code> <span class="org-variable-name">bearing_dict</span> = {}</code>
<code> <span class="org-keyword">for</span> row <span class="org-keyword">in</span> bearings[1:]:</code>
<code> bearing_dict[row[0]] = row</code>
<code> <span class="org-keyword">for</span> g <span class="org-keyword">in</span> font:</code>
<code> <span class="org-variable-name">key</span> = font[g].glyphname</code>
<code> <span class="org-variable-name">m</span> = glyph_base_name(key)</code>
<code> <span class="org-keyword">if</span> <span class="org-keyword">not</span> key <span class="org-keyword">in</span> bearing_dict:</code>
<code> <span class="org-keyword">if</span> m <span class="org-keyword">and</span> m <span class="org-keyword">in</span> bearing_dict:</code>
<code> <span class="org-variable-name">key</span> = m</code>
<code> <span class="org-keyword">else</span>:</code>
<code> <span class="org-variable-name">key</span> = <span class="org-string">'Default'</span></code>
<code> <span class="org-keyword">if</span> bearing_dict[key][1] != <span class="org-string">''</span>:</code>
<code> font[g]<span class="org-variable-name">.left_side_bearing</span> = bearing_dict[key][1]</code>
<code> <span class="org-keyword">else</span>:</code>
<code> font[g]<span class="org-variable-name">.left_side_bearing</span> = bearing_dict[<span class="org-string">'Default'</span>][1]</code>
<code> <span class="org-keyword">if</span> bearing_dict[key][2] != <span class="org-string">''</span>:</code>
<code> font[g]<span class="org-variable-name">.right_side_bearing</span> = bearing_dict[key][2]</code>
<code> <span class="org-keyword">else</span>:</code>
<code> font[g]<span class="org-variable-name">.right_side_bearing</span> = bearing_dict[<span class="org-string">'Default'</span>][2]</code>
<code> <span class="org-keyword">if</span> <span class="org-string">'space'</span> <span class="org-keyword">not</span> <span class="org-keyword">in</span> bearing_dict:</code>
<code> <span class="org-variable-name">space</span> = font.createMappedChar(<span class="org-string">'space'</span>)</code>
<code> <span class="org-variable-name">space.width</span> = <span class="org-builtin">int</span>(font.em / 5)</code>
<code> <span class="org-keyword">return</span> font</code>
</pre>
</div>
</div>
</div>
<div id="outline-container-org1738e5e" class="outline-3">
<h3 id="org1738e5e">Kern the font</h3>
<div class="outline-text-3" id="text-org1738e5e">
</div>
<div id="outline-container-org06893d2" class="outline-4">
<h4 id="org06893d2">Kern by classes</h4>
<div class="outline-text-4" id="text-org06893d2">
<p>
NOTE: This removes the old kerning table.
</p>
<div class="org-src-container">
<pre class="src src-python" id="org43b6171"><code><span class="org-keyword">def</span> <span class="org-function-name">get_classes</span>(row):</code>
<code> <span class="org-variable-name">result</span> = []</code>
<code> <span class="org-keyword">for</span> x <span class="org-keyword">in</span> row:</code>
<code> <span class="org-keyword">if</span> x == <span class="org-string">""</span> <span class="org-keyword">or</span> x == <span class="org-string">"None"</span> <span class="org-keyword">or</span> x <span class="org-keyword">is</span> <span class="org-constant">None</span>:</code>
<code> result.append(<span class="org-constant">None</span>)</code>
<code> <span class="org-keyword">elif</span> <span class="org-builtin">isinstance</span>(x, <span class="org-builtin">str</span>):</code>
<code> result.append(x.split(<span class="org-string">','</span>))</code>
<code> <span class="org-keyword">else</span>:</code>
<code> result.append(x)</code>
<code> <span class="org-keyword">return</span> result</code>
<code></code>
<code><span class="org-keyword">def</span> <span class="org-function-name">kern_classes</span>(font, kerning_matrix):</code>
<code> <span class="org-keyword">try</span>:</code>
<code> font.removeLookup(<span class="org-string">'kern'</span>)</code>