diff --git a/novelwriter/common.py b/novelwriter/common.py index 7cc5f67b..8e946cee 100644 --- a/novelwriter/common.py +++ b/novelwriter/common.py @@ -436,11 +436,13 @@ def describeFont(font: QFont) -> str: def fontMatcher(font: QFont) -> QFont: """Make sure the font is the correct family, if possible. This - ensures that Qt doesn't use the GUI or document font instead. + ensures that Qt doesn't reuse another font under the hood. The + default Qt5 font matching algorithm doesn't handle well changing + fonts at runtime. """ info = QFontInfo(font) if (famRequest := font.family()) != (famActual := info.family()): - logger.warning("Font mismatch: %s != %s", famRequest, famActual) + logger.warning("Font mismatch: Requested '%s', but got '%s'", famRequest, famActual) db = QFontDatabase() if famRequest in db.families(): styleRequest, sizeRequest = font.styleName(), font.pointSize() @@ -450,8 +452,8 @@ def fontMatcher(font: QFont) -> QFont: if famFound == famRequest: logger.info("Found: %s, %s, %d pt", famFound, styleFound, sizeFound) return temp - logger.warning("Could not find a font match") - logger.warning("You may need to restart the application") + logger.warning("Could not find a font match in the font database") + logger.warning("If you just changed font, you may need to restart the application") return font diff --git a/novelwriter/config.py b/novelwriter/config.py index 45dc01d9..d1ae57b5 100644 --- a/novelwriter/config.py +++ b/novelwriter/config.py @@ -40,7 +40,10 @@ from PyQt5.QtCore import ( from PyQt5.QtGui import QFont, QFontDatabase from PyQt5.QtWidgets import QApplication -from novelwriter.common import NWConfigParser, checkInt, checkPath, describeFont, formatTimeStamp +from novelwriter.common import ( + NWConfigParser, checkInt, checkPath, describeFont, fontMatcher, + formatTimeStamp +) from novelwriter.constants import nwFiles, nwUnicode from novelwriter.error import formatException, logException @@ -369,10 +372,11 @@ class Config: def setGuiFont(self, value: QFont | str | None) -> None: """Update the GUI's font style from settings.""" if isinstance(value, QFont): - self.guiFont = value + self.guiFont = fontMatcher(value) elif value and isinstance(value, str): - self.guiFont = QFont() - self.guiFont.fromString(value) + font = QFont() + font.fromString(value) + self.guiFont = fontMatcher(font) else: font = QFont() fontDB = QFontDatabase() @@ -382,11 +386,9 @@ class Config: font.setPointSize(10) else: font = fontDB.systemFont(QFontDatabase.SystemFont.GeneralFont) - self.guiFont = font + self.guiFont = fontMatcher(font) logger.debug("GUI font set to: %s", describeFont(font)) - QApplication.setFont(self.guiFont) - return def setTextFont(self, value: QFont | str | None) -> None: @@ -394,10 +396,11 @@ class Config: set to default font. """ if isinstance(value, QFont): - self.textFont = value + self.textFont = fontMatcher(value) elif value and isinstance(value, str): - self.textFont = QFont() - self.textFont.fromString(value) + font = QFont() + font.fromString(value) + self.textFont = fontMatcher(font) else: fontDB = QFontDatabase() fontFam = fontDB.families() @@ -411,8 +414,8 @@ class Config: font.setPointSize(12) else: font = fontDB.systemFont(QFontDatabase.SystemFont.GeneralFont) - self.textFont = font - logger.debug("Text font set to: %s", describeFont(font)) + self.textFont = fontMatcher(font) + logger.debug("Text font set to: %s", describeFont(self.textFont)) return ## diff --git a/novelwriter/formats/toqdoc.py b/novelwriter/formats/toqdoc.py index 0030a3be..59331bac 100644 --- a/novelwriter/formats/toqdoc.py +++ b/novelwriter/formats/toqdoc.py @@ -148,6 +148,7 @@ class ToQTextDocument(Tokenizer): fPt = self._textFont.pointSizeF() fPx = fPt*4.0/3.0 # 1 em in pixels + fDt = fPx * self._resolution/96.0 # In dots for a given resolution self._mHead = { BlockTyp.TITLE: (fPx * self._marginTitle[0], fPx * self._marginTitle[1]), @@ -170,8 +171,8 @@ class ToQTextDocument(Tokenizer): self._mMeta = (fPx * self._marginMeta[0], fPx * self._marginMeta[1]) self._mSep = (fPx * self._marginSep[0], fPx * self._marginSep[1]) - self._mIndent = fPx * self._resolution/96 * 2.0 - self._tIndent = fPx * self._resolution/96 * self._firstWidth + self._mIndent = fDt * 2.0 + self._tIndent = fDt * self._firstWidth # Text Formats # ============ diff --git a/novelwriter/guimain.py b/novelwriter/guimain.py index 8d83bd92..d0f74bdc 100644 --- a/novelwriter/guimain.py +++ b/novelwriter/guimain.py @@ -1051,11 +1051,6 @@ class GuiMain(QMainWindow): self.initMain() self.saveDocument() - if restart: - SHARED.info(self.tr( - "Some changes will not be applied until novelWriter has been restarted." - )) - if tree: SHARED.project.tree.refreshAllItems() @@ -1088,6 +1083,11 @@ class GuiMain(QMainWindow): self._lastTotalCount = 0 self._updateStatusWordCount() + if restart: + SHARED.info(self.tr( + "Some changes will not be applied until novelWriter has been restarted." + )) + return @pyqtSlot() diff --git a/novelwriter/tools/manussettings.py b/novelwriter/tools/manussettings.py index e5221cd2..dc64a316 100644 --- a/novelwriter/tools/manussettings.py +++ b/novelwriter/tools/manussettings.py @@ -37,7 +37,7 @@ from PyQt5.QtWidgets import ( ) from novelwriter import CONFIG, SHARED -from novelwriter.common import describeFont, qtLambda +from novelwriter.common import describeFont, fontMatcher, qtLambda from novelwriter.constants import nwHeadFmt, nwKeyWords, nwLabels, nwStyles, trConst from novelwriter.core.buildsettings import BuildSettings, FilterMode from novelwriter.extensions.configlayout import ( @@ -1281,8 +1281,9 @@ class _FormattingTab(NScrollableForm): # Text Format # =========== - self._textFont = QFont() - self._textFont.fromString(self._build.getStr("format.textFont")) + font = QFont() + font.fromString(self._build.getStr("format.textFont")) + self._textFont = fontMatcher(font) self.textFont.setText(describeFont(self._textFont)) self.textFont.setCursorPosition(0) @@ -1437,9 +1438,9 @@ class _FormattingTab(NScrollableForm): """Open the QFontDialog and set a font for the font style.""" font, status = SHARED.getFont(self._textFont, CONFIG.nativeFont) if status: - self.textFont.setText(describeFont(font)) + self._textFont = fontMatcher(font) + self.textFont.setText(describeFont(self._textFont)) self.textFont.setCursorPosition(0) - self._textFont = font return @pyqtSlot(int)