Skip to content

Commit cc6f6ce

Browse files
authored
Custom font path (#30)
* fix: wrong resource type name * add: custom font path
1 parent 6c9b0a2 commit cc6f6ce

File tree

7 files changed

+118
-40
lines changed

7 files changed

+118
-40
lines changed

cmd/awsdac/main.go

+44-13
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import (
2121

2222
func stringToColor(c string) color.RGBA {
2323
var r, g, b, a uint8
24-
fmt.Sscanf(c, "rgba(%d,%d,%d,%d)", &r, &g, &b, &a)
24+
_, err := fmt.Sscanf(c, "rgba(%d,%d,%d,%d)", &r, &g, &b, &a)
25+
if err != nil {
26+
log.Fatal(err)
27+
}
2528
return color.RGBA{r, g, b, a}
2629
}
2730

@@ -43,14 +46,16 @@ type DefinitionFile struct {
4346
}
4447

4548
type Resource struct {
46-
Type string `yaml:"Type"`
47-
Icon string `yaml:"Icon"`
48-
Direction string `yaml:"Direction"`
49-
Preset string `yaml:"Preset"`
50-
Align string `yaml:"Align"`
51-
FillColor string `yaml:"FillColor"`
52-
Title string `yaml:"Title"`
53-
Children []string `yaml:"Children"`
49+
Type string `yaml:"Type"`
50+
Icon string `yaml:"Icon"`
51+
Direction string `yaml:"Direction"`
52+
Preset string `yaml:"Preset"`
53+
Align string `yaml:"Align"`
54+
FillColor string `yaml:"FillColor"`
55+
Title string `yaml:"Title"`
56+
TitleColor string `yaml:"TitleColor"`
57+
Font string `yaml:"Font"`
58+
Children []string `yaml:"Children"`
5459
}
5560

5661
type Link struct {
@@ -151,7 +156,16 @@ func main() {
151156
resources[k].SetBorderColor(stringToColor(border.Color))
152157
}
153158
if label := def.Label; label != nil {
154-
resources[k].SetLabel(label.Title, stringToColor(label.Color))
159+
if label.Title != "" {
160+
resources[k].SetLabel(&label.Title, nil, nil)
161+
}
162+
if label.Color != "" {
163+
c := stringToColor(label.Color)
164+
resources[k].SetLabel(nil, &c, nil)
165+
}
166+
if label.Font != "" {
167+
resources[k].SetLabel(nil, nil, &label.Font)
168+
}
155169
}
156170
if icon := def.Icon; icon != nil {
157171
if def.CacheFilePath == "" {
@@ -177,7 +191,17 @@ func main() {
177191
resources[k].SetBorderColor(stringToColor(border.Color))
178192
}
179193
if label := def.Label; label != nil {
180-
resources[k].SetLabel(label.Title, stringToColor(label.Color))
194+
if label.Title != "" {
195+
resources[k].SetLabel(&label.Title, nil, nil)
196+
}
197+
if label.Color != "" {
198+
c := stringToColor(label.Color)
199+
resources[k].SetLabel(nil, &c, nil)
200+
}
201+
if label.Font != "" {
202+
resources[k].SetLabel(nil, nil, &label.Font)
203+
}
204+
181205
}
182206
if icon := def.Icon; icon != nil {
183207
if def.CacheFilePath == "" {
@@ -190,7 +214,14 @@ func main() {
190214
resources[k].LoadIcon(v.Icon)
191215
}
192216
if v.Title != "" {
193-
resources[k].SetLabel(title, color.RGBA{0, 0, 0, 255})
217+
resources[k].SetLabel(&title, nil, nil)
218+
}
219+
if v.TitleColor != "" {
220+
c := stringToColor(v.TitleColor)
221+
resources[k].SetLabel(nil, &c, nil)
222+
}
223+
if v.Font != "" {
224+
resources[k].SetLabel(nil, nil, &v.Font)
194225
}
195226
if v.Align != "" {
196227
resources[k].SetAlign(v.Align)
@@ -244,7 +275,7 @@ func main() {
244275
log.Info("Drawing")
245276
resources["Canvas"].Scale()
246277
resources["Canvas"].ZeroAdjust()
247-
img := resources["Canvas"].Draw(nil)
278+
img := resources["Canvas"].Draw(nil, nil)
248279

249280
log.Infof("Save %s\n", *outputfile)
250281
f, _ := os.OpenFile(*outputfile, os.O_WRONLY|os.O_CREATE, 0600)

internal/definition/definition.go

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Definition struct {
1818
type DefinitionLabel struct {
1919
Title string `yaml:"Title"`
2020
Color string `yaml:"Color"`
21+
Font string `yaml:"Font"`
2122
}
2223

2324
type DefinitionFill struct {

internal/types/group.go

+35-11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ type Group struct {
2525
borderColor color.RGBA
2626
fillColor color.RGBA
2727
label string
28-
fontColor color.RGBA
28+
labelFont string
29+
labelColor *color.RGBA
2930
width int
3031
height int
3132
margin Margin
@@ -45,7 +46,8 @@ func (g Group) Init() Node {
4546
gr.borderColor = color.RGBA{0, 0, 0, 0}
4647
gr.fillColor = color.RGBA{0, 0, 0, 0}
4748
gr.label = ""
48-
gr.fontColor = color.RGBA{0, 0, 0, 0}
49+
gr.labelFont = ""
50+
gr.labelColor = nil
4951
gr.width = 320
5052
gr.height = 190
5153
gr.margin = Margin{20, 15, 20, 15}
@@ -97,9 +99,17 @@ func (g *Group) SetFillColor(fillColor color.RGBA) {
9799
g.fillColor = fillColor
98100
}
99101

100-
func (g *Group) SetLabel(label string, fontColor color.RGBA) {
101-
g.label = label
102-
g.fontColor = fontColor
102+
func (g *Group) SetLabel(label *string, labelColor *color.RGBA, labelFont *string) {
103+
104+
if label != nil {
105+
g.label = *label
106+
}
107+
if labelColor != nil {
108+
g.labelColor = labelColor
109+
}
110+
if labelFont != nil {
111+
g.labelFont = *labelFont
112+
}
103113
}
104114

105115
func (g *Group) SetAlign(align string) {
@@ -226,7 +236,7 @@ func (g *Group) IsDrawn() bool {
226236
return g.drawn
227237
}
228238

229-
func (g *Group) Draw(img *image.RGBA) *image.RGBA {
239+
func (g *Group) Draw(img *image.RGBA, parent *Group) *image.RGBA {
230240
if img == nil {
231241
img = image.NewRGBA(g.bindings)
232242
}
@@ -236,10 +246,10 @@ func (g *Group) Draw(img *image.RGBA) *image.RGBA {
236246
rctSrc := g.iconImage.Bounds()
237247
draw.CatmullRom.Scale(img, x, g.iconImage, rctSrc, draw.Over, nil)
238248

239-
g.drawLabel(img)
249+
g.drawLabel(img, parent)
240250

241251
for _, subGroup := range g.children {
242-
subGroup.Draw(img)
252+
subGroup.Draw(img, g)
243253
}
244254
g.drawn = true
245255
for _, v := range g.links {
@@ -273,11 +283,25 @@ func (g *Group) drawFrame(img *image.RGBA) {
273283
}
274284
}
275285

276-
func (g *Group) drawLabel(img *image.RGBA) {
286+
func (g *Group) drawLabel(img *image.RGBA, parent *Group) {
277287

278288
p := g.bindings.Min.Add(g.iconBounds.Max)
279289

280-
f, err := os.Open(fontPath.Arial)
290+
if g.labelFont == "" {
291+
if parent != nil && parent.labelFont != "" {
292+
g.labelFont = parent.labelFont
293+
} else {
294+
g.labelFont = fontPath.Arial
295+
}
296+
}
297+
if g.labelColor == nil {
298+
if parent != nil && parent.labelColor != nil {
299+
g.labelColor = parent.labelColor
300+
} else {
301+
g.labelColor = &color.RGBA{0, 0, 0, 255}
302+
}
303+
}
304+
f, err := os.Open(g.labelFont)
281305
if err != nil {
282306
panic(err)
283307
}
@@ -312,7 +336,7 @@ func (g *Group) drawLabel(img *image.RGBA) {
312336

313337
d := &font.Drawer{
314338
Dst: img,
315-
Src: image.NewUniform(g.fontColor),
339+
Src: image.NewUniform(g.labelColor),
316340
Face: face,
317341
Dot: point,
318342
}

internal/types/horizontal_stack.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type HorizontalStack struct {
1515
borderColor color.RGBA
1616
fillColor color.RGBA
1717
label string
18-
fontColor color.RGBA
18+
labelColor color.RGBA
1919
width int
2020
height int
2121
margin Margin
@@ -32,7 +32,7 @@ func (v HorizontalStack) Init() Node {
3232
sr.borderColor = color.RGBA{0, 0, 0, 0}
3333
sr.fillColor = color.RGBA{0, 0, 0, 0}
3434
sr.label = ""
35-
sr.fontColor = color.RGBA{0, 0, 0, 0}
35+
sr.labelColor = &color.RGBA{0, 0, 0, 0}
3636
sr.width = 320
3737
sr.height = 190
3838
sr.margin = Margin{0, 0, 0, 0}

internal/types/resource.go

+32-10
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ type Resource struct {
2424
borderColor color.RGBA
2525
fillColor color.RGBA
2626
label string
27-
fontColor color.RGBA
27+
labelFont string
28+
labelColor *color.RGBA
2829
width int
2930
height int
3031
margin Margin
@@ -41,7 +42,7 @@ func (r Resource) Init() Node {
4142
rr.borderColor = color.RGBA{0, 0, 0, 0}
4243
rr.fillColor = color.RGBA{0, 0, 0, 0}
4344
rr.label = ""
44-
rr.fontColor = color.RGBA{0, 0, 0, 0}
45+
rr.labelColor = &color.RGBA{0, 0, 0, 0}
4546
rr.width = 64
4647
rr.height = 64
4748
rr.margin = Margin{30, 100, 30, 100}
@@ -89,9 +90,16 @@ func (r *Resource) SetFillColor(fillColor color.RGBA) {
8990
r.fillColor = fillColor
9091
}
9192

92-
func (r *Resource) SetLabel(label string, fontColor color.RGBA) {
93-
r.label = label
94-
r.fontColor = fontColor
93+
func (r *Resource) SetLabel(label *string, labelColor *color.RGBA, labelFont *string) {
94+
if label != nil {
95+
r.label = *label
96+
}
97+
if labelColor != nil {
98+
r.labelColor = labelColor
99+
}
100+
if labelFont != nil {
101+
r.labelFont = *labelFont
102+
}
95103
}
96104

97105
func (r *Resource) SetAlign(align string) {
@@ -130,7 +138,7 @@ func (r *Resource) IsDrawn() bool {
130138
return r.drawn
131139
}
132140

133-
func (r *Resource) Draw(img *image.RGBA) *image.RGBA {
141+
func (r *Resource) Draw(img *image.RGBA, parent *Group) *image.RGBA {
134142
if img == nil {
135143
img = image.NewRGBA(r.bindings)
136144
}
@@ -140,7 +148,7 @@ func (r *Resource) Draw(img *image.RGBA) *image.RGBA {
140148

141149
r.drawFrame(img)
142150

143-
r.drawLabel(img)
151+
r.drawLabel(img, parent)
144152

145153
r.drawn = true
146154
for _, v := range r.links {
@@ -174,9 +182,23 @@ func (r *Resource) drawFrame(img *image.RGBA) {
174182
}
175183
}
176184

177-
func (r *Resource) drawLabel(img *image.RGBA) {
185+
func (r *Resource) drawLabel(img *image.RGBA, parent *Group) {
178186

179-
f, err := os.Open(fontPath.Arial)
187+
if r.labelFont == "" {
188+
if parent != nil && parent.labelFont != "" {
189+
r.labelFont = parent.labelFont
190+
} else {
191+
r.labelFont = fontPath.Arial
192+
}
193+
}
194+
if r.labelColor == nil {
195+
if parent != nil && parent.labelColor != nil {
196+
r.labelColor = parent.labelColor
197+
} else {
198+
r.labelColor = &color.RGBA{0, 0, 0, 255}
199+
}
200+
}
201+
f, err := os.Open(r.labelFont)
180202
if err != nil {
181203
panic(err)
182204
}
@@ -213,7 +235,7 @@ func (r *Resource) drawLabel(img *image.RGBA) {
213235

214236
d := &font.Drawer{
215237
Dst: img,
216-
Src: image.NewUniform(r.fontColor),
238+
Src: image.NewUniform(r.labelColor),
217239
Face: face,
218240
Dot: point,
219241
}

internal/types/types.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ type Padding struct {
2828
type Node interface {
2929
Init() Node
3030
IsDrawn() bool
31-
Draw(*image.RGBA) *image.RGBA
31+
Draw(*image.RGBA, *Group) *image.RGBA
3232
Scale()
3333
GetBindings() image.Rectangle
3434
GetMargin() Margin
3535
LoadIcon(string)
3636
SetIconBounds(image.Rectangle)
3737
SetBorderColor(color.RGBA)
3838
SetFillColor(color.RGBA)
39-
SetLabel(string, color.RGBA)
39+
SetLabel(*string, *color.RGBA, *string)
4040
SetAlign(string)
4141
SetDirection(string)
4242
AddLink(*Link)

internal/types/vertical_stack.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type VerticalStack struct {
1515
borderColor color.RGBA
1616
fillColor color.RGBA
1717
label string
18-
fontColor color.RGBA
18+
labelColor color.RGBA
1919
width int
2020
height int
2121
margin Margin
@@ -32,7 +32,7 @@ func (h VerticalStack) Init() Node {
3232
sr.borderColor = color.RGBA{0, 0, 0, 0}
3333
sr.fillColor = color.RGBA{0, 0, 0, 0}
3434
sr.label = ""
35-
sr.fontColor = color.RGBA{0, 0, 0, 0}
35+
sr.labelColor = &color.RGBA{0, 0, 0, 0}
3636
sr.width = 320
3737
sr.height = 190
3838
sr.margin = Margin{0, 0, 0, 0}

0 commit comments

Comments
 (0)