Skip to content

Commit

Permalink
attempt to integrate liblouis into pyodide environment
Browse files Browse the repository at this point in the history
  • Loading branch information
davidaustinm committed Dec 13, 2024
1 parent 632f814 commit 09292d9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
3 changes: 2 additions & 1 deletion prefig/core/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ def init(format, environment):

if environment == "pyodide":
text_measurements = label_tools.PyodideTextMeasurements()
braille_translator = label_tools.PyodideBrailleTranslator()
else:
text_measurements = label_tools.CairoTextMeasurements()
braille_translator = label_tools.LocalLouisBrailleTranslator()

braille_translator = label_tools.LocalLouisBrailleTranslator()

def add_macros(macros):
math_labels.add_macros(macros)
Expand Down
24 changes: 22 additions & 2 deletions prefig/core/label_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,28 @@ def translate(self, text, typeform):
text,
typeform=typeform
)




class PyodideBrailleTranslator(AbstractBrailleTranslator):
def __init__(self):
global prefigBrowserApi
import prefigBrowserApi

def initialized(self):
return True

def translate(self, text, typeform):
log.info('Called translate text')
try:
# `prefigBrowserApi` will return a JsProxy. We want a native python object,
# so we convert it to a list.
braille_string = prefigBrowserApi.translate_text(text, typeform).to_py()
return braille_string
except Exception as e:
log.error(str(e))
log.error("Error in translating text")


class PyodideTextMeasurements(AbstractTextMeasurements):
def measure_text(self, text, font_data):
log.info('Called measure text')
Expand Down
7 changes: 7 additions & 0 deletions website/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions website/packages/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"comlink": "^4.4.2",
"easy-peasy": "^6.0.5",
"file-saver": "^2.0.5",
"liblouis": "^0.2.0",
"pyodide": "^0.26.4",
"react": "^18.3.1",
"react-bootstrap-icons": "^1.11.5",
Expand Down
18 changes: 18 additions & 0 deletions website/packages/playground/src/worker/compat-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
* This is the API used by PreFigure when running in the browser. It implements the necessary
* functions for Prefigure's abstract classes.
*/

//import { translateString } from "liblouis";

export class PrefigBrowserApi {
offscreenCanvas: OffscreenCanvas | null = null;
ctx: OffscreenCanvasRenderingContext2D | null = null;

measure_text(text: string, _font_data?: unknown) {
if (!this.offscreenCanvas) {
this.offscreenCanvas = new OffscreenCanvas(200, 200);
Expand All @@ -26,6 +30,20 @@ export class PrefigBrowserApi {
tm.actualBoundingBoxDescent,
];
}

translate_text(text: string, typeform: number[]): string {
//const tables = ["tables/braille-patterns.cti", "tables/en-us-g2.ctb"];
const tables = "tables/en-us-g2.ctb";
try {
// const unicode_result = translateString(tables, text);
const unicode_result = '';
console.log("Translated string", unicode_result);
return unicode_result
} catch(error) {
console.log("Error translating text:", error);
return '';
}
}
}

export const prefigBrowserApi = new PrefigBrowserApi();
Expand Down

0 comments on commit 09292d9

Please sign in to comment.