F:/KPlato/koffice/libs/kformula/prototype/gensymbolfontmap.py

Aller à la documentation de ce fichier.
00001 #!/usr/bin/env python
00002 
00003 import time
00004 from xml.sax import saxutils, handler, make_parser
00005 
00006 unicodetable = { "normal":{}, "bold":{}, "italic":{},
00007                  "slant":{}, "boldItalic":{} }
00008 fonttable = {}
00009 
00010 class ContentGenerator(handler.ContentHandler):  
00011 
00012     def __init__(self):
00013         handler.ContentHandler.__init__(self)
00014         self.fontfont = None
00015         
00016     def startElement(self, name, attrs):
00017         if name == 'unicodetable':
00018             self.fontfont = None
00019             for (name, value) in attrs.items():
00020                 if name == "font" and value:
00021                     self.fontfont = value
00022                     if value not in fonttable:
00023                         fonttable[value] = []
00024         elif self.fontfont and name == 'entry':
00025             number = ''
00026             for (name, value) in attrs.items():
00027                 if name == "key": key = int(value)
00028                 elif name == "number": number = value
00029                 elif name == "name": latexName = value
00030                 elif name == "class": charClass = value
00031                 elif name == "style": style = value
00032 
00033             if number != '':
00034                 unicodetable[style][number] = (latexName, charClass)
00035                 fonttable[self.fontfont].append((key, number, style))
00036 
00037 def fontkey(font, style, number):
00038     for mapping in fonttable[font]:
00039         k, n, s = mapping
00040         if s == style and n == number:
00041             return k
00042 
00043 
00044 def writeFontTable(fontname, f):
00045     f.write('\n\nstatic InternFontTable ' + fontname + 'Map[] = {\n')
00046     for style in unicodetable:
00047         for key in unicodetable[style]:
00048             latexName, charClass = unicodetable[style][key]
00049             pos = fontkey(fontname, style, key)
00050             if pos:
00051                 f.write('    { ' + key + ', ' + hex(pos) + ', ' + charClass + ', ' + style + 'Char },\n')
00052     f.write('    { 0, 0, ORDINARY, normalChar }\n};\n\n')
00053 
00054 
00055 def write_header(f):
00056     print >>f, '''//
00057 // Created: ''' + time.ctime(time.time()) + '''
00058 //      by: gensymbolfontmap.py
00059 //    from: symbol.xml
00060 //
00061 // WARNING! All changes made in this file will be lost!
00062 '''
00063     
00064 def main():
00065     f = open('../symbolfontmapping.cc', 'w')
00066     write_header(f)
00067     writeFontTable("symbol", f)
00068     f.close()
00069     
00070     f = open('../esstixfontmapping.cc', 'w')
00071     write_header(f)
00072     fontnames = [ "esstixnine", 
00073                   "esstixthirteen", 
00074                   "esstixeleven", 
00075                   "esstixfourteen", 
00076                   "esstixfive", 
00077                   "esstixfifteen", 
00078                   "esstixeight", 
00079                   "esstixthree", 
00080                   "esstixten", 
00081                   "esstixsixteen", 
00082                   "esstixone", 
00083                   "esstixtwo", 
00084                   "esstixsix", 
00085                   "esstixseven", 
00086                   "esstixtwelve", 
00087                   "esstixseventeen", 
00088                   "esstixfour" ]
00089     for fn in fontnames:
00090         writeFontTable(fn, f)
00091     f.close()
00092 
00093     f = open('../cmmapping.cc', 'w')
00094     write_header(f)
00095     fontnames = [ "cmbx10", 
00096                   "cmex10", 
00097                   "cmmi10", 
00098                   "cmr10", 
00099                   #"cmsl10", 
00100                   "cmsy10", 
00101                   #"cmti10", 
00102                   #"cmtt10", 
00103                   "msam10",
00104                   "msbm10"
00105                   ]
00106     for fn in fontnames:
00107         writeFontTable(fn, f)
00108     f.close()
00109 
00110     f = open('../unicodenames.cc', 'w')
00111     write_header(f)
00112     print >>f, 'struct UnicodeNameTable { short unicode; const char* name; };'
00113     print >>f, 'static UnicodeNameTable nameTable[] = {'
00114     nameDir = {}
00115     table = {}
00116     for style in unicodetable:
00117         table.update(unicodetable[style])
00118 
00119     for key in table:
00120         latexName, charClass = table[key]
00121         if len(latexName) > 0:
00122             #for fn in fontnames:
00123             #    if fontkey(fn, style, key):
00124                     print >>f, '    { ' + key + ', "' + latexName + '" },'
00125                     #break
00126     print >>f, '    { 0, 0 }\n};'
00127     f.close()
00128     
00129 
00130    
00131 def make_unicode_table():
00132     header = []
00133     codes = {}
00134     f = open('../config/unicode.tbl', 'r')
00135     for line in f.xreadlines():
00136         if line[0] == '#':
00137             header.append(line.strip())
00138         else:
00139             break
00140     for line in f.xreadlines():
00141         if len(line) > 0:
00142             codes[line.split(',')[0].strip()] = line
00143     f.close()
00144     
00145     for key in unicodetable:
00146         latexName, charClass = unicodetable[key]
00147         if len(latexName) > 0:
00148             codes[key] = key + ', ' + charClass + ', ' + latexName.replace('\\', '')
00149         else:
00150             codes[key] = key + ', ' + charClass
00151             
00152     f = open('../config/unicode.tbl', 'w')
00153     for line in header:
00154         print >> f, line
00155     for key in codes:
00156         print >> f, codes[key]
00157     f.close()
00158 
00159 def make_font_table(font):
00160 ##    header = []
00161 ##    try:
00162 ##        f = open('../config/' + font + '.font', 'r')
00163 ##        for line in f.xreadlines():
00164 ##            if line[0] == '#':
00165 ##                header.append(line.strip())
00166 ##            else:
00167 ##                break
00168 ##        f.close()
00169 ##    except IOError:
00170 ##        pass
00171     
00172     #f = open('../config/' + font + '.font', 'w')
00173     f = open(font + '.font', 'w')
00174 ##    for line in header:
00175 ##        print >> f, line
00176     #print >> f, "name = " + font
00177     for key in unicodetable:
00178         latexName, charClass = unicodetable[key]
00179         pos = fontkey(font, key)
00180         if pos:
00181             print >> f, str(pos), key, charClass, latexName
00182     f.close()
00183 
00184 def make_all_font_tables():
00185     for font in fonttable:
00186         make_font_table(font)
00187 
00188 
00189 def symbol_entry(pos, unicode, charClass, name):
00190     return '    <entry key="%d" number="%s" name="%s" class="%s"/>' % \
00191            (pos, unicode, name, charClass)
00192 
00193 
00194 def compare_font(font):
00195     for line in file(font+".font"):
00196         list = line.split()
00197         pos = int(list[0])
00198         unicode = list[1]
00199         charClass = list[2]
00200         if len(list)>3:
00201             name = list[3]
00202         else:
00203             name = ""
00204 
00205         if (pos, unicode) not in fonttable[font]:
00206             print "not in font", font, (pos, unicode)
00207             print symbol_entry(pos, unicode, charClass, name)
00208         if unicode not in unicodetable:
00209             print font, unicode, (name, charClass)
00210             print symbol_entry(pos, unicode, charClass, name)
00211         elif unicodetable[unicode] != (name, charClass):
00212             print font, unicode, pos, unicodetable[unicode], "!=", (name, charClass)
00213 
00214 def compare():
00215     fontnames = [ "symbol",
00216                   "esstixnine", 
00217                   "esstixthirteen", 
00218                   "esstixeleven", 
00219                   "esstixfourteen", 
00220                   "esstixfive", 
00221                   "esstixfifteen", 
00222                   "esstixeight", 
00223                   "esstixthree", 
00224                   "esstixten", 
00225                   "esstixsixteen", 
00226                   "esstixone", 
00227                   "esstixtwo", 
00228                   "esstixsix", 
00229                   "esstixseven", 
00230                   "esstixtwelve", 
00231                   "esstixseventeen", 
00232                   "esstixfour" ]
00233 
00234     for font in fontnames:
00235         compare_font(font)
00236 
00237         
00238 if __name__ == '__main__':
00239     parser = make_parser()
00240     parser.setContentHandler(ContentGenerator())
00241     parser.parse("symbol.xml")
00242 
00243     #print fonttable
00244     #print unicodetable
00245 
00246     #compare()
00247     
00248     main()
00249     #make_unicode_table()
00250     #make_all_font_tables()

Généré le Wed Nov 22 23:41:01 2006 pour KPlato par  doxygen 1.5.1-p1