36 RTF_COLORDEF = "\\red{0:d}\\green{1:d}\\blue{2:d};" |
36 RTF_COLORDEF = "\\red{0:d}\\green{1:d}\\blue{2:d};" |
37 RTF_COLORDEFCLOSE = "}" |
37 RTF_COLORDEFCLOSE = "}" |
38 RTF_INFOOPEN = "{\\info " |
38 RTF_INFOOPEN = "{\\info " |
39 RTF_INFOCLOSE = "}" |
39 RTF_INFOCLOSE = "}" |
40 RTF_COMMENT = "{\\comment Generated by eric5's RTF export filter.}" |
40 RTF_COMMENT = "{\\comment Generated by eric5's RTF export filter.}" |
41 RTF_CREATED = "{\creatim\yr%Y\mo%m\dy%d\hr%H\min%M\sec%S}" # to be used by strftime |
41 # to be used by strftime |
|
42 RTF_CREATED = "{\creatim\yr%Y\mo%m\dy%d\hr%H\min%M\sec%S}" |
42 RTF_BODYOPEN = "" |
43 RTF_BODYOPEN = "" |
43 RTF_BODYCLOSE = "}" |
44 RTF_BODYCLOSE = "}" |
44 |
45 |
45 RTF_SETFONTFACE = "\\f" |
46 RTF_SETFONTFACE = "\\f" |
46 RTF_SETFONTSIZE = "\\fs" |
47 RTF_SETFONTSIZE = "\\fs" |
84 pos += 1 |
85 pos += 1 |
85 return pos, style[oldpos:pos] |
86 return pos, style[oldpos:pos] |
86 |
87 |
87 def __GetRTFStyleChange(self, last, current): |
88 def __GetRTFStyleChange(self, last, current): |
88 """ |
89 """ |
89 Private method to extract control words that are different between two styles. |
90 Private method to extract control words that are different between two |
|
91 styles. |
90 |
92 |
91 @param last least recently used style (string) |
93 @param last least recently used style (string) |
92 @param current current style (string) |
94 @param current current style (string) |
93 @return string containing the delta between these styles (string) |
95 @return string containing the delta between these styles (string) |
94 """ |
96 """ |
97 currentPos = 0 |
99 currentPos = 0 |
98 delta = '' |
100 delta = '' |
99 i = 0 |
101 i = 0 |
100 while i < 6: |
102 while i < 6: |
101 lastPos, lastControl = self.__GetRTFNextControl(lastPos, last) |
103 lastPos, lastControl = self.__GetRTFNextControl(lastPos, last) |
102 currentPos, currentControl = self.__GetRTFNextControl(currentPos, current) |
104 currentPos, currentControl = self.__GetRTFNextControl(currentPos, |
|
105 current) |
103 if lastControl != currentControl: |
106 if lastControl != currentControl: |
104 delta += currentControl |
107 delta += currentControl |
105 i += 1 |
108 i += 1 |
106 if delta != '': |
109 if delta != '': |
107 delta += ' ' |
110 delta += ' ' |
128 wysiwyg = Preferences.getEditorExporter("RTF/WYSIWYG") |
131 wysiwyg = Preferences.getEditorExporter("RTF/WYSIWYG") |
129 if wysiwyg: |
132 if wysiwyg: |
130 if lex: |
133 if lex: |
131 defaultFont = lex.font(QsciScintilla.STYLE_DEFAULT) |
134 defaultFont = lex.font(QsciScintilla.STYLE_DEFAULT) |
132 else: |
135 else: |
133 defaultFont = Preferences.getEditorOtherFonts("DefaultFont") |
136 defaultFont = Preferences.getEditorOtherFonts( |
|
137 "DefaultFont") |
134 else: |
138 else: |
135 defaultFont = Preferences.getEditorExporter("RTF/Font") |
139 defaultFont = Preferences.getEditorExporter("RTF/Font") |
136 fontface = defaultFont.family() |
140 fontface = defaultFont.family() |
137 fontsize = QFontInfo(defaultFont).pointSize() << 1 |
141 fontsize = QFontInfo(defaultFont).pointSize() << 1 |
138 if fontsize == 0: |
142 if fontsize == 0: |
171 if lex.description(istyle): |
175 if lex.description(istyle): |
172 font = lex.font(istyle) |
176 font = lex.font(istyle) |
173 if wysiwyg: |
177 if wysiwyg: |
174 fontKey = None |
178 fontKey = None |
175 for key, value in list(fonts.items()): |
179 for key, value in list(fonts.items()): |
176 if value.lower() == font.family().lower(): |
180 if value.lower() == \ |
|
181 font.family().lower(): |
177 fontKey = key |
182 fontKey = key |
178 break |
183 break |
179 if fontKey is None: |
184 if fontKey is None: |
180 fonts[fontCount] = font.family() |
185 fonts[fontCount] = font.family() |
181 f.write(self.RTF_FONTDEF.format( |
186 f.write(self.RTF_FONTDEF.format( |
182 fontCount, characterset, font.family())) |
187 fontCount, characterset, |
|
188 font.family())) |
183 fontKey = fontCount |
189 fontKey = fontCount |
184 fontCount += 1 |
190 fontCount += 1 |
185 lastStyle = self.RTF_SETFONTFACE + "{0:d}".format( |
191 lastStyle = self.RTF_SETFONTFACE + \ |
186 fontKey) |
192 "{0:d}".format(fontKey) |
187 else: |
193 else: |
188 lastStyle = self.RTF_SETFONTFACE + "0" |
194 lastStyle = self.RTF_SETFONTFACE + "0" |
189 |
195 |
190 if wysiwyg and QFontInfo(font).pointSize(): |
196 if wysiwyg and QFontInfo(font).pointSize(): |
191 lastStyle += self.RTF_SETFONTSIZE + \ |
197 lastStyle += self.RTF_SETFONTSIZE + \ |
192 "{0:d}".format( |
198 "{0:d}".format( |
193 QFontInfo(font).pointSize() << 1) |
199 QFontInfo(font).pointSize() << 1) |
194 else: |
200 else: |
195 lastStyle += self.RTF_SETFONTSIZE + \ |
201 lastStyle += self.RTF_SETFONTSIZE + \ |
196 "{0:d}".format(fontsize) |
202 "{0:d}".format(fontsize) |
197 |
203 |
198 sColour = lex.color(istyle) |
204 sColour = lex.color(istyle) |
229 lastStyle += self.RTF_ITALIC_ON |
235 lastStyle += self.RTF_ITALIC_ON |
230 else: |
236 else: |
231 lastStyle += self.RTF_ITALIC_OFF |
237 lastStyle += self.RTF_ITALIC_OFF |
232 styles[istyle] = lastStyle |
238 styles[istyle] = lastStyle |
233 else: |
239 else: |
234 styles[istyle] = self.RTF_SETFONTFACE + "0" + \ |
240 styles[istyle] = \ |
235 self.RTF_SETFONTSIZE + \ |
241 self.RTF_SETFONTFACE + "0" + \ |
236 "{0:d}".format(fontsize) + \ |
242 self.RTF_SETFONTSIZE + \ |
237 self.RTF_SETCOLOR + "0" + \ |
243 "{0:d}".format(fontsize) + \ |
238 self.RTF_SETBACKGROUND + "1" + \ |
244 self.RTF_SETCOLOR + "0" + \ |
239 self.RTF_BOLD_OFF + self.RTF_ITALIC_OFF |
245 self.RTF_SETBACKGROUND + "1" + \ |
|
246 self.RTF_BOLD_OFF + self.RTF_ITALIC_OFF |
240 |
247 |
241 istyle += 1 |
248 istyle += 1 |
242 else: |
249 else: |
243 styles[0] = self.RTF_SETFONTFACE + "0" + \ |
250 styles[0] = self.RTF_SETFONTFACE + "0" + \ |
244 self.RTF_SETFONTSIZE + "{0:d}".format(fontsize) + \ |
251 self.RTF_SETFONTSIZE + \ |
|
252 "{0:d}".format(fontsize) + \ |
245 self.RTF_SETCOLOR + "0" + \ |
253 self.RTF_SETCOLOR + "0" + \ |
246 self.RTF_SETBACKGROUND + "1" + \ |
254 self.RTF_SETBACKGROUND + "1" + \ |
247 self.RTF_BOLD_OFF + self.RTF_ITALIC_OFF |
255 self.RTF_BOLD_OFF + self.RTF_ITALIC_OFF |
248 |
256 |
249 f.write(self.RTF_FONTDEFCLOSE + self.RTF_COLORDEFOPEN) |
257 f.write(self.RTF_FONTDEFCLOSE + self.RTF_COLORDEFOPEN) |
276 |
284 |
277 while pos < lengthDoc: |
285 while pos < lengthDoc: |
278 ch = self.editor.byteAt(pos) |
286 ch = self.editor.byteAt(pos) |
279 style = self.editor.styleAt(pos) |
287 style = self.editor.styleAt(pos) |
280 if style != styleCurrent: |
288 if style != styleCurrent: |
281 deltaStyle = self.__GetRTFStyleChange(lastStyle, styles[style]) |
289 deltaStyle = self.__GetRTFStyleChange( |
|
290 lastStyle, styles[style]) |
282 if deltaStyle: |
291 if deltaStyle: |
283 f.write(deltaStyle) |
292 f.write(deltaStyle) |
284 styleCurrent = style |
293 styleCurrent = style |
285 lastStyle = styles[style] |
294 lastStyle = styles[style] |
286 |
295 |
335 |
344 |
336 f.write(self.RTF_BODYCLOSE) |
345 f.write(self.RTF_BODYCLOSE) |
337 f.close() |
346 f.close() |
338 except IOError as err: |
347 except IOError as err: |
339 QApplication.restoreOverrideCursor() |
348 QApplication.restoreOverrideCursor() |
340 E5MessageBox.critical(self.editor, |
349 E5MessageBox.critical( |
|
350 self.editor, |
341 self.trUtf8("Export source"), |
351 self.trUtf8("Export source"), |
342 self.trUtf8( |
352 self.trUtf8( |
343 """<p>The source could not be exported to <b>{0}</b>.</p>""" |
353 """<p>The source could not be exported to""" |
344 """<p>Reason: {1}</p>""")\ |
354 """ <b>{0}</b>.</p><p>Reason: {1}</p>""")\ |
345 .format(filename, str(err))) |
355 .format(filename, str(err))) |
346 finally: |
356 finally: |
347 QApplication.restoreOverrideCursor() |
357 QApplication.restoreOverrideCursor() |