From cf16b57f6665ee6c0ea5bfec329cb154ff85bc23 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Wed, 22 May 2024 23:13:16 +0200 Subject: [PATCH] Clean up font handling in HTML and ODT --- novelwriter/config.py | 1 + novelwriter/core/tohtml.py | 7 +++---- novelwriter/core/toodt.py | 18 +++++++++++------- novelwriter/dialogs/preferences.py | 7 +++---- novelwriter/shared.py | 15 ++++++++++++++- novelwriter/tools/manuscript.py | 6 +++--- novelwriter/tools/manussettings.py | 10 +++++----- novelwriter/types.py | 13 ------------- 8 files changed, 40 insertions(+), 37 deletions(-) diff --git a/novelwriter/config.py b/novelwriter/config.py index 2bff5d2b..90d25c13 100644 --- a/novelwriter/config.py +++ b/novelwriter/config.py @@ -114,6 +114,7 @@ class Config: self.hideVScroll = False # Hide vertical scroll bars on main widgets self.hideHScroll = False # Hide horizontal scroll bars on main widgets self.lastNotes = "0x0" # The latest release notes that have been shown + self.nativeFont = False # Use native font dialog # Size Settings self._mainWinSize = [1200, 650] # Last size of the main GUI window diff --git a/novelwriter/core/tohtml.py b/novelwriter/core/tohtml.py index ce4cc1c5..f16cc515 100644 --- a/novelwriter/core/tohtml.py +++ b/novelwriter/core/tohtml.py @@ -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(( diff --git a/novelwriter/core/toodt.py b/novelwriter/core/toodt.py index 661e3998..f380e3d7 100644 --- a/novelwriter/core/toodt.py +++ b/novelwriter/core/toodt.py @@ -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, }) diff --git a/novelwriter/dialogs/preferences.py b/novelwriter/dialogs/preferences.py index affe3117..675b9800 100644 --- a/novelwriter/dialogs/preferences.py +++ b/novelwriter/dialogs/preferences.py @@ -30,8 +30,7 @@ from PyQt5.QtCore import Qt, pyqtSignal, pyqtSlot from PyQt5.QtGui import QCloseEvent, QKeyEvent, QKeySequence from PyQt5.QtWidgets import ( QAbstractButton, QApplication, QCompleter, QDialog, QDialogButtonBox, - QFileDialog, QFontDialog, QHBoxLayout, QLineEdit, QPushButton, QVBoxLayout, - QWidget + QFileDialog, QHBoxLayout, QLineEdit, QPushButton, QVBoxLayout, QWidget ) from novelwriter import CONFIG, SHARED @@ -804,7 +803,7 @@ class GuiPreferences(QDialog): @pyqtSlot() def _selectGuiFont(self) -> None: """Open the QFontDialog and set a font for the font style.""" - font, status = QFontDialog.getFont(self._guiFont, self) + font, status = SHARED.getFont(self._guiFont, CONFIG.nativeFont) if status: self.guiFont.setText(describeFont(font)) self.guiFont.setCursorPosition(0) @@ -814,7 +813,7 @@ class GuiPreferences(QDialog): @pyqtSlot() def _selectTextFont(self) -> None: """Open the QFontDialog and set a font for the font style.""" - font, status = QFontDialog.getFont(CONFIG.textFont, self) + font, status = SHARED.getFont(self._textFont, CONFIG.nativeFont) if status: self.textFont.setText(describeFont(font)) self.textFont.setCursorPosition(0) diff --git a/novelwriter/shared.py b/novelwriter/shared.py index e703eb2c..f6d8aad5 100644 --- a/novelwriter/shared.py +++ b/novelwriter/shared.py @@ -31,7 +31,8 @@ from time import time from typing import TYPE_CHECKING, TypeVar from PyQt5.QtCore import QObject, QRunnable, QThreadPool, QTimer, pyqtSignal -from PyQt5.QtWidgets import QFileDialog, QMessageBox, QWidget +from PyQt5.QtGui import QFont +from PyQt5.QtWidgets import QFileDialog, QFontDialog, QMessageBox, QWidget from novelwriter.common import formatFileFilter from novelwriter.constants import nwFiles @@ -267,6 +268,18 @@ class SharedData(QObject): ) return Path(selected) if selected else None + def getFont(self, current: QFont, native: bool) -> tuple[QFont, bool]: + """Open the font dialog and select a font.""" + if native: + font, status = QFontDialog.getFont(current, self.mainGui) + else: + options = QFontDialog.FontDialogOption.DontUseNativeDialog + font, status = QFontDialog.getFont(current, self.mainGui, options=options) + font.setOverline(False) + font.setStrikeOut(False) + font.setUnderline(False) + return font, status + def findTopLevelWidget(self, kind: type[NWWidget]) -> NWWidget | None: """Find a top level widget.""" for widget in self.mainGui.children(): diff --git a/novelwriter/tools/manuscript.py b/novelwriter/tools/manuscript.py index d70442f0..7e27b867 100644 --- a/novelwriter/tools/manuscript.py +++ b/novelwriter/tools/manuscript.py @@ -402,11 +402,11 @@ class GuiManuscript(NToolDialog): def _updatePreview(self, data: dict, build: BuildSettings) -> None: """Update the preview widget and set relevant values.""" - self.docPreview.setContent(data) - self.docPreview.setBuildName(build.name) - textFont = QFont() textFont.fromString(build.getStr("format.textFont")) + + self.docPreview.setContent(data) + self.docPreview.setBuildName(build.name) self.docPreview.setTextFont(textFont) self.docPreview.setJustify( build.getBool("format.justifyText") diff --git a/novelwriter/tools/manussettings.py b/novelwriter/tools/manussettings.py index 53a84be6..1488c3b8 100644 --- a/novelwriter/tools/manussettings.py +++ b/novelwriter/tools/manussettings.py @@ -30,10 +30,10 @@ from typing import TYPE_CHECKING from PyQt5.QtCore import QEvent, pyqtSignal, pyqtSlot from PyQt5.QtGui import QFont, QIcon, QSyntaxHighlighter, QTextCharFormat, QTextDocument from PyQt5.QtWidgets import ( - QAbstractButton, QAbstractItemView, QDialogButtonBox, QFontDialog, QFrame, - QGridLayout, QHBoxLayout, QHeaderView, QLabel, QLineEdit, QMenu, - QPlainTextEdit, QPushButton, QSplitter, QStackedWidget, QTreeWidget, - QTreeWidgetItem, QVBoxLayout, QWidget + QAbstractButton, QAbstractItemView, QDialogButtonBox, QFrame, QGridLayout, + QHBoxLayout, QHeaderView, QLabel, QLineEdit, QMenu, QPlainTextEdit, + QPushButton, QSplitter, QStackedWidget, QTreeWidget, QTreeWidgetItem, + QVBoxLayout, QWidget ) from novelwriter import CONFIG, SHARED @@ -1241,7 +1241,7 @@ class _FormatTab(NScrollableForm): @pyqtSlot() def _selectFont(self) -> None: """Open the QFontDialog and set a font for the font style.""" - font, status = QFontDialog.getFont(self._textFont, self) + font, status = SHARED.getFont(self._textFont, CONFIG.nativeFont) if status: self.textFont.setText(describeFont(font)) self.textFont.setCursorPosition(0) diff --git a/novelwriter/types.py b/novelwriter/types.py index d0e8eff9..c70d2893 100644 --- a/novelwriter/types.py +++ b/novelwriter/types.py @@ -120,19 +120,6 @@ FONT_WEIGHTS: dict[int, int] = { QFont.Weight.Black: 900, } -FONT_STRETCH: dict[int, str] = { - QFont.AnyStretch: "normal", - QFont.UltraCondensed: "ultra-condensed", - QFont.ExtraCondensed: "extra-condensed", - QFont.Condensed: "condensed", - QFont.SemiCondensed: "semi-condensed", - QFont.Unstretched: "normal", - QFont.SemiExpanded: "semi-expanded", - QFont.Expanded: "expanded", - QFont.ExtraExpanded: "extra-expanded", - QFont.UltraExpanded: "ultra-expanded", -} - FONT_STYLE: dict[int, str] = { QFont.Style.StyleNormal: "normal", QFont.Style.StyleItalic: "italic",