Skip to content

Commit

Permalink
Add utftex LaTeX renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
joouha committed Apr 23, 2024
1 parent 2d209b4 commit 42ec6f3
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions euporie/core/convert/formats/ansi.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,26 +169,26 @@ def get_data(self) -> str:
@register(
from_="latex",
to="ansi",
filter_=have_modules("flatlatex.latexfuntypes"),
filter_=command_exists("utftex"),
weight=0,
)
async def latex_to_ansi_py_flatlatex(
async def latex_to_ansi_utftex(
datum: Datum,
cols: int | None = None,
rows: int | None = None,
fg: str | None = None,
bg: str | None = None,
extend: bool = True,
) -> str:
"""Convert LaTeX to ANSI using :py:mod:`flatlatex`."""
import flatlatex

return flatlatex.converter().convert(datum.data.strip().strip("$").strip())
"""Render LaTeX maths as unicode."""
return (await call_subproc(datum.data, ["utftex"])).decode()


@register(
from_="latex",
to="ansi",
filter_=have_modules("pylatexenc"),
weight=0,
)
async def latex_to_ansi_py_pylatexenc(
datum: Datum,
Expand All @@ -204,6 +204,36 @@ async def latex_to_ansi_py_pylatexenc(
return LatexNodes2Text().latex_to_text(datum.data.strip().strip("$").strip())


@register(
from_="latex",
to="ansi",
filter_=have_modules("flatlatex.latexfuntypes"),
weight=0,
)
async def latex_to_ansi_py_flatlatex(
datum: Datum,
cols: int | None = None,
rows: int | None = None,
fg: str | None = None,
bg: str | None = None,
extend: bool = True,
) -> str:
"""Convert LaTeX to ANSI using :py:mod:`flatlatex`."""
import flatlatex
from flatlatex.latexfuntypes import latexfun

converter = flatlatex.converter()
for style in (
r"\textstyle",
r"\displaystyle",
r"\scriptstyle",
r"\scriptscriptstyle",
):
converter._converter__cmds[style] = latexfun(lambda x: "", 0)

return converter.convert(datum.data.strip().strip("$").strip())


@register(
from_="latex",
to="ansi",
Expand Down Expand Up @@ -261,7 +291,7 @@ async def pil_to_ansi_py_timg(
assert rows is not None
assert cols is not None

# `timg` assumes a 2x1 terminal cell aspect ratio, so we correct for while
# `timg` assumes a 2x1 terminal cell aspect ratio, so we correct for this while
# resizing the image
data = data.resize((cols, ceil(rows * 2 * (px / py) / 0.5)))

Expand Down

0 comments on commit 42ec6f3

Please sign in to comment.