From ab8b7bc95fcb457f54137b97113fbc51d6df7356 Mon Sep 17 00:00:00 2001 From: NSoiffer Date: Wed, 23 Aug 2023 20:57:12 +0100 Subject: [PATCH] Add CMU to menu Fix up "Auto" for language setting. Auto follows the speech language of the voice. --- addon/globalPlugins/MathCAT/MathCAT.py | 39 +++++++++++++++---- .../MathCAT/MathCATPreferences.py | 34 ++++++++++------ addon/globalPlugins/MathCAT/MathCATgui.py | 2 +- 3 files changed, 55 insertions(+), 20 deletions(-) diff --git a/addon/globalPlugins/MathCAT/MathCAT.py b/addon/globalPlugins/MathCAT/MathCAT.py index 48e357db..55860eac 100644 --- a/addon/globalPlugins/MathCAT/MathCAT.py +++ b/addon/globalPlugins/MathCAT/MathCAT.py @@ -85,7 +85,7 @@ def getLanguageToUse(mathMl:str) -> str: def ConvertSSMLTextForNVDA(text:str, language:str="") -> list: # MathCAT's default rate is 180 wpm. # Assume that 0% is 80 wpm and 100% is 450 wpm and scale accordingly. - # log.info(f"\nSpeech str: '{text}'") + # log.info(f"\nSpeech ({language}) str: '{text}'") if language == "": # shouldn't happen language = "en" # fallback to what was being used mathCATLanguageSetting = "en" # fallback in case GetPreference fails for unknown reasons @@ -109,7 +109,7 @@ def ConvertSSMLTextForNVDA(text:str, language:str="") -> list: out = [] if mathCATLanguageSetting != language: try: - # log.info(f"Setting language to {language}") + # log.info(f"ConvertSSMLTextForNVDA: setting language to {language}") libmathcat.SetPreference("Language", language) out.append(LangChangeCommand(language)) except Exception as e: @@ -153,6 +153,7 @@ def ConvertSSMLTextForNVDA(text:str, language:str="") -> list: out.extend((" ", m.group(0), " ")) if mathCATLanguageSetting != language: try: + # log.info(f" ...: resetting language to {language}") libmathcat.SetPreference("Language", mathCATLanguageSetting) out.append(LangChangeCommand(None)) except Exception as e: @@ -160,10 +161,26 @@ def ConvertSSMLTextForNVDA(text:str, language:str="") -> list: # log.info(f"Speech commands: '{out}'") return out +def GetSpokenTextUsingLanguage(lang: str) -> str: + """ + Returns the MathCAT generated text in the given language. This is used to resolve 'Auto' + """ + try: + old_lang = libmathcat.GetPreference("Language") + libmathcat.SetPreference("Language", lang) + text = libmathcat.GetSpokenText() + libmathcat.SetPreference("Language", old_lang) + except Exception as e: + log.error(e) + speech.speakMessage(_("Error in starting navigation of math: see NVDA error log for details")) + text = _("Error") + return text + class MathCATInteraction(mathPres.MathInteractionNVDAObject): - # Put MathML on the clipboard using the two formats below (defined by MathML spec) - # We use both formats because some apps may only use one or the other - # Note: filed https://github.com/nvaccess/nvda/issues/13240 to make this usable outside of MathCAT + """Put MathML on the clipboard using the two formats below (defined by MathML spec) + We use both formats because some apps may only use one or the other + Note: filed https://github.com/nvaccess/nvda/issues/13240 to make this usable outside of MathCAT + """ CF_MathML = windll.user32.RegisterClipboardFormatW("MathML") CF_MathML_Presentation = windll.user32.RegisterClipboardFormatW("MathML Presentation") # log.info("2**** MathCAT registering data formats: CF_MathML %x, CF_MathML_Presentation %x" % (CF_MathML, CF_MathML_Presentation)) @@ -177,7 +194,10 @@ def __init__(self, provider=None, mathMl: Optional[str]=None): def reportFocus(self): super(MathCATInteraction, self).reportFocus() try: + old_lang = libmathcat.GetPreference("Language") # handle Auto + libmathcat.SetPreference("Language", self._language) text = libmathcat.DoNavigateCommand("ZoomIn") + libmathcat.SetPreference("Language", old_lang) speech.speak(ConvertSSMLTextForNVDA(text, self._language)) except Exception as e: log.error(e) @@ -219,8 +239,11 @@ def script_navigate(self, gesture: KeyboardInputGesture): try: if gesture != None: # == None when initial focus -- handled in reportFocus() modNames = gesture.modifierNames + old_lang = libmathcat.GetPreference("Language") # handle Auto + libmathcat.SetPreference("Language", self._language) text = libmathcat.DoNavigateKeyPress(gesture.vkCode, "shift" in modNames, "control" in modNames, "alt" in modNames, False) + libmathcat.SetPreference("Language", old_lang) speech.speak(ConvertSSMLTextForNVDA(text, self._language)) # update the braille to reflect the nav position (might be excess code, but it works) @@ -362,9 +385,11 @@ def getSpeechForMathMl(self, mathml: str): if PitchCommand in supported_commands: libmathcat.SetPreference("CapitalLetters_Pitch", str(synthConfig["capPitchChange"])) if self._add_sounds(): - return [BeepCommand(800,25)] + ConvertSSMLTextForNVDA(libmathcat.GetSpokenText(), self._language) + [BeepCommand(600,15)] + return [BeepCommand(800,25)] + \ + ConvertSSMLTextForNVDA(GetSpokenTextUsingLanguage(self._language), self._language) + \ + [BeepCommand(600,15)] else: - return ConvertSSMLTextForNVDA(libmathcat.GetSpokenText(), self._language) + return ConvertSSMLTextForNVDA(GetSpokenTextUsingLanguage(self._language), self._language) except Exception as e: log.error(e) diff --git a/addon/globalPlugins/MathCAT/MathCATPreferences.py b/addon/globalPlugins/MathCAT/MathCATPreferences.py index 9f1e8803..2dc25379 100644 --- a/addon/globalPlugins/MathCAT/MathCATPreferences.py +++ b/addon/globalPlugins/MathCAT/MathCATPreferences.py @@ -2,8 +2,11 @@ import math import wx +from . import libmathcat from . import MathCATgui from . import yaml +from .MathCAT import getLanguageToUse + import os import glob import sys @@ -33,7 +36,7 @@ Navigation_NavVerbosity = ("Terse", "Medium", "Verbose") #Navigation_AutoZoomOut is boolean Braille_BrailleNavHighlight = ("Off", "FirstChar", "EndPoints", "All") -Braille_BrailleCode = ("Nemeth", "UEB", "Vietnam") +Braille_BrailleCode = ("CMU", "Nemeth", "UEB", "Vietnam") class UserInterface(MathCATgui.MathCATPreferencesDialog): def __init__(self,parent): @@ -247,6 +250,7 @@ def GetLanguages(self): #clear the language names in the dialog self.m_choiceLanguage.Clear() #populate the available language names in the dialog + self.m_choiceLanguage.Append(_("Auto")) for f in os.listdir(UserInterface.path_to_languages_folder()): if os.path.isdir(UserInterface.path_to_languages_folder()+"\\"+f): if languages_dict.get(f, 'missing') == 'missing': @@ -254,16 +258,24 @@ def GetLanguages(self): else: self.m_choiceLanguage.Append(languages_dict[f] + " (" + f + ")") - def GetLanguageCode(self): + def GetLanguageCode(self, expand_auto: bool): lang_selection = self.m_choiceLanguage.GetStringSelection() - lang_code = lang_selection[lang_selection.find("(")+1 : lang_selection.find(")")] + if lang_selection == "Auto": + if not(expand_auto): + return "Auto" + lang_code = getLanguageToUse("") + region_start = lang_code.find('-') + if region_start != -1: + lang_code = lang_code[0:region_start] + else: + lang_code = lang_selection[lang_selection.find("(")+1 : lang_selection.find(")")] return lang_code def GetSpeechStyles(self, this_SpeechStyle: str): #clear the SpeechStyle choices self.m_choiceSpeechStyle.Clear() #get the currently selected language code - this_language_code = UserInterface.GetLanguageCode(self) + this_language_code = UserInterface.GetLanguageCode(self, True) this_path = os.path.expanduser('~')+"\\AppData\\Roaming\\nvda\\addons\\MathCAT\\globalPlugins\\MathCAT\\Rules\\Languages\\"+this_language_code+"\\*_Rules.yaml" #populate the m_choiceSpeechStyle choices @@ -325,7 +337,7 @@ def get_ui_values(self): global user_preferences # read the values from the UI and update the user preferences dictionary user_preferences["Speech"]["Impairment"] = Speech_Impairment[self.m_choiceImpairment.GetSelection()] - user_preferences["Speech"]["Language"] = self.GetLanguageCode() + user_preferences["Speech"]["Language"] = self.GetLanguageCode(False) user_preferences["Speech"]["SpeechStyle"] = self.m_choiceSpeechStyle.GetStringSelection() user_preferences["Speech"]["Verbosity"] = Speech_Verbosity[self.m_choiceSpeechAmount.GetSelection()] user_preferences["Speech"]["MathRate"] = self.m_sliderRelativeSpeed.GetValue() @@ -441,13 +453,11 @@ def validate_user_preferences(): #Braille: # BrailleNavHighlight: EndPoints # Highlight with dots 7 & 8 the current nav node -- values are Off, FirstChar, EndPoints, All UserInterface.validate("Braille", "BrailleNavHighlight", ["Off", "FirstChar", "EndPoints", "All"], "EndPoints") - # BrailleCode: "Nemeth" # Any supported braille code (currently Nemeth, UEB, Vietnam) - UserInterface.validate("Braille", "BrailleCode", ["Nemeth", "UEB", "Vietnam"], "Nemeth") + # BrailleCode: "Nemeth" # Any supported braille code (currently CMU, Nemeth, UEB, Vietnam) + UserInterface.validate("Braille", "BrailleCode", ["CMU", "Nemeth", "UEB", "Vietnam"], "Nemeth") - @staticmethod - def write_user_preferences(): + def write_user_preferences(self): # Language is special because it is set elsewhere by SetPreference which overrides the user_prefs -- so set it here - from . import libmathcat libmathcat.SetPreference("Language", user_preferences["Speech"]["Language"]) if not os.path.exists(UserInterface.path_to_user_preferences_folder()): #create a folder for the user preferences @@ -474,7 +484,7 @@ def OnPauseFactorChanged( self, event ): def OnClickOK(self,event): UserInterface.get_ui_values(self) - UserInterface.write_user_preferences() + UserInterface.write_user_preferences(self) self.Destroy() def OnClickCancel(self,event): @@ -482,7 +492,7 @@ def OnClickCancel(self,event): def OnClickApply(self,event): UserInterface.get_ui_values(self) - UserInterface.write_user_preferences() + UserInterface.write_user_preferences(self) def OnClickReset(self,event): UserInterface.load_default_preferences() diff --git a/addon/globalPlugins/MathCAT/MathCATgui.py b/addon/globalPlugins/MathCAT/MathCATgui.py index 94865a21..da1dd2a7 100644 --- a/addon/globalPlugins/MathCAT/MathCATgui.py +++ b/addon/globalPlugins/MathCAT/MathCATgui.py @@ -253,7 +253,7 @@ def __init__( self, parent ): bSizerBrailleMathCode.Add( self.m_staticTextBrailleMathCode, 0, wx.ALL, 5 ) - m_choiceBrailleMathCodeChoices = [ _(u"Nemeth"), _(u"UEB"), _(u"Vietnam") ] + m_choiceBrailleMathCodeChoices = [ _(u"CMU"), _(u"Nemeth"), _(u"UEB"), _(u"Vietnam") ] self.m_choiceBrailleMathCode = wx.Choice( self.m_panelBraille, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, m_choiceBrailleMathCodeChoices, 0 ) self.m_choiceBrailleMathCode.SetSelection( 1 ) bSizerBrailleMathCode.Add( self.m_choiceBrailleMathCode, 0, wx.ALL, 5 )