badge

package module
v0.0.0-...-93ee2c9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2018 License: MIT Imports: 10 Imported by: 0

README

go-badge GoDoc

go-badge is a library to render shield badges to SVG.

Installation

Using go get

go get github.com/glaslos/go-badge

Usage

import (
	"os"

	"github.com/glaslos/go-badge"
	"github.com/valyala/fasttemplate"
)

func main() {
	tmpl := fasttemplate.New(badge.FlatTemplate, "{{", "}}")
	fd, _ := badge.NewFace(11, 72, "fonts/vera.ttf")
	println(badge.Render("godoc", "reference", "#5272B4", fd, tmpl))
}

License

MIT

Documentation

Index

Constants

View Source
const (
	ColorBrightgreen = Color("brightgreen")
	ColorGreen       = Color("green")
	ColorYellow      = Color("yellow")
	ColorYellowgreen = Color("yellowgreen")
	ColorOrange      = Color("orange")
	ColorRed         = Color("red")
	ColorBlue        = Color("blue")
	ColorGrey        = Color("grey")
	ColorGray        = Color("gray")
	ColorLightgrey   = Color("lightgrey")
	ColorLightgray   = Color("lightgray")
)

Standard colors.

Variables

View Source
var ColorScheme = map[string]string{
	"brightgreen": "#4c1",
	"green":       "#97ca00",
	"yellow":      "#dfb317",
	"yellowgreen": "#a4a61d",
	"orange":      "#fe7d37",
	"red":         "#e05d44",
	"blue":        "#007ec6",
	"grey":        "#555",
	"gray":        "#555",
	"lightgrey":   "#9f9f9f",
	"lightgray":   "#9f9f9f",
}

ColorScheme contains named colors that could be used to render the badge.

View Source
var FlatTemplate = strings.TrimSpace(`
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{{dx}}" height="20">
  <linearGradient id="s" x2="0" y2="100%">
    <stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
    <stop offset="1" stop-opacity=".1"/>
  </linearGradient>

  <mask id="r">
    <rect width="{{dx}}" height="20" rx="3" fill="#fff"/>
  </mask>

  <g mask="url(#r)">
    <rect width="{{subjectDx}}" height="20" fill="#555"/>
    <rect x="{{subjectDx}}" width="{{statusDx}}" height="20" fill="{{color}}"/>
    <rect width="{{dx}}" height="20" fill="url(#s)"/>
  </g>

  <g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
    <text x="{{subjectX}}" y="15" fill="#010101" fill-opacity=".3">{{subject}}</text>
    <text x="{{subjectX}}" y="14">{{subject}}</text>
    <text x="{{statusX}}" y="15" fill="#010101" fill-opacity=".3">{{status}}</text>
    <text x="{{statusX}}" y="14">{{status}}</text>
  </g>
</svg>
`)

Functions

func NewFace

func NewFace(size, dpi float64, fontPath string) (face font.Face, err error)

NewFace creates a new face based on font, size and dpi

func NewFaceStream

func NewFaceStream(size, dpi float64, raw []byte) (face font.Face, err error)

NewFaceStream creates a new face based on font bytes, size and dpi

func Render

func Render(subject, status string, color Color, fd font.Face, tmpl *fasttemplate.Template) (svg string, err error)

Render renders a badge of the given color, with given subject and status to w.

func RenderDef

func RenderDef(subject, status string, color Color) (svg string, err error)

Types

type Color

type Color string

Color represents color of the badge.

func (Color) String

func (c Color) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL