Skip to content

Commit

Permalink
Update to use new MathCAT structure (published as a crate).
Browse files Browse the repository at this point in the history
Found problem with addon not working -- needed to include python3.dll in build
  • Loading branch information
NSoiffer committed Feb 15, 2022
1 parent 294e9b0 commit a3776d1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "MathCatForPython"
version = "0.1.0"
version = "0.1.1"
authors = ["Neil Soiffer <[email protected]>"]
edition = "2018"

Expand All @@ -12,8 +12,8 @@ edition = "2018"
name = "libmathcat_py"
crate-type = ["cdylib"]

[dependencies.MathCAT]
path = "../mathcat/"
[dependencies]
mathcat = "0.1.2"

[dependencies.pyo3]
version = "0.15.1"
Expand Down
9 changes: 5 additions & 4 deletions NVDA-addon/addon/globalPlugins/MathCAT/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
# Copyright: this file is copyright GPL2
# The code additionally makes use of the MathCAT library (written in Rust) which is covered by the MIT license
# and also (obviously) requires external speech engines and braille drivers.
# Note: this code is a lot of cut/paste from other code and very likely could be substantially improved/cleaned.
# The plugin also requires the use of a small python dll: python3.dll
# python3.dll has "Copyright © 2001-2022 Python Software Foundation; All Rights Reserved"


# Note: this code is a lot of cut/paste from other code and very likely could be substantially improved/cleaned.
import braille # we generate braille
import globalPlugins # we are a global plugin
import globalPluginHandler # we are a global plugin
Expand Down Expand Up @@ -278,13 +281,13 @@ def getSpeechForMathMl(self, mathml):


def getBrailleForMathMl(self, mathml):
log.info("***MathCAT getBrailleForMathMl")
try:
libmathcat.SetMathML(mathml)
except Exception as e:
log.error(e)
speech.speakMessage(_("Illegal MathML found: see NVDA error log for details"))
try:
log.info("***MathCAT getBrailleForMathMl")
return libmathcat.GetBraille("")
except Exception as e:
log.error(e)
Expand All @@ -311,5 +314,3 @@ class GlobalPlugin(globalPluginHandler.GlobalPlugin):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
MathCAT.__init__(self)

log.info("---- read mathcat file 1 ---")
2 changes: 1 addition & 1 deletion NVDA-addon/build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/csh

cargo build --release
cargo build --target i686-pc-windows-msvc --release

# copy over all the rules and then remove a few "extra" files
cp -r ../../MathCAT/Rules addon/globalPlugins/MathCAT
Expand Down
29 changes: 12 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#![allow(non_snake_case)]
#![allow(clippy::needless_return)]

use libmathcat::interface::StringOrFloat;
use libmathcat::*;
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
use pyo3::exceptions::PyOSError;
Expand All @@ -36,7 +36,7 @@ fn convert_error<T>(result: Result<T, libmathcat::errors::Error>) -> PyResult<T>
return match result {
Ok(answer) => Ok(answer),
Err(e) => {
Err( PyOSError::new_err(libmathcat::interface::errors_to_string(&e)) )
Err( PyOSError::new_err(errors_to_string(&e)) )
},
};
}
Expand All @@ -45,7 +45,7 @@ fn convert_error<T>(result: Result<T, libmathcat::errors::Error>) -> PyResult<T>
/// The absolute path location of the MathCAT Rules dir.
/// IMPORTANT: This should be the first call to MathCAT
pub fn SetRulesDir(_py: Python, rules_dir_location: String) -> PyResult<()> {
return convert_error( libmathcat::interface::SetRulesDir(rules_dir_location) );
return convert_error( set_rules_dir(rules_dir_location) );
}

#[pyfunction]
Expand All @@ -55,13 +55,13 @@ pub fn SetRulesDir(_py: Python, rules_dir_location: String) -> PyResult<()> {
/// Returns: the MathML that was set, annotated with 'id' values on each node (if none were present)
/// The 'id' values can be used during navigation for highlighting the current node
pub fn SetMathML(_py: Python, mathml_str: String) -> PyResult<String> {
return convert_error( libmathcat::interface::SetMathML(mathml_str) );
return convert_error( set_mathml(mathml_str) );
}
#[pyfunction]
/// Get the spoken text of the MathML that was set.
/// The speech takes into account any AT or user preferences.
pub fn GetSpokenText(_py: Python) -> PyResult<String> {
return convert_error( libmathcat::interface::GetSpokenText() );
return convert_error( get_spoken_text() );
}

#[pyfunction]
Expand All @@ -71,12 +71,7 @@ pub fn GetSpokenText(_py: Python) -> PyResult<String> {
/// This function can be called multiple times to set different values.
/// The values are persistent but can be overwritten by setting a preference with the same name and a different value.
pub fn SetPreference(_py: Python, name: String, value: String) -> PyResult<()> {
let as_float = value.parse::<f64>();
let str_or_float = match as_float {
Ok(f) => StringOrFloat::AsFloat(f),
Err(_) => StringOrFloat::AsString(value),
};
return convert_error( libmathcat::interface::SetPreference(name, str_or_float) );
return convert_error( set_preference(name, value) );
}

#[pyfunction]
Expand All @@ -86,7 +81,7 @@ pub fn SetPreference(_py: Python, name: String, value: String) -> PyResult<()> {
/// This function can be called multiple times to set different values.
/// The values are persistent but can be overwritten by setting a preference with the same name and a different value.
pub fn GetPreference(_py: Python, name: String) -> PyResult<String> {
return match libmathcat::interface::GetPreference(name) {
return match get_preference(name) {
Some(value) => Ok(value),
None => Err( PyOSError::new_err("Unknown preference name") ),
}
Expand All @@ -99,15 +94,15 @@ pub fn GetPreference(_py: Python, name: String) -> PyResult<String> {
///
/// The braille returned depends upon the preference for braille output.
pub fn GetBraille(_py: Python, nav_node_id: String) -> PyResult<String> {
return convert_error( libmathcat::interface::GetBraille(nav_node_id) );
return convert_error( get_braille(nav_node_id) );
}

#[pyfunction]
/// Given a key code along with the modifier keys, the current node is moved accordingly (or value reported in some cases).
///
/// The spoken text for the new current node is returned.
pub fn DoNavigateKeyPress(_py: Python, key: usize, shift_key: bool, control_key: bool, alt_key: bool, meta_key: bool) -> PyResult<String> {
return convert_error( libmathcat::interface::DoNavigateKeyPress(key, shift_key, control_key, alt_key, meta_key) );
return convert_error( do_navigate_keypress(key, shift_key, control_key, alt_key, meta_key) );
}

#[pyfunction]
Expand All @@ -130,19 +125,19 @@ pub fn DoNavigateKeyPress(_py: Python, key: usize, shift_key: bool, control_key:
/// "Describe0","Describe1","Describe2","Describe3","Describe4","Describe5","Describe6","Describe7","Describe8","Describe9",
/// "SetPlacemarker0","SetPlacemarker1","SetPlacemarker2","SetPlacemarker3","SetPlacemarker4","SetPlacemarker5","SetPlacemarker6","SetPlacemarker7","SetPlacemarker8","SetPlacemarker9",
pub fn DoNavigateCommand(_py: Python, command: String) -> PyResult<String> {
return convert_error( libmathcat::interface::DoNavigateCommand(command) );
return convert_error( do_navigate_command(command) );
}

#[pyfunction]
/// Return the MathML associated with the current (navigation) node.
pub fn GetNavigationMathMLId(_py: Python) -> PyResult<(String, usize)> {
return convert_error( libmathcat::interface::GetNavigationMathMLId() );
return convert_error( get_navigation_mathml_id() );
}

#[pyfunction]
/// Return the MathML associated with the current (navigation) node.
pub fn GetNavigationMathML(_py: Python) -> PyResult<(String, usize)> {
return convert_error( libmathcat::interface::GetNavigationMathML() );
return convert_error( get_navigation_mathml() );
}

#[pymodule]
Expand Down

0 comments on commit a3776d1

Please sign in to comment.