Reset to default font style after emphasis in QTextDocuments (#2121)
This commit is contained in:
@@ -74,11 +74,15 @@ class ToQTextDocument(Tokenizer):
|
||||
self._usedFields: list[tuple[int, str]] = []
|
||||
|
||||
self._init = False
|
||||
self._bold = QFont.Weight.Bold
|
||||
self._normal = QFont.Weight.Normal
|
||||
self._newPage = False
|
||||
self._anchors = True
|
||||
|
||||
self._hWeight = QFont.Weight.Bold
|
||||
self._dWeight = QFont.Weight.Normal
|
||||
self._dItalic = False
|
||||
self._dStrike = False
|
||||
self._dUnderline = False
|
||||
|
||||
self._pageSize = QPageSize(QPageSize.PageSizeId.A4)
|
||||
self._pageMargins = QMarginsF(20.0, 20.0, 20.0, 20.0)
|
||||
|
||||
@@ -128,12 +132,21 @@ class ToQTextDocument(Tokenizer):
|
||||
self._document.clear()
|
||||
self._document.setDefaultFont(self._textFont)
|
||||
|
||||
fPt = self._textFont.pointSizeF()
|
||||
fPx = fPt*96.0/72.0 # 1 em in pixels
|
||||
# Default Styles
|
||||
self._dWeight = self._textFont.weight()
|
||||
self._dItalic = self._textFont.italic()
|
||||
self._dStrike = self._textFont.strikeOut()
|
||||
self._dUnderline = self._textFont.underline()
|
||||
|
||||
# Header Weight
|
||||
self._hWeight = QFont.Weight.Bold if self._boldHeads else self._dWeight
|
||||
|
||||
# Scaled Sizes
|
||||
# ============
|
||||
|
||||
fPt = self._textFont.pointSizeF()
|
||||
fPx = fPt*4.0/3.0 # 1 em in pixels
|
||||
|
||||
self._mHead = {
|
||||
BlockTyp.TITLE: (fPx * self._marginTitle[0], fPx * self._marginTitle[1]),
|
||||
BlockTyp.HEAD1: (fPx * self._marginHead1[0], fPx * self._marginHead1[1]),
|
||||
@@ -318,21 +331,21 @@ class ToQTextDocument(Tokenizer):
|
||||
|
||||
# Construct next format
|
||||
if fmt == TextFmt.B_B:
|
||||
cFmt.setFontWeight(self._bold)
|
||||
cFmt.setFontWeight(QFont.Weight.Bold)
|
||||
elif fmt == TextFmt.B_E:
|
||||
cFmt.setFontWeight(self._normal)
|
||||
cFmt.setFontWeight(self._dWeight)
|
||||
elif fmt == TextFmt.I_B:
|
||||
cFmt.setFontItalic(True)
|
||||
elif fmt == TextFmt.I_E:
|
||||
cFmt.setFontItalic(False)
|
||||
cFmt.setFontItalic(self._dItalic)
|
||||
elif fmt == TextFmt.D_B:
|
||||
cFmt.setFontStrikeOut(True)
|
||||
elif fmt == TextFmt.D_E:
|
||||
cFmt.setFontStrikeOut(False)
|
||||
cFmt.setFontStrikeOut(self._dStrike)
|
||||
elif fmt == TextFmt.U_B:
|
||||
cFmt.setFontUnderline(True)
|
||||
elif fmt == TextFmt.U_E:
|
||||
cFmt.setFontUnderline(False)
|
||||
cFmt.setFontUnderline(self._dUnderline)
|
||||
elif fmt == TextFmt.M_B:
|
||||
cFmt.setBackground(self._theme.highlight)
|
||||
elif fmt == TextFmt.M_E:
|
||||
@@ -376,7 +389,7 @@ class ToQTextDocument(Tokenizer):
|
||||
cFmt.setAnchorHref(data)
|
||||
elif fmt == TextFmt.HRF_E:
|
||||
cFmt.setForeground(primary or self._theme.text)
|
||||
cFmt.setFontUnderline(False)
|
||||
cFmt.setFontUnderline(self._dUnderline)
|
||||
cFmt.setAnchor(False)
|
||||
cFmt.setAnchorHref("")
|
||||
elif fmt == TextFmt.FNOTE:
|
||||
@@ -422,6 +435,10 @@ class ToQTextDocument(Tokenizer):
|
||||
bFmt.setLineHeight(75.0, QtPropLineHeight)
|
||||
|
||||
cFmt = QTextCharFormat(self._charFmt)
|
||||
cFmt.setFontItalic(False)
|
||||
cFmt.setFontUnderline(False)
|
||||
cFmt.setFontStrikeOut(False)
|
||||
cFmt.setFontWeight(QFont.Weight.Normal)
|
||||
cFmt.setFontPointSize(0.75*self._textFont.pointSizeF())
|
||||
cFmt.setForeground(self._theme.comment)
|
||||
|
||||
@@ -440,7 +457,7 @@ class ToQTextDocument(Tokenizer):
|
||||
hCol = self._colorHeads and hType != BlockTyp.TITLE
|
||||
cFmt = QTextCharFormat(self._charFmt)
|
||||
cFmt.setForeground(self._theme.head if hCol else self._theme.text)
|
||||
cFmt.setFontWeight(self._bold if self._boldHeads else self._normal)
|
||||
cFmt.setFontWeight(self._hWeight)
|
||||
cFmt.setFontPointSize(self._sHead.get(hType, 1.0))
|
||||
if hKey and self._anchors:
|
||||
cFmt.setAnchorNames([hKey])
|
||||
|
||||
@@ -101,7 +101,7 @@ def testBaseError_Handler(qtbot, monkeypatch, nwGUI):
|
||||
with monkeypatch.context() as mp:
|
||||
mp.setattr(NWErrorMessage, "exec", lambda *a: None)
|
||||
mp.setattr("PyQt5.QtWidgets.QApplication.exit", lambda *a: None)
|
||||
mp.setattr(nwGUI, "closeMain", causeException)
|
||||
mp.setattr("novelwriter.guimain.GuiMain.closeMain", causeException)
|
||||
exceptionHandler(Exception, "Error Message", None) # type: ignore
|
||||
|
||||
nwGUI.closeMain()
|
||||
|
||||
@@ -22,7 +22,7 @@ from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from PyQt5.QtGui import QTextBlock, QTextCharFormat, QTextCursor
|
||||
from PyQt5.QtGui import QFont, QTextBlock, QTextCharFormat, QTextCursor
|
||||
|
||||
from novelwriter import CONFIG
|
||||
from novelwriter.constants import nwUnicode
|
||||
@@ -71,7 +71,7 @@ def testFmtToQTextDocument_ConvertHeaders(mockGUI):
|
||||
assert bFmt.topMargin() == doc._mHead[BlockTyp.TITLE][0]
|
||||
assert bFmt.bottomMargin() == doc._mHead[BlockTyp.TITLE][1]
|
||||
cFmt = charFmtInBlock(block, 1)
|
||||
assert cFmt.fontWeight() == doc._bold
|
||||
assert cFmt.fontWeight() == QFont.Weight.Bold
|
||||
assert cFmt.fontPointSize() == doc._sHead[BlockTyp.TITLE]
|
||||
assert cFmt.foreground().color() == THEME.text
|
||||
|
||||
@@ -82,7 +82,7 @@ def testFmtToQTextDocument_ConvertHeaders(mockGUI):
|
||||
assert bFmt.topMargin() == doc._mHead[BlockTyp.HEAD1][0]
|
||||
assert bFmt.bottomMargin() == doc._mHead[BlockTyp.HEAD1][1]
|
||||
cFmt = charFmtInBlock(block, 1)
|
||||
assert cFmt.fontWeight() == doc._bold
|
||||
assert cFmt.fontWeight() == QFont.Weight.Bold
|
||||
assert cFmt.fontPointSize() == doc._sHead[BlockTyp.HEAD1]
|
||||
assert cFmt.foreground().color() == THEME.head
|
||||
|
||||
@@ -93,7 +93,7 @@ def testFmtToQTextDocument_ConvertHeaders(mockGUI):
|
||||
assert bFmt.topMargin() == doc._mHead[BlockTyp.HEAD2][0]
|
||||
assert bFmt.bottomMargin() == doc._mHead[BlockTyp.HEAD2][1]
|
||||
cFmt = charFmtInBlock(block, 1)
|
||||
assert cFmt.fontWeight() == doc._bold
|
||||
assert cFmt.fontWeight() == QFont.Weight.Bold
|
||||
assert cFmt.fontPointSize() == doc._sHead[BlockTyp.HEAD2]
|
||||
assert cFmt.foreground().color() == THEME.head
|
||||
|
||||
@@ -104,7 +104,7 @@ def testFmtToQTextDocument_ConvertHeaders(mockGUI):
|
||||
assert bFmt.topMargin() == doc._mHead[BlockTyp.HEAD3][0]
|
||||
assert bFmt.bottomMargin() == doc._mHead[BlockTyp.HEAD3][1]
|
||||
cFmt = charFmtInBlock(block, 1)
|
||||
assert cFmt.fontWeight() == doc._bold
|
||||
assert cFmt.fontWeight() == QFont.Weight.Bold
|
||||
assert cFmt.fontPointSize() == doc._sHead[BlockTyp.HEAD3]
|
||||
assert cFmt.foreground().color() == THEME.head
|
||||
|
||||
@@ -115,7 +115,7 @@ def testFmtToQTextDocument_ConvertHeaders(mockGUI):
|
||||
assert bFmt.topMargin() == doc._mHead[BlockTyp.HEAD4][0]
|
||||
assert bFmt.bottomMargin() == doc._mHead[BlockTyp.HEAD4][1]
|
||||
cFmt = charFmtInBlock(block, 1)
|
||||
assert cFmt.fontWeight() == doc._bold
|
||||
assert cFmt.fontWeight() == QFont.Weight.Bold
|
||||
assert cFmt.fontPointSize() == doc._sHead[BlockTyp.HEAD4]
|
||||
assert cFmt.foreground().color() == THEME.head
|
||||
|
||||
@@ -478,11 +478,11 @@ def testFmtToQTextDocument_TextCharFormats(mockGUI):
|
||||
block = doc.document.findBlockByNumber(1)
|
||||
assert block.text() == "With bold text"
|
||||
cFmt = charFmtInBlock(block, 1)
|
||||
assert cFmt.fontWeight() == doc._normal
|
||||
assert cFmt.fontWeight() == doc._dWeight
|
||||
cFmt = charFmtInBlock(block, 6)
|
||||
assert cFmt.fontWeight() == doc._bold
|
||||
assert cFmt.fontWeight() == QFont.Weight.Bold
|
||||
cFmt = charFmtInBlock(block, 10)
|
||||
assert cFmt.fontWeight() == doc._normal
|
||||
assert cFmt.fontWeight() == doc._dWeight
|
||||
|
||||
# 2: Italic
|
||||
block = doc.document.findBlockByNumber(2)
|
||||
|
||||
Reference in New Issue
Block a user