// Package badge provides methods for generating SVG badges package badge // ////////////////////////////////////////////////////////////////////////////////// // // // // Copyright (c) 2021 ESSENTIAL KAOS // // Apache License, Version 2.0 // // // // ////////////////////////////////////////////////////////////////////////////////// // import ( "io/ioutil" "strconv" "strings" "github.com/golang/freetype/truetype" "golang.org/x/image/font" ) // ////////////////////////////////////////////////////////////////////////////////// // 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 font offset DEFAULT_SPACING = 0 // default letter spacing ) // ////////////////////////////////////////////////////////////////////////////////// // const _TEMPLATE_PLASTIC = `{LABEL}: {MESSAGE}{LABEL}{MESSAGE}` const _TEMPLATE_FLAT = `{LABEL}: {MESSAGE}{LABEL}{MESSAGE}` const _TEMPLATE_FLAT_SQUARE = `{LABEL}: {MESSAGE}{LABEL}{MESSAGE}` // ////////////////////////////////////////////////////////////////////////////////// // // Generator is badge generator type Generator struct { Offset int // Text offset Spacing float64 // Letter spacing fontSize int fontName string drawer *font.Drawer } // ////////////////////////////////////////////////////////////////////////////////// // // NewGenerator creates new badge generator with given font func NewGenerator(fontFile string, fontSize int) (*Generator, error) { fontData, err := ioutil.ReadFile(fontFile) if err != nil { return nil, err } fontTTF, err := truetype.Parse(fontData) if err != nil { return nil, err } return &Generator{ Offset: DEFAULT_OFFSET, Spacing: DEFAULT_SPACING, fontSize: fontSize, fontName: fontTTF.Name(truetype.NameIDFontFullName), drawer: &font.Drawer{ Face: truetype.NewFace(fontTTF, &truetype.Options{ Size: float64(fontSize), DPI: 72, Hinting: font.HintingFull, }), }, }, nil } // ////////////////////////////////////////////////////////////////////////////////// // // GeneratePlastic generates SVG badge in plastic style 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) []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) []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) []byte { if !strings.HasPrefix(color, "#") { color = "#" + color } lW := int(g.drawer.MeasureString(label)>>6) + g.Offset mW := int(g.drawer.MeasureString(message)>>6) + g.Offset fW := lW + mW lX := ((lW/2 + 1) * 10) + 5 mX := ((lW + (mW / 2) - 1) * 10) + 5 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) badge = strings.ReplaceAll(badge, "{COLOR}", color) badge = strings.ReplaceAll(badge, "{WIDTH}", strconv.Itoa(fW)) badge = strings.ReplaceAll(badge, "{LABEL_WIDTH}", strconv.Itoa(lW)) badge = strings.ReplaceAll(badge, "{MESSAGE_WIDTH}", strconv.Itoa(mW)) badge = strings.ReplaceAll(badge, "{LABEL_X}", strconv.Itoa(lX)) 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 []byte(badge) }