# -*- coding: UTF-8 -*- #speech.py #A part of NonVisual Desktop Access (NVDA) #This file is covered by the GNU General Public License. #See the file COPYING for more details. #Copyright (C) 2006-2014 NV Access Limited, Peter Vágner, Aleksey Sadovoy """High-level functions to speak information. """ import itertools import weakref import unicodedata import colors import globalVars from logHandler import log import api import controlTypes import config import tones import synthDriverHandler from synthDriverHandler import * import re import textInfos import queueHandler import speechDictHandler import characterProcessing import languageHandler import sys import vlibrashook speechMode_off=0 speechMode_beeps=1 speechMode_talk=2 #: How speech should be handled; one of speechMode_off, speechMode_beeps or speechMode_talk. speechMode=speechMode_talk speechMode_beeps_ms=15 beenCanceled=True isPaused=False curWordChars=[] vlibrashookclient=vlibrashook.client("127.0.0.1", 10001) #Set containing locale codes for languages supporting conjunct characters LANGS_WITH_CONJUNCT_CHARS = {'hi', 'as', 'bn', 'gu', 'kn', 'kok', 'ml', 'mni', 'mr', 'pa', 'te', 'ur', 'ta'} # The REASON_* constants in this module are deprecated and will be removed in a future release. # Use controlTypes.REASON_* instead. from controlTypes import REASON_FOCUS, REASON_FOCUSENTERED, REASON_MOUSE, REASON_QUERY, REASON_CHANGE, REASON_MESSAGE, REASON_SAYALL, REASON_CARET, REASON_ONLYCACHE #: The string used to separate distinct chunks of text when multiple chunks should be spoken without pauses. # #555: Use two spaces so that numbers from adjacent chunks aren't treated as a single number # for languages such as French and German which use space as a thousands separator. CHUNK_SEPARATOR = " " oldTreeLevel=None oldTableID=None oldRowNumber=None oldColumnNumber=None def initialize(): """Loads and sets the synth driver configured in nvda.ini.""" synthDriverHandler.initialize() setSynth(config.conf["speech"]["synth"]) global vlibrashookclient vlibrashookclient.start() def terminate(): setSynth(None) speechViewerObj=None #: If a chunk of text contains only these characters, it will be considered blank. BLANK_CHUNK_CHARS = frozenset((" ", "\n", "\r", "\0", u"\xa0")) def isBlank(text): """Determine whether text should be reported as blank. @param text: The text in question. @type text: str @return: C{True} if the text is blank, C{False} if not. @rtype: bool """ return not text or set(text) <= BLANK_CHUNK_CHARS RE_CONVERT_WHITESPACE = re.compile("[\0\r\n]") def processText(locale,text,symbolLevel): text = speechDictHandler.processText(text) text = characterProcessing.processSpeechSymbols(locale, text, symbolLevel) text = RE_CONVERT_WHITESPACE.sub(u" ", text) return text.strip() def getLastSpeechIndex(): """Gets the last index passed by the synthesizer. Indexing is used so that its possible to find out when a certain peace of text has been spoken yet. Usually the character position of the text is passed to speak functions as the index. @returns: the last index encountered @rtype: int """ return getSynth().lastIndex def cancelSpeech(): """Interupts the synthesizer from currently speaking""" global beenCanceled, isPaused, _speakSpellingGenerator # Import only for this function to avoid circular import. import sayAllHandler sayAllHandler.stop() speakWithoutPauses._pendingSpeechSequence=[] speakWithoutPauses.lastSentIndex=None if _speakSpellingGenerator: _speakSpellingGenerator.close() if beenCanceled: return elif speechMode==speechMode_off: return elif speechMode==speechMode_beeps: return getSynth().cancel() beenCanceled=True isPaused=False def pauseSpeech(switch): global isPaused, beenCanceled getSynth().pause(switch) isPaused=switch beenCanceled=False def speakMessage(text,index=None): """Speaks a given message. @param text: the message to speak @type text: string @param index: the index to mark this current text with, its best to use the character position of the text if you know it @type index: int """ speakText(text,index=index,reason=controlTypes.REASON_MESSAGE) def getCurrentLanguage(): try: language=getSynth().language if config.conf['speech']['trustVoiceLanguage'] else None except NotImplementedError: language=None if language: language=languageHandler.normalizeLanguage(language) if not language: language=languageHandler.getLanguage() return language def spellTextInfo(info,useCharacterDescriptions=False): """Spells the text from the given TextInfo, honouring any LangChangeCommand objects it finds if autoLanguageSwitching is enabled.""" if not config.conf['speech']['autoLanguageSwitching']: speakSpelling(info.text,useCharacterDescriptions=useCharacterDescriptions) return curLanguage=None for field in info.getTextWithFields({}): if isinstance(field,basestring): speakSpelling(field,curLanguage,useCharacterDescriptions=useCharacterDescriptions) elif isinstance(field,textInfos.FieldCommand) and field.command=="formatChange": curLanguage=field.field.get('language') _speakSpellingGenerator=None def speakSpelling(text,locale=None,useCharacterDescriptions=False): global beenCanceled, _speakSpellingGenerator import speechViewer if speechViewer.isActive: speechViewer.appendText(text) if speechMode==speechMode_off: return elif speechMode==speechMode_beeps: tones.beep(config.conf["speech"]["beepSpeechModePitch"],speechMode_beeps_ms) return if isPaused: cancelSpeech() beenCanceled=False defaultLanguage=getCurrentLanguage() if not locale or (not config.conf['speech']['autoDialectSwitching'] and locale.split('_')[0]==defaultLanguage.split('_')[0]): locale=defaultLanguage if not text: # Translators: This is spoken when NVDA moves to an empty line. return getSynth().speak((_("blank"),)) if not text.isspace(): text=text.rstrip() if _speakSpellingGenerator and _speakSpellingGenerator.gi_frame: _speakSpellingGenerator.send((text,locale,useCharacterDescriptions)) else: _speakSpellingGenerator=_speakSpellingGen(text,locale,useCharacterDescriptions) try: # Speak the first character before this function returns. next(_speakSpellingGenerator) except StopIteration: return queueHandler.registerGeneratorObject(_speakSpellingGenerator) def getCharDescListFromText(text,locale): """This method prepares a list, which contains character and its description for all characters the text is made up of, by checking the presence of character descriptions in characterDescriptions.dic of that locale for all possible combination of consecutive characters in the text. This is done to take care of conjunct characters present in several languages such as Hindi, Urdu, etc. """ charDescList = [] charDesc=None i = len(text) while i: subText = text[:i] charDesc = characterProcessing.getCharacterDescription(locale,subText) if charDesc or i==1: if not charDesc: # #5375: We're down to a single character (i == 1) and we don't have a description. # Try converting to lower case. # This provides for upper case English characters (which only have lower case descriptions). charDesc = characterProcessing.getCharacterDescription(locale,subText.lower()) charDescList.append((subText,charDesc)) text = text[i:] i = len(text) else: i = i - 1 return charDescList def _speakSpellingGen(text,locale,useCharacterDescriptions): synth=getSynth() synthConfig=config.conf["speech"][synth.name] buf=[(text,locale,useCharacterDescriptions)] for text,locale,useCharacterDescriptions in buf: textLength=len(text) count = 0 localeHasConjuncts = True if locale.split('_',1)[0] in LANGS_WITH_CONJUNCT_CHARS else False charDescList = getCharDescListFromText(text,locale) if localeHasConjuncts else text for item in charDescList: if localeHasConjuncts: # item is a tuple containing character and its description char = item[0] charDesc = item[1] else: # item is just a character. char = item if useCharacterDescriptions: charDesc=characterProcessing.getCharacterDescription(locale,char.lower()) uppercase=char.isupper() if useCharacterDescriptions and charDesc: #Consider changing to multiple synth speech calls char=charDesc[0] if textLength>1 else u"\u3001".join(charDesc) else: char=characterProcessing.processSpeechSymbol(locale,char) if uppercase and synthConfig["sayCapForCapitals"]: # Translators: cap will be spoken before the given letter when it is capitalized. char=_("cap %s")%char if uppercase and synth.isSupported("pitch") and synthConfig["capPitchChange"]: oldPitch=synthConfig["pitch"] synth.pitch=max(0,min(oldPitch+synthConfig["capPitchChange"],100)) count = len(char) index=count+1 log.io("Speaking character %r"%char) speechSequence=[LangChangeCommand(locale)] if config.conf['speech']['autoLanguageSwitching'] else [] if len(char) == 1 and synthConfig["useSpellingFunctionality"]: speechSequence.append(CharacterModeCommand(True)) if index is not None: speechSequence.append(IndexCommand(index)) speechSequence.append(char) synth.speak(speechSequence) if uppercase and synth.isSupported("pitch") and synthConfig["capPitchChange"]: synth.pitch=oldPitch while textLength>1 and (isPaused or getLastSpeechIndex()!=index): for x in xrange(2): args=yield if args: buf.append(args) if uppercase and synthConfig["beepForCapitals"]: tones.beep(2000,50) args=yield if args: buf.append(args) def speakObjectProperties(obj,reason=controlTypes.REASON_QUERY,index=None,**allowedProperties): #Fetch the values for all wanted properties newPropertyValues={} positionInfo=None for name,value in allowedProperties.iteritems(): if name=="includeTableCellCoords": # This is verbosity info. newPropertyValues[name]=value elif name.startswith('positionInfo_') and value: if positionInfo is None: positionInfo=obj.positionInfo elif value: try: newPropertyValues[name]=getattr(obj,name) except NotImplementedError: pass if positionInfo: if allowedProperties.get('positionInfo_level',False) and 'level' in positionInfo: newPropertyValues['positionInfo_level']=positionInfo['level'] if allowedProperties.get('positionInfo_indexInGroup',False) and 'indexInGroup' in positionInfo: newPropertyValues['positionInfo_indexInGroup']=positionInfo['indexInGroup'] if allowedProperties.get('positionInfo_similarItemsInGroup',False) and 'similarItemsInGroup' in positionInfo: newPropertyValues['positionInfo_similarItemsInGroup']=positionInfo['similarItemsInGroup'] #Fetched the cached properties and update them with the new ones oldCachedPropertyValues=getattr(obj,'_speakObjectPropertiesCache',{}).copy() cachedPropertyValues=oldCachedPropertyValues.copy() cachedPropertyValues.update(newPropertyValues) obj._speakObjectPropertiesCache=cachedPropertyValues #If we should only cache we can stop here if reason==controlTypes.REASON_ONLYCACHE: return #If only speaking change, then filter out all values that havn't changed if reason==controlTypes.REASON_CHANGE: for name in set(newPropertyValues)&set(oldCachedPropertyValues): if newPropertyValues[name]==oldCachedPropertyValues[name]: del newPropertyValues[name] elif name=="states": #states need specific handling oldStates=oldCachedPropertyValues[name] newStates=newPropertyValues[name] newPropertyValues['states']=newStates-oldStates newPropertyValues['negativeStates']=oldStates-newStates #properties such as states need to know the role to speak properly, give it as a _ name newPropertyValues['_role']=newPropertyValues.get('role',obj.role) # The real states are needed also, as the states entry might be filtered. newPropertyValues['_states']=obj.states if "rowNumber" in newPropertyValues or "columnNumber" in newPropertyValues: # We're reporting table cell info, so pass the table ID. try: newPropertyValues["_tableID"]=obj.tableID except NotImplementedError: pass #Get the speech text for the properties we want to speak, and then speak it text=getSpeechTextForProperties(reason,**newPropertyValues) if text: speakText(text,index=index) def speakObject(obj,reason=controlTypes.REASON_QUERY,index=None): from NVDAObjects import NVDAObjectTextInfo role=obj.role isEditable=(reason!=controlTypes.REASON_FOCUSENTERED and obj.TextInfo!=NVDAObjectTextInfo and (role in (controlTypes.ROLE_EDITABLETEXT,controlTypes.ROLE_TERMINAL) or controlTypes.STATE_EDITABLE in obj.states)) allowProperties={'name':True,'role':True,'states':True,'value':True,'description':True,'keyboardShortcut':True,'positionInfo_level':True,'positionInfo_indexInGroup':True,'positionInfo_similarItemsInGroup':True,"cellCoordsText":True,"rowNumber":True,"columnNumber":True,"includeTableCellCoords":True,"columnCount":True,"rowCount":True,"rowHeaderText":True,"columnHeaderText":True} if reason==controlTypes.REASON_FOCUSENTERED: allowProperties["value"]=False allowProperties["keyboardShortcut"]=False allowProperties["positionInfo_level"]=False # Aside from excluding some properties, focus entered should be spoken like focus. reason=controlTypes.REASON_FOCUS if not config.conf["presentation"]["reportObjectDescriptions"]: allowProperties["description"]=False if not config.conf["presentation"]["reportKeyboardShortcuts"]: allowProperties["keyboardShortcut"]=False if not config.conf["presentation"]["reportObjectPositionInformation"]: allowProperties["positionInfo_level"]=False allowProperties["positionInfo_indexInGroup"]=False allowProperties["positionInfo_similarItemsInGroup"]=False if reason!=controlTypes.REASON_QUERY: allowProperties["rowCount"]=False allowProperties["columnCount"]=False formatConf=config.conf["documentFormatting"] if not formatConf["reportTableCellCoords"]: allowProperties["cellCoordsText"]=False # rowNumber and columnNumber might be needed even if we're not reporting coordinates. allowProperties["includeTableCellCoords"]=False if not formatConf["reportTableHeaders"]: allowProperties["rowHeaderText"]=False allowProperties["columnHeaderText"]=False if (not formatConf["reportTables"] or (not formatConf["reportTableCellCoords"] and not formatConf["reportTableHeaders"])): # We definitely aren't reporting any table info at all. allowProperties["rowNumber"]=False allowProperties["columnNumber"]=False if isEditable: allowProperties['value']=False speakObjectProperties(obj,reason=reason,index=index,**allowProperties) if reason==controlTypes.REASON_ONLYCACHE: return if isEditable: try: info=obj.makeTextInfo(textInfos.POSITION_SELECTION) if not info.isCollapsed: # Translators: This is spoken to indicate what has been selected. for example 'selected hello world' speakSelectionMessage(_("selected %s"),info.text) else: info.expand(textInfos.UNIT_LINE) speakTextInfo(info,unit=textInfos.UNIT_LINE,reason=controlTypes.REASON_CARET) except: newInfo=obj.makeTextInfo(textInfos.POSITION_ALL) speakTextInfo(newInfo,unit=textInfos.UNIT_PARAGRAPH,reason=controlTypes.REASON_CARET) elif role==controlTypes.ROLE_MATH: import mathPres mathPres.ensureInit() if mathPres.speechProvider: try: speak(mathPres.speechProvider.getSpeechForMathMl(obj.mathMl)) except (NotImplementedError, LookupError): pass def speakText(text,index=None,reason=controlTypes.REASON_MESSAGE,symbolLevel=None): """Speaks some text. @param text: The text to speak. @type text: str @param index: The index to mark this text with, which can be used later to determine whether this piece of text has been spoken. @type index: int @param reason: The reason for this speech; one of the controlTypes.REASON_* constants. @param symbolLevel: The symbol verbosity level; C{None} (default) to use the user's configuration. """ speechSequence=[] if index is not None: speechSequence.append(IndexCommand(index)) if text is not None: if isBlank(text): # Translators: This is spoken when the line is considered blank. text=_("blank") speechSequence.append(text) speak(speechSequence,symbolLevel=symbolLevel) RE_INDENTATION_SPLIT = re.compile(r"^([^\S\r\n\f\v]*)(.*)$", re.UNICODE | re.DOTALL) def splitTextIndentation(text): """Splits indentation from the rest of the text. @param text: The text to split. @type text: basestring @return: Tuple of indentation and content. @rtype: (basestring, basestring) """ return RE_INDENTATION_SPLIT.match(text).groups() RE_INDENTATION_CONVERT = re.compile(r"(?P\s)(?P=char)*", re.UNICODE) IDT_BASE_FREQUENCY = 220 #One octave below middle A. IDT_TONE_DURATION = 80 #Milleseconds IDT_MAX_SPACES = 72 def getIndentationSpeech(indentation, formatConfig): """Retrieves the phrase to be spoken for a given string of indentation. @param indentation: The string of indentation. @type indentation: unicode @param formatConfig: The configuration to use. @type formatConfig: dict @return: The phrase to be spoken. @rtype: unicode """ speechIndentConfig = formatConfig["reportLineIndentation"] toneIndentConfig = formatConfig["reportLineIndentationWithTones"] and speechMode == speechMode_talk if not indentation: if toneIndentConfig: tones.beep(IDT_BASE_FREQUENCY, IDT_TONE_DURATION) # Translators: This is spoken when the given line has no indentation. return (_("no indent") if speechIndentConfig else "") #The non-breaking space is semantically a space, so we replace it here. indentation = indentation.replace(u"\xa0", u" ") res = [] locale=languageHandler.getLanguage() quarterTones = 0 for m in RE_INDENTATION_CONVERT.finditer(indentation): raw = m.group() symbol = characterProcessing.processSpeechSymbol(locale, raw[0]) count = len(raw) if symbol == raw[0]: # There is no replacement for this character, so do nothing. res.append(raw) elif count == 1: res.append(symbol) else: res.append(u"{count} {symbol}".format(count=count, symbol=symbol)) quarterTones += (count*4 if raw[0]== "\t" else count) speak = speechIndentConfig if toneIndentConfig: if quarterTones <= IDT_MAX_SPACES: #Remove me during speech refactor. pitch = IDT_BASE_FREQUENCY*2**(quarterTones/24.0) #24 quarter tones per octave. tones.beep(pitch, IDT_TONE_DURATION) else: #we have more than 72 spaces (18 tabs), and must speak it since we don't want to hurt the users ears. speak = True return (" ".join(res) if speak else "") def speak(speechSequence,symbolLevel=None): """Speaks a sequence of text and speech commands @param speechSequence: the sequence of text and L{SpeechCommand} objects to speak @param symbolLevel: The symbol verbosity level; C{None} (default) to use the user's configuration. """ if not speechSequence: #Pointless - nothing to speak return import speechViewer if speechViewer.isActive: for item in speechSequence: if isinstance(item,basestring): speechViewer.appendText(item) global beenCanceled, curWordChars curWordChars=[] if speechMode==speechMode_off: return elif speechMode==speechMode_beeps: tones.beep(config.conf["speech"]["beepSpeechModePitch"],speechMode_beeps_ms) return if isPaused: cancelSpeech() beenCanceled=False #Filter out redundant LangChangeCommand objects #And also fill in default values autoLanguageSwitching=config.conf['speech']['autoLanguageSwitching'] autoDialectSwitching=config.conf['speech']['autoDialectSwitching'] curLanguage=defaultLanguage=getCurrentLanguage() prevLanguage=None defaultLanguageRoot=defaultLanguage.split('_')[0] oldSpeechSequence=speechSequence speechSequence=[] for item in oldSpeechSequence: if isinstance(item,LangChangeCommand): if not autoLanguageSwitching: continue curLanguage=item.lang if not curLanguage or (not autoDialectSwitching and curLanguage.split('_')[0]==defaultLanguageRoot): curLanguage=defaultLanguage elif isinstance(item,basestring): if not item: continue if autoLanguageSwitching and curLanguage!=prevLanguage: speechSequence.append(LangChangeCommand(curLanguage)) prevLanguage=curLanguage speechSequence.append(item) else: speechSequence.append(item) if not speechSequence: # After normalisation, the sequence is empty. # There's nothing to speak. return global vlibrashookclient try: if vlibrashookclient.connected(): for item in speechSequence: if isinstance(item,basestring): if vlibrashookclient.connected(): if not vlibrashookclient.send(item.encode('utf-8')): break except: pass log.io("Speaking %r" % speechSequence) if symbolLevel is None: symbolLevel=config.conf["speech"]["symbolLevel"] curLanguage=defaultLanguage inCharacterMode=False for index in xrange(len(speechSequence)): item=speechSequence[index] if isinstance(item,CharacterModeCommand): inCharacterMode=item.state if autoLanguageSwitching and isinstance(item,LangChangeCommand): curLanguage=item.lang if isinstance(item,basestring): speechSequence[index]=processText(curLanguage,item,symbolLevel) if not inCharacterMode: speechSequence[index]+=CHUNK_SEPARATOR getSynth().speak(speechSequence) def speakSelectionMessage(message,text): if len(text) < 512: speakMessage(message % text) else: # Translators: This is spoken when the user has selected a large portion of text. Example output "1000 characters" speakMessage(message % _("%d characters") % len(text)) def speakSelectionChange(oldInfo,newInfo,speakSelected=True,speakUnselected=True,generalize=False): """Speaks a change in selection, either selected or unselected text. @param oldInfo: a TextInfo instance representing what the selection was before @type oldInfo: L{textInfos.TextInfo} @param newInfo: a TextInfo instance representing what the selection is now @type newInfo: L{textInfos.TextInfo} @param generalize: if True, then this function knows that the text may have changed between the creation of the oldInfo and newInfo objects, meaning that changes need to be spoken more generally, rather than speaking the specific text, as the bounds may be all wrong. @type generalize: boolean """ selectedTextList=[] unselectedTextList=[] if newInfo.isCollapsed and oldInfo.isCollapsed: return startToStart=newInfo.compareEndPoints(oldInfo,"startToStart") startToEnd=newInfo.compareEndPoints(oldInfo,"startToEnd") endToStart=newInfo.compareEndPoints(oldInfo,"endToStart") endToEnd=newInfo.compareEndPoints(oldInfo,"endToEnd") if speakSelected and oldInfo.isCollapsed: selectedTextList.append(newInfo.text) elif speakUnselected and newInfo.isCollapsed: unselectedTextList.append(oldInfo.text) else: if startToEnd>0 or endToStart<0: if speakSelected and not newInfo.isCollapsed: selectedTextList.append(newInfo.text) if speakUnselected and not oldInfo.isCollapsed: unselectedTextList.append(oldInfo.text) else: if speakSelected and startToStart<0 and not newInfo.isCollapsed: tempInfo=newInfo.copy() tempInfo.setEndPoint(oldInfo,"endToStart") selectedTextList.append(tempInfo.text) if speakSelected and endToEnd>0 and not newInfo.isCollapsed: tempInfo=newInfo.copy() tempInfo.setEndPoint(oldInfo,"startToEnd") selectedTextList.append(tempInfo.text) if startToStart>0 and not oldInfo.isCollapsed: tempInfo=oldInfo.copy() tempInfo.setEndPoint(newInfo,"endToStart") unselectedTextList.append(tempInfo.text) if endToEnd<0 and not oldInfo.isCollapsed: tempInfo=oldInfo.copy() tempInfo.setEndPoint(newInfo,"startToEnd") unselectedTextList.append(tempInfo.text) locale=languageHandler.getLanguage() if speakSelected: if not generalize: for text in selectedTextList: if len(text)==1: text=characterProcessing.processSpeechSymbol(locale,text) # Translators: This is spoken while the user is in the process of selecting something, For example: "hello selected" speakSelectionMessage(_("%s selected"),text) elif len(selectedTextList)>0: text=newInfo.text if len(text)==1: text=characterProcessing.processSpeechSymbol(locale,text) # Translators: This is spoken to indicate what has been selected. for example 'selected hello world' speakSelectionMessage(_("selected %s"),text) if speakUnselected: if not generalize: for text in unselectedTextList: if len(text)==1: text=characterProcessing.processSpeechSymbol(locale,text) # Translators: This is spoken to indicate what has been unselected. for example 'hello unselected' speakSelectionMessage(_("%s unselected"),text) elif len(unselectedTextList)>0: if not newInfo.isCollapsed: text=newInfo.text if len(text)==1: text=characterProcessing.processSpeechSymbol(locale,text) # Translators: This is spoken to indicate when the previous selection was removed and a new selection was made. for example 'hello world selected instead' speakSelectionMessage(_("%s selected instead"),text) else: # Translators: Reported when selection is removed. speakMessage(_("selection removed")) def speakTypedCharacters(ch): global curWordChars; typingIsProtected=api.isTypingProtected() if typingIsProtected: realChar="*" else: realChar=ch if unicodedata.category(ch)[0] in "LMN": curWordChars.append(realChar) elif ch=="\b": # Backspace, so remove the last character from our buffer. del curWordChars[-1:] elif ch==u'\u007f': # delete character produced in some apps with control+backspace return elif len(curWordChars)>0: typedWord="".join(curWordChars) curWordChars=[] if log.isEnabledFor(log.IO): log.io("typed word: %s"%typedWord) if config.conf["keyboard"]["speakTypedWords"] and not typingIsProtected: speakText(typedWord) if config.conf["keyboard"]["speakTypedCharacters"] and ord(ch)>=32: speakSpelling(realChar) class SpeakTextInfoState(object): """Caches the state of speakTextInfo such as the current controlField stack, current formatfield and indentation.""" __slots__=[ 'objRef', 'controlFieldStackCache', 'formatFieldAttributesCache', 'indentationCache', ] def __init__(self,obj): if isinstance(obj,SpeakTextInfoState): oldState=obj self.objRef=oldState.objRef else: self.objRef=weakref.ref(obj) oldState=getattr(obj,'_speakTextInfoState',None) self.controlFieldStackCache=list(oldState.controlFieldStackCache) if oldState else [] self.formatFieldAttributesCache=oldState.formatFieldAttributesCache if oldState else {} self.indentationCache=oldState.indentationCache if oldState else "" def updateObj(self): obj=self.objRef() if obj: obj._speakTextInfoState=self.copy() def copy(self): return self.__class__(self) def _speakTextInfo_addMath(speechSequence, info, field): import mathPres mathPres.ensureInit() if not mathPres.speechProvider: return try: speechSequence.extend(mathPres.speechProvider.getSpeechForMathMl(info.getMathMl(field))) except (NotImplementedError, LookupError): return def speakTextInfo(info,useCache=True,formatConfig=None,unit=None,reason=controlTypes.REASON_QUERY,index=None,onlyInitialFields=False,suppressBlanks=False): if isinstance(useCache,SpeakTextInfoState): speakTextInfoState=useCache elif useCache: speakTextInfoState=SpeakTextInfoState(info.obj) else: speakTextInfoState=None autoLanguageSwitching=config.conf['speech']['autoLanguageSwitching'] extraDetail=unit in (textInfos.UNIT_CHARACTER,textInfos.UNIT_WORD) if not formatConfig: formatConfig=config.conf["documentFormatting"] if extraDetail: formatConfig=formatConfig.copy() formatConfig['extraDetail']=True reportIndentation=unit==textInfos.UNIT_LINE and ( formatConfig["reportLineIndentation"] or formatConfig["reportLineIndentationWithTones"]) speechSequence=[] #Fetch the last controlFieldStack, or make a blank one controlFieldStackCache=speakTextInfoState.controlFieldStackCache if speakTextInfoState else [] formatFieldAttributesCache=speakTextInfoState.formatFieldAttributesCache if speakTextInfoState else {} textWithFields=info.getTextWithFields(formatConfig) # We don't care about node bounds, especially when comparing fields. # Remove them. for command in textWithFields: if not isinstance(command,textInfos.FieldCommand): continue field=command.field if not field: continue try: del field["_startOfNode"] except KeyError: pass try: del field["_endOfNode"] except KeyError: pass #Make a new controlFieldStack and formatField from the textInfo's initialFields newControlFieldStack=[] newFormatField=textInfos.FormatField() initialFields=[] for field in textWithFields: if isinstance(field,textInfos.FieldCommand) and field.command in ("controlStart","formatChange"): initialFields.append(field.field) else: break if len(initialFields)>0: del textWithFields[0:len(initialFields)] endFieldCount=0 for field in reversed(textWithFields): if isinstance(field,textInfos.FieldCommand) and field.command=="controlEnd": endFieldCount+=1 else: break if endFieldCount>0: del textWithFields[0-endFieldCount:] for field in initialFields: if isinstance(field,textInfos.ControlField): newControlFieldStack.append(field) elif isinstance(field,textInfos.FormatField): newFormatField.update(field) else: raise ValueError("unknown field: %s"%field) #Calculate how many fields in the old and new controlFieldStacks are the same commonFieldCount=0 for count in xrange(min(len(newControlFieldStack),len(controlFieldStackCache))): # #2199: When comparing controlFields try using uniqueID if it exists before resorting to compairing the entire dictionary oldUniqueID=controlFieldStackCache[count].get('uniqueID') newUniqueID=newControlFieldStack[count].get('uniqueID') if ((oldUniqueID is not None or newUniqueID is not None) and newUniqueID==oldUniqueID) or (newControlFieldStack[count]==controlFieldStackCache[count]): commonFieldCount+=1 else: break #Get speech text for any fields in the old controlFieldStack that are not in the new controlFieldStack endingBlock=False for count in reversed(xrange(commonFieldCount,len(controlFieldStackCache))): text=info.getControlFieldSpeech(controlFieldStackCache[count],controlFieldStackCache[0:count],"end_removedFromControlFieldStack",formatConfig,extraDetail,reason=reason) if text: speechSequence.append(text) if not endingBlock and reason==controlTypes.REASON_SAYALL: endingBlock=bool(int(controlFieldStackCache[count].get('isBlock',0))) if endingBlock: speechSequence.append(SpeakWithoutPausesBreakCommand()) # The TextInfo should be considered blank if we are only exiting fields (i.e. we aren't entering any new fields and there is no text). isTextBlank=True # Even when there's no speakable text, we still need to notify the synth of the index. if index is not None: speechSequence.append(IndexCommand(index)) #Get speech text for any fields that are in both controlFieldStacks, if extra detail is not requested if not extraDetail: for count in xrange(commonFieldCount): field=newControlFieldStack[count] text=info.getControlFieldSpeech(field,newControlFieldStack[0:count],"start_inControlFieldStack",formatConfig,extraDetail,reason=reason) if text: speechSequence.append(text) isTextBlank=False if field.get("role")==controlTypes.ROLE_MATH: isTextBlank=False _speakTextInfo_addMath(speechSequence,info,field) #Get speech text for any fields in the new controlFieldStack that are not in the old controlFieldStack for count in xrange(commonFieldCount,len(newControlFieldStack)): field=newControlFieldStack[count] text=info.getControlFieldSpeech(field,newControlFieldStack[0:count],"start_addedToControlFieldStack",formatConfig,extraDetail,reason=reason) if text: speechSequence.append(text) isTextBlank=False if field.get("role")==controlTypes.ROLE_MATH: isTextBlank=False _speakTextInfo_addMath(speechSequence,info,field) commonFieldCount+=1 #Fetch the text for format field attributes that have changed between what was previously cached, and this textInfo's initialFormatField. text=getFormatFieldSpeech(newFormatField,formatFieldAttributesCache,formatConfig,unit=unit,extraDetail=extraDetail) if text: speechSequence.append(text) if autoLanguageSwitching: language=newFormatField.get('language') speechSequence.append(LangChangeCommand(language)) lastLanguage=language if onlyInitialFields or (unit in (textInfos.UNIT_CHARACTER,textInfos.UNIT_WORD) and len(textWithFields)>0 and len(textWithFields[0])==1 and all((isinstance(x,textInfos.FieldCommand) and x.command=="controlEnd") for x in itertools.islice(textWithFields,1,None) )): if onlyInitialFields or any(isinstance(x,basestring) for x in speechSequence): speak(speechSequence) if not onlyInitialFields: speakSpelling(textWithFields[0],locale=language if autoLanguageSwitching else None) if useCache: speakTextInfoState.controlFieldStackCache=newControlFieldStack speakTextInfoState.formatFieldAttributesCache=formatFieldAttributesCache if not isinstance(useCache,SpeakTextInfoState): speakTextInfoState.updateObj() return #Move through the field commands, getting speech text for all controlStarts, controlEnds and formatChange commands #But also keep newControlFieldStack up to date as we will need it for the ends # Add any text to a separate list, as it must be handled differently. #Also make sure that LangChangeCommand objects are added before any controlField or formatField speech relativeSpeechSequence=[] inTextChunk=False allIndentation="" indentationDone=False for command in textWithFields: if isinstance(command,basestring): if reportIndentation and not indentationDone: indentation,command=splitTextIndentation(command) # Combine all indentation into one string for later processing. allIndentation+=indentation if command: # There was content after the indentation, so there is no more indentation. indentationDone=True if command: if inTextChunk: relativeSpeechSequence[-1]+=command else: relativeSpeechSequence.append(command) inTextChunk=True elif isinstance(command,textInfos.FieldCommand): newLanguage=None if command.command=="controlStart": # Control fields always start a new chunk, even if they have no field text. inTextChunk=False fieldText=info.getControlFieldSpeech(command.field,newControlFieldStack,"start_relative",formatConfig,extraDetail,reason=reason) newControlFieldStack.append(command.field) elif command.command=="controlEnd": # Control fields always start a new chunk, even if they have no field text. inTextChunk=False fieldText=info.getControlFieldSpeech(newControlFieldStack[-1],newControlFieldStack[0:-1],"end_relative",formatConfig,extraDetail,reason=reason) del newControlFieldStack[-1] if commonFieldCount>len(newControlFieldStack): commonFieldCount=len(newControlFieldStack) elif command.command=="formatChange": fieldText=getFormatFieldSpeech(command.field,formatFieldAttributesCache,formatConfig,unit=unit,extraDetail=extraDetail) if fieldText: inTextChunk=False if autoLanguageSwitching: newLanguage=command.field.get('language') if lastLanguage!=newLanguage: # The language has changed, so this starts a new text chunk. inTextChunk=False if not inTextChunk: if fieldText: if autoLanguageSwitching and lastLanguage is not None: # Fields must be spoken in the default language. relativeSpeechSequence.append(LangChangeCommand(None)) lastLanguage=None relativeSpeechSequence.append(fieldText) if command.command=="controlStart" and command.field.get("role")==controlTypes.ROLE_MATH: _speakTextInfo_addMath(relativeSpeechSequence,info,command.field) if autoLanguageSwitching and newLanguage!=lastLanguage: relativeSpeechSequence.append(LangChangeCommand(newLanguage)) lastLanguage=newLanguage if reportIndentation and speakTextInfoState and allIndentation!=speakTextInfoState.indentationCache: indentationSpeech=getIndentationSpeech(allIndentation, formatConfig) if autoLanguageSwitching and speechSequence[-1].lang is not None: # Indentation must be spoken in the default language, # but the initial format field specified a different language. # Insert the indentation before the LangChangeCommand. speechSequence.insert(-1, indentationSpeech) else: speechSequence.append(indentationSpeech) if speakTextInfoState: speakTextInfoState.indentationCache=allIndentation # Don't add this text if it is blank. relativeBlank=True for x in relativeSpeechSequence: if isinstance(x,basestring) and not isBlank(x): relativeBlank=False break if not relativeBlank: speechSequence.extend(relativeSpeechSequence) isTextBlank=False #Finally get speech text for any fields left in new controlFieldStack that are common with the old controlFieldStack (for closing), if extra detail is not requested if autoLanguageSwitching and lastLanguage is not None: speechSequence.append(LangChangeCommand(None)) lastLanguage=None if not extraDetail: for count in reversed(xrange(min(len(newControlFieldStack),commonFieldCount))): text=info.getControlFieldSpeech(newControlFieldStack[count],newControlFieldStack[0:count],"end_inControlFieldStack",formatConfig,extraDetail,reason=reason) if text: speechSequence.append(text) isTextBlank=False # If there is nothing that should cause the TextInfo to be considered non-blank, blank should be reported, unless we are doing a say all. if not suppressBlanks and reason != controlTypes.REASON_SAYALL and isTextBlank: # Translators: This is spoken when the line is considered blank. speechSequence.append(_("blank")) #Cache a copy of the new controlFieldStack for future use if useCache: speakTextInfoState.controlFieldStackCache=list(newControlFieldStack) speakTextInfoState.formatFieldAttributesCache=formatFieldAttributesCache if not isinstance(useCache,SpeakTextInfoState): speakTextInfoState.updateObj() if speechSequence: if reason==controlTypes.REASON_SAYALL: speakWithoutPauses(speechSequence) else: speak(speechSequence) def getSpeechTextForProperties(reason=controlTypes.REASON_QUERY,**propertyValues): global oldTreeLevel, oldTableID, oldRowNumber, oldColumnNumber textList=[] name=propertyValues.get('name') if name: textList.append(name) if 'role' in propertyValues: role=propertyValues['role'] speakRole=True elif '_role' in propertyValues: speakRole=False role=propertyValues['_role'] else: speakRole=False role=controlTypes.ROLE_UNKNOWN value=propertyValues.get('value') if role not in controlTypes.silentValuesForRoles else None cellCoordsText=propertyValues.get('cellCoordsText') rowNumber=propertyValues.get('rowNumber') columnNumber=propertyValues.get('columnNumber') includeTableCellCoords=propertyValues.get('includeTableCellCoords',True) if speakRole and (reason not in (controlTypes.REASON_SAYALL,controlTypes.REASON_CARET,controlTypes.REASON_FOCUS) or not (name or value or cellCoordsText or rowNumber or columnNumber) or role not in controlTypes.silentRolesOnFocus) and (role!=controlTypes.ROLE_MATH or reason not in (controlTypes.REASON_CARET,controlTypes.REASON_SAYALL)): textList.append(controlTypes.roleLabels[role]) if value: textList.append(value) states=propertyValues.get('states') realStates=propertyValues.get('_states',states) if states is not None: positiveStates=controlTypes.processPositiveStates(role,realStates,reason,states) textList.extend([controlTypes.stateLabels[x] for x in positiveStates]) if 'negativeStates' in propertyValues: negativeStates=propertyValues['negativeStates'] else: negativeStates=None if negativeStates is not None or (reason != controlTypes.REASON_CHANGE and states is not None): negativeStates=controlTypes.processNegativeStates(role, realStates, reason, negativeStates) if controlTypes.STATE_DROPTARGET in negativeStates: # "not drop target" doesn't make any sense, so use a custom message. # Translators: Reported when drag and drop is finished. # This is only reported for objects which support accessible drag and drop. textList.append(_("done dragging")) negativeStates.discard(controlTypes.STATE_DROPTARGET) # Translators: Indicates that a particular state on an object is negated. # Separate strings have now been defined for commonly negated states (e.g. not selected and not checked), # but this still might be used in some other cases. # %s will be replaced with the negated state. textList.extend([controlTypes.negativeStateLabels.get(x, _("not %s")%controlTypes.stateLabels[x]) for x in negativeStates]) if 'description' in propertyValues: textList.append(propertyValues['description']) if 'keyboardShortcut' in propertyValues: textList.append(propertyValues['keyboardShortcut']) if includeTableCellCoords and cellCoordsText: textList.append(cellCoordsText) if cellCoordsText or rowNumber or columnNumber: tableID = propertyValues.get("_tableID") # Always treat the table as different if there is no tableID. sameTable = (tableID and tableID == oldTableID) # Don't update the oldTableID if no tableID was given. if tableID and not sameTable: oldTableID = tableID if rowNumber and (not sameTable or rowNumber != oldRowNumber): rowHeaderText = propertyValues.get("rowHeaderText") if rowHeaderText: textList.append(rowHeaderText) if includeTableCellCoords and not cellCoordsText: # Translators: Speaks current row number (example output: row 3). textList.append(_("row %s")%rowNumber) oldRowNumber = rowNumber if columnNumber and (not sameTable or columnNumber != oldColumnNumber): columnHeaderText = propertyValues.get("columnHeaderText") if columnHeaderText: textList.append(columnHeaderText) if includeTableCellCoords and not cellCoordsText: # Translators: Speaks current column number (example output: column 3). textList.append(_("column %s")%columnNumber) oldColumnNumber = columnNumber rowCount=propertyValues.get('rowCount',0) columnCount=propertyValues.get('columnCount',0) if rowCount and columnCount: # Translators: Speaks number of columns and rows in a table (example output: with 3 rows and 2 columns). textList.append(_("with {rowCount} rows and {columnCount} columns").format(rowCount=rowCount,columnCount=columnCount)) elif columnCount and not rowCount: # Translators: Speaks number of columns (example output: with 4 columns). textList.append(_("with %s columns")%columnCount) elif rowCount and not columnCount: # Translators: Speaks number of rows (example output: with 2 rows). textList.append(_("with %s rows")%rowCount) if rowCount or columnCount: # The caller is entering a table, so ensure that it is treated as a new table, even if the previous table was the same. oldTableID = None indexInGroup=propertyValues.get('positionInfo_indexInGroup',0) similarItemsInGroup=propertyValues.get('positionInfo_similarItemsInGroup',0) if 00 and lastStartIndex