Draw renders the given text with the provided style and position on the canvas.
(c vg.Canvas, txt string, sty Style, pt vg.Point)
| 53 | // Draw renders the given text with the provided style and position |
| 54 | // on the canvas. |
| 55 | func (hdlr Plain) Draw(c vg.Canvas, txt string, sty Style, pt vg.Point) { |
| 56 | txt = strings.TrimRight(txt, "\n") |
| 57 | if len(txt) == 0 { |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | fnt := hdlr.Fonts.Lookup(sty.Font, sty.Font.Size) |
| 62 | c.SetColor(sty.Color) |
| 63 | |
| 64 | if sty.Rotation != 0 { |
| 65 | c.Push() |
| 66 | c.Rotate(sty.Rotation) |
| 67 | } |
| 68 | |
| 69 | sin64, cos64 := math.Sincos(sty.Rotation) |
| 70 | cos := vg.Length(cos64) |
| 71 | sin := vg.Length(sin64) |
| 72 | pt.X, pt.Y = pt.Y*sin+pt.X*cos, pt.Y*cos-pt.X*sin |
| 73 | |
| 74 | lines := hdlr.Lines(txt) |
| 75 | ht := sty.Height(txt) |
| 76 | pt.Y += ht*vg.Length(sty.YAlign) - fnt.Extents().Ascent |
| 77 | for i, line := range lines { |
| 78 | xoffs := vg.Length(sty.XAlign) * fnt.Width(line) |
| 79 | n := vg.Length(len(lines) - i) |
| 80 | c.FillString(fnt, pt.Add(vg.Point{X: xoffs, Y: n * sty.Font.Size}), line) |
| 81 | } |
| 82 | |
| 83 | if sty.Rotation != 0 { |
| 84 | c.Pop() |
| 85 | } |
| 86 | } |