--- a/eric6/QScintilla/Exporters/ExporterPDF.py Thu Apr 08 17:27:12 2021 +0200 +++ b/eric6/QScintilla/Exporters/ExporterPDF.py Thu Apr 08 18:27:47 2021 +0200 @@ -43,7 +43,7 @@ } -class PDFStyle(object): +class PDFStyle: """ Simple class to store the values of a PDF style. """ @@ -55,7 +55,7 @@ self.font = 0 -class PDFObjectTracker(object): +class PDFObjectTracker: """ Class to conveniently handle the tracking of PDF objects so that the cross-reference table can be built (PDF1.4Ref(p39)). @@ -118,7 +118,7 @@ return xrefStart -class PDFRender(object): +class PDFRender: """ Class to manage line and page rendering. @@ -313,7 +313,7 @@ self.styleCurrent = style_ # escape these characters - if ch == ')' or ch == '(' or ch == '\\': + if ch in (')', '(', '\\'): self.segment += '\\' if ch != ' ': self.justWhiteSpace = False @@ -419,7 +419,7 @@ pdfColor = "" for component in [color.red(), color.green(), color.blue()]: c = (component * 1000 + 127) // 255 - if c == 0 or c == 1000: + if c in (0, 1000): pdfColor += "{0:d} ".format(c // 1000) else: pdfColor += "0.{0:03d} ".format(c) @@ -556,81 +556,79 @@ else: self.pr.fontSize = PDF_FONTSIZE_DEFAULT - try: - with E5OverrideCursor(): - # save file in win ansi using cp1250 - with open(filename, "w", encoding="cp1250", - errors="backslashreplace") as f: - - # initialise PDF rendering - ot = PDFObjectTracker(f) - self.pr.oT = ot - self.pr.startPDF() + with E5OverrideCursor(), open(filename, "w", encoding="cp1250", + errors="backslashreplace") as f: + # save file in win ansi using cp1250 + try: + # initialise PDF rendering + ot = PDFObjectTracker(f) + self.pr.oT = ot + self.pr.startPDF() + + # do here all the writing + lengthDoc = self.editor.length() + + if lengthDoc == 0: + self.pr.nextLine() # enable zero length docs + else: + pos = 0 + column = 0 + utf8 = self.editor.isUtf8() + utf8Ch = b"" + utf8Len = 0 - # do here all the writing - lengthDoc = self.editor.length() - - if lengthDoc == 0: - self.pr.nextLine() # enable zero length docs - else: - pos = 0 - column = 0 - utf8 = self.editor.isUtf8() - utf8Ch = b"" - utf8Len = 0 + while pos < lengthDoc: + ch = self.editor.byteAt(pos) + style = self.editor.styleAt(pos) - while pos < lengthDoc: - ch = self.editor.byteAt(pos) - style = self.editor.styleAt(pos) - - if ch == b'\t': - # expand tabs - ts = tabSize - (column % tabSize) - column += ts - self.pr.add(' ' * ts, style) - elif ch == b'\r' or ch == b'\n': - if ( - ch == b'\r' and - self.editor.byteAt(pos + 1) == b'\n' - ): - pos += 1 - # close and begin a newline... - self.pr.nextLine() - column = 0 + if ch == b'\t': + # expand tabs + ts = tabSize - (column % tabSize) + column += ts + self.pr.add(' ' * ts, style) + elif ch in (b'\r', b'\n'): + if ( + ch == b'\r' and + self.editor.byteAt(pos + 1) == b'\n' + ): + pos += 1 + # close and begin a newline... + self.pr.nextLine() + column = 0 + else: + # write the character normally... + if ord(ch) > 127 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') + self.pr.add(ch, style) + utf8Ch = b"" + utf8Len = 0 + else: + column -= 1 + # will be incremented again later else: - # write the character normally... - if ord(ch) > 127 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') - self.pr.add(ch, style) - utf8Ch = b"" - utf8Len = 0 - else: - column -= 1 - # will be incremented again later - else: - self.pr.add(ch.decode(), style) - column += 1 - - pos += 1 - - # write required stuff and close the PDF file - self.pr.endPDF() - 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))) + self.pr.add(ch.decode(), style) + column += 1 + + pos += 1 + + # write required stuff and close the PDF file + self.pr.endPDF() + 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)))