├── .gitignore
├── test
├── 1.gif
├── 1.mp4
├── 1.odt
├── all.odt
└── z80-sans.ttf
├── resources
├── droid-sans-mono.ttf
├── NotoSansMono-Regular.ttf
├── fontcustom.diff
├── fontforge.diff
└── instructions.json
├── .gitmodules
├── ttf_to_ttx.sh
├── ttx_to_ttf.sh
├── LICENSE.txt
├── LICENSE.OFL.txt
├── LICENSE.LGPL3.txt
├── README.md
├── LICENSE.Apache.txt
└── gen.py
/.gitignore:
--------------------------------------------------------------------------------
1 | .fontcustom-manifest.json
2 | out_svg/
3 |
--------------------------------------------------------------------------------
/test/1.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevesnunes/z80-sans/HEAD/test/1.gif
--------------------------------------------------------------------------------
/test/1.mp4:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevesnunes/z80-sans/HEAD/test/1.mp4
--------------------------------------------------------------------------------
/test/1.odt:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevesnunes/z80-sans/HEAD/test/1.odt
--------------------------------------------------------------------------------
/test/all.odt:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevesnunes/z80-sans/HEAD/test/all.odt
--------------------------------------------------------------------------------
/test/z80-sans.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevesnunes/z80-sans/HEAD/test/z80-sans.ttf
--------------------------------------------------------------------------------
/resources/droid-sans-mono.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevesnunes/z80-sans/HEAD/resources/droid-sans-mono.ttf
--------------------------------------------------------------------------------
/resources/NotoSansMono-Regular.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevesnunes/z80-sans/HEAD/resources/NotoSansMono-Regular.ttf
--------------------------------------------------------------------------------
/.gitmodules:
--------------------------------------------------------------------------------
1 | [submodule "modules/fontcustom"]
2 | path = modules/fontcustom
3 | url = https://github.com/FontCustom/fontcustom
4 | [submodule "modules/woff2"]
5 | path = modules/woff2
6 | url = https://github.com/google/woff2.git
7 | [submodule "modules/fontforge"]
8 | path = modules/fontforge
9 | url = https://github.com/fontforge/fontforge
10 |
--------------------------------------------------------------------------------
/ttf_to_ttx.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | set -eux
4 |
5 | rm -f .fontcustom-manifest.json
6 | env GEM_PATH="$HOME/.gem/ruby/2.7.0:$GEM_PATH" ~/.gem/ruby/2.7.0/gems/fontcustom-2.0.0/bin/fontcustom compile ./out_svg/ --debug
7 |
8 | cd ./fontcustom
9 | rm -f fontcustom_*.ttx
10 | last_ttf=$(find . -iname 'fontcustom_*.ttf' -print0 | xargs -r -0 ls -1 -t | head -1)
11 | ttx "$last_ttf"
12 |
--------------------------------------------------------------------------------
/ttx_to_ttf.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | set -eux
4 |
5 | cd ./fontcustom
6 | rm -f fontcustom_*.ttf
7 | last_ttx=$(find . -iname 'fontcustom_*.ttx' -print0 | xargs -r -0 ls -1 -t | head -1)
8 | ttx "$last_ttx"
9 |
10 | rm -f ~/.local/share/fonts/fontcustom_*.ttf
11 | last_ttf=$(find . -iname 'fontcustom_*.ttf' -print0 | xargs -r -0 ls -1 -t | head -1)
12 | cp "$last_ttf" ~/.local/share/fonts/
13 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) 2024 nevesnunes
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in
11 | all copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 | THE SOFTWARE.
20 |
--------------------------------------------------------------------------------
/resources/fontcustom.diff:
--------------------------------------------------------------------------------
1 | diff --git a/lib/fontcustom/scripts/generate.py b/lib/fontcustom/scripts/generate.py
2 | index dfe6bee..b5d95ea 100755
3 | --- a/lib/fontcustom/scripts/generate.py
4 | +++ b/lib/fontcustom/scripts/generate.py
5 | @@ -30,18 +30,22 @@ options = manifest['options']
6 |
7 | design_px = options['font_em'] / options['font_design_size']
8 |
9 | -font = fontforge.font()
10 | -font.encoding = 'UnicodeFull'
11 | -font.design_size = options['font_design_size']
12 | -font.em = options['font_em']
13 | -font.ascent = options['font_ascent']
14 | -font.descent = options['font_descent']
15 | -font.fontname = options['font_name']
16 | -font.familyname = options['font_name']
17 | -font.fullname = options['font_name']
18 | -font.copyright = options['copyright']
19 | +font = fontforge.open('/tmp/base.ttf')
20 | +# font = fontforge.font()
21 | +# font.encoding = 'UnicodeFull'
22 | +# font.design_size = options['font_design_size']
23 | +# font.em = options['font_em']
24 | +# font.ascent = options['font_ascent']
25 | +# font.descent = options['font_descent']
26 | +# font.fontname = options['font_name']
27 | +# font.familyname = options['font_name']
28 | +# font.fullname = options['font_name']
29 | +font.fontname = 'Z80 Sans'
30 | +font.familyname = 'Z80 Sans'
31 | +font.fullname = 'Z80 Sans'
32 | +# font.copyright = options['copyright']
33 | if options['autowidth']:
34 | - font.autoWidth(0, 0, options['font_em'])
35 | + font.autoWidth(0, 0, 2500)
36 |
37 | #
38 | # Glyphs
39 | @@ -65,9 +69,10 @@ def createGlyph( name, source, code ):
40 | if ext == '.svg':
41 | temp = removeSwitchFromSvg(source)
42 | glyph = font.createChar(code, name)
43 | - glyph.importOutlines(temp)
44 | + glyph.importOutlines(temp, correctdir=False, scale=False)
45 | os.unlink(temp)
46 |
47 | + '''
48 | if options['autowidth']:
49 | glyph.left_side_bearing = glyph.right_side_bearing = 0
50 | glyph.round()
51 | @@ -79,6 +84,13 @@ def createGlyph( name, source, code ):
52 | shift = glyph.left_side_bearing % design_px
53 | glyph.left_side_bearing = glyph.left_side_bearing - shift
54 | glyph.right_side_bearing = glyph.right_side_bearing + shift
55 | + '''
56 | + _ = glyph.left_side_bearing
57 | + _ = glyph.right_side_bearing
58 | + glyph.left_side_bearing = 0
59 | + glyph.right_side_bearing = 0
60 | + glyph.round()
61 | +
62 |
63 | # Add valid space glyph to avoid "unknown character" box on IE11
64 | glyph = font.createChar(32)
65 |
--------------------------------------------------------------------------------
/resources/fontforge.diff:
--------------------------------------------------------------------------------
1 | diff --git a/fontforge/python.c b/fontforge/python.c
2 | index 8a3cb0fd9..d50b8f32c 100644
3 | --- a/fontforge/python.c
4 | +++ b/fontforge/python.c
5 | @@ -7059,14 +7059,6 @@ return( -1 );
6 | return( 0 );
7 | }
8 |
9 | -static PyObject *PyFF_Glyph_get_rsb(PyFF_Glyph *self, void *UNUSED(closure)) {
10 | - DBounds b;
11 | -
12 | - SplineCharFindBounds(self->sc,&b);
13 | -
14 | -return( Py_BuildValue("d", self->sc->width - b.maxx ));
15 | -}
16 | -
17 | static int PyFF_Glyph_set_rsb(PyFF_Glyph *self,PyObject *value, void *UNUSED(closure)) {
18 | int val;
19 | DBounds b;
20 | @@ -7081,6 +7073,17 @@ return( -1 );
21 | return( 0 );
22 | }
23 |
24 | +static PyObject *PyFF_Glyph_get_rsb(PyFF_Glyph *self, void *UNUSED(closure)) {
25 | + DBounds b;
26 | +
27 | + SplineCharFindBounds(self->sc,&b);
28 | +
29 | + PyFF_Glyph_set_lsb(self,PyLong_FromLong(0),NULL);
30 | + PyFF_Glyph_set_rsb(self,PyLong_FromLong(0),NULL);
31 | +
32 | +return( Py_BuildValue("d", self->sc->width - b.maxx ));
33 | +}
34 | +
35 | static PyObject *PyFF_Glyph_get_vwidth(PyFF_Glyph *self, void *UNUSED(closure)) {
36 |
37 | return( Py_BuildValue("i", self->sc->vwidth ));
38 | diff --git a/fontforge/svg.c b/fontforge/svg.c
39 | index d98414b08..7ecc85431 100644
40 | --- a/fontforge/svg.c
41 | +++ b/fontforge/svg.c
42 | @@ -2853,6 +2853,42 @@ static Entity *SVGParseSVG(xmlNodePtr svg,int em_size,int ascent,bool scale,Spli
43 | swidth = strtod((char *) end+1,&end);
44 | sheight = strtod((char *) end+1,&end);
45 | xmlFree(num);
46 | + fprintf(stderr, " wh %08f %08f\n", width, height);
47 | + fprintf(stderr, "swh %08d %08f %08f\n", em_size, swidth, sheight);
48 | + fprintf(stderr, " st %08f %08f %08f %08f\n", st.transform[0], st.transform[1], st.transform[2], st.transform[3]);
49 | +
50 | + st.transform[0] *= 6;
51 | + st.transform[3] *= 6;
52 | +
53 | + /*
54 | + int newwidth = 3000;
55 | + st.transform[1] = st.transform[2] = st.transform[5] = 0;
56 | + st.transform[4] = newwidth;
57 | + if (sc) {
58 | + FVTrans( (FontViewBase *)sc->parent->fv,sc,st.transform,NULL,fvt_dontmovewidth);
59 | + SCSynchronizeWidth(sc,newwidth,sc->width,NULL);
60 | + SCCharChangedUpdate(sc,ly_none);
61 | + }
62 | + */
63 | +
64 | + /*
65 | + if ( width>height ) {
66 | + if ( swidth!=0 && sheight!=0 ) {
67 | + st.transform[0] *= swidth/sheight;
68 | + st.transform[3] *= swidth/sheight;
69 | + }
70 | + } else {
71 | + if ( swidth!=0 && sheight!=0 ) {
72 | + st.transform[0] *= sheight/swidth;
73 | + st.transform[3] *= sheight/swidth;
74 | + }
75 | + }
76 | + */
77 | + /*
78 | + if ( scale && swidth!=0 && sheight!=0) {
79 | + //st.transform[0] *= em_size * 6;
80 | + //st.transform[3] *= em_size * 6;
81 | + }
82 | if ( width>height ) {
83 | if ( scale && swidth!=0 ) {
84 | st.transform[0] *= em_size/swidth;
85 | @@ -2864,6 +2900,7 @@ static Entity *SVGParseSVG(xmlNodePtr svg,int em_size,int ascent,bool scale,Spli
86 | st.transform[3] *= em_size/sheight;
87 | }
88 | }
89 | + */
90 | }
91 | return( _SVGParseSVG(svg,svg,&st));
92 | }
93 | @@ -2877,8 +2914,12 @@ static void SVGParseGlyphBody(SplineChar *sc, xmlNodePtr glyph,
94 | sc->layers[ly_fore].splines = SVGParseExtendedPath(glyph,glyph);
95 | xmlFree(path);
96 | } else {
97 | - Entity *ent = SVGParseSVG(glyph,sc->parent->ascent+sc->parent->descent,
98 | - sc->parent->ascent,ip->scale,ip->dimensions ? sc : NULL,false);
99 | + Entity *ent = SVGParseSVG(glyph,
100 | + sc->parent->ascent+sc->parent->descent,
101 | + sc->parent->ascent,
102 | + ip->scale,
103 | + ip->dimensions ? sc : NULL,
104 | + false);
105 | sc->layer_cnt = 1;
106 | SCAppendEntityLayers(sc,ent,ip);
107 | if ( sc->layer_cnt==1 ) ++sc->layer_cnt;
108 |
--------------------------------------------------------------------------------
/LICENSE.OFL.txt:
--------------------------------------------------------------------------------
1 | Copyright 2022 The Noto Project Authors (https://github.com/notofonts/latin-greek-cyrillic)
2 |
3 | This Font Software is licensed under the SIL Open Font License, Version 1.1.
4 | This license is copied below, and is also available with a FAQ at:
5 | https://scripts.sil.org/OFL
6 |
7 |
8 | -----------------------------------------------------------
9 | SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10 | -----------------------------------------------------------
11 |
12 | PREAMBLE
13 | The goals of the Open Font License (OFL) are to stimulate worldwide
14 | development of collaborative font projects, to support the font creation
15 | efforts of academic and linguistic communities, and to provide a free and
16 | open framework in which fonts may be shared and improved in partnership
17 | with others.
18 |
19 | The OFL allows the licensed fonts to be used, studied, modified and
20 | redistributed freely as long as they are not sold by themselves. The
21 | fonts, including any derivative works, can be bundled, embedded,
22 | redistributed and/or sold with any software provided that any reserved
23 | names are not used by derivative works. The fonts and derivatives,
24 | however, cannot be released under any other type of license. The
25 | requirement for fonts to remain under this license does not apply
26 | to any document created using the fonts or their derivatives.
27 |
28 | DEFINITIONS
29 | "Font Software" refers to the set of files released by the Copyright
30 | Holder(s) under this license and clearly marked as such. This may
31 | include source files, build scripts and documentation.
32 |
33 | "Reserved Font Name" refers to any names specified as such after the
34 | copyright statement(s).
35 |
36 | "Original Version" refers to the collection of Font Software components as
37 | distributed by the Copyright Holder(s).
38 |
39 | "Modified Version" refers to any derivative made by adding to, deleting,
40 | or substituting -- in part or in whole -- any of the components of the
41 | Original Version, by changing formats or by porting the Font Software to a
42 | new environment.
43 |
44 | "Author" refers to any designer, engineer, programmer, technical
45 | writer or other person who contributed to the Font Software.
46 |
47 | PERMISSION & CONDITIONS
48 | Permission is hereby granted, free of charge, to any person obtaining
49 | a copy of the Font Software, to use, study, copy, merge, embed, modify,
50 | redistribute, and sell modified and unmodified copies of the Font
51 | Software, subject to the following conditions:
52 |
53 | 1) Neither the Font Software nor any of its individual components,
54 | in Original or Modified Versions, may be sold by itself.
55 |
56 | 2) Original or Modified Versions of the Font Software may be bundled,
57 | redistributed and/or sold with any software, provided that each copy
58 | contains the above copyright notice and this license. These can be
59 | included either as stand-alone text files, human-readable headers or
60 | in the appropriate machine-readable metadata fields within text or
61 | binary files as long as those fields can be easily viewed by the user.
62 |
63 | 3) No Modified Version of the Font Software may use the Reserved Font
64 | Name(s) unless explicit written permission is granted by the corresponding
65 | Copyright Holder. This restriction only applies to the primary font name as
66 | presented to the users.
67 |
68 | 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69 | Software shall not be used to promote, endorse or advertise any
70 | Modified Version, except to acknowledge the contribution(s) of the
71 | Copyright Holder(s) and the Author(s) or with their explicit written
72 | permission.
73 |
74 | 5) The Font Software, modified or unmodified, in part or in whole,
75 | must be distributed entirely under this license, and must not be
76 | distributed under any other license. The requirement for fonts to
77 | remain under this license does not apply to any document created
78 | using the Font Software.
79 |
80 | TERMINATION
81 | This license becomes null and void if any of the above conditions are
82 | not met.
83 |
84 | DISCLAIMER
85 | THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88 | OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89 | COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90 | INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91 | DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92 | FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93 | OTHER DEALINGS IN THE FONT SOFTWARE.
94 |
--------------------------------------------------------------------------------
/LICENSE.LGPL3.txt:
--------------------------------------------------------------------------------
1 | GNU LESSER GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 |
9 | This version of the GNU Lesser General Public License incorporates
10 | the terms and conditions of version 3 of the GNU General Public
11 | License, supplemented by the additional permissions listed below.
12 |
13 | 0. Additional Definitions.
14 |
15 | As used herein, "this License" refers to version 3 of the GNU Lesser
16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU
17 | General Public License.
18 |
19 | "The Library" refers to a covered work governed by this License,
20 | other than an Application or a Combined Work as defined below.
21 |
22 | An "Application" is any work that makes use of an interface provided
23 | by the Library, but which is not otherwise based on the Library.
24 | Defining a subclass of a class defined by the Library is deemed a mode
25 | of using an interface provided by the Library.
26 |
27 | A "Combined Work" is a work produced by combining or linking an
28 | Application with the Library. The particular version of the Library
29 | with which the Combined Work was made is also called the "Linked
30 | Version".
31 |
32 | The "Minimal Corresponding Source" for a Combined Work means the
33 | Corresponding Source for the Combined Work, excluding any source code
34 | for portions of the Combined Work that, considered in isolation, are
35 | based on the Application, and not on the Linked Version.
36 |
37 | The "Corresponding Application Code" for a Combined Work means the
38 | object code and/or source code for the Application, including any data
39 | and utility programs needed for reproducing the Combined Work from the
40 | Application, but excluding the System Libraries of the Combined Work.
41 |
42 | 1. Exception to Section 3 of the GNU GPL.
43 |
44 | You may convey a covered work under sections 3 and 4 of this License
45 | without being bound by section 3 of the GNU GPL.
46 |
47 | 2. Conveying Modified Versions.
48 |
49 | If you modify a copy of the Library, and, in your modifications, a
50 | facility refers to a function or data to be supplied by an Application
51 | that uses the facility (other than as an argument passed when the
52 | facility is invoked), then you may convey a copy of the modified
53 | version:
54 |
55 | a) under this License, provided that you make a good faith effort to
56 | ensure that, in the event an Application does not supply the
57 | function or data, the facility still operates, and performs
58 | whatever part of its purpose remains meaningful, or
59 |
60 | b) under the GNU GPL, with none of the additional permissions of
61 | this License applicable to that copy.
62 |
63 | 3. Object Code Incorporating Material from Library Header Files.
64 |
65 | The object code form of an Application may incorporate material from
66 | a header file that is part of the Library. You may convey such object
67 | code under terms of your choice, provided that, if the incorporated
68 | material is not limited to numerical parameters, data structure
69 | layouts and accessors, or small macros, inline functions and templates
70 | (ten or fewer lines in length), you do both of the following:
71 |
72 | a) Give prominent notice with each copy of the object code that the
73 | Library is used in it and that the Library and its use are
74 | covered by this License.
75 |
76 | b) Accompany the object code with a copy of the GNU GPL and this license
77 | document.
78 |
79 | 4. Combined Works.
80 |
81 | You may convey a Combined Work under terms of your choice that,
82 | taken together, effectively do not restrict modification of the
83 | portions of the Library contained in the Combined Work and reverse
84 | engineering for debugging such modifications, if you also do each of
85 | the following:
86 |
87 | a) Give prominent notice with each copy of the Combined Work that
88 | the Library is used in it and that the Library and its use are
89 | covered by this License.
90 |
91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license
92 | document.
93 |
94 | c) For a Combined Work that displays copyright notices during
95 | execution, include the copyright notice for the Library among
96 | these notices, as well as a reference directing the user to the
97 | copies of the GNU GPL and this license document.
98 |
99 | d) Do one of the following:
100 |
101 | 0) Convey the Minimal Corresponding Source under the terms of this
102 | License, and the Corresponding Application Code in a form
103 | suitable for, and under terms that permit, the user to
104 | recombine or relink the Application with a modified version of
105 | the Linked Version to produce a modified Combined Work, in the
106 | manner specified by section 6 of the GNU GPL for conveying
107 | Corresponding Source.
108 |
109 | 1) Use a suitable shared library mechanism for linking with the
110 | Library. A suitable mechanism is one that (a) uses at run time
111 | a copy of the Library already present on the user's computer
112 | system, and (b) will operate properly with a modified version
113 | of the Library that is interface-compatible with the Linked
114 | Version.
115 |
116 | e) Provide Installation Information, but only if you would otherwise
117 | be required to provide such information under section 6 of the
118 | GNU GPL, and only to the extent that such information is
119 | necessary to install and execute a modified version of the
120 | Combined Work produced by recombining or relinking the
121 | Application with a modified version of the Linked Version. (If
122 | you use option 4d0, the Installation Information must accompany
123 | the Minimal Corresponding Source and Corresponding Application
124 | Code. If you use option 4d1, you must provide the Installation
125 | Information in the manner specified by section 6 of the GNU GPL
126 | for conveying Corresponding Source.)
127 |
128 | 5. Combined Libraries.
129 |
130 | You may place library facilities that are a work based on the
131 | Library side by side in a single library together with other library
132 | facilities that are not Applications and are not covered by this
133 | License, and convey such a combined library under terms of your
134 | choice, if you do both of the following:
135 |
136 | a) Accompany the combined library with a copy of the same work based
137 | on the Library, uncombined with any other library facilities,
138 | conveyed under the terms of this License.
139 |
140 | b) Give prominent notice with the combined library that part of it
141 | is a work based on the Library, and explaining where to find the
142 | accompanying uncombined form of the same work.
143 |
144 | 6. Revised Versions of the GNU Lesser General Public License.
145 |
146 | The Free Software Foundation may publish revised and/or new versions
147 | of the GNU Lesser General Public License from time to time. Such new
148 | versions will be similar in spirit to the present version, but may
149 | differ in detail to address new problems or concerns.
150 |
151 | Each version is given a distinguishing version number. If the
152 | Library as you received it specifies that a certain numbered version
153 | of the GNU Lesser General Public License "or any later version"
154 | applies to it, you have the option of following the terms and
155 | conditions either of that published version or of any later version
156 | published by the Free Software Foundation. If the Library as you
157 | received it does not specify a version number of the GNU Lesser
158 | General Public License, you may choose any version of the GNU Lesser
159 | General Public License ever published by the Free Software Foundation.
160 |
161 | If the Library as you received it specifies that a proxy can decide
162 | whether future versions of the GNU Lesser General Public License shall
163 | apply, that proxy's public statement of acceptance of any version is
164 | permanent authorization for you to choose that version for the
165 | Library.
166 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Z80 Sans
2 |
3 | What's your favourite disassembler? Mine's a font:
4 |
5 | https://github.com/user-attachments/assets/bb6ceb18-c2fd-40a9-be4f-202321a214d9
6 |
7 | This font converts sequences of hexadecimal lowercase characters into disassembled Z80 instructions, by making extensive use of OpenType's [Glyph Substitution Table (GSUB)](https://learn.microsoft.com/en-us/typography/opentype/spec/gsub) and [Glyph Positioning Table (GPOS)](https://learn.microsoft.com/en-us/typography/opentype/spec/gpos).
8 |
9 | If you just want to try it out, a copy is available under `./test/z80-sans.ttf`.
10 |
11 | # Install
12 |
13 | Tested on Debian GNU/Linux 12. Note that this Debian version ships with ruby version 3, while fontcustom was written for ruby version 2, and is incompatible with later versions (e.g. syntax errors). A ruby install also requires a compatible OpenSSL version. Therefore, RVM can be used to manage both ruby and a local install of OpenSSL.
14 |
15 | ```sh
16 | apt install imagemagick potrace
17 | pip install fonttools
18 |
19 | git submodule update --init --recursive
20 |
21 | # fontforge
22 | (
23 | cd ./modules/fontforge/
24 | git checkout 4f4907d9541857b135bd0b361099e778325b4e28
25 | git apply ../../resources/fontforge.diff
26 | mkdir -p build
27 | cd build
28 | cmake -GNinja ..
29 | ninja
30 | ninja install
31 | )
32 |
33 | # woff2
34 | (
35 | cd ./modules/woff2/
36 | make clean all
37 | )
38 |
39 | # fontcustom
40 | rvm use 2.7
41 | rvm pkg install openssl
42 | rvm install 2.4 --with-openssl-dir=$HOME/.rvm/usr
43 | gem update --system 3.3.22
44 | (
45 | export PATH=$PWD/modules/woff2/build:$PATH
46 | cd ./modules/fontcustom/
47 | git apply ../../resources/fontcustom.diff
48 | gem build fontcustom.gemspec
49 | gem install ./fontcustom-2.0.0.gem
50 | )
51 | ```
52 |
53 | # Running
54 |
55 | ```sh
56 | cp ./resources/droid-sans-mono.ttf /tmp/base.ttf
57 | ./gen.py ./resources/instructions.json
58 | ```
59 |
60 | The .ttf font file is copied to `~/.local/share/fonts/`, which is used by e.g. LibreOffice.
61 |
62 | # Design
63 |
64 | Compared to other cursed fonts, Z80 Sans has these challenges:
65 |
66 | * **Multiple characters to render**: it would be impractical to manually define character by character all substitution rules for rendering, so we can create glyphs that combine multiple literals (e.g. mnemonics like `CALL`), however this also ties to the next point...
67 | * **Multiple combinations**: recall that some Z80 instructions can take 16-bit addresses and registers as operands, which means that a single instruction can have up to `65536 * 7 = 458752` possible combinations;
68 | * **Out-of-order operands**: e.g. register and offsets can be encoded into hexadecimal bytes in one order, but disassembled in another order, which complicates backtracking/lookaheads rules;
69 | * **Little-endian addresses**: Characters for the least-significant byte need to be rendered before the most-significant byte;
70 | * **Signed offsets**: All offsets in range `0x80..0xff` need to be rendered as a negative two's-complement number;
71 |
72 | All of this invites a programmatic solution. While fontcustom and ImageMagick take care of generating glyphs, it seems that a convenient way to write lookup rules is the .fea format, but I didn't find a way to integrate it with fonttools' .ttx format (which is basically xml). I took the lowest common denominator approach of directly editing the .ttx of Noto Sans Mono (although glyph shapes are computed from Droid Sans Mono, as that's what I started with when patching FontForge).
73 |
74 | A recursive descent parser is used to generate all possible glyphs, which helps with evaluating expressions in encodings (e.g. `SET b,(IX+o)` takes a bit and a displacement, encoded as expression `DD CB o C6+8*b`). These encodings were then expanded to all possible values that operands can take, before finally associating 1 or more hexadecimal bytes to each disassembly glyph required to render an expanded instruction.
75 |
76 | There are some nice references for OpenType features, but they are written at a high-level, or in .fea(?) format:
77 |
78 | * [OpenType Feature File Specification \| afdko](http://adobe-type-tools.github.io/afdko/OpenTypeFeatureFileSpecification.html)
79 | * [GSUB — Glyph Substitution Table \(OpenType 1\.9\.1\) \- Typography \| Microsoft Learn](https://learn.microsoft.com/en-us/typography/opentype/spec/gsub)
80 | * [Fonts and Layout for Global Scripts](https://simoncozens.github.io/fonts-and-layout/)
81 | * [GitHub \- brew/opentype\-feature\-intro: An introduction to OpenType features for type designers\.](https://github.com/brew/opentype-feature-intro)
82 | * [Features, part 3: advanced contextual alternates \| Glyphs](https://glyphsapp.com/learn/features-part-3-advanced-contextual-alternates)
83 | * [Opentype subtitution many by many \(different number\) \- Glyphs Forum](https://forum.glyphsapp.com/t/opentype-subtitution-many-by-many-different-number/13126)
84 |
85 | It's never very clear how to translate them to .ttx, so in the end I just converted all of the Noto Sans family and used the good ol' fashioned bruteforce approach of "learning by example". This is even more fun that it sounds, thanks to plenty of silent failures when converting from .ttx to .ttf, where lookups will not match due to some assumptions not validated by fonttools (e.g. class definitions for contextual chaining substitutions must have at least one coverage glyph with class value="1").
86 |
87 | Pretty much most challenges were solved with contextual chaining rules. To handle addresses, each nibble in range `0..f` was encoded with distinct glyphs, with spacing characters used to create multiple substitutions, one character at a time. Displacements also have additional signed variants. This gives us a total of `(4 + 2) * 16` glyphs for numbers. This was already enough to keep the font file under the 65536 glyphs limit.
88 |
89 | The worst part was of course out-of-order operands. However, due to the limited number of variations these have in instructions, they could be covered by the same strategy as instructions with ambiguously encoded prefixes, e.g.
90 | ```
91 | ["SET b,(IX+o)", "DD CB o C6+8*b"],
92 | ["SET b,(IY+o)", "FD CB o C6+8*b"],
93 | ```
94 |
95 | Is covered by the same lookup rules as:
96 | ```
97 | ["SRA (IX+o)", "DD CB o 2E"],
98 | ["SRA (IY+o)", "FD CB o 2E"],
99 | ["SRL (IX+o)", "DD CB o 3E"],
100 | ["SRL (IY+o)", "FD CB o 3E"],
101 | ```
102 |
103 | An interesting property in the Z80 ISA is that bits and registers have up to 8 variations, and these out-of-order cases only involve offsets and one of those specific operands. Therefore, we can encode bits or registers as literals. With sufficient lookaheads, we can match up to the last hexadecimal byte, and create dedicated lookups for each case. The last literals can be reduced by generating a ligature that matches the suffix glyph. The end result was dozens more generated lookups for these cases (which can likely be grouped to reduce this number).
104 |
105 | # Known Issues
106 |
107 | * While all of the original instruction set should be disassembled, some instructions have minor glitches:
108 | * `LD (IX+o),r` is rendered as `LD (IX+o r),`;
109 | * `SET b,(IX+o)` is rendered as `SET b,(IX+o))`;
110 | * "CTF quality" code 😅;
111 |
112 | # Future Work
113 |
114 | FontForge supports scriptable modification of features using commands [GenerateFeatureFile()](https://fontforge.org/docs/scripting/scripting-alpha.html#GenerateFeatureFile) and [MergeFeature()](https://fontforge.org/docs/scripting/scripting-alpha.html#MergeFeature) (briefly covered in [The Terrible Secret of OpenType Glyph Substitution \- Ansuz \- mskala's home page](https://ansuz.sooke.bc.ca/entry/131)). I was only aware of this after making the .ttx based implementation, but it could potentially have avoided messing with .ttx files.
115 |
116 | For more complex instruction sets, an alternative approach that seems to have less constraints is to use font shapers. Some examples:
117 | * [fuglede/llama\.ttf: A font for writing tiny stories](https://github.com/fuglede/llama.ttf);
118 | * [hsfzxjy/handwriter\.ttf: Handwriting synthesis with Harfbuzz WASM\.](https://github.com/hsfzxjy/handwriter.ttf);
119 |
120 | # Credits
121 |
122 | * [Droid Sans Mono](https://github.com/google/fonts/tree/7503f3c66297f9ec08aecf04edf355247da70ab8/apache/droidsansmono) and [Noto Sans Mono](https://github.com/google/fonts/tree/d917462c0d0f44b2e205aeb769790a175b3e752f/ofl/notosansmono) were used as base for Z80 Sans;
123 | * `./resources/instructions.json` was adapted from [maziac/z80\-instruction\-set](https://github.com/maziac/z80-instruction-set/blob/3b6bfaeedebd68cc590348c0231b48a4d44edfe5/src/z80InstructionSetRawData.ts);
124 | * Inspiration for GSUB substitutions:
125 | * [Font with Built-In Syntax Highlighting](https://blog.glyphdrawing.club/font-with-built-in-syntax-highlighting/);
126 | * [Fontemon](https://www.coderelay.io/fontemon.html), in particular ["How I did it"](https://github.com/mmulet/code-relay/blob/main/markdown/HowIDidIt.md);
127 | * [Addition Font](https://litherum.blogspot.com/2019/03/addition-font.html);
128 | * [Sans Bullshit Sans](https://pixelambacht.nl/2015/sans-bullshit-sans/);
129 |
130 | # License
131 |
132 | * Droid Sans Mono is under [Apache Licence](./LICENSE.Apache.txt);
133 | * Noto Sans Mono is under [Open Font License](./LICENSE.OFL.txt);
134 | * `./resources/instructions.json` is under [GNU Lesser General Public License version 3](./LICENSE.LGPL3.txt);
135 | * Other files are under [MIT License](./LICENSE.txt);
136 |
--------------------------------------------------------------------------------
/resources/instructions.json:
--------------------------------------------------------------------------------
1 | [
2 | ["ADC A,(HL)", "8E"],
3 | ["ADC A,(IX+o)", "DD 8E o"],
4 | ["ADC A,(IY+o)", "FD 8E o"],
5 | ["ADC A,IXh", "DD 8C"],
6 | ["ADC A,IXl", "DD 8D"],
7 | ["ADC A,IYh", "FD 8C"],
8 | ["ADC A,IYl", "FD 8D"],
9 | ["ADC A,n", "CE n"],
10 | ["ADC A,r", "88+r"],
11 | ["ADC HL,BC", "ED 4A"],
12 | ["ADC HL,DE", "ED 5A"],
13 | ["ADC HL,HL", "ED 6A"],
14 | ["ADC HL,SP", "ED 7A"],
15 | ["ADD A,(HL)", "86"],
16 | ["ADD A,(IX+o)", "DD 86 o"],
17 | ["ADD A,(IY+o)", "FD 86 o"],
18 | ["ADD A,IXh", "DD 84"],
19 | ["ADD A,IXl", "DD 85"],
20 | ["ADD A,IYh", "FD 84"],
21 | ["ADD A,IYl", "FD 85"],
22 | ["ADD A,n", "C6 n"],
23 | ["ADD A,r", "80+r"],
24 | ["ADD HL,BC", "9"],
25 | ["ADD HL,DE", "19"],
26 | ["ADD HL,HL", "29"],
27 | ["ADD HL,SP", "39"],
28 | ["ADD IX,BC", "DD 09"],
29 | ["ADD IX,DE", "DD 19"],
30 | ["ADD IX,IX", "DD 29"],
31 | ["ADD IX,SP", "DD 39"],
32 | ["ADD IY,BC", "FD 09"],
33 | ["ADD IY,DE", "FD 19"],
34 | ["ADD IY,IY", "FD 29"],
35 | ["ADD IY,SP", "FD 39"],
36 | ["AND (HL)", "A6"],
37 | ["AND (IX+o)", "DD A6 o"],
38 | ["AND (IY+o)", "FD A6 o"],
39 | ["AND IXh", "DD A4"],
40 | ["AND IXl", "DD A5"],
41 | ["AND IYh", "FD A4"],
42 | ["AND IYl", "FD A5"],
43 | ["AND n", "E6 n"],
44 | ["AND r", "A0+r"],
45 | ["BIT b,(HL)", "CB 46+8*b"],
46 | ["BIT b,(IX+o)", "DD CB o 46+8*b"],
47 | ["BIT b,(IY+o)", "FD CB o 46+8*b"],
48 | ["BIT b,r", "CB 40+8*b+r"],
49 | ["CALL C,nn", "DC nn nn"],
50 | ["CALL M,nn", "FC nn nn"],
51 | ["CALL NC,nn", "D4 nn nn"],
52 | ["CALL nn", "CD nn nn"],
53 | ["CALL NZ,nn", "C4 nn nn"],
54 | ["CALL P,nn", "F4 nn nn"],
55 | ["CALL PE,nn", "EC nn nn"],
56 | ["CALL PO,nn", "E4 nn nn"],
57 | ["CALL Z,nn", "CC nn nn"],
58 | ["CCF", "3F"],
59 | ["CP (HL)", "BE"],
60 | ["CP (IX+o)", "DD BE o"],
61 | ["CP (IY+o)", "FD BE o"],
62 | ["CP IXh", "DD BC"],
63 | ["CP IXl", "DD BD"],
64 | ["CP IYh", "FD BC"],
65 | ["CP IYl", "FD BD"],
66 | ["CP n", "FE n"],
67 | ["CP r", "B8+r"],
68 | ["CPD", "ED A9"],
69 | ["CPDR", "ED B9"],
70 | ["CPI", "ED A1"],
71 | ["CPIR", "ED B1"],
72 | ["CPL", "2F"],
73 | ["DAA", "27"],
74 | ["DEC (HL)", "35"],
75 | ["DEC (IX+o)", "DD 35 o"],
76 | ["DEC (IY+o)", "FD 35 o"],
77 | ["DEC A", "3D"],
78 | ["DEC B", "5"],
79 | [ "DEC BC", "0B"],
80 | ["DEC C", "0D"],
81 | ["DEC D", "15"],
82 | ["DEC DE", "1B"],
83 | ["DEC E", "1D"],
84 | ["DEC H", "25"],
85 | ["DEC HL", "2B"],
86 | ["DEC IX", "DD 2B"],
87 | ["DEC IXh", "DD 25"],
88 | ["DEC IXl", "DD 2D"],
89 | ["DEC IY", "FD 2B"],
90 | ["DEC IYh", "FD 25"],
91 | ["DEC IYl", "FD 2D"],
92 | ["DEC L", "2D"],
93 | ["DEC SP", "3B"],
94 | [ "DI", "F3"],
95 | ["DJNZ o", "10 o"],
96 | ["EI", "FB"],
97 | ["EX (SP),HL", "E3"],
98 | ["EX (SP),IX", "DD E3"],
99 | ["EX (SP),IY", "FD E3"],
100 | ["EX AF,AF'", "8"],
101 | ["EX DE,HL", "EB"],
102 | ["EXX", "D9"],
103 | ["HALT", "76"],
104 | [ "IM 0", "ED 46"],
105 | ["IM 1", "ED 56"],
106 | ["IM 2", "ED 5E"],
107 | [ "IN (C)", "ED 70"],
108 | ["IN A,(C)", "ED 78"],
109 | ["IN A,(n)", "DB n"],
110 | ["IN B,(C)", "ED 40"],
111 | ["IN C,(C)", "ED 48"],
112 | ["IN D,(C)", "ED 50"],
113 | ["IN E,(C)", "ED 58"],
114 | ["IN F,(C)", "ED 70"],
115 | ["IN H,(C)", "ED 60"],
116 | ["IN L,(C)", "ED 68"],
117 | ["INC (HL)", "34"],
118 | ["INC (IX+o)", "DD 34 o"],
119 | ["INC (IY+o)", "FD 34 o"],
120 | ["INC A", "3C"],
121 | ["INC B", "4"],
122 | ["INC BC", "3"],
123 | ["INC C", "0C"],
124 | ["INC D", "14"],
125 | ["INC DE", "13"],
126 | ["INC E", "1C"],
127 | ["INC H", "24"],
128 | ["INC HL", "23"],
129 | ["INC IX", "DD 23"],
130 | ["INC IXh", "DD 24"],
131 | ["INC IXl", "DD 2C"],
132 | ["INC IY", "FD 23"],
133 | ["INC IYh", "FD 24"],
134 | ["INC IYl", "FD 2C"],
135 | ["INC L", "2C"],
136 | ["INC SP", "33"],
137 | ["IND", "ED AA"],
138 | ["INDR", "ED BA"],
139 | ["INI", "ED A2"],
140 | ["INIR", "ED B2"],
141 | [ "JP (HL)", "E9"],
142 | ["JP (IX)", "DD E9"],
143 | ["JP (IY)", "FD E9"],
144 | ["JP C,nn", "DA nn nn"],
145 | ["JP HL", "E9"],
146 | ["JP M,nn", "FA nn nn"],
147 | ["JP NC,nn", "D2 nn nn"],
148 | ["JP nn", "C3 nn nn"],
149 | ["JP NZ,nn", "C2 nn nn"],
150 | ["JP P,nn", "F2 nn nn"],
151 | ["JP PE,nn", "EA nn nn"],
152 | ["JP PO,nn", "E2 nn nn"],
153 | ["JP Z,nn", "CA nn nn"],
154 | ["JR C,o", "38 o"],
155 | ["JR NC,o", "30 o"],
156 | ["JR NZ,o", "20 o"],
157 | ["JR o", "18 o"],
158 | ["JR Z,o", "28 o"],
159 | [ "LD (BC),A", "2"],
160 | [ "LD (DE),A", "12"],
161 | [ "LD (HL),n", "36 n"],
162 | [ "LD (HL),r", "70+r"],
163 | [ "LD (IX+o),n", "DD 36 o n"],
164 | [ "LD (IX+o),r", "DD 70+r o"],
165 | [ "LD (IY+o),n", "FD 36 o n"],
166 | [ "LD (IY+o),r", "FD 70+r o"],
167 | [ "LD (nn),A", "32 nn nn"],
168 | [ "LD (nn),BC", "ED 43 nn nn"],
169 | [ "LD (nn),DE", "ED 53 nn nn"],
170 | [ "LD (nn),HL", "22 nn nn"],
171 | [ "LD (nn),IX", "DD 22 nn nn"],
172 | [ "LD (nn),IY", "FD 22 nn nn"],
173 | [ "LD (nn),SP", "ED 73 nn nn"],
174 | [ "LD A,(BC)", "0A"],
175 | [ "LD A,(DE)", "1A"],
176 | [ "LD A,(HL)", "7E"],
177 | [ "LD A,(IX+o)", "DD 7E o"],
178 | [ "LD A,(IY+o)", "FD 7E o"],
179 | [ "LD A,(nn)", "3A nn nn"],
180 | ["LD A,I", "ED 57"],
181 | ["LD A,IXh", "DD 7C"],
182 | ["LD A,IXl", "DD 7D"],
183 | ["LD A,IYh", "FD 7C"],
184 | ["LD A,IYl", "FD 7D"],
185 | [ "LD A,n", "3E n"],
186 | [ "LD A,r", "78+r"],
187 | [ "LD A,R", "ED 5F"],
188 | [ "LD B,(HL)", "46"],
189 | [ "LD B,(IX+o)", "DD 46 o"],
190 | [ "LD B,(IY+o)", "FD 46 o"],
191 | ["LD B,IXh", "DD 44"],
192 | ["LD B,IXl", "DD 45"],
193 | ["LD B,IYh", "FD 44"],
194 | ["LD B,IYl", "FD 45"],
195 | [ "LD B,n", "06 n"],
196 | [ "LD B,r", "40+r"],
197 | [ "LD BC,(nn)", "ED 4B nn nn"],
198 | [ "LD BC,nn", "01 nn nn"],
199 | [ "LD C,(HL)", "4E"],
200 | [ "LD C,(IX+o)", "DD 4E o"],
201 | [ "LD C,(IY+o)", "FD 4E o"],
202 | ["LD C,IXh", "DD 4C"],
203 | ["LD C,IXl", "DD 4D"],
204 | ["LD C,IYh", "FD 4C"],
205 | ["LD C,IYl", "FD 4D"],
206 | [ "LD C,n", "0E n"],
207 | [ "LD C,r", "48+r"],
208 | [ "LD D,(HL)", "56"],
209 | [ "LD D,(IX+o)", "DD 56 o"],
210 | [ "LD D,(IY+o)", "FD 56 o"],
211 | ["LD D,IXh", "DD 54"],
212 | ["LD D,IXl", "DD 55"],
213 | ["LD D,IYh", "FD 54"],
214 | ["LD D,IYl", "FD 55"],
215 | [ "LD D,n", "16 n"],
216 | [ "LD D,r", "50+r"],
217 | [ "LD DE,(nn)", "ED 5B nn nn"],
218 | [ "LD DE,nn", "11 nn nn"],
219 | [ "LD E,(HL)", "5E"],
220 | [ "LD E,(IX+o)", "DD 5E o"],
221 | [ "LD E,(IY+o)", "FD 5E o"],
222 | ["LD E,IXh", "DD 5c"],
223 | ["LD E,IXl", "DD 5d"],
224 | ["LD E,IYh", "FD 5c"],
225 | ["LD E,IYl", "FD 5d"],
226 | [ "LD E,n", "1E n"],
227 | [ "LD E,r", "58+r"],
228 | [ "LD H,(HL)", "66"],
229 | [ "LD H,(IX+o)", "DD 66 o"],
230 | [ "LD H,(IY+o)", "FD 66 o"],
231 | [ "LD H,n", "26 n"],
232 | [ "LD H,r", "60+r"],
233 | [ "LD HL,(nn)", "2A nn nn"],
234 | [ "LD HL,nn", "21 nn nn"],
235 | [ "LD I,A", "ED 47"],
236 | [ "LD IX,(nn)", "DD 2A nn nn"],
237 | [ "LD IX,nn", "DD 21 nn nn"],
238 | [ "LD IXh,n", "DD 26 n"],
239 | [ "LD IXh,p", "DD 60+p"],
240 | [ "LD IXl,n", "DD 2E n"],
241 | [ "LD IXl,p", "DD 68+p"],
242 | [ "LD IY,(nn)", "FD 2A nn nn"],
243 | [ "LD IY,nn", "FD 21 nn nn"],
244 | [ "LD IYh,n", "FD 26 n"],
245 | [ "LD IYh,q", "FD 60+q"],
246 | [ "LD IYl,n", "FD 2E n"],
247 | [ "LD IYl,q", "FD 68+q"],
248 | [ "LD L,(HL)", "6E"],
249 | [ "LD L,(IX+o)", "DD 6E o"],
250 | [ "LD L,(IY+o)", "FD 6E o"],
251 | [ "LD L,n", "2E n"],
252 | [ "LD L,r", "68+r"],
253 | [ "LD R,A", "ED 4F"],
254 | [ "LD SP,(nn)", "ED 7B nn nn"],
255 | [ "LD SP,HL", "F9"],
256 | [ "LD SP,IX", "DD F9"],
257 | [ "LD SP,IY", "FD F9"],
258 | [ "LD SP,nn", "31 nn nn"],
259 | ["LDD", "ED A8"],
260 | ["LDDR", "ED B8"],
261 | ["LDI", "ED A0"],
262 | ["LDIR", "ED B0"],
263 | ["NEG", "ED 44"],
264 | [ "NOP", "0"],
265 | ["OR (HL)", "B6"],
266 | ["OR (IX+o)", "DD B6 o"],
267 | ["OR (IY+o)", "FD B6 o"],
268 | ["OR IXh", "DD B4"],
269 | ["OR IXl", "DD B5"],
270 | ["OR IYh", "FD B4"],
271 | ["OR IYl", "FD B5"],
272 | ["OR n", "F6 n"],
273 | ["OR r", "B0+r"],
274 | ["OTDR", "ED BB"],
275 | ["OTIR", "ED B3"],
276 | [ "OUT (C),0", "ED 71"],
277 | ["OUT (C),A", "ED 79"],
278 | ["OUT (C),B", "ED 41"],
279 | ["OUT (C),C", "ED 49"],
280 | ["OUT (C),D", "ED 51"],
281 | ["OUT (C),E", "ED 59"],
282 | ["OUT (C),H", "ED 61"],
283 | ["OUT (C),L", "ED 69"],
284 | ["OUT (n),A", "D3 n"],
285 | ["OUTD", "ED AB"],
286 | ["OUTI", "ED A3"],
287 | ["POP AF", "F1"],
288 | ["POP BC", "C1"],
289 | ["POP DE", "D1"],
290 | ["POP HL", "E1"],
291 | ["POP IX", "DD E1"],
292 | ["POP IY", "FD E1"],
293 | ["PUSH AF", "F5"],
294 | ["PUSH BC", "C5"],
295 | ["PUSH DE", "D5"],
296 | ["PUSH HL", "E5"],
297 | ["PUSH IX", "DD E5"],
298 | ["PUSH IY", "FD E5"],
299 | ["RES b,(HL)", "CB 86+8*b"],
300 | ["RES b,(IX+o)", "DD CB o 86+8*b"],
301 | ["RES b,(IY+o)", "FD CB o 86+8*b"],
302 | ["RES b,r", "CB 80+8*b+r"],
303 | ["RET", "C9"],
304 | ["RET C", "D8"],
305 | ["RET M", "F8"],
306 | ["RET NC", "D0"],
307 | ["RET NZ", "C0"],
308 | ["RET P", "F0"],
309 | ["RET PE", "E8"],
310 | ["RET PO", "E0"],
311 | ["RET Z", "C8"],
312 | ["RETI", "ED 4D"],
313 | ["RETN", "ED 45"],
314 | ["RL (HL)", "CB 16"],
315 | ["RL (IX+o)", "DD CB o 16"],
316 | ["RL (IY+o)", "FD CB o 16"],
317 | ["RL r", "CB 10+r"],
318 | ["RLA", "17"],
319 | ["RLC (HL)", "CB 06"],
320 | ["RLC (IX+o)", "DD CB o 06"],
321 | ["RLC (IY+o)", "FD CB o 06"],
322 | ["RLC r", "CB 00+r"],
323 | ["RLCA", "07"],
324 | ["RLD", "ED 6F"],
325 | ["RR (HL)", "CB 1E"],
326 | ["RR (IX+o)", "DD CB o 1E"],
327 | ["RR (IY+o)", "FD CB o 1E"],
328 | ["RR r", "CB 18+r"],
329 | ["RRA", "1F"],
330 | ["RRC (HL)", "CB 0E"],
331 | ["RRC (IX+o)", "DD CB o 0E"],
332 | ["RRC (IY+o)", "FD CB o 0E"],
333 | ["RRC r", "CB 08+r"],
334 | ["RRCA", "0F"],
335 | ["RRD", "ED 67"],
336 | ["RST 0", "C7"],
337 | ["RST 8H", "CF"],
338 | ["RST 10H", "D7"],
339 | ["RST 18H", "DF"],
340 | ["RST 20H", "E7"],
341 | ["RST 28H", "EF"],
342 | ["RST 30H", "F7"],
343 | ["RST 38H", "FF"],
344 | ["SBC A,(HL)", "9E"],
345 | ["SBC A,(IX+o)", "DD 9E o"],
346 | ["SBC A,(IY+o)", "FD 9E o"],
347 | ["SBC A,IXh", "DD 9C"],
348 | ["SBC A,IXl", "DD 9D"],
349 | ["SBC A,IYh", "FD 9C"],
350 | ["SBC A,IYl", "FD 9D"],
351 | ["SBC A,n", "DE n"],
352 | ["SBC A,r", "98+r"],
353 | ["SBC HL,BC", "ED 42"],
354 | ["SBC HL,DE", "ED 52"],
355 | ["SBC HL,HL", "ED 62"],
356 | ["SBC HL,SP", "ED 72"],
357 | ["SCF", "37"],
358 | ["SET b,(HL)", "CB C6+8*b"],
359 | ["SET b,(IX+o)", "DD CB o C6+8*b"],
360 | ["SET b,(IY+o)", "FD CB o C6+8*b"],
361 | ["SET b,r", "CB C0+8*b+r"],
362 | ["SLA (HL)", "CB 26"],
363 | ["SLA (IX+o)", "DD CB o 26"],
364 | ["SLA (IY+o)", "FD CB o 26"],
365 | ["SLA r", "CB 20+r"],
366 | ["SLL (HL)", "CB 36"],
367 | ["SLL (IX+o)", "DD CB o 36"],
368 | ["SLL (IY+o)", "FD CB o 36"],
369 | ["SLL r", "CB 30+r"],
370 | ["SRA (HL)", "CB 2E"],
371 | ["SRA (IX+o)", "DD CB o 2E"],
372 | ["SRA (IY+o)", "FD CB o 2E"],
373 | ["SRA r", "CB 28+r"],
374 | ["SRL (HL)", "CB 3E"],
375 | ["SRL (IX+o)", "DD CB o 3E"],
376 | ["SRL (IY+o)", "FD CB o 3E"],
377 | ["SRL r", "CB 38+r"],
378 | ["SUB (HL)", "96"],
379 | ["SUB (IX+o)", "DD 96 o"],
380 | ["SUB (IY+o)", "FD 96 o"],
381 | ["SUB IXh", "DD 94"],
382 | ["SUB IXl", "DD 95"],
383 | ["SUB IYh", "FD 94"],
384 | ["SUB IYl", "FD 95"],
385 | ["SUB n", "D6 n"],
386 | ["SUB r", "90+r"],
387 | ["XOR (HL)", "AE"],
388 | ["XOR (IX+o)", "DD AE o"],
389 | ["XOR (IY+o)", "FD AE o"],
390 | ["XOR IXh", "DD AC"],
391 | ["XOR IXl", "DD AD"],
392 | ["XOR IYh", "FD AC"],
393 | ["XOR IYl", "FD AD"],
394 | ["XOR n", "EE n"],
395 | ["XOR r", "A8+r"]
396 | ]
397 |
--------------------------------------------------------------------------------
/LICENSE.Apache.txt:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright [yyyy] [name of copyright owner]
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
--------------------------------------------------------------------------------
/gen.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 |
3 | from bs4 import BeautifulSoup
4 | from bs4.formatter import XMLFormatter
5 | from collections import OrderedDict
6 | from dataclasses import dataclass, field
7 | from pathlib import Path
8 | from typing import Any, Dict, List
9 | import ipdb
10 | import json
11 | import logging
12 | import os
13 | import re
14 | import subprocess
15 | import sys
16 |
17 |
18 | logging.basicConfig(
19 | # Uncomment for trace logging
20 | # level=logging.DEBUG,
21 | level=logging.INFO,
22 | format="\033[35;1m[%(funcName)s():%(lineno)s]\033[1;0m %(message)s",
23 | )
24 |
25 |
26 | class UnsortedAttributes(XMLFormatter):
27 | def attributes(self, tag):
28 | for k, v in tag.attrs.items():
29 | yield k, v
30 |
31 |
32 | """
33 | Lexer
34 | """
35 |
36 |
37 | @dataclass
38 | class TokWhitespace:
39 | def __str__(self):
40 | return " "
41 |
42 |
43 | @dataclass
44 | class TokLiteral:
45 | data: str
46 |
47 |
48 | @dataclass
49 | class TokDelimiter:
50 | data: str
51 |
52 |
53 | @dataclass
54 | class TokHex:
55 | data: str
56 |
57 |
58 | @dataclass
59 | class TokDec:
60 | data: str
61 |
62 |
63 | @dataclass
64 | class TokAdd:
65 | def __str__(self):
66 | return "+"
67 |
68 |
69 | @dataclass
70 | class TokMul:
71 | def __str__(self):
72 | return "*"
73 |
74 |
75 | @dataclass
76 | class TokVar:
77 | data: str
78 |
79 |
80 | @dataclass
81 | class TokReg(TokVar):
82 | pass
83 |
84 |
85 | @dataclass
86 | class TokP(TokVar):
87 | pass
88 |
89 |
90 | @dataclass
91 | class TokQ(TokVar):
92 | pass
93 |
94 |
95 | @dataclass
96 | class TokS8(TokVar):
97 | pass
98 |
99 |
100 | @dataclass
101 | class TokU4(TokVar):
102 | pass
103 |
104 |
105 | @dataclass
106 | class TokU8(TokVar):
107 | pass
108 |
109 |
110 | @dataclass
111 | class TokU16(TokVar):
112 | pass
113 |
114 |
115 | asm_patterns = [
116 | (r"nn[ n]*", lambda s, t: TokU16(t)),
117 | (r"n", lambda s, t: TokU8(t)),
118 | (r"b", lambda s, t: TokU4(t)),
119 | (r"o", lambda s, t: TokS8(t)),
120 | (r"r", lambda s, t: TokReg(t)),
121 | (r"p", lambda s, t: TokP(t)),
122 | (r"q", lambda s, t: TokQ(t)),
123 | (r"[a-zA-Z0-9][a-zA-Z0-9]", lambda s, t: TokHex(t)),
124 | (r"^[0-9]$", lambda s, t: TokHex(t)),
125 | (r"[0-9]", lambda s, t: TokDec(t)),
126 | (r"[+]", lambda s, t: TokAdd()),
127 | (r"[*]", lambda s, t: TokMul()),
128 | (r"[" " \\n]+", lambda s, t: TokWhitespace()),
129 | ]
130 |
131 |
132 | disasm_patterns = [
133 | (r"nn[ n]*", lambda s, t: TokU16(t)),
134 | (r"n", lambda s, t: TokU8(t)),
135 | (r"b", lambda s, t: TokU4(t)),
136 | (r"o", lambda s, t: TokS8(t)),
137 | (r"r", lambda s, t: TokReg(t)),
138 | (r"p", lambda s, t: TokP(t)),
139 | (r"q", lambda s, t: TokQ(t)),
140 | (r"[a-zA-Z0-9][a-zA-Z0-9']*", lambda s, t: TokLiteral(t)),
141 | (r"[\+\*,() \\n]", lambda s, t: TokDelimiter(t)),
142 | ]
143 |
144 |
145 | def disasm_tokenize(disasm):
146 | acceptable = re.Scanner(disasm_patterns)
147 | matched, unk = acceptable.scan(disasm)
148 | if unk:
149 | raise RuntimeError(f'Unknown token: "{unk}".')
150 | return matched
151 |
152 |
153 | def asm_tokenize(asm):
154 | acceptable = re.Scanner(asm_patterns)
155 | matched, unk = acceptable.scan(asm)
156 | if unk:
157 | raise RuntimeError(f'Unknown token: "{unk}".')
158 | return matched
159 |
160 |
161 | """
162 | Parser
163 | """
164 |
165 |
166 | @dataclass
167 | class RuleCounter:
168 | n: int
169 | i: int = field(default=0)
170 | is_name_output: bool = field(default=False)
171 | is_reg: bool = field(default=False)
172 | is_signed: bool = field(default=False)
173 |
174 | def reset(self):
175 | self.i = 0
176 |
177 | def next(self):
178 | if self.i >= self.n:
179 | return None
180 | next_i = self.i
181 | self.i += 1
182 | return next_i
183 |
184 |
185 | @dataclass
186 | class RuleCounterOptions(RuleCounter):
187 | n: int = field(default=0)
188 | i: int = field(default=0)
189 | options: dict = field(default_factory=lambda: {})
190 |
191 | def next(self):
192 | while self.i not in self.options.keys():
193 | if self.i >= self.n:
194 | return None
195 | self.i += 1
196 | next_i = self.i
197 | self.i += 1
198 | return self.options[next_i] if self.is_name_output else next_i
199 |
200 |
201 | @dataclass
202 | class RuleCounterBit(RuleCounterOptions):
203 | n: int = field(default=8)
204 | i: int = field(default=0)
205 | is_reg: bool = field(default=True)
206 | options: dict = field(
207 | default_factory=lambda: {
208 | 0: "0",
209 | 1: "1",
210 | 2: "2",
211 | 3: "3",
212 | 4: "4",
213 | 5: "5",
214 | 6: "6",
215 | 7: "7",
216 | }
217 | )
218 |
219 |
220 | @dataclass
221 | class RuleCounterReg(RuleCounterOptions):
222 | n: int = field(default=8)
223 | i: int = field(default=0)
224 | is_reg: bool = field(default=True)
225 | options: dict = field(
226 | default_factory=lambda: {
227 | 0: "B",
228 | 1: "C",
229 | 2: "D",
230 | 3: "E",
231 | 4: "H",
232 | 5: "L",
233 | 7: "A",
234 | }
235 | )
236 |
237 |
238 | @dataclass
239 | class RuleCounterP(RuleCounterReg):
240 | options: dict = field(
241 | default_factory=lambda: {
242 | 0: "B",
243 | 1: "C",
244 | 2: "D",
245 | 3: "E",
246 | 4: "IXh",
247 | 5: "IXl",
248 | 7: "A",
249 | }
250 | )
251 |
252 |
253 | @dataclass
254 | class RuleCounterQ(RuleCounterReg):
255 | options: dict = field(
256 | default_factory=lambda: {
257 | 0: "B",
258 | 1: "C",
259 | 2: "D",
260 | 3: "E",
261 | 4: "IYh",
262 | 5: "IYl",
263 | 7: "A",
264 | }
265 | )
266 |
267 |
268 | @dataclass
269 | class RuleSplittable:
270 | pass
271 |
272 |
273 | @dataclass
274 | class RuleLiteral(RuleSplittable):
275 | data: str
276 | is_offset: bool = field(default=False)
277 | is_signed: bool = field(default=False)
278 | var_names: List = field(default_factory=list)
279 | split_i: int = field(default=0)
280 | seq_i: int = field(default=0)
281 |
282 | def eval(self, ctx=None):
283 | return self.data
284 |
285 | def __str__(self):
286 | return self.data
287 |
288 |
289 | @dataclass
290 | class RuleHex(RuleSplittable):
291 | data: int
292 | is_offset: bool = field(default=False)
293 | is_signed: bool = field(default=False)
294 | var_names: List = field(default_factory=list)
295 | split_i: int = field(default=0)
296 | seq_i: int = field(default=0)
297 |
298 | def eval(self, ctx=None):
299 | return self.data
300 |
301 | def glyph_str(self):
302 | return f"{self.data:02x}"
303 |
304 | def __str__(self):
305 | return self.glyph_str()
306 |
307 |
308 | @dataclass
309 | class RuleVarRef(RuleSplittable):
310 | name: str
311 | split_i: int = field(default=0)
312 | seq_i: int = field(default=0)
313 |
314 | def eval(self, ctx):
315 | return ctx[name].eval(ctx)
316 |
317 |
318 | @dataclass
319 | class RuleVar:
320 | name: str
321 | counter: RuleCounter
322 |
323 | def eval(self, ctx=None):
324 | return counter.next()
325 |
326 |
327 | @dataclass
328 | class RuleExpr:
329 | lhs: Any
330 | op: str
331 | rhs: Any
332 |
333 | def eval(self, arg, ctx):
334 | if isinstance(arg, (RuleExpr, RuleHex, RuleVar)):
335 | return arg.eval(ctx)
336 | return arg
337 |
338 | def eval(self, ctx):
339 | if isinstance(self.op, TokAdd):
340 | op_func = lambda x, y: x + y
341 | elif isinstance(self.op, TokMul):
342 | op_func = lambda x, y: x * y
343 | else:
344 | raise RuntimeError(f'Unknown expr op: "{self.op}".')
345 | return op_func(self.lhs.eval(ctx), self.rhs.eval(ctx))
346 |
347 |
348 | @dataclass
349 | class RuleEnd(RuleSplittable):
350 | split_i: int = field(default=99999)
351 | seq_i: int = field(default=99999)
352 |
353 |
354 | @dataclass
355 | class Parser:
356 | variables: Dict = field(default_factory=dict)
357 | rules: List = field(default_factory=list)
358 | tokens: List = field(default_factory=list)
359 | tok_i: int = field(default=0)
360 |
361 | def normalize_name(self, name):
362 | return name.replace("nn nn", "nn")
363 |
364 | def advance(self):
365 | if self.tok_i >= len(self.tokens):
366 | return None
367 |
368 | next_token = self.tokens[self.tok_i]
369 | self.tok_i += 1
370 | return next_token
371 |
372 | def peek(self, k=0):
373 | if (self.tok_i + k) >= len(self.tokens):
374 | return None
375 |
376 | return self.tokens[self.tok_i + k]
377 |
378 | def accept(self, expected):
379 | next_token = self.advance()
380 | if next_token is None:
381 | return None
382 |
383 | if isinstance(next_token, expected):
384 | return next_token
385 | else:
386 | raise RuntimeError(
387 | f'Unexpected token "{next_token}". Expected "{expected}".'
388 | )
389 |
390 | def parse(self):
391 | pass
392 |
393 |
394 | @dataclass
395 | class AsmParser(Parser):
396 | def rule_operand(self, resolved):
397 | k = resolved.data
398 | if isinstance(resolved, TokDec):
399 | return RuleHex(int(resolved.data, 10))
400 | elif isinstance(resolved, TokHex):
401 | return RuleHex(int(resolved.data, 16))
402 | elif isinstance(resolved, TokVar):
403 | tok_name = self.normalize_name(resolved.data)
404 | if isinstance(resolved, TokU16):
405 | counter = RuleCounter(256 * 256)
406 | elif isinstance(resolved, TokU8):
407 | counter = RuleCounter(256)
408 | elif isinstance(resolved, TokS8):
409 | counter = RuleCounter(256, is_signed=True)
410 | elif isinstance(resolved, TokU4):
411 | counter = RuleCounterBit()
412 | elif isinstance(resolved, TokReg):
413 | counter = RuleCounterReg()
414 | elif isinstance(resolved, TokP):
415 | counter = RuleCounterP()
416 | elif isinstance(resolved, TokQ):
417 | counter = RuleCounterQ()
418 | else:
419 | raise RuntimeError(f'Unknown var tok: "{tok_name}".')
420 | self.variables[tok_name] = RuleVar(tok_name, counter)
421 | return RuleVarRef(tok_name)
422 | else:
423 | raise RuntimeError(f'Unknown tok: "{resolved}".')
424 | return k
425 |
426 | def rule_binop(self, lhs):
427 | next_type = (TokAdd, TokMul, type(None))
428 | op = self.accept(next_type)
429 | if isinstance(op, type(None)):
430 | return lhs
431 | elif isinstance(op, TokAdd):
432 | rhs = self.rule_operand(self.accept((TokDec, TokHex, TokVar)))
433 | next_op = self.peek()
434 | if isinstance(next_op, (TokAdd, TokMul)):
435 | return RuleExpr(lhs, op, self.rule_binop(rhs))
436 | return RuleExpr(lhs, op, rhs)
437 | elif isinstance(op, TokMul):
438 | rhs = self.rule_operand(self.accept((TokDec, TokHex, TokVar)))
439 | expr = RuleExpr(lhs, op, rhs)
440 | next_op = self.peek()
441 | next_lhs = self.peek(1)
442 | if isinstance(next_op, (TokAdd, TokMul)) and isinstance(
443 | next_lhs, (TokHex, TokVar)
444 | ):
445 | return self.rule_binop(expr)
446 | return expr
447 |
448 | def parse(self):
449 | next_type = (TokHex, TokVar)
450 | while self.peek():
451 | resolved = self.accept(next_type)
452 | if isinstance(resolved, (TokDec, TokHex)):
453 | k = self.rule_operand(resolved)
454 | if isinstance(self.peek(), (TokAdd, TokMul)):
455 | self.rules.append(self.rule_binop(k))
456 | else:
457 | self.rules.append(k)
458 |
459 | next_type = (TokWhitespace, type(None))
460 | elif isinstance(resolved, TokWhitespace):
461 | # Irrelevant
462 | next_type = (TokHex, TokVar, type(None))
463 | elif isinstance(resolved, TokVar):
464 | tok_name = self.normalize_name(resolved.data)
465 | if isinstance(resolved, TokU16):
466 | counter = RuleCounter(256 * 256)
467 | elif isinstance(resolved, TokU8):
468 | counter = RuleCounter(256)
469 | elif isinstance(resolved, TokS8):
470 | counter = RuleCounter(256, is_signed=True)
471 | elif isinstance(resolved, TokU4):
472 | counter = RuleCounterBit()
473 | elif isinstance(resolved, TokReg):
474 | counter = RuleCounterReg()
475 | elif isinstance(resolved, TokP):
476 | counter = RuleCounterP()
477 | elif isinstance(resolved, TokQ):
478 | counter = RuleCounterQ()
479 | else:
480 | raise RuntimeError(f'Unknown var tok: "{tok_name}".')
481 | self.rules.append(RuleVarRef(tok_name))
482 | self.variables[tok_name] = RuleVar(tok_name, counter)
483 | next_type = (TokWhitespace, type(None))
484 | else:
485 | raise RuntimeError(f'Unknown tok: "{resolved}".')
486 | self.rules.append(RuleEnd())
487 |
488 |
489 | @dataclass
490 | class DisasmParser(Parser):
491 | def parse(self):
492 | next_type = TokLiteral
493 | while self.peek():
494 | resolved = self.accept(next_type)
495 | if isinstance(resolved, TokLiteral):
496 | self.rules.append(RuleLiteral(resolved.data))
497 | next_type = (TokLiteral, TokDelimiter, TokVar, type(None))
498 | elif isinstance(resolved, TokDelimiter):
499 | self.rules.append(RuleLiteral(resolved.data))
500 | next_type = (TokLiteral, TokDelimiter, TokVar, type(None))
501 | elif isinstance(resolved, TokVar):
502 | tok_name = self.normalize_name(resolved.data)
503 | if isinstance(resolved, TokU16):
504 | counter = RuleCounter(256 * 256)
505 | elif isinstance(resolved, TokU8):
506 | counter = RuleCounter(256)
507 | elif isinstance(resolved, TokS8):
508 | counter = RuleCounter(256, is_signed=True)
509 | elif isinstance(resolved, TokU4):
510 | counter = RuleCounterBit(is_name_output=True)
511 | elif isinstance(resolved, TokReg):
512 | counter = RuleCounterReg(is_name_output=True)
513 | elif isinstance(resolved, TokP):
514 | counter = RuleCounterP(is_name_output=True)
515 | elif isinstance(resolved, TokQ):
516 | counter = RuleCounterQ(is_name_output=True)
517 | else:
518 | raise RuntimeError(f'Unknown var tok: "{tok_name}".')
519 | self.rules.append(RuleVarRef(tok_name))
520 | self.variables[tok_name] = RuleVar(tok_name, counter)
521 | self.rules.append(RuleEnd())
522 |
523 |
524 | """
525 | Generator
526 | """
527 |
528 |
529 | @dataclass
530 | class EmitGlyph:
531 | name: str
532 | asms: List = field(default_factory=list)
533 | lookup_i: int = field(default=0)
534 | is_offset: bool = field(default=False)
535 |
536 |
537 | @dataclass
538 | class EmitLookup:
539 | pass
540 |
541 |
542 | @dataclass
543 | class EmitLookupLigatureSubst(EmitLookup):
544 | ligature_set_glyphs: Dict = field(default_factory=dict)
545 |
546 |
547 | @dataclass
548 | class EmitLookupMultipleSubst(EmitLookup):
549 | in_glyphs: Dict = field(default_factory=dict)
550 |
551 |
552 | @dataclass
553 | class Generator:
554 | def generate(self, data):
555 | all_disasm_emitted = []
556 | all_asm_emitted = []
557 | for d in data:
558 | disasm = d[0]
559 | asm = d[1]
560 | disasm_tokens = disasm_tokenize(disasm)
561 | asm_tokens = asm_tokenize(asm)
562 | logging.debug(f"{disasm}=>{disasm_tokens} | {asm}=>{asm_tokens}")
563 |
564 | disasm_parser = DisasmParser(tokens=disasm_tokens)
565 | disasm_parser.parse()
566 | logging.debug("".join(str(x) for x in disasm_parser.rules))
567 | logging.debug(disasm_parser.variables)
568 |
569 | asm_parser = AsmParser(tokens=asm_tokens)
570 | asm_parser.parse()
571 | logging.debug("".join(str(x) for x in asm_parser.rules))
572 | logging.debug(asm_parser.variables)
573 |
574 | # First expand assembly encoding, since vars will have the order for disassembly splits
575 | asm_gen = ExpandGenerator(asm_parser.variables, asm_parser.rules)
576 | asm_emitted = asm_gen.generate()
577 | all_asm_emitted.extend(asm_emitted)
578 | logging.debug(asm_emitted)
579 |
580 | disasm_parser.rules = preprocess_splits(disasm_parser.rules, asm_emitted)
581 | logging.debug(disasm_parser.rules)
582 |
583 | disasm_gen = ExpandGenerator(disasm_parser.variables, disasm_parser.rules)
584 | disasm_emitted = disasm_gen.generate()
585 | all_disasm_emitted.extend(disasm_emitted)
586 | logging.debug(disasm_emitted)
587 |
588 | return all_disasm_emitted, all_asm_emitted
589 |
590 |
591 | @dataclass
592 | class ExpandGenerator(Generator):
593 | variables: Dict = field(default_factory=dict)
594 | rules: List = field(default_factory=list)
595 | rule_i: int = field(default=0)
596 | emitted: List = field(default_factory=list)
597 |
598 | def advance(self):
599 | if self.rule_i >= len(self.rules):
600 | return None
601 |
602 | next_rule = self.rules[self.rule_i]
603 | self.rule_i += 1
604 | return next_rule
605 |
606 | def peek(self, k=0):
607 | if (self.rule_i + k) >= len(self.rules):
608 | return None
609 |
610 | return self.rules[self.rule_i + k]
611 |
612 | def seek(self, i=0):
613 | if i >= len(self.rules):
614 | raise RuntimeError(f'Cannot seek to "{i}" >= "{len(self.rules)}".')
615 | self.rule_i = i
616 |
617 | def generate_expr(self, expr, out, level):
618 | if isinstance(expr.lhs, RuleVarRef):
619 | child = expr.lhs
620 | counter = self.variables[child.name].counter
621 | counter.reset()
622 | next_k = counter.next()
623 | while not isinstance(next_k, type(None)):
624 | prev_rule_i = self.rule_i
625 | self.generate_expr(
626 | RuleExpr(
627 | RuleHex(
628 | next_k,
629 | is_offset=(not counter.is_reg),
630 | is_signed=counter.is_signed,
631 | var_names=[self.variables[child.name].name],
632 | split_i=child.split_i,
633 | seq_i=child.seq_i,
634 | ),
635 | expr.op,
636 | expr.rhs,
637 | ),
638 | out[:],
639 | level + 1,
640 | )
641 | next_k = counter.next()
642 | self.seek(prev_rule_i)
643 |
644 | # Next tokens were already parsed on children, force flush
645 | while not isinstance(child, type(None)):
646 | child = self.advance()
647 | elif isinstance(expr.lhs, RuleHex):
648 | if isinstance(expr.rhs, RuleVarRef):
649 | child = expr.rhs
650 | counter = self.variables[child.name].counter
651 | counter.reset()
652 | next_k = counter.next()
653 | while not isinstance(next_k, type(None)):
654 | prev_rule_i = self.rule_i
655 | self.generate_expr(
656 | RuleExpr(
657 | expr.lhs,
658 | expr.op,
659 | RuleHex(
660 | next_k,
661 | is_offset=(not counter.is_reg),
662 | is_signed=counter.is_signed,
663 | var_names=[self.variables[child.name].name],
664 | split_i=child.split_i,
665 | seq_i=child.seq_i,
666 | ),
667 | ),
668 | out[:],
669 | level + 1,
670 | )
671 | next_k = counter.next()
672 | self.seek(prev_rule_i)
673 |
674 | # Next tokens were already parsed on children, force flush
675 | while not isinstance(child, type(None)):
676 | child = self.advance()
677 | elif isinstance(expr.rhs, RuleHex):
678 | var_names = []
679 | var_names_set = set()
680 | if isinstance(expr.lhs, RuleVarRef):
681 | name = self.variables[expr.lhs.name].name
682 | var_names.append(name)
683 | var_names_set.add(name)
684 | elif isinstance(expr.lhs, RuleHex):
685 | for name in expr.lhs.var_names:
686 | var_names.append(name)
687 | var_names_set.add(name)
688 | if isinstance(expr.rhs, RuleVarRef):
689 | name = self.variables[expr.rhs.name].name
690 | if name not in var_names_set:
691 | var_names.append(name)
692 | var_names_set.add(name)
693 | elif isinstance(expr.rhs, RuleHex):
694 | for name in expr.rhs.var_names:
695 | if name not in var_names_set:
696 | var_names.append(name)
697 | var_names_set.add(name)
698 | out.append(
699 | RuleHex(
700 | expr.eval(self.variables),
701 | is_offset=(expr.lhs.is_offset or expr.rhs.is_offset),
702 | is_signed=(expr.lhs.is_signed or expr.rhs.is_signed),
703 | var_names=var_names,
704 | split_i=min(expr.lhs.split_i, expr.rhs.split_i), # ???
705 | seq_i=min(expr.lhs.seq_i, expr.rhs.seq_i), # ???
706 | )
707 | )
708 | self.emitted.append(out)
709 | elif isinstance(expr.rhs, RuleExpr):
710 | child = expr.rhs
711 | prev_rule_i = self.rule_i
712 | self.generate_expr(child, out[:], level + 1)
713 | self.seek(prev_rule_i)
714 | child_emitted = self.emitted[:]
715 | self.emitted = []
716 | for rules in child_emitted:
717 | # WARN: Assuming sub-expression only generates a single RuleHex
718 | rule = rules[-1]
719 | prev_rule_i = self.rule_i
720 | self.generate_expr(
721 | RuleExpr(
722 | expr.lhs,
723 | expr.op,
724 | rule,
725 | ),
726 | out[:],
727 | level + 1,
728 | )
729 | self.seek(prev_rule_i)
730 |
731 | # Next tokens were already parsed on children, force flush
732 | while not isinstance(child, type(None)):
733 | child = self.advance()
734 | else:
735 | raise RuntimeError(f'Cannot generate rhs "{expr.rhs}".')
736 | elif isinstance(expr.lhs, RuleExpr):
737 | child = expr.lhs
738 | prev_rule_i = self.rule_i
739 | self.generate_expr(child, out[:], level + 1)
740 | self.seek(prev_rule_i)
741 | child_emitted = self.emitted[:]
742 | self.emitted = []
743 | for rules in child_emitted:
744 | # WARN: Assuming sub-expression only generates a single RuleHex
745 | rule = rules[-1]
746 | prev_rule_i = self.rule_i
747 | self.generate_expr(
748 | RuleExpr(
749 | rule,
750 | expr.op,
751 | expr.rhs,
752 | ),
753 | out[:],
754 | level + 1,
755 | )
756 | self.seek(prev_rule_i)
757 |
758 | # Next tokens were already parsed on children, force flush
759 | while not isinstance(child, type(None)):
760 | child = self.advance()
761 | else:
762 | raise RuntimeError(f'Cannot generate lhs "{expr.lhs}".')
763 |
764 | def generate_child(self, child, out, level):
765 | while self.peek():
766 | if not child:
767 | child = self.advance()
768 | if isinstance(child, RuleVarRef):
769 | counter = self.variables[child.name].counter
770 | counter.reset()
771 | next_k = counter.next()
772 | while not isinstance(next_k, type(None)):
773 | prev_rule_i = self.rule_i
774 | self.generate_child(
775 | (
776 | RuleLiteral(
777 | next_k,
778 | var_names=[self.variables[child.name].name],
779 | split_i=child.split_i,
780 | seq_i=child.seq_i,
781 | )
782 | if counter.is_name_output
783 | else RuleHex(
784 | next_k,
785 | is_offset=(not counter.is_reg),
786 | is_signed=counter.is_signed,
787 | var_names=[self.variables[child.name].name],
788 | split_i=child.split_i,
789 | seq_i=child.seq_i,
790 | )
791 | ),
792 | out[:],
793 | level + 1,
794 | )
795 | next_k = counter.next()
796 | self.seek(prev_rule_i)
797 |
798 | # Next tokens were already parsed on children, force flush
799 | while not isinstance(child, type(None)):
800 | child = self.advance()
801 | elif isinstance(child, RuleExpr):
802 | prev_emitted = self.emitted[:]
803 | self.emitted = []
804 | prev_rule_i = self.rule_i
805 | self.generate_expr(child, out[:], level + 1)
806 | self.seek(prev_rule_i)
807 | child_emitted = self.emitted[:]
808 | self.emitted = []
809 | for rules in child_emitted:
810 | # WARN: Assuming sub-expression only generates a single RuleHex
811 | rule = rules[-1]
812 | self.generate_child(None, out[:] + [rule], level + 1)
813 |
814 | self.generate_child(None, out[:], level + 1)
815 | self.seek(prev_rule_i)
816 | self.emitted.extend(prev_emitted)
817 |
818 | # Next tokens were already parsed on children, force flush
819 | while not isinstance(child, type(None)):
820 | child = self.advance()
821 | elif isinstance(child, (RuleLiteral, RuleHex)):
822 | out.append(child)
823 | elif isinstance(child, RuleEnd):
824 | break
825 | else:
826 | raise RuntimeError(f'Cannot generate rule "{child}".')
827 | child = None
828 | if isinstance(child, RuleEnd):
829 | self.emitted.append(out)
830 |
831 | def generate(self):
832 | logging.debug(self.rules)
833 | self.generate_child(None, [], 0)
834 | return self.emitted
835 |
836 |
837 | def text_to_bmp(text, name):
838 | print(f'Generating {name}.bmp: width="{85 * len(text)}", text="{text}"')
839 | filename = f"{name}.bmp"
840 | cmd = [
841 | "convert",
842 | "-size",
843 | f"{85 * len(text)}x72",
844 | "xc:white",
845 | "-font",
846 | "resources/NotoSansMono-Regular.ttf",
847 | "-pointsize",
848 | "72",
849 | "-fill",
850 | "black",
851 | "-draw",
852 | f"text 25, 65 '{text}'",
853 | filename,
854 | ]
855 | try:
856 | proc = subprocess.run(cmd, capture_output=True)
857 | output_file = Path(f"./{filename}")
858 | if not output_file.exists():
859 | print(
860 | f"convert returned success for {filename}, but file not found:\n{proc.stdout}\n{proc.stderr}",
861 | file=sys.stderr,
862 | )
863 | except subprocess.CalledProcessError as e:
864 | print(
865 | f"convert for {filename} failed with code {e.returncode}):\n{e.stdout}\n{e.stderr}",
866 | file=sys.stderr,
867 | )
868 | try:
869 | os.remove(filename)
870 | except OSError:
871 | pass
872 | raise e
873 |
874 |
875 | def text_to_svg(name):
876 | Path("./out_svg").mkdir(parents=True, exist_ok=True)
877 | output_file = Path(f"./out_svg/{name}.svg")
878 | filename = f"{name}.bmp"
879 | cmd = [
880 | "potrace",
881 | "-s",
882 | filename,
883 | "-o",
884 | output_file.absolute(),
885 | ]
886 | try:
887 | proc = subprocess.run(cmd, capture_output=True)
888 | output_file = Path(f"./{filename}")
889 | if not output_file.exists():
890 | print(
891 | f"potrace returned success for {filename}, but file not found:\n{proc.stdout}\n{proc.stderr}",
892 | file=sys.stderr,
893 | )
894 | except subprocess.CalledProcessError as e:
895 | print(
896 | f"potrace for {filename} failed with code {e.returncode}):\n{e.stdout}\n{e.stderr}",
897 | file=sys.stderr,
898 | )
899 | raise e
900 | finally:
901 | try:
902 | os.remove(filename)
903 | except OSError:
904 | pass
905 |
906 |
907 | def ttf_to_ttx():
908 | cmd = ["./ttf_to_ttx.sh"]
909 | try:
910 | proc = subprocess.run(cmd, capture_output=True, shell=True)
911 | except subprocess.CalledProcessError as e:
912 | print(
913 | f"ttf_to_ttx failed with code {e.returncode}):\n{e.stdout}\n{e.stderr}", file=sys.stderr,
914 | )
915 | raise e
916 |
917 |
918 | def ttx_to_ttf():
919 | cmd = ["./ttx_to_ttf.sh"]
920 | try:
921 | proc = subprocess.run(cmd, capture_output=True, shell=True)
922 | except subprocess.CalledProcessError as e:
923 | print(
924 | f"ttx_to_ttf failed with code {e.returncode}):\n{e.stdout}\n{e.stderr}",
925 | file=sys.stderr,
926 | )
927 | raise e
928 |
929 |
930 | def digit_to_name(x):
931 | options = {
932 | 0: "zero",
933 | 1: "one",
934 | 2: "two",
935 | 3: "three",
936 | 4: "four",
937 | 5: "five",
938 | 6: "six",
939 | 7: "seven",
940 | 8: "eight",
941 | 9: "nine",
942 | 10: "a",
943 | 11: "b",
944 | 12: "c",
945 | 13: "d",
946 | 14: "e",
947 | 15: "f",
948 | }
949 | if isinstance(x, str):
950 | x = int(x, 16)
951 | return options[x]
952 |
953 |
954 | def emit_multiple_subst(emitter, index):
955 | tmpl = """
956 |
957 |
958 |
959 |
960 |
961 |
962 | """
963 | tmpl = tmpl.replace("__i__", str(index))
964 | soup = BeautifulSoup(tmpl, features="xml")
965 |
966 | lookup = soup.find("Lookup")
967 | lookup_tag_multiple_subst = lookup.find("MultipleSubst")
968 | for key in emitter.in_glyphs.keys():
969 | tag_substitution = soup.new_tag(
970 | "Substitution",
971 | attrs={
972 | "in": key,
973 | "out": emitter.in_glyphs[key],
974 | },
975 | )
976 | lookup_tag_multiple_subst.append(tag_substitution)
977 |
978 | return soup
979 |
980 |
981 | def emit_ligature_subst(emitter, index):
982 | tmpl = """
983 |
984 |
985 |
986 |
987 |
988 |
989 | """
990 | tmpl = tmpl.replace("__i__", str(index))
991 | soup = BeautifulSoup(tmpl, features="xml")
992 |
993 | lookup = soup.find("Lookup")
994 | lookup_tag_ligature_subst = lookup.find("LigatureSubst")
995 | for key in emitter.ligature_set_glyphs.keys():
996 | tag_ligature_set = soup.new_tag(
997 | "LigatureSet",
998 | attrs={
999 | "glyph": key,
1000 | },
1001 | )
1002 | emitLookup_ligature_set = emitter.ligature_set_glyphs[key]
1003 | for ligature_key in emitLookup_ligature_set.keys():
1004 | tag_ligature = soup.new_tag(
1005 | "Ligature",
1006 | attrs={
1007 | "components": ligature_key,
1008 | "glyph": emitLookup_ligature_set[ligature_key],
1009 | },
1010 | )
1011 | tag_ligature_set.append(tag_ligature)
1012 | lookup_tag_ligature_subst.append(tag_ligature_set)
1013 |
1014 | return soup
1015 |
1016 |
1017 | def emit_chain_suffixes(
1018 | prefixes, suffix_key, lookup6_tmpl, lookup7_tmpl, offset_suffix_str
1019 | ):
1020 | lookup6 = BeautifulSoup(
1021 | lookup6_tmpl.replace("__i__", str(lookup_list_i)).replace(
1022 | "__i2__", str(lookup_list_i + 1)
1023 | ),
1024 | features="xml",
1025 | )
1026 | lookup6_tag_extension_subst0 = lookup6.find("BacktrackClassDef")
1027 | lookup7 = BeautifulSoup(
1028 | lookup7_tmpl.replace("__i__", str(lookup_list_i + 1)),
1029 | features="xml",
1030 | )
1031 | lookup7_tag_multiple_subst = lookup7.find("MultipleSubst")
1032 |
1033 | emitLookup7 = EmitLookupMultipleSubst()
1034 | for i in range(0x10):
1035 | emitLookup7.in_glyphs[f"offset_{i:01x}_{offset_suffix_str}"] = (
1036 | f"offset_{i:01x}_{offset_suffix_str},{suffix_key}"
1037 | )
1038 | for key in emitLookup7.in_glyphs.keys():
1039 | tag_substitution = soup.new_tag(
1040 | "Substitution",
1041 | attrs={
1042 | "in": key,
1043 | "out": emitLookup7.in_glyphs[key],
1044 | },
1045 | )
1046 | lookup7_tag_multiple_subst.append(tag_substitution)
1047 |
1048 | for prefix in prefixes:
1049 | tag_class_def = soup.new_tag(
1050 | "ClassDef",
1051 | attrs={
1052 | "glyph": prefix,
1053 | "class": "1",
1054 | },
1055 | )
1056 | lookup6_tag_extension_subst0.append(tag_class_def)
1057 |
1058 | return lookup6, lookup7
1059 |
1060 |
1061 | def preprocess_literals(splits):
1062 | splits = sorted(splits, key=lambda x: x.seq_i)
1063 |
1064 | merged_disasm_instruction = []
1065 | disasm_str = ""
1066 | prev_split_i = splits[0].split_i
1067 | prev_seq_i = splits[0].seq_i
1068 | for r in splits:
1069 | # TODO/FIXME
1070 | # if prev_split_i == r.split_i and isinstance(r, RuleLiteral):
1071 | if isinstance(r, RuleLiteral):
1072 | disasm_str += r.eval()
1073 | else:
1074 | if disasm_str:
1075 | is_signed = False
1076 | if isinstance(r, RuleHex) and r.is_signed:
1077 | if disasm_str[-1] == "+":
1078 | disasm_str = disasm_str[:-1]
1079 | is_signed = True
1080 | merged_disasm_instruction.append(
1081 | RuleLiteral(disasm_str, is_signed=is_signed, split_i=prev_split_i, seq_i=prev_seq_i)
1082 | )
1083 | disasm_str = ""
1084 | if isinstance(r, RuleLiteral):
1085 | disasm_str = r.eval()
1086 | else:
1087 | merged_disasm_instruction.append(r)
1088 | prev_split_i = r.split_i
1089 | prev_seq_i = r.seq_i
1090 |
1091 | if disasm_str:
1092 | merged_disasm_instruction.append(RuleLiteral(disasm_str, split_i=prev_split_i, seq_i=prev_seq_i))
1093 | disasm_str = ""
1094 |
1095 | return merged_disasm_instruction
1096 |
1097 |
1098 | def preprocess_splits(disasm_rules, asm_emitted):
1099 | asm_mappings = {}
1100 | for asm_rules in asm_emitted:
1101 | split_i = 0
1102 | for i, rule in enumerate(asm_rules):
1103 | if len(rule.var_names) > 0:
1104 | split_i += 1
1105 | for name in rule.var_names:
1106 | asm_mappings[name] = split_i
1107 | rule.split_i = split_i
1108 | rule.seq_i = i
1109 |
1110 | split_i = 0
1111 | disasm_splits = []
1112 | for i, rule in enumerate(disasm_rules):
1113 | if not isinstance(rule, RuleEnd):
1114 | if isinstance(rule, RuleVarRef):
1115 | name = rule.name
1116 | mapping_i = asm_mappings[name]
1117 | split_i = mapping_i
1118 | elif len(rule.var_names) > 0:
1119 | for name in rule.var_names:
1120 | assert len(rule.var_names) == 1
1121 | mapping_i = asm_mappings[name]
1122 | split_i = mapping_i
1123 | rule.split_i = split_i
1124 | rule.seq_i = i
1125 | disasm_splits.append(rule)
1126 |
1127 | # TODO/FIXME
1128 | return sorted(disasm_splits, key=lambda x: x.split_i)
1129 | # return disasm_splits
1130 |
1131 |
1132 | def emit_chain_init_fini(
1133 | seen_lookup_ambiguous,
1134 | seen_lookup_init_fini_mappings,
1135 | tmpl_chain_context_subst_init_fini_lit,
1136 | tmpl_ligature_subst_init_lit,
1137 | lookup_list_i,
1138 | ):
1139 | # Note: Assuming ambiguous cases to only have an offset in the middle (no u16 or other variants)
1140 | lookups = []
1141 | for key in seen_lookup_ambiguous:
1142 | if len(list(seen_lookup_ambiguous[key].keys())) > 1:
1143 | raise RuntimeError(f"len>1 {seen_lookup_ambiguous[key]}")
1144 |
1145 | # Next chars in init prefix
1146 | assert len(list(seen_lookup_ambiguous[key].keys())) == 1
1147 | component_key = list(seen_lookup_ambiguous[key].keys())[0]
1148 | for prefix in seen_lookup_ambiguous[key][component_key]:
1149 | lookup0 = BeautifulSoup(
1150 | tmpl_chain_context_subst_init_fini_lit.replace("__i__", str(lookup_list_i)),
1151 | features="xml",
1152 | )
1153 | tag_ext = lookup0.find("ExtensionSubst", {"index": "0"})
1154 | tag_chain_ctx = tag_ext.find("ChainContextSubst")
1155 |
1156 | # First char in init prefix
1157 | tag_cov = soup.new_tag(
1158 | "InputCoverage",
1159 | attrs={
1160 | "index": 0,
1161 | },
1162 | )
1163 | tag_glyph = soup.new_tag(
1164 | "Glyph",
1165 | attrs={
1166 | "value": key,
1167 | },
1168 | )
1169 | tag_cov.append(tag_glyph)
1170 | tag_chain_ctx.append(tag_cov)
1171 |
1172 | la_i = 0
1173 | for component in component_key.split(","):
1174 | tag_cov = soup.new_tag(
1175 | "LookAheadCoverage",
1176 | attrs={
1177 | "index": la_i,
1178 | },
1179 | )
1180 | la_i += 1
1181 | tag_glyph = soup.new_tag(
1182 | "Glyph",
1183 | attrs={
1184 | "value": component,
1185 | },
1186 | )
1187 | tag_cov.append(tag_glyph)
1188 | tag_chain_ctx.append(tag_cov)
1189 |
1190 | # Chars for offset
1191 | for i in range(2):
1192 | tag_cov = soup.new_tag(
1193 | "LookAheadCoverage",
1194 | attrs={
1195 | "index": la_i,
1196 | },
1197 | )
1198 | la_i += 1
1199 | for offset_i in range(16):
1200 | tag_glyph = soup.new_tag(
1201 | "Glyph",
1202 | attrs={
1203 | "value": digit_to_name(offset_i),
1204 | },
1205 | )
1206 | tag_cov.append(tag_glyph)
1207 | tag_chain_ctx.append(tag_cov)
1208 |
1209 | # Chars for fini suffix
1210 | fini = seen_lookup_init_fini_mappings[prefix].split(",")[1]
1211 | for nibble in fini:
1212 | tag_cov = soup.new_tag(
1213 | "LookAheadCoverage",
1214 | attrs={
1215 | "index": la_i,
1216 | },
1217 | )
1218 | la_i += 1
1219 | tag_glyph = soup.new_tag(
1220 | "Glyph",
1221 | attrs={
1222 | "value": digit_to_name(nibble),
1223 | },
1224 | )
1225 | tag_cov.append(tag_glyph)
1226 | tag_chain_ctx.append(tag_cov)
1227 | tag_cov = soup.new_tag(
1228 | "LookAheadCoverage",
1229 | attrs={
1230 | "index": la_i,
1231 | },
1232 | )
1233 | la_i += 1
1234 | tag_glyph = soup.new_tag(
1235 | "Glyph",
1236 | attrs={
1237 | "value": ".null",
1238 | },
1239 | )
1240 | tag_cov.append(tag_glyph)
1241 | tag_chain_ctx.append(tag_cov)
1242 |
1243 | # Sub rule for next lookup
1244 | tag_ext = lookup0.find("ExtensionSubst", {"index": "1"})
1245 | tag_chain_ctx = tag_ext.find("ChainContextSubst")
1246 | tag_cov = soup.new_tag("Coverage")
1247 | tag_glyph = soup.new_tag(
1248 | "Glyph",
1249 | attrs={
1250 | "value": key,
1251 | },
1252 | )
1253 | tag_cov.append(tag_glyph)
1254 | tag_chain_ctx.append(tag_cov)
1255 | tag_chain_subruleset = soup.new_tag("ChainSubRuleSet", attrs={ "index":"0" })
1256 |
1257 | subrule_i = 0
1258 | for offset_i in range(16):
1259 | for offset_j in range(16):
1260 | tmpl_chain = """
1261 |
1262 |
1263 |
1264 |
1265 |
1266 | """
1267 | tag_chain_subrule = BeautifulSoup(
1268 | tmpl_chain.replace("__subrule_i__", str(subrule_i)).replace("__i2__", str(lookup_list_i + 1)),
1269 | features="xml",
1270 | )
1271 | subrule_i += 1
1272 | tag_subrule = tag_chain_subrule.find("ChainSubRule")
1273 |
1274 | la_i = 0
1275 | for component in component_key.split(","):
1276 | tag_glyph = soup.new_tag(
1277 | "LookAhead",
1278 | attrs={
1279 | "index": la_i,
1280 | "value": component,
1281 | },
1282 | )
1283 | la_i += 1
1284 | tag_subrule.append(tag_glyph)
1285 | tag_glyph = soup.new_tag(
1286 | "LookAhead",
1287 | attrs={
1288 | "index": la_i,
1289 | "value": digit_to_name(offset_i),
1290 | },
1291 | )
1292 | la_i += 1
1293 | tag_subrule.append(tag_glyph)
1294 | tag_glyph = soup.new_tag(
1295 | "LookAhead",
1296 | attrs={
1297 | "index": la_i,
1298 | "value": digit_to_name(offset_j),
1299 | },
1300 | )
1301 | la_i += 1
1302 | tag_subrule.append(tag_glyph)
1303 | for nibble in fini:
1304 | tag_glyph = soup.new_tag(
1305 | "LookAhead",
1306 | attrs={
1307 | "index": la_i,
1308 | "value": digit_to_name(nibble),
1309 | },
1310 | )
1311 | la_i += 1
1312 | tag_subrule.append(tag_glyph)
1313 |
1314 | tag_chain_subruleset.append(tag_chain_subrule)
1315 | tag_chain_ctx.append(tag_chain_subruleset)
1316 |
1317 | lookups.append([lookup0, True])
1318 | lookup_list_i += 1
1319 |
1320 | # Generate ligature for next lookup
1321 | lookup1 = BeautifulSoup(
1322 | tmpl_ligature_subst_init_lit.replace("__i__", str(lookup_list_i)),
1323 | features="xml",
1324 | )
1325 | lookup1_tag_ligature_subst = lookup1.find("LigatureSubst")
1326 | tag_ligature_set = soup.new_tag(
1327 | "LigatureSet",
1328 | attrs={
1329 | "glyph": key,
1330 | },
1331 | )
1332 | tag_ligature = soup.new_tag(
1333 | "Ligature",
1334 | attrs={
1335 | "components": component_key,
1336 | "glyph": prefix
1337 | },
1338 | )
1339 | tag_ligature_set.append(tag_ligature)
1340 | lookup1_tag_ligature_subst.append(tag_ligature_set)
1341 |
1342 | lookups.append([lookup1, False])
1343 | lookup_list_i += 1
1344 |
1345 | return lookups, lookup_list_i
1346 |
1347 |
1348 | with open(sys.argv[1], "rb") as f:
1349 | g = Generator()
1350 | print("Computing rules.")
1351 | disasm_emitted, asm_emitted = g.generate(json.load(f))
1352 | print("Emitted lengths:", len(disasm_emitted), len(asm_emitted))
1353 | assert len(disasm_emitted) == len(asm_emitted)
1354 |
1355 | glyph_chains = []
1356 | glyph_ids = {}
1357 | glyph_i = 0
1358 | is_offset_seen = False
1359 | seen_init_fini = set()
1360 | seen_signed_literals = set()
1361 | seen_fini_with_asms = set()
1362 | for i in range(len(disasm_emitted)):
1363 | disasm_instruction = disasm_emitted[i]
1364 | asm_instruction = asm_emitted[i]
1365 |
1366 | disasm_instruction = preprocess_literals(disasm_instruction)
1367 | logging.debug((asm_instruction, disasm_instruction))
1368 |
1369 | asm_i = 0
1370 | prev_asm_i = 0
1371 | prev_split_i = 0
1372 | prev_is_offset = False
1373 | disasm_str = ""
1374 | glyph_chain = []
1375 | lookup_i = 0
1376 | is_signed = False
1377 | asms_mappings = {}
1378 | for ri in range(len(disasm_instruction)):
1379 | r = disasm_instruction[ri]
1380 | if isinstance(r, RuleLiteral):
1381 | if prev_is_offset != r.is_offset:
1382 | lookup_i += 1
1383 | prev_is_offset = r.is_offset
1384 |
1385 | is_signed = r.is_signed
1386 | disasm_str = r.eval()
1387 |
1388 | asms = []
1389 | while (
1390 | asm_i < len(asm_instruction)
1391 | and asm_instruction[asm_i].is_offset == False
1392 | # TODO/FIXME
1393 | # and asm_instruction[asm_i].split_i == r.split_i
1394 | ):
1395 | asms.append(asm_instruction[asm_i].glyph_str())
1396 | asm_i += 1
1397 | asms_mappings[ri] = asms
1398 |
1399 | is_distinct_glyph = disasm_str not in glyph_ids
1400 | if (
1401 | disasm_str
1402 | and (ri == 0 or ri == len(disasm_instruction) - 1)
1403 | and len(disasm_instruction) > 2
1404 | and isinstance(disasm_instruction[-1], RuleLiteral)
1405 | ):
1406 | # ["ADC A,(IX+o)", "DD 8E o"],
1407 | # ["SRA (IX+o)", "DD CB o 2E"],
1408 | if ri == 0:
1409 | init_fini = f'{disasm_str}({"".join(asms)})_{disasm_instruction[-1].eval()}'
1410 | else:
1411 | init_fini = f'{disasm_str}({"".join(asms_mappings[0])})_{disasm_instruction[-1].eval()}({"".join(asms)})'
1412 | if init_fini not in seen_init_fini:
1413 | is_distinct_glyph = True
1414 | seen_init_fini.add(init_fini)
1415 | if is_distinct_glyph:
1416 | glyph_name = (
1417 | f"{glyph_i:08X}_{re.sub('[^A-Za-z0-9]+', '_', disasm_str)}"
1418 | )
1419 | glyph_i += 1
1420 | glyph_ids[disasm_str] = EmitGlyph(glyph_name, asms, lookup_i)
1421 | text_to_bmp(disasm_str, glyph_name)
1422 | text_to_svg(glyph_name)
1423 | if is_signed:
1424 | seen_signed_literals.add(glyph_ids[disasm_str].name)
1425 | prev_asm_i = asm_i
1426 | glyph_chain.append(glyph_ids[disasm_str])
1427 | if lookup_i == 2 and len(asms) > 0:
1428 | seen_fini_with_asms.add(glyph_ids[disasm_str].name)
1429 | else:
1430 | if prev_is_offset != r.is_offset:
1431 | lookup_i += 1
1432 | prev_is_offset = r.is_offset
1433 |
1434 | offset_str = r.eval()
1435 | logging.debug((offset_str, prev_asm_i, asm_i))
1436 |
1437 | asm_i = prev_asm_i
1438 | asms = []
1439 | while (
1440 | asm_i < len(asm_instruction)
1441 | and asm_instruction[asm_i].is_offset == True
1442 | # TODO/FIXME
1443 | # and asm_instruction[asm_i].split_i == r.split_i
1444 | ):
1445 | asms.append(asm_instruction[asm_i].glyph_str())
1446 | asm_i += 1
1447 | asms_mappings[ri] = asms
1448 |
1449 | for nibble in f"{offset_str:02x}":
1450 | if nibble not in glyph_ids:
1451 | glyph_name = f"offset_{nibble}"
1452 | glyph_ids[nibble] = EmitGlyph(
1453 | glyph_name, asms, lookup_i, is_offset=True
1454 | )
1455 | text_to_bmp(f"{nibble}", glyph_name)
1456 | text_to_bmp(f"{nibble}", f"{glyph_name}_2")
1457 | text_to_bmp(f"{nibble}", f"{glyph_name}_3")
1458 | text_to_bmp(f"{nibble}", f"{glyph_name}_4")
1459 | text_to_bmp(f"{nibble}", f"{glyph_name}_s")
1460 | text_to_bmp(f"{nibble}", f"{glyph_name}_s2")
1461 | text_to_svg(glyph_name)
1462 | text_to_svg(f"{glyph_name}_2")
1463 | text_to_svg(f"{glyph_name}_3")
1464 | text_to_svg(f"{glyph_name}_4")
1465 | text_to_svg(f"{glyph_name}_s")
1466 | text_to_svg(f"{glyph_name}_s2")
1467 | glyph_chain.append(glyph_ids[nibble])
1468 |
1469 | glyph_chains.append(glyph_chain)
1470 |
1471 | ttf_to_ttx()
1472 |
1473 | gpos_tmpl = """
1474 |
1475 |
1476 |
1477 |
1478 |
1479 |
1485 |
1486 |
1487 |
1488 |
1489 |
1490 |
1491 |
1492 |
1493 |
1494 |
1495 |
1496 |
1497 |
1498 |
1499 |
1500 |
1501 |
1502 |
1503 |
1504 |
1505 |
1506 |
1507 |
1508 |
1509 |
1510 |
1511 |
1512 |
1513 |
1514 |
1515 |
1516 |
1517 |
1518 |
1519 |
1520 |
1521 |
1522 |
1523 |
1524 |
1525 |
1526 |
1527 |
1528 |
1529 |
1530 |
1531 |
1532 |
1533 |
1534 |
1535 |
1536 |
1537 |
1538 |
1539 |
1540 |
1541 |
1542 |
1543 |
1544 |
1545 |
1546 |
1547 |
1548 |
1549 |
1550 |
1551 |
1552 |
1553 |
1554 |
1555 |
1556 |
1557 |
1558 |
1559 |
1560 |
1561 |
1562 |
1563 |
1564 |
1565 |
1566 |
1567 |
1568 |
1569 |
1570 |
1571 |
1572 |
1573 |
1574 |
1575 |
1576 |
1577 |
1578 |
1579 |
1580 |
1581 |
1582 |
1583 |
1584 |
1585 |
1586 |
1587 |
1588 |
1589 |
1590 |
1591 |
1592 |
1593 |
1594 |
1595 |
1596 |
1597 |
1598 |
1599 |
1600 |
1601 |
1602 |
1603 |
1604 |
1605 |
1606 |
1607 |
1608 |
1609 |
1610 |
1611 |
1612 |
1613 |
1614 |
1615 |
1616 |
1617 |
1618 |
1619 |
1620 |
1621 |
1622 |
1623 |
1624 |
1625 |
1626 |
1627 |
1628 |
1629 |
1630 |
1631 |
1632 |
1633 |
1634 |
1635 |
1636 |
1637 |
1638 |
1639 |
1640 |
1641 |
1642 |
1643 |
1644 |
1645 |
1646 |
1647 |
1648 |
1649 |
1650 |
1651 |
1652 |
1653 |
1654 |
1655 |
1656 |
1657 |
1658 |
1659 |
1660 |
1661 |
1662 |
1663 |
1664 |
1665 |
1666 |
1667 |
1668 |
1669 |
1670 |
1671 |
1672 |
1673 |
1674 |
1675 |
1676 |
1677 |
1678 |
1679 |
1680 |
1681 |
1682 |
1683 |
1684 |
1685 |
1686 |
1687 |
1688 |
1689 |
1690 |
1691 |
1692 |
1693 |
1694 |
1695 |
1696 |
1697 |
1698 |
1699 |
1700 |
1701 |
1702 |
1703 |
1704 |
1705 |
1706 |
1707 |
1708 |
1709 |
1710 |
1711 |
1712 |
1713 |
1714 |
1715 |
1716 |
1717 |
1718 |
1719 |
1720 |
1721 |
1722 |
1723 |
1724 |
1725 |
1726 |
1727 |
1728 |
1729 |
1730 |
1731 |
1732 |
1733 |
1734 |
1735 |
1736 |
1737 |
1738 |
1739 |
1740 |
1741 |
1742 |
1743 |
1744 |
1745 |
1746 |
1747 |
1748 |
1749 |
1750 |
1751 |
1752 |
1753 |
1754 |
1755 |
1756 |
1757 |
1758 |
1759 |
1760 |
1761 |
1762 |
1763 |
1764 |
1765 |
1766 |
1767 |
1768 |
1769 |
1770 |
1771 |
1772 |
1773 |
1774 |
1775 |
1776 |
1777 |
1778 |
1779 |
1780 |
1781 |
1782 |
1783 |
1784 |
1785 |
1786 |
1787 |
1788 |
1789 |
1790 |
1791 |
1792 |
1793 |
1794 |
1795 |
1796 |
1797 |
1798 |
1799 |
1800 |
1801 |
1802 |
1803 |
1804 |
1805 |
1806 |
1807 |
1808 |
1809 |
1810 |
1811 |
1812 |
1813 |
1814 |
1815 |
1816 |
1817 |
1818 |
1819 |
1820 |
1821 |
1822 |
1823 |
1824 |
1825 |
1826 |
1827 |
1828 |
1829 |
1830 |
1831 |
1832 |
1833 |
1834 |
1835 |
1836 |
1837 |
1838 |
1839 |
1840 |
1841 |
1842 |
1843 |
1844 |
1845 |
1846 |
1847 |
1848 |
1849 |
1850 |
1851 |
1852 |
1853 |
1854 |
1855 |
1856 |
1857 |
1858 |
1859 |
1860 |
1861 |
1862 |
1863 |
1864 |
1865 |
1866 |
1867 |
1868 |
1869 |
1870 |
1871 |
1872 |
1873 |
1874 |
1875 |
1876 |
1877 |
1878 |
1879 |
1880 |
1881 |
1882 |
1883 |
1884 |
1885 |
1886 |
1887 |
1888 |
1889 |
1890 |
1891 |
1892 |
1893 |
1894 |
1895 |
1896 |
1897 |
1898 |
1899 |
1900 |
1901 |
1902 |
1903 |
1904 |
1905 |
1906 | """
1907 |
1908 | gsub_tmpl = """
1909 |
1910 |
1911 |
1912 |
1913 |
1914 |
1920 |
1921 |
1922 |
1923 |
1924 |
1925 |
1926 |
1927 |
1928 |
1929 |
1930 |
1931 |
1932 | """
1933 |
1934 | tmpl_chain_context_subst_init_fini_lit = """
1935 |
1936 |
1937 |
1938 |
1939 |
1940 |
1941 |
1942 |
1943 |
1944 |
1945 |
1946 |
1947 |
1948 |
1949 | """
1950 |
1951 | tmpl0_chain_context_subst_init_lit = """
1952 |
1953 |
1954 |
1955 |
1956 |
1957 |
1958 |
1959 |
1960 |
1961 |
1962 |
1963 |
1964 |
1965 |
1966 |
1967 |
1968 |
1969 |
1970 |
1971 |
1972 |
1973 |
1974 |
1975 |
1976 |
1977 |
1978 |
1979 |
1980 |
1981 |
1982 |
1983 |
1984 |
1985 |
1986 |
1987 |
1988 |
1989 |
1990 |
1991 |
1992 |
1993 |
1994 |
1995 |
1996 |
1997 |
1998 |
1999 |
2000 |
2001 |
2002 |
2003 |
2004 |
2005 |
2006 |
2007 |
2008 |
2009 |
2010 |
2011 |
2012 |
2013 |
2014 |
2015 |
2016 |
2017 |
2018 |
2019 |
2020 |
2021 |
2022 |
2023 |
2024 |
2025 |
2026 |
2027 |
2028 |
2029 |
2030 |
2031 |
2032 |
2033 |
2034 |
2035 |
2036 |
2037 |
2038 |
2039 |
2040 |
2041 |
2042 |
2043 |
2044 |
2045 |
2046 |
2047 |
2048 |
2049 |
2050 |
2051 |
2052 |
2053 |
2054 |
2055 |
2056 |
2057 |
2058 |
2059 |
2060 |
2061 |
2062 |
2063 |
2064 |
2065 |
2066 |
2067 |
2068 |
2069 |
2070 |
2071 |
2072 |
2073 |
2074 |
2075 |
2076 |
2077 |
2078 |
2079 |
2080 |
2081 |
2082 |
2083 |
2084 |
2085 |
2086 |
2087 |
2088 |
2089 |
2090 |
2091 |
2092 |
2093 |
2094 |
2095 |
2096 |
2097 |
2098 |
2099 |
2100 |
2101 |
2102 |
2103 |
2104 | """
2105 |
2106 | tmpl_ligature_subst_init_lit = """
2107 |
2108 |
2109 |
2110 |
2111 |
2112 |
2113 |
2114 |
2115 |
2116 | """
2117 |
2118 | tmpl0_multiple_subst_offset1_delim = """
2119 |
2120 |
2121 |
2122 |
2123 |
2124 |
2125 | """
2126 |
2127 | tmpl0_ligature_subst_offset1_value = """
2128 |
2129 |
2130 |
2131 |
2132 |
2133 |
2134 | """
2135 |
2136 | tmpl1_chain_context_subst_offset2_lit = """
2137 |
2138 |
2139 |
2140 |
2141 |
2142 |
2143 |
2144 |
2145 |
2146 |
2147 |
2148 |
2149 |
2150 |
2151 |
2152 |
2153 |
2154 |
2155 |
2156 |
2157 |
2158 |
2159 |
2160 |
2161 |
2162 |
2163 |
2164 |
2165 |
2166 |
2167 |
2168 |
2169 |
2170 |
2171 |
2172 |
2173 |
2174 |
2175 |
2176 |
2177 |
2178 |
2179 |
2180 |
2181 |
2182 |
2183 |
2184 |
2185 |
2186 |
2187 |
2188 |
2189 |
2190 |
2191 |
2192 |
2193 |
2194 |
2195 |
2196 |
2197 |
2198 |
2199 |
2200 |
2201 |
2202 |
2203 |
2204 |
2205 |
2206 |
2207 |
2208 |
2209 |
2210 |
2211 |
2212 |
2213 |
2214 |
2215 | """
2216 |
2217 | tmpl1_chain_context_subst_offset4_lit = """
2218 |
2219 |
2220 |
2221 |
2222 |
2223 |
2224 |
2225 |
2226 |
2227 |
2228 |
2229 |
2230 |
2231 |
2232 |
2233 |
2234 |
2235 |
2236 |
2237 |
2238 |
2239 |
2240 |
2241 |
2242 |
2243 |
2244 |
2245 |
2246 |
2247 |
2248 |
2249 |
2250 |
2251 |
2252 |
2253 |
2254 |
2255 |
2256 |
2257 |
2258 |
2259 |
2260 |
2261 |
2262 |
2263 |
2264 |
2265 |
2266 |
2267 |
2268 |
2269 |
2270 |
2271 |
2272 |
2273 |
2274 |
2275 |
2276 |
2277 |
2278 |
2279 |
2280 |
2281 |
2282 |
2283 |
2284 |
2285 |
2286 |
2287 |
2288 |
2289 |
2290 |
2291 |
2292 |
2293 |
2294 |
2295 |
2296 |
2297 |
2298 |
2299 |
2300 |
2301 |
2302 |
2303 |
2304 |
2305 |
2306 |
2307 |
2308 |
2309 |
2310 |
2311 |
2312 |
2313 |
2314 |
2315 |
2316 |
2317 |
2318 |
2319 |
2320 |
2321 |
2322 |
2323 |
2324 |
2325 |
2326 |
2327 |
2328 |
2329 |
2330 | """
2331 |
2332 | tmpl1_multiple_subst_offset2_lit = """
2333 |
2334 |
2335 |
2336 |
2337 |
2338 |
2339 |
2340 |
2341 |
2342 | """
2343 |
2344 | tmpl2_chain_context_subst_offset3_delim = """
2345 |
2346 |
2347 |
2348 |
2349 |
2350 |
2351 |
2352 |
2353 |
2354 |
2355 |
2356 |
2357 |
2358 |
2359 |
2360 |
2361 |
2362 |
2363 |
2364 |
2365 |
2366 |
2367 |
2368 |
2369 |
2370 |
2371 |
2372 |
2373 |
2374 |
2375 |
2376 |
2377 |
2378 |
2379 |
2380 |
2381 |
2382 |
2383 |
2384 |
2385 |
2386 |
2387 |
2388 |
2389 |
2390 |
2391 |
2392 |
2393 |
2394 |
2395 |
2396 |
2397 |
2398 |
2399 |
2400 |
2401 |
2402 |
2403 |
2404 |
2405 |
2406 |
2407 |
2408 |
2409 |
2410 |
2411 |
2412 |
2413 |
2414 |
2415 |
2416 |
2417 |
2418 |
2419 |
2420 |
2421 |
2422 |
2423 | """
2424 |
2425 | tmpl_chain_context_subst_signed = """
2426 |
2427 |
2428 |
2429 |
2430 |
2431 |
2432 |
2433 |
2434 |
2435 |
2436 |
2437 |
2438 |
2439 |
2440 |
2441 |
2442 |
2443 |
2444 |
2445 |
2446 |
2447 |
2448 |
2449 |
2450 |
2451 |
2452 |
2453 |
2454 | """
2455 |
2456 | tmpl_multiple_subst_signed = """
2457 |
2458 |
2459 |
2460 |
2461 |
2462 |
2463 | """
2464 |
2465 | tmpl_chain_context_subst_signed_offset_2 = """
2466 |
2467 |
2468 |
2469 |
2470 |
2471 |
2472 |
2473 |
2474 |
2475 |
2476 |
2477 |
2478 |
2479 |
2480 |
2481 |
2482 |
2483 |
2484 |
2485 |
2486 |
2487 |
2488 |
2489 |
2490 |
2491 |
2492 |
2493 |
2494 |
2495 |
2496 |
2497 |
2498 |
2499 |
2500 |
2501 |
2502 |
2503 |
2504 |
2505 |
2506 |
2507 |
2508 |
2509 |
2510 |
2511 |
2512 |
2513 |
2514 |
2515 |
2516 |
2517 |
2518 |
2519 |
2520 |
2521 |
2522 |
2523 |
2524 |
2525 |
2526 |
2527 |
2528 |
2529 |
2530 |
2531 |
2532 |
2533 |
2534 |
2535 |
2536 |
2537 |
2538 |
2539 |
2540 | """
2541 |
2542 | tmpl_multiple_subst_signed_minus_offset_2 = """
2543 |
2544 |
2545 |
2546 |
2547 |
2548 |
2549 |
2550 |
2551 |
2552 |
2553 |
2554 |
2555 |
2556 |
2557 |
2558 |
2559 |
2560 |
2561 |
2562 |
2563 |
2564 |
2565 | """
2566 |
2567 | tmpl_multiple_subst_signed_plus_offset_2 = """
2568 |
2569 |
2570 |
2571 |
2572 |
2573 |
2574 |
2575 |
2576 |
2577 |
2578 |
2579 |
2580 |
2581 |
2582 |
2583 |
2584 |
2585 |
2586 |
2587 |
2588 |
2589 |
2590 | """
2591 |
2592 | soup = BeautifulSoup(gsub_tmpl, features="xml")
2593 |
2594 | seen_lookup_ambiguous = {}
2595 | seen_lookup_init_fini = {}
2596 | seen_lookup_init_fini_mappings = {}
2597 | seen_lookup1_glyph_names = set()
2598 | emitLookup1 = EmitLookupLigatureSubst()
2599 | emitLookup2 = EmitLookupMultipleSubst()
2600 | emitLookup3 = EmitLookupLigatureSubst()
2601 | emitLookup4 = EmitLookupMultipleSubst()
2602 | emitLookup5 = EmitLookupLigatureSubst()
2603 | seen_lookup6_suffixes = {}
2604 | seen_lookup8_glyph_names = set()
2605 | emitLookup9 = EmitLookupMultipleSubst()
2606 | emitLookup10 = EmitLookupLigatureSubst()
2607 | emitLookup11 = EmitLookupMultipleSubst()
2608 | emitLookup12 = EmitLookupLigatureSubst()
2609 | for glyph_chain in glyph_chains:
2610 | logging.debug(glyph_chain)
2611 |
2612 | init_fini_joined = ",".join(
2613 | [" ".join(glyph_chain[0].asms), " ".join(glyph_chain[-1].asms) if len(glyph_chain) > 1 and not glyph_chain[-1].is_offset else ""]
2614 | )
2615 | current_lookup1_glyph_name = None
2616 | glyph_chain_offset_i = 0
2617 | for glyph in glyph_chain:
2618 | is_first_asm = True
2619 | if glyph.lookup_i == 0:
2620 | ligature_components = []
2621 | ligature_components_key = None
2622 | for asm in glyph.asms:
2623 | current_lookup1_glyph_name = glyph.name
2624 | # HACK: sort after splits doesn't guarantee counter order, but first one to assign is correct...
2625 | if current_lookup1_glyph_name not in seen_lookup_init_fini_mappings:
2626 | seen_lookup_init_fini_mappings[current_lookup1_glyph_name] = init_fini_joined
2627 | seen_lookup1_glyph_names.add(glyph.name)
2628 | nibble0 = digit_to_name(asm[0])
2629 | nibble1 = digit_to_name(asm[1])
2630 | if is_first_asm:
2631 | ligature_components_key = nibble0
2632 | if (
2633 | ligature_components_key
2634 | not in emitLookup1.ligature_set_glyphs
2635 | ):
2636 | emitLookup1.ligature_set_glyphs[ligature_components_key] = (
2637 | OrderedDict()
2638 | )
2639 | else:
2640 | ligature_components.append(nibble0)
2641 | ligature_components.append(nibble1)
2642 | is_first_asm = False
2643 |
2644 | ligature_components_joined = ",".join(ligature_components)
2645 | if ligature_components_key not in seen_lookup_init_fini:
2646 | seen_lookup_init_fini[ligature_components_key] = {}
2647 | if ligature_components_joined not in seen_lookup_init_fini[ligature_components_key]:
2648 | seen_lookup_init_fini[ligature_components_key][ligature_components_joined] = set()
2649 | if ligature_components_joined in emitLookup1.ligature_set_glyphs[ligature_components_key] and init_fini_joined not in seen_lookup_init_fini[ligature_components_key][ligature_components_joined]:
2650 | if ligature_components_key not in seen_lookup_ambiguous:
2651 | seen_lookup_ambiguous[ligature_components_key] = {}
2652 | if ligature_components_joined not in seen_lookup_ambiguous[ligature_components_key]:
2653 | seen_lookup_ambiguous[ligature_components_key][ligature_components_joined] = set()
2654 |
2655 | seen_lookup_ambiguous[ligature_components_key][ligature_components_joined].add(emitLookup1.ligature_set_glyphs[ligature_components_key][ligature_components_joined])
2656 | seen_lookup_ambiguous[ligature_components_key][ligature_components_joined].add(glyph.name)
2657 | else:
2658 | emitLookup1.ligature_set_glyphs[ligature_components_key][ligature_components_joined] = glyph.name
2659 | seen_lookup_init_fini[ligature_components_key][ligature_components_joined].add(init_fini_joined)
2660 | elif glyph.lookup_i == 1:
2661 | glyph_chain_offset_i += 1
2662 | if glyph_chain_offset_i == 4:
2663 | # Matched 16-bit address
2664 | seen_lookup8_glyph_names.add(current_lookup1_glyph_name)
2665 | elif glyph.lookup_i == 2:
2666 | if glyph.name not in seen_lookup6_suffixes:
2667 | seen_lookup6_suffixes[glyph.name] = set()
2668 | seen_lookup6_suffixes[glyph.name].add(current_lookup1_glyph_name)
2669 | else:
2670 | raise RuntimeError(f'TODO: lookup_i for "{glyph}".')
2671 |
2672 | effective_ligature_set_glyphs = {}
2673 | for key in emitLookup1.ligature_set_glyphs:
2674 | if key in seen_lookup_ambiguous:
2675 | for key2 in emitLookup1.ligature_set_glyphs[key]:
2676 | if key2 not in seen_lookup_ambiguous[key]:
2677 | if key not in effective_ligature_set_glyphs:
2678 | effective_ligature_set_glyphs[key] = {}
2679 | effective_ligature_set_glyphs[key][key2] = emitLookup1.ligature_set_glyphs[key][key2]
2680 | else:
2681 | effective_ligature_set_glyphs[key] = emitLookup1.ligature_set_glyphs[key]
2682 | emitLookup1.ligature_set_glyphs = effective_ligature_set_glyphs
2683 |
2684 | logging.debug(seen_lookup1_glyph_names)
2685 | logging.debug(emitLookup1.ligature_set_glyphs)
2686 | logging.debug(seen_lookup_ambiguous)
2687 | logging.debug(seen_lookup_init_fini)
2688 | logging.debug(seen_lookup_init_fini_mappings)
2689 |
2690 | for name in seen_lookup1_glyph_names:
2691 | emitLookup2.in_glyphs[name] = f"{name},uni200B"
2692 | for i in range(0x10):
2693 | emitLookup4.in_glyphs[f"offset_{i:01x}"] = f"offset_{i:01x},uni200A"
2694 | emitLookup9.in_glyphs[f"offset_{i:01x}_2"] = f"offset_{i:01x}_2,uni2009"
2695 | emitLookup11.in_glyphs[f"offset_{i:01x}_3"] = f"offset_{i:01x}_3,uni2008"
2696 |
2697 | emitLookup3.ligature_set_glyphs["uni200B"] = OrderedDict()
2698 | emitLookup5.ligature_set_glyphs["uni200A"] = OrderedDict()
2699 | emitLookup10.ligature_set_glyphs["uni2009"] = OrderedDict()
2700 | emitLookup12.ligature_set_glyphs["uni2008"] = OrderedDict()
2701 | for i in range(0x10):
2702 | for j in range(0x10):
2703 | emitLookup3.ligature_set_glyphs["uni200B"][
2704 | digit_to_name(i)
2705 | ] = f"offset_{i:01x}"
2706 | emitLookup5.ligature_set_glyphs["uni200A"][
2707 | digit_to_name(j)
2708 | ] = f"offset_{j:01x}_2"
2709 | emitLookup10.ligature_set_glyphs["uni2009"][
2710 | digit_to_name(j)
2711 | ] = f"offset_{j:01x}_3"
2712 | emitLookup12.ligature_set_glyphs["uni2008"][
2713 | digit_to_name(j)
2714 | ] = f"offset_{j:01x}_4"
2715 |
2716 | lookups = []
2717 | lookup_list_i = 0
2718 |
2719 | # ambiguous init fini case
2720 | lookups, lookup_list_i = emit_chain_init_fini(
2721 | seen_lookup_ambiguous,
2722 | seen_lookup_init_fini_mappings,
2723 | tmpl_chain_context_subst_init_fini_lit,
2724 | tmpl_ligature_subst_init_lit,
2725 | lookup_list_i
2726 | )
2727 |
2728 | # general init fini case
2729 | lookup0 = BeautifulSoup(
2730 | tmpl0_chain_context_subst_init_lit.replace("__i__", str(lookup_list_i)),
2731 | features="xml",
2732 | )
2733 |
2734 | lookup0_tag_extension_subst0 = lookup0.find("ExtensionSubst", {"index": "0"})
2735 | lookup0_tag_backtrack_coverage = lookup0_tag_extension_subst0.find(
2736 | "BacktrackCoverage", {"index": "2"}
2737 | )
2738 | for name in seen_lookup1_glyph_names:
2739 | tag_glyph = soup.new_tag(
2740 | "Glyph",
2741 | attrs={
2742 | "value": name,
2743 | },
2744 | )
2745 | lookup0_tag_backtrack_coverage.append(tag_glyph)
2746 | lookup0_tag_extension_subst1 = lookup0.find("ExtensionSubst", {"index": "1"})
2747 | lookup0_tag_backtrack_coverage = lookup0_tag_extension_subst1.find(
2748 | "BacktrackCoverage", {"index": "1"}
2749 | )
2750 | for name in seen_lookup1_glyph_names:
2751 | tag_glyph = soup.new_tag(
2752 | "Glyph",
2753 | attrs={
2754 | "value": name,
2755 | },
2756 | )
2757 | lookup0_tag_backtrack_coverage.append(tag_glyph)
2758 | lookup0_tag_extension_subst2 = lookup0.find("ExtensionSubst", {"index": "2"})
2759 | lookup0_tag_backtrack_coverage = lookup0_tag_extension_subst2.find(
2760 | "BacktrackCoverage", {"index": "0"}
2761 | )
2762 | for name in seen_lookup1_glyph_names:
2763 | tag_glyph = soup.new_tag(
2764 | "Glyph",
2765 | attrs={
2766 | "value": name,
2767 | },
2768 | )
2769 | lookup0_tag_backtrack_coverage.append(tag_glyph)
2770 |
2771 | lookup0_tag_extension_subst3 = lookup0.find("ExtensionSubst", {"index": "3"})
2772 | lookup0_tag_chain_context_subst = lookup0_tag_extension_subst3.find(
2773 | "ChainContextSubst"
2774 | )
2775 | lookup0_tag_coverage = lookup0_tag_chain_context_subst.find("Coverage")
2776 | chain_set_i = 0
2777 | for key in emitLookup1.ligature_set_glyphs.keys():
2778 | tag_glyph = soup.new_tag(
2779 | "Glyph",
2780 | attrs={
2781 | "value": key,
2782 | },
2783 | )
2784 | lookup0_tag_coverage.append(tag_glyph)
2785 |
2786 | gsub_chain_tmpl = """
2787 |
2788 |
2789 |
2790 |
2791 |
2792 |
2793 |
2794 | """
2795 | gsub_chain_tmpl = gsub_chain_tmpl.replace("__chain_set_i", str(chain_set_i))
2796 | gsub_chain_tmpl = gsub_chain_tmpl.replace("__i2__", str(lookup_list_i + 1))
2797 | lookup0_tag_chain_context_subst.append(gsub_chain_tmpl)
2798 | chain_set_i += 1
2799 | lookups.append([lookup0, True])
2800 | lookup_list_i += 1
2801 |
2802 | lookup1 = BeautifulSoup(
2803 | tmpl_ligature_subst_init_lit.replace("__i__", str(lookup_list_i)),
2804 | features="xml",
2805 | )
2806 | lookup1_tag_ligature_subst = lookup1.find("LigatureSubst")
2807 | for key in emitLookup1.ligature_set_glyphs.keys():
2808 | tag_ligature_set = soup.new_tag(
2809 | "LigatureSet",
2810 | attrs={
2811 | "glyph": key,
2812 | },
2813 | )
2814 | emitLookup1_ligature_set = emitLookup1.ligature_set_glyphs[key]
2815 | for ligature_key in emitLookup1_ligature_set.keys():
2816 | tag_ligature = soup.new_tag(
2817 | "Ligature",
2818 | attrs={
2819 | "components": ligature_key,
2820 | "glyph": emitLookup1_ligature_set[ligature_key],
2821 | },
2822 | )
2823 | tag_ligature_set.append(tag_ligature)
2824 | lookup1_tag_ligature_subst.append(tag_ligature_set)
2825 | lookups.append([lookup1, False])
2826 | lookup_list_i += 1
2827 |
2828 | lookup2 = BeautifulSoup(
2829 | tmpl0_multiple_subst_offset1_delim.replace("__i__", str(lookup_list_i)),
2830 | features="xml",
2831 | )
2832 | lookup2_tag_multiple_subst = lookup2.find("MultipleSubst")
2833 | for key in emitLookup2.in_glyphs.keys():
2834 | tag_substitution = soup.new_tag(
2835 | "Substitution",
2836 | attrs={
2837 | "in": key,
2838 | "out": emitLookup2.in_glyphs[key],
2839 | },
2840 | )
2841 | lookup2_tag_multiple_subst.append(tag_substitution)
2842 | lookups.append([lookup2, True])
2843 | lookup_list_i += 1
2844 |
2845 | lookup3 = BeautifulSoup(
2846 | tmpl0_ligature_subst_offset1_value.replace("__i__", str(lookup_list_i)),
2847 | features="xml",
2848 | )
2849 | lookup3_tag_ligature_subst = lookup3.find("LigatureSubst")
2850 | for key in emitLookup3.ligature_set_glyphs.keys():
2851 | tag_ligature_set = soup.new_tag(
2852 | "LigatureSet",
2853 | attrs={
2854 | "glyph": key,
2855 | },
2856 | )
2857 | emitLookup3_ligature_set = emitLookup3.ligature_set_glyphs[key]
2858 | for ligature_key in emitLookup3_ligature_set.keys():
2859 | tag_ligature = soup.new_tag(
2860 | "Ligature",
2861 | attrs={
2862 | "components": ligature_key,
2863 | "glyph": emitLookup3_ligature_set[ligature_key],
2864 | },
2865 | )
2866 | tag_ligature_set.append(tag_ligature)
2867 | lookup3_tag_ligature_subst.append(tag_ligature_set)
2868 | lookups.append([lookup3, True])
2869 | lookup_list_i += 1
2870 |
2871 | lookups.append([emit_multiple_subst(emitLookup4, lookup_list_i), True])
2872 | lookup_list_i += 1
2873 | lookups.append([emit_ligature_subst(emitLookup5, lookup_list_i), True])
2874 | lookup_list_i += 1
2875 |
2876 | lookup8 = BeautifulSoup(
2877 | tmpl2_chain_context_subst_offset3_delim.replace(
2878 | "__i__", str(lookup_list_i)
2879 | ).replace("__i2__", str(lookup_list_i + 1)),
2880 | features="xml",
2881 | )
2882 | lookup8_tag_extension_subst0 = lookup8.find("BacktrackClassDef")
2883 | for name in seen_lookup8_glyph_names:
2884 | tag_class_def = soup.new_tag(
2885 | "ClassDef",
2886 | attrs={
2887 | "glyph": name,
2888 | "class": "1",
2889 | },
2890 | )
2891 | lookup8_tag_extension_subst0.append(tag_class_def)
2892 | lookups.append([lookup8, True])
2893 | lookup_list_i += 1
2894 |
2895 | # offset 2-3
2896 | lookups.append([emit_multiple_subst(emitLookup9, lookup_list_i), False])
2897 | lookup_list_i += 1
2898 | lookups.append([emit_ligature_subst(emitLookup10, lookup_list_i), True])
2899 | lookup_list_i += 1
2900 |
2901 | # offset 3-4
2902 | lookups.append([emit_multiple_subst(emitLookup11, lookup_list_i), True])
2903 | lookup_list_i += 1
2904 | lookups.append([emit_ligature_subst(emitLookup12, lookup_list_i), True])
2905 | lookup_list_i += 1
2906 |
2907 | # fini suffixes
2908 | for suffix_key in seen_lookup6_suffixes.keys():
2909 | prefixes_u8 = []
2910 | prefixes_u16 = []
2911 | for prefix in seen_lookup6_suffixes[suffix_key]:
2912 | if prefix in seen_lookup8_glyph_names:
2913 | prefixes_u16.append(prefix)
2914 | else:
2915 | prefixes_u8.append(prefix)
2916 |
2917 | if len(prefixes_u8) > 0:
2918 | lookup6, lookup7 = emit_chain_suffixes(
2919 | prefixes_u8,
2920 | suffix_key,
2921 | tmpl1_chain_context_subst_offset2_lit,
2922 | tmpl1_multiple_subst_offset2_lit,
2923 | "2",
2924 | )
2925 | lookups.append([lookup6, True])
2926 | lookup_list_i += 1
2927 | lookups.append([lookup7, False])
2928 | lookup_list_i += 1
2929 |
2930 | if len(prefixes_u16) > 0:
2931 | lookup6, lookup7 = emit_chain_suffixes(
2932 | prefixes_u16,
2933 | suffix_key,
2934 | tmpl1_chain_context_subst_offset4_lit,
2935 | tmpl1_multiple_subst_offset2_lit,
2936 | "4",
2937 | )
2938 | lookups.append([lookup6, True])
2939 | lookup_list_i += 1
2940 | lookups.append([lookup7, False])
2941 | lookup_list_i += 1
2942 |
2943 | # signed substs
2944 | for signed_offset_2_list in [[0], list(range(1, 16))]:
2945 | for signed_nibbles in [list(range(0, 8)), list(range(8, 16))]:
2946 |
2947 | lookup0 = BeautifulSoup(
2948 | tmpl_chain_context_subst_signed.replace(
2949 | "__i__", str(lookup_list_i)
2950 | ).replace("__i2__", str(lookup_list_i + 1)),
2951 | features="xml",
2952 | )
2953 | lookup0_tag_extension_subst0 = lookup0.find(
2954 | "ExtensionSubst", {"index": "0"}
2955 | )
2956 | lookup0_tag_coverage = lookup0_tag_extension_subst0.find("Coverage")
2957 | lookup0_tag_input_coverage = lookup0_tag_extension_subst0.find(
2958 | "InputClassDef", {"index": "0"}
2959 | )
2960 | for signed_nibble in signed_nibbles:
2961 | tag_glyph = soup.new_tag(
2962 | "Glyph",
2963 | attrs={
2964 | "value": f"offset_{signed_nibble:01x}",
2965 | },
2966 | )
2967 | lookup0_tag_coverage.append(tag_glyph)
2968 | tag_class_def = soup.new_tag(
2969 | "ClassDef",
2970 | attrs={
2971 | "glyph": f"offset_{signed_nibble:01x}",
2972 | "class": "1",
2973 | },
2974 | )
2975 | lookup0_tag_input_coverage.append(tag_class_def)
2976 |
2977 | lookup0_tag_backtrack_coverage = lookup0_tag_extension_subst0.find(
2978 | "BacktrackClassDef", {"index": "0"}
2979 | )
2980 | for name in seen_signed_literals:
2981 | tag_class_def = soup.new_tag(
2982 | "ClassDef",
2983 | attrs={
2984 | "glyph": name,
2985 | "class": "1",
2986 | },
2987 | )
2988 | lookup0_tag_backtrack_coverage.append(tag_class_def)
2989 |
2990 | lookup0_tag_lookahead_coverage = lookup0_tag_extension_subst0.find(
2991 | "LookAheadClassDef", {"index": "0"}
2992 | )
2993 | for signed_offset_2 in signed_offset_2_list:
2994 | tag_class_def = soup.new_tag(
2995 | "ClassDef",
2996 | attrs={
2997 | "glyph": f"offset_{signed_offset_2:01x}_2",
2998 | "class": "1",
2999 | },
3000 | )
3001 | lookup0_tag_lookahead_coverage.append(tag_class_def)
3002 | lookups.append([lookup0, True])
3003 | lookup_list_i += 1
3004 |
3005 | lookup2 = BeautifulSoup(
3006 | tmpl_multiple_subst_signed.replace("__i__", str(lookup_list_i)),
3007 | features="xml",
3008 | )
3009 | lookup2_tag_multiple_subst = lookup2.find("MultipleSubst")
3010 | for signed_nibble in signed_nibbles:
3011 | out_sign = "plus"
3012 | out_signed_nibble = signed_nibble
3013 | if signed_nibble > 7:
3014 | out_sign = "minus"
3015 | if signed_offset_2_list[0] > 0:
3016 | out_signed_nibble = 16 - (signed_nibble + 1)
3017 | else:
3018 | out_signed_nibble = 16 - signed_nibble
3019 | tag_substitution = soup.new_tag(
3020 | "Substitution",
3021 | attrs={
3022 | "in": f"offset_{signed_nibble:01x}",
3023 | "out": f"{out_sign},offset_{out_signed_nibble:01x}_s",
3024 | },
3025 | )
3026 | lookup2_tag_multiple_subst.append(tag_substitution)
3027 | lookups.append([lookup2, False])
3028 | lookup_list_i += 1
3029 |
3030 | for sign in ["minus", "plus"]:
3031 | lookup0 = BeautifulSoup(
3032 | tmpl_chain_context_subst_signed_offset_2.replace(
3033 | "__i__", str(lookup_list_i)
3034 | ).replace("__i2__", str(lookup_list_i + 1)),
3035 | features="xml",
3036 | )
3037 | lookup0_tag_backtrack_coverage = lookup0.find(
3038 | "BacktrackClassDef", {"index": "0"}
3039 | )
3040 | tag_class_def = soup.new_tag(
3041 | "ClassDef",
3042 | attrs={
3043 | "glyph": sign,
3044 | "class": "2",
3045 | },
3046 | )
3047 | lookup0_tag_backtrack_coverage.append(tag_class_def)
3048 | lookups.append([lookup0, True])
3049 | lookup_list_i += 1
3050 |
3051 | lookup0 = BeautifulSoup(
3052 | (
3053 | tmpl_multiple_subst_signed_minus_offset_2
3054 | if sign == "minus"
3055 | else tmpl_multiple_subst_signed_plus_offset_2
3056 | ).replace("__i__", str(lookup_list_i)),
3057 | features="xml",
3058 | )
3059 | lookups.append([lookup0, False])
3060 | lookup_list_i += 1
3061 |
3062 |
3063 | # ambi fini clear
3064 | for name in seen_fini_with_asms:
3065 | emitLookup5 = EmitLookupLigatureSubst()
3066 | emitLookup5.ligature_set_glyphs[name] = OrderedDict()
3067 | for i in range(0x10):
3068 | for j in range(0x10):
3069 | emitLookup5.ligature_set_glyphs[name][",".join([digit_to_name(i),digit_to_name(j)])] = name
3070 | lookups.append([emit_ligature_subst(emitLookup5, lookup_list_i), True])
3071 | lookup_list_i += 1
3072 |
3073 | # only add chain context lookups
3074 | lookup_index = 0
3075 | tag_feature = soup.find("Feature")
3076 | tag_lookup_list = soup.find("LookupList")
3077 | for lookup in lookups:
3078 | emit_soup = lookup[0]
3079 | lookup_value = emit_soup.find("Lookup")["index"]
3080 | is_added_to_feature = lookup[1]
3081 | if is_added_to_feature:
3082 | tag_lookup_list_index = soup.new_tag(
3083 | "LookupListIndex",
3084 | attrs={
3085 | "index": lookup_index,
3086 | "value": lookup_value,
3087 | },
3088 | )
3089 | tag_feature.append(tag_lookup_list_index)
3090 | lookup_index += 1
3091 | tag_lookup_list.append(emit_soup)
3092 |
3093 | # Finally, add tables to .ttx
3094 |
3095 | formatter = UnsortedAttributes(indent=4)
3096 | gsub_updated = soup.prettify(formatter=formatter)
3097 | gsub_updated = re.sub(
3098 | re.escape(''), "", gsub_updated
3099 | )
3100 |
3101 | print("Adding tables.")
3102 | ttx_files = Path("./fontcustom").glob("fontcustom_*.ttx")
3103 | latest_ttx_file = max([f for f in ttx_files], key=lambda item: item.stat().st_mtime)
3104 | with latest_ttx_file.open("r") as f:
3105 | soup = BeautifulSoup(f.read(), features="xml")
3106 | tag_ttfont = soup.find("ttFont")
3107 | tag_ttfont.append(gpos_tmpl)
3108 | tag_ttfont.append(gsub_updated)
3109 |
3110 | tag_hmtx = soup.find("hmtx")
3111 | for mtx in tag_hmtx.find_all("mtx"):
3112 | if mtx["name"].startswith("offset_"):
3113 | prev_width = int(mtx["width"], 10)
3114 | new_lsb = (1229 - prev_width) // 2
3115 | new_width = prev_width + new_lsb
3116 | mtx["lsb"] = str(new_lsb)
3117 | mtx["width"] = str(new_width)
3118 |
3119 | with latest_ttx_file.open("w") as f:
3120 | f.write(soup.prettify(formatter=formatter))
3121 |
3122 | print("Outputting ttf.")
3123 | ttx_to_ttf()
3124 |
--------------------------------------------------------------------------------