forked from nvaccess/nvda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathType.py
47 lines (38 loc) · 1.21 KB
/
mathType.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- coding: UTF-8 -*-
# mathType.py
# A part of NonVisual Desktop Access (NVDA)
# Copyright (C) 2014-2020 NV Access Limited
# This file is covered by the GNU General Public License.
# See the file COPYING for more details.
"""Utilities for working with MathType."""
import winreg
import ctypes
import mathPres
def _getDllPath():
with winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
r"SOFTWARE\Design Science\DSMT6\Directories",
0,
winreg.KEY_WOW64_32KEY | winreg.KEY_QUERY_VALUE,
) as key:
return winreg.QueryValueEx(key, "AppSystemDir32")[0] + "\\MT6.dll"
lib = ctypes.windll[_getDllPath()]
def getMathMl(oleFormat, runForConversion=True):
"""Get MathML from a MathType OLEFormat object."""
if runForConversion:
oleFormat.DoVerb(2) # "RunForConversion"
mt = oleFormat.object._comobj
length = ctypes.c_long()
# 2 is get MathML
try:
if lib.MTGetLangStrFromEqn(mt, 2, None, ctypes.byref(length)) != 0:
raise RuntimeError
mathMl = (ctypes.c_char * length.value)()
if lib.MTGetLangStrFromEqn(mt, 2, ctypes.byref(mathMl), ctypes.byref(length)) != 0:
raise RuntimeError
finally:
# 1 is OLECLOSE_NOSAVE
lib.MTCloseOleObject(1, mt)
return mathPres.stripExtraneousXml(
mathMl.value.decode("utf8"),
)