| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
from __future__ import print_function, unicode_literals |
| 4 |
__license__ = """ |
| 5 |
This file is part of GNU FreeFont. |
| 6 |
|
| 7 |
GNU FreeFont is free software: you can redistribute it and/or modify it under |
| 8 |
the terms of the GNU General Public License as published by the Free Software |
| 9 |
Foundation, either version 3 of the License, or (at your option) any later |
| 10 |
version. |
| 11 |
|
| 12 |
GNU FreeFont is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 14 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License along with |
| 17 |
GNU FreeFont. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
""" |
| 19 |
__author__ = "Emmanuel Vallois" |
| 20 |
__email__ = "[email protected]" |
| 21 |
__copyright__ = "Copyright 2011 Emmanuel Vallois" |
| 22 |
__date__ = "$Date: 2018-06-03 22:35:12 +0200 (So, 03. Jun 2018) $" |
| 23 |
__version__ = "$Revision: 3926 $" |
| 24 |
__doc__ = """ |
| 25 |
Writes in the file named by the first argument an HTML page comprising a table |
| 26 |
for testing arabic characters, their behavior and consistency with presentation |
| 27 |
forms. |
| 28 |
|
| 29 |
Runs under normal Python, version 2.7 or above. |
| 30 |
|
| 31 |
Typical usage: |
| 32 |
arabic_test.py "Arabic test page.html" |
| 33 |
""" |
| 34 |
import sys |
| 35 |
from codecs import open |
| 36 |
from string import Template |
| 37 |
from io import StringIO |
| 38 |
from unicodedata import normalize, name, unidata_version, decomposition |
| 39 |
|
| 40 |
_module_missing_msg = """Please run |
| 41 |
generate_arabic_shaping.py |
| 42 |
to generate |
| 43 |
arabic_shaping.py""" |
| 44 |
|
| 45 |
try: |
| 46 |
from arabic_shaping import joining_type |
| 47 |
except: |
| 48 |
print( _module_missing_msg, file=sys.stderr) |
| 49 |
sys.exit( 1 ) |
| 50 |
|
| 51 |
if len(sys.argv) > 1: |
| 52 |
outfile = sys.argv[1] |
| 53 |
else: |
| 54 |
outfile = 'Arabic test page.html' |
| 55 |
|
| 56 |
sys.stdout = open(outfile, 'w', 'utf-8') |
| 57 |
|
| 58 |
def uniname(char): |
| 59 |
return name(char, new_names.get(char, "<reserved-{:04X}>".format(ord(char)))) |
| 60 |
|
| 61 |
def non_positional_name(char): |
| 62 |
return uniname(char).replace(' INITIAL','').replace(' FINAL','').replace(' MEDIAL','').replace(' ISOLATED','').replace(' FORM','') |
| 63 |
|
| 64 |
arabic_ranges = list(range(0x600, 0x61B + 1)) |
| 65 |
arabic_ranges.extend(range(0x61E, 0x6FF + 1)) |
| 66 |
arabic_ranges.extend(range(0x750, 0x77F + 1)) |
| 67 |
arabic_ranges.extend(range(0x8A0, 0x8B1 + 1)) |
| 68 |
arabic_ranges.extend(range(0x8E4, 0x8FF + 1)) |
| 69 |
arabic_ranges.extend(range(0xFB50, 0xFBC1 + 1)) |
| 70 |
arabic_ranges.extend(range(0xFBD3, 0xFD3F + 1)) |
| 71 |
arabic_ranges.extend(range(0xFD50, 0xFD8F + 1)) |
| 72 |
arabic_ranges.extend(range(0xFD92, 0xFDC7 + 1)) |
| 73 |
arabic_ranges.extend(range(0xFDF0, 0xFDFD + 1)) |
| 74 |
arabic_ranges.extend(range(0xFE70, 0xFE74 + 1)) |
| 75 |
arabic_ranges.extend(range(0xFE76, 0xFEFC + 1)) |
| 76 |
|
| 77 |
unicode61_new_ranges = [0x604, 0x8A0] |
| 78 |
unicode61_new_ranges.extend(range(0x8A2, 0x8AC + 1)) |
| 79 |
unicode61_new_ranges.extend(range(0x8E4, 0x8FE + 1)) |
| 80 |
unicode62_new_ranges = [0x605, 0x8A1] |
| 81 |
unicode62_new_ranges.extend(range(0x8AD, 0x8B1 + 1)) |
| 82 |
unicode62_new_ranges.append(0x8FF) |
| 83 |
new_names = {} |
| 84 |
new_names['\u0604'] = 'ARABIC SIGN SAMVAT' |
| 85 |
new_names['\u0605'] = 'ARABIC NUMBER MARK ABOVE' |
| 86 |
new_names['\u08A0'] = 'ARABIC LETTER BEH WITH SMALL V BELOW' |
| 87 |
new_names['\u08A1'] = 'ARABIC LETTER BEH WITH HAMZA ABOVE' |
| 88 |
new_names['\u08A2'] = 'ARABIC LETTER JEEM WITH TWO DOTS ABOVE' |
| 89 |
new_names['\u08A3'] = 'ARABIC LETTER TAH WITH TWO DOTS ABOVE' |
| 90 |
new_names['\u08A4'] = 'ARABIC LETTER FEH WITH DOT BELOW AND THREE DOTS ABOVE' |
| 91 |
new_names['\u08A5'] = 'ARABIC LETTER QAF WITH DOT BELOW' |
| 92 |
new_names['\u08A6'] = 'ARABIC LETTER LAM WITH DOUBLE BAR' |
| 93 |
new_names['\u08A7'] = 'ARABIC LETTER MEEM WITH THREE DOTS ABOVE' |
| 94 |
new_names['\u08A8'] = 'ARABIC LETTER YEH WITH TWO DOTS BELOW AND HAMZA ABOVE' |
| 95 |
new_names['\u08A9'] = 'ARABIC LETTER YEH WITH TWO DOTS BELOW AND DOT ABOVE' |
| 96 |
new_names['\u08AA'] = 'ARABIC LETTER REH WITH LOOP' |
| 97 |
new_names['\u08AB'] = 'ARABIC LETTER WAW WITH DOT WITHIN' |
| 98 |
new_names['\u08AC'] = 'ARABIC LETTER ROHINGYA YEH' |
| 99 |
new_names['\u08E4'] = 'ARABIC CURLY FATHA' |
| 100 |
new_names['\u08E5'] = 'ARABIC CURLY DAMMA' |
| 101 |
new_names['\u08E6'] = 'ARABIC CURLY KASRA' |
| 102 |
new_names['\u08E7'] = 'ARABIC CURLY FATHATAN' |
| 103 |
new_names['\u08E8'] = 'ARABIC CURLY DAMMATAN' |
| 104 |
new_names['\u08E9'] = 'ARABIC CURLY KASRATAN' |
| 105 |
new_names['\u08EA'] = 'ARABIC TONE ONE DOT ABOVE' |
| 106 |
new_names['\u08EB'] = 'ARABIC TONE TWO DOTS ABOVE' |
| 107 |
new_names['\u08EC'] = 'ARABIC TONE LOOP ABOVE' |
| 108 |
new_names['\u08ED'] = 'ARABIC TONE ONE DOT BELOW' |
| 109 |
new_names['\u08EE'] = 'ARABIC TONE TWO DOTS BELOW' |
| 110 |
new_names['\u08EF'] = 'ARABIC TONE LOOP BELOW' |
| 111 |
new_names['\u08F0'] = 'ARABIC OPEN FATHATAN' |
| 112 |
new_names['\u08F1'] = 'ARABIC OPEN DAMMATAN' |
| 113 |
new_names['\u08F2'] = 'ARABIC OPEN KASRATAN' |
| 114 |
new_names['\u08F3'] = 'ARABIC SMALL HIGH WAW' |
| 115 |
new_names['\u08F4'] = 'ARABIC FATHA WITH RING' |
| 116 |
new_names['\u08F5'] = 'ARABIC FATHA WITH DOT ABOVE' |
| 117 |
new_names['\u08F6'] = 'ARABIC KASRA WITH DOT BELOW' |
| 118 |
new_names['\u08F7'] = 'ARABIC LEFT ARROWHEAD ABOVE' |
| 119 |
new_names['\u08F8'] = 'ARABIC RIGHT ARROWHEAD ABOVE' |
| 120 |
new_names['\u08F9'] = 'ARABIC LEFT ARROWHEAD BELOW' |
| 121 |
new_names['\u08FA'] = 'ARABIC RIGHT ARROWHEAD BELOW' |
| 122 |
new_names['\u08FB'] = 'ARABIC DOUBLE RIGHT ARROWHEAD ABOVE' |
| 123 |
new_names['\u08FC'] = 'ARABIC DOUBLE RIGHT ARROWHEAD ABOVE WITH DOT' |
| 124 |
new_names['\u08FD'] = 'ARABIC RIGHT ARROWHEAD ABOVE WITH DOT' |
| 125 |
new_names['\u08FE'] = 'ARABIC DAMMA WITH DOT' |
| 126 |
new_names['\u08AD'] = 'ARABIC LETTER LOW ALEF' |
| 127 |
new_names['\u08AE'] = 'ARABIC LETTER DAL WITH THREE DOTS BELOW' |
| 128 |
new_names['\u08AF'] = 'ARABIC LETTER SAD WITH THREE DOTS BELOW' |
| 129 |
new_names['\u08B0'] = 'ARABIC LETTER GAF WITH INVERTED STROKE' |
| 130 |
new_names['\u08B1'] = 'ARABIC LETTER STRAIGHT WAW' |
| 131 |
new_names['\u08FF'] = 'ARABIC MARK SIDEWAYS NOON GHUNNA' |
| 132 |
|
| 133 |
# Unicode 6.0 additions not present in Python 2.7 |
| 134 |
new_names['\u0620'] = 'ARABIC LETTER KASHMIRI YEH' |
| 135 |
new_names['\u065F'] = 'ARABIC WAVY HAMZA BELOW' |
| 136 |
new_names['\uFBB2'] = 'ARABIC SYMBOL DOT ABOVE' |
| 137 |
new_names['\uFBB3'] = 'ARABIC SYMBOL DOT BELOW' |
| 138 |
new_names['\uFBB4'] = 'ARABIC SYMBOL TWO DOTS ABOVE' |
| 139 |
new_names['\uFBB5'] = 'ARABIC SYMBOL TWO DOTS BELOW' |
| 140 |
new_names['\uFBB6'] = 'ARABIC SYMBOL THREE DOTS ABOVE' |
| 141 |
new_names['\uFBB7'] = 'ARABIC SYMBOL THREE DOTS BELOW' |
| 142 |
new_names['\uFBB8'] = 'ARABIC SYMBOL THREE DOTS POINTING DOWNWARDS ABOVE' |
| 143 |
new_names['\uFBB9'] = 'ARABIC SYMBOL THREE DOTS POINTING DOWNWARDS BELOW' |
| 144 |
new_names['\uFBBA'] = 'ARABIC SYMBOL FOUR DOTS ABOVE' |
| 145 |
new_names['\uFBBB'] = 'ARABIC SYMBOL FOUR DOTS BELOW' |
| 146 |
new_names['\uFBBC'] = 'ARABIC SYMBOL DOUBLE VERTICAL BAR BELOW' |
| 147 |
new_names['\uFBBD'] = 'ARABIC SYMBOL TWO DOTS VERTICALLY ABOVE' |
| 148 |
new_names['\uFBBE'] = 'ARABIC SYMBOL TWO DOTS VERTICALLY BELOW' |
| 149 |
new_names['\uFBBF'] = 'ARABIC SYMBOL RING' |
| 150 |
new_names['\uFBC0'] = 'ARABIC SYMBOL SMALL TAH ABOVE' |
| 151 |
new_names['\uFBC1'] = 'ARABIC SYMBOL SMALL TAH BELOW' |
| 152 |
|
| 153 |
'''Class Equiv stores the correspondence between a code point and its NFKC-normalized equivalent, |
| 154 |
for usual characters it is the character itself, for decomposable characters it is the compatibility |
| 155 |
decompostion.''' |
| 156 |
class Equiv: |
| 157 |
code_point = 0 |
| 158 |
compat = 0 |
| 159 |
def __init__(self, code_point, compat): |
| 160 |
self.code_point = code_point |
| 161 |
self.compat = compat |
| 162 |
def sort_key(self): |
| 163 |
return '{:02X}'.format(len(self.compat.lstrip(' '))) + self.compat.lstrip(' ') |
| 164 |
def __repr__(self): |
| 165 |
return 'Equiv(0x{:04X}, compat={})'.format(self.code_point, self.compat) |
| 166 |
|
| 167 |
equivs = [] |
| 168 |
for cp in arabic_ranges: |
| 169 |
normalized = normalize('NFKC', unichr(cp)) |
| 170 |
equivs.append(Equiv(cp, normalized)) |
| 171 |
# Sort our characters by length of the decomposition and by decomposition itself |
| 172 |
equivs.sort(key=Equiv.sort_key) |
| 173 |
#for e in equivs: |
| 174 |
# print(e, file=sys.stderr) |
| 175 |
|
| 176 |
contextual_form_formats = { 'isolat':'{}', 'final>':'‍{}', 'medial':'‍{}‍', 'initia':'{}‍' } |
| 177 |
contextual_forms = 'isolat', 'final>', 'medial', 'initia' |
| 178 |
current_line = {} |
| 179 |
equiv = None |
| 180 |
char = None |
| 181 |
def store_contextual_form(): |
| 182 |
# print('store_contextual_form', equiv, file=sys.stderr) |
| 183 |
compat_disp = equiv.compat |
| 184 |
if equiv.compat[0] == ' ': compat_disp = '\u00A0' + compat_disp[1:] |
| 185 |
#nonlocal current_line |
| 186 |
form_cells = StringIO() |
| 187 |
form = decomposition(char)[1:7] |
| 188 |
print('<td class="ch">{}{}</td>'.format(contextual_form_formats.get(form, '{}').format(compat_disp), |
| 189 |
'<small><br/>{}</small>'.format(ord_mul(compat_disp)) if len(compat_disp) >=2 else ''), file=form_cells) |
| 190 |
print('<td class="ch">{}<small><br />{:04X}</small></td>'.format(char, equiv.code_point), file=form_cells) |
| 191 |
#if current_line.get(form, 'not found') != 'not found': print('collision', current_line[form].rstrip(), equiv, file=stderr) |
| 192 |
current_line[form] = form_cells.getvalue() |
| 193 |
form_cells.close() |
| 194 |
|
| 195 |
table_head = ''' |
| 196 |
<table frame="box" rules="rows"> |
| 197 |
{} |
| 198 |
<colgroup><col/><col/></colgroup> |
| 199 |
<colgroup id="characterCols"><col span="2"/><col span="2"/><col span="2"/><col span="2"/></colgroup> |
| 200 |
<tr> |
| 201 |
<th rowspan="2">General<br />Unicode</th> |
| 202 |
<th rowspan="2">Name</th> |
| 203 |
<th colspan="8">Contextual Forms</th> |
| 204 |
</tr> |
| 205 |
<tr><th>Isolated</th><th>Isolated (compat)</th><th>Final</th><th>Final (compat)</th> |
| 206 |
<th>Medial</th><th>Medial (compat)</th><th>Initial</th><th>Initial (compat)</th></tr>''' |
| 207 |
|
| 208 |
def print_table(): |
| 209 |
global current_line, char |
| 210 |
def end_line(): |
| 211 |
for form in contextual_forms: |
| 212 |
print(current_line.get(form, '<td colspan="2"></td>').rstrip()) |
| 213 |
print('</tr>') |
| 214 |
current_line.clear() |
| 215 |
def print_equiv(equiv): |
| 216 |
# print('print_equiv', equiv, file=sys.stderr) |
| 217 |
cp = equiv.code_point |
| 218 |
char = unichr(cp) |
| 219 |
print('<tr{}><td>{}</td>'.format(' class="nextVersion"' if cp in unicode61_new_ranges else ' class="furtherFuture"' if cp in unicode62_new_ranges else '', |
| 220 |
'compat' if len(equiv.compat.replace(' ', '')) > 1 else '{:04X}'.format(ord(equiv.compat.lstrip()[0])))) |
| 221 |
print('<td>{}</td>'.format(non_positional_name(char))) |
| 222 |
if equiv.compat.replace(' ', '') == char: # character is not a decomposable character, or is a standalone combining mark (decomposable to space + combining mark) |
| 223 |
i = 0 |
| 224 |
for form in contextual_forms: |
| 225 |
print('<td class="ch">{}</td><td></td>'.format(contextual_form_formats[form].format(char))) |
| 226 |
i += 1 |
| 227 |
if { 'T':'isolat', 'U':'isolat', 'C':'isolat', 'R':'final>', 'D':'' }[joining_type(cp)] == form: |
| 228 |
break |
| 229 |
if i < 4: |
| 230 |
print('<td colspan="{}"></td>'.format((4 - i) * 2)) |
| 231 |
print('</tr>') |
| 232 |
else: |
| 233 |
end_line() |
| 234 |
|
| 235 |
print(table_head.format(caption)) |
| 236 |
last_equiv = None |
| 237 |
global equiv |
| 238 |
for equiv in equivs: |
| 239 |
char = unichr(equiv.code_point) |
| 240 |
|
| 241 |
if last_equiv: |
| 242 |
#special case FC03 because there is one set of plain YEH WITH HAMZA ABOVE WITH ALEF MAKSURA and one of 'uighur kirghiz' compatibility ligatures |
| 243 |
if equiv.compat.lstrip() == last_equiv.compat.lstrip() and equiv.code_point != 0xFC03: |
| 244 |
store_contextual_form() |
| 245 |
else: |
| 246 |
print_equiv(last_equiv) |
| 247 |
if equiv.compat != char: |
| 248 |
store_contextual_form() |
| 249 |
last_equiv = equiv |
| 250 |
print_equiv(last_equiv) |
| 251 |
print('</table>') |
| 252 |
|
| 253 |
def ord_mul(s): |
| 254 |
code_points = '' |
| 255 |
for c in s: |
| 256 |
code_points += '{:X} '.format(ord(c)) |
| 257 |
return code_points[:-1] |
| 258 |
|
| 259 |
html_heading = Template('''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 260 |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> |
| 261 |
<head> |
| 262 |
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> |
| 263 |
<title>$title</title> |
| 264 |
<style type="text/css"> |
| 265 |
.captionSquare { float: left; width: 2em; height: 1em; margin-right: 0.5em } |
| 266 |
caption { width: 60em; text-align: left } |
| 267 |
table { text-align: center; font-family: FreeSerif } |
| 268 |
td { padding: 10px } |
| 269 |
small { font-size: small } |
| 270 |
#characterCols { border-left: medium double black; border-right: medium double black } |
| 271 |
.nextVersion { background-color: #CCFF99 } |
| 272 |
.furtherFuture { background-color: #FFFFCC } |
| 273 |
.name { width: 10em } |
| 274 |
.ch { vertical-align: baseline; line-height: 75%; font-size: 250%; width: 1em; direction: rtl } |
| 275 |
.empty { background:#EEEEEE } |
| 276 |
</style> |
| 277 |
</head> |
| 278 |
<body> |
| 279 |
<h1>$title</h1> |
| 280 |
<p>Choose the font to test: <select onchange="changefont(this)"><option>FreeSerif</option><option>FreeSerif, bold</option><option>FreeMono</option></select></p> |
| 281 |
<script type="text/javascript">//<![CDATA[ |
| 282 |
function changefont(select) { |
| 283 |
var font = select.options.item(select.selectedIndex).value.split(', '); |
| 284 |
var bold = font.length > 1 ? font[1] == 'bold' : false; |
| 285 |
font = font[0]; |
| 286 |
var elementsToStyle = document.getElementsByClassName("ch"); |
| 287 |
|
| 288 |
for (i = 0; i < elementsToStyle.length; i++) { |
| 289 |
elementsToStyle[i].style.fontFamily = font; |
| 290 |
elementsToStyle[i].style.fontWeight = bold ? 'bold' : 'normal'; |
| 291 |
} |
| 292 |
}//]]></script>''') |
| 293 |
|
| 294 |
caption='''<caption><span class="captionSquare nextVersion"> </span> New characters in Unicode 6.1, which will be published in February 2012. |
| 295 |
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 |
| 296 |
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> |
| 297 |
for U+0604, <a href="http://std.dkuug.dk/JTC1/SC2/WG2/docs/n3882.pdf">the complete |
| 298 |
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/> |
| 299 |
<span class="captionSquare furtherFuture"> </span> Future new characters in Unicode 6.2. These can will probably be standardized this way, |
| 300 |
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, |
| 301 |
<a href="http://std.dkuug.dk/JTC1/SC2/WG2/docs/n4072.pdf">N4072 proposal</a> about U+08AD-U+08B1, and |
| 302 |
<a href="http://std.dkuug.dk/JTC1/SC2/WG2/docs/n3989.pdf">N3989 proposal</a> about U+08FF.</caption>''' |
| 303 |
|
| 304 |
def print_arabic_test_page(): |
| 305 |
print(html_heading.substitute(title='Test for Unicode Arabic range')) |
| 306 |
print_table() |
| 307 |
print('</body>') |
| 308 |
print('</html>') |
| 309 |
|
| 310 |
print_arabic_test_page() |