Clean up font handling in HTML and ODT

This commit is contained in:
Veronica Berglyd Olsen
2024-05-22 23:13:16 +02:00
parent 411163f750
commit cf16b57f66
8 changed files with 40 additions and 37 deletions
+3 -4
View File
@@ -34,7 +34,7 @@ from novelwriter.common import formatTimeStamp
from novelwriter.constants import nwHeadFmt, nwHtmlUnicode, nwKeyWords, nwLabels
from novelwriter.core.project import NWProject
from novelwriter.core.tokenizer import T_Formats, Tokenizer, stripEscape
from novelwriter.types import FONT_STRETCH, FONT_STYLE, FONT_WEIGHTS
from novelwriter.types import FONT_STYLE, FONT_WEIGHTS
logger = logging.getLogger(__name__)
@@ -377,13 +377,12 @@ class ToHtml(Tokenizer):
font = self._textFont
styles.append((
"body {{"
"font-family: '{0:s}'; font-size: {1:d}pt; font-weight: {2:d}; "
"font-stretch: {3:s}; font-style: {4:s};"
"font-family: '{0:s}'; font-size: {1:d}pt; "
"font-weight: {2:d}; font-style: {3:s};"
"}}"
).format(
font.family(), font.pointSize(),
FONT_WEIGHTS.get(font.weight(), 400),
FONT_STRETCH.get(font.stretch(), "normal"),
FONT_STYLE.get(font.style(), "normal"),
))
styles.append((
+11 -7
View File
@@ -42,7 +42,7 @@ from novelwriter.common import xmlIndent
from novelwriter.constants import nwHeadFmt, nwKeyWords, nwLabels
from novelwriter.core.project import NWProject
from novelwriter.core.tokenizer import T_Formats, Tokenizer, stripEscape
from novelwriter.types import FONT_WEIGHTS
from novelwriter.types import FONT_STYLE, FONT_WEIGHTS
logger = logging.getLogger(__name__)
@@ -165,6 +165,7 @@ class ToOdt(Tokenizer):
self._fontFamily = "Liberation Serif"
self._fontSize = 12
self._fontWeight = "normal"
self._fontStyle = "normal"
self._fontPitch = "variable"
self._fontBold = "bold"
self._fSizeTitle = "30pt"
@@ -272,11 +273,12 @@ class ToOdt(Tokenizer):
fontWeight = str(intWeight)
fontBold = str(min(intWeight + 300, 900))
self._fontFamily = self._textFont.family()
self._fontSize = self._textFont.pointSize()
self._fontWeight = FONT_WEIGHT_MAP.get(fontWeight, fontWeight)
self._fontPitch = "fixed" if self._textFont.fixedPitch() else "variable"
self._fontBold = FONT_WEIGHT_MAP.get(fontBold, fontBold)
self._fontFamily = self._textFont.family()
self._fontSize = self._textFont.pointSize()
self._fontWeight = FONT_WEIGHT_MAP.get(fontWeight, fontWeight)
self._fontStyle = FONT_STYLE.get(self._textFont.style(), "normal")
self._fontPitch = "fixed" if self._textFont.fixedPitch() else "variable"
self._fontBold = FONT_WEIGHT_MAP.get(fontBold, fontBold)
self._fSizeTitle = f"{round(2.50 * self._fontSize):d}pt"
self._fSizeHead1 = f"{round(2.00 * self._fontSize):d}pt"
@@ -335,7 +337,6 @@ class ToOdt(Tokenizer):
fAttr = {}
fAttr[_mkTag("style", "name")] = self._fontFamily
fAttr[_mkTag("style", "font-pitch")] = self._fontPitch
fAttr[_mkTag("style", "font-weight")] = self._fontWeight
if self._isFlat:
@@ -825,6 +826,7 @@ class ToOdt(Tokenizer):
_mkTag("style", "font-name"): self._fontFamily,
_mkTag("fo", "font-family"): self._fontFamily,
_mkTag("fo", "font-weight"): self._fontWeight,
_mkTag("fo", "font-style"): self._fontStyle,
_mkTag("fo", "font-size"): self._fSizeText,
_mkTag("fo", "language"): self._dLanguage,
_mkTag("fo", "country"): self._dCountry,
@@ -840,6 +842,7 @@ class ToOdt(Tokenizer):
_mkTag("style", "font-name"): self._fontFamily,
_mkTag("fo", "font-family"): self._fontFamily,
_mkTag("fo", "font-weight"): self._fontWeight,
_mkTag("fo", "font-style"): self._fontStyle,
_mkTag("fo", "font-size"): self._fSizeText,
})
@@ -860,6 +863,7 @@ class ToOdt(Tokenizer):
_mkTag("style", "font-name"): self._fontFamily,
_mkTag("fo", "font-family"): self._fontFamily,
_mkTag("fo", "font-weight"): self._fontWeight,
_mkTag("fo", "font-style"): self._fontStyle,
_mkTag("fo", "font-size"): self._fSizeHead,
})