From 666b3ca48232f516b2c93281e68cac800e535c25 Mon Sep 17 00:00:00 2001 From: CHLBC <42777336+chlbc@users.noreply.github.com> Date: Tue, 16 Jul 2024 17:05:26 +0800 Subject: [PATCH] Improve Font Setting of First Execution (#897) * init default font change * rename * improve default font list * fix --- .../LunaTranslator/LunaTranslator.py | 10 ++++-- .../LunaTranslator/myutils/config.py | 35 +++++++++++++++++++ .../files/defaultconfig/static_data.json | 18 ++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/LunaTranslator/LunaTranslator/LunaTranslator.py b/LunaTranslator/LunaTranslator/LunaTranslator.py index f0a43d6d..dfa0b0bb 100644 --- a/LunaTranslator/LunaTranslator/LunaTranslator.py +++ b/LunaTranslator/LunaTranslator/LunaTranslator.py @@ -12,6 +12,8 @@ from myutils.config import ( setlanguage, static_data, tryreadconfig, + getlanguse, + set_font_default, ) import sqlite3 from myutils.utils import ( @@ -1186,9 +1188,11 @@ class MAINUI: def parsedefaultfont(self): for k in ["fonttype", "fonttype2", "settingfonttype"]: if globalconfig[k] == "": - globalconfig[k] = QFontDatabase.systemFont( - QFontDatabase.SystemFont.GeneralFont - ).family() + l = "ja" if k=="fonttype" else getlanguse() + set_font_default(l, k) + # globalconfig[k] = QFontDatabase.systemFont( + # QFontDatabase.SystemFont.GeneralFont + # ).family() def loadui(self): self.installeventfillter() diff --git a/LunaTranslator/LunaTranslator/myutils/config.py b/LunaTranslator/LunaTranslator/myutils/config.py index d745d97c..624a03fa 100644 --- a/LunaTranslator/LunaTranslator/myutils/config.py +++ b/LunaTranslator/LunaTranslator/myutils/config.py @@ -1,6 +1,7 @@ import json import os, time, uuid from traceback import print_exc +from qtsymbols import * def tryreadconfig(path, default=None): @@ -488,3 +489,37 @@ def saveallconfig(): "./files/lang/{}.json".format(getlanguse()), languageshow, ) + + +def is_font_installed(font: str) -> bool: + return QFont(font).exactMatch() + + +# font_default_used = {} + + +def get_font_default(lang: str, issetting: bool) -> str: + # global font_default_used + # if lang in font_default_used.keys(): + # return font_default_used[lang] + + t = "setting_font_type_default" if issetting else "font_type_default" + l = lang if lang in static_data[t].keys() else "default" + + font_default = "" + for font in static_data[t][l]: + if is_font_installed(font): + font_default = font + break + if font_default == "": + font_default = QFontDatabase.systemFont( + QFontDatabase.SystemFont.GeneralFont + ).family() + + # font_default_used["lang"] = font_default + return font_default + + +def set_font_default(lang: str, fonttype: str) -> None: + globalconfig[fonttype] = get_font_default(lang, True if fonttype=="settingfonttype" else False) + diff --git a/LunaTranslator/files/defaultconfig/static_data.json b/LunaTranslator/files/defaultconfig/static_data.json index a322e8b3..db50567b 100644 --- a/LunaTranslator/files/defaultconfig/static_data.json +++ b/LunaTranslator/files/defaultconfig/static_data.json @@ -99,6 +99,24 @@ "th", "bo" ], + "font_type_default": { + "default": ["Segoe UI", "Arial", "Tahoma", "Microsoft Sans Serif"], + "zh": ["微软雅黑", "Microsoft YaHei", "黑体", "SimHei", "新宋体", "NSimSun", "宋体", "SimSun"], + "ja": ["游ゴシック", "Yu Gothic", "メイリオ", "Meiryo", "MS Pゴシック", "MS PGothic", "MS ゴシック", "MS Gothic"], + "ko": ["맑은 고딕", "Malgun Gothic", "돋움체", "Dotum", "굴림체", "Gulim", "바탕체", "Batang"], + "cht": ["微軟正黑體", "Microsoft JhengHei", "標楷體", "DFKai-SB", "新細明體", "PMingLiU", "細明體", "MingLiU"], + "th": ["Leelawadee", "Tahoma", "Microsoft Sans Serif"], + "bo": ["Microsoft Himalaya", "Noto Serif Tibetan", "Tibetan Machine Uni"] + }, + "setting_font_type_default": { + "default": ["Segoe UI", "Tahoma", "Microsoft Sans Serif", "Arial"], + "zh": ["Microsoft YaHei UI", "微软雅黑", "Microsoft YaHei", "黑体", "SimHei", "新宋体", "NSimSun", "宋体", "SimSun"], + "ja": ["Yu Gothic UI", "游ゴシック", "Yu Gothic", "Meiryo UI", "メイリオ", "Meiryo", "MS UI Gothic", "MS Pゴシック", "MS PGothic", "MS ゴシック", "MS Gothic"], + "ko": ["맑은 고딕", "Malgun Gothic", "굴림체", "Gulim", "돋움체", "Dotum", "바탕체", "Batang"], + "cht": ["Microsoft JhengHei UI", "微軟正黑體", "Microsoft JhengHei", "新細明體", "PMingLiU", "細明體", "MingLiU"], + "th": ["Leelawadee UI", "Tahoma", "Microsoft Sans Serif"], + "bo": ["Microsoft YaHei UI", "微软雅黑", "Microsoft YaHei", "黑体", "SimHei", "新宋体", "NSimSun", "宋体", "SimSun"] + }, "codepage_display": [ "日语(CP932,SHIFT-JIS)", "UTF8(CP65001)",