Skip to content

Add Superscript and Subscript font styles#348

Merged
oneplus1000 merged 6 commits into
signintech:masterfrom
s-johri:feat/superscript-subscript
Jul 26, 2026
Merged

Add Superscript and Subscript font styles#348
oneplus1000 merged 6 commits into
signintech:masterfrom
s-johri:feat/superscript-subscript

Conversation

@s-johri

@s-johri s-johri commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

This adds superscript and subscript text support.

pdf.SetFont("font", "", 14)
pdf.Cell(nil, "E = mc")
pdf.SetFontWithStyle("font", gopdf.Superscript, 14)
pdf.Cell(nil, "2")
pdf.SetFontWithStyle("font", gopdf.Regular, 14)
pdf.Cell(nil, ", said Einstein")

How it works

  • Two new style bits, Superscript = 8 and Subscript = 16. Like Underline, they are decoration bits: they reuse the already-loaded font face (the face lookup now strips all decoration bits, not just Underline).
  • The glyph size and baseline shift are derived from the font's own OS/2 metrics (ySuperscriptYSize/YOffset, ySubscriptYSize/YOffset), which ParseOS2 previously skipped over. Fonts with missing or zero metrics fall back to conventional ratios (58.3% size, +33.3%/-14.1% baseline shift).
  • Rendering uses the PDF text-rise operator: the state line becomes /F1 9.1 Tf 0 Tc 6.34 Ts. Ts is emitted on every draw (0 Ts for plain text) because text state persists across BT/ET blocks, so each draw must reset it.
  • The caller's font size keeps governing vertical layout (line height, baseline placement), so script runs share the baseline of surrounding text with no caller arithmetic. Width measurement (MeasureTextWidth, wrapping, justify adjustments) uses the scaled size so measurements match rendering.

Notes for review

  • The string style API ("B"/"I"/"U") is not extended: getConvertedStyle matches single letters via Contains, so "SUP"/"SUB" would falsely match Bold/Underline. The flags are int-style only.
  • Underline rectangles are intentionally not shifted by the rise; that can follow if anyone needs underlined superscripts.
  • TestSuperscriptSubscriptVisualDemo writes test/out/superscript_subscript_demo.pdf for visual checking (chemical formulas, exponents, footnote markers).

@oneplus1000

Copy link
Copy Markdown
Collaborator

Hello again! Thank you for PR. I will check and review it on the weekend.

@oneplus1000
oneplus1000 merged commit b4a2c05 into signintech:master Jul 26, 2026
2 checks passed
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.

2 participants