eric6/QScintilla/Exporters/ExporterPDF.py

changeset 7771
787a6b3f8c9f
parent 7360
9190402e4505
child 7781
607a6098cb44
equal deleted inserted replaced
7770:49f3377aebf1 7771:787a6b3f8c9f
9 9
10 10
11 # This code is a port of the C++ code found in SciTE 1.74 11 # This code is a port of the C++ code found in SciTE 1.74
12 # Original code: Copyright 1998-2006 by Neil Hodgson <neilh@scintilla.org> 12 # Original code: Copyright 1998-2006 by Neil Hodgson <neilh@scintilla.org>
13 13
14 from PyQt5.QtCore import Qt 14 from PyQt5.QtGui import QFontInfo
15 from PyQt5.QtGui import QCursor, QFontInfo
16 from PyQt5.QtWidgets import QApplication
17 from PyQt5.Qsci import QsciScintilla 15 from PyQt5.Qsci import QsciScintilla
18 16
19 from E5Gui import E5MessageBox 17 from E5Gui import E5MessageBox
18 from E5Gui.E5OverrideCursor import E5OverrideCursor
20 19
21 from .ExporterBase import ExporterBase 20 from .ExporterBase import ExporterBase
22 21
23 import Preferences 22 import Preferences
24 23
435 434
436 filename = self._getFileName(self.tr("PDF Files (*.pdf)")) 435 filename = self._getFileName(self.tr("PDF Files (*.pdf)"))
437 if not filename: 436 if not filename:
438 return 437 return
439 438
439 self.editor.recolor(0, -1)
440 lex = self.editor.getLexer()
441
442 tabSize = self.editor.getEditorConfig("TabWidth")
443 if tabSize == 0:
444 tabSize = 4
445
446 # get magnification value to add to default screen font size
447 self.pr.fontSize = Preferences.getEditorExporter(
448 "PDF/Magnification")
449
450 # set font family according to face name
451 fontName = Preferences.getEditorExporter("PDF/Font")
452 self.pr.fontSet = PDF_FONT_DEFAULT
453 if fontName == "Courier":
454 self.pr.fontSet = 0
455 elif fontName == "Helvetica":
456 self.pr.fontSet = 1
457 elif fontName == "Times":
458 self.pr.fontSet = 2
459
460 # page size: height, width,
461 pageSize = Preferences.getEditorExporter("PDF/PageSize")
440 try: 462 try:
441 QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) 463 pageDimensions = PDFpageSizes[pageSize]
442 QApplication.processEvents() 464 except KeyError:
465 pageDimensions = PDFpageSizes["A4"]
466 self.pr.pageHeight = pageDimensions[0]
467 self.pr.pageWidth = pageDimensions[1]
468
469 # page margins: left, right, top, bottom
470 # < 0 to use PDF default values
471 val = Preferences.getEditorExporter("PDF/MarginLeft")
472 if val < 0:
473 self.pr.pageMargins["left"] = PDF_MARGIN_DEFAULT
474 else:
475 self.pr.pageMargins["left"] = val
476 val = Preferences.getEditorExporter("PDF/MarginRight")
477 if val < 0:
478 self.pr.pageMargins["right"] = PDF_MARGIN_DEFAULT
479 else:
480 self.pr.pageMargins["right"] = val
481 val = Preferences.getEditorExporter("PDF/MarginTop")
482 if val < 0:
483 self.pr.pageMargins["top"] = PDF_MARGIN_DEFAULT
484 else:
485 self.pr.pageMargins["top"] = val
486 val = Preferences.getEditorExporter("PDF/MarginBottom")
487 if val < 0:
488 self.pr.pageMargins["bottom"] = PDF_MARGIN_DEFAULT
489 else:
490 self.pr.pageMargins["bottom"] = val
491
492 # collect all styles available for that 'language'
493 # or the default style if no language is available...
494 if lex:
495 istyle = 0
496 while istyle <= QsciScintilla.STYLE_MAX:
497 if (istyle <= QsciScintilla.STYLE_DEFAULT or
498 istyle > QsciScintilla.STYLE_LASTPREDEFINED):
499 if (
500 lex.description(istyle) or
501 istyle == QsciScintilla.STYLE_DEFAULT
502 ):
503 style = PDFStyle()
504
505 font = lex.font(istyle)
506 if font.italic():
507 style.font |= 2
508 if font.bold():
509 style.font |= 1
510
511 colour = lex.color(istyle)
512 style.fore = self.__getPDFRGB(colour)
513 self.pr.style[istyle] = style
514
515 # grab font size from default style
516 if istyle == QsciScintilla.STYLE_DEFAULT:
517 fontSize = QFontInfo(font).pointSize()
518 if fontSize > 0:
519 self.pr.fontSize += fontSize
520 else:
521 self.pr.fontSize = PDF_FONTSIZE_DEFAULT
522
523 istyle += 1
524 else:
525 style = PDFStyle()
443 526
444 self.editor.recolor(0, -1) 527 font = Preferences.getEditorOtherFonts("DefaultFont")
445 lex = self.editor.getLexer() 528 if font.italic():
529 style.font |= 2
530 if font.bold():
531 style.font |= 1
446 532
447 tabSize = self.editor.getEditorConfig("TabWidth") 533 colour = self.editor.color()
448 if tabSize == 0: 534 style.fore = self.__getPDFRGB(colour)
449 tabSize = 4 535 self.pr.style[0] = style
536 self.pr.style[QsciScintilla.STYLE_DEFAULT] = style
450 537
451 # get magnification value to add to default screen font size 538 fontSize = QFontInfo(font).pointSize()
452 self.pr.fontSize = Preferences.getEditorExporter( 539 if fontSize > 0:
453 "PDF/Magnification") 540 self.pr.fontSize += fontSize
454
455 # set font family according to face name
456 fontName = Preferences.getEditorExporter("PDF/Font")
457 self.pr.fontSet = PDF_FONT_DEFAULT
458 if fontName == "Courier":
459 self.pr.fontSet = 0
460 elif fontName == "Helvetica":
461 self.pr.fontSet = 1
462 elif fontName == "Times":
463 self.pr.fontSet = 2
464
465 # page size: height, width,
466 pageSize = Preferences.getEditorExporter("PDF/PageSize")
467 try:
468 pageDimensions = PDFpageSizes[pageSize]
469 except KeyError:
470 pageDimensions = PDFpageSizes["A4"]
471 self.pr.pageHeight = pageDimensions[0]
472 self.pr.pageWidth = pageDimensions[1]
473
474 # page margins: left, right, top, bottom
475 # < 0 to use PDF default values
476 val = Preferences.getEditorExporter("PDF/MarginLeft")
477 if val < 0:
478 self.pr.pageMargins["left"] = PDF_MARGIN_DEFAULT
479 else: 541 else:
480 self.pr.pageMargins["left"] = val 542 self.pr.fontSize = PDF_FONTSIZE_DEFAULT
481 val = Preferences.getEditorExporter("PDF/MarginRight") 543
482 if val < 0: 544 try:
483 self.pr.pageMargins["right"] = PDF_MARGIN_DEFAULT 545 with E5OverrideCursor():
484 else:
485 self.pr.pageMargins["right"] = val
486 val = Preferences.getEditorExporter("PDF/MarginTop")
487 if val < 0:
488 self.pr.pageMargins["top"] = PDF_MARGIN_DEFAULT
489 else:
490 self.pr.pageMargins["top"] = val
491 val = Preferences.getEditorExporter("PDF/MarginBottom")
492 if val < 0:
493 self.pr.pageMargins["bottom"] = PDF_MARGIN_DEFAULT
494 else:
495 self.pr.pageMargins["bottom"] = val
496
497 # collect all styles available for that 'language'
498 # or the default style if no language is available...
499 if lex:
500 istyle = 0
501 while istyle <= QsciScintilla.STYLE_MAX:
502 if (istyle <= QsciScintilla.STYLE_DEFAULT or
503 istyle > QsciScintilla.STYLE_LASTPREDEFINED):
504 if (
505 lex.description(istyle) or
506 istyle == QsciScintilla.STYLE_DEFAULT
507 ):
508 style = PDFStyle()
509
510 font = lex.font(istyle)
511 if font.italic():
512 style.font |= 2
513 if font.bold():
514 style.font |= 1
515
516 colour = lex.color(istyle)
517 style.fore = self.__getPDFRGB(colour)
518 self.pr.style[istyle] = style
519
520 # grab font size from default style
521 if istyle == QsciScintilla.STYLE_DEFAULT:
522 fontSize = QFontInfo(font).pointSize()
523 if fontSize > 0:
524 self.pr.fontSize += fontSize
525 else:
526 self.pr.fontSize = PDF_FONTSIZE_DEFAULT
527
528 istyle += 1
529 else:
530 style = PDFStyle()
531
532 font = Preferences.getEditorOtherFonts("DefaultFont")
533 if font.italic():
534 style.font |= 2
535 if font.bold():
536 style.font |= 1
537
538 colour = self.editor.color()
539 style.fore = self.__getPDFRGB(colour)
540 self.pr.style[0] = style
541 self.pr.style[QsciScintilla.STYLE_DEFAULT] = style
542
543 fontSize = QFontInfo(font).pointSize()
544 if fontSize > 0:
545 self.pr.fontSize += fontSize
546 else:
547 self.pr.fontSize = PDF_FONTSIZE_DEFAULT
548
549 try:
550 # save file in win ansi using cp1250 546 # save file in win ansi using cp1250
551 f = open(filename, "w", encoding="cp1250", 547 f = open(filename, "w", encoding="cp1250",
552 errors="backslashreplace") 548 errors="backslashreplace")
553 549
554 # initialise PDF rendering 550 # initialise PDF rendering
614 pos += 1 610 pos += 1
615 611
616 # write required stuff and close the PDF file 612 # write required stuff and close the PDF file
617 self.pr.endPDF() 613 self.pr.endPDF()
618 f.close() 614 f.close()
619 except IOError as err: 615 except IOError as err:
620 QApplication.restoreOverrideCursor() 616 E5MessageBox.critical(
621 E5MessageBox.critical( 617 self.editor,
622 self.editor, 618 self.tr("Export source"),
623 self.tr("Export source"), 619 self.tr(
624 self.tr( 620 """<p>The source could not be exported to"""
625 """<p>The source could not be exported to""" 621 """ <b>{0}</b>.</p><p>Reason: {1}</p>""")
626 """ <b>{0}</b>.</p><p>Reason: {1}</p>""") 622 .format(filename, str(err)))
627 .format(filename, str(err)))
628 finally:
629 QApplication.restoreOverrideCursor()

eric ide

mercurial