Example #1
0
 def comp(a, b):
     scripta = script_count[unicodedata2.script(a)]
     scriptb = script_count[unicodedata2.script(b)]
     if scripta == scriptb:
         if a < b:
             return -1
         else:
             return 1
     else:
         if scripta < scriptb:
             return 1
         else:
             return -1
Example #2
0
 def write_char_table(alphabet):
     fout.write("<h3>Character map</h3>\n")
     
     fout.write("<p>The following characters, grouped by Unicode script, are considered for this language pack.</p>\n")
     
     script_count = dict()
     for c in (set(alphabet['charset']) | set(char_map.keys())):
         script = unicodedata2.script(c)
         if not script in script_count:
             script_count[script] = 0
         if c in char_map:
             script_count[script] += char_map[c]
         
     max_count = 0
     for c, count in char_map.items():
         if c != SPACE:
             if count > max_count:
                 max_count = count
     last_script = None
     
     def comp(a, b):
         scripta = script_count[unicodedata2.script(a)]
         scriptb = script_count[unicodedata2.script(b)]
         if scripta == scriptb:
             if a < b:
                 return -1
             else:
                 return 1
         else:
             if scripta < scriptb:
                 return 1
             else:
                 return -1
             
     def cmp_to_key(mycmp):
         'Convert a cmp= function into a key= function'
         class K(object):
             def __init__(self, obj, *args):
                 self.obj = obj
             def __lt__(self, other):
                 return mycmp(self.obj, other.obj) < 0
             def __gt__(self, other):
                 return mycmp(self.obj, other.obj) > 0
             def __eq__(self, other):
                 return mycmp(self.obj, other.obj) == 0
             def __le__(self, other):
                 return mycmp(self.obj, other.obj) <= 0  
             def __ge__(self, other):
                 return mycmp(self.obj, other.obj) >= 0
             def __ne__(self, other):
                 return mycmp(self.obj, other.obj) != 0
         return K
     
     for c in sorted(list(set(alphabet['charset']) | set(char_map.keys())), key = cmp_to_key(comp)):
         script = unicodedata2.script(c)
         if script != last_script:
             fout.write("<h4>%s</h4>\n" % script)
         last_script = script
             
         ratio = 0.0
         if c in char_map:
             ratio = float(char_map[c]) / max_count
             if ratio > 1.0:
                 ratio = 1.0
         ratio = ratio ** 0.33
         name = '(unknown)'
         try:
             name = unicodedata.name(c)
         except ValueError:
             pass
         color = mix_colors('#ffffff', '#73d216', ratio)
         font_color = '#000'
         if c not in char_map or (char_map[c] < 10):
             font_color = '#aaa'
         css_class = ''
         if c in alphabet['lookup']:
             ci = alphabet['lookup'][c]
             if ci >= alphabet['prefix_start'] and ci < alphabet['prefix_end']:
                 css_class = 'butter'
             elif ci >= alphabet['prefix_end'] and ci < alphabet['escape_offset']:
                 css_class = 'butter-dashed'
             elif ci > alphabet['escape_offset']:
                 css_class = 'aluminium'
         else:
             css_class = 'strike'
         fout.write("<div title='%s' class='cb %s' style='color: %s; background-color: %s;'>%s</div>\n" % (name, css_class, font_color, color, c))
     fout.write("\n")