Skip to content

Commit

Permalink
Allow replace font family #388, fix global font format
Browse files Browse the repository at this point in the history
  • Loading branch information
dmMaze committed Feb 29, 2024
1 parent fe037dc commit 66db26a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions ui/configpanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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()

Expand Down
9 changes: 7 additions & 2 deletions ui/fontformat_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions ui/fontformatpanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
8 changes: 7 additions & 1 deletion ui/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions ui/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def __eq__(self, other):
td_pattern = re.compile(r'<td(.*?)>(.*?)</td>', re.DOTALL)
table_pattern = re.compile(r'(.*?)<table', re.DOTALL)
fontsize_pattern = re.compile(r'font-size:(.*?)pt;', re.DOTALL)
ffamily_pattern = re.compile(r'font-family:\'(.*?)\'', re.DOTALL)


def span_repl_func(matched, color):
Expand All @@ -127,6 +128,9 @@ def set_html_color(html, rgb):
return color_pattern.sub(f'color:{hex_color};', html)
else:
return span_pattern.sub(lambda matched: span_repl_func(matched, hex_color), html)

def set_html_family(html, family):
return ffamily_pattern.sub(f'font-family:\'{family}\'', html)

def html_max_fontsize(html: str) -> float:
size_list = fontsize_pattern.findall(html)
Expand Down
2 changes: 1 addition & 1 deletion utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():

Expand Down

0 comments on commit 66db26a

Please sign in to comment.