Fancy opengraph
parent
8ceb78caad
commit
a990c55476
Binary file not shown.
@ -0,0 +1,73 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"embed"
|
||||
"image/color"
|
||||
"strings"
|
||||
|
||||
"github.com/jbuchbinder/gg"
|
||||
"github.com/jiro4989/textimg/v3/token"
|
||||
)
|
||||
|
||||
//go:embed fonts
|
||||
var fonts embed.FS
|
||||
|
||||
func Draw(tokens token.Tokens) ([]byte, error) {
|
||||
foreground := color.RGBA{205, 214, 244, 255}
|
||||
background := color.RGBA{30, 30, 46, 255}
|
||||
|
||||
dc := gg.NewContext(1200, 630)
|
||||
fgCol := foreground
|
||||
bgCol := background
|
||||
dc.SetColor(bgCol)
|
||||
if err := dc.LoadFontFaceFS(fonts, "fonts/JetBrainsMono-Regular.ttf", 20); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dc.Clear()
|
||||
curX, curY := 50.0, 50.0
|
||||
|
||||
for _, t := range tokens {
|
||||
switch t.Kind {
|
||||
case token.KindColor:
|
||||
switch t.ColorType {
|
||||
case token.ColorTypeReset:
|
||||
fgCol = foreground
|
||||
bgCol = background
|
||||
case token.ColorTypeResetForeground:
|
||||
fgCol = foreground
|
||||
case token.ColorTypeResetBackground:
|
||||
bgCol = background
|
||||
case token.ColorTypeReverse:
|
||||
fgCol, bgCol = bgCol, fgCol
|
||||
case token.ColorTypeForeground:
|
||||
fgCol = color.RGBA(t.Color)
|
||||
case token.ColorTypeBackground:
|
||||
bgCol = color.RGBA(t.Color)
|
||||
}
|
||||
case token.KindText:
|
||||
w, h := dc.MeasureMultilineString(t.Text, 1.0)
|
||||
dc.Push()
|
||||
dc.SetColor(bgCol)
|
||||
dc.DrawRectangle(curX, curY, w, h)
|
||||
dc.Fill()
|
||||
dc.Pop()
|
||||
dc.SetColor(fgCol)
|
||||
dc.DrawStringAnchored(strings.ReplaceAll(strings.ReplaceAll(t.Text, "\t", " "), "\n", ""), curX, curY, 0.0, 1.0)
|
||||
curX += w
|
||||
if strings.Contains(t.Text, "\n") {
|
||||
curY += h
|
||||
curX = 50
|
||||
}
|
||||
}
|
||||
}
|
||||
dc.Push()
|
||||
dc.SetColor(background)
|
||||
dc.DrawRectangle(1150, 0, 50, 630)
|
||||
dc.DrawRectangle(0, 580, 1200, 50)
|
||||
dc.Fill()
|
||||
dc.Pop()
|
||||
buffer := new(bytes.Buffer)
|
||||
dc.EncodePNG(buffer)
|
||||
return buffer.Bytes(), nil
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,74 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/jiro4989/textimg/v3/color"
|
||||
"github.com/jiro4989/textimg/v3/token"
|
||||
)
|
||||
|
||||
type ParserFunc struct {
|
||||
// pegが生成するTokensと名前が衝突するので別名にする
|
||||
Tk token.Tokens
|
||||
}
|
||||
|
||||
func Parse(s string) (token.Tokens, error) {
|
||||
p := &Parser{Buffer: s}
|
||||
if err := p.Init(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := p.Parse(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p.Execute()
|
||||
return p.Tk, nil
|
||||
}
|
||||
|
||||
func (p *ParserFunc) pushResetColor() {
|
||||
p.Tk = append(p.Tk, token.NewResetColor())
|
||||
}
|
||||
|
||||
func (p *ParserFunc) pushResetForegroundColor() {
|
||||
p.Tk = append(p.Tk, token.NewResetForegroundColor())
|
||||
}
|
||||
|
||||
func (p *ParserFunc) pushResetBackgroundColor() {
|
||||
p.Tk = append(p.Tk, token.NewResetBackgroundColor())
|
||||
}
|
||||
|
||||
func (p *ParserFunc) pushReverseColor() {
|
||||
p.Tk = append(p.Tk, token.NewReverseColor())
|
||||
}
|
||||
|
||||
func (p *ParserFunc) pushText(text string) {
|
||||
p.Tk = append(p.Tk, token.NewText(text))
|
||||
}
|
||||
|
||||
func (p *ParserFunc) pushStandardColorWithCategory(text string) {
|
||||
p.Tk = append(p.Tk, token.NewStandardColorWithCategory(text))
|
||||
}
|
||||
|
||||
func (p *ParserFunc) pushExtendedColor(text string) {
|
||||
p.Tk = append(p.Tk, token.NewExtendedColor(text))
|
||||
}
|
||||
|
||||
func (p *ParserFunc) setExtendedColor256(text string) {
|
||||
n, _ := strconv.ParseUint(text, 10, 8)
|
||||
p.Tk[len(p.Tk)-1].Color = color.Map256[int(n)]
|
||||
}
|
||||
|
||||
func (p *ParserFunc) setExtendedColorR(text string) {
|
||||
n, _ := strconv.ParseUint(text, 10, 8)
|
||||
p.Tk[len(p.Tk)-1].Color.R = uint8(n)
|
||||
}
|
||||
|
||||
func (p *ParserFunc) setExtendedColorG(text string) {
|
||||
n, _ := strconv.ParseUint(text, 10, 8)
|
||||
p.Tk[len(p.Tk)-1].Color.G = uint8(n)
|
||||
}
|
||||
|
||||
func (p *ParserFunc) setExtendedColorB(text string) {
|
||||
n, _ := strconv.ParseUint(text, 10, 8)
|
||||
p.Tk[len(p.Tk)-1].Color.B = uint8(n)
|
||||
}
|
Loading…
Reference in New Issue