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

Allow selection of BRF encoding - adding Eurobraille as an option #309

Merged
merged 2 commits into from
May 21, 2024
Merged
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
8 changes: 6 additions & 2 deletions ui/book/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .book_file import LoadState
from ..manual import Manual, manual_filename
from ..i18n import DEFAULT_LOCALE
from ..braille import DEFAULT_ENCODING
from .. import state


Expand All @@ -14,6 +15,7 @@ def __init__(self, root: 'state.RootState'):
self.root = root
self.current_book = manual_filename
self.current_language = DEFAULT_LOCALE
self.current_encoding = DEFAULT_ENCODING
self.books = OrderedDict({manual_filename: manual})

@property
Expand All @@ -25,11 +27,12 @@ def successful_books(self):
# should this be a (generator)?
return [b for b in self.books.values() if b.load_state != LoadState.FAILED]

def load(self, current_book, current_language):
def load(self, current_book, current_language, current_encoding):
self.current_book = current_book
if not self.current_book == manual_filename:
self.root.app.library.show_files_dir(self.current_book)
self.current_language = current_language
self.current_encoding = current_encoding

def go_to_start(self):
self.set_book_page(0)
Expand Down Expand Up @@ -78,5 +81,6 @@ def to_file(self, media_dir):
current_book = self.current_book
return {
'current_book': current_book,
'current_language': self.current_language
'current_language': self.current_language,
'current_encoding': self.current_encoding
}
28 changes: 24 additions & 4 deletions ui/braille.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import logging
from collections import namedtuple, OrderedDict
import curses.ascii as ASCII

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -134,10 +135,29 @@ def pin_num_to_unicode(pin_num):
"""
return chr(pin_num + UNICODE_BRAILLE_BASE)


# mapping from
# http://en.wikipedia.org/wiki/Braille_ASCII#Braille_ASCII_values
mapping = ' A1B\'K2L@CIF/MSP"E3H9O6R^DJG>NTQ,*5<-U8V.%[$+X!&;:4\\0Z7(_?W]#Y)='
# define _ as the identity function so that gettext picks up the msgids
# but we won't actually translate until later (see encoding.view)
def _(x): return x
Encoding = namedtuple('BuiltinEnc', ['name', 'title', 'mapping'])
encodings = [
# mapping from http://en.wikipedia.org/wiki/Braille_ASCII#Braille_ASCII_values
Encoding(name='braille-ascii', title=_('North American Braille ASCII'),
mapping=' A1B\'K2L@CIF/MSP"E3H9O6R^DJG>NTQ,*5<-U8V.%[$+X!&;:4\\0Z7(_?W]#Y)='),
# see http://www.braille.ch/brlchr-e.htm
Encoding(name='euro-braille', title=_('Eurobraille'),
mapping=' A,B.K;L"CIF\\MSP!E:H*O+R>DJG@NTQ\'1?2-U:V$3960X^&<5/8)Z=[_4W7#Y]%')
]
del _
BUILTIN_ENCODINGS = OrderedDict([
(enc.name, enc) for enc in encodings
])
DEFAULT_ENCODING = 'braille-ascii'

mapping = BUILTIN_ENCODINGS[DEFAULT_ENCODING].mapping

def set_encoding(encoding):
global mapping
mapping = BUILTIN_ENCODINGS[encoding].mapping


def pin_num_to_alpha(numeric):
Expand Down
3 changes: 2 additions & 1 deletion ui/config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def import_pages(module):
'go_to_page',
'bookmarks_menu',
'system_menu',
'language'
'language',
'encoding'
])
return { p:__import__(f'{p}.{module}', globals(), fromlist=[None], level=1) for p in pages }
40 changes: 40 additions & 0 deletions ui/encoding/buttons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from ..state import state


# create a function to call when the button is pressed
# (otherwise the function call happens immediately)
def select_encoding(index):
return lambda: state.app.encoding.select_encoding(index)


buttons = {
'single': {
'L': state.app.close_menu,
'2': select_encoding(0),
'3': select_encoding(1),
'4': select_encoding(2),
'5': select_encoding(3),
'6': select_encoding(4),
'7': select_encoding(5),
'8': select_encoding(6),
'9': select_encoding(7),
'<': state.app.previous_page,
'>': state.app.next_page,
'R': state.app.help_menu.toggle
},
'long': {
'L': state.app.close_menu,
'2': select_encoding(0),
'3': select_encoding(1),
'4': select_encoding(2),
'5': select_encoding(3),
'6': select_encoding(4),
'7': select_encoding(5),
'8': select_encoding(6),
'9': select_encoding(7),
'<': state.app.previous_page,
'>': state.app.next_page,
'R': state.app.help_menu.toggle,
'X': state.hardware.reset_display
}
}
19 changes: 19 additions & 0 deletions ui/encoding/state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from ..braille import BUILTIN_ENCODINGS, set_encoding
from .. import state


class EncodingState:
def __init__(self, root: 'state.RootState'):
self.root = root
self.available = BUILTIN_ENCODINGS

def select_encoding(self, value):
enc = abs(int(value))
keys = list(self.available.keys())
if enc < len(keys):
encoding = keys[enc]
set_encoding(encoding)
self.root.app.user.current_encoding = encoding
self.root.app.location = 'book'
self.root.refresh_display()
self.root.save_state()
55 changes: 55 additions & 0 deletions ui/encoding/view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from ..braille import from_ascii, format_title, to_ueb_number, from_unicode

def render_help(width, height):
data = []
para = _('''\
You can change the braille encoding by pressing the \
line select button to the left of your chosen encoding. \
The encoding will be used to display BRF files. \
''')

for line in para.split('\n'):
data.append(from_unicode(line))

while len(data) % height:
data.append(tuple())

return tuple(data)


async def render(width, height, state):
help_menu = state.app.help_menu.visible
if help_menu:
all_lines = render_help(width, height)
num_pages = len(all_lines) // height
page_num = min(state.app.help_menu.page, num_pages - 1)
first_line = page_num * height
off_end = first_line + height
page = all_lines[first_line:off_end]
return page

enc = state.app.user.current_encoding
encodings = state.app.encoding.available
current_enc = _(encodings.get(enc).title)

try:
cur_index = list(encodings.keys()).index(enc)
except ValueError:
cur_index = -1

# TRANSLATORS: A menu title, to which the encoding title is appended
title = _('encoding:') + ' {}'
title = format_title(
title.format(current_enc),
width, cur_index, len(encodings.keys()))
data = [title]

for enc in encodings:
encs = list(encodings.keys()).index(enc)
n = from_ascii(to_ueb_number(encs + 1) + ' ')
data.append(n + tuple(from_unicode(_(encodings[enc].title))))

while len(data) < height:
data.append(tuple())

return tuple(data)
11 changes: 10 additions & 1 deletion ui/initial_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .book.book_file import BookFile
from .library.explorer import Library, Directory, LocalFile
from .i18n import install, DEFAULT_LOCALE, OLD_DEFAULT_LOCALE
from .braille import set_encoding, DEFAULT_ENCODING

from . import config_loader

Expand Down Expand Up @@ -90,6 +91,7 @@ async def read_user_state(media_dir, state):
global manual
current_book = manual_filename
current_language = None
current_encoding = None

# walk the available filesystems for directories and braille files
library = Library(media_dir, configured_source_dirs(), ('brf', 'pef'))
Expand All @@ -105,6 +107,8 @@ async def read_user_state(media_dir, state):
current_book = main_state['current_book']
if 'current_language' in main_state:
current_language = main_state['current_language']
if 'current_encoding' in main_state:
current_encoding = main_state['current_encoding']
log.info(f'loaded {main_toml}')
break
except Exception:
Expand All @@ -113,6 +117,11 @@ async def read_user_state(media_dir, state):
if not current_language or current_language == OLD_DEFAULT_LOCALE:
current_language = DEFAULT_LOCALE

if not current_encoding:
current_encoding = DEFAULT_ENCODING
else:
set_encoding(current_encoding)

install(current_language)
manual = Manual.create()

Expand Down Expand Up @@ -167,7 +176,7 @@ async def read_user_state(media_dir, state):
state.app.user.books = books
state.app.library.media_dir = media_dir
state.app.library.dirs = library.dirs
state.app.user.load(current_book, current_language)
state.app.user.load(current_book, current_language, current_encoding)


async def read(media_dir, state):
Expand Down
2 changes: 0 additions & 2 deletions ui/language/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ class LanguageState:
def __init__(self, root: 'state.RootState'):
self.root = root
self.available = BUILTIN_LANGUAGES
self.selection = ''
self.keys_pressed = ''

def select_language(self, value):
lang = abs(int(value))
Expand Down
30 changes: 27 additions & 3 deletions ui/locale/canute.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-05-20 13:12+0100\n"
"POT-Creation-Date: 2024-05-20 18:54+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand All @@ -17,6 +17,14 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.13.1\n"

#: ui/braille.py:144
msgid "North American Braille ASCII"
msgstr ""

#: ui/braille.py:147
msgid "Eurobraille"
msgstr ""

#. TRANSLATORS: This is a language name menu item, so should always appear
#. in the language it denotes so that it remains readable to those who
#. speak only that language, just as "Deutsch" should always be left as
Expand Down Expand Up @@ -138,6 +146,18 @@ msgstr ""
msgid "end of book"
msgstr ""

#: ui/encoding/view.py:5
msgid ""
"You can change the braille encoding by pressing the line select button to"
" the left of your chosen encoding. The encoding will be used to display "
"BRF files. "
msgstr ""

#. TRANSLATORS: A menu title, to which the encoding title is appended
#: ui/encoding/view.py:41
msgid "encoding:"
msgstr ""

#: ui/go_to_page/view.py:6
msgid ""
"You can navigate to a page within a file by entering the page number "
Expand Down Expand Up @@ -251,14 +271,18 @@ msgid "shutdown"
msgstr ""

#: ui/system_menu/view.py:35
msgid "backup log to USB stick"
msgid "select language and code"
msgstr ""

#: ui/system_menu/view.py:36
msgid "select language and code"
msgid "choose BRF encoding"
msgstr ""

#: ui/system_menu/view.py:37
msgid "backup log to USB stick"
msgstr ""

#: ui/system_menu/view.py:38
msgid "install upgrade from "
msgstr ""

Expand Down
Binary file modified ui/locale/de_DE.UTF-8@ueb1/LC_MESSAGES/canute.mo
Binary file not shown.
Loading
Loading