summaryrefslogtreecommitdiffstats
path: root/lib/kformula/prototype/unicode.py
blob: 6b725b5def26117f1a696afd6482c3c32f232787 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python

import sys
from TQt.tqt import *
from xml.sax import saxutils, handler, make_parser

class Form1(TQWidget):
    def __init__(self,parent = None,name = None,fl = 0):
        TQWidget.__init__(self,parent,name,fl)

        if name == None:
            self.setName('Form1')

        self.setCaption(self.tr('Form1'))
        grid = TQGridLayout(self)
        grid.setSpacing(6)
        grid.setMargin(11)

        self.chars = {}
        self.fontName = None

        begin = 32
        end = 256
        for i in range(begin, end):

            charLabel = TQLabel(self,'charLabel' + chr(i))
            charLabel.setFont(TQFont("symbol", 16))
            charLabel.setText(self.tr(chr(i)))
            grid.addWidget(charLabel, i-begin, 0)

            number = TQLineEdit(self,'Number' + chr(i))
            grid.addWidget(number, i-begin, 1)

            latexName = TQLineEdit(self,'latexName' + chr(i))
            grid.addWidget(latexName, i-begin, 2)

            charClass = TQLineEdit(self,'charClass' + chr(i))
            grid.addWidget(charClass, i-begin, 3)

            self.chars[i] = (charLabel, number, latexName, charClass)

    def fontList(self):
        list = []
        for i in self.chars:
            charLabel, number, latexName, charClass = self.chars[i]
            if str(number.text()) != "" or str(latexName.text()) != "" or str(charClass.text()) != "":
                list.append((i, str(number.text()), str(latexName.text()), str(charClass.text())))
        return list

    def setFont(self, fontName, font):
        fontName = fontName.replace("%20", " ")
        self.fontName = fontName
        for i in self.chars:
            charLabel, number, latexName, charClass = self.chars[i]
            charLabel.setFont(TQFont(fontName, 16))
            number.setText("")
            latexName.setText("")
            charClass.setText("")

        for (key, number, latexName, charClass) in font:
            i = int(key)
            charLabel, numberWidget, latexNameWidget, charClassWidget = self.chars[i]
            numberWidget.setText(number)
            latexNameWidget.setText(latexName)
            charClassWidget.setText(charClass)


class Widget(TQWidget):

    def __init__(self):
        TQWidget.__init__(self)

        vbox = TQVBoxLayout(self)
        vbox.setSpacing(6)
        vbox.setMargin(0)

        hbox = TQHBoxLayout()
        hbox.setSpacing(6)
        hbox.setMargin(0)

        loadButton = TQPushButton("load", self)
        saveButton = TQPushButton("save", self)

        TQObject.connect(loadButton, TQ_SIGNAL("pressed()"), self.load)
        TQObject.connect(saveButton, TQ_SIGNAL("pressed()"), self.save)

        hbox.addWidget(loadButton)
        hbox.addWidget(saveButton)

        vbox.addLayout(hbox)

        splitter = TQSplitter(self)
        splitter.setOrientation(TQt.Vertical)

        self.listbox = TQListBox(splitter)

        sv = TQScrollView(splitter)
        big_box = TQVBox(sv.viewport())
        sv.addChild(big_box, 0, 0)
        self.child = Form1(big_box)

        vbox.addWidget(splitter)

        self.connect(self.listbox, TQ_SIGNAL('highlighted( const TQString& )'),
                     self.fontHighlighted)

    def fontHighlighted(self, fontStr):
        if self.child.fontName:
            self.fonts[self.child.fontName] = self.child.fontList()

        font = str(fontStr)
        self.child.setFont(font, self.fonts[font])

    def load(self):
        self.fonts = {}
        parser = make_parser()
        parser.setContentHandler(ContentGenerator(self.fonts))
        parser.parse("symbol.xml")

        self.listbox.clear()
        for font in self.fonts:
            self.listbox.insertItem(font)
        self.listbox.sort()

    def save(self):
        if self.child.fontName:
            self.fonts[self.child.fontName] = self.child.fontList()

        f = open("symbol.xml", "w")
        print('<?xml version="1.0" encoding="iso-8859-1"?>', file=f)
        print('<table>', file=f)
        for font in self.fonts:
            print('  <unicodetable font="' + font + '">', file=f)
            for (key, number, latexName, charClass) in self.fonts[font]:
                if not charClass or charClass == '':
                    charClass = 'ORDINARY'
                print('    <entry key="' + str(key) + \
                      '" number="' + str(number) + \
                      '" name="' + str(latexName) + \
                      '" class="' + str(charClass) + \
                      '"/>', file=f)

            print('  </unicodetable>', file=f)
        print('</table>', file=f)
        f.close()


class ContentGenerator(handler.ContentHandler):
    def __init__(self, fonts):
        handler.ContentHandler.__init__(self)
        self.fonts = fonts
        self.currentFont = None

    def startElement(self, name, attrs):
        if name == 'unicodetable':
            for (name, value) in list(attrs.items()):
                if name == "font":
                    self.currentFont = value
                    self.fonts[self.currentFont] = []
        elif name == 'entry':
            if not self.currentFont:
                raise "entry must belong to a font"
            for (name, value) in list(attrs.items()):
                if name == "key":
                    if len(value) > 1 and value[:2] == "0x":
                        key = int(value[2:], 16)
                    else:
                        key = int(value)
                elif name == "number": number = value
                elif name == "name": latexName = value
                elif name == "class": charClass = value

            self.fonts[self.currentFont].append((key, number, latexName, charClass))
            #numberWidget, latexNameWidget, charClassWidget = self.widgets[key]
            #numberWidget.setText(number)
            #latexNameWidget.setText(latexName)
            #charClassWidget.setText(charClass)


def main():
    a = TQApplication(sys.argv)

    mw = Widget()
    mw.setCaption('Unicode mapping util')
    mw.show()

    a.connect(a, TQ_SIGNAL('lastWindowClosed()'), a, TQ_SLOT('quit()'))
    a.exec_loop()

if __name__ == '__main__':

    main()