diff --git a/novelwriter/formats/todocx.py b/novelwriter/formats/todocx.py index 1a9af220f..c9057eed2 100644 --- a/novelwriter/formats/todocx.py +++ b/novelwriter/formats/todocx.py @@ -33,7 +33,7 @@ from typing import NamedTuple from zipfile import ZIP_DEFLATED, ZipFile -from PyQt5.QtCore import QMarginsF, QSizeF +from PyQt5.QtCore import QMargins, QSize from PyQt5.QtGui import QColor from novelwriter import __version__ @@ -95,6 +95,11 @@ def _wText(parent: ET.Element, text: str) -> ET.Element: return xmlSubElem(parent, _wTag("t"), text, attrib=attrib) +def _mmToSz(value: float) -> int: + """Convert millimetres to internal margin size units""" + return int(value*20.0*72.0/25.4) + + # Cached W_VAL = _wTag("val") @@ -182,8 +187,8 @@ def __init__(self, project: NWProject) -> None: # Internal self._fontFamily = "Liberation Serif" self._fontSize = 12.0 - self._pageSize = QSizeF(210.0, 297.0) - self._pageMargins = QMarginsF(20.0, 20.0, 20.0, 20.0) + self._pageSize = QSize(_mmToSz(210.0), _mmToSz(297.0)) + self._pageMargins = QMargins(_mmToSz(20.0), _mmToSz(20.0), _mmToSz(20.0), _mmToSz(20.0)) # Data Variables self._pars: list[DocXParagraph] = [] @@ -203,8 +208,8 @@ def setPageLayout( self, width: float, height: float, top: float, bottom: float, left: float, right: float ) -> None: """Set the document page size and margins in millimetres.""" - self._pageSize = QSizeF(width, height) - self._pageMargins = QMarginsF(left, top, right, bottom) + self._pageSize = QSize(_mmToSz(width), _mmToSz(height)) + self._pageMargins = QMargins(_mmToSz(left), _mmToSz(top), _mmToSz(right), _mmToSz(bottom)) return def setHeaderFormat(self, format: str, offset: int) -> None: @@ -926,9 +931,6 @@ def _documentXml(self, hFirst: str | None, hDefault: str | None) -> str: for par in pars: par.toXml(xBody) - def szScale(value: float) -> str: - return str(int(value*2.0*72.0/2.54)) - # Write Settings xSect = xmlSubElem(xBody, _wTag("sectPr")) if hFirst and hDefault: @@ -945,16 +947,16 @@ def szScale(value: float) -> str: }) xmlSubElem(xSect, _wTag("pgSz"), attrib={ - _wTag("w"): szScale(self._pageSize.width()), - _wTag("h"): szScale(self._pageSize.height()), + _wTag("w"): str(self._pageSize.width()), + _wTag("h"): str(self._pageSize.height()), _wTag("orient"): "portrait", }) xmlSubElem(xSect, _wTag("pgMar"), attrib={ - _wTag("top"): szScale(self._pageMargins.top()), - _wTag("right"): szScale(self._pageMargins.right()), - _wTag("bottom"): szScale(self._pageMargins.bottom()), - _wTag("left"): szScale(self._pageMargins.left()), - _wTag("header"): szScale(self._pageMargins.top()/2.0), + _wTag("top"): str(self._pageMargins.top()), + _wTag("right"): str(self._pageMargins.right()), + _wTag("bottom"): str(self._pageMargins.bottom()), + _wTag("left"): str(self._pageMargins.left()), + _wTag("header"): str(self._pageMargins.top() - int(35.0*self._fontSize)), _wTag("footer"): "0", _wTag("gutter"): "0", }) diff --git a/novelwriter/formats/toodt.py b/novelwriter/formats/toodt.py index dedec194e..80259cf43 100644 --- a/novelwriter/formats/toodt.py +++ b/novelwriter/formats/toodt.py @@ -666,7 +666,7 @@ def _generateField(self, key: str, fmt: int) -> ET.Element | None: def _emToCm(self, value: float) -> str: """Converts an em value to centimetres.""" - return f"{value*2.54/72*self._fontSize:.3f}cm" + return f"{value*self._fontSize*2.54/72.0:.3f}cm" def _emToPt(self, scale: float) -> str: """Compute relative font size in points.""" @@ -694,10 +694,10 @@ def _pageStyles(self) -> None: xHead = ET.SubElement(xPage, _mkTag("style", "header-style")) ET.SubElement(xHead, _mkTag("style", "header-footer-properties"), attrib={ - _mkTag("fo", "min-height"): "0.600cm", + _mkTag("fo", "min-height"): self._emToCm(1.5), _mkTag("fo", "margin-left"): "0.000cm", _mkTag("fo", "margin-right"): "0.000cm", - _mkTag("fo", "margin-bottom"): "0.500cm", + _mkTag("fo", "margin-bottom"): self._emToCm(0.5), }) return diff --git a/tests/reference/fmtToDocX_SaveDocument_document.xml b/tests/reference/fmtToDocX_SaveDocument_document.xml index 4f4be68fe..7ac52384b 100644 --- a/tests/reference/fmtToDocX_SaveDocument_document.xml +++ b/tests/reference/fmtToDocX_SaveDocument_document.xml @@ -1432,7 +1432,7 @@ - + diff --git a/tests/reference/fmtToOdt_SaveFlat_document.fodt b/tests/reference/fmtToOdt_SaveFlat_document.fodt index a8cc3808b..5c824b942 100644 --- a/tests/reference/fmtToOdt_SaveFlat_document.fodt +++ b/tests/reference/fmtToOdt_SaveFlat_document.fodt @@ -1,13 +1,13 @@ - 2024-11-13T20:28:36 + 2024-11-29T00:34:11 novelWriter/2.6b1 Jane Smith 1234 P42DT12H34M56S Test Project - 2024-11-13T20:28:36 + 2024-11-29T00:34:11 Jane Smith @@ -76,7 +76,7 @@ - + diff --git a/tests/reference/fmtToOdt_SaveFull_styles.xml b/tests/reference/fmtToOdt_SaveFull_styles.xml index 87323ee18..a7858837d 100644 --- a/tests/reference/fmtToOdt_SaveFull_styles.xml +++ b/tests/reference/fmtToOdt_SaveFull_styles.xml @@ -66,7 +66,7 @@ - + diff --git a/tests/reference/mBuildDocBuild_OpenDocument_Lorem_Ipsum.fodt b/tests/reference/mBuildDocBuild_OpenDocument_Lorem_Ipsum.fodt index c95b67ee6..94125e3a3 100644 --- a/tests/reference/mBuildDocBuild_OpenDocument_Lorem_Ipsum.fodt +++ b/tests/reference/mBuildDocBuild_OpenDocument_Lorem_Ipsum.fodt @@ -1,13 +1,13 @@ - 2024-11-13T20:33:59 + 2024-11-29T00:39:37 novelWriter/2.6b1 lipsum.com - 50 - P0DT0H40M48S + 51 + P0DT0H40M56S Lorem Ipsum - 2024-11-13T20:33:59 + 2024-11-29T00:39:37 lipsum.com @@ -76,7 +76,7 @@ - + diff --git a/tests/test_formats/test_fmt_todocx.py b/tests/test_formats/test_fmt_todocx.py index 4acab27ac..3c751ebf6 100644 --- a/tests/test_formats/test_fmt_todocx.py +++ b/tests/test_formats/test_fmt_todocx.py @@ -628,7 +628,7 @@ def testFmtToDocX_Fields(mockGUI): '' '' '' + 'w:header="748" w:footer="0" w:gutter="0" />' '' '' )