82
82
83
83
# try to get around espeak bug where voice slows down (for other voices, just a waste of time)
84
84
# we use a global that gets set at a time when the rate is probably good (SetMathML)
85
- _synthesizer_rate : int | None = None
85
+ _synthesizerRate : int | None = None
86
86
87
87
88
88
def getLanguageToUse (mathMl : str = "" ) -> str :
@@ -110,7 +110,7 @@ def getLanguageToUse(mathMl: str = "") -> str:
110
110
return language
111
111
112
112
113
- def ConvertSSMLTextForNVDA (text : str ) -> list :
113
+ def convertSSMLTextForNVDA (text : str ) -> list :
114
114
"""Change the SSML in the text into NVDA's command structure.
115
115
The environment is examined to determine whether a language switch is needed"""
116
116
# MathCAT's default rate is 180 wpm.
@@ -135,14 +135,14 @@ def ConvertSSMLTextForNVDA(text: str) -> list:
135
135
# At "50" espeak finished in 46 sec, sapi in 75 sec, and one core in 70; at '100' one core was much slower than the others
136
136
wpm = 2 * getSynth ()._get_rate ()
137
137
breakMulti = 180.0 / wpm
138
- supported_commands = synth .supportedCommands
139
- use_break = BreakCommand in supported_commands
140
- use_pitch = PitchCommand in supported_commands
138
+ supportedCommands = synth .supportedCommands
139
+ useBreak = BreakCommand in supportedCommands
140
+ usePitch = PitchCommand in supportedCommands
141
141
# use_rate = RateCommand in supported_commands
142
142
# use_volume = VolumeCommand in supported_commands
143
- use_phoneme = PhonemeCommand in supported_commands
143
+ usePhoneme = PhonemeCommand in supportedCommands
144
144
# as of 7/23, oneCore voices do not implement the CharacterModeCommand despite it being in supported_commands
145
- use_character = CharacterModeCommand in supported_commands and synth .name != "oneCore"
145
+ useCharacter = CharacterModeCommand in supportedCommands and synth .name != "oneCore"
146
146
out = []
147
147
if mathCATLanguageSetting != language :
148
148
# log.info(f"Setting language to {language}")
@@ -158,31 +158,31 @@ def ConvertSSMLTextForNVDA(text: str) -> list:
158
158
# log.info(f"\ntext: {text}")
159
159
for m in RE_MATHML_SPEECH .finditer (text ):
160
160
if m .lastgroup == "break" :
161
- if use_break :
161
+ if useBreak :
162
162
out .append (BreakCommand (time = int (int (m .group ("break" )) * breakMulti )))
163
163
elif m .lastgroup == "char" :
164
164
ch = m .group ("char" )
165
- if use_character :
165
+ if useCharacter :
166
166
out .extend ((CharacterModeCommand (True ), ch , CharacterModeCommand (False )))
167
167
else :
168
168
out .extend ((" " , "eigh" if ch == "a" and language .startswith ("en" ) else ch , " " ))
169
169
elif m .lastgroup == "beep" :
170
170
out .append (BeepCommand (2000 , 50 ))
171
171
elif m .lastgroup == "pitch" :
172
- if use_pitch :
172
+ if usePitch :
173
173
out .append (PitchCommand (multiplier = int (m .group (m .lastgroup ))))
174
174
resetProsody .append (PitchCommand )
175
175
elif m .lastgroup in PROSODY_COMMANDS :
176
176
command = PROSODY_COMMANDS [m .lastgroup ]
177
- if command in supported_commands :
177
+ if command in supportedCommands :
178
178
out .append (command (multiplier = int (m .group (m .lastgroup )) / 100.0 ))
179
179
resetProsody .append (command )
180
180
elif m .lastgroup == "prosodyReset" :
181
181
# for command in resetProsody: # only supported commands were added, so no need to check
182
182
command = resetProsody .pop ()
183
183
out .append (command (multiplier = 1 ))
184
184
elif m .lastgroup == "phonemeText" :
185
- if use_phoneme :
185
+ if usePhoneme :
186
186
out .append (PhonemeCommand (m .group ("ipa" ), text = m .group ("phonemeText" )))
187
187
else :
188
188
out .append (m .group ("phonemeText" ))
@@ -220,27 +220,27 @@ class MathCATInteraction(mathPres.MathInteractionNVDAObject):
220
220
def __init__ (self , provider = None , mathMl : Optional [str ] = None ):
221
221
super (MathCATInteraction , self ).__init__ (provider = provider , mathMl = mathMl )
222
222
if mathMl is None :
223
- self .init_mathml = "<math></math>"
223
+ self .initMathML = "<math></math>"
224
224
else :
225
- self .init_mathml = mathMl
225
+ self .initMathML = mathMl
226
226
227
227
def reportFocus (self ):
228
228
super (MathCATInteraction , self ).reportFocus ()
229
229
# try to get around espeak bug where voice slows down
230
- if _synthesizer_rate and getSynth ().name == "espeak" :
231
- getSynth ()._set_rate (_synthesizer_rate )
230
+ if _synthesizerRate and getSynth ().name == "espeak" :
231
+ getSynth ()._set_rate (_synthesizerRate )
232
232
try :
233
233
text = libmathcat .DoNavigateCommand ("ZoomIn" )
234
- speech .speak (ConvertSSMLTextForNVDA (text ))
234
+ speech .speak (convertSSMLTextForNVDA (text ))
235
235
except Exception as e :
236
236
log .exception (e )
237
237
# Translators: this message directs users to look in the log file
238
238
speech .speakMessage (_ ("Error in starting navigation of math: see NVDA error log for details" ))
239
239
finally :
240
240
# try to get around espeak bug where voice slows down
241
- if _synthesizer_rate and getSynth ().name == "espeak" :
241
+ if _synthesizerRate and getSynth ().name == "espeak" :
242
242
# log.info(f'reportFocus: reset to {_synthesizer_rate}')
243
- getSynth ()._set_rate (_synthesizer_rate )
243
+ getSynth ()._set_rate (_synthesizerRate )
244
244
245
245
def getBrailleRegions (self , review : bool = False ):
246
246
# log.info("***MathCAT start getBrailleRegions")
@@ -295,8 +295,8 @@ def getScript(self, gesture: KeyboardInputGesture):
295
295
def script_navigate (self , gesture : KeyboardInputGesture ):
296
296
try :
297
297
# try to get around espeak bug where voice slows down
298
- if _synthesizer_rate and getSynth ().name == "espeak" :
299
- getSynth ()._set_rate (_synthesizer_rate )
298
+ if _synthesizerRate and getSynth ().name == "espeak" :
299
+ getSynth ()._set_rate (_synthesizerRate )
300
300
if gesture is not None : # == None when initial focus -- handled in reportFocus()
301
301
modNames = gesture .modifierNames
302
302
text = libmathcat .DoNavigateKeyPress (
@@ -307,27 +307,27 @@ def script_navigate(self, gesture: KeyboardInputGesture):
307
307
False ,
308
308
)
309
309
# log.info(f"Navigate speech for {gesture.vkCode}/(s={'shift' in modNames}, c={'control' in modNames}): '{text}'")
310
- speech .speak (ConvertSSMLTextForNVDA (text ))
310
+ speech .speak (convertSSMLTextForNVDA (text ))
311
311
except Exception as e :
312
312
log .exception (e )
313
313
# Translators: this message directs users to look in the log file
314
314
speech .speakMessage (_ ("Error in navigating math: see NVDA error log for details" ))
315
315
finally :
316
316
# try to get around espeak bug where voice slows down
317
- if _synthesizer_rate and getSynth ().name == "espeak" :
317
+ if _synthesizerRate and getSynth ().name == "espeak" :
318
318
# log.info(f'script_navigate: reset to {_synthesizer_rate}')
319
- getSynth ()._set_rate (_synthesizer_rate )
319
+ getSynth ()._set_rate (_synthesizerRate )
320
320
321
321
if not braille .handler .enabled :
322
322
return
323
323
324
324
try :
325
325
# update the braille to reflect the nav position (might be excess code, but it works)
326
- nav_node = libmathcat .GetNavigationMathMLId ()
327
- braille_chars = libmathcat .GetBraille (nav_node [0 ])
326
+ navNode = libmathcat .GetNavigationMathMLId ()
327
+ brailleChars = libmathcat .GetBraille (navNode [0 ])
328
328
# log.info(f'braille display = {config.conf["braille"]["display"]}, braille_chars: {braille_chars}')
329
329
region = braille .Region ()
330
- region .rawText = braille_chars
330
+ region .rawText = brailleChars
331
331
region .focusToHardLeft = True
332
332
region .update ()
333
333
braille .handler .buffer .regions .append (region )
@@ -350,44 +350,44 @@ def script_navigate(self, gesture: KeyboardInputGesture):
350
350
) # type: ignore
351
351
def script_rawdataToClip (self , gesture : KeyboardInputGesture ):
352
352
try :
353
- copy_as = "mathml" # value used even if "CopyAs" pref is invalid
354
- text_to_copy = ""
353
+ copyAs = "mathml" # value used even if "CopyAs" pref is invalid
354
+ textToCopy = ""
355
355
try :
356
- copy_as = libmathcat .GetPreference ("CopyAs" ).lower ()
356
+ copyAs = libmathcat .GetPreference ("CopyAs" ).lower ()
357
357
except Exception as e :
358
358
log .exception (f"Not able to get 'CopyAs' preference: { e } " )
359
- if copy_as == "asciimath" or copy_as == "latex" :
359
+ if copyAs == "asciimath" or copyAs == "latex" :
360
360
# save the old braille code, set the new one, get the braille, then reset the code
361
- saved_braille_code : str = libmathcat .GetPreference ("BrailleCode" )
362
- libmathcat .SetPreference ("BrailleCode" , "LaTeX" if copy_as == "latex" else "ASCIIMath" )
363
- text_to_copy = libmathcat .GetNavigationBraille ()
364
- libmathcat .SetPreference ("BrailleCode" , saved_braille_code )
365
- if copy_as == "asciimath" :
366
- copy_as = "ASCIIMath" # speaks better in at least some voices
361
+ savedBrailleCode : str = libmathcat .GetPreference ("BrailleCode" )
362
+ libmathcat .SetPreference ("BrailleCode" , "LaTeX" if copyAs == "latex" else "ASCIIMath" )
363
+ textToCopy = libmathcat .GetNavigationBraille ()
364
+ libmathcat .SetPreference ("BrailleCode" , savedBrailleCode )
365
+ if copyAs == "asciimath" :
366
+ copyAs = "ASCIIMath" # speaks better in at least some voices
367
367
else :
368
368
mathml = libmathcat .GetNavigationMathML ()[0 ]
369
369
if not re .match (self ._startsWithMath , mathml ):
370
370
mathml = "<math>\n " + mathml + "</math>" # copy will fix up name spacing
371
- elif self .init_mathml != "" :
372
- mathml = self .init_mathml
373
- if copy_as == "speech" :
371
+ elif self .initMathML != "" :
372
+ mathml = self .initMathML
373
+ if copyAs == "speech" :
374
374
# save the old MathML, set the navigation MathML as MathMl, get the speech, then reset the MathML
375
- saved_mathml : str = self .init_mathml
376
- saved_tts = libmathcat .GetPreference ("TTS" )
377
- if saved_mathml == "" : # shouldn't happen
375
+ savedMathML : str = self .initMathML
376
+ savedTTS = libmathcat .GetPreference ("TTS" )
377
+ if savedMathML == "" : # shouldn't happen
378
378
raise Exception ("Internal error -- MathML not set for copy" )
379
379
libmathcat .SetPreference ("TTS" , "None" )
380
380
libmathcat .SetMathML (mathml )
381
381
# get the speech text and collapse the whitespace
382
- text_to_copy = " " .join (libmathcat .GetSpokenText ().split ())
383
- libmathcat .SetPreference ("TTS" , saved_tts )
384
- libmathcat .SetMathML (saved_mathml )
382
+ textToCopy = " " .join (libmathcat .GetSpokenText ().split ())
383
+ libmathcat .SetPreference ("TTS" , savedTTS )
384
+ libmathcat .SetMathML (savedMathML )
385
385
else :
386
- text_to_copy = self ._wrapMathMLForClipBoard (mathml )
386
+ textToCopy = self ._wrapMathMLForClipBoard (mathml )
387
387
388
- self ._copyToClipAsMathML (text_to_copy , copy_as == "mathml" )
388
+ self ._copyToClipAsMathML (textToCopy , copyAs == "mathml" )
389
389
# Translators: copy to clipboard
390
- ui .message (_ ("copy as " ) + copy_as )
390
+ ui .message (_ ("copy as " ) + copyAs )
391
391
except Exception as e :
392
392
log .exception (e )
393
393
# Translators: this message directs users to look in the log file
@@ -402,16 +402,16 @@ def script_rawdataToClip(self, gesture: KeyboardInputGesture):
402
402
def _wrapMathMLForClipBoard (self , text : str ) -> str :
403
403
# cleanup the MathML a little
404
404
text = re .sub (self ._hasAddedId , "" , text )
405
- mathml_with_ns = re .sub (self ._hasDataAttr , "" , text )
406
- if not re .match (self ._mathTagHasNameSpace , mathml_with_ns ):
407
- mathml_with_ns = mathml_with_ns .replace (
405
+ mathMLWithNS = re .sub (self ._hasDataAttr , "" , text )
406
+ if not re .match (self ._mathTagHasNameSpace , mathMLWithNS ):
407
+ mathMLWithNS = mathMLWithNS .replace (
408
408
"math" ,
409
409
"math xmlns='http://www.w3.org/1998/Math/MathML'" ,
410
410
1 ,
411
411
)
412
- return mathml_with_ns
412
+ return mathMLWithNS
413
413
414
- def _copyToClipAsMathML (self , text : str , is_mathml : bool , notify : Optional [bool ] = False ) -> bool :
414
+ def _copyToClipAsMathML (self , text : str , isMathML : bool , notify : Optional [bool ] = False ) -> bool :
415
415
"""Copies the given text to the windows clipboard.
416
416
@returns: True if it succeeds, False otherwise.
417
417
@param text: the text which will be copied to the clipboard
@@ -424,7 +424,7 @@ def _copyToClipAsMathML(self, text: str, is_mathml: bool, notify: Optional[bool]
424
424
try :
425
425
with winUser .openClipboard (gui .mainFrame .Handle ):
426
426
winUser .emptyClipboard ()
427
- if is_mathml :
427
+ if isMathML :
428
428
self ._setClipboardData (self .CF_MathML , '<?xml version="1.0"?>' + text )
429
429
self ._setClipboardData (self .CF_MathML_Presentation , '<?xml version="1.0"?>' + text )
430
430
self ._setClipboardData (winUser .CF_UNICODETEXT , text )
@@ -468,23 +468,23 @@ def __init__(self):
468
468
469
469
try :
470
470
# IMPORTANT -- SetRulesDir must be the first call to libmathcat besides GetVersion()
471
- rules_dir = path .join (path .dirname (path .abspath (__file__ )), "Rules" )
472
- log .info (f"MathCAT { libmathcat .GetVersion ()} installed. Using rules dir: { rules_dir } " )
473
- libmathcat .SetRulesDir (rules_dir )
471
+ rulesDir = path .join (path .dirname (path .abspath (__file__ )), "Rules" )
472
+ log .info (f"MathCAT { libmathcat .GetVersion ()} installed. Using rules dir: { rulesDir } " )
473
+ libmathcat .SetRulesDir (rulesDir )
474
474
libmathcat .SetPreference ("TTS" , "SSML" )
475
475
except Exception as e :
476
476
log .exception (e )
477
477
# Translators: this message directs users to look in the log file
478
478
speech .speakMessage (_ ("MathCAT initialization failed: see NVDA error log for details" ))
479
479
480
480
def getSpeechForMathMl (self , mathml : str ):
481
- global _synthesizer_rate
481
+ global _synthesizerRate
482
482
synth = getSynth ()
483
483
synthConfig = config .conf ["speech" ][synth .name ]
484
484
if synth .name == "espeak" :
485
- _synthesizer_rate = synthConfig ["rate" ]
485
+ _synthesizerRate = synthConfig ["rate" ]
486
486
# log.info(f'_synthesizer_rate={_synthesizer_rate}, get_rate()={getSynth()._get_rate()}')
487
- getSynth ()._set_rate (_synthesizer_rate )
487
+ getSynth ()._set_rate (_synthesizerRate )
488
488
# log.info(f'..............get_rate()={getSynth()._get_rate()}, name={synth.name}')
489
489
try :
490
490
# need to set Language before the MathML for DecimalSeparator canonicalization
@@ -499,7 +499,7 @@ def getSpeechForMathMl(self, mathml: str):
499
499
speech .speakMessage (_ ("Illegal MathML found: see NVDA error log for details" ))
500
500
libmathcat .SetMathML ("<math></math>" ) # set it to something
501
501
try :
502
- supported_commands = synth .supportedCommands
502
+ supportedCommands = synth .supportedCommands
503
503
# Set preferences for capital letters
504
504
libmathcat .SetPreference (
505
505
"CapitalLetters_Beep" ,
@@ -510,16 +510,16 @@ def getSpeechForMathMl(self, mathml: str):
510
510
"true" if synthConfig ["sayCapForCapitals" ] else "false" ,
511
511
)
512
512
# log.info(f"Speech text: {libmathcat.GetSpokenText()}")
513
- if PitchCommand in supported_commands :
513
+ if PitchCommand in supportedCommands :
514
514
libmathcat .SetPreference ("CapitalLetters_Pitch" , str (synthConfig ["capPitchChange" ]))
515
- if self ._add_sounds ():
515
+ if self ._addSounds ():
516
516
return (
517
517
[BeepCommand (800 , 25 )]
518
- + ConvertSSMLTextForNVDA (libmathcat .GetSpokenText ())
518
+ + convertSSMLTextForNVDA (libmathcat .GetSpokenText ())
519
519
+ [BeepCommand (600 , 15 )]
520
520
)
521
521
else :
522
- return ConvertSSMLTextForNVDA (libmathcat .GetSpokenText ())
522
+ return convertSSMLTextForNVDA (libmathcat .GetSpokenText ())
523
523
524
524
except Exception as e :
525
525
log .exception (e )
@@ -528,11 +528,11 @@ def getSpeechForMathMl(self, mathml: str):
528
528
return ["" ]
529
529
finally :
530
530
# try to get around espeak bug where voice slows down
531
- if _synthesizer_rate and getSynth ().name == "espeak" :
531
+ if _synthesizerRate and getSynth ().name == "espeak" :
532
532
# log.info(f'getSpeechForMathMl: reset to {_synthesizer_rate}')
533
- getSynth ()._set_rate (_synthesizer_rate )
533
+ getSynth ()._set_rate (_synthesizerRate )
534
534
535
- def _add_sounds (self ):
535
+ def _addSounds (self ):
536
536
try :
537
537
return libmathcat .GetPreference ("SpeechSound" ) != "None"
538
538
except Exception as e :
@@ -572,10 +572,10 @@ def _monkeyPatchESpeak():
572
572
return # already patched
573
573
574
574
CACHED_SYNTH = currentSynth
575
- currentSynth .speak = patched_speak .__get__ (currentSynth , type (currentSynth ))
575
+ currentSynth .speak = patchedSpeak .__get__ (currentSynth , type (currentSynth ))
576
576
577
577
578
- def patched_speak (self , speechSequence : SpeechSequence ): # noqa: C901
578
+ def patchedSpeak (self , speechSequence : SpeechSequence ): # noqa: C901
579
579
# log.info(f"\npatched_speak input: {speechSequence}")
580
580
textList : List [str ] = []
581
581
langChanged = False
0 commit comments