Skip to content

Commit

Permalink
Custom font path (#30)
Browse files Browse the repository at this point in the history
* fix: wrong resource type name

* add: custom font path
  • Loading branch information
ugwis authored Apr 2, 2024
1 parent 6c9b0a2 commit cc6f6ce
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 40 deletions.
57 changes: 44 additions & 13 deletions cmd/awsdac/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import (

func stringToColor(c string) color.RGBA {
var r, g, b, a uint8
fmt.Sscanf(c, "rgba(%d,%d,%d,%d)", &r, &g, &b, &a)
_, err := fmt.Sscanf(c, "rgba(%d,%d,%d,%d)", &r, &g, &b, &a)
if err != nil {
log.Fatal(err)
}
return color.RGBA{r, g, b, a}
}

Expand All @@ -43,14 +46,16 @@ type DefinitionFile struct {
}

type Resource struct {
Type string `yaml:"Type"`
Icon string `yaml:"Icon"`
Direction string `yaml:"Direction"`
Preset string `yaml:"Preset"`
Align string `yaml:"Align"`
FillColor string `yaml:"FillColor"`
Title string `yaml:"Title"`
Children []string `yaml:"Children"`
Type string `yaml:"Type"`
Icon string `yaml:"Icon"`
Direction string `yaml:"Direction"`
Preset string `yaml:"Preset"`
Align string `yaml:"Align"`
FillColor string `yaml:"FillColor"`
Title string `yaml:"Title"`
TitleColor string `yaml:"TitleColor"`
Font string `yaml:"Font"`
Children []string `yaml:"Children"`
}

type Link struct {
Expand Down Expand Up @@ -151,7 +156,16 @@ func main() {
resources[k].SetBorderColor(stringToColor(border.Color))
}
if label := def.Label; label != nil {
resources[k].SetLabel(label.Title, stringToColor(label.Color))
if label.Title != "" {
resources[k].SetLabel(&label.Title, nil, nil)
}
if label.Color != "" {
c := stringToColor(label.Color)
resources[k].SetLabel(nil, &c, nil)
}
if label.Font != "" {
resources[k].SetLabel(nil, nil, &label.Font)
}
}
if icon := def.Icon; icon != nil {
if def.CacheFilePath == "" {
Expand All @@ -177,7 +191,17 @@ func main() {
resources[k].SetBorderColor(stringToColor(border.Color))
}
if label := def.Label; label != nil {
resources[k].SetLabel(label.Title, stringToColor(label.Color))
if label.Title != "" {
resources[k].SetLabel(&label.Title, nil, nil)
}
if label.Color != "" {
c := stringToColor(label.Color)
resources[k].SetLabel(nil, &c, nil)
}
if label.Font != "" {
resources[k].SetLabel(nil, nil, &label.Font)
}

}
if icon := def.Icon; icon != nil {
if def.CacheFilePath == "" {
Expand All @@ -190,7 +214,14 @@ func main() {
resources[k].LoadIcon(v.Icon)
}
if v.Title != "" {
resources[k].SetLabel(title, color.RGBA{0, 0, 0, 255})
resources[k].SetLabel(&title, nil, nil)
}
if v.TitleColor != "" {
c := stringToColor(v.TitleColor)
resources[k].SetLabel(nil, &c, nil)
}
if v.Font != "" {
resources[k].SetLabel(nil, nil, &v.Font)
}
if v.Align != "" {
resources[k].SetAlign(v.Align)
Expand Down Expand Up @@ -244,7 +275,7 @@ func main() {
log.Info("Drawing")
resources["Canvas"].Scale()
resources["Canvas"].ZeroAdjust()
img := resources["Canvas"].Draw(nil)
img := resources["Canvas"].Draw(nil, nil)

log.Infof("Save %s\n", *outputfile)
f, _ := os.OpenFile(*outputfile, os.O_WRONLY|os.O_CREATE, 0600)
Expand Down
1 change: 1 addition & 0 deletions internal/definition/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Definition struct {
type DefinitionLabel struct {
Title string `yaml:"Title"`
Color string `yaml:"Color"`
Font string `yaml:"Font"`
}

type DefinitionFill struct {
Expand Down
46 changes: 35 additions & 11 deletions internal/types/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ type Group struct {
borderColor color.RGBA
fillColor color.RGBA
label string
fontColor color.RGBA
labelFont string
labelColor *color.RGBA
width int
height int
margin Margin
Expand All @@ -45,7 +46,8 @@ func (g Group) Init() Node {
gr.borderColor = color.RGBA{0, 0, 0, 0}
gr.fillColor = color.RGBA{0, 0, 0, 0}
gr.label = ""
gr.fontColor = color.RGBA{0, 0, 0, 0}
gr.labelFont = ""
gr.labelColor = nil
gr.width = 320
gr.height = 190
gr.margin = Margin{20, 15, 20, 15}
Expand Down Expand Up @@ -97,9 +99,17 @@ func (g *Group) SetFillColor(fillColor color.RGBA) {
g.fillColor = fillColor
}

func (g *Group) SetLabel(label string, fontColor color.RGBA) {
g.label = label
g.fontColor = fontColor
func (g *Group) SetLabel(label *string, labelColor *color.RGBA, labelFont *string) {

if label != nil {
g.label = *label
}
if labelColor != nil {
g.labelColor = labelColor
}
if labelFont != nil {
g.labelFont = *labelFont
}
}

func (g *Group) SetAlign(align string) {
Expand Down Expand Up @@ -226,7 +236,7 @@ func (g *Group) IsDrawn() bool {
return g.drawn
}

func (g *Group) Draw(img *image.RGBA) *image.RGBA {
func (g *Group) Draw(img *image.RGBA, parent *Group) *image.RGBA {
if img == nil {
img = image.NewRGBA(g.bindings)
}
Expand All @@ -236,10 +246,10 @@ func (g *Group) Draw(img *image.RGBA) *image.RGBA {
rctSrc := g.iconImage.Bounds()
draw.CatmullRom.Scale(img, x, g.iconImage, rctSrc, draw.Over, nil)

g.drawLabel(img)
g.drawLabel(img, parent)

for _, subGroup := range g.children {
subGroup.Draw(img)
subGroup.Draw(img, g)
}
g.drawn = true
for _, v := range g.links {
Expand Down Expand Up @@ -273,11 +283,25 @@ func (g *Group) drawFrame(img *image.RGBA) {
}
}

func (g *Group) drawLabel(img *image.RGBA) {
func (g *Group) drawLabel(img *image.RGBA, parent *Group) {

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

f, err := os.Open(fontPath.Arial)
if g.labelFont == "" {
if parent != nil && parent.labelFont != "" {
g.labelFont = parent.labelFont
} else {
g.labelFont = fontPath.Arial
}
}
if g.labelColor == nil {
if parent != nil && parent.labelColor != nil {
g.labelColor = parent.labelColor
} else {
g.labelColor = &color.RGBA{0, 0, 0, 255}
}
}
f, err := os.Open(g.labelFont)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -312,7 +336,7 @@ func (g *Group) drawLabel(img *image.RGBA) {

d := &font.Drawer{
Dst: img,
Src: image.NewUniform(g.fontColor),
Src: image.NewUniform(g.labelColor),
Face: face,
Dot: point,
}
Expand Down
4 changes: 2 additions & 2 deletions internal/types/horizontal_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type HorizontalStack struct {
borderColor color.RGBA
fillColor color.RGBA
label string
fontColor color.RGBA
labelColor color.RGBA
width int
height int
margin Margin
Expand All @@ -32,7 +32,7 @@ func (v HorizontalStack) Init() Node {
sr.borderColor = color.RGBA{0, 0, 0, 0}
sr.fillColor = color.RGBA{0, 0, 0, 0}
sr.label = ""
sr.fontColor = color.RGBA{0, 0, 0, 0}
sr.labelColor = &color.RGBA{0, 0, 0, 0}
sr.width = 320
sr.height = 190
sr.margin = Margin{0, 0, 0, 0}
Expand Down
42 changes: 32 additions & 10 deletions internal/types/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type Resource struct {
borderColor color.RGBA
fillColor color.RGBA
label string
fontColor color.RGBA
labelFont string
labelColor *color.RGBA
width int
height int
margin Margin
Expand All @@ -41,7 +42,7 @@ func (r Resource) Init() Node {
rr.borderColor = color.RGBA{0, 0, 0, 0}
rr.fillColor = color.RGBA{0, 0, 0, 0}
rr.label = ""
rr.fontColor = color.RGBA{0, 0, 0, 0}
rr.labelColor = &color.RGBA{0, 0, 0, 0}
rr.width = 64
rr.height = 64
rr.margin = Margin{30, 100, 30, 100}
Expand Down Expand Up @@ -89,9 +90,16 @@ func (r *Resource) SetFillColor(fillColor color.RGBA) {
r.fillColor = fillColor
}

func (r *Resource) SetLabel(label string, fontColor color.RGBA) {
r.label = label
r.fontColor = fontColor
func (r *Resource) SetLabel(label *string, labelColor *color.RGBA, labelFont *string) {
if label != nil {
r.label = *label
}
if labelColor != nil {
r.labelColor = labelColor
}
if labelFont != nil {
r.labelFont = *labelFont
}
}

func (r *Resource) SetAlign(align string) {
Expand Down Expand Up @@ -130,7 +138,7 @@ func (r *Resource) IsDrawn() bool {
return r.drawn
}

func (r *Resource) Draw(img *image.RGBA) *image.RGBA {
func (r *Resource) Draw(img *image.RGBA, parent *Group) *image.RGBA {
if img == nil {
img = image.NewRGBA(r.bindings)
}
Expand All @@ -140,7 +148,7 @@ func (r *Resource) Draw(img *image.RGBA) *image.RGBA {

r.drawFrame(img)

r.drawLabel(img)
r.drawLabel(img, parent)

r.drawn = true
for _, v := range r.links {
Expand Down Expand Up @@ -174,9 +182,23 @@ func (r *Resource) drawFrame(img *image.RGBA) {
}
}

func (r *Resource) drawLabel(img *image.RGBA) {
func (r *Resource) drawLabel(img *image.RGBA, parent *Group) {

f, err := os.Open(fontPath.Arial)
if r.labelFont == "" {
if parent != nil && parent.labelFont != "" {
r.labelFont = parent.labelFont
} else {
r.labelFont = fontPath.Arial
}
}
if r.labelColor == nil {
if parent != nil && parent.labelColor != nil {
r.labelColor = parent.labelColor
} else {
r.labelColor = &color.RGBA{0, 0, 0, 255}
}
}
f, err := os.Open(r.labelFont)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -213,7 +235,7 @@ func (r *Resource) drawLabel(img *image.RGBA) {

d := &font.Drawer{
Dst: img,
Src: image.NewUniform(r.fontColor),
Src: image.NewUniform(r.labelColor),
Face: face,
Dot: point,
}
Expand Down
4 changes: 2 additions & 2 deletions internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ type Padding struct {
type Node interface {
Init() Node
IsDrawn() bool
Draw(*image.RGBA) *image.RGBA
Draw(*image.RGBA, *Group) *image.RGBA
Scale()
GetBindings() image.Rectangle
GetMargin() Margin
LoadIcon(string)
SetIconBounds(image.Rectangle)
SetBorderColor(color.RGBA)
SetFillColor(color.RGBA)
SetLabel(string, color.RGBA)
SetLabel(*string, *color.RGBA, *string)
SetAlign(string)
SetDirection(string)
AddLink(*Link)
Expand Down
4 changes: 2 additions & 2 deletions internal/types/vertical_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type VerticalStack struct {
borderColor color.RGBA
fillColor color.RGBA
label string
fontColor color.RGBA
labelColor color.RGBA
width int
height int
margin Margin
Expand All @@ -32,7 +32,7 @@ func (h VerticalStack) Init() Node {
sr.borderColor = color.RGBA{0, 0, 0, 0}
sr.fillColor = color.RGBA{0, 0, 0, 0}
sr.label = ""
sr.fontColor = color.RGBA{0, 0, 0, 0}
sr.labelColor = &color.RGBA{0, 0, 0, 0}
sr.width = 320
sr.height = 190
sr.margin = Margin{0, 0, 0, 0}
Expand Down

0 comments on commit cc6f6ce

Please sign in to comment.