Apply the font matcher closer to config read

This commit is contained in:
Veronica Berglyd Olsen
2024-11-26 17:06:34 +01:00
parent 18c70ffe3e
commit c091e0b9bb
5 changed files with 35 additions and 28 deletions
+6 -4
View File
@@ -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
+15 -12
View File
@@ -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
##
+3 -2
View File
@@ -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
# ============
+5 -5
View File
@@ -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()
+6 -5
View File
@@ -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)