QScintilla/Exporters/ExporterRTF.py

changeset 945
8cd4d08fa9f6
parent 791
9ec2ac20e54e
child 1112
8a7d1b9d18db
equal deleted inserted replaced
944:1b59c4ba121e 945:8cd4d08fa9f6
19 from E5Gui import E5MessageBox 19 from E5Gui import E5MessageBox
20 20
21 from .ExporterBase import ExporterBase 21 from .ExporterBase import ExporterBase
22 22
23 import Preferences 23 import Preferences
24
24 25
25 class ExporterRTF(ExporterBase): 26 class ExporterRTF(ExporterBase):
26 """ 27 """
27 Class implementing an exporter for RTF. 28 Class implementing an exporter for RTF.
28 """ 29 """
35 RTF_COLORDEF = "\\red{0:d}\\green{1:d}\\blue{2:d};" 36 RTF_COLORDEF = "\\red{0:d}\\green{1:d}\\blue{2:d};"
36 RTF_COLORDEFCLOSE = "}" 37 RTF_COLORDEFCLOSE = "}"
37 RTF_INFOOPEN = "{\\info " 38 RTF_INFOOPEN = "{\\info "
38 RTF_INFOCLOSE = "}" 39 RTF_INFOCLOSE = "}"
39 RTF_COMMENT = "{\\comment Generated by eric5's RTF export filter.}" 40 RTF_COMMENT = "{\\comment Generated by eric5's RTF export filter.}"
40 RTF_CREATED = "{\creatim\yr%Y\mo%m\dy%d\hr%H\min%M\sec%S}" # to be used by strftime 41 RTF_CREATED = "{\creatim\yr%Y\mo%m\dy%d\hr%H\min%M\sec%S}" # to be used by strftime
41 RTF_BODYOPEN = "" 42 RTF_BODYOPEN = ""
42 RTF_BODYCLOSE = "}" 43 RTF_BODYCLOSE = "}"
43 44
44 RTF_SETFONTFACE = "\\f" 45 RTF_SETFONTFACE = "\\f"
45 RTF_SETFONTSIZE = "\\fs" 46 RTF_SETFONTSIZE = "\\fs"
53 RTF_EOLN = "\\par\n" 54 RTF_EOLN = "\\par\n"
54 RTF_TAB = "\\tab " 55 RTF_TAB = "\\tab "
55 56
56 RTF_COLOR = "#000000" 57 RTF_COLOR = "#000000"
57 58
58 def __init__(self, editor, parent = None): 59 def __init__(self, editor, parent=None):
59 """ 60 """
60 Constructor 61 Constructor
61 62
62 @param editor reference to the editor object (QScintilla.Editor.Editor) 63 @param editor reference to the editor object (QScintilla.Editor.Editor)
63 @param parent parent object of the exporter (QObject) 64 @param parent parent object of the exporter (QObject)
145 else: 146 else:
146 fgColour = self.editor.color() 147 fgColour = self.editor.color()
147 bgColour = self.editor.paper() 148 bgColour = self.editor.paper()
148 149
149 try: 150 try:
150 f = open(filename, "w", encoding = "utf-8") 151 f = open(filename, "w", encoding="utf-8")
151 152
152 styles = {} 153 styles = {}
153 fonts = {} 154 fonts = {}
154 colors = {} 155 colors = {}
155 lastStyle = "" 156 lastStyle = ""
311 utf8Len = 4 312 utf8Len = 4
312 elif (utf8Ch[0] & 0xE0) == 0xE0: 313 elif (utf8Ch[0] & 0xE0) == 0xE0:
313 utf8Len = 3 314 utf8Len = 3
314 elif (utf8Ch[0] & 0xC0) == 0xC0: 315 elif (utf8Ch[0] & 0xC0) == 0xC0:
315 utf8Len = 2 316 utf8Len = 2
316 column -= 1 # will be incremented again later 317 column -= 1 # will be incremented again later
317 elif len(utf8Ch) == utf8Len: 318 elif len(utf8Ch) == utf8Len:
318 ch = utf8Ch.decode('utf8') 319 ch = utf8Ch.decode('utf8')
319 if ord(ch) <= 0xff: 320 if ord(ch) <= 0xff:
320 f.write("\\'{0:x}".format(ord(ch))) 321 f.write("\\'{0:x}".format(ord(ch)))
321 else: 322 else:
322 f.write("\\u{0:d}\\'{1:x}".format( 323 f.write("\\u{0:d}\\'{1:x}".format(
323 ord(ch), ord(ch) & 0xFF)) 324 ord(ch), ord(ch) & 0xFF))
324 utf8Ch = b"" 325 utf8Ch = b""
325 utf8Len = 0 326 utf8Len = 0
326 else: 327 else:
327 column -= 1 # will be incremented again later 328 column -= 1 # will be incremented again later
328 else: 329 else:
329 f.write(ch.decode()) 330 f.write(ch.decode())
330 331
331 column += 1 332 column += 1
332 prevCR = ch == '\r' 333 prevCR = ch == '\r'

eric ide

mercurial