Skip to content

Commit 2db11cf

Browse files
committed
contmeta: support super and subcript rendering in all renderers
1 parent 7b4a239 commit 2db11cf

5 files changed

Lines changed: 43 additions & 4 deletions

File tree

paint/pdf/pdf_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func TestText(t *testing.T) {
9898
prv := UseStandardFonts()
9999
sh := shaped.NewShaper()
100100

101-
src := "PDF can put <b>HTML</b> <br>formatted Text where you <i>want</i>"
101+
src := "PDF can put <b>HTML</b> <br>formatted Text where you <i>want</i> <br>including<sup>1</sup> and sub<sub>script</sub>"
102102
rsty := &sty.Font
103103
tsty := &sty.Text
104104

paint/pdf/text.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ func (r *PDF) textRunRegions(m math32.Matrix2, run *shapedgt.Run, ln *shaped.Lin
122122
func (r *PDF) textRun(style *styles.Paint, m math32.Matrix2, run *shapedgt.Run, ln *shaped.Line, lns *shaped.Lines, runes []rune, clr image.Image, off math32.Vector2) {
123123
// dir := run.Direction
124124
region := run.Runes()
125-
offTrans := math32.Translate2D(off.X, off.Y)
126125
rbb := run.MaxBounds.Translate(off)
127126
fill := clr
128127
if run.FillColor != nil {
@@ -131,14 +130,24 @@ func (r *PDF) textRun(style *styles.Paint, m math32.Matrix2, run *shapedgt.Run,
131130
fsz := math32.FromFixed(run.Size)
132131
lineW := max(fsz/16, 1) // 1 at 16, bigger if biggerr
133132
if run.Math.Path != nil {
134-
r.w.PushTransform(offTrans)
133+
r.w.PushTransform(math32.Translate2D(off.X, off.Y))
135134
psty := *style
136135
psty.Stroke.Color = run.StrokeColor
137136
psty.Fill.Color = fill
138137
r.Path(*run.Math.Path, &psty, math32.Identity2())
139138
r.w.PopStack()
140139
return
141140
}
141+
sty := run.Font.Style(&style.Text)
142+
// https://en.wikipedia.org/wiki/Subscript_and_superscript: latex does -.14 sub, .25 super
143+
// others often use -.33 and .33
144+
if sty.Special == rich.Super {
145+
off.X += 0.05 * fsz // tends to be cramped otherwise
146+
off.Y -= 0.25 * fsz
147+
} else if sty.Special == rich.Sub {
148+
off.X += 0.05 * fsz // tends to be cramped otherwise
149+
off.Y += 0.14 * fsz
150+
}
142151

143152
idm := math32.Identity2()
144153
if run.Decoration.HasFlag(rich.Underline) || run.Decoration.HasFlag(rich.DottedUnderline) {
@@ -160,7 +169,7 @@ func (r *PDF) textRun(style *styles.Paint, m math32.Matrix2, run *shapedgt.Run,
160169
}
161170
}
162171

163-
r.w.StartTextObject(offTrans)
172+
r.w.StartTextObject(math32.Translate2D(off.X, off.Y))
164173
r.setTextStyle(&run.Font, style, fill, run.StrokeColor, math32.FromFixed(run.Size), lns.LineHeight)
165174
raw := string(runes[region.Start:region.End])
166175
r.w.WriteText(raw)

paint/renderers/htmlcanvas/text.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ func (rs *Renderer) TextRun(ctx *render.Context, run *shapedgt.Run, ln *shaped.L
120120
rs.setTransform(ctx)
121121
return
122122
}
123+
sty := run.Font.Style(&ctx.Style.Text)
124+
// https://en.wikipedia.org/wiki/Subscript_and_superscript: latex does -.14 sub, .25 super
125+
// others often use -.33 and .33
126+
if sty.Special == rich.Super {
127+
off.Y -= 0.25 * fsz
128+
} else if sty.Special == rich.Sub {
129+
off.Y += 0.14 * fsz
130+
}
123131

124132
if run.Decoration.HasFlag(rich.Underline) || run.Decoration.HasFlag(rich.DottedUnderline) {
125133
dash := []float32{2, 2}

paint/renderers/rasterx/text.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ func (rs *Renderer) TextRun(ctx *render.Context, run *shapedgt.Run, ln *shaped.L
131131
return
132132
}
133133

134+
sty := run.Font.Style(&ctx.Style.Text)
135+
// https://en.wikipedia.org/wiki/Subscript_and_superscript: latex does -.14 sub, .25 super
136+
// others often use -.33 and .33
137+
if sty.Special == rich.Super {
138+
off.Y -= 0.25 * fsz
139+
} else if sty.Special == rich.Sub {
140+
off.Y += 0.14 * fsz
141+
}
142+
134143
if run.Decoration.HasFlag(rich.Underline) || run.Decoration.HasFlag(rich.DottedUnderline) {
135144
dash := []float32{2, 2}
136145
if run.Decoration.HasFlag(rich.Underline) {

text/shaped/shaped_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,16 @@ func TestGlyphAlign(t *testing.T) {
415415
pc.DrawText(lns, pos)
416416
})
417417
}
418+
419+
func TestGlyphSuperSub(t *testing.T) {
420+
RunTest(t, "supersub", 300, 300, func(pc *paint.Painter, sh Shaper, tsty *text.Style) {
421+
src := "Super<sup>script</sup> and Sub<sub>script</sub>"
422+
sty := rich.NewStyle()
423+
sty.Size = 0.845
424+
tx, err := htmltext.HTMLToRich([]byte(src), sty, nil)
425+
assert.NoError(t, err)
426+
lns := sh.WrapLines(tx, sty, tsty, math32.Vec2(250, 250))
427+
pos := math32.Vec2(10.3, 10.96875)
428+
pc.DrawText(lns, pos)
429+
})
430+
}

0 commit comments

Comments
 (0)