@@ -122,7 +122,6 @@ func (r *PDF) textRunRegions(m math32.Matrix2, run *shapedgt.Run, ln *shaped.Lin
122122func (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 )
0 commit comments