eric6/QScintilla/Exporters/ExporterRTF.py

changeset 8205
4a0f1f896341
parent 7960
e8fc383322f7
--- a/eric6/QScintilla/Exporters/ExporterRTF.py	Thu Apr 08 17:27:12 2021 +0200
+++ b/eric6/QScintilla/Exporters/ExporterRTF.py	Thu Apr 08 18:27:47 2021 +0200
@@ -124,101 +124,100 @@
         if tabSize == 0:
             tabSize = 4
         
-        try:
-            with E5OverrideCursor():
-                with open(filename, "w", encoding="utf-8") as f:
-                    styles, fontsize = self.__prepareStyles(f)
-                    
-                    lastStyle = (
-                        self.RTF_SETFONTFACE + "0" +
-                        self.RTF_SETFONTSIZE + "{0:d}".format(fontsize) +
-                        self.RTF_SETCOLOR + "0" +
-                        self.RTF_SETBACKGROUND + "1" +
-                        self.RTF_BOLD_OFF +
-                        self.RTF_ITALIC_OFF
-                    )
-                    
-                    lengthDoc = self.editor.length()
-                    prevCR = False
-                    column = 0
-                    pos = 0
-                    deltaStyle = ""
-                    styleCurrent = -1
-                    utf8 = self.editor.isUtf8()
-                    utf8Ch = b""
-                    utf8Len = 0
+        with E5OverrideCursor(), open(filename, "w", encoding="utf-8") as f:
+            try:
+                styles, fontsize = self.__prepareStyles(f)
+                
+                lastStyle = (
+                    self.RTF_SETFONTFACE + "0" +
+                    self.RTF_SETFONTSIZE + "{0:d}".format(fontsize) +
+                    self.RTF_SETCOLOR + "0" +
+                    self.RTF_SETBACKGROUND + "1" +
+                    self.RTF_BOLD_OFF +
+                    self.RTF_ITALIC_OFF
+                )
+                
+                lengthDoc = self.editor.length()
+                prevCR = False
+                column = 0
+                pos = 0
+                deltaStyle = ""
+                styleCurrent = -1
+                utf8 = self.editor.isUtf8()
+                utf8Ch = b""
+                utf8Len = 0
+                
+                while pos < lengthDoc:
+                    ch = self.editor.byteAt(pos)
+                    style = self.editor.styleAt(pos)
+                    if style != styleCurrent:
+                        deltaStyle = self.__GetRTFStyleChange(
+                            lastStyle, styles[style])
+                        if deltaStyle:
+                            f.write(deltaStyle)
+                        styleCurrent = style
+                        lastStyle = styles[style]
                     
-                    while pos < lengthDoc:
-                        ch = self.editor.byteAt(pos)
-                        style = self.editor.styleAt(pos)
-                        if style != styleCurrent:
-                            deltaStyle = self.__GetRTFStyleChange(
-                                lastStyle, styles[style])
-                            if deltaStyle:
-                                f.write(deltaStyle)
-                            styleCurrent = style
-                            lastStyle = styles[style]
-                        
-                        if ch == b'{':
-                            f.write('\\{')
-                        elif ch == b'}':
-                            f.write('\\}')
-                        elif ch == b'\\':
-                            f.write('\\\\')
-                        elif ch == b'\t':
-                            if tabs:
-                                f.write(self.RTF_TAB)
-                            else:
-                                ts = tabSize - (column % tabSize)
-                                f.write(' ' * ts)
-                                column += ts - 1
-                        elif ch == b'\n':
-                            if not prevCR:
-                                f.write(self.RTF_EOLN)
-                                column -= 1
-                        elif ch == b'\r':
+                    if ch == b'{':
+                        f.write('\\{')
+                    elif ch == b'}':
+                        f.write('\\}')
+                    elif ch == b'\\':
+                        f.write('\\\\')
+                    elif ch == b'\t':
+                        if tabs:
+                            f.write(self.RTF_TAB)
+                        else:
+                            ts = tabSize - (column % tabSize)
+                            f.write(' ' * ts)
+                            column += ts - 1
+                    elif ch == b'\n':
+                        if not prevCR:
                             f.write(self.RTF_EOLN)
                             column -= 1
+                    elif ch == b'\r':
+                        f.write(self.RTF_EOLN)
+                        column -= 1
+                    else:
+                        if ord(ch) > 0x7F and utf8:
+                            utf8Ch += ch
+                            if utf8Len == 0:
+                                if (utf8Ch[0] & 0xF0) == 0xF0:
+                                    utf8Len = 4
+                                elif (utf8Ch[0] & 0xE0) == 0xE0:
+                                    utf8Len = 3
+                                elif (utf8Ch[0] & 0xC0) == 0xC0:
+                                    utf8Len = 2
+                                column -= 1
+                                # will be incremented again later
+                            elif len(utf8Ch) == utf8Len:
+                                ch = utf8Ch.decode('utf8')
+                                if ord(ch) <= 0xff:
+                                    f.write("\\'{0:x}".format(ord(ch)))
+                                else:
+                                    f.write("\\u{0:d}\\'{1:x}".format(
+                                            ord(ch), ord(ch) & 0xFF))
+                                utf8Ch = b""
+                                utf8Len = 0
+                            else:
+                                column -= 1
+                                # will be incremented again later
                         else:
-                            if ord(ch) > 0x7F and utf8:
-                                utf8Ch += ch
-                                if utf8Len == 0:
-                                    if (utf8Ch[0] & 0xF0) == 0xF0:
-                                        utf8Len = 4
-                                    elif (utf8Ch[0] & 0xE0) == 0xE0:
-                                        utf8Len = 3
-                                    elif (utf8Ch[0] & 0xC0) == 0xC0:
-                                        utf8Len = 2
-                                    column -= 1
-                                    # will be incremented again later
-                                elif len(utf8Ch) == utf8Len:
-                                    ch = utf8Ch.decode('utf8')
-                                    if ord(ch) <= 0xff:
-                                        f.write("\\'{0:x}".format(ord(ch)))
-                                    else:
-                                        f.write("\\u{0:d}\\'{1:x}".format(
-                                                ord(ch), ord(ch) & 0xFF))
-                                    utf8Ch = b""
-                                    utf8Len = 0
-                                else:
-                                    column -= 1
-                                    # will be incremented again later
-                            else:
-                                f.write(ch.decode())
-                        
-                        column += 1
-                        prevCR = ch == b'\r'
-                        pos += 1
+                            f.write(ch.decode())
                     
-                    f.write(self.RTF_BODYCLOSE)
-        except OSError as err:
-            E5MessageBox.critical(
-                self.editor,
-                self.tr("Export source"),
-                self.tr(
-                    """<p>The source could not be exported to"""
-                    """ <b>{0}</b>.</p><p>Reason: {1}</p>""")
-                .format(filename, str(err)))
+                    column += 1
+                    prevCR = ch == b'\r'
+                    pos += 1
+                
+                f.write(self.RTF_BODYCLOSE)
+            except OSError as err:
+                E5MessageBox.critical(
+                    self.editor,
+                    self.tr("Export source"),
+                    self.tr(
+                        """<p>The source could not be exported to"""
+                        """ <b>{0}</b>.</p><p>Reason: {1}</p>""")
+                    .format(filename, str(err)))
     
     def __prepareStyles(self, f):
         """

eric ide

mercurial