Add a font matcher for text fonts (#2118)
This commit is contained in:
+22
-1
@@ -38,7 +38,7 @@ from urllib.parse import urljoin
|
||||
from urllib.request import pathname2url
|
||||
|
||||
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.enum import nwItemClass, nwItemLayout, nwItemType
|
||||
@@ -434,6 +434,27 @@ def describeFont(font: QFont) -> str:
|
||||
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:
|
||||
"""A replacement for Python lambdas that works for Qt slots."""
|
||||
def wrapper(*a_: Any) -> None:
|
||||
|
||||
@@ -35,7 +35,7 @@ from PyQt5.QtCore import QLocale
|
||||
from PyQt5.QtGui import QColor, QFont
|
||||
|
||||
from novelwriter import CONFIG
|
||||
from novelwriter.common import checkInt, numberToRoman
|
||||
from novelwriter.common import checkInt, fontMatcher, numberToRoman
|
||||
from novelwriter.constants import (
|
||||
nwHeadFmt, nwKeyWords, nwLabels, nwShortcode, nwStats, nwStyles, nwUnicode,
|
||||
trConst
|
||||
@@ -304,7 +304,7 @@ class Tokenizer(ABC):
|
||||
|
||||
def setTextFont(self, font: QFont) -> None:
|
||||
"""Set the build font."""
|
||||
self._textFont = font
|
||||
self._textFont = fontMatcher(font)
|
||||
return
|
||||
|
||||
def setLineHeight(self, height: float) -> None:
|
||||
|
||||
@@ -53,7 +53,7 @@ from PyQt5.QtWidgets import (
|
||||
)
|
||||
|
||||
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.core.document import NWDocument
|
||||
from novelwriter.enum import (
|
||||
@@ -348,8 +348,9 @@ class GuiDocEditor(QPlainTextEdit):
|
||||
SHARED.updateSpellCheckLanguage()
|
||||
|
||||
# Set the font. See issues #1862 and #1875.
|
||||
self.setFont(CONFIG.textFont)
|
||||
self._qDocument.setDefaultFont(CONFIG.textFont)
|
||||
font = fontMatcher(CONFIG.textFont)
|
||||
self.setFont(font)
|
||||
self._qDocument.setDefaultFont(font)
|
||||
self.docHeader.updateFont()
|
||||
self.docFooter.updateFont()
|
||||
self.docSearch.updateFont()
|
||||
|
||||
Reference in New Issue
Block a user