Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it easier for add-ons to supply custom braille tables #5489

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions source/braille.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@

#: The directory in which liblouis braille tables are located.
TABLES_DIR = r"louis\tables"
UNICODE_BRAILLE_TABLE = os.path.join(TABLES_DIR, "braille-patterns.cti")

#: The table file names and information.
TABLES = (
#: The braille table file names and information.
tables = [
# (fileName, displayName, supportsInput),
# Translators: The name of a braille table displayed in the
# braille settings dialog.
Expand Down Expand Up @@ -277,10 +278,16 @@
# Translators: The name of a braille table displayed in the
# braille settings dialog.
("zh-tw.ctb", _("Chinese (Taiwan, Mandarin)"), False),
)
]

#: Braille tables that support input (only computer braille tables yet).
INPUT_TABLES = tuple(t for t in TABLES if t[2])
def _getTablePath(table):
if not os.path.isabs(table):
# This is a stock table.
return os.path.join(TABLES_DIR, table)
return table

def _getTableList(table):
return [_getTablePath(table), UNICODE_BRAILLE_TABLE]

roleLabels = {
# Translators: Displayed in braille for an object which is an
Expand Down Expand Up @@ -446,9 +453,7 @@ def update(self):
mode |= louis.compbrlAtCursor
text=unicode(self.rawText).replace('\0','')
braille, self.brailleToRawPos, self.rawToBraillePos, brailleCursorPos = louis.translate(
[os.path.join(TABLES_DIR, config.conf["braille"]["translationTable"]),
"braille-patterns.cti"],
text,
handler.translationTableList, text,
# liblouis mutates typeform if it is a list.
typeform=tuple(self.rawTextTypeforms) if isinstance(self.rawTextTypeforms, list) else self.rawTextTypeforms,
mode=mode, cursorPos=self.cursorPos or 0)
Expand Down Expand Up @@ -1349,6 +1354,7 @@ class BrailleHandler(baseObject.AutoPropertyObject):
def __init__(self):
self.display = None
self.displaySize = 0
self.translationTableList = []
self.mainBuffer = BrailleBuffer(self)
self.messageBuffer = BrailleBuffer(self)
self._messageCallLater = None
Expand Down Expand Up @@ -1427,6 +1433,19 @@ def setDisplayByName(self, name, isFallback=False):
self.setDisplayByName("noBraille", isFallback=True)
return False

def setTranslationTable(self, table, isFallback=False):
tableList = _getTableList(table)
if not isFallback:
try:
louis.checkTable(tableList)
except:
log.error("Error compiling translation tables", exc_info=True)
self.setTranslationTable("en-us-comp8.ctb", isFallback=True)
return False
config.conf["braille"]["translationTable"] = table
self.translationTableList = tableList
return True

def _updateDisplay(self):
if self._cursorBlinkTimer:
self._cursorBlinkTimer.Stop()
Expand Down Expand Up @@ -1609,13 +1628,15 @@ def handleConfigProfileSwitch(self):
display = config.conf["braille"]["display"]
if display != self.display.name:
self.setDisplayByName(display)
self.setTranslationTable(config.conf["braille"]["translationTable"])

def initialize():
global handler
config.addConfigDirsToPythonPackagePath(brailleDisplayDrivers)
log.info("Using liblouis version %s" % louis.version())
handler = BrailleHandler()
handler.setDisplayByName(config.conf["braille"]["display"])
handler.setTranslationTable(config.conf["braille"]["translationTable"])

# Update the display to the current focus/review position.
if not handler.enabled or not api.getDesktopObject():
Expand Down
25 changes: 21 additions & 4 deletions source/brailleInput.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#See the file COPYING for more details.
#Copyright (C) 2012-2013 NV Access Limited, Rui Batista

import os.path
import louis
import braille
import config
Expand All @@ -25,6 +24,7 @@
def initialize():
global handler
handler = BrailleInputHandler()
handler.setInputTable(config.conf["braille"]["inputTable"])
log.info("Braille input initialized")

def terminate():
Expand All @@ -35,15 +35,29 @@ class BrailleInputHandler(object):
"""Handles braille input.
"""

def __init__(self):
self.inputTableList = []

def setInputTable(self, table, isFallback=False):
tableList = braille._getTableList(table)
if not isFallback:
try:
louis.checkTable(tableList)
except:
log.error("Error compiling input tables", exc_info=True)
self.setInputTable("en-us-comp8.ctb", isFallback=True)
return False
config.conf["braille"]["inputTable"] = table
self.inputTableList = tableList
return True

def input(self, dots):
"""Handle one cell of braille input.
"""
# liblouis requires us to set the highest bit for proper use of dotsIO.
char = unichr(dots | 0x8000)
text = louis.backTranslate(
[os.path.join(braille.TABLES_DIR, config.conf["braille"]["inputTable"]),
"braille-patterns.cti"],
char, mode=louis.dotsIO)
braille.handler.inputTableList, char, mode=louis.dotsIO)
chars = text[0]
if len(chars) > 0:
self.sendChars(chars)
Expand All @@ -60,6 +74,9 @@ def sendChars(self, chars):
inputs.append(input)
winUser.SendInput(inputs)

def handleConfigProfileSwitch(self):
self.setInputTable(config.conf["braille"]["translationTable"])

class BrailleInputGesture(inputCore.InputGesture):
"""Input (dots and/or space bar) from a braille keyboard.
This could either be as part of a braille display or a stand-alone unit.
Expand Down
2 changes: 2 additions & 0 deletions source/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,9 @@ def _handleProfileSwitch(self):
import synthDriverHandler
synthDriverHandler.handleConfigProfileSwitch()
import braille
import brailleInput
braille.handler.handleConfigProfileSwitch()
brailleInput.handler.handleConfigProfileSwitch()

def _initBaseConf(self, factoryDefaults=False):
fn = os.path.join(globalVars.appArgs.configPath, "nvda.ini")
Expand Down
21 changes: 15 additions & 6 deletions source/gui/settingsDialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import appModuleHandler
import queueHandler
import braille
import brailleInput
import core
import keyboardHandler
import characterProcessing
Expand Down Expand Up @@ -1370,8 +1371,9 @@ def makeSettings(self, settingsSizer):
sizer = wx.BoxSizer(wx.HORIZONTAL)
# Translators: The label for a setting in braille settings to select the output table (the braille table used to read braille text on the braille display).
label = wx.StaticText(self, wx.ID_ANY, label=_("&Output table:"))
self.tableNames = [table[0] for table in braille.TABLES]
self.tableList = wx.Choice(self, wx.ID_ANY, choices=[table[1] for table in braille.TABLES])
tables = sorted(braille.tables, key=lambda table: table[1])
self.tableNames = [table[0] for table in tables]
self.tableList = wx.Choice(self, wx.ID_ANY, choices=[table[1] for table in tables])
try:
selection = self.tableNames.index(config.conf["braille"]["translationTable"])
self.tableList.SetSelection(selection)
Expand All @@ -1384,8 +1386,9 @@ def makeSettings(self, settingsSizer):
sizer = wx.BoxSizer(wx.HORIZONTAL)
# Translators: The label for a setting in braille settings to select the input table (the braille table used to type braille characters on a braille keyboard).
label = wx.StaticText(self, wx.ID_ANY, label=_("&Input table:"))
self.inputTableNames = [table[0] for table in braille.INPUT_TABLES]
self.inputTableList = wx.Choice(self, wx.ID_ANY, choices=[table[1] for table in braille.INPUT_TABLES])
tables = [table for table in tables if table[2]]
self.inputTableNames = [table[0] for table in tables]
self.inputTableList = wx.Choice(self, wx.ID_ANY, choices=[table[1] for table in tables])
try:
selection = self.inputTableNames.index(config.conf["braille"]["inputTable"])
self.inputTableList.SetSelection(selection)
Expand Down Expand Up @@ -1456,8 +1459,14 @@ def onOk(self, evt):
if not braille.handler.setDisplayByName(display):
gui.messageBox(_("Could not load the %s display.")%display, _("Braille Display Error"), wx.OK|wx.ICON_WARNING, self)
return
config.conf["braille"]["translationTable"] = self.tableNames[self.tableList.GetSelection()]
config.conf["braille"]["inputTable"] = self.inputTableNames[self.inputTableList.GetSelection()]
table = self.tableNames[self.tableList.GetSelection()]
if not braille.handler.setTranslationTable(table):
gui.messageBox(_("Could not load the %s translation table.")%table, _("Braille Table Error"), wx.OK|wx.ICON_WARNING, self)
return
table = self.inputTableNames[self.inputTableList.GetSelection()]
if not brailleInput.handler.setInputTable(table):
gui.messageBox(_("Could not load the %s input table.")%table, _("Braille Table Error"), wx.OK|wx.ICON_WARNING, self)
return
config.conf["braille"]["expandAtCursor"] = self.expandAtCursorCheckBox.GetValue()
try:
val = int(self.cursorBlinkRateEdit.GetValue())
Expand Down