Skip to content

Commit

Permalink
Align headers to bottom of page margin for ODT and DocX (#2128)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkbo authored Nov 29, 2024
2 parents 976328c + 243124a commit 6502c42
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 29 deletions.
32 changes: 17 additions & 15 deletions novelwriter/formats/todocx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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] = []
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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",
})
Expand Down
6 changes: 3 additions & 3 deletions novelwriter/formats/toodt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/fmtToDocX_SaveDocument_document.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
6 changes: 3 additions & 3 deletions tests/reference/fmtToOdt_SaveFlat_document.fodt
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -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">
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/fmtToOdt_SaveFull_styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
10 changes: 5 additions & 5 deletions tests/reference/mBuildDocBuild_OpenDocument_Lorem_Ipsum.fodt
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -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">
Expand Down
2 changes: 1 addition & 1 deletion tests/test_formats/test_fmt_todocx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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>'
)
Expand Down

0 comments on commit 6502c42

Please sign in to comment.