Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (r *Renderer) drawBitmap(g shaping.Glyph, bitmap font.GlyphBitmap, img draw
}

if bitmap.Outline != nil {
r.drawOutline(g, *bitmap.Outline, r.filler, r.fillerScale, x, y)
r.drawOutline(*bitmap.Outline, r.fillerScale, x, y)
}
return nil
}
Expand Down
41 changes: 18 additions & 23 deletions render.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package render

import (
"image"
"image/color"
"image/draw"
"math"
Expand All @@ -9,8 +10,8 @@ import (
"github.com/go-text/typesetting/font"
"github.com/go-text/typesetting/font/opentype"
"github.com/go-text/typesetting/shaping"
"github.com/srwiley/rasterx"
"golang.org/x/image/math/fixed"
"golang.org/x/image/vector"
)

// Renderer defines a type that can render strings to a bitmap canvas.
Expand All @@ -30,7 +31,7 @@ type Renderer struct {
segmenter shaping.Segmenter
shaper shaping.HarfbuzzShaper
wrapper shaping.LineWrapper
filler *rasterx.Filler
rasterizer *vector.Rasterizer
fillerScale float32
}

Expand Down Expand Up @@ -96,17 +97,16 @@ func (r *Renderer) DrawStringAt(str string, img draw.Image, x, y int, face *font
// Note that startX and startY are not multiplied by the `PixScale` value as they refer to output coordinates.
// The return value is the X pixel position of the end of the drawn string.
func (r *Renderer) DrawShapedRunAt(run shaping.Output, img draw.Image, startX, startY int) int {
if r.rasterizer == nil {
r.rasterizer = &vector.Rasterizer{}
}
r.rasterizer.Reset(img.Bounds().Dx(), img.Bounds().Dy())
if r.PixScale == 0 {
r.PixScale = 1
}
scale := r.FontSize * r.PixScale / float32(run.Face.Upem())
r.fillerScale = scale

b := img.Bounds()
scanner := rasterx.NewScannerGV(b.Dx(), b.Dy(), img, b)
f := rasterx.NewFiller(b.Dx(), b.Dy(), scanner)
r.filler = f
f.SetColor(r.Color)
x := float32(startX)
y := float32(startY)
for _, g := range run.Glyphs {
Expand All @@ -115,7 +115,7 @@ func (r *Renderer) DrawShapedRunAt(run shaping.Output, img draw.Image, startX, s
data := run.Face.GlyphData(g.GlyphID)
switch format := data.(type) {
case font.GlyphOutline:
r.drawOutline(g, format, f, scale, xPos, yPos)
r.drawOutline(format, scale, xPos, yPos)
case font.GlyphBitmap:
_ = r.drawBitmap(g, format, img, xPos, yPos)
case font.GlyphSVG:
Expand All @@ -124,38 +124,33 @@ func (r *Renderer) DrawShapedRunAt(run shaping.Output, img draw.Image, startX, s

x += fixed266ToFloat(g.Advance) * r.PixScale
}
f.Draw()
r.filler = nil
r.rasterizer.Draw(img, img.Bounds(), image.NewUniform(r.Color), image.Point{})
return int(math.Ceil(float64(x)))
}

func (r *Renderer) drawOutline(g shaping.Glyph, bitmap font.GlyphOutline, f *rasterx.Filler, scale float32, x, y float32) {
func (r *Renderer) drawOutline(bitmap font.GlyphOutline, scale float32, x, y float32) {
raster := r.rasterizer
for _, s := range bitmap.Segments {
switch s.Op {
case opentype.SegmentOpMoveTo:
f.Start(fixed.Point26_6{X: floatToFixed266(s.Args[0].X*scale + x), Y: floatToFixed266(-s.Args[0].Y*scale + y)})
raster.MoveTo(s.Args[0].X*scale+x, -s.Args[0].Y*scale+y)
case opentype.SegmentOpLineTo:
f.Line(fixed.Point26_6{X: floatToFixed266(s.Args[0].X*scale + x), Y: floatToFixed266(-s.Args[0].Y*scale + y)})
raster.LineTo(s.Args[0].X*scale+x, -s.Args[0].Y*scale+y)
case opentype.SegmentOpQuadTo:
f.QuadBezier(fixed.Point26_6{X: floatToFixed266(s.Args[0].X*scale + x), Y: floatToFixed266(-s.Args[0].Y*scale + y)},
fixed.Point26_6{X: floatToFixed266(s.Args[1].X*scale + x), Y: floatToFixed266(-s.Args[1].Y*scale + y)})
raster.QuadTo(s.Args[0].X*scale+x, -s.Args[0].Y*scale+y, s.Args[1].X*scale+x, -s.Args[1].Y*scale+y)
case opentype.SegmentOpCubeTo:
f.CubeBezier(fixed.Point26_6{X: floatToFixed266(s.Args[0].X*scale + x), Y: floatToFixed266(-s.Args[0].Y*scale + y)},
fixed.Point26_6{X: floatToFixed266(s.Args[1].X*scale + x), Y: floatToFixed266(-s.Args[1].Y*scale + y)},
fixed.Point26_6{X: floatToFixed266(s.Args[2].X*scale + x), Y: floatToFixed266(-s.Args[2].Y*scale + y)})
raster.CubeTo(s.Args[0].X*scale+x, -s.Args[0].Y*scale+y,
s.Args[1].X*scale+x, -s.Args[1].Y*scale+y,
s.Args[2].X*scale+x, -s.Args[2].Y*scale+y)
}
}
f.Stop(true)
raster.ClosePath()
}

func fixed266ToFloat(i fixed.Int26_6) float32 {
return float32(float64(i) / 64)
}

func floatToFixed266(f float32) fixed.Int26_6 {
return fixed.Int26_6(int(float64(f) * 64))
}

type singleFontMap struct {
face *font.Face
}
Expand Down
Binary file modified testdata/mixed_ltr_rtl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified testdata/out.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified testdata/out_hindi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading