From 66db26acfae5d9559b3d18e25a2f287a52a358ec Mon Sep 17 00:00:00 2001 From: dmMaze Date: Thu, 29 Feb 2024 14:44:23 +0800 Subject: [PATCH] Allow replace font family #388, fix global font format --- launch.py | 2 +- ui/configpanel.py | 7 +++++++ ui/fontformat_commands.py | 9 +++++++-- ui/fontformatpanel.py | 8 ++++---- ui/mainwindow.py | 8 +++++++- ui/misc.py | 4 ++++ utils/config.py | 2 +- 7 files changed, 31 insertions(+), 9 deletions(-) diff --git a/launch.py b/launch.py index 7e3455f9..95454d41 100644 --- a/launch.py +++ b/launch.py @@ -212,7 +212,7 @@ def main(): if fnt_idx >= 0: shared.CUSTOM_FONTS.append(QFontDatabase.applicationFontFamilies(fnt_idx)[0]) - shared.FONT_FAMILIES = set(f.lower() for f in QFontDatabase.families()) + shared.FONT_FAMILIES = set(f for f in QFontDatabase.families()) yahei = QFont('Microsoft YaHei UI') if yahei.exactMatch() and not sys.platform == 'darwin': QGuiApplication.setFont(yahei) diff --git a/ui/configpanel.py b/ui/configpanel.py index ee3c26ae..53ca3452 100644 --- a/ui/configpanel.py +++ b/ui/configpanel.py @@ -452,6 +452,10 @@ def __init__(self, *args, **kwargs) -> None: self.let_writing_mode_combox, sublock = combobox_with_label([dec_program_str, use_global_str], self.tr('Writing-mode'), parent=self, insert_stretch=True) self.let_writing_mode_combox.currentIndexChanged.connect(self.on_writing_mode_flag_changed) global_fntfmt_layout.addWidget(sublock, 3, 0) + self.let_family_combox, sublock = combobox_with_label([self.tr('Keep existing'), self.tr('Always use global setting')], self.tr('Font Family'), parent=self, insert_stretch=True) + self.let_family_combox.currentIndexChanged.connect(self.on_family_flag_changed) + global_fntfmt_layout.addWidget(sublock, 3, 1) + global_fntfmt_layout.addItem(QSpacerItem(0, 0, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding), 0, 2) self.let_autolayout_checker, sublock = generalConfigPanel.addCheckBox(self.tr('Auto layout'), @@ -576,6 +580,9 @@ def on_alignment_flag_changed(self): def on_writing_mode_flag_changed(self): pcfg.let_writing_mode_flag = self.let_writing_mode_combox.currentIndex() + def on_family_flag_changed(self): + pcfg.let_family_flag = self.let_family_combox.currentIndex() + def on_effect_flag_changed(self): pcfg.let_fnteffect_flag = self.let_effect_combox.currentIndex() diff --git a/ui/fontformat_commands.py b/ui/fontformat_commands.py index 47feec77..9399c54d 100644 --- a/ui/fontformat_commands.py +++ b/ui/fontformat_commands.py @@ -46,6 +46,8 @@ def font_formating(push_undostack: bool = False): def func_wrapper(formatting_func): def wrapper(param_name: str, values: str, act_ffmt: FontFormat, is_global: bool, blkitems: List[TextBlkItem] = None, set_focus: bool = False, *args, **kwargs): + if is_global: + act_ffmt[param_name] = values blkitems, values = wrap_fntformat_input(values, blkitems, is_global) if len(blkitems) > 0: act_ffmt[param_name] = values[0] @@ -88,10 +90,13 @@ def ffmt_change_weight(param_name: str, values: str, act_ffmt: FontFormat, is_gl for blkitem, value in zip(blkitems, values): blkitem.setFontWeight(value, **set_kwargs) +@font_formating() def ffmt_change_bold(param_name: str, values: str, act_ffmt: FontFormat, is_global: bool, blkitems: List[TextBlkItem] = None, **kwargs): - blkitems, values = wrap_fntformat_input(values, blkitems, is_global) + set_kwargs = global_default_set_kwargs if is_global else local_default_set_kwargs values = [QFont.Bold if value else QFont.Normal for value in values] - ffmt_change_weight('weight', values, act_ffmt, is_global, blkitems, **kwargs) + # ffmt_change_weight('weight', values, act_ffmt, is_global, blkitems, **kwargs) + for blkitem, value in zip(blkitems, values): + blkitem.setFontWeight(value, **set_kwargs) @font_formating(push_undostack=True) def ffmt_change_letter_spacing(param_name: str, values: str, act_ffmt: FontFormat, is_global: bool, blkitems: List[TextBlkItem], **kwargs): diff --git a/ui/fontformatpanel.py b/ui/fontformatpanel.py index cac5b521..6e4d4dea 100644 --- a/ui/fontformatpanel.py +++ b/ui/fontformatpanel.py @@ -328,22 +328,22 @@ def __init__(self, emit_if_focused=True, *args, **kwargs) -> None: lineedit.return_pressed_wochange.connect(self.apply_fontfamily) self.setLineEdit(lineedit) self.emit_if_focused = emit_if_focused - self._current_font = self.currentFont().family().lower() + self._current_font = self.currentFont().family() def apply_fontfamily(self): - ffamily = self.currentFont().family().lower() + ffamily = self.currentFont().family() if ffamily in shared.FONT_FAMILIES: self.param_changed.emit('family', ffamily) self._current_font = ffamily def on_fontfamily_changed(self): if not self.hasFocus(): - self._current_font = self.currentFont().family().lower() + self._current_font = self.currentFont().family() self.lineedit._text_changed = False if self.emit_if_focused and not self.hasFocus(): return - ffamily = self.currentFont().family().lower() + ffamily = self.currentFont().family() if self._current_font != ffamily: self.apply_fontfamily() diff --git a/ui/mainwindow.py b/ui/mainwindow.py index e2dd316f..349f0d3a 100644 --- a/ui/mainwindow.py +++ b/ui/mainwindow.py @@ -14,7 +14,7 @@ from utils.textblock import TextBlock from utils import shared from modules.translators.trans_chatgpt import GPTTranslator -from .misc import parse_stylesheet +from .misc import parse_stylesheet, set_html_family from utils.config import ProgramConfig, pcfg, save_config, text_styles, save_text_styles, load_textstyle_from from utils.fontformat import pt2px from .config_proj import ProjImgTrans @@ -870,6 +870,7 @@ def on_pagtrans_finished(self, page_index: int): override_alignment = pcfg.let_alignment_flag == 1 override_effect = pcfg.let_fnteffect_flag == 1 override_writing_mode = pcfg.let_writing_mode_flag == 1 + override_font_family = pcfg.let_family_flag == 1 gf = self.textPanel.formatpanel.global_format inpaint_only = pcfg.module.enable_inpaint @@ -900,6 +901,11 @@ def on_pagtrans_finished(self, page_index: int): blk.shadow_offset = gf.shadow_offset if override_writing_mode: blk.vertical = gf.vertical + if override_font_family: + blk.font_family = gf.family + if blk.rich_text: + blk.rich_text = set_html_family(blk.rich_text, gf.family) + print(blk.rich_text) blk.line_spacing = gf.line_spacing blk.letter_spacing = gf.letter_spacing diff --git a/ui/misc.py b/ui/misc.py index 65285858..dfcc1ff7 100644 --- a/ui/misc.py +++ b/ui/misc.py @@ -109,6 +109,7 @@ def __eq__(self, other): td_pattern = re.compile(r'(.*?)', re.DOTALL) table_pattern = re.compile(r'(.*?) float: size_list = fontsize_pattern.findall(html) diff --git a/utils/config.py b/utils/config.py index e37c8825..d42511d3 100644 --- a/utils/config.py +++ b/utils/config.py @@ -79,6 +79,7 @@ class ProgramConfig(Config): let_fnteffect_flag: int = 1 let_alignment_flag: int = 0 let_writing_mode_flag: int = 0 + let_family_flag: int = 0 let_autolayout_flag: bool = True let_autolayout_adaptive_fntsz: bool = True let_uppercase_flag: bool = True @@ -173,7 +174,6 @@ def load_textstyle_from(p: str, raise_exception = False): text_styles.clear() text_styles.extend(styles_loaded) pcfg.text_styles_path = p - LOGGER.info(f'Text style loaded from {p}') def load_config():