From 01c1669cb978f1181bcff46d6d6070f3f8ee0b54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Thu, 12 Sep 2024 21:01:26 +0800 Subject: [PATCH] ... --- .../LunaTranslator/LunaTranslator_main.py | 8 +- .../gui/dialog_savedgame_setting.py | 10 +- .../gui/setting_display_text.py | 1 + .../LunaTranslator/gui/setting_lang.py | 18 +- .../LunaTranslator/gui/setting_translate.py | 12 +- .../LunaTranslator/gui/textbrowser.py | 9 +- .../LunaTranslator/gui/translatorUI.py | 11 +- .../LunaTranslator/myutils/commonbase.py | 4 +- .../LunaTranslator/myutils/config.py | 16 +- .../LunaTranslator/myutils/utils.py | 19 +- .../LunaTranslator/ocrengines/windowsocr.py | 11 +- .../LunaTranslator/rendertext/textbrowser.py | 34 +- .../rendertext/textbrowser_imp/yinying.py | 27 +- .../LunaTranslator/rendertext/webview.html | 15 +- .../LunaTranslator/rendertext/webview.py | 18 +- .../LunaTranslator/translator/atlas.py | 61 ++ .../LunaTranslator/translator/baidu.py | 2 + .../LunaTranslator/translator/baidu_ai.py | 1 + .../LunaTranslator/translator/baidu_dev.py | 3 + .../LunaTranslator/translator/baiduapi.py | 2 + .../LunaTranslator/translator/deepl.py | 2 +- .../translator/deeplapi-free.py | 2 +- .../LunaTranslator/translator/papago.py | 21 +- .../files/defaultconfig/config.json | 26 +- .../files/defaultconfig/static_data.json | 292 ++++++--- .../defaultconfig/translatorsetting.json | 16 +- LunaTranslator/files/lang/ar.json | 3 +- LunaTranslator/files/lang/cht.json | 3 +- LunaTranslator/files/lang/cs.json | 3 +- LunaTranslator/files/lang/de.json | 3 +- LunaTranslator/files/lang/en.json | 3 +- LunaTranslator/files/lang/es.json | 3 +- LunaTranslator/files/lang/fr.json | 3 +- LunaTranslator/files/lang/it.json | 3 +- LunaTranslator/files/lang/ja.json | 3 +- LunaTranslator/files/lang/ko.json | 3 +- LunaTranslator/files/lang/nl.json | 3 +- LunaTranslator/files/lang/pl.json | 3 +- LunaTranslator/files/lang/pt.json | 3 +- LunaTranslator/files/lang/ru.json | 3 +- LunaTranslator/files/lang/sv.json | 3 +- LunaTranslator/files/lang/th.json | 3 +- LunaTranslator/files/lang/tr.json | 3 +- LunaTranslator/files/lang/uk.json | 3 +- LunaTranslator/files/lang/vi.json | 3 +- LunaTranslator/files/lang/zh.json | 4 +- README.md | 2 + build.py | 2 +- docs/zh/alltoolbuttons.md | 3 +- docs/zh/fastkeys.md | 2 +- docs/zh/offlinellm.md | 2 +- ...urallmkagglecolab.md => sakurallmcolab.md} | 26 +- docs/zh/sidebar.md | 2 +- plugins/CMakeLists.txt | 4 +- plugins/shareddllproxy/Atlas.cpp | 601 ++++++++++++++++++ plugins/shareddllproxy/Atlas.h | 49 ++ plugins/shareddllproxy/CMakeLists.txt | 2 +- plugins/shareddllproxy/shareddllproxy.cpp | 2 + 58 files changed, 1138 insertions(+), 261 deletions(-) create mode 100644 LunaTranslator/LunaTranslator/translator/atlas.py rename docs/zh/{sakurallmkagglecolab.md => sakurallmcolab.md} (70%) create mode 100644 plugins/shareddllproxy/Atlas.cpp create mode 100644 plugins/shareddllproxy/Atlas.h diff --git a/LunaTranslator/LunaTranslator/LunaTranslator_main.py b/LunaTranslator/LunaTranslator/LunaTranslator_main.py index 1dd61eb5..c1351a5d 100644 --- a/LunaTranslator/LunaTranslator/LunaTranslator_main.py +++ b/LunaTranslator/LunaTranslator/LunaTranslator_main.py @@ -67,7 +67,7 @@ def loadmainui(): def checklang(): - from myutils.config import globalconfig, oldlanguage, loadlangviss + from myutils.config import globalconfig, oldlanguage, static_data from qtsymbols import ( QDialog, pyqtSignal, @@ -96,10 +96,12 @@ def checklang(): self.setFont(font) self.current = "zh" language_listcombox = QComboBox() - _, vis = loadlangviss() + inner, vis = [_[1] for _ in static_data["language_list_show"]], [ + _[0] for _ in static_data["language_list_show"] + ] language_listcombox.addItems(vis) language_listcombox.currentIndexChanged.connect( - lambda x: setattr(self, "current", _[x]) + lambda x: setattr(self, "current", inner[x]) ) vb = QVBoxLayout(self) diff --git a/LunaTranslator/LunaTranslator/gui/dialog_savedgame_setting.py b/LunaTranslator/LunaTranslator/gui/dialog_savedgame_setting.py index 9775eab1..fe6f33ba 100644 --- a/LunaTranslator/LunaTranslator/gui/dialog_savedgame_setting.py +++ b/LunaTranslator/LunaTranslator/gui/dialog_savedgame_setting.py @@ -268,7 +268,7 @@ def maybehavebutton(self, gameuid, post): callback = functools.partial( autoinitdialog, self, - save_text_process_info["postprocessconfig"][post]['args'], + save_text_process_info["postprocessconfig"][post]["args"], postprocessconfig[post]["name"], 600, items, @@ -1032,19 +1032,19 @@ class dialog_setting_game_internal(QWidget): formLayout2.addRow( "源语言", getsimplecombobox( - static_data["language_list_translator"], + [_["zh"] for _ in static_data["lang_list_all"]], savehook_new_data[gameuid], "private_srclang_2", - internal=static_data["language_list_translator_inner"], + internal=[_["code"] for _ in static_data["lang_list_all"]], ), ) formLayout2.addRow( "目标语言", getsimplecombobox( - static_data["language_list_translator"], + [_["zh"] for _ in static_data["lang_list_all"]], savehook_new_data[gameuid], "private_tgtlang_2", - internal=static_data["language_list_translator_inner"], + internal=[_["code"] for _ in static_data["lang_list_all"]], ), ) diff --git a/LunaTranslator/LunaTranslator/gui/setting_display_text.py b/LunaTranslator/LunaTranslator/gui/setting_display_text.py index 95b81865..f1ba9202 100644 --- a/LunaTranslator/LunaTranslator/gui/setting_display_text.py +++ b/LunaTranslator/LunaTranslator/gui/setting_display_text.py @@ -34,6 +34,7 @@ from gui.dynalang import LPushButton, LFormLayout def __changeuibuttonstate(self, x): gobject.baseobject.translation_ui.refreshtoolicon() + gobject.baseobject.translation_ui.translate_text.textbrowser.showhideorigin(x) try: self.fenyinsettings.setEnabled(x) except: diff --git a/LunaTranslator/LunaTranslator/gui/setting_lang.py b/LunaTranslator/LunaTranslator/gui/setting_lang.py index 2d9661dc..11909e49 100644 --- a/LunaTranslator/LunaTranslator/gui/setting_lang.py +++ b/LunaTranslator/LunaTranslator/gui/setting_lang.py @@ -1,5 +1,5 @@ import os -from myutils.config import globalconfig, static_data, getlanguse, loadlangviss +from myutils.config import globalconfig, static_data, getlanguse from gui.usefulwidget import ( D_getsimplecombobox, D_getIconButton, @@ -14,7 +14,9 @@ def changelang(_): def setTablanglz(self): - inner, vis = loadlangviss() + inner, vis = [_[1] for _ in static_data["language_list_show"]], [ + _[0] for _ in static_data["language_list_show"] + ] return [ [ ( @@ -25,22 +27,22 @@ def setTablanglz(self): [ "源语言", D_getsimplecombobox( - static_data["language_list_translator"], + [_["zh"] for _ in static_data["lang_list_all"]], globalconfig, "srclang4", - internal=static_data[ - "language_list_translator_inner" + internal=[ + _["code"] for _ in static_data["lang_list_all"] ], ), ], [ "目标语言", D_getsimplecombobox( - static_data["language_list_translator"], + [_["zh"] for _ in static_data["lang_list_all"]], globalconfig, "tgtlang4", - internal=static_data[ - "language_list_translator_inner" + internal=[ + _["code"] for _ in static_data["lang_list_all"] ], ), ], diff --git a/LunaTranslator/LunaTranslator/gui/setting_translate.py b/LunaTranslator/LunaTranslator/gui/setting_translate.py index 2e178978..082e6f29 100644 --- a/LunaTranslator/LunaTranslator/gui/setting_translate.py +++ b/LunaTranslator/LunaTranslator/gui/setting_translate.py @@ -152,11 +152,11 @@ def selectllmcallback(self, countnum, btnplus, fanyi, name): callback=functools.partial( autoinitdialogx, self, - translatorsetting[uid]['args'], + translatorsetting[uid]["args"], (globalconfig["fanyi"][uid]["name"]), 800, items, - "userconfig.copyed."+uid, + "userconfig.copyed." + uid, uid, ), icon="fa.gear", @@ -308,7 +308,7 @@ def initsome11(self, l, label=None, btnplus=False): line = [] countnum = [] for fanyi in l: - which=translate_exits(fanyi,which=True) + which = translate_exits(fanyi, which=True) if which is None: continue i += 1 @@ -316,8 +316,8 @@ def initsome11(self, l, label=None, btnplus=False): if fanyi in translatorsetting: items = autoinitdialog_items(translatorsetting[fanyi]) - - if which==0: + + if which == 0: aclass = "translator." + fanyi elif which == 1: aclass = "userconfig.copyed." + fanyi @@ -325,7 +325,7 @@ def initsome11(self, l, label=None, btnplus=False): callback=functools.partial( autoinitdialogx, self, - translatorsetting[fanyi]['args'], + translatorsetting[fanyi]["args"], globalconfig["fanyi"][fanyi]["name"], 800, items, diff --git a/LunaTranslator/LunaTranslator/gui/textbrowser.py b/LunaTranslator/LunaTranslator/gui/textbrowser.py index 43499c2e..4e00873e 100644 --- a/LunaTranslator/LunaTranslator/gui/textbrowser.py +++ b/LunaTranslator/LunaTranslator/gui/textbrowser.py @@ -54,6 +54,7 @@ class Textbrowser(QFrame): self.textbrowser.resize(size) self.textbrowser.show() self.textbrowser.setselectable(globalconfig["selectable"]) + self.textbrowser.showhideorigin(globalconfig["isshowrawtext"]) def __init__(self, parent): super().__init__(parent) @@ -64,16 +65,12 @@ class Textbrowser(QFrame): self.loadinternal() def iter_append(self, iter_context_class, origin, atcenter, text, color): - cleared = self.cleared self.cleared = False - self.textbrowser.iter_append( - iter_context_class, origin, atcenter, text, color, cleared - ) + self.textbrowser.iter_append(iter_context_class, origin, atcenter, text, color) def append(self, origin, atcenter, text, tag, flags, color): - cleared = self.cleared self.cleared = False - self.textbrowser.append(origin, atcenter, text, tag, flags, color, cleared) + self.textbrowser.append(origin, atcenter, text, tag, flags, color) def clear(self): self.cleared = True diff --git a/LunaTranslator/LunaTranslator/gui/translatorUI.py b/LunaTranslator/LunaTranslator/gui/translatorUI.py index bb795114..ff889120 100644 --- a/LunaTranslator/LunaTranslator/gui/translatorUI.py +++ b/LunaTranslator/LunaTranslator/gui/translatorUI.py @@ -328,10 +328,7 @@ class TranslatorWindow(resizableframeless): color = kwargs.get("color") clear = True - if globalconfig["isshowrawtext"]: - self.showline(clear=clear, text=text, isshowrawtext=True, color=color) - else: - self.showline(clear=clear) + self.showline(clear=clear, text=text, color=color) def showstatus(self, res, color, clear, origin): self.showline(clear=clear, text=res, color=color, origin=origin) @@ -351,7 +348,6 @@ class TranslatorWindow(resizableframeless): origin = kwargs.get("origin", True) text = kwargs.get("text", None) color = kwargs.get("color", "black") - isshowrawtext = kwargs.get("isshowrawtext", False) iter_context = kwargs.get("iter_context", None) if clear: @@ -373,7 +369,7 @@ class TranslatorWindow(resizableframeless): else: hira = [] isshowhira = isshow_fenci = isfenciclick = False - if isshowrawtext: + if origin: isshowhira = globalconfig["isshowhira"] isshow_fenci = globalconfig["show_fenci"] isfenciclick = ( @@ -1074,6 +1070,9 @@ class TranslatorWindow(resizableframeless): except: globalconfig["isshowrawtext"] = not globalconfig["isshowrawtext"] self.refreshtoolicon() + self.translate_text.textbrowser.showhideorigin( + globalconfig["isshowrawtext"] + ) def changeshowhidetrans(self): try: diff --git a/LunaTranslator/LunaTranslator/myutils/commonbase.py b/LunaTranslator/LunaTranslator/myutils/commonbase.py index 122e8f1c..dc012681 100644 --- a/LunaTranslator/LunaTranslator/myutils/commonbase.py +++ b/LunaTranslator/LunaTranslator/myutils/commonbase.py @@ -88,8 +88,8 @@ class commonbase: def langmap_(self): _ = dict( zip( - static_data["language_list_translator_inner"], - static_data["language_list_translator_inner"], + [_["code"] for _ in static_data["lang_list_all"]], + [_["code"] for _ in static_data["lang_list_all"]], ) ) _.update({"cht": "zh"}) diff --git a/LunaTranslator/LunaTranslator/myutils/config.py b/LunaTranslator/LunaTranslator/myutils/config.py index 2e3ab8c3..8519b17d 100644 --- a/LunaTranslator/LunaTranslator/myutils/config.py +++ b/LunaTranslator/LunaTranslator/myutils/config.py @@ -528,18 +528,6 @@ def langfile(lang): return "./files/lang/{}.json".format(lang) -def loadlangviss(): - inners = [] - vis = [] - for i, l in enumerate(static_data["language_list_translator_inner"]): - if not os.path.exists(langfile(l)): - continue - inners.append(l) - vis.append(static_data["language_list_show"][i]) - - return inners, vis - - def loadlanguage(): global language_last, languageshow _language = getlanguse() @@ -657,8 +645,8 @@ def _TRL(kk): def getlang_inner2show(langcode): return dict( zip( - static_data["language_list_translator_inner"], - static_data["language_list_translator"], + [_["code"] for _ in static_data["lang_list_all"]], + [_["zh"] for _ in static_data["lang_list_all"]], ) ).get(langcode, "??") diff --git a/LunaTranslator/LunaTranslator/myutils/utils.py b/LunaTranslator/LunaTranslator/myutils/utils.py index 99f253d6..e8a2ceb9 100644 --- a/LunaTranslator/LunaTranslator/myutils/utils.py +++ b/LunaTranslator/LunaTranslator/myutils/utils.py @@ -569,14 +569,21 @@ def parsemayberegexreplace(lst: list, line: str): return line +def checklangisusing(langs): + if langs is None: + return True + elif isinstance(langs, list): + return getlanguse() in langs + elif isinstance(langs, str): + return getlanguse() == langs + raise Exception(langs) + + def checkpostlangmatch(name): for item in static_data["transoptimi"]: if name == item["name"]: try: - if isinstance(item["languageuse"], list): - return getlanguse() in item["languageuse"] - elif isinstance(item["languageuse"], str): - return getlanguse() == item["languageuse"] + return checklangisusing(item.get("languageuse", None)) except: return True @@ -913,7 +920,7 @@ def createurl(url: str): def createenglishlangmap(): return dict( zip( - static_data["language_list_translator_inner"], - static_data["language_list_translator_inner_english"], + [_["code"] for _ in static_data["lang_list_all"]], + [_["en"] for _ in static_data["lang_list_all"]], ) ) diff --git a/LunaTranslator/LunaTranslator/ocrengines/windowsocr.py b/LunaTranslator/LunaTranslator/ocrengines/windowsocr.py index b6a0c049..abc64ee9 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/windowsocr.py +++ b/LunaTranslator/LunaTranslator/ocrengines/windowsocr.py @@ -11,7 +11,7 @@ from gui.dynalang import LPushButton, LFormLayout, LLabel def initsupports(): _allsupport = winrtutils.getlanguagelist() supportmap = {} - for lang in static_data["language_list_translator_inner"] + [ + for lang in [_["code"] for _ in static_data["lang_list_all"]] + [ "zh-Hans", "zh-Hant", ]: @@ -38,7 +38,9 @@ def question(dialog: QDialog): supportlang.setText("_,_".join([getlang_inner2show(f) for f in _allsupport])) btndownload = LPushButton("添加语言包") btndownload.clicked.connect( - lambda: gobject.baseobject.openlink(dynamiclink("{docs_server}/#/zh/windowsocr")) + lambda: gobject.baseobject.openlink( + dynamiclink("{docs_server}/#/zh/windowsocr") + ) ) formLayout.addRow( "当前支持的语言", getboxlayout([supportlang, btndownload], makewidget=True) @@ -53,11 +55,11 @@ class OCR(baseocr): if self.srclang not in self.supportmap: _allsupport = initsupports() - idx = static_data["language_list_translator_inner"].index(self.srclang) + idx = [_["code"] for _ in static_data["lang_list_all"]].index(self.srclang) raise Exception( _TR("系统未安装") + ' "' - + _TR(static_data["language_list_translator"][idx]) + + _TR([_["zh"] for _ in static_data["lang_list_all"]][idx]) + '" ' + _TR("的OCR模型") + "\n" @@ -66,7 +68,6 @@ class OCR(baseocr): + ", ".join([_TR(getlang_inner2show(f)) for f in _allsupport]) ) - ret = winrtutils.OCR_f(imagebinary, self.supportmap[self.srclang], self.space) boxs = [_[1:] for _ in ret] texts = [_[0] for _ in ret] diff --git a/LunaTranslator/LunaTranslator/rendertext/textbrowser.py b/LunaTranslator/LunaTranslator/rendertext/textbrowser.py index 217c50a8..ef6cc8fd 100644 --- a/LunaTranslator/LunaTranslator/rendertext/textbrowser.py +++ b/LunaTranslator/LunaTranslator/rendertext/textbrowser.py @@ -131,6 +131,7 @@ class TextBrowser(QWidget, dataget): self.iteryinyinglabelsave = {} self.saveiterclasspointer = {} self.extra_height = 0 + self.trace = [] self.resets1() def resets1(self): @@ -198,16 +199,19 @@ class TextBrowser(QWidget, dataget): c.setCharFormat(f) self.textbrowser.setTextCursor(c) - def iter_append(self, iter_context_class, origin, atcenter, text, color, cleared): - self._textbrowser_iter_append( - iter_context_class, origin, atcenter, text, color, cleared - ) + def showhideorigin(self, show): + traces = self.trace.copy() + self.clear() + for t, trace in traces: + if t == 0: + self.append(*trace) + elif t == 1: + self.iter_append(*trace) - def _textbrowser_iter_append( - self, iter_context_class, origin, atcenter, text, color, cleared - ): + def iter_append(self, iter_context_class, origin, atcenter, text, color): + self.trace.append((1, (iter_context_class, origin, atcenter, text, color))) if iter_context_class not in self.saveiterclasspointer: - self._textbrowser_append(origin, atcenter, "", [], color, cleared) + self._textbrowser_append(origin, atcenter, "", [], color) self.saveiterclasspointer[iter_context_class] = { "currtext": "", "curr": self._getcurrpointer(), @@ -247,16 +251,18 @@ class TextBrowser(QWidget, dataget): self._createqfont(origin), ) - def append(self, origin, atcenter, text, tag, flags, color, cleared): + def append(self, origin, atcenter, text, tag, flags, color): + self.trace.append((0, (origin, atcenter, text, tag, flags, color))) + if origin and not globalconfig["isshowrawtext"]: + return isshowhira, isshow_fenci, isfenciclick = flags if len(tag): font = self._createqfont(origin) textlines, linetags = self._splitlinestags(font, tag, text) text = "\n".join(textlines) tag = self._join_tags(linetags, True) - self._textbrowser_append( - origin, atcenter, text, tag if isshowhira else [], color, cleared + origin, atcenter, text, tag if isshowhira else [], color ) if isshow_fenci or isfenciclick: self.addsearchwordmask(isshow_fenci, isfenciclick, tag) @@ -264,11 +270,10 @@ class TextBrowser(QWidget, dataget): def _getqalignment(self, atcenter): return Qt.AlignmentFlag.AlignCenter if atcenter else Qt.AlignmentFlag.AlignLeft - def _textbrowser_append( - self, origin, atcenter, text: str, tag: list, color, cleared - ): + def _textbrowser_append(self, origin, atcenter, text: str, tag: list, color): self.textbrowser.document().blockSignals(True) font = self._createqfont(origin) + cleared = len(self.textbrowser.toPlainText()) == 0 self._setnextfont(font, cleared) self.textbrowser.setAlignment(self._getqalignment(atcenter)) @@ -722,3 +727,4 @@ class TextBrowser(QWidget, dataget): self.saveiterclasspointer.clear() self.textbrowser.move(0, 0) self.atback_color.move(0, 0) + self.trace = [] diff --git a/LunaTranslator/LunaTranslator/rendertext/textbrowser_imp/yinying.py b/LunaTranslator/LunaTranslator/rendertext/textbrowser_imp/yinying.py index 55b2ea21..5608491c 100644 --- a/LunaTranslator/LunaTranslator/rendertext/textbrowser_imp/yinying.py +++ b/LunaTranslator/LunaTranslator/rendertext/textbrowser_imp/yinying.py @@ -12,6 +12,31 @@ class QGraphicsDropShadowEffect_multi(QGraphicsDropShadowEffect): super().draw(painter) +class CachedQGraphicsDropShadowEffect_multi(QGraphicsDropShadowEffect): + def __init__(self, parent=None, x=1): + super().__init__(parent) + self.shadow_pixmap = QPixmap() + self.x = x + + def draw(self, painter): + r = self.parent().devicePixelRatioF() + if self.shadow_pixmap.isNull(): + size = QSize(painter.device().width(), painter.device().height()) * r + self.shadow_pixmap = QPixmap(size) + self.shadow_pixmap.setDevicePixelRatio(r) + self.shadow_pixmap.fill(Qt.GlobalColor.transparent) + shadow_painter = QPainter(self.shadow_pixmap) + shadow_painter.setRenderHint(QPainter.RenderHint.Antialiasing) + for _ in range(self.x): + super().draw(shadow_painter) + painter.setRenderHint(QPainter.RenderHint.Antialiasing) + painter.drawPixmap( + -int(self.parent().x()), + -int(self.parent().y()) - self.parent().parent().parent().parent().y(), + self.shadow_pixmap, + ) + + class TextLine(TextLabel_0): def usingcolor(self): @@ -19,7 +44,7 @@ class TextLine(TextLabel_0): def setShadow_internal(self, colorshadow, width=1, deepth=1): - shadow2 = QGraphicsDropShadowEffect_multi(deepth) + shadow2 = CachedQGraphicsDropShadowEffect_multi(self, deepth) shadow2.setBlurRadius(width) shadow2.setOffset(0) diff --git a/LunaTranslator/LunaTranslator/rendertext/webview.html b/LunaTranslator/LunaTranslator/rendertext/webview.html index be63147f..4a263d69 100644 --- a/LunaTranslator/LunaTranslator/rendertext/webview.html +++ b/LunaTranslator/LunaTranslator/rendertext/webview.html @@ -215,10 +215,22 @@ return wrap } +