| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- """imgui_microtex: native LaTeX math rendering via MicroTeX + FreeType."""
- from typing import Optional, overload
- import numpy as np
- from imgui_bundle.imgui import ImVec4
- from imgui_bundle import hello_imgui as HelloImGui
- import enum
- ImU32 = int
- ImTextureID = int
- # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! AUTOGENERATED CODE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- # <litgen_stub> // Autogenerated code below! Do not edit!
- #################### <generated_from:imgui_microtex.h> ####################
- # Public API for imgui_microtex: native LaTeX math rendering via MicroTeX + FreeType.
- #
- # Level 1: render LaTeX to an RGBA pixel buffer.
- # Level 2: render LaTeX to an owning HelloImGui::TextureGpuPtr (cached).
- #
- # Thread safety: all functions are protected by a mutex and can be called from any thread.
- class TexStyle(enum.IntEnum):
- """ ============================================================================
- TeX style
- ============================================================================
- Selects the layout style used when rendering a formula. This maps directly
- to MicroTeX's TexStyle and corresponds to the four TeX styles defined by
- Knuth (D, T, S, SS). Pick Display for centered "display math" ($$...$$)
- and Text for inline math ($...$).
- The style affects symbol size, big-operator appearance, and the spacing
- around \frac (numerator shift-up and denominator shift-down): Display gives
- generous spacing; Text is compact.
- """
- # Largest size. Big operators (\sum, \int, ...) use their large variants
- # with limits placed above and below. \frac uses generous vertical
- # spacing. This is what LaTeX uses inside $$...$$ and \[...\].
- display = enum.auto() # (= 0)
- # Default inline size. Big operators use their small variants with
- # limits attached as sub/superscripts. \frac uses compact spacing.
- # This is what LaTeX uses inside $...$ and \(...\).
- text = enum.auto() # (= 1)
- # Smaller size used by LaTeX inside sub/superscripts. Rarely useful at
- # the top level; MicroTeX switches to it automatically where needed.
- script = enum.auto() # (= 2)
- # Smallest size, used inside scripts-of-scripts. Same caveat as Script.
- script_script = enum.auto() # (= 3)
- # ============================================================================
- # Initialization / shutdown
- # ============================================================================
- def init(clm_file: str, font_file: str) -> None:
- """ Initialize MicroTeX + FreeType backend.
- clmFile: path to the .clm1 font metrics file
- fontFile: path to the .otf font file
- Safe to call repeatedly: subsequent calls after the first successful
- Init() no-op (MicroTeX itself stays initialized for process life; the
- underlying MicroTeX::init()/release() pair is not re-entrant, so we
- defer the real teardown to std::atexit: see imgui_microtex.cpp).
- """
- pass
- def is_initialized() -> bool:
- """ Check if initialized."""
- pass
- def release() -> None:
- """ Drop the cached GPU texture set so the GL context can be torn down
- cleanly. Call from BeforeExit (or any point where the GL context is
- about to die). Safe to call multiple times, and safe to call Init()
- again afterwards: the underlying MicroTeX library stays alive for
- the whole process and its real teardown runs once at exit via a
- std::atexit handler installed on first Init().
- """
- pass
- # ============================================================================
- # Level 1: LaTeX -> RGBA pixel buffer
- # ============================================================================
- class RenderedFormula:
- width: int = 0
- height: int = 0
- depth: int = 0 # distance below baseline (in pixels, unpadded)
- # BaselineY: pixel y-offset from the TOP of the (padded) image to
- # the formula's typographic baseline. Use this to align the formula
- # with surrounding text:
- #
- # // ImGui text baseline is at cursor.y + GetFontBaked()->Ascent.
- # float ascent = ImGui::GetFontBaked()->Ascent;
- # float imageTop = ImGui::GetCursorPosY() + ascent - formula.BaselineY;
- # ImGui::SetCursorPosY(imageTop);
- # ImGui::Image(texId, ImVec2(formula.Width, formula.Height));
- #
- baseline_y: int = 0
- def __init__(
- self,
- width: int = 0,
- height: int = 0,
- depth: int = 0,
- baseline_y: int = 0
- ) -> None:
- """Auto-generated default constructor with named params"""
- pass
- def pixels_as_array(self) -> np.ndarray:
- """Return pixels as a numpy array with shape (height, width, 4) in RGBA format.
- The returned array is a view into the internal buffer (no copy).
- """
- ...
- # Render a LaTeX string to an RGBA pixel buffer.
- # latex: the LaTeX math string (without $ delimiters)
- # fontSize: font size in pixels
- # color: foreground color (alpha channel is used)
- # style: TeX layout style (Display for $$...$$, Text for $...$)
- @overload
- def render(
- latex: str,
- font_size: float,
- color: Optional[ImU32] = None,
- style: TexStyle = TexStyle.text
- ) -> RenderedFormula:
- """Python bindings defaults:
- If color is None, then its default value will be: IM_COL32_BLACK
- """
- pass
- @overload
- def render(
- latex: str,
- font_size: float,
- color: ImVec4,
- style: TexStyle = TexStyle.text
- ) -> RenderedFormula:
- pass
- # ============================================================================
- # Level 2: LaTeX -> HelloImGui::TextureGpuPtr (with caching)
- # ============================================================================
- class FormulaTexture:
- """ FormulaTexture owns its GPU texture via a HelloImGui::TextureGpuPtr.
- The texture is freed when the last shared reference drops; this happens
- at the latest when the imgui_microtex texture cache is cleared (via
- ClearTextureCache() or Release()), but a caller may also keep its own
- reference to extend the lifetime.
- """
- texture: HelloImGui.TextureGpu
- width: int = 0
- height: int = 0
- depth: int = 0
- # BaselineY: pixel y-offset from the TOP of the image to the formula's
- # typographic baseline. See RenderedFormula::BaselineY for details.
- baseline_y: int = 0
- # LastUsedFrame: ImGui::GetFrameCount() at the most recent cache hit
- # or insertion. Used by the optional frame-generation eviction (see
- # SetEvictionFrames). Not interesting to direct API consumers.
- last_used_frame: int = 0
- def texture_id(self) -> ImTextureID:
- """ Convenience: returns the GPU texture id, or 0 if no texture is held."""
- pass
- def __init__(
- self,
- texture: Optional[HelloImGui.TextureGpu] = None,
- width: int = 0,
- height: int = 0,
- depth: int = 0,
- baseline_y: int = 0,
- last_used_frame: int = 0
- ) -> None:
- """Auto-generated default constructor with named params
- Python bindings defaults:
- If Texture is None, then its default value will be: HelloImGui.TextureGpu()
- """
- pass
- # Render a LaTeX string to an ImGui texture (cached for the lifetime of imgui_microtex).
- # style: TeX layout style (Display for $$...$$, Text for $...$).
- @overload
- def render_to_texture(
- latex: str,
- font_size: float,
- color: Optional[ImU32] = None,
- style: TexStyle = TexStyle.text
- ) -> FormulaTexture:
- """Python bindings defaults:
- If color is None, then its default value will be: IM_COL32_BLACK
- """
- pass
- @overload
- def render_to_texture(
- latex: str,
- font_size: float,
- color: ImVec4,
- style: TexStyle = TexStyle.text
- ) -> FormulaTexture:
- pass
- def to_texture(formula: RenderedFormula) -> FormulaTexture:
- """ Convert a previously rendered formula to an ImGui texture (not cached)."""
- pass
- def clear_texture_cache() -> None:
- """ Clear the texture cache."""
- pass
- def set_eviction_frames(n: int) -> None:
- """ ============================================================================
- Frame-generation eviction for the texture cache
- ============================================================================
- imgui_microtex maintains a texture cache keyed by (latex, fontSize, color)
- so that re-rendering the same formula every frame is essentially free.
- To prevent unbounded growth in long-running interactive use cases — LaTeX
- REPLs, multi-document browsers, notebooks where users page through many
- formulas they will never see again — the cache evicts entries that have
- not been touched in the last N frames. Static documentation viewers see
- no functional change: every formula they render is touched every frame,
- so it never falls below the eviction threshold.
- SetEvictionFrames(N) configures the threshold:
- - N > 0: cache entries not touched for N frames are dropped on the
- next cache insertion (lazy: no insert -> no sweep).
- - N == 0: eviction disabled, cache grows for the lifetime of the
- process. Use this for short-lived apps where you do not
- want any eviction overhead.
- The eviction is "lazy on insert" only. If no new formula is ever
- rendered, no sweep runs — call ClearTextureCache() manually for the
- rare case where rendering stops entirely and you want to reclaim
- memory immediately.
- Default: N = 60 (~1 second at 60 FPS).
- """
- pass
- def get_cache_size() -> int:
- """ Returns the current cache size (number of formula entries). Useful for
- diagnostics, monitoring, and tests.
- """
- pass
- #################### </generated_from:imgui_microtex.h> ####################
- # </litgen_stub> // Autogenerated code end
- # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! AUTOGENERATED CODE END !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|