Debian Codegraph @

tools/test/ranges/Arabic/unicode_joining.py

raw
1 #!/usr/bin/python
2 from __future__ import print_function, unicode_literals
3 __license__ = """
4 This file is part of GNU FreeFont.
5
6 GNU FreeFont is free software: you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free Software
8 Foundation, either version 3 of the License, or (at your option) any later
9 version.
10
11 GNU FreeFont is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along with
16 GNU FreeFont. If not, see <http://www.gnu.org/licenses/>.
17 """
18 __author__ = "Emmanuel Vallois"
19 __email__ = "[email protected]"
20 __copyright__ = "Copyright 2011 Emmanuel Vallois"
21 __date__ = "$Date: 2018-06-03 22:35:12 +0200 (So, 03. Jun 2018) $"
22 __version__ = "$Revision: 3926 $"
23 __doc__ = """
24 Writes in the file named by the first argument an HTML page comprising a table
25 for testing joining cursive script characters.
26
27 Runs under normal Python, version 2.7 or above.
28
29 Typical usage:
30 unicode_joining.py "Unicode joining test page.html"
31 """
32 import sys
33 from codecs import open
34 from string import Template
35 from collections import OrderedDict
36 from itertools import chain
37
38 _module_missing_msg = """Please run
39 generate_arabic_shaping.py
40 to generate
41 arabic_shaping.py"""
42
43 try:
44 from arabic_shaping import arabic_shapings, joining_type
45 except:
46 print( _module_missing_msg, file=sys.stderr)
47 sys.exit( 1 )
48
49 if len(sys.argv) > 1:
50 outfile = sys.argv[1]
51 else:
52 outfile = 'Unicode joining test page.html'
53
54 sys.stdout = open(outfile, 'w', 'utf-8')
55
56 class OrderedDefaultDict(OrderedDict):
57 def __missing__(self, key):
58 self[key] = rv = []
59 return rv
60 def move_to_end(self, key):
61 tmp = self[key]
62 del self[key]
63 self[key] = tmp
64
65 arabic_ranges = tuple(chain(range(0x600, 0x6FF +1), range(0x750, 0x77F +1), range(0x8A0, 0x8FF)))
66 unicode61_new_ranges = [0x604, 0x8A0]
67 unicode61_new_ranges.extend(range(0x8A2, 0x8AC + 1))
68 unicode61_new_ranges.extend(range(0x8E4, 0x8FE + 1))
69 unicode62_new_ranges = [0x605, 0x8A1]
70 unicode62_new_ranges.extend(range(0x8AD, 0x8B1 + 1))
71 unicode62_new_ranges.append(0x8FF)
72
73 shapings = filter(lambda s: s.joining_type in 'RD' and (s.joining_group != 'No_Joining_Group' or s.code_point not in arabic_ranges), arabic_shapings.values())
74 jg_shapings_arabic = OrderedDefaultDict()
75 jg_shapings_other_scripts = OrderedDefaultDict()
76 for s in shapings:
77 if s.code_point in arabic_ranges:
78 jg_shapings_arabic[s.joining_group].append(s)
79 else:
80 jg_shapings_other_scripts[s.joining_group].append(s)
81 if s.code_point == 0x62B:
82 jg_shapings_arabic.move_to_end('TEH MARBUTA')
83 jg_shapings_arabic['TEH MARBUTA GOAL']
84 elif s.code_point == 0x642:
85 jg_shapings_arabic.move_to_end('GAF')
86 jg_shapings_arabic['SWASH KAF']
87 elif s.code_point == 0x646:
88 jg_shapings_arabic['NYA']
89 elif s.code_point == 0x647:
90 jg_shapings_arabic['KNOTTED HEH']
91 jg_shapings_arabic['HEH GOAL']
92 elif s.code_point == 0x64A:
93 jg_shapings_arabic.move_to_end('FARSI YEH')
94 elif s.code_point in chain(range(0x627, 0x63A + 1), range(0x641, 0x64A + 1)):
95 jg_shapings_arabic.move_to_end(s.joining_group)
96
97 #for jg, ls in jg_shapings_arabic.items():
98 # for s in ls:
99 # print(jg, ls, file=sys.stderr)
100
101 table_head = '''
102 <table frame="box" rules="rows">
103 {}
104 <colgroup><col/><col/><col/></colgroup>
105 <colgroup id="characterCols"><col/><col/><col/><col/></colgroup>
106 <colgroup><col/></colgroup>'''
107 table_internal_title = '''<tr><td colspan="8"><h2>{}</h2></td></tr>
108 <tr>
109 <th rowspan="2">Joining Group</th>
110 <th rowspan="2">Code Point</th>
111 <th rowspan="2">Short Name</th>
112 <th colspan="5">Contextual Forms</th>
113 </tr>
114 <tr><th>Isolated</th><th>Final</th><th>Medial</th><th>Initial</th><th>Joined</th></tr>'''
115
116 def print_table():
117 contextual_form_formats = { 'isolat':'{}', 'final>':'&zwj;{}', 'medial':'&zwj;{}&zwj;', 'initia':'{}&zwj;' }
118 contextual_forms = 'isolat', 'final>', 'medial', 'initia'
119 def print_shaping(shaping, rowspan):
120 # print('print_shaping', shaping, file=sys.stderr)
121 cp = shaping.code_point
122 char = unichr(cp)
123 print('<tr{}>'.format(' class="nextVersion"' if cp in unicode61_new_ranges else ' class="furtherFuture"' if cp in unicode62_new_ranges else ''))
124 if rowspan: print('<td rowspan="{}">{}</td>'.format(rowspan, shaping.joining_group))
125 print('<td>{:04X}</td>'.format(cp))
126 print('<td>{}</td>'.format(shaping.short_name))
127 i = 0
128 for form in contextual_forms:
129 print('<td class="ch">{}</td>'.format(contextual_form_formats[form].format(char)))
130 i += 1
131 if { 'R':'final>', 'D':'' }[joining_type(cp)] == form:
132 break
133 if i < 4:
134 print('<td colspan="{}"></td>'.format(4 - i))
135 print('<td class="ch">{}</td>'.format('\u0640' * (4 - i) + char * (i - 1) + ' ' + char))
136 print('</tr>')
137
138 print(table_head.format(caption))
139 print(table_internal_title.format('Arabic'))
140 for shaping_list in jg_shapings_arabic.values():
141 rowspan = len(shaping_list)
142 for shaping in shaping_list:
143 print_shaping(shaping, rowspan)
144 rowspan = None
145
146 print(table_internal_title.format('Syriac, Nko and Mandaic'))
147 for shaping_list in jg_shapings_other_scripts.values():
148 rowspan = len(shaping_list)
149 for shaping in shaping_list:
150 print_shaping(shaping, rowspan)
151 rowspan = None
152
153 print('</table>')
154
155 html_heading = Template('''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
156 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
157 <head>
158 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
159 <title>$title</title>
160 <style type="text/css">
161 .captionSquare { float: left; width: 2em; height: 1em; margin-right: 0.5em }
162 caption { width: 60em; text-align: left }
163 table { text-align: center; font-family: FreeSerif, FreeSans }
164 td { padding: 10px }
165 small { font-size: small }
166 #characterCols { border-left: medium double black; border-right: medium double black }
167 .nextVersion { background-color: #CCFF99 }
168 .furtherFuture { background-color: #FFFFCC }
169 .name { width: 10em }
170 .ch { vertical-align: baseline; line-height: 75%; font-size: 250%; direction: rtl }
171 .empty { background:#EEEEEE }
172 </style>
173 </head>
174 <body>
175 <h1>$title</h1>
176 <p>Choose the font to test: <select onchange="changefont(this)"><option>FreeSerif</option><option>FreeSerif, bold</option><option>FreeSans</option><option>FreeMono</option></select></p>
177 <script type="text/javascript">//<![CDATA[
178 function changefont(select) {
179 var font = select.options.item(select.selectedIndex).value.split(', ');
180 var bold = font.length > 1 ? font[1] == 'bold' : false;
181 font = font[0];
182 var elementsToStyle = document.getElementsByClassName("ch");
183
184 for (i = 0; i < elementsToStyle.length; i++) {
185 elementsToStyle[i].style.fontFamily = font;
186 elementsToStyle[i].style.fontWeight = bold ? 'bold' : 'normal';
187 }
188 }//]]></script>''')
189
190 caption='''<caption><span class="captionSquare nextVersion">&nbsp;</span> New characters in Unicode 6.1, which will be published in February 2012.
191 These can be relied upon and will not change or be removed. See <a href="http://www.unicode.org/Public/6.1.0/charts/blocks//U08A0.pdf">the
192 Unicode chart for the new block <b>Arabic Extended-A</b></a>, and for more about these characters, see <a href="http://std.dkuug.dk/JTC1/SC2/WG2/docs/n3734.pdf">N3734</a>
193 for U+0604, <a href="http://std.dkuug.dk/JTC1/SC2/WG2/docs/n3882.pdf">the complete
194 proposal</a> for most characters, <a href="http://std.dkuug.dk/JTC1/SC2/WG2/docs/n3791.pdf">N3791</a> for U+08F0-U+08F3.<br/>
195 <span class="captionSquare furtherFuture">&nbsp;</span> Future new characters in Unicode 6.2. These can will probably be standardized this way,
196 but could in principle still change or be removed. See <a href="http://std.dkuug.dk/JTC1/SC2/WG2/docs/n3990.pdf">N3990, in 4.2 Orthography</a> for U+0605,
197 <a href="http://std.dkuug.dk/JTC1/SC2/WG2/docs/n4072.pdf">N4072 proposal</a> about U+08AD-U+08B1, and
198 <a href="http://std.dkuug.dk/JTC1/SC2/WG2/docs/n3989.pdf">N3989 proposal</a> about U+08FF.</caption>'''
199
200 def print_arabic_test_page():
201 print(html_heading.substitute(title='Test of Joining Characters From Unicode Cursive Scripts'))
202
203 print_table()
204 print('</body>')
205 print('</html>')
206
207 print_arabic_test_page()