-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
180 lines (138 loc) · 3.82 KB
/
context.go
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package prism
import (
_ "embed"
"fmt"
"io"
"strings"
"github.com/fogleman/gg"
"github.com/mrmarble/prism/themer"
"github.com/mrmarble/prism/tokenizer"
"golang.org/x/image/font"
)
type Context struct {
points float64
lineSpacing float64
margin int
vMargin float64
hMargin float64
width int
height int
lines int
font font.Face
theme themer.Theme
lang tokenizer.Language
}
//go:embed fonts/FiraCode-Regular.ttf
var firaCode []byte
func NewContext() *Context {
points := 46.0
margin := 50
lineSpacing := 1.25
font, _ := parseFontFace(firaCode, points) //nolint
return &Context{points: points, margin: margin, lineSpacing: lineSpacing, font: font, theme: themer.Dark()}
}
func (ctx *Context) SetFontFace(path string, points float64) error {
font, err := loadFontFace(path, points)
if err != nil {
return err
}
ctx.font = font
ctx.points = points
return nil
}
func (ctx *Context) SetTheme(theme themer.Theme) {
ctx.theme = theme
}
func (ctx *Context) SetLanguage(lang tokenizer.Language) {
ctx.lang = lang
}
func (ctx *Context) EncodePNG(code string, writer io.Writer, options Options) error {
dc := ctx.parse(code, options)
return dc.EncodePNG(writer)
}
func (ctx *Context) SavePNG(code string, output string, options Options) error {
dc := ctx.parse(code, options)
return dc.SavePNG(output)
}
func (ctx *Context) calculateSize(code string, options Options) {
codeWidth, codeHeight := ctx.measureMultilineString(code)
ctx.hMargin = float64(ctx.margin)
ctx.vMargin = ctx.hMargin
if options.LineNumbers {
ctx.hMargin += ctx.measureString(fmt.Sprint(ctx.lines)) + (ctx.hMargin / 2) //nolint
}
if options.Header {
ctx.vMargin += 70
}
ctx.width = int(codeWidth) + ctx.margin + int(ctx.hMargin)
ctx.height = int(codeHeight) + ctx.margin + int(ctx.vMargin)
}
func (ctx *Context) calculateLines(code string, options Options) string {
ctx.lines = strings.Count(code, "\n") + 1
if (Range{}) != options.Range {
var err error
code, err = substr(code, options.Range)
if err != nil {
panic(err)
}
if options.Relative {
ctx.lines = 1 + options.Range.End + -options.Range.Start
} else {
ctx.lines = strings.Count(code, "\n") + 1
}
}
return code
}
func (ctx *Context) parse(code string, options Options) *gg.Context {
code = removeAccents(code)
code = ctx.calculateLines(code, options)
ctx.calculateSize(code, options)
// gg
dc := gg.NewContext(ctx.width, ctx.height)
// background
dc.SetHexColor(ctx.theme["body"])
dc.Clear()
// base color
dc.SetHexColor(ctx.theme[tokenizer.COMMENT])
// font
dc.SetFontFace(ctx.font)
options.Apply(dc, ctx)
// tokens
tokens := tokenizer.Tokenize(code, ctx.lang)
runeWidth := ctx.measureString(" ") // rethink this for variable-width fonts
for _, token := range tokens {
color := ctx.theme.GetColor(token)
dc.SetHexColor(color)
x := ctx.hMargin + float64(token.Col)*runeWidth
y := ctx.vMargin + ctx.points + ((ctx.points * ctx.lineSpacing) * float64(token.Line))
dc.DrawString(token.Content, x, y)
}
return dc
}
func (ctx *Context) measureMultilineString(s string) (width, height float64) {
lines := strings.Split(s, "\n")
// sync h formula with DrawStringWrapped
height = float64(len(lines)) * ctx.points * ctx.lineSpacing
height -= (ctx.lineSpacing - 1) * ctx.points
d := &font.Drawer{
Face: ctx.font,
}
// max width from lines
for _, line := range lines {
adv := d.MeasureString(line)
currentWidth := float64(adv >> 6) //nolint
if currentWidth > width {
width = currentWidth
}
}
return width, height
}
// measureString returns the rendered width and height of the specified text
// given the current font face.
func (ctx *Context) measureString(s string) float64 {
d := &font.Drawer{
Face: ctx.font,
}
a := d.MeasureString(s)
return float64(a >> 6) //nolint
}