Skip to content

Commit

Permalink
Fix PDF formatting (#2104)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkbo authored Nov 15, 2024
2 parents 5a80451 + f3c9a77 commit 97312ae
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
1 change: 1 addition & 0 deletions novelwriter/core/docbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def iterBuildDocument(self, path: Path, bFormat: nwBuildFmt) -> Iterable[tuple[i

elif bFormat == nwBuildFmt.PDF:
makeObj = ToQTextDocument(self._project)
makeObj.disableAnchors()
filtered = self._setupBuild(makeObj)
makeObj.initDocument()
yield from self._iterBuild(makeObj, filtered)
Expand Down
67 changes: 40 additions & 27 deletions novelwriter/formats/toqdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@

from PyQt5.QtCore import QMarginsF, QSizeF
from PyQt5.QtGui import (
QColor, QFont, QFontMetricsF, QPageSize, QTextBlockFormat, QTextCharFormat,
QTextCursor, QTextDocument
QColor, QFont, QPageSize, QTextBlockFormat, QTextCharFormat, QTextCursor,
QTextDocument
)
from PyQt5.QtPrintSupport import QPrinter

from novelwriter import __version__
from novelwriter.constants import nwStyles, nwUnicode
from novelwriter.core.project import NWProject
from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt
Expand Down Expand Up @@ -67,7 +68,7 @@ def __init__(self, project: NWProject) -> None:
super().__init__(project)
self._document = QTextDocument()
self._document.setUndoRedoEnabled(False)
self._document.setDocumentMargin(0)
self._document.setDocumentMargin(0.0)

self._usedNotes: dict[str, int] = {}
self._usedFields: list[tuple[int, str]] = []
Expand All @@ -76,6 +77,7 @@ def __init__(self, project: NWProject) -> None:
self._bold = QFont.Weight.Bold
self._normal = QFont.Weight.Normal
self._newPage = False
self._anchors = True

self._pageSize = QPageSize(QPageSize.PageSizeId.A4)
self._pageMargins = QMarginsF(20.0, 20.0, 20.0, 20.0)
Expand Down Expand Up @@ -108,6 +110,11 @@ def setShowNewPage(self, state: bool) -> None:
self._newPage = state
return

def disableAnchors(self) -> None:
"""Disable anchors for when writing to file."""
self._anchors = False
return

##
# Class Methods
##
Expand All @@ -121,19 +128,18 @@ def initDocument(self) -> None:
self._document.clear()
self._document.setDefaultFont(self._textFont)

qMetric = QFontMetricsF(self._textFont)
mPx = qMetric.ascent() # 1 em in pixels
fPt = self._textFont.pointSizeF()
fPx = fPt*96.0/72.0 # 1 em in pixels

# Scaled Sizes
# ============

self._mHead = {
BlockTyp.TITLE: (mPx * self._marginTitle[0], mPx * self._marginTitle[1]),
BlockTyp.HEAD1: (mPx * self._marginHead1[0], mPx * self._marginHead1[1]),
BlockTyp.HEAD2: (mPx * self._marginHead2[0], mPx * self._marginHead2[1]),
BlockTyp.HEAD3: (mPx * self._marginHead3[0], mPx * self._marginHead3[1]),
BlockTyp.HEAD4: (mPx * self._marginHead4[0], mPx * self._marginHead4[1]),
BlockTyp.TITLE: (fPx * self._marginTitle[0], fPx * self._marginTitle[1]),
BlockTyp.HEAD1: (fPx * self._marginHead1[0], fPx * self._marginHead1[1]),
BlockTyp.HEAD2: (fPx * self._marginHead2[0], fPx * self._marginHead2[1]),
BlockTyp.HEAD3: (fPx * self._marginHead3[0], fPx * self._marginHead3[1]),
BlockTyp.HEAD4: (fPx * self._marginHead4[0], fPx * self._marginHead4[1]),
}

hScale = self._scaleHeads
Expand All @@ -145,12 +151,12 @@ def initDocument(self) -> None:
BlockTyp.HEAD4: (nwStyles.H_SIZES.get(4, 1.0) * fPt) if hScale else fPt,
}

self._mText = (mPx * self._marginText[0], mPx * self._marginText[1])
self._mMeta = (mPx * self._marginMeta[0], mPx * self._marginMeta[1])
self._mSep = (mPx * self._marginSep[0], mPx * self._marginSep[1])
self._mText = (fPx * self._marginText[0], fPx * self._marginText[1])
self._mMeta = (fPx * self._marginMeta[0], fPx * self._marginMeta[1])
self._mSep = (fPx * self._marginSep[0], fPx * self._marginSep[1])

self._mIndent = mPx * 2.0
self._tIndent = mPx * self._firstWidth
self._mIndent = fPx * 2.0
self._tIndent = fPx * self._firstWidth

# Text Formats
# ============
Expand Down Expand Up @@ -243,13 +249,16 @@ def saveDocument(self, path: Path) -> None:
"""Save the document as a PDF file."""
m = self._pageMargins

printer = QPrinter(QPrinter.PrinterMode.PrinterResolution)
printer = QPrinter(QPrinter.PrinterMode.HighResolution)
printer.setDocName(self._project.data.name)
printer.setCreator(f"novelWriter/{__version__}")
printer.setOutputFormat(QPrinter.OutputFormat.PdfFormat)
printer.setPageSize(self._pageSize)
printer.setPageMargins(m.left(), m.top(), m.right(), m.bottom(), QPrinter.Unit.Millimeter)
printer.setOutputFileName(str(path))

self._document.setPageSize(self._pageSize.size(QPageSize.Unit.Point))
self._document.documentLayout().setPaintDevice(printer)
self._document.setPageSize(QSizeF(printer.pageRect().size()))
self._document.print(printer)

return
Expand Down Expand Up @@ -344,18 +353,22 @@ def _insertFragments(
cFmt.setForeground(self._theme.text)
primary = None
elif fmt == TextFmt.ANM_B:
cFmt.setAnchor(True)
cFmt.setAnchorNames([data])
if self._anchors:
cFmt.setAnchor(True)
cFmt.setAnchorNames([data])
elif fmt == TextFmt.ANM_E:
cFmt.setAnchor(False)
if self._anchors:
cFmt.setAnchor(False)
elif fmt == TextFmt.ARF_B:
cFmt.setFontUnderline(True)
cFmt.setAnchor(True)
cFmt.setAnchorHref(data)
if self._anchors:
cFmt.setFontUnderline(True)
cFmt.setAnchor(True)
cFmt.setAnchorHref(data)
elif fmt == TextFmt.ARF_E:
cFmt.setFontUnderline(False)
cFmt.setAnchor(False)
cFmt.setAnchorHref("")
if self._anchors:
cFmt.setFontUnderline(False)
cFmt.setAnchor(False)
cFmt.setAnchorHref("")
elif fmt == TextFmt.HRF_B:
cFmt.setForeground(self._theme.link)
cFmt.setFontUnderline(True)
Expand Down Expand Up @@ -429,7 +442,7 @@ def _genHeadStyle(self, hType: BlockTyp, hKey: str, rFmt: QTextBlockFormat) -> T
cFmt.setForeground(self._theme.head if hCol else self._theme.text)
cFmt.setFontWeight(self._bold if self._boldHeads else self._normal)
cFmt.setFontPointSize(self._sHead.get(hType, 1.0))
if hKey:
if hKey and self._anchors:
cFmt.setAnchorNames([hKey])
cFmt.setAnchor(True)

Expand Down

0 comments on commit 97312ae

Please sign in to comment.