From 8edf6a5e8aca02e9fb4436f89fe8b1cf7cbcff4b Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Mon, 18 Jul 2016 11:43:41 -0700 Subject: [PATCH] adding style.String() --- drawing_helpers.go | 26 +++++++++--------- style.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 13 deletions(-) diff --git a/drawing_helpers.go b/drawing_helpers.go index 05e94ef..f12294b 100644 --- a/drawing_helpers.go +++ b/drawing_helpers.go @@ -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) } diff --git a/style.go b/style.go index 7c5e2f2..dbfb4fb 100644 --- a/style.go +++ b/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() {