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