adding style.String()
This commit is contained in:
parent
f661cfc8c1
commit
8edf6a5e8a
2 changed files with 81 additions and 13 deletions
|
@ -172,20 +172,24 @@ func MeasureAnnotation(r Renderer, canvasBox Box, s Style, lx, ly int, label str
|
|||
}
|
||||
|
||||
// DrawAnnotation draws an anotation with a renderer.
|
||||
func DrawAnnotation(r Renderer, canvasBox Box, s Style, lx, ly int, label string) {
|
||||
r.SetFillColor(s.GetFillColor(DefaultAnnotationFillColor))
|
||||
r.SetStrokeColor(s.GetStrokeColor())
|
||||
r.SetStrokeWidth(s.GetStrokeWidth())
|
||||
r.SetStrokeDashArray(s.GetStrokeDashArray())
|
||||
func DrawAnnotation(r Renderer, canvasBox Box, style Style, lx, ly int, label string) {
|
||||
r.SetFillColor(style.GetFillColor(DefaultAnnotationFillColor))
|
||||
r.SetStrokeColor(style.GetStrokeColor())
|
||||
r.SetStrokeWidth(style.GetStrokeWidth())
|
||||
r.SetStrokeDashArray(style.GetStrokeDashArray())
|
||||
|
||||
r.SetFont(style.GetFont())
|
||||
r.SetFontColor(style.GetFontColor(DefaultTextColor))
|
||||
r.SetFontSize(style.GetFontSize(DefaultAnnotationFontSize))
|
||||
|
||||
textBox := r.MeasureText(label)
|
||||
textWidth := textBox.Width()
|
||||
halfTextHeight := textBox.Height() >> 1
|
||||
|
||||
pt := s.Padding.GetTop(DefaultAnnotationPadding.Top)
|
||||
pl := s.Padding.GetLeft(DefaultAnnotationPadding.Left)
|
||||
pr := s.Padding.GetRight(DefaultAnnotationPadding.Right)
|
||||
pb := s.Padding.GetBottom(DefaultAnnotationPadding.Bottom)
|
||||
pt := style.Padding.GetTop(DefaultAnnotationPadding.Top)
|
||||
pl := style.Padding.GetLeft(DefaultAnnotationPadding.Left)
|
||||
pr := style.Padding.GetRight(DefaultAnnotationPadding.Right)
|
||||
pb := style.Padding.GetBottom(DefaultAnnotationPadding.Bottom)
|
||||
|
||||
textX := lx + pl + DefaultAnnotationDeltaWidth
|
||||
textY := ly + halfTextHeight
|
||||
|
@ -211,10 +215,6 @@ func DrawAnnotation(r Renderer, canvasBox Box, s Style, lx, ly int, label string
|
|||
r.Close()
|
||||
r.FillStroke()
|
||||
|
||||
r.SetFont(s.GetFont())
|
||||
r.SetFontColor(s.GetFontColor(DefaultTextColor))
|
||||
r.SetFontSize(s.GetFontSize(DefaultAnnotationFontSize))
|
||||
|
||||
r.Text(label, textX, textY)
|
||||
}
|
||||
|
||||
|
|
68
style.go
68
style.go
|
@ -28,6 +28,74 @@ func (s Style) IsZero() bool {
|
|||
return s.StrokeColor.IsZero() && s.FillColor.IsZero() && s.StrokeWidth == 0 && s.FontColor.IsZero() && s.FontSize == 0 && s.Font == nil
|
||||
}
|
||||
|
||||
func (s Style) String() string {
|
||||
if s.IsZero() {
|
||||
return "{}"
|
||||
}
|
||||
|
||||
var output []string
|
||||
if s.Show {
|
||||
output = []string{"\"show\": true"}
|
||||
} else {
|
||||
output = []string{"\"show\": false"}
|
||||
}
|
||||
|
||||
if !s.Padding.IsZero() {
|
||||
output = append(output, fmt.Sprintf("\"padding\": %s", s.Padding.String()))
|
||||
} else {
|
||||
output = append(output, "\"padding\": null")
|
||||
}
|
||||
|
||||
if s.StrokeWidth >= 0 {
|
||||
output = append(output, fmt.Sprintf("\"stroke_width\": %0.2f", s.StrokeWidth))
|
||||
} else {
|
||||
output = append(output, "\"stroke_width\": null")
|
||||
}
|
||||
|
||||
if !s.StrokeColor.IsZero() {
|
||||
output = append(output, fmt.Sprintf("\"stroke_color\": %s", s.StrokeColor.String()))
|
||||
} else {
|
||||
output = append(output, "\"stroke_color\": null")
|
||||
}
|
||||
|
||||
if len(s.StrokeDashArray) > 0 {
|
||||
var elements []string
|
||||
for _, v := range s.StrokeDashArray {
|
||||
elements = append(elements, fmt.Sprintf("%.2f", v))
|
||||
}
|
||||
dashArray := strings.Join(elements, ", ")
|
||||
output = append(output, fmt.Sprintf("\"stroke_dash_array\": [%s]", dashArray))
|
||||
} else {
|
||||
output = append(output, "\"stroke_dash_array\": null")
|
||||
}
|
||||
|
||||
if !s.FillColor.IsZero() {
|
||||
output = append(output, fmt.Sprintf("\"fill_color\": %s", s.FillColor.String()))
|
||||
} else {
|
||||
output = append(output, "\"fill_color\": null")
|
||||
}
|
||||
|
||||
if s.FontSize != 0 {
|
||||
output = append(output, fmt.Sprintf("\"font_size\": \"%0.2fpt\"", s.FontSize))
|
||||
} else {
|
||||
output = append(output, "\"fill_color\": null")
|
||||
}
|
||||
|
||||
if !s.FillColor.IsZero() {
|
||||
output = append(output, fmt.Sprintf("\"font_color\": %s", s.FillColor.String()))
|
||||
} else {
|
||||
output = append(output, "\"font_color\": null")
|
||||
}
|
||||
|
||||
if s.Font != nil {
|
||||
output = append(output, fmt.Sprintf("\"font\": \"%s\"", s.Font.Name(truetype.NameIDFontFamily)))
|
||||
} else {
|
||||
output = append(output, "\"font_color\": null")
|
||||
}
|
||||
|
||||
return "{" + strings.Join(output, ", ") + "}"
|
||||
}
|
||||
|
||||
// GetStrokeColor returns the stroke color.
|
||||
func (s Style) GetStrokeColor(defaults ...drawing.Color) drawing.Color {
|
||||
if s.StrokeColor.IsZero() {
|
||||
|
|
Loading…
Reference in a new issue