imgui_microtex.pyi 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. """imgui_microtex: native LaTeX math rendering via MicroTeX + FreeType."""
  2. from typing import Optional, overload
  3. import numpy as np
  4. from imgui_bundle.imgui import ImVec4
  5. from imgui_bundle import hello_imgui as HelloImGui
  6. import enum
  7. ImU32 = int
  8. ImTextureID = int
  9. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! AUTOGENERATED CODE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  10. # <litgen_stub> // Autogenerated code below! Do not edit!
  11. #################### <generated_from:imgui_microtex.h> ####################
  12. # Public API for imgui_microtex: native LaTeX math rendering via MicroTeX + FreeType.
  13. #
  14. # Level 1: render LaTeX to an RGBA pixel buffer.
  15. # Level 2: render LaTeX to an owning HelloImGui::TextureGpuPtr (cached).
  16. #
  17. # Thread safety: all functions are protected by a mutex and can be called from any thread.
  18. class TexStyle(enum.IntEnum):
  19. """ ============================================================================
  20. TeX style
  21. ============================================================================
  22. Selects the layout style used when rendering a formula. This maps directly
  23. to MicroTeX's TexStyle and corresponds to the four TeX styles defined by
  24. Knuth (D, T, S, SS). Pick Display for centered "display math" ($$...$$)
  25. and Text for inline math ($...$).
  26. The style affects symbol size, big-operator appearance, and the spacing
  27. around \frac (numerator shift-up and denominator shift-down): Display gives
  28. generous spacing; Text is compact.
  29. """
  30. # Largest size. Big operators (\sum, \int, ...) use their large variants
  31. # with limits placed above and below. \frac uses generous vertical
  32. # spacing. This is what LaTeX uses inside $$...$$ and \[...\].
  33. display = enum.auto() # (= 0)
  34. # Default inline size. Big operators use their small variants with
  35. # limits attached as sub/superscripts. \frac uses compact spacing.
  36. # This is what LaTeX uses inside $...$ and \(...\).
  37. text = enum.auto() # (= 1)
  38. # Smaller size used by LaTeX inside sub/superscripts. Rarely useful at
  39. # the top level; MicroTeX switches to it automatically where needed.
  40. script = enum.auto() # (= 2)
  41. # Smallest size, used inside scripts-of-scripts. Same caveat as Script.
  42. script_script = enum.auto() # (= 3)
  43. # ============================================================================
  44. # Initialization / shutdown
  45. # ============================================================================
  46. def init(clm_file: str, font_file: str) -> None:
  47. """ Initialize MicroTeX + FreeType backend.
  48. clmFile: path to the .clm1 font metrics file
  49. fontFile: path to the .otf font file
  50. Safe to call repeatedly: subsequent calls after the first successful
  51. Init() no-op (MicroTeX itself stays initialized for process life; the
  52. underlying MicroTeX::init()/release() pair is not re-entrant, so we
  53. defer the real teardown to std::atexit: see imgui_microtex.cpp).
  54. """
  55. pass
  56. def is_initialized() -> bool:
  57. """ Check if initialized."""
  58. pass
  59. def release() -> None:
  60. """ Drop the cached GPU texture set so the GL context can be torn down
  61. cleanly. Call from BeforeExit (or any point where the GL context is
  62. about to die). Safe to call multiple times, and safe to call Init()
  63. again afterwards: the underlying MicroTeX library stays alive for
  64. the whole process and its real teardown runs once at exit via a
  65. std::atexit handler installed on first Init().
  66. """
  67. pass
  68. # ============================================================================
  69. # Level 1: LaTeX -> RGBA pixel buffer
  70. # ============================================================================
  71. class RenderedFormula:
  72. width: int = 0
  73. height: int = 0
  74. depth: int = 0 # distance below baseline (in pixels, unpadded)
  75. # BaselineY: pixel y-offset from the TOP of the (padded) image to
  76. # the formula's typographic baseline. Use this to align the formula
  77. # with surrounding text:
  78. #
  79. # // ImGui text baseline is at cursor.y + GetFontBaked()->Ascent.
  80. # float ascent = ImGui::GetFontBaked()->Ascent;
  81. # float imageTop = ImGui::GetCursorPosY() + ascent - formula.BaselineY;
  82. # ImGui::SetCursorPosY(imageTop);
  83. # ImGui::Image(texId, ImVec2(formula.Width, formula.Height));
  84. #
  85. baseline_y: int = 0
  86. def __init__(
  87. self,
  88. width: int = 0,
  89. height: int = 0,
  90. depth: int = 0,
  91. baseline_y: int = 0
  92. ) -> None:
  93. """Auto-generated default constructor with named params"""
  94. pass
  95. def pixels_as_array(self) -> np.ndarray:
  96. """Return pixels as a numpy array with shape (height, width, 4) in RGBA format.
  97. The returned array is a view into the internal buffer (no copy).
  98. """
  99. ...
  100. # Render a LaTeX string to an RGBA pixel buffer.
  101. # latex: the LaTeX math string (without $ delimiters)
  102. # fontSize: font size in pixels
  103. # color: foreground color (alpha channel is used)
  104. # style: TeX layout style (Display for $$...$$, Text for $...$)
  105. @overload
  106. def render(
  107. latex: str,
  108. font_size: float,
  109. color: Optional[ImU32] = None,
  110. style: TexStyle = TexStyle.text
  111. ) -> RenderedFormula:
  112. """Python bindings defaults:
  113. If color is None, then its default value will be: IM_COL32_BLACK
  114. """
  115. pass
  116. @overload
  117. def render(
  118. latex: str,
  119. font_size: float,
  120. color: ImVec4,
  121. style: TexStyle = TexStyle.text
  122. ) -> RenderedFormula:
  123. pass
  124. # ============================================================================
  125. # Level 2: LaTeX -> HelloImGui::TextureGpuPtr (with caching)
  126. # ============================================================================
  127. class FormulaTexture:
  128. """ FormulaTexture owns its GPU texture via a HelloImGui::TextureGpuPtr.
  129. The texture is freed when the last shared reference drops; this happens
  130. at the latest when the imgui_microtex texture cache is cleared (via
  131. ClearTextureCache() or Release()), but a caller may also keep its own
  132. reference to extend the lifetime.
  133. """
  134. texture: HelloImGui.TextureGpu
  135. width: int = 0
  136. height: int = 0
  137. depth: int = 0
  138. # BaselineY: pixel y-offset from the TOP of the image to the formula's
  139. # typographic baseline. See RenderedFormula::BaselineY for details.
  140. baseline_y: int = 0
  141. # LastUsedFrame: ImGui::GetFrameCount() at the most recent cache hit
  142. # or insertion. Used by the optional frame-generation eviction (see
  143. # SetEvictionFrames). Not interesting to direct API consumers.
  144. last_used_frame: int = 0
  145. def texture_id(self) -> ImTextureID:
  146. """ Convenience: returns the GPU texture id, or 0 if no texture is held."""
  147. pass
  148. def __init__(
  149. self,
  150. texture: Optional[HelloImGui.TextureGpu] = None,
  151. width: int = 0,
  152. height: int = 0,
  153. depth: int = 0,
  154. baseline_y: int = 0,
  155. last_used_frame: int = 0
  156. ) -> None:
  157. """Auto-generated default constructor with named params
  158. Python bindings defaults:
  159. If Texture is None, then its default value will be: HelloImGui.TextureGpu()
  160. """
  161. pass
  162. # Render a LaTeX string to an ImGui texture (cached for the lifetime of imgui_microtex).
  163. # style: TeX layout style (Display for $$...$$, Text for $...$).
  164. @overload
  165. def render_to_texture(
  166. latex: str,
  167. font_size: float,
  168. color: Optional[ImU32] = None,
  169. style: TexStyle = TexStyle.text
  170. ) -> FormulaTexture:
  171. """Python bindings defaults:
  172. If color is None, then its default value will be: IM_COL32_BLACK
  173. """
  174. pass
  175. @overload
  176. def render_to_texture(
  177. latex: str,
  178. font_size: float,
  179. color: ImVec4,
  180. style: TexStyle = TexStyle.text
  181. ) -> FormulaTexture:
  182. pass
  183. def to_texture(formula: RenderedFormula) -> FormulaTexture:
  184. """ Convert a previously rendered formula to an ImGui texture (not cached)."""
  185. pass
  186. def clear_texture_cache() -> None:
  187. """ Clear the texture cache."""
  188. pass
  189. def set_eviction_frames(n: int) -> None:
  190. """ ============================================================================
  191. Frame-generation eviction for the texture cache
  192. ============================================================================
  193. imgui_microtex maintains a texture cache keyed by (latex, fontSize, color)
  194. so that re-rendering the same formula every frame is essentially free.
  195. To prevent unbounded growth in long-running interactive use cases — LaTeX
  196. REPLs, multi-document browsers, notebooks where users page through many
  197. formulas they will never see again — the cache evicts entries that have
  198. not been touched in the last N frames. Static documentation viewers see
  199. no functional change: every formula they render is touched every frame,
  200. so it never falls below the eviction threshold.
  201. SetEvictionFrames(N) configures the threshold:
  202. - N > 0: cache entries not touched for N frames are dropped on the
  203. next cache insertion (lazy: no insert -> no sweep).
  204. - N == 0: eviction disabled, cache grows for the lifetime of the
  205. process. Use this for short-lived apps where you do not
  206. want any eviction overhead.
  207. The eviction is "lazy on insert" only. If no new formula is ever
  208. rendered, no sweep runs — call ClearTextureCache() manually for the
  209. rare case where rendering stops entirely and you want to reclaim
  210. memory immediately.
  211. Default: N = 60 (~1 second at 60 FPS).
  212. """
  213. pass
  214. def get_cache_size() -> int:
  215. """ Returns the current cache size (number of formula entries). Useful for
  216. diagnostics, monitoring, and tests.
  217. """
  218. pass
  219. #################### </generated_from:imgui_microtex.h> ####################
  220. # </litgen_stub> // Autogenerated code end
  221. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! AUTOGENERATED CODE END !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!