Repository navigation
pdf: render math via the editor's Unicode approximation - #67
Merged
Merged
Conversation
$`$…$`$ and $$`…$$` spans in PDF export now use math.rs's Unicode approximator through a new pub(crate) pieces() API that also marks the constructs it cannot read. Converted math prints italic in a DejaVu Sans subset (the Noto faces lack operators, arrows and super/subscripts), $$ blocks center on their own line, and failed or unsupported atoms fall back to their LaTeX source in mono so they read as source rather than broken glyphs. Money guards stay literal. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Contributor
Author
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PDF export ignored math —
$…$/$$…$$printed as raw LaTeX with the$stripped (delimiters are concealed bymd::Analysis, so the spans vanished but the source leaked through as plain text). This PR renders math in the PDF with the same Unicode approximation the editor already uses.How:
src/math.rsgains apub(crate) pieces()API — the same walker assubs(), refactored into a sharedscan(src, failed)that additionally reports every construct it could not read (unknown commands, scripts with no glyphs, malformed\fracargs) asPiece { range, text: None }. No conversion tables were duplicated;subs()/approx()are unchanged for the editor.src/pdf.rsnow handlesAnalysis.mathsinspecial()/special_start()— the same arm that already covers images, wiki-links and footnotes:$…$→ converted runs in an italic DejaVu face (math_style()).$$…$$withblock: true→ centeredParagraphon its own line (flow.element(…).aligned(Center)).Piece { text: None }→ the LaTeX source in mono (mono_style()) so\foo{n}ore^{i\pi}reads as source, not broken glyphs.R$ 10 e $ 20still parses as plain prose (math_spansbehavior unchanged).Font coverage: every bundled Noto face misses ~half of the 416 characters
math.rscan emit (all arrows, ∑ ≤ ∞ ∫, phonetic super/subscripts, double-struck). The exporter now embeds a pyftsubset-built DejaVu Sans + DejaVu Sans-Oblique subset covering ASCII, Latin-1/Ext-A, Greek, Cyrillic, General Punctuation and the whole math charset — +286 KB in the binary (DejaVuSans-math-subset.ttf149 KB +DejaVuSans-Oblique-math-subset.ttf137 KB), same ~286 KB per exported PDF since genpdf embeds the fonts it uses. License included (assets/fonts/DejaVu-LICENSE.txt, Bitstream Vera). Four emittable chars DejaVu lacks (⌢ ⌣ ⨆ ⨿) are listed inMATH_TOFU— a run containing them falls back to LaTeX source instead of showing.notdefboxes.Tests:
math_converts_to_unicode(inline + display through the realEmitter/inlinepath),unconvertible_math_shows_its_source,money_amounts_stay_literal,math_glyphs_reach_the_pdf(asserts the converted codepoints appear in the embedded font's ToUnicode CMap — i.e. real glyphs were drawn, not tofu).Verified in the app: exported a note via the real "Export as PDF…" flow and opened the PDF in Preview — all glyphs render (α² ≤ ∞, xᵢ → ∑ᵢ, ∫₀¹, √(c), →), no tofu. PDFKit text extraction confirms:
The PDF (Preview):

Same note in the editor for comparison:

Link to Devin session: https://app.devin.ai/sessions/a075cd854db947cdb802a60f4e413e85
Open in Devin Desktop: https://app.devin.ai/desktop/session/a075cd854db947cdb802a60f4e413e85?variant=devin
Requested by: @horizzon3507