From 4a2a14bc059d9b724046f68c301e370a8522454d Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Fri, 29 Nov 2024 00:40:45 +0100 Subject: [PATCH 1/2] Make header margins similar in ODT and DocX files (#2120) --- novelwriter/formats/todocx.py | 32 +++++++++++++++++--------------- novelwriter/formats/toodt.py | 6 +++--- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/novelwriter/formats/todocx.py b/novelwriter/formats/todocx.py index 1a9af220..c9057eed 100644 --- a/novelwriter/formats/todocx.py +++ b/novelwriter/formats/todocx.py @@ -33,7 +33,7 @@ from pathlib import Path 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 @@ class ToDocX(Tokenizer): # 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 @@ class ToDocX(Tokenizer): 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 @@ class ToDocX(Tokenizer): 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 @@ class ToDocX(Tokenizer): }) 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 dedec194..80259cf4 100644 --- a/novelwriter/formats/toodt.py +++ b/novelwriter/formats/toodt.py @@ -666,7 +666,7 @@ class ToOdt(Tokenizer): 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 @@ class ToOdt(Tokenizer): 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 From 243124a4ba4aecb8a3391e74621ebf3d5797cf4a Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Fri, 29 Nov 2024 00:41:15 +0100 Subject: [PATCH 2/2] Update tests --- tests/reference/fmtToDocX_SaveDocument_document.xml | 2 +- tests/reference/fmtToOdt_SaveFlat_document.fodt | 6 +++--- tests/reference/fmtToOdt_SaveFull_styles.xml | 2 +- .../mBuildDocBuild_OpenDocument_Lorem_Ipsum.fodt | 10 +++++----- tests/test_formats/test_fmt_todocx.py | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/reference/fmtToDocX_SaveDocument_document.xml b/tests/reference/fmtToDocX_SaveDocument_document.xml index 4f4be68f..7ac52384 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 a8cc3808..5c824b94 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 87323ee1..a7858837 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 c95b67ee..94125e3a 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 4acab27a..3c751ebf 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" />' '' '' )