| 1 |
package drawing |
| 2 |
|
| 3 |
import ( |
| 4 |
"image" |
| 5 |
"image/color" |
| 6 |
|
| 7 |
"github.com/golang/freetype/truetype" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
type StackGraphicContext struct { |
| 12 |
current *ContextStack |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
type ContextStack struct { |
| 17 |
Tr Matrix |
| 18 |
Path *Path |
| 19 |
LineWidth float64 |
| 20 |
Dash []float64 |
| 21 |
DashOffset float64 |
| 22 |
StrokeColor color.Color |
| 23 |
FillColor color.Color |
| 24 |
FillRule FillRule |
| 25 |
Cap LineCap |
| 26 |
Join LineJoin |
| 27 |
|
| 28 |
FontSizePoints float64 |
| 29 |
Font *truetype.Font |
| 30 |
|
| 31 |
Scale float64 |
| 32 |
|
| 33 |
Previous *ContextStack |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
func NewStackGraphicContext() *StackGraphicContext { |
| 38 |
gc := &StackGraphicContext{} |
| 39 |
gc.current = new(ContextStack) |
| 40 |
gc.current.Tr = NewIdentityMatrix() |
| 41 |
gc.current.Path = new(Path) |
| 42 |
gc.current.LineWidth = 1.0 |
| 43 |
gc.current.StrokeColor = image.Black |
| 44 |
gc.current.FillColor = image.White |
| 45 |
gc.current.Cap = RoundCap |
| 46 |
gc.current.FillRule = FillRuleEvenOdd |
| 47 |
gc.current.Join = RoundJoin |
| 48 |
gc.current.FontSizePoints = 10 |
| 49 |
return gc |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
func (gc *StackGraphicContext) GetMatrixTransform() Matrix { |
| 54 |
return gc.current.Tr |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
func (gc *StackGraphicContext) SetMatrixTransform(tr Matrix) { |
| 59 |
gc.current.Tr = tr |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
func (gc *StackGraphicContext) ComposeMatrixTransform(tr Matrix) { |
| 64 |
gc.current.Tr.Compose(tr) |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
func (gc *StackGraphicContext) Rotate(angle float64) { |
| 69 |
gc.current.Tr.Rotate(angle) |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
func (gc *StackGraphicContext) Translate(tx, ty float64) { |
| 74 |
gc.current.Tr.Translate(tx, ty) |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
func (gc *StackGraphicContext) Scale(sx, sy float64) { |
| 79 |
gc.current.Tr.Scale(sx, sy) |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
func (gc *StackGraphicContext) SetStrokeColor(c color.Color) { |
| 84 |
gc.current.StrokeColor = c |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
func (gc *StackGraphicContext) SetFillColor(c color.Color) { |
| 89 |
gc.current.FillColor = c |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
func (gc *StackGraphicContext) SetFillRule(f FillRule) { |
| 94 |
gc.current.FillRule = f |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
func (gc *StackGraphicContext) SetLineWidth(lineWidth float64) { |
| 99 |
gc.current.LineWidth = lineWidth |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
func (gc *StackGraphicContext) SetLineCap(cap LineCap) { |
| 104 |
gc.current.Cap = cap |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
func (gc *StackGraphicContext) SetLineJoin(join LineJoin) { |
| 109 |
gc.current.Join = join |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
func (gc *StackGraphicContext) SetLineDash(dash []float64, dashOffset float64) { |
| 114 |
gc.current.Dash = dash |
| 115 |
gc.current.DashOffset = dashOffset |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
func (gc *StackGraphicContext) SetFontSize(fontSizePoints float64) { |
| 120 |
gc.current.FontSizePoints = fontSizePoints |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
func (gc *StackGraphicContext) GetFontSize() float64 { |
| 125 |
return gc.current.FontSizePoints |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
func (gc *StackGraphicContext) SetFont(f *truetype.Font) { |
| 130 |
gc.current.Font = f |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
func (gc *StackGraphicContext) GetFont() *truetype.Font { |
| 135 |
return gc.current.Font |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
func (gc *StackGraphicContext) BeginPath() { |
| 140 |
gc.current.Path.Clear() |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
func (gc *StackGraphicContext) IsEmpty() bool { |
| 145 |
return gc.current.Path.IsEmpty() |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
func (gc *StackGraphicContext) LastPoint() (x float64, y float64) { |
| 150 |
return gc.current.Path.LastPoint() |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
func (gc *StackGraphicContext) MoveTo(x, y float64) { |
| 155 |
gc.current.Path.MoveTo(x, y) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
func (gc *StackGraphicContext) LineTo(x, y float64) { |
| 160 |
gc.current.Path.LineTo(x, y) |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
func (gc *StackGraphicContext) QuadCurveTo(cx, cy, x, y float64) { |
| 165 |
gc.current.Path.QuadCurveTo(cx, cy, x, y) |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
func (gc *StackGraphicContext) CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64) { |
| 170 |
gc.current.Path.CubicCurveTo(cx1, cy1, cx2, cy2, x, y) |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
func (gc *StackGraphicContext) ArcTo(cx, cy, rx, ry, startAngle, delta float64) { |
| 175 |
gc.current.Path.ArcTo(cx, cy, rx, ry, startAngle, delta) |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
func (gc *StackGraphicContext) Close() { |
| 180 |
gc.current.Path.Close() |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
func (gc *StackGraphicContext) Save() { |
| 185 |
context := new(ContextStack) |
| 186 |
context.FontSizePoints = gc.current.FontSizePoints |
| 187 |
context.Font = gc.current.Font |
| 188 |
context.LineWidth = gc.current.LineWidth |
| 189 |
context.StrokeColor = gc.current.StrokeColor |
| 190 |
context.FillColor = gc.current.FillColor |
| 191 |
context.FillRule = gc.current.FillRule |
| 192 |
context.Dash = gc.current.Dash |
| 193 |
context.DashOffset = gc.current.DashOffset |
| 194 |
context.Cap = gc.current.Cap |
| 195 |
context.Join = gc.current.Join |
| 196 |
context.Path = gc.current.Path.Copy() |
| 197 |
context.Font = gc.current.Font |
| 198 |
context.Scale = gc.current.Scale |
| 199 |
copy(context.Tr[:], gc.current.Tr[:]) |
| 200 |
context.Previous = gc.current |
| 201 |
gc.current = context |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
func (gc *StackGraphicContext) Restore() { |
| 206 |
if gc.current.Previous != nil { |
| 207 |
oldContext := gc.current |
| 208 |
gc.current = gc.current.Previous |
| 209 |
oldContext.Previous = nil |
| 210 |
} |
| 211 |
} |