Skip to content

Commit

Permalink
Merge pull request #20 from go-text/fix/fontapicompile
Browse files Browse the repository at this point in the history
Update to refactored API types
  • Loading branch information
andydotxyz authored Sep 28, 2024
2 parents dd62631 + d545d5d commit b5b5c25
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 31 deletions.
9 changes: 4 additions & 5 deletions bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ import (
_ "image/jpeg" // load image formats for users of the API
_ "image/png"

"github.com/go-text/typesetting/font"
"github.com/go-text/typesetting/shaping"
scale "golang.org/x/image/draw"
_ "golang.org/x/image/tiff" // load image formats for users of the API

"github.com/go-text/typesetting/opentype/api"
)

func (r *Renderer) drawBitmap(g shaping.Glyph, bitmap api.GlyphBitmap, img draw.Image, x, y float32) error {
func (r *Renderer) drawBitmap(g shaping.Glyph, bitmap font.GlyphBitmap, img draw.Image, x, y float32) error {
// scaled glyph rect content
top := y - fixed266ToFloat(g.YBearing)*r.PixScale
bottom := top - fixed266ToFloat(g.Height)*r.PixScale
right := x + fixed266ToFloat(g.Width)*r.PixScale
switch bitmap.Format {
case api.BlackAndWhite:
case font.BlackAndWhite:
rec := image.Rect(0, 0, bitmap.Width, bitmap.Height)
sub := image.NewPaletted(rec, color.Palette{color.Transparent, r.Color})

Expand All @@ -31,7 +30,7 @@ func (r *Renderer) drawBitmap(g shaping.Glyph, bitmap api.GlyphBitmap, img draw.

rect := image.Rect(int(x), int(top), int(right), int(bottom))
scale.NearestNeighbor.Scale(img, rect, sub, sub.Bounds(), draw.Over, nil)
case api.JPG, api.PNG, api.TIFF:
case font.JPG, font.PNG, font.TIFF:
pix, _, err := image.Decode(bytes.NewReader(bitmap.Data))
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/go-text/render
go 1.17

require (
github.com/go-text/typesetting v0.1.0
github.com/go-text/typesetting-utils v0.0.0-20240329101916-eee87fb235a3
github.com/go-text/typesetting v0.2.0
github.com/go-text/typesetting-utils v0.0.0-20240317173224-1986cbe96c66
github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c
github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef
golang.org/x/image v0.3.0
Expand Down
9 changes: 4 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
github.com/go-text/typesetting v0.1.0 h1:vioSaLPYcHwPEPLT7gsjCGDCoYSbljxoHJzMnKwVvHw=
github.com/go-text/typesetting v0.1.0/go.mod h1:d22AnmeKq/on0HNv73UFriMKc4Ez6EqZAofLhAzpSzI=
github.com/go-text/typesetting-utils v0.0.0-20231211103740-d9332ae51f04/go.mod h1:DDxDdQEnB70R8owOx3LVpEFvpMK9eeH1o2r0yZhFI9o=
github.com/go-text/typesetting-utils v0.0.0-20240329101916-eee87fb235a3 h1:levTnuLLUmpavLGbJYLJA7fQnKeS7P1eCdAlM+vReXk=
github.com/go-text/typesetting-utils v0.0.0-20240329101916-eee87fb235a3/go.mod h1:DDxDdQEnB70R8owOx3LVpEFvpMK9eeH1o2r0yZhFI9o=
github.com/go-text/typesetting v0.2.0 h1:fbzsgbmk04KiWtE+c3ZD4W2nmCRzBqrqQOvYlwAOdho=
github.com/go-text/typesetting v0.2.0/go.mod h1:2+owI/sxa73XA581LAzVuEBZ3WEEV2pXeDswCH/3i1I=
github.com/go-text/typesetting-utils v0.0.0-20240317173224-1986cbe96c66 h1:GUrm65PQPlhFSKjLPGOZNPNxLCybjzjYBzjfoBGaDUY=
github.com/go-text/typesetting-utils v0.0.0-20240317173224-1986cbe96c66/go.mod h1:DDxDdQEnB70R8owOx3LVpEFvpMK9eeH1o2r0yZhFI9o=
github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c h1:km8GpoQut05eY3GiYWEedbTT0qnSxrCjsVbb7yKY1KE=
github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c/go.mod h1:cNQ3dwVJtS5Hmnjxy6AgTPd0Inb3pW05ftPSX7NZO7Q=
github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780/go.mod h1:mvWM0+15UqyrFKqdRjY6LuAVJR0HOVhJlEgZ5JWtSWU=
Expand Down
28 changes: 14 additions & 14 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"math"

"github.com/go-text/typesetting/font"
"github.com/go-text/typesetting/opentype/api"
"github.com/go-text/typesetting/font/opentype"
"github.com/go-text/typesetting/shaping"
"github.com/srwiley/rasterx"
"golang.org/x/image/math/fixed"
Expand All @@ -32,7 +32,7 @@ type Renderer struct {
fillerScale float32
}

func (r *Renderer) shape(str string, face font.Face) (_ shaping.Line, ascent int) {
func (r *Renderer) shape(str string, face *font.Face) (_ shaping.Line, ascent int) {
text := []rune(str)
in := shaping.Input{
Text: text,
Expand All @@ -58,7 +58,7 @@ func (r *Renderer) shape(str string, face font.Face) (_ shaping.Line, ascent int
// The text will be drawn starting at the left edge, down from the image top by the
// font ascent value, so that the text is all visible.
// The return value is the X pixel position of the end of the drawn string.
func (r *Renderer) DrawString(str string, img draw.Image, face font.Face) int {
func (r *Renderer) DrawString(str string, img draw.Image, face *font.Face) int {
line, ascent := r.shape(str, face)
x := 0
for _, run := range line {
Expand All @@ -71,7 +71,7 @@ func (r *Renderer) DrawString(str string, img draw.Image, face font.Face) int {
// The text will be drawn starting at the x, y pixel position.
// Note that x and y 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) DrawStringAt(str string, img draw.Image, x, y int, face font.Face) int {
func (r *Renderer) DrawStringAt(str string, img draw.Image, x, y int, face *font.Face) int {
line, _ := r.shape(str, face)
for _, run := range line {
x = r.DrawShapedRunAt(run, img, x, y)
Expand Down Expand Up @@ -102,11 +102,11 @@ func (r *Renderer) DrawShapedRunAt(run shaping.Output, img draw.Image, startX, s
yPos := y - fixed266ToFloat(g.YOffset)*r.PixScale
data := run.Face.GlyphData(g.GlyphID)
switch format := data.(type) {
case api.GlyphOutline:
case font.GlyphOutline:
r.drawOutline(g, format, f, scale, xPos, yPos)
case api.GlyphBitmap:
case font.GlyphBitmap:
_ = r.drawBitmap(g, format, img, xPos, yPos)
case api.GlyphSVG:
case font.GlyphSVG:
_ = r.drawSVG(g, format, img, xPos, yPos)
}

Expand All @@ -117,17 +117,17 @@ func (r *Renderer) DrawShapedRunAt(run shaping.Output, img draw.Image, startX, s
return int(math.Ceil(float64(x)))
}

func (r *Renderer) drawOutline(g shaping.Glyph, bitmap api.GlyphOutline, f *rasterx.Filler, scale float32, x, y float32) {
func (r *Renderer) drawOutline(g shaping.Glyph, bitmap font.GlyphOutline, f *rasterx.Filler, scale float32, x, y float32) {
for _, s := range bitmap.Segments {
switch s.Op {
case api.SegmentOpMoveTo:
case opentype.SegmentOpMoveTo:
f.Start(fixed.Point26_6{X: floatToFixed266(s.Args[0].X*scale + x), Y: floatToFixed266(-s.Args[0].Y*scale + y)})
case api.SegmentOpLineTo:
case opentype.SegmentOpLineTo:
f.Line(fixed.Point26_6{X: floatToFixed266(s.Args[0].X*scale + x), Y: floatToFixed266(-s.Args[0].Y*scale + y)})
case api.SegmentOpQuadTo:
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)})
case api.SegmentOpCubeTo:
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)})
Expand All @@ -145,7 +145,7 @@ func floatToFixed266(f float32) fixed.Int26_6 {
}

type singleFontMap struct {
face font.Face
face *font.Face
}

func (sf singleFontMap) ResolveFace(rune) font.Face { return sf.face }
func (sf singleFontMap) ResolveFace(rune) *font.Face { return sf.face }
6 changes: 3 additions & 3 deletions render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func Test_Render(t *testing.T) {
Size: fixed.I(int(r.FontSize)),
}
seg := shaping.Segmenter{}
runs := seg.Split(in, fixedFontmap([]font.Face{f1, f2, f3}))
runs := seg.Split(in, fixedFontmap([]*font.Face{f1, f2, f3}))

line := make(shaping.Line, len(runs))
for i, run := range runs {
Expand Down Expand Up @@ -133,10 +133,10 @@ func TestRenderHindi(t *testing.T) {
w.Close()
}

type fixedFontmap []font.Face
type fixedFontmap []*font.Face

// ResolveFace panics if the slice is empty
func (ff fixedFontmap) ResolveFace(r rune) font.Face {
func (ff fixedFontmap) ResolveFace(r rune) *font.Face {
for _, f := range ff {
if _, has := f.NominalGlyph(r); has {
return f
Expand Down
4 changes: 2 additions & 2 deletions svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"image/draw"
"io"

"github.com/go-text/typesetting/opentype/api"
"github.com/go-text/typesetting/font"
"github.com/go-text/typesetting/shaping"
"github.com/srwiley/oksvg"
"github.com/srwiley/rasterx"
)

func (r *Renderer) drawSVG(g shaping.Glyph, svg api.GlyphSVG, img draw.Image, x, y float32) error {
func (r *Renderer) drawSVG(g shaping.Glyph, svg font.GlyphSVG, img draw.Image, x, y float32) error {
pixWidth := int(fixed266ToFloat(g.Width) * r.PixScale)
pixHeight := int(fixed266ToFloat(-g.Height) * r.PixScale)
pix, err := renderSVGStream(bytes.NewReader(svg.Source), pixWidth, pixHeight)
Expand Down

0 comments on commit b5b5c25

Please sign in to comment.