Skip to content

feat(tui): preview inline math in TUI - #107

Open
ompugao wants to merge 6 commits into
masterfrom
feat/tui_preview_inline_math
Open

feat(tui): preview inline math in TUI#107
ompugao wants to merge 6 commits into
masterfrom
feat/tui_preview_inline_math

Conversation

@ompugao

@ompugao ompugao commented May 22, 2026

Copy link
Copy Markdown
Owner

Summary

  • render inline math in patto-preview-tui via Typst-backed images
  • wrap mixed text and inline math correctly while keeping inline math atomic
  • cache inline math dimensions for stable scrolling/layout and preserve text fallback when graphics are unavailable

Notes

  • included a small follow-up fix to reuse the shared inline math cache-key helper and trim a couple of local warnings

ompugao and others added 6 commits May 22, 2026 15:42
- Add DocElement::InlineMathLine with InlineSegment (Text/Math) to
  tui_renderer.rs; render_ast gains an inline_math: bool parameter
- render_latex_inline uses Typst text/inline style ($formula$, no spaces)
  at 14pt so fractions are compact but readable, like LaTeX \textstyle
- image_cache: load_inline_math computes natural row height
  (ceil(img.height / cell_h)) instead of hardcoding 1; stores column
  width in new elem_widths field
- wrap: InlineMathLine height = max math-segment height from elem_heights
- ui: InlineMathLine render arm lays out segments with x_cursor;
  text is vertically centred (row_y + elem_h/2), math images anchor to top
- Fix column calculation to use UnicodeWidthStr::width() instead of
  chars().count() so CJK/Japanese text does not cause overlap
- Add inline_math_rendering: bool config flag to TuiConfig (default true);
  when false, inline math falls back to plain magenta text

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…r clipping

Denominator descenders in fractions were trimmed at 2pt bottom margin.
Increase to 4pt (matching block math) so the full fraction is visible.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Text segments after inline math were treated as atomic units — if the
remaining text didn't fit in the space after a math image, the whole
block jumped to the next row, leaving visible gaps and making lines
appear to break "right after the inline math".

Fix: iterate text character-by-character in both the render arm (ui.rs)
and the height calculator (wrap.rs/inline_math_line_wrap_height).  When
a character would overflow the right edge the accumulated text is flushed
and a new row begins, exactly like regular TextLine wrapping.  Math
segments remain atomic — if an image doesn't fit it is moved to the next
row as a whole.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Five surgical refactors with no behavioral changes:

1. Extract draw_inline_math_line() from draw_content's 155-line match arm
2. Consolidate three pre-load viewport-scan loops into one pass (~50 lines removed)
3. Unify math_render.rs: MathStyle enum replaces four duplicated render functions
4. RenderConfig struct replaces (syntax_theme, inline_math) threaded through 17 call sites
5. ElemSizeCache struct replaces (elem_heights, elem_widths) HashMap pair everywhere

Net result: -85 lines, cleaner APIs, easier to extend.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant