Add support for page layout settings in builds
This commit is contained in:
@@ -71,6 +71,14 @@ SETTINGS_TEMPLATE = {
|
||||
"format.justifyText": (bool, False),
|
||||
"format.stripUnicode": (bool, False),
|
||||
"format.replaceTabs": (bool, False),
|
||||
"format.pageUnit": (str, "cm"),
|
||||
"format.pageSize": (str, "A4"),
|
||||
"format.pageWidth": (float, 21.0),
|
||||
"format.pageHeight": (float, 29.7),
|
||||
"format.topMargin": (float, 2.0),
|
||||
"format.bottomMargin": (float, 2.0),
|
||||
"format.leftMargin": (float, 2.0),
|
||||
"format.rightMargin": (float, 2.0),
|
||||
"odt.addColours": (bool, True),
|
||||
"html.addStyles": (bool, True),
|
||||
}
|
||||
@@ -99,7 +107,7 @@ SETTINGS_LABELS = {
|
||||
"text.addNoteHeadings": QT_TRANSLATE_NOOP("Builds", "Add Titles for Notes"),
|
||||
|
||||
"format.grpFormat": QT_TRANSLATE_NOOP("Builds", "Text Format"),
|
||||
"format.buildLang": QT_TRANSLATE_NOOP("Builds", "Document Language"),
|
||||
"format.buildLang": QT_TRANSLATE_NOOP("Builds", "Language"),
|
||||
"format.textFont": QT_TRANSLATE_NOOP("Builds", "Font Family"),
|
||||
"format.textSize": QT_TRANSLATE_NOOP("Builds", "Font Size"),
|
||||
"format.lineHeight": QT_TRANSLATE_NOOP("Builds", "Line Height"),
|
||||
@@ -107,6 +115,15 @@ SETTINGS_LABELS = {
|
||||
"format.justifyText": QT_TRANSLATE_NOOP("Builds", "Justify Text Margins"),
|
||||
"format.stripUnicode": QT_TRANSLATE_NOOP("Builds", "Replace Unicode Characters"),
|
||||
"format.replaceTabs": QT_TRANSLATE_NOOP("Builds", "Replace Tabs with Spaces"),
|
||||
"format.grpPage": QT_TRANSLATE_NOOP("Builds", "Page Layout"),
|
||||
"format.pageUnit": QT_TRANSLATE_NOOP("Builds", "Unit"),
|
||||
"format.pageSize": QT_TRANSLATE_NOOP("Builds", "Page Size"),
|
||||
"format.pageWidth": QT_TRANSLATE_NOOP("Builds", "Page Width"),
|
||||
"format.pageHeight": QT_TRANSLATE_NOOP("Builds", "Page Height"),
|
||||
"format.topMargin": QT_TRANSLATE_NOOP("Builds", "Top Margin"),
|
||||
"format.bottomMargin": QT_TRANSLATE_NOOP("Builds", "Bottom Margin"),
|
||||
"format.leftMargin": QT_TRANSLATE_NOOP("Builds", "Left Margin"),
|
||||
"format.rightMargin": QT_TRANSLATE_NOOP("Builds", "Right Margin"),
|
||||
|
||||
"odt": QT_TRANSLATE_NOOP("Builds", "Open Document"),
|
||||
"odt.addColours": QT_TRANSLATE_NOOP("Builds", "Add Highlight Colours"),
|
||||
@@ -115,6 +132,15 @@ SETTINGS_LABELS = {
|
||||
"html.addStyles": QT_TRANSLATE_NOOP("Builds", "Add CSS Styles"),
|
||||
}
|
||||
|
||||
PAGE_SIZES = {
|
||||
"A4": (210.0, 297.0),
|
||||
"A5": (148.0, 210.0),
|
||||
"A6": (105.0, 148.0),
|
||||
"Legal": (215.9, 355.6),
|
||||
"Letter": (215.9, 279.4),
|
||||
"Custom": (-1.0, -1.0),
|
||||
}
|
||||
|
||||
|
||||
class FilterMode(Enum):
|
||||
"""The decision reason for an item in a filtered project."""
|
||||
@@ -160,7 +186,7 @@ class BuildSettings:
|
||||
|
||||
@property
|
||||
def buildID(self) -> str:
|
||||
"""The build ID as an UUID."""
|
||||
"""The build ID as a UUID."""
|
||||
return self._uuid
|
||||
|
||||
@property
|
||||
@@ -196,24 +222,24 @@ class BuildSettings:
|
||||
|
||||
def getStr(self, key: str) -> str:
|
||||
"""Type safe value access for strings."""
|
||||
value = self._settings.get(key, SETTINGS_TEMPLATE.get(key, (None, None)[1]))
|
||||
value = self._settings.get(key, SETTINGS_TEMPLATE.get(key, (None, None))[1])
|
||||
return str(value)
|
||||
|
||||
def getBool(self, key: str) -> bool:
|
||||
"""Type safe value access for bools."""
|
||||
value = self._settings.get(key, SETTINGS_TEMPLATE.get(key, (None, None)[1]))
|
||||
value = self._settings.get(key, SETTINGS_TEMPLATE.get(key, (None, None))[1])
|
||||
return bool(value)
|
||||
|
||||
def getInt(self, key: str) -> int:
|
||||
"""Type safe value access for integers."""
|
||||
value = self._settings.get(key, SETTINGS_TEMPLATE.get(key, (None, None)[1]))
|
||||
value = self._settings.get(key, SETTINGS_TEMPLATE.get(key, (None, None))[1])
|
||||
if isinstance(value, (int, float)):
|
||||
return int(value)
|
||||
return 0
|
||||
|
||||
def getFloat(self, key: str) -> float:
|
||||
"""Type safe value access for floats."""
|
||||
value = self._settings.get(key, SETTINGS_TEMPLATE.get(key, (None, None)[1]))
|
||||
value = self._settings.get(key, SETTINGS_TEMPLATE.get(key, (None, None))[1])
|
||||
if isinstance(value, (int, float)):
|
||||
return float(value)
|
||||
return 0.0
|
||||
|
||||
@@ -33,13 +33,14 @@ from PyQt5.QtGui import QFont, QFontInfo
|
||||
from novelwriter import CONFIG
|
||||
from novelwriter.enum import nwBuildFmt
|
||||
from novelwriter.error import formatException, logException
|
||||
from novelwriter.constants import nwLabels
|
||||
from novelwriter.core.item import NWItem
|
||||
from novelwriter.core.tomd import ToMarkdown
|
||||
from novelwriter.core.toodt import ToOdt
|
||||
from novelwriter.core.tohtml import ToHtml
|
||||
from novelwriter.core.project import NWProject
|
||||
from novelwriter.core.tokenizer import Tokenizer
|
||||
from novelwriter.core.buildsettings import BuildSettings
|
||||
from novelwriter.core.buildsettings import PAGE_SIZES, BuildSettings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -290,6 +291,17 @@ class NWBuildDocument:
|
||||
bldObj.setColourHeaders(self._build.getBool("odt.addColours"))
|
||||
bldObj.setLanguage(buildLang)
|
||||
|
||||
scale = nwLabels.UNIT_SCALE.get(self._build.getStr("format.pageUnit"), 1.0)
|
||||
width, height = PAGE_SIZES.get(self._build.getStr("format.pageSize"), (-1.0, -1.0))
|
||||
bldObj.setPageLayout(
|
||||
width if width > 0.0 else scale*self._build.getFloat("format.pageWidth"),
|
||||
height if height > 0.0 else scale*self._build.getFloat("format.pageHeight"),
|
||||
scale*self._build.getFloat("format.topMargin"),
|
||||
scale*self._build.getFloat("format.bottomMargin"),
|
||||
scale*self._build.getFloat("format.leftMargin"),
|
||||
scale*self._build.getFloat("format.rightMargin"),
|
||||
)
|
||||
|
||||
filtered = self._build.buildItemFilter(
|
||||
self._project, withRoots=self._build.getBool("text.addNoteHeadings")
|
||||
)
|
||||
|
||||
@@ -147,7 +147,7 @@ class ToOdt(Tokenizer):
|
||||
self._dLanguage = "en"
|
||||
self._dCountry = "GB"
|
||||
|
||||
# Text Margings in Units of em
|
||||
# Text Margings
|
||||
self._mTopTitle = "0.423cm"
|
||||
self._mTopHead1 = "0.423cm"
|
||||
self._mTopHead2 = "0.353cm"
|
||||
@@ -166,11 +166,13 @@ class ToOdt(Tokenizer):
|
||||
self._mBotText = "0.247cm"
|
||||
self._mBotMeta = "0.106cm"
|
||||
|
||||
# Document Margins
|
||||
self._mDocTop = "2.000cm"
|
||||
self._mDocBtm = "2.000cm"
|
||||
self._mDocLeft = "2.000cm"
|
||||
self._mDocRight = "2.000cm"
|
||||
# Document Size and Margins
|
||||
self._mDocWidth = "21.0cm"
|
||||
self._mDocHeight = "29.7cm"
|
||||
self._mDocTop = "2.000cm"
|
||||
self._mDocBtm = "2.000cm"
|
||||
self._mDocLeft = "2.000cm"
|
||||
self._mDocRight = "2.000cm"
|
||||
|
||||
# Colour
|
||||
self._colHead12 = None
|
||||
@@ -200,6 +202,19 @@ class ToOdt(Tokenizer):
|
||||
self._colourHead = state
|
||||
return
|
||||
|
||||
def setPageLayout(
|
||||
self, width: int | float, height: int | float,
|
||||
top: int | float, bottom: int | float, left: int | float, right: int | float
|
||||
):
|
||||
"""Set the document page size and margins in millimetres."""
|
||||
self._mDocWidth = f"{width/10.0:.3f}cm"
|
||||
self._mDocHeight = f"{height/10.0:.3f}cm"
|
||||
self._mDocTop = f"{top/10.0:.3f}cm"
|
||||
self._mDocBtm = f"{bottom/10.0:.3f}cm"
|
||||
self._mDocLeft = f"{left/10.0:.3f}cm"
|
||||
self._mDocRight = f"{right/10.0:.3f}cm"
|
||||
return
|
||||
|
||||
##
|
||||
# Class Methods
|
||||
##
|
||||
@@ -721,10 +736,13 @@ class ToOdt(Tokenizer):
|
||||
xPage = ET.SubElement(self._xAut2, _mkTag("style", "page-layout"), attrib=tAttr)
|
||||
|
||||
tAttr = {}
|
||||
tAttr[_mkTag("fo", "page-width")] = self._mDocWidth
|
||||
tAttr[_mkTag("fo", "page-height")] = self._mDocHeight
|
||||
tAttr[_mkTag("fo", "margin-top")] = self._mDocTop
|
||||
tAttr[_mkTag("fo", "margin-bottom")] = self._mDocBtm
|
||||
tAttr[_mkTag("fo", "margin-left")] = self._mDocLeft
|
||||
tAttr[_mkTag("fo", "margin-right")] = self._mDocRight
|
||||
tAttr[_mkTag("fo", "print-orientation")] = "portrait"
|
||||
ET.SubElement(xPage, _mkTag("style", "page-layout-properties"), attrib=tAttr)
|
||||
|
||||
xHead = ET.SubElement(xPage, _mkTag("style", "header-style"))
|
||||
|
||||
Reference in New Issue
Block a user