Debian Codegraph @

writer/standard/image_qr_shape.go

raw
1 package standard
2
3 import (
4 "image/color"
5
6 "github.com/fogleman/gg"
7 )
8
9 var (
10 _shapeRectangle IShape = rectangle{}
11 _shapeCircle IShape = circle{}
12 )
13
14 type IShape interface {
15 // Draw the shape of QRCode block in IShape implemented way.
16 Draw(ctx *DrawContext)
17
18 // DrawFinder to fill the finder pattern of QRCode, what's finder? google it for more information.
19 DrawFinder(ctx *DrawContext)
20 }
21
22 // DrawContext is a rectangle area
23 type DrawContext struct {
24 *gg.Context
25
26 x, y float64
27 w, h int
28
29 color color.Color
30 }
31
32 // UpperLeft returns the point which indicates the upper left position.
33 func (dc *DrawContext) UpperLeft() (dx, dy float64) {
34 return dc.x, dc.y
35 }
36
37 // Edge returns width and height of each shape could take at most.
38 func (dc *DrawContext) Edge() (width, height int) {
39 return dc.w, dc.h
40 }
41
42 // Color returns the color which should be fill into the shape. Note that if you're not
43 // using this color but your coded color.Color, some ImageOption functions those set foreground color
44 // would take no effect.
45 func (dc *DrawContext) Color() color.Color {
46 return dc.color
47 }
48
49 // rectangle IShape
50 type rectangle struct{}
51
52 func (r rectangle) Draw(c *DrawContext) {
53 // FIXED(@yeqown): miss parameter of DrawRectangle
54 c.DrawRectangle(c.x, c.y, float64(c.w), float64(c.h))
55 c.SetColor(c.color)
56 c.Fill()
57 }
58
59 func (r rectangle) DrawFinder(ctx *DrawContext) {
60 r.Draw(ctx)
61 }
62
63 // circle IShape
64 type circle struct{}
65
66 // Draw
67 // FIXED: Draw could not draw circle
68 func (r circle) Draw(c *DrawContext) {
69 // choose a proper radius values
70 radius := c.w / 2
71 r2 := c.h / 2
72 if r2 <= radius {
73 radius = r2
74 }
75
76 cx, cy := c.x+float64(c.w)/2.0, c.y+float64(c.h)/2.0 // get center point
77 c.DrawCircle(cx, cy, float64(radius))
78 c.SetColor(c.color)
79 c.Fill()
80 }
81
82 func (r circle) DrawFinder(ctx *DrawContext) {
83 r.Draw(ctx)
84 }