Add a font matcher for text fonts (#2118)

This commit is contained in:
Veronica Berglyd Olsen
2024-11-26 02:03:09 +01:00
parent 8463188336
commit ff470028be
3 changed files with 28 additions and 6 deletions
+22 -1
View File
@@ -38,7 +38,7 @@ from urllib.parse import urljoin
from urllib.request import pathname2url from urllib.request import pathname2url
from PyQt5.QtCore import QCoreApplication, QMimeData, QUrl from PyQt5.QtCore import QCoreApplication, QMimeData, QUrl
from PyQt5.QtGui import QColor, QDesktopServices, QFont, QFontInfo from PyQt5.QtGui import QColor, QDesktopServices, QFont, QFontDatabase, QFontInfo
from novelwriter.constants import nwConst, nwLabels, nwUnicode, trConst from novelwriter.constants import nwConst, nwLabels, nwUnicode, trConst
from novelwriter.enum import nwItemClass, nwItemLayout, nwItemType from novelwriter.enum import nwItemClass, nwItemLayout, nwItemType
@@ -434,6 +434,27 @@ def describeFont(font: QFont) -> str:
return "Error" return "Error"
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.
"""
info = QFontInfo(font)
if (famRequest := font.family()) != (famActual := info.family()):
logger.warning("Font mismatch: %s != %s", famRequest, famActual)
db = QFontDatabase()
if famRequest in db.families():
styleRequest, sizeRequest = font.styleName(), font.pointSize()
logger.info("Lookup: %s, %s, %d pt", famRequest, styleRequest, sizeRequest)
temp = db.font(famRequest, styleRequest, sizeRequest)
famFound, styleFound, sizeFound = temp.family(), temp.styleName(), temp.pointSize()
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")
return font
def qtLambda(func: Callable, *args: Any, **kwargs: Any) -> Callable: def qtLambda(func: Callable, *args: Any, **kwargs: Any) -> Callable:
"""A replacement for Python lambdas that works for Qt slots.""" """A replacement for Python lambdas that works for Qt slots."""
def wrapper(*a_: Any) -> None: def wrapper(*a_: Any) -> None:
+2 -2
View File
@@ -35,7 +35,7 @@ from PyQt5.QtCore import QLocale
from PyQt5.QtGui import QColor, QFont from PyQt5.QtGui import QColor, QFont
from novelwriter import CONFIG from novelwriter import CONFIG
from novelwriter.common import checkInt, numberToRoman from novelwriter.common import checkInt, fontMatcher, numberToRoman
from novelwriter.constants import ( from novelwriter.constants import (
nwHeadFmt, nwKeyWords, nwLabels, nwShortcode, nwStats, nwStyles, nwUnicode, nwHeadFmt, nwKeyWords, nwLabels, nwShortcode, nwStats, nwStyles, nwUnicode,
trConst trConst
@@ -304,7 +304,7 @@ class Tokenizer(ABC):
def setTextFont(self, font: QFont) -> None: def setTextFont(self, font: QFont) -> None:
"""Set the build font.""" """Set the build font."""
self._textFont = font self._textFont = fontMatcher(font)
return return
def setLineHeight(self, height: float) -> None: def setLineHeight(self, height: float) -> None:
+4 -3
View File
@@ -53,7 +53,7 @@ from PyQt5.QtWidgets import (
) )
from novelwriter import CONFIG, SHARED from novelwriter import CONFIG, SHARED
from novelwriter.common import decodeMimeHandles, minmax, qtLambda, transferCase from novelwriter.common import decodeMimeHandles, fontMatcher, minmax, qtLambda, transferCase
from novelwriter.constants import nwConst, nwKeyWords, nwShortcode, nwUnicode from novelwriter.constants import nwConst, nwKeyWords, nwShortcode, nwUnicode
from novelwriter.core.document import NWDocument from novelwriter.core.document import NWDocument
from novelwriter.enum import ( from novelwriter.enum import (
@@ -348,8 +348,9 @@ class GuiDocEditor(QPlainTextEdit):
SHARED.updateSpellCheckLanguage() SHARED.updateSpellCheckLanguage()
# Set the font. See issues #1862 and #1875. # Set the font. See issues #1862 and #1875.
self.setFont(CONFIG.textFont) font = fontMatcher(CONFIG.textFont)
self._qDocument.setDefaultFont(CONFIG.textFont) self.setFont(font)
self._qDocument.setDefaultFont(font)
self.docHeader.updateFont() self.docHeader.updateFont()
self.docFooter.updateFont() self.docFooter.updateFont()
self.docSearch.updateFont() self.docSearch.updateFont()