Make header margins similar in ODT and DocX files (#2120)
This commit is contained in:
@@ -33,7 +33,7 @@ from pathlib import Path
|
|||||||
from typing import NamedTuple
|
from typing import NamedTuple
|
||||||
from zipfile import ZIP_DEFLATED, ZipFile
|
from zipfile import ZIP_DEFLATED, ZipFile
|
||||||
|
|
||||||
from PyQt5.QtCore import QMarginsF, QSizeF
|
from PyQt5.QtCore import QMargins, QSize
|
||||||
from PyQt5.QtGui import QColor
|
from PyQt5.QtGui import QColor
|
||||||
|
|
||||||
from novelwriter import __version__
|
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)
|
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
|
# Cached
|
||||||
W_VAL = _wTag("val")
|
W_VAL = _wTag("val")
|
||||||
|
|
||||||
@@ -182,8 +187,8 @@ class ToDocX(Tokenizer):
|
|||||||
# Internal
|
# Internal
|
||||||
self._fontFamily = "Liberation Serif"
|
self._fontFamily = "Liberation Serif"
|
||||||
self._fontSize = 12.0
|
self._fontSize = 12.0
|
||||||
self._pageSize = QSizeF(210.0, 297.0)
|
self._pageSize = QSize(_mmToSz(210.0), _mmToSz(297.0))
|
||||||
self._pageMargins = QMarginsF(20.0, 20.0, 20.0, 20.0)
|
self._pageMargins = QMargins(_mmToSz(20.0), _mmToSz(20.0), _mmToSz(20.0), _mmToSz(20.0))
|
||||||
|
|
||||||
# Data Variables
|
# Data Variables
|
||||||
self._pars: list[DocXParagraph] = []
|
self._pars: list[DocXParagraph] = []
|
||||||
@@ -203,8 +208,8 @@ class ToDocX(Tokenizer):
|
|||||||
self, width: float, height: float, top: float, bottom: float, left: float, right: float
|
self, width: float, height: float, top: float, bottom: float, left: float, right: float
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set the document page size and margins in millimetres."""
|
"""Set the document page size and margins in millimetres."""
|
||||||
self._pageSize = QSizeF(width, height)
|
self._pageSize = QSize(_mmToSz(width), _mmToSz(height))
|
||||||
self._pageMargins = QMarginsF(left, top, right, bottom)
|
self._pageMargins = QMargins(_mmToSz(left), _mmToSz(top), _mmToSz(right), _mmToSz(bottom))
|
||||||
return
|
return
|
||||||
|
|
||||||
def setHeaderFormat(self, format: str, offset: int) -> None:
|
def setHeaderFormat(self, format: str, offset: int) -> None:
|
||||||
@@ -926,9 +931,6 @@ class ToDocX(Tokenizer):
|
|||||||
for par in pars:
|
for par in pars:
|
||||||
par.toXml(xBody)
|
par.toXml(xBody)
|
||||||
|
|
||||||
def szScale(value: float) -> str:
|
|
||||||
return str(int(value*2.0*72.0/2.54))
|
|
||||||
|
|
||||||
# Write Settings
|
# Write Settings
|
||||||
xSect = xmlSubElem(xBody, _wTag("sectPr"))
|
xSect = xmlSubElem(xBody, _wTag("sectPr"))
|
||||||
if hFirst and hDefault:
|
if hFirst and hDefault:
|
||||||
@@ -945,16 +947,16 @@ class ToDocX(Tokenizer):
|
|||||||
})
|
})
|
||||||
|
|
||||||
xmlSubElem(xSect, _wTag("pgSz"), attrib={
|
xmlSubElem(xSect, _wTag("pgSz"), attrib={
|
||||||
_wTag("w"): szScale(self._pageSize.width()),
|
_wTag("w"): str(self._pageSize.width()),
|
||||||
_wTag("h"): szScale(self._pageSize.height()),
|
_wTag("h"): str(self._pageSize.height()),
|
||||||
_wTag("orient"): "portrait",
|
_wTag("orient"): "portrait",
|
||||||
})
|
})
|
||||||
xmlSubElem(xSect, _wTag("pgMar"), attrib={
|
xmlSubElem(xSect, _wTag("pgMar"), attrib={
|
||||||
_wTag("top"): szScale(self._pageMargins.top()),
|
_wTag("top"): str(self._pageMargins.top()),
|
||||||
_wTag("right"): szScale(self._pageMargins.right()),
|
_wTag("right"): str(self._pageMargins.right()),
|
||||||
_wTag("bottom"): szScale(self._pageMargins.bottom()),
|
_wTag("bottom"): str(self._pageMargins.bottom()),
|
||||||
_wTag("left"): szScale(self._pageMargins.left()),
|
_wTag("left"): str(self._pageMargins.left()),
|
||||||
_wTag("header"): szScale(self._pageMargins.top()/2.0),
|
_wTag("header"): str(self._pageMargins.top() - int(35.0*self._fontSize)),
|
||||||
_wTag("footer"): "0",
|
_wTag("footer"): "0",
|
||||||
_wTag("gutter"): "0",
|
_wTag("gutter"): "0",
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -666,7 +666,7 @@ class ToOdt(Tokenizer):
|
|||||||
|
|
||||||
def _emToCm(self, value: float) -> str:
|
def _emToCm(self, value: float) -> str:
|
||||||
"""Converts an em value to centimetres."""
|
"""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:
|
def _emToPt(self, scale: float) -> str:
|
||||||
"""Compute relative font size in points."""
|
"""Compute relative font size in points."""
|
||||||
@@ -694,10 +694,10 @@ class ToOdt(Tokenizer):
|
|||||||
|
|
||||||
xHead = ET.SubElement(xPage, _mkTag("style", "header-style"))
|
xHead = ET.SubElement(xPage, _mkTag("style", "header-style"))
|
||||||
ET.SubElement(xHead, _mkTag("style", "header-footer-properties"), attrib={
|
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-left"): "0.000cm",
|
||||||
_mkTag("fo", "margin-right"): "0.000cm",
|
_mkTag("fo", "margin-right"): "0.000cm",
|
||||||
_mkTag("fo", "margin-bottom"): "0.500cm",
|
_mkTag("fo", "margin-bottom"): self._emToCm(0.5),
|
||||||
})
|
})
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user