adding style.String()

This commit is contained in:
Will Charczuk 2016-07-18 11:43:41 -07:00
parent f661cfc8c1
commit 8edf6a5e8a
2 changed files with 81 additions and 13 deletions

View File

@ -172,20 +172,24 @@ func MeasureAnnotation(r Renderer, canvasBox Box, s Style, lx, ly int, label str
} }
// DrawAnnotation draws an anotation with a renderer. // DrawAnnotation draws an anotation with a renderer.
func DrawAnnotation(r Renderer, canvasBox Box, s Style, lx, ly int, label string) { func DrawAnnotation(r Renderer, canvasBox Box, style Style, lx, ly int, label string) {
r.SetFillColor(s.GetFillColor(DefaultAnnotationFillColor)) r.SetFillColor(style.GetFillColor(DefaultAnnotationFillColor))
r.SetStrokeColor(s.GetStrokeColor()) r.SetStrokeColor(style.GetStrokeColor())
r.SetStrokeWidth(s.GetStrokeWidth()) r.SetStrokeWidth(style.GetStrokeWidth())
r.SetStrokeDashArray(s.GetStrokeDashArray()) r.SetStrokeDashArray(style.GetStrokeDashArray())
r.SetFont(style.GetFont())
r.SetFontColor(style.GetFontColor(DefaultTextColor))
r.SetFontSize(style.GetFontSize(DefaultAnnotationFontSize))
textBox := r.MeasureText(label) textBox := r.MeasureText(label)
textWidth := textBox.Width() textWidth := textBox.Width()
halfTextHeight := textBox.Height() >> 1 halfTextHeight := textBox.Height() >> 1
pt := s.Padding.GetTop(DefaultAnnotationPadding.Top) pt := style.Padding.GetTop(DefaultAnnotationPadding.Top)
pl := s.Padding.GetLeft(DefaultAnnotationPadding.Left) pl := style.Padding.GetLeft(DefaultAnnotationPadding.Left)
pr := s.Padding.GetRight(DefaultAnnotationPadding.Right) pr := style.Padding.GetRight(DefaultAnnotationPadding.Right)
pb := s.Padding.GetBottom(DefaultAnnotationPadding.Bottom) pb := style.Padding.GetBottom(DefaultAnnotationPadding.Bottom)
textX := lx + pl + DefaultAnnotationDeltaWidth textX := lx + pl + DefaultAnnotationDeltaWidth
textY := ly + halfTextHeight textY := ly + halfTextHeight
@ -211,10 +215,6 @@ func DrawAnnotation(r Renderer, canvasBox Box, s Style, lx, ly int, label string
r.Close() r.Close()
r.FillStroke() r.FillStroke()
r.SetFont(s.GetFont())
r.SetFontColor(s.GetFontColor(DefaultTextColor))
r.SetFontSize(s.GetFontSize(DefaultAnnotationFontSize))
r.Text(label, textX, textY) r.Text(label, textX, textY)
} }

View File

@ -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 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. // GetStrokeColor returns the stroke color.
func (s Style) GetStrokeColor(defaults ...drawing.Color) drawing.Color { func (s Style) GetStrokeColor(defaults ...drawing.Color) drawing.Color {
if s.StrokeColor.IsZero() { if s.StrokeColor.IsZero() {