Commit 0d2879e6 authored by dmMaze's avatar dmMaze
Browse files

Allow replace font family #388, fix global font format

parent fe037dcb
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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)
+7 −0
Original line number Diff line number Diff line
@@ -452,6 +452,10 @@ class ConfigPanel(Widget):
        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 @@ class ConfigPanel(Widget):
    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()

+7 −2
Original line number Diff line number Diff line
@@ -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):
+4 −4
Original line number Diff line number Diff line
@@ -328,22 +328,22 @@ class FontFamilyComboBox(QFontComboBox):
        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()
            
+6 −1
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ from utils.text_processing import is_cjk, full_len, half_len
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 @@ class MainWindow(FramelessWindow):
        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,10 @@ class MainWindow(FramelessWindow):
                    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)
                
                blk.line_spacing = gf.line_spacing
                blk.letter_spacing = gf.letter_spacing
Loading