Skip to content
Open
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
3 changes: 3 additions & 0 deletions paddleocr/_doc2md/math/latex_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@
"lim": "\\lim_{{{lim}}}",
"max": "\\max_{{{lim}}}",
"min": "\\min_{{{lim}}}",
"inf": "\\inf_{{{lim}}}",
"sup": "\\sup_{{{lim}}}",
"gcd": "\\gcd_{{{lim}}}",
}

LIM_TO = ("\\rightarrow", "\\to")
Expand Down
63 changes: 44 additions & 19 deletions paddleocr/_doc2md/math/omml.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def process_unknow(self, elm, stag):
class Pr(Tag2Method):
text = ""

__val_tags = ("chr", "pos", "begChr", "endChr", "type")
__val_tags = ("chr", "pos", "begChr", "endChr", "sepChr", "type")

__innerdict = None # can't use the __dict__

Expand Down Expand Up @@ -174,6 +174,7 @@ def do_common(self, elm):
"pos": do_common,
"begChr": do_common,
"endChr": do_common,
"sepChr": do_common,
"type": do_common,
}

Expand Down Expand Up @@ -227,33 +228,51 @@ def do_acc(self, elm):
the accent function
"""
c_dict = self.process_children_dict(elm)
pr = c_dict.get("accPr")
latex_s = get_val(
c_dict["accPr"].chr, default=CHR_DEFAULT.get("ACC_VAL"), store=CHR
pr.chr if pr is not None else None,
default=CHR_DEFAULT.get("ACC_VAL"),
store=CHR,
)
return latex_s.format(c_dict["e"])
return (pr.text if pr is not None else "") + latex_s.format(c_dict["e"])

def do_bar(self, elm):
"""
the bar function
"""
c_dict = self.process_children_dict(elm)
pr = c_dict["barPr"]
latex_s = get_val(pr.pos, default=POS_DEFAULT.get("BAR_VAL"), store=POS)
return pr.text + latex_s.format(c_dict["e"])
pr = c_dict.get("barPr")
latex_s = get_val(
pr.pos if pr is not None else None,
default=POS_DEFAULT.get("BAR_VAL"),
store=POS,
)
return (pr.text if pr is not None else "") + latex_s.format(c_dict["e"])

def do_d(self, elm):
"""
the delimiter object
"""
c_dict = self.process_children_dict(elm)
pr = c_dict["dPr"]
children = list(self.process_children_list(elm))
pr = next((text for stag, text, _ in children if stag == "dPr"), None)
null = D_DEFAULT.get("null")

s_val = get_val(pr.begChr, default=D_DEFAULT.get("left"), store=T)
e_val = get_val(pr.endChr, default=D_DEFAULT.get("right"), store=T)
delim = pr.text + D.format(
expressions = [text for stag, text, _ in children if stag == "e"]
separator = get_val(pr.sepChr if pr is not None else None, default=",")
separator = r"\middle|" if separator == "|" else escape_latex(separator)

s_val = get_val(
pr.begChr if pr is not None else None,
default=D_DEFAULT.get("left"),
store=T,
)
e_val = get_val(
pr.endChr if pr is not None else None,
default=D_DEFAULT.get("right"),
store=T,
)
delim = (pr.text if pr is not None else "") + D.format(
left=null if not s_val else escape_latex(s_val),
text=c_dict["e"],
text=separator.join(expressions),
right=null if not e_val else escape_latex(e_val),
)
return delim
Expand Down Expand Up @@ -288,6 +307,8 @@ def do_func(self, elm):
"""
c_dict = self.process_children_dict(elm)
func_name = c_dict.get("fName")
if func_name is None:
return c_dict.get("e", "")
return func_name.replace(FUNC_PLACE, c_dict.get("e"))

def do_fname(self, elm):
Expand Down Expand Up @@ -315,9 +336,12 @@ def do_groupchr(self, elm):
the Group-Character object
"""
c_dict = self.process_children_dict(elm)
pr = c_dict["groupChrPr"]
latex_s = get_val(pr.chr)
return pr.text + latex_s.format(c_dict["e"])
pr = c_dict.get("groupChrPr")
latex_s = get_val(
pr.chr if pr is not None else None,
default=CHR["\u23df"],
)
return (pr.text if pr is not None else "") + latex_s.format(c_dict["e"])

def do_rad(self, elm):
"""
Expand Down Expand Up @@ -355,9 +379,10 @@ def do_limlow(self, elm):
t_dict = self.process_children_dict(elm, include=("e", "lim"))
latex_s = LIM_FUNC.get(t_dict["e"])
if not latex_s:
raise RuntimeError("Not support lim {}".format(t_dict["e"]))
else:
return latex_s.format(lim=t_dict.get("lim"))
return r"\underset{{{lim}}}{{{text}}}".format(
lim=t_dict.get("lim"), text=t_dict["e"]
)
return latex_s.format(lim=t_dict.get("lim"))

def do_limupp(self, elm):
"""
Expand Down
94 changes: 94 additions & 0 deletions tests/unit/test_doc2md_omml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Regression tests for standalone OMML conversion."""

import importlib.util
import sys
import types
from pathlib import Path

import pytest

etree = pytest.importorskip("lxml.etree")
pytest.importorskip("pylatexenc")

MATH_DIR = Path(__file__).resolve().parents[2] / "paddleocr" / "_doc2md" / "math"
OMML_NS = "http://schemas.openxmlformats.org/officeDocument/2006/math"


def _load_omml():
"""Load the converter without importing the full PaddleOCR model stack."""
package = types.ModuleType("test_doc2md_math")
package.__path__ = [str(MATH_DIR)]
sys.modules[package.__name__] = package
spec = importlib.util.spec_from_file_location(
"test_doc2md_math.omml", MATH_DIR / "omml.py"
)
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
return module


@pytest.mark.parametrize(
("tag", "expected"),
[
("acc", r"\hat{x}"),
("bar", r"\overline{x}"),
("d", r"\left(x\right)"),
("groupChr", r"\underbrace{x}"),
("func", "x"),
],
)
def test_optional_property_nodes_do_not_drop_formula(tag, expected):
omml = _load_omml()
xml = (
f'<m:oMath xmlns:m="{OMML_NS}"><m:{tag}>'
"<m:e><m:r><m:t>x</m:t></m:r></m:e>"
f"</m:{tag}></m:oMath>"
)
assert str(omml.oMath2Latex(etree.fromstring(xml))) == expected


def test_explicit_bar_position_is_preserved():
omml = _load_omml()
xml = (
f'<m:oMath xmlns:m="{OMML_NS}"><m:bar>'
'<m:barPr><m:pos m:val="bot"/></m:barPr>'
"<m:e><m:r><m:t>x</m:t></m:r></m:e>"
"</m:bar></m:oMath>"
)
assert str(omml.oMath2Latex(etree.fromstring(xml))) == r"\underline{x}"


@pytest.mark.parametrize(
("base", "expected"),
[
("min", r"\min_{n}"),
("inf", r"\inf_{n}"),
("sup", r"\sup_{n}"),
("gcd", r"\gcd_{n}"),
("foo", r"\underset{n}{foo}"),
],
)
def test_limit_below_preserves_unsupported_base(base, expected):
omml = _load_omml()
xml = (
f'<m:oMath xmlns:m="{OMML_NS}"><m:limLow>'
f"<m:e><m:r><m:t>{base}</m:t></m:r></m:e>"
"<m:lim><m:r><m:t>n</m:t></m:r></m:lim>"
"</m:limLow></m:oMath>"
)
assert str(omml.oMath2Latex(etree.fromstring(xml))) == expected
64 changes: 64 additions & 0 deletions tests/unit/test_doc2md_omml_delimiter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Regression tests for delimiters with multiple OMML expressions."""

import importlib.util
import sys
import types
from pathlib import Path

import pytest

etree = pytest.importorskip("lxml.etree")
pytest.importorskip("pylatexenc")

MATH_DIR = Path(__file__).resolve().parents[2] / "paddleocr" / "_doc2md" / "math"
OMML_NS = "http://schemas.openxmlformats.org/officeDocument/2006/math"


def _load_omml():
package = types.ModuleType("test_doc2md_delimiter_math")
package.__path__ = [str(MATH_DIR)]
sys.modules[package.__name__] = package
spec = importlib.util.spec_from_file_location(
"test_doc2md_delimiter_math.omml", MATH_DIR / "omml.py"
)
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
return module


@pytest.mark.parametrize(
("values", "separator", "expected"),
[
(["n", "k"], "|", r"\left(n\middle|k\right)"),
(["a", "b", "c"], ";", r"\left(a;b;c\right)"),
(["n", "k"], None, r"\left(n,k\right)"),
(["x"], "|", r"\left(x\right)"),
],
)
def test_delimiter_keeps_all_expressions_and_separator(values, separator, expected):
omml = _load_omml()
prop = (
f'<m:dPr><m:sepChr m:val="{separator}"/></m:dPr>'
if separator is not None
else "<m:dPr/>"
)
expressions = "".join(
f"<m:e><m:r><m:t>{value}</m:t></m:r></m:e>" for value in values
)
xml = f'<m:oMath xmlns:m="{OMML_NS}"><m:d>{prop}{expressions}</m:d></m:oMath>'
assert str(omml.oMath2Latex(etree.fromstring(xml))) == expected