Debian Codegraph @

raster_renderer.go

raw symbols SCIP
1 package chart
2
3 import (
4 "image"
5 "image/png"
6 "io"
7 "math"
8
9 "github.com/golang/freetype/truetype"
10 "github.com/wcharczuk/go-chart/v2/drawing"
11 )
12
13 // PNG returns a new png/raster renderer.
14 func PNG(width, height int) (Renderer, error) {
15 i := image.NewRGBA(image.Rect(0, 0, width, height))
16 gc, err := drawing.NewRasterGraphicContext(i)
17 if err == nil {
18 return &rasterRenderer{
19 i: i,
20 gc: gc,
21 }, nil
22 }
23 return nil, err
24 }
25
26 // rasterRenderer renders chart commands to a bitmap.
27 type rasterRenderer struct {
28 i *image.RGBA
29 gc *drawing.RasterGraphicContext
30
31 rotateRadians *float64
32
33 s Style
34 }
35
36 func (rr *rasterRenderer) ResetStyle() {
37 rr.s = Style{Font: rr.s.Font}
38 rr.ClearTextRotation()
39 }
40
41 // GetDPI returns the dpi.
42 func (rr *rasterRenderer) GetDPI() float64 {
43 return rr.gc.GetDPI()
44 }
45
46 // SetDPI implements the interface method.
47 func (rr *rasterRenderer) SetDPI(dpi float64) {
48 rr.gc.SetDPI(dpi)
49 }
50
51 // SetClassName implements the interface method. However, PNGs have no classes.
52 func (rr *rasterRenderer) SetClassName(_ string) {}
53
54 // SetStrokeColor implements the interface method.
55 func (rr *rasterRenderer) SetStrokeColor(c drawing.Color) {
56 rr.s.StrokeColor = c
57 }
58
59 // SetLineWidth implements the interface method.
60 func (rr *rasterRenderer) SetStrokeWidth(width float64) {
61 rr.s.StrokeWidth = width
62 }
63
64 // StrokeDashArray sets the stroke dash array.
65 func (rr *rasterRenderer) SetStrokeDashArray(dashArray []float64) {
66 rr.s.StrokeDashArray = dashArray
67 }
68
69 // SetFillColor implements the interface method.
70 func (rr *rasterRenderer) SetFillColor(c drawing.Color) {
71 rr.s.FillColor = c
72 }
73
74 // MoveTo implements the interface method.
75 func (rr *rasterRenderer) MoveTo(x, y int) {
76 rr.gc.MoveTo(float64(x), float64(y))
77 }
78
79 // LineTo implements the interface method.
80 func (rr *rasterRenderer) LineTo(x, y int) {
81 rr.gc.LineTo(float64(x), float64(y))
82 }
83
84 // QuadCurveTo implements the interface method.
85 func (rr *rasterRenderer) QuadCurveTo(cx, cy, x, y int) {
86 rr.gc.QuadCurveTo(float64(cx), float64(cy), float64(x), float64(y))
87 }
88
89 // ArcTo implements the interface method.
90 func (rr *rasterRenderer) ArcTo(cx, cy int, rx, ry, startAngle, delta float64) {
91 rr.gc.ArcTo(float64(cx), float64(cy), rx, ry, startAngle, delta)
92 }
93
94 // Close implements the interface method.
95 func (rr *rasterRenderer) Close() {
96 rr.gc.Close()
97 }
98
99 // Stroke implements the interface method.
100 func (rr *rasterRenderer) Stroke() {
101 rr.gc.SetStrokeColor(rr.s.StrokeColor)
102 rr.gc.SetLineWidth(rr.s.StrokeWidth)
103 rr.gc.SetLineDash(rr.s.StrokeDashArray, 0)
104 rr.gc.Stroke()
105 }
106
107 // Fill implements the interface method.
108 func (rr *rasterRenderer) Fill() {
109 rr.gc.SetFillColor(rr.s.FillColor)
110 rr.gc.Fill()
111 }
112
113 // FillStroke implements the interface method.
114 func (rr *rasterRenderer) FillStroke() {
115 rr.gc.SetFillColor(rr.s.FillColor)
116 rr.gc.SetStrokeColor(rr.s.StrokeColor)
117 rr.gc.SetLineWidth(rr.s.StrokeWidth)
118 rr.gc.SetLineDash(rr.s.StrokeDashArray, 0)
119 rr.gc.FillStroke()
120 }
121
122 // Circle fully draws a circle at a given point but does not apply the fill or stroke.
123 func (rr *rasterRenderer) Circle(radius float64, x, y int) {
124 xf := float64(x)
125 yf := float64(y)
126
127 rr.gc.MoveTo(xf-radius, yf) //9
128 rr.gc.QuadCurveTo(xf-radius, yf-radius, xf, yf-radius) //12
129 rr.gc.QuadCurveTo(xf+radius, yf-radius, xf+radius, yf) //3
130 rr.gc.QuadCurveTo(xf+radius, yf+radius, xf, yf+radius) //6
131 rr.gc.QuadCurveTo(xf-radius, yf+radius, xf-radius, yf) //9
132 }
133
134 // SetFont implements the interface method.
135 func (rr *rasterRenderer) SetFont(f *truetype.Font) {
136 rr.s.Font = f
137 }
138
139 // SetFontSize implements the interface method.
140 func (rr *rasterRenderer) SetFontSize(size float64) {
141 rr.s.FontSize = size
142 }
143
144 // SetFontColor implements the interface method.
145 func (rr *rasterRenderer) SetFontColor(c drawing.Color) {
146 rr.s.FontColor = c
147 }
148
149 // Text implements the interface method.
150 func (rr *rasterRenderer) Text(body string, x, y int) {
151 xf, yf := rr.getCoords(x, y)
152 rr.gc.SetFont(rr.s.Font)
153 rr.gc.SetFontSize(rr.s.FontSize)
154 rr.gc.SetFillColor(rr.s.FontColor)
155 rr.gc.CreateStringPath(body, float64(xf), float64(yf))
156 rr.gc.Fill()
157 }
158
159 // MeasureText returns the height and width in pixels of a string.
160 func (rr *rasterRenderer) MeasureText(body string) Box {
161 rr.gc.SetFont(rr.s.Font)
162 rr.gc.SetFontSize(rr.s.FontSize)
163 rr.gc.SetFillColor(rr.s.FontColor)
164 l, t, r, b, err := rr.gc.GetStringBounds(body)
165 if err != nil {
166 return Box{}
167 }
168 if l < 0 {
169 r = r - l // equivalent to r+(-1*l)
170 l = 0
171 }
172 if t < 0 {
173 b = b - t
174 t = 0
175 }
176
177 if l > 0 {
178 r = r + l
179 l = 0
180 }
181
182 if t > 0 {
183 b = b + t
184 t = 0
185 }
186
187 textBox := Box{
188 Top: int(math.Ceil(t)),
189 Left: int(math.Ceil(l)),
190 Right: int(math.Ceil(r)),
191 Bottom: int(math.Ceil(b)),
192 }
193 if rr.rotateRadians == nil {
194 return textBox
195 }
196
197 return textBox.Corners().Rotate(RadiansToDegrees(*rr.rotateRadians)).Box()
198 }
199
200 // SetTextRotation sets a text rotation.
201 func (rr *rasterRenderer) SetTextRotation(radians float64) {
202 rr.rotateRadians = &radians
203 }
204
205 func (rr *rasterRenderer) getCoords(x, y int) (xf, yf int) {
206 if rr.rotateRadians == nil {
207 xf = x
208 yf = y
209 return
210 }
211
212 rr.gc.Translate(float64(x), float64(y))
213 rr.gc.Rotate(*rr.rotateRadians)
214 return
215 }
216
217 // ClearTextRotation clears text rotation.
218 func (rr *rasterRenderer) ClearTextRotation() {
219 rr.gc.SetMatrixTransform(drawing.NewIdentityMatrix())
220 rr.rotateRadians = nil
221 }
222
223 // Save implements the interface method.
224 func (rr *rasterRenderer) Save(w io.Writer) error {
225 if typed, isTyped := w.(RGBACollector); isTyped {
226 typed.SetRGBA(rr.i)
227 return nil
228 }
229 return png.Encode(w, rr.i)
230 }