From 09292d9e212717d84c02a349c31712e2dd62e24b Mon Sep 17 00:00:00 2001 From: David Austin Date: Fri, 13 Dec 2024 10:13:29 -0500 Subject: [PATCH] attempt to integrate liblouis into pyodide environment --- prefig/core/label.py | 3 ++- prefig/core/label_tools.py | 24 +++++++++++++++++-- website/package-lock.json | 7 ++++++ website/packages/playground/package.json | 1 + .../playground/src/worker/compat-api.ts | 18 ++++++++++++++ 5 files changed, 50 insertions(+), 3 deletions(-) diff --git a/prefig/core/label.py b/prefig/core/label.py index d5e5ad6..cfb4051 100644 --- a/prefig/core/label.py +++ b/prefig/core/label.py @@ -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) diff --git a/prefig/core/label_tools.py b/prefig/core/label_tools.py index 236f1a0..4fd6ec8 100644 --- a/prefig/core/label_tools.py +++ b/prefig/core/label_tools.py @@ -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') diff --git a/website/package-lock.json b/website/package-lock.json index 369c58b..e6f17ff 100644 --- a/website/package-lock.json +++ b/website/package-lock.json @@ -1876,6 +1876,12 @@ "graceful-fs": "^4.1.6" } }, + "node_modules/liblouis": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/liblouis/-/liblouis-0.2.1.tgz", + "integrity": "sha512-SxHzkXdfMd9yEN8+qo2wf6qeMkjEF+L+aMD3rpoZn2MEUDedx5EyY6bzXwYCdXpJC7kEY5Fekjoer6UZNVv9jQ==", + "license": "GPL-3.0" + }, "node_modules/loose-envify": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", @@ -2576,6 +2582,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", diff --git a/website/packages/playground/package.json b/website/packages/playground/package.json index e67b736..bd49f14 100644 --- a/website/packages/playground/package.json +++ b/website/packages/playground/package.json @@ -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", diff --git a/website/packages/playground/src/worker/compat-api.ts b/website/packages/playground/src/worker/compat-api.ts index 8a6b53f..dc60bfc 100644 --- a/website/packages/playground/src/worker/compat-api.ts +++ b/website/packages/playground/src/worker/compat-api.ts @@ -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); @@ -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();