refactoring how the raster renderer handles styles.

This commit is contained in:
Will Charczuk 2016-07-14 18:51:42 -07:00
parent 7bce09c859
commit 16721a5844
2 changed files with 22 additions and 17 deletions

View File

@ -143,7 +143,7 @@ func DrawBox(r Renderer, b Box, s Style) {
// DrawText draws text with a given style. // DrawText draws text with a given style.
func DrawText(r Renderer, text string, x, y int, s Style) { func DrawText(r Renderer, text string, x, y int, s Style) {
r.SetFillColor(s.GetFillColor(DefaultTextColor)) r.SetFontColor(s.GetFontColor(DefaultTextColor))
r.SetStrokeColor(s.GetStrokeColor()) r.SetStrokeColor(s.GetStrokeColor())
r.SetStrokeWidth(s.GetStrokeWidth()) r.SetStrokeWidth(s.GetStrokeWidth())
r.SetFont(s.GetFont()) r.SetFont(s.GetFont())
@ -154,7 +154,7 @@ func DrawText(r Renderer, text string, x, y int, s Style) {
// DrawTextCentered draws text with a given style centered. // DrawTextCentered draws text with a given style centered.
func DrawTextCentered(r Renderer, text string, x, y int, s Style) { func DrawTextCentered(r Renderer, text string, x, y int, s Style) {
r.SetFillColor(s.GetFillColor(DefaultTextColor)) r.SetFontColor(s.GetFontColor(DefaultTextColor))
r.SetStrokeColor(s.GetStrokeColor()) r.SetStrokeColor(s.GetStrokeColor())
r.SetStrokeWidth(s.GetStrokeWidth()) r.SetStrokeWidth(s.GetStrokeWidth())
r.SetFont(s.GetFont()) r.SetFont(s.GetFont())

View File

@ -2,7 +2,6 @@ package chart
import ( import (
"image" "image"
"image/color"
"image/png" "image/png"
"io" "io"
"math" "math"
@ -29,9 +28,7 @@ type rasterRenderer struct {
i *image.RGBA i *image.RGBA
gc *drawing.RasterGraphicContext gc *drawing.RasterGraphicContext
fontSize float64 s Style
fontColor color.Color
f *truetype.Font
} }
// GetDPI returns the dpi. // GetDPI returns the dpi.
@ -46,22 +43,22 @@ func (rr *rasterRenderer) SetDPI(dpi float64) {
// SetStrokeColor implements the interface method. // SetStrokeColor implements the interface method.
func (rr *rasterRenderer) SetStrokeColor(c drawing.Color) { func (rr *rasterRenderer) SetStrokeColor(c drawing.Color) {
rr.gc.SetStrokeColor(c) rr.s.StrokeColor = c
} }
// SetLineWidth implements the interface method. // SetLineWidth implements the interface method.
func (rr *rasterRenderer) SetStrokeWidth(width float64) { func (rr *rasterRenderer) SetStrokeWidth(width float64) {
rr.gc.SetLineWidth(width) rr.s.StrokeWidth = width
} }
// StrokeDashArray sets the stroke dash array. // StrokeDashArray sets the stroke dash array.
func (rr *rasterRenderer) SetStrokeDashArray(dashArray []float64) { func (rr *rasterRenderer) SetStrokeDashArray(dashArray []float64) {
rr.gc.SetLineDash(dashArray, 0.0) rr.s.StrokeDashArray = dashArray
} }
// SetFillColor implements the interface method. // SetFillColor implements the interface method.
func (rr *rasterRenderer) SetFillColor(c drawing.Color) { func (rr *rasterRenderer) SetFillColor(c drawing.Color) {
rr.gc.SetFillColor(c) rr.s.FillColor = c
} }
// MoveTo implements the interface method. // MoveTo implements the interface method.
@ -81,16 +78,22 @@ func (rr *rasterRenderer) Close() {
// Stroke implements the interface method. // Stroke implements the interface method.
func (rr *rasterRenderer) Stroke() { func (rr *rasterRenderer) Stroke() {
rr.gc.SetStrokeColor(rr.s.StrokeColor)
rr.gc.SetLineWidth(rr.s.StrokeWidth)
rr.gc.Stroke() rr.gc.Stroke()
} }
// Fill implements the interface method. // Fill implements the interface method.
func (rr *rasterRenderer) Fill() { func (rr *rasterRenderer) Fill() {
rr.gc.SetFillColor(rr.s.FillColor)
rr.gc.Fill() rr.gc.Fill()
} }
// FillStroke implements the interface method. // FillStroke implements the interface method.
func (rr *rasterRenderer) FillStroke() { func (rr *rasterRenderer) FillStroke() {
rr.gc.SetFillColor(rr.s.FillColor)
rr.gc.SetStrokeColor(rr.s.StrokeColor)
rr.gc.SetLineWidth(rr.s.StrokeWidth)
rr.gc.FillStroke() rr.gc.FillStroke()
} }
@ -109,31 +112,33 @@ func (rr *rasterRenderer) Circle(radius float64, x, y int) {
// SetFont implements the interface method. // SetFont implements the interface method.
func (rr *rasterRenderer) SetFont(f *truetype.Font) { func (rr *rasterRenderer) SetFont(f *truetype.Font) {
rr.f = f rr.s.Font = f
rr.gc.SetFont(f)
} }
// SetFontSize implements the interface method. // SetFontSize implements the interface method.
func (rr *rasterRenderer) SetFontSize(size float64) { func (rr *rasterRenderer) SetFontSize(size float64) {
rr.fontSize = size rr.s.FontSize = size
rr.gc.SetFontSize(size)
} }
// SetFontColor implements the interface method. // SetFontColor implements the interface method.
func (rr *rasterRenderer) SetFontColor(c drawing.Color) { func (rr *rasterRenderer) SetFontColor(c drawing.Color) {
rr.fontColor = c rr.s.FontColor = c
rr.gc.SetFillColor(c)
rr.gc.SetStrokeColor(c)
} }
// Text implements the interface method. // Text implements the interface method.
func (rr *rasterRenderer) Text(body string, x, y int) { func (rr *rasterRenderer) Text(body string, x, y int) {
rr.gc.SetFont(rr.s.Font)
rr.gc.SetFontSize(rr.s.FontSize)
rr.gc.SetFillColor(rr.s.FontColor)
rr.gc.CreateStringPath(body, float64(x), float64(y)) rr.gc.CreateStringPath(body, float64(x), float64(y))
rr.gc.Fill() rr.gc.Fill()
} }
// MeasureText returns the height and width in pixels of a string. // MeasureText returns the height and width in pixels of a string.
func (rr *rasterRenderer) MeasureText(body string) Box { func (rr *rasterRenderer) MeasureText(body string) Box {
rr.gc.SetFont(rr.s.Font)
rr.gc.SetFontSize(rr.s.FontSize)
rr.gc.SetFillColor(rr.s.FontColor)
l, t, r, b, err := rr.gc.GetStringBounds(body) l, t, r, b, err := rr.gc.GetStringBounds(body)
if err != nil { if err != nil {
return Box{} return Box{}