-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgoshot.go
More file actions
107 lines (94 loc) · 2.73 KB
/
Copy pathgoshot.go
File metadata and controls
107 lines (94 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Package goshot composes beautiful screenshots of code and terminal
// output: content is rendered to an image, wrapped in window chrome, and
// placed on a backdrop.
//
// img, err := goshot.New().
// WithContent(code.New(source).WithTheme("dracula")).
// WithChrome(chrome.Mac().WithTitle("main.go").Dark()).
// WithBackground(background.Solid(color.RGBA{30, 30, 46, 255})).
// Image()
package goshot
import (
"fmt"
"image"
"image/jpeg"
"image/png"
"io"
"os"
"path/filepath"
"strings"
"golang.org/x/image/bmp"
)
// Content produces the innermost image — highlighted code, terminal
// output, or anything else.
type Content interface {
Render() (image.Image, error)
}
// Layer wraps an image in additional imagery. Window chrome and
// backgrounds are layers.
type Layer interface {
Render(image.Image) (image.Image, error)
}
// Canvas is a render pipeline: content, then chrome, then background.
// Every part is optional, but at least one must be set.
type Canvas struct {
content Content
chrome Layer
background Layer
}
// New creates an empty canvas.
func New() *Canvas { return &Canvas{} }
// WithContent sets the content to render.
func (c *Canvas) WithContent(content Content) *Canvas { c.content = content; return c }
// WithChrome wraps the content in window decorations.
func (c *Canvas) WithChrome(l Layer) *Canvas { c.chrome = l; return c }
// WithBackground places the result on a backdrop.
func (c *Canvas) WithBackground(l Layer) *Canvas { c.background = l; return c }
// Image renders the full pipeline.
func (c *Canvas) Image() (image.Image, error) {
if c.content == nil && c.chrome == nil && c.background == nil {
return nil, fmt.Errorf("goshot: canvas is empty")
}
var img image.Image
var err error
if c.content != nil {
if img, err = c.content.Render(); err != nil {
return nil, err
}
}
for _, layer := range []Layer{c.chrome, c.background} {
if layer == nil {
continue
}
if img, err = layer.Render(img); err != nil {
return nil, err
}
}
return img, nil
}
// Save renders the canvas and writes it to path, picking the format from
// the file extension (.png, .jpg/.jpeg, or .bmp).
func (c *Canvas) Save(path string) error {
img, err := c.Image()
if err != nil {
return err
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return Encode(f, img, strings.TrimPrefix(filepath.Ext(path), "."))
}
// Encode writes an image in the named format (png, jpg/jpeg, or bmp).
func Encode(w io.Writer, img image.Image, format string) error {
switch strings.ToLower(format) {
case "png", "":
return png.Encode(w, img)
case "jpg", "jpeg":
return jpeg.Encode(w, img, nil)
case "bmp":
return bmp.Encode(w, img)
}
return fmt.Errorf("goshot: unsupported format %q", format)
}