-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.go
180 lines (150 loc) · 4.89 KB
/
converter.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 ansitoimage
import (
"bytes"
"strings"
"unicode/utf8"
"github.com/fogleman/gg"
"github.com/leaanthony/go-ansi-parser"
"github.com/pkg/errors"
"golang.org/x/image/font"
)
// NewConverter creates new ANSI-to-Image converter.
func NewConverter(config Config) (*Converter, error) {
converter := &Converter{config: config}
// Load monospaced regular font.
monoRegularFontFace, err := loadFontFace(config.MonoRegularFontBytes, config.MonoRegularFontPoints)
if err != nil {
return nil, errors.Wrap(err, "failed to load monospaced regular font")
}
converter.monoRegularFontFace = monoRegularFontFace
// Load monospaced bold font.
monoBoldFontFace, err := loadFontFace(config.MonoBoldFontBytes, config.MonoBoldFontPoints)
if err != nil {
return nil, errors.Wrap(err, "failed to load monospaced bold font")
}
converter.monoBoldFontFace = monoBoldFontFace
// Load monospaced oblique font.
monoObliqueFontFace, err := loadFontFace(config.MonoObliqueFontBytes, config.MonoObliqueFontPoints)
if err != nil {
return nil, errors.Wrap(err, "failed to load monospaced oblique font")
}
converter.monoObliqueFontFace = monoObliqueFontFace
// Load monospaced oblique bold font.
monoBoldObliqueFontFace, err := loadFontFace(config.MonoObliqueBoldFontBytes, config.MonoObliqueBoldFontPoints)
if err != nil {
return nil, errors.Wrap(err, "failed to load monospaced oblique bold font")
}
converter.monoBoldObliqueFontFace = monoBoldObliqueFontFace
// Prepare gg context.
converter.ggContext = gg.NewContext(
config.Padding*2+config.PageCols*config.CharWidth,
config.Padding*2+config.PageRows*config.LineHeight,
)
return converter, nil
}
// Converter converts ANSI texts to image.
type Converter struct {
// Converter config.
config Config
// Graphics toolkit context.
ggContext *gg.Context
// Graphics toolkit fonts.
monoRegularFontFace font.Face
monoBoldFontFace font.Face
monoObliqueFontFace font.Face
monoBoldObliqueFontFace font.Face
}
// Parse parses ANSI texts to image.
func (c *Converter) Parse(text string) error {
c.ggContext.SetRGB(0, 0, 0)
c.ggContext.Clear()
if len(text) != 0 {
styledTexts, err := ansi.Parse(
text,
ansi.WithDefaultForegroundColor("37"),
ansi.WithDefaultBackgroundColor("30"),
ansi.WithIgnoreInvalidCodes(),
)
if err != nil {
return errors.Wrap(err, "failed to parse ANSI text")
}
offsetXPix, offsetYPix := c.config.Padding, c.config.Padding
nextX, nextY := 0, 0
for _, styledText := range styledTexts {
lines := strings.Split(styledText.Label, c.config.LineBreak)
for lineIndex, styledTextLine := range lines {
if lineIndex > 0 {
nextY += 1
nextX = 0
}
lineCharsCount := utf8.RuneCountInString(styledTextLine)
lineXBasePix := float64(offsetXPix + (nextX * c.config.CharWidth))
lineYBasePix := float64(offsetYPix + (nextY * c.config.LineHeight))
if styledText.BgCol != nil {
c.ggContext.SetRGB(
float64(styledText.BgCol.Rgb.R)/255,
float64(styledText.BgCol.Rgb.G)/255,
float64(styledText.BgCol.Rgb.B)/255,
)
rectWidth := lineCharsCount
if lineIndex < len(lines)-1 {
rectWidth = c.config.PageCols - nextX
}
c.ggContext.DrawRectangle(
lineXBasePix, lineYBasePix,
float64(rectWidth*c.config.CharWidth), float64(c.config.LineHeight),
)
c.ggContext.Fill()
}
// Select font face.
switch {
case styledText.Bold() && styledText.Italic():
c.ggContext.SetFontFace(c.monoBoldObliqueFontFace)
case styledText.Italic():
c.ggContext.SetFontFace(c.monoObliqueFontFace)
case styledText.Bold():
c.ggContext.SetFontFace(c.monoBoldFontFace)
default:
c.ggContext.SetFontFace(c.monoRegularFontFace)
}
// Additional styles unfortunately not supported by toolkit.
// See: https://github.com/fogleman/gg/issues/82.
switch {
case styledText.Style&ansi.Underlined > 0:
case styledText.Style&ansi.Strikethrough > 0:
}
if styledText.Style == ansi.Bold {
c.ggContext.SetFontFace(c.monoBoldFontFace)
} else {
c.ggContext.SetFontFace(c.monoRegularFontFace)
}
if styledText.FgCol != nil {
c.ggContext.SetRGB(
float64(styledText.FgCol.Rgb.R)/255,
float64(styledText.FgCol.Rgb.G)/255,
float64(styledText.FgCol.Rgb.B)/255,
)
} else {
c.ggContext.SetRGB(1.0, 1.0, 1.0)
}
for runeIndex, runeValue := range []rune(styledTextLine) {
c.ggContext.DrawString(
string(runeValue),
lineXBasePix+(float64(runeIndex*c.config.CharWidth)),
lineYBasePix+(float64(c.config.LineHeight-c.config.LineShift)),
)
}
nextX += lineCharsCount
}
}
}
return nil
}
// ToPNG encodes parsed text to PNG image.
func (c *Converter) ToPNG() ([]byte, error) {
buffer := bytes.Buffer{}
if err := c.ggContext.EncodePNG(&buffer); err != nil {
return nil, errors.Wrap(err, "failed to encode PNG")
}
return buffer.Bytes(), nil
}