Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ODT build #1477

Merged
merged 8 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions novelwriter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
__author__ = "Veronica Berglyd Olsen"
__maintainer__ = "Veronica Berglyd Olsen"
__email__ = "[email protected]"
__version__ = "2.0.7"
__hexversion__ = "0x020007f0"
__date__ = "2023-04-16"
__version__ = "2.1-beta1"
__hexversion__ = "0x020100b1"
__date__ = "2023-06-19"
__status__ = "Stable"
__domain__ = "novelwriter.io"

Expand Down
34 changes: 30 additions & 4 deletions novelwriter/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from __future__ import annotations

from PyQt5.QtCore import QCoreApplication, QT_TRANSLATE_NOOP

from novelwriter.enum import nwBuildFmt, nwItemClass, nwItemLayout, nwOutline


def trConst(tString):
"""Wrapper function for locally translating constants.
"""
return QCoreApplication.translate("Constant", tString)
def trConst(text: str) -> str:
"""Wrapper function for locally translating constants."""
return QCoreApplication.translate("Constant", text)


class nwConst:
Expand Down Expand Up @@ -225,6 +225,32 @@ class nwLabels:
nwBuildFmt.J_HTML: ".json",
nwBuildFmt.J_NWD: ".json",
}
UNIT_NAME = {
"mm": QT_TRANSLATE_NOOP("Constant", "Millimetres"),
"cm": QT_TRANSLATE_NOOP("Constant", "Centimetres"),
"in": QT_TRANSLATE_NOOP("Constant", "Inches"),
}
UNIT_SCALE = {
"mm": 1.0,
"cm": 10.0,
"in": 25.4,
}
PAPER_NAME = {
"A4": QT_TRANSLATE_NOOP("Constant", "A4"),
"A5": QT_TRANSLATE_NOOP("Constant", "A5"),
"A6": QT_TRANSLATE_NOOP("Constant", "A6"),
"Legal": QT_TRANSLATE_NOOP("Constant", "US Legal"),
"Letter": QT_TRANSLATE_NOOP("Constant", "US Letter"),
"Custom": QT_TRANSLATE_NOOP("Constant", "Custom"),
}
PAPER_SIZE = {
"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),
}

# END Class nwLabels

Expand Down
29 changes: 23 additions & 6 deletions novelwriter/core/buildsettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@
"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),
}
Expand Down Expand Up @@ -99,14 +107,23 @@
"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"),
"format.grpOptions": QT_TRANSLATE_NOOP("Builds", "Text Options"),
"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"),
Expand Down Expand Up @@ -160,7 +177,7 @@ def name(self) -> str:

@property
def buildID(self) -> str:
"""The build ID as an UUID."""
"""The build ID as a UUID."""
return self._uuid

@property
Expand Down Expand Up @@ -196,24 +213,24 @@ def getLabel(key: str) -> str:

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
Expand Down
12 changes: 12 additions & 0 deletions novelwriter/core/docbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
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
Expand Down Expand Up @@ -290,6 +291,17 @@ def _setupBuild(self, bldObj: Tokenizer) -> dict:
bldObj.setColourHeaders(self._build.getBool("odt.addColours"))
bldObj.setLanguage(buildLang)

scale = nwLabels.UNIT_SCALE.get(self._build.getStr("format.pageUnit"), 1.0)
pW, pH = nwLabels.PAPER_SIZE.get(self._build.getStr("format.pageSize"), (-1.0, -1.0))
bldObj.setPageLayout(
pW if pW > 0.0 else scale*self._build.getFloat("format.pageWidth"),
pH if pH > 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")
)
Expand Down
53 changes: 45 additions & 8 deletions novelwriter/core/toodt.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def __init__(self, project: NWProject, isFlat: bool):
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"
Expand All @@ -166,11 +166,13 @@ def __init__(self, project: NWProject, isFlat: bool):
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
Expand Down Expand Up @@ -200,6 +202,19 @@ def setColourHeaders(self, state: bool):
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
##
Expand Down Expand Up @@ -454,10 +469,10 @@ def doConvert(self):
self._addTextPar("Heading_20_4", oStyle, tHead, isHead=True, oLevel="4")

elif tType == self.T_SEP:
self._addTextPar("Text_20_body", oStyle, tText)
self._addTextPar("Separator", oStyle, tText)

elif tType == self.T_SKIP:
self._addTextPar("Text_20_body", oStyle, "")
self._addTextPar("Separator", oStyle, "")

elif tType == self.T_TEXT:
if parStyle is None:
Expand Down Expand Up @@ -721,10 +736,13 @@ def _pageStyles(self):
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"))
Expand Down Expand Up @@ -869,6 +887,25 @@ def _useableStyles(self):

self._mainPara["Title"] = oStyle

# Add Separator Style
# ===================

oStyle = ODTParagraphStyle()
oStyle.setDisplayName("Separator")
oStyle.setParentStyleName("Standard")
oStyle.setNextStyleName("Text_20_body")
oStyle.setClass("text")
oStyle.setTextAlign("center")
oStyle.setMarginTop(self._mTopText)
oStyle.setMarginBottom(self._mBotText)
oStyle.setLineHeight(self._fLineHeight)
oStyle.setFontName(self._textFont)
oStyle.setFontFamily(self._fontFamily)
oStyle.setFontSize(self._fSizeText)
oStyle.packXML(self._xStyl, "Separator")

self._mainPara["Separator"] = oStyle

# Add Heading 1 Style
# ===================

Expand Down
Loading