From 85c30b8852fc9f995e99d6f668bdb4ca1a97d194 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Fri, 13 Aug 2021 23:58:12 +0300 Subject: [PATCH 1/2] Improvements --- README.md | 15 +++++++++--- badge.go | 66 +++++++++++++++++++++++++++++++++++++++------------ badge_test.go | 12 +++++----- 3 files changed, 69 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 701eb72..d2e19d2 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,14 @@

PkgGoDev GitHub Actions CI Status + GoReportCard Coverage Status Codebeat badge GitHub Actions CodeQL Status

-

InstallationUsage exampleBuild StatusContributingLicense

+

InstallationUsage exampleBuild StatusContributingThanksLicense


@@ -44,13 +45,13 @@ import ( // ////////////////////////////////////////////////////////////////////////// // func main() { - g, err := badge.NewGenerator("Verdana.ttf") + g, err := badge.NewGenerator("Verdana.ttf", 11) if err != nil { panic(err) } - fmt.Println(g.GeneratePlastic("status", "ok", "#97ca00")) + fmt.Println(string(g.GeneratePlastic("status", "ok", "#97ca00"))) } ``` @@ -65,6 +66,14 @@ func main() { Before contributing to this project please read our [Contributing Guidelines](https://github.com/essentialkaos/contributing-guidelines#contributing-guidelines). +### Thanks + +We would like to thank: + +* All authors and [contributors](https://github.com/badges/shields/graphs/contributors) of [shields.io](https://shields.io) service; +* All authors of [`freetype`](https://github.com/golang/freetype/blob/master/AUTHORS) package; +* [@narqo](https://github.com/narqo) for [`go-badge`](https://github.com/narqo/go-badge) package. + ### License [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) diff --git a/badge.go b/badge.go index 2baacd7..cc893e2 100644 --- a/badge.go +++ b/badge.go @@ -20,23 +20,51 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // -const _TEMPLATE_PLASTIC = `{LABEL}: {MESSAGE}{LABEL}{MESSAGE}` +const ( + COLOR_BLUE = "#007ec6" + COLOR_BRIGHTGREEN = "#4c1" + COLOR_GREEN = "#97ca00" + COLOR_GREY = "#555" + COLOR_LIGHTGREY = "#9f9f9f" + COLOR_ORANGE = "#fe7d37" + COLOR_RED = "#e05d44" + COLOR_YELLOW = "#dfb317" + COLOR_YELLOWGREEN = "#a4a61d" + + COLOR_SUCCESS = "#4c1" + COLOR_IMPORTANT = "#fe7d37" + COLOR_CRITICAL = "#e05d44" + COLOR_INFORMATIONAL = "#007ec6" + COLOR_INACTIVE = "#9f9f9f" +) + +const ( + DEFAULT_OFFSET = 9 + DEFAULT_SPACING = 0 +) + +// ////////////////////////////////////////////////////////////////////////////////// // + +const _TEMPLATE_PLASTIC = `{LABEL}: {MESSAGE}{LABEL}{MESSAGE}` -const _TEMPLATE_FLAT = `{LABEL}: {MESSAGE}{LABEL}{MESSAGE}` +const _TEMPLATE_FLAT = `{LABEL}: {MESSAGE}{LABEL}{MESSAGE}` -const _TEMPLATE_FLAT_SQUARE = `{LABEL}: {MESSAGE}{LABEL}{MESSAGE}` +const _TEMPLATE_FLAT_SQUARE = `{LABEL}: {MESSAGE}{LABEL}{MESSAGE}` // ////////////////////////////////////////////////////////////////////////////////// // type Generator struct { - Offset int + Offset int // Text offset + Spacing float64 // Text spacing - drawer *font.Drawer + fontSize int + fontName string + drawer *font.Drawer } // ////////////////////////////////////////////////////////////////////////////////// // -func NewGenerator(fontFile string) (*Generator, error) { +func NewGenerator(fontFile string, fontSize int) (*Generator, error) { fontData, err := ioutil.ReadFile(fontFile) if err != nil { @@ -50,10 +78,15 @@ func NewGenerator(fontFile string) (*Generator, error) { } return &Generator{ - Offset: 9, + Offset: DEFAULT_OFFSET, + Spacing: DEFAULT_SPACING, + + fontSize: fontSize, + + fontName: fontTTF.Name(truetype.NameIDFontFullName), drawer: &font.Drawer{ Face: truetype.NewFace(fontTTF, &truetype.Options{ - Size: 11, + Size: float64(fontSize), DPI: 72, Hinting: font.HintingFull, }), @@ -64,24 +97,24 @@ func NewGenerator(fontFile string) (*Generator, error) { // ////////////////////////////////////////////////////////////////////////////////// // // GeneratePlastic generates SVG badge in plastic style -func (g *Generator) GeneratePlastic(label, message, color string) string { +func (g *Generator) GeneratePlastic(label, message, color string) []byte { return g.generateBadge(_TEMPLATE_PLASTIC, label, message, color) } // GenerateFlat generates SVG badge in flat style -func (g *Generator) GenerateFlat(label, message, color string) string { +func (g *Generator) GenerateFlat(label, message, color string) []byte { return g.generateBadge(_TEMPLATE_FLAT, label, message, color) } // GenerateFlatSquare generates SVG badge in flat-square style -func (g *Generator) GenerateFlatSquare(label, message, color string) string { +func (g *Generator) GenerateFlatSquare(label, message, color string) []byte { return g.generateBadge(_TEMPLATE_FLAT_SQUARE, label, message, color) } // ////////////////////////////////////////////////////////////////////////////////// // // generateBadge generates badge with given template -func (g *Generator) generateBadge(template, label, message, color string) string { +func (g *Generator) generateBadge(template, label, message, color string) []byte { if !strings.HasPrefix(color, "#") { color = "#" + color } @@ -91,8 +124,9 @@ func (g *Generator) generateBadge(template, label, message, color string) string fW := lW + mW lX := ((lW/2 + 1) * 10) + 5 mX := ((lW + (mW / 2) - 1) * 10) + 5 - lL := (lW - 10) * 10 - mL := (mW - 10) * 10 + lL := int(float64(lW-10) * (10.0 + g.Spacing)) + mL := int(float64(mW-10) * (10.0 + g.Spacing)) + fS := g.fontSize * 10 badge := strings.ReplaceAll(template, "{LABEL}", label) badge = strings.ReplaceAll(badge, "{MESSAGE}", message) @@ -104,6 +138,8 @@ func (g *Generator) generateBadge(template, label, message, color string) string badge = strings.ReplaceAll(badge, "{MESSAGE_X}", strconv.Itoa(mX)) badge = strings.ReplaceAll(badge, "{LABEL_LENGTH}", strconv.Itoa(lL)) badge = strings.ReplaceAll(badge, "{MESSAGE_LENGTH}", strconv.Itoa(mL)) + badge = strings.ReplaceAll(badge, "{FONT}", g.fontName) + badge = strings.ReplaceAll(badge, "{FONT_SIZE}", strconv.Itoa(fS)) - return badge + return []byte(badge) } diff --git a/badge_test.go b/badge_test.go index 1350cc0..8b8a8b6 100644 --- a/badge_test.go +++ b/badge_test.go @@ -30,7 +30,7 @@ var _ = Suite(&BadgeSuite{}) func (s *BadgeSuite) SetUpTest(c *C) { var err error - s.generator, err = NewGenerator("Verdana.ttf") + s.generator, err = NewGenerator("Verdana.ttf", 11) if err != nil { c.Fatal(err.Error()) @@ -38,11 +38,11 @@ func (s *BadgeSuite) SetUpTest(c *C) { } func (s *BadgeSuite) TestErrors(c *C) { - _, err := NewGenerator("unknown.ttf") + _, err := NewGenerator("unknown.ttf", 0) c.Assert(err, NotNil) - _, err = NewGenerator("badge.go") + _, err = NewGenerator("badge.go", 0) c.Assert(err, NotNil) } @@ -56,7 +56,7 @@ func (s *BadgeSuite) TestPlastic(c *C) { ourBadge := s.generator.GeneratePlastic("test1", "good", "ff69b4") - c.Assert(ourBadge, Equals, string(srcBadge)) + c.Assert(ourBadge, DeepEquals, srcBadge) } func (s *BadgeSuite) TestFlat(c *C) { @@ -68,7 +68,7 @@ func (s *BadgeSuite) TestFlat(c *C) { ourBadge := s.generator.GenerateFlat("test1", "good", "ff69b4") - c.Assert(ourBadge, Equals, string(srcBadge)) + c.Assert(ourBadge, DeepEquals, srcBadge) } func (s *BadgeSuite) TestFlatSquare(c *C) { @@ -80,5 +80,5 @@ func (s *BadgeSuite) TestFlatSquare(c *C) { ourBadge := s.generator.GenerateFlatSquare("test1", "good", "ff69b4") - c.Assert(ourBadge, Equals, string(srcBadge)) + c.Assert(ourBadge, DeepEquals, srcBadge) } From dc978ab0f59d8831bd7bd915b0856dea1f68e8af Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Sat, 14 Aug 2021 00:09:16 +0300 Subject: [PATCH 2/2] Improvements --- badge.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/badge.go b/badge.go index cc893e2..adca173 100644 --- a/badge.go +++ b/badge.go @@ -39,8 +39,8 @@ const ( ) const ( - DEFAULT_OFFSET = 9 - DEFAULT_SPACING = 0 + DEFAULT_OFFSET = 9 // default font offset + DEFAULT_SPACING = 0 // default letter spacing ) // ////////////////////////////////////////////////////////////////////////////////// // @@ -53,9 +53,10 @@ const _TEMPLATE_FLAT_SQUARE = `