Skip to content

Commit

Permalink
text,vg/draw: use vg.Canvas in TextHandler interface
Browse files Browse the repository at this point in the history
  • Loading branch information
sbinet committed Jan 29, 2021
1 parent a02d161 commit 61a5e05
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
23 changes: 20 additions & 3 deletions text/latex.go
Expand Up @@ -6,6 +6,7 @@ package text

import (
"fmt"
"image/color"
"math"
"strings"

Expand Down Expand Up @@ -73,7 +74,7 @@ func (hdlr Latex) Box(txt string, fnt vg.Font) (width, height, depth vg.Length)

// Draw renders the given text with the provided style and position
// on the canvas.
func (hdlr Latex) Draw(c *draw.Canvas, txt string, sty draw.TextStyle, pt vg.Point) {
func (hdlr Latex) Draw(c vg.Canvas, txt string, sty draw.TextStyle, pt vg.Point) {
cnv := drawtex.New()
fnts := &ttf.Fonts{
Rm: sty.Font.Font(),
Expand Down Expand Up @@ -135,7 +136,7 @@ func (hdlr Latex) dpi() float64 {
}

type latex struct {
cnv *draw.Canvas
cnv vg.Canvas
sty draw.TextStyle
pt vg.Point

Expand Down Expand Up @@ -204,11 +205,27 @@ func (r *latex) drawRect(dpi float64, op drawtex.RectOp) {
vg.Point{X: x1, Y: y1}.Add(pt),
}

r.cnv.FillPolygon(r.sty.Color, pts)
fillPolygon(r.cnv, r.sty.Color, pts)
}

func (r *latex) rotate(x, y vg.Length) (vg.Length, vg.Length) {
u := x*r.cos + y*r.sin
v := y*r.cos - x*r.sin
return u, v
}

// FillPolygon fills a polygon with the given color.
func fillPolygon(c vg.Canvas, clr color.Color, pts []vg.Point) {
if len(pts) == 0 {
return
}

c.SetColor(clr)
p := make(vg.Path, 0, len(pts)+1)
p.Move(pts[0])
for _, pt := range pts[1:] {
p.Line(pt)
}
p.Close()
c.Fill(p)
}
2 changes: 1 addition & 1 deletion vg/draw/text.go
Expand Up @@ -24,7 +24,7 @@ type TextHandler interface {

// Draw renders the given text with the provided style and position
// on the canvas.
Draw(c *Canvas, txt string, sty TextStyle, pt vg.Point)
Draw(c vg.Canvas, txt string, sty TextStyle, pt vg.Point)
}

// TextStyle describes what text will look like.
Expand Down
2 changes: 1 addition & 1 deletion vg/draw/text_plain.go
Expand Up @@ -37,7 +37,7 @@ func (hdlr PlainTextHandler) Box(txt string, fnt vg.Font) (width, height, depth

// Draw renders the given text with the provided style and position
// on the canvas.
func (hdlr PlainTextHandler) Draw(c *Canvas, txt string, sty TextStyle, pt vg.Point) {
func (hdlr PlainTextHandler) Draw(c vg.Canvas, txt string, sty TextStyle, pt vg.Point) {
txt = strings.TrimRight(txt, "\n")
if len(txt) == 0 {
return
Expand Down

0 comments on commit 61a5e05

Please sign in to comment.