Align headers to bottom of page margin for ODT and DocX (#2128)

This commit is contained in:
Veronica Berglyd Olsen
2024-11-29 12:52:50 +01:00
committed by GitHub
7 changed files with 31 additions and 29 deletions
+17 -15
View File
@@ -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",
})
+3 -3
View File
@@ -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
@@ -1432,7 +1432,7 @@
<w:numFmt w:val="decimal" />
</w:footnotePr>
<w:pgSz w:w="11905" w:h="16837" w:orient="portrait" />
<w:pgMar w:top="1133" w:right="1133" w:bottom="1133" w:left="1133" w:header="566" w:footer="0" w:gutter="0" />
<w:pgMar w:top="1133" w:right="1133" w:bottom="1133" w:left="1133" w:header="713" w:footer="0" w:gutter="0" />
<w:pgNumType w:start="1" w:fmt="decimal" />
<w:titlePg />
</w:sectPr>
@@ -1,13 +1,13 @@
<?xml version='1.0' encoding='utf-8'?>
<office:document xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" office:version="1.3" office:mimetype="application/vnd.oasis.opendocument.text">
<office:meta>
<meta:creation-date>2024-11-13T20:28:36</meta:creation-date>
<meta:creation-date>2024-11-29T00:34:11</meta:creation-date>
<meta:generator>novelWriter/2.6b1</meta:generator>
<meta:initial-creator>Jane Smith</meta:initial-creator>
<meta:editing-cycles>1234</meta:editing-cycles>
<meta:editing-duration>P42DT12H34M56S</meta:editing-duration>
<dc:title>Test Project</dc:title>
<dc:date>2024-11-13T20:28:36</dc:date>
<dc:date>2024-11-29T00:34:11</dc:date>
<dc:creator>Jane Smith</dc:creator>
</office:meta>
<office:font-face-decls>
@@ -76,7 +76,7 @@
<style:page-layout style:name="PM1">
<style:page-layout-properties fo:page-width="14.800cm" fo:page-height="21.000cm" fo:margin-top="2.000cm" fo:margin-bottom="1.800cm" fo:margin-left="1.700cm" fo:margin-right="1.500cm" />
<style:header-style>
<style:header-footer-properties fo:min-height="0.600cm" fo:margin-left="0.000cm" fo:margin-right="0.000cm" fo:margin-bottom="0.500cm" />
<style:header-footer-properties fo:min-height="0.635cm" fo:margin-left="0.000cm" fo:margin-right="0.000cm" fo:margin-bottom="0.212cm" />
</style:header-style>
</style:page-layout>
<style:style style:name="P1" style:family="paragraph" style:parent-style-name="Heading_20_2">
+1 -1
View File
@@ -66,7 +66,7 @@
<style:page-layout style:name="PM1">
<style:page-layout-properties fo:page-width="21.0cm" fo:page-height="29.7cm" fo:margin-top="2.000cm" fo:margin-bottom="2.000cm" fo:margin-left="2.000cm" fo:margin-right="2.000cm" />
<style:header-style>
<style:header-footer-properties fo:min-height="0.600cm" fo:margin-left="0.000cm" fo:margin-right="0.000cm" fo:margin-bottom="0.500cm" />
<style:header-footer-properties fo:min-height="0.635cm" fo:margin-left="0.000cm" fo:margin-right="0.000cm" fo:margin-bottom="0.212cm" />
</style:header-style>
</style:page-layout>
</office:automatic-styles>
@@ -1,13 +1,13 @@
<?xml version='1.0' encoding='utf-8'?>
<office:document xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" office:version="1.3" office:mimetype="application/vnd.oasis.opendocument.text">
<office:meta>
<meta:creation-date>2024-11-13T20:33:59</meta:creation-date>
<meta:creation-date>2024-11-29T00:39:37</meta:creation-date>
<meta:generator>novelWriter/2.6b1</meta:generator>
<meta:initial-creator>lipsum.com</meta:initial-creator>
<meta:editing-cycles>50</meta:editing-cycles>
<meta:editing-duration>P0DT0H40M48S</meta:editing-duration>
<meta:editing-cycles>51</meta:editing-cycles>
<meta:editing-duration>P0DT0H40M56S</meta:editing-duration>
<dc:title>Lorem Ipsum</dc:title>
<dc:date>2024-11-13T20:33:59</dc:date>
<dc:date>2024-11-29T00:39:37</dc:date>
<dc:creator>lipsum.com</dc:creator>
</office:meta>
<office:font-face-decls>
@@ -76,7 +76,7 @@
<style:page-layout style:name="PM1">
<style:page-layout-properties fo:page-width="21.000cm" fo:page-height="29.700cm" fo:margin-top="2.000cm" fo:margin-bottom="2.000cm" fo:margin-left="2.000cm" fo:margin-right="2.000cm" />
<style:header-style>
<style:header-footer-properties fo:min-height="0.600cm" fo:margin-left="0.000cm" fo:margin-right="0.000cm" fo:margin-bottom="0.500cm" />
<style:header-footer-properties fo:min-height="0.635cm" fo:margin-left="0.000cm" fo:margin-right="0.000cm" fo:margin-bottom="0.212cm" />
</style:header-style>
</style:page-layout>
<style:style style:name="P1" style:family="paragraph" style:parent-style-name="Text_20_body">
+1 -1
View File
@@ -628,7 +628,7 @@ def testFmtToDocX_Fields(mockGUI):
'<w:footnotePr><w:numFmt w:val="decimal" /></w:footnotePr>'
'<w:pgSz w:w="11905" w:h="16837" w:orient="portrait" />'
'<w:pgMar w:top="1133" w:right="1133" w:bottom="1133" w:left="1133" '
'w:header="566" w:footer="0" w:gutter="0" />'
'w:header="748" w:footer="0" w:gutter="0" />'
'<w:pgNumType w:start="1" w:fmt="decimal" /><w:titlePg />'
'</w:sectPr></w:body></w:document>'
)