Clean up font handling in HTML and ODT

This commit is contained in:
Veronica Berglyd Olsen
2024-05-22 23:13:16 +02:00
parent 411163f750
commit cf16b57f66
8 changed files with 40 additions and 37 deletions
+1
View File
@@ -114,6 +114,7 @@ class Config:
self.hideVScroll = False # Hide vertical scroll bars on main widgets
self.hideHScroll = False # Hide horizontal scroll bars on main widgets
self.lastNotes = "0x0" # The latest release notes that have been shown
self.nativeFont = False # Use native font dialog
# Size Settings
self._mainWinSize = [1200, 650] # Last size of the main GUI window
+3 -4
View File
@@ -34,7 +34,7 @@ from novelwriter.common import formatTimeStamp
from novelwriter.constants import nwHeadFmt, nwHtmlUnicode, nwKeyWords, nwLabels
from novelwriter.core.project import NWProject
from novelwriter.core.tokenizer import T_Formats, Tokenizer, stripEscape
from novelwriter.types import FONT_STRETCH, FONT_STYLE, FONT_WEIGHTS
from novelwriter.types import FONT_STYLE, FONT_WEIGHTS
logger = logging.getLogger(__name__)
@@ -377,13 +377,12 @@ class ToHtml(Tokenizer):
font = self._textFont
styles.append((
"body {{"
"font-family: '{0:s}'; font-size: {1:d}pt; font-weight: {2:d}; "
"font-stretch: {3:s}; font-style: {4:s};"
"font-family: '{0:s}'; font-size: {1:d}pt; "
"font-weight: {2:d}; font-style: {3:s};"
"}}"
).format(
font.family(), font.pointSize(),
FONT_WEIGHTS.get(font.weight(), 400),
FONT_STRETCH.get(font.stretch(), "normal"),
FONT_STYLE.get(font.style(), "normal"),
))
styles.append((
+11 -7
View File
@@ -42,7 +42,7 @@ from novelwriter.common import xmlIndent
from novelwriter.constants import nwHeadFmt, nwKeyWords, nwLabels
from novelwriter.core.project import NWProject
from novelwriter.core.tokenizer import T_Formats, Tokenizer, stripEscape
from novelwriter.types import FONT_WEIGHTS
from novelwriter.types import FONT_STYLE, FONT_WEIGHTS
logger = logging.getLogger(__name__)
@@ -165,6 +165,7 @@ class ToOdt(Tokenizer):
self._fontFamily = "Liberation Serif"
self._fontSize = 12
self._fontWeight = "normal"
self._fontStyle = "normal"
self._fontPitch = "variable"
self._fontBold = "bold"
self._fSizeTitle = "30pt"
@@ -272,11 +273,12 @@ class ToOdt(Tokenizer):
fontWeight = str(intWeight)
fontBold = str(min(intWeight + 300, 900))
self._fontFamily = self._textFont.family()
self._fontSize = self._textFont.pointSize()
self._fontWeight = FONT_WEIGHT_MAP.get(fontWeight, fontWeight)
self._fontPitch = "fixed" if self._textFont.fixedPitch() else "variable"
self._fontBold = FONT_WEIGHT_MAP.get(fontBold, fontBold)
self._fontFamily = self._textFont.family()
self._fontSize = self._textFont.pointSize()
self._fontWeight = FONT_WEIGHT_MAP.get(fontWeight, fontWeight)
self._fontStyle = FONT_STYLE.get(self._textFont.style(), "normal")
self._fontPitch = "fixed" if self._textFont.fixedPitch() else "variable"
self._fontBold = FONT_WEIGHT_MAP.get(fontBold, fontBold)
self._fSizeTitle = f"{round(2.50 * self._fontSize):d}pt"
self._fSizeHead1 = f"{round(2.00 * self._fontSize):d}pt"
@@ -335,7 +337,6 @@ class ToOdt(Tokenizer):
fAttr = {}
fAttr[_mkTag("style", "name")] = self._fontFamily
fAttr[_mkTag("style", "font-pitch")] = self._fontPitch
fAttr[_mkTag("style", "font-weight")] = self._fontWeight
if self._isFlat:
@@ -825,6 +826,7 @@ class ToOdt(Tokenizer):
_mkTag("style", "font-name"): self._fontFamily,
_mkTag("fo", "font-family"): self._fontFamily,
_mkTag("fo", "font-weight"): self._fontWeight,
_mkTag("fo", "font-style"): self._fontStyle,
_mkTag("fo", "font-size"): self._fSizeText,
_mkTag("fo", "language"): self._dLanguage,
_mkTag("fo", "country"): self._dCountry,
@@ -840,6 +842,7 @@ class ToOdt(Tokenizer):
_mkTag("style", "font-name"): self._fontFamily,
_mkTag("fo", "font-family"): self._fontFamily,
_mkTag("fo", "font-weight"): self._fontWeight,
_mkTag("fo", "font-style"): self._fontStyle,
_mkTag("fo", "font-size"): self._fSizeText,
})
@@ -860,6 +863,7 @@ class ToOdt(Tokenizer):
_mkTag("style", "font-name"): self._fontFamily,
_mkTag("fo", "font-family"): self._fontFamily,
_mkTag("fo", "font-weight"): self._fontWeight,
_mkTag("fo", "font-style"): self._fontStyle,
_mkTag("fo", "font-size"): self._fSizeHead,
})
+3 -4
View File
@@ -30,8 +30,7 @@ from PyQt5.QtCore import Qt, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QCloseEvent, QKeyEvent, QKeySequence
from PyQt5.QtWidgets import (
QAbstractButton, QApplication, QCompleter, QDialog, QDialogButtonBox,
QFileDialog, QFontDialog, QHBoxLayout, QLineEdit, QPushButton, QVBoxLayout,
QWidget
QFileDialog, QHBoxLayout, QLineEdit, QPushButton, QVBoxLayout, QWidget
)
from novelwriter import CONFIG, SHARED
@@ -804,7 +803,7 @@ class GuiPreferences(QDialog):
@pyqtSlot()
def _selectGuiFont(self) -> None:
"""Open the QFontDialog and set a font for the font style."""
font, status = QFontDialog.getFont(self._guiFont, self)
font, status = SHARED.getFont(self._guiFont, CONFIG.nativeFont)
if status:
self.guiFont.setText(describeFont(font))
self.guiFont.setCursorPosition(0)
@@ -814,7 +813,7 @@ class GuiPreferences(QDialog):
@pyqtSlot()
def _selectTextFont(self) -> None:
"""Open the QFontDialog and set a font for the font style."""
font, status = QFontDialog.getFont(CONFIG.textFont, self)
font, status = SHARED.getFont(self._textFont, CONFIG.nativeFont)
if status:
self.textFont.setText(describeFont(font))
self.textFont.setCursorPosition(0)
+14 -1
View File
@@ -31,7 +31,8 @@ from time import time
from typing import TYPE_CHECKING, TypeVar
from PyQt5.QtCore import QObject, QRunnable, QThreadPool, QTimer, pyqtSignal
from PyQt5.QtWidgets import QFileDialog, QMessageBox, QWidget
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QFileDialog, QFontDialog, QMessageBox, QWidget
from novelwriter.common import formatFileFilter
from novelwriter.constants import nwFiles
@@ -267,6 +268,18 @@ class SharedData(QObject):
)
return Path(selected) if selected else None
def getFont(self, current: QFont, native: bool) -> tuple[QFont, bool]:
"""Open the font dialog and select a font."""
if native:
font, status = QFontDialog.getFont(current, self.mainGui)
else:
options = QFontDialog.FontDialogOption.DontUseNativeDialog
font, status = QFontDialog.getFont(current, self.mainGui, options=options)
font.setOverline(False)
font.setStrikeOut(False)
font.setUnderline(False)
return font, status
def findTopLevelWidget(self, kind: type[NWWidget]) -> NWWidget | None:
"""Find a top level widget."""
for widget in self.mainGui.children():
+3 -3
View File
@@ -402,11 +402,11 @@ class GuiManuscript(NToolDialog):
def _updatePreview(self, data: dict, build: BuildSettings) -> None:
"""Update the preview widget and set relevant values."""
self.docPreview.setContent(data)
self.docPreview.setBuildName(build.name)
textFont = QFont()
textFont.fromString(build.getStr("format.textFont"))
self.docPreview.setContent(data)
self.docPreview.setBuildName(build.name)
self.docPreview.setTextFont(textFont)
self.docPreview.setJustify(
build.getBool("format.justifyText")
+5 -5
View File
@@ -30,10 +30,10 @@ from typing import TYPE_CHECKING
from PyQt5.QtCore import QEvent, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QFont, QIcon, QSyntaxHighlighter, QTextCharFormat, QTextDocument
from PyQt5.QtWidgets import (
QAbstractButton, QAbstractItemView, QDialogButtonBox, QFontDialog, QFrame,
QGridLayout, QHBoxLayout, QHeaderView, QLabel, QLineEdit, QMenu,
QPlainTextEdit, QPushButton, QSplitter, QStackedWidget, QTreeWidget,
QTreeWidgetItem, QVBoxLayout, QWidget
QAbstractButton, QAbstractItemView, QDialogButtonBox, QFrame, QGridLayout,
QHBoxLayout, QHeaderView, QLabel, QLineEdit, QMenu, QPlainTextEdit,
QPushButton, QSplitter, QStackedWidget, QTreeWidget, QTreeWidgetItem,
QVBoxLayout, QWidget
)
from novelwriter import CONFIG, SHARED
@@ -1241,7 +1241,7 @@ class _FormatTab(NScrollableForm):
@pyqtSlot()
def _selectFont(self) -> None:
"""Open the QFontDialog and set a font for the font style."""
font, status = QFontDialog.getFont(self._textFont, self)
font, status = SHARED.getFont(self._textFont, CONFIG.nativeFont)
if status:
self.textFont.setText(describeFont(font))
self.textFont.setCursorPosition(0)
-13
View File
@@ -120,19 +120,6 @@ FONT_WEIGHTS: dict[int, int] = {
QFont.Weight.Black: 900,
}
FONT_STRETCH: dict[int, str] = {
QFont.AnyStretch: "normal",
QFont.UltraCondensed: "ultra-condensed",
QFont.ExtraCondensed: "extra-condensed",
QFont.Condensed: "condensed",
QFont.SemiCondensed: "semi-condensed",
QFont.Unstretched: "normal",
QFont.SemiExpanded: "semi-expanded",
QFont.Expanded: "expanded",
QFont.ExtraExpanded: "extra-expanded",
QFont.UltraExpanded: "ultra-expanded",
}
FONT_STYLE: dict[int, str] = {
QFont.Style.StyleNormal: "normal",
QFont.Style.StyleItalic: "italic",