2019-10-06 19:58:36 +11:00
|
|
|
// Package svg contains an SVG formatter.
|
2019-09-28 23:27:59 +02:00
|
|
|
package svg
|
|
|
|
|
|
|
|
import (
|
2019-10-15 14:38:42 +02:00
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
2019-09-28 23:27:59 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-10-15 14:38:42 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
2019-09-28 23:27:59 +02:00
|
|
|
"strings"
|
|
|
|
|
Version 2 of Chroma
This cleans up the API in general, removing a bunch of deprecated stuff,
cleaning up circular imports, etc.
But the biggest change is switching to an optional XML format for the
regex lexer.
Having lexers defined only in Go is not ideal for a couple of reasons.
Firstly, it impedes a significant portion of contributors who use Chroma
in Hugo, but don't know Go. Secondly, it bloats the binary size of any
project that imports Chroma.
Why XML? YAML is an abomination and JSON is not human editable. XML
also compresses very well (eg. Go template lexer XML compresses from
3239 bytes to 718).
Why a new syntax format? All major existing formats rely on the
Oniguruma regex engine, which is extremely complex and for which there
is no Go port.
Why not earlier? Prior to the existence of fs.FS this was not a viable
option.
Benchmarks:
$ hyperfine --warmup 3 \
'./chroma.master --version' \
'./chroma.xml-pre-opt --version' \
'./chroma.xml --version'
Benchmark 1: ./chroma.master --version
Time (mean ± σ): 5.3 ms ± 0.5 ms [User: 3.6 ms, System: 1.4 ms]
Range (min … max): 4.2 ms … 6.6 ms 233 runs
Benchmark 2: ./chroma.xml-pre-opt --version
Time (mean ± σ): 50.6 ms ± 0.5 ms [User: 52.4 ms, System: 3.6 ms]
Range (min … max): 49.2 ms … 51.5 ms 51 runs
Benchmark 3: ./chroma.xml --version
Time (mean ± σ): 6.9 ms ± 1.1 ms [User: 5.1 ms, System: 1.5 ms]
Range (min … max): 5.7 ms … 19.9 ms 196 runs
Summary
'./chroma.master --version' ran
1.30 ± 0.23 times faster than './chroma.xml --version'
9.56 ± 0.83 times faster than './chroma.xml-pre-opt --version'
A slight increase in init time, but I think this is okay given the
increase in flexibility.
And binary size difference:
$ du -h lexers.test*
$ du -sh chroma* 951371ms
8.8M chroma.master
7.8M chroma.xml
7.8M chroma.xml-pre-opt
Benchmarks:
$ hyperfine --warmup 3 \
'./chroma.master --version' \
'./chroma.xml-pre-opt --version' \
'./chroma.xml --version'
Benchmark 1: ./chroma.master --version
Time (mean ± σ): 5.3 ms ± 0.5 ms [User: 3.6 ms, System: 1.4 ms]
Range (min … max): 4.2 ms … 6.6 ms 233 runs
Benchmark 2: ./chroma.xml-pre-opt --version
Time (mean ± σ): 50.6 ms ± 0.5 ms [User: 52.4 ms, System: 3.6 ms]
Range (min … max): 49.2 ms … 51.5 ms 51 runs
Benchmark 3: ./chroma.xml --version
Time (mean ± σ): 6.9 ms ± 1.1 ms [User: 5.1 ms, System: 1.5 ms]
Range (min … max): 5.7 ms … 19.9 ms 196 runs
Summary
'./chroma.master --version' ran
1.30 ± 0.23 times faster than './chroma.xml --version'
9.56 ± 0.83 times faster than './chroma.xml-pre-opt --version'
Incompatible changes:
- (*RegexLexer).SetAnalyser: changed from func(func(text string) float32) *RegexLexer to func(func(text string) float32) Lexer
- (*TokenType).UnmarshalJSON: removed
- Lexer.AnalyseText: added
- Lexer.SetAnalyser: added
- Lexer.SetRegistry: added
- MustNewLazyLexer: removed
- MustNewLexer: changed from func(*Config, Rules) *RegexLexer to func(*Config, func() Rules) *RegexLexer
- Mutators: changed from func(...Mutator) MutatorFunc to func(...Mutator) Mutator
- NewLazyLexer: removed
- NewLexer: changed from func(*Config, Rules) (*RegexLexer, error) to func(*Config, func() Rules) (*RegexLexer, error)
- Pop: changed from func(int) MutatorFunc to func(int) Mutator
- Push: changed from func(...string) MutatorFunc to func(...string) Mutator
- TokenType.MarshalJSON: removed
- Using: changed from func(Lexer) Emitter to func(string) Emitter
- UsingByGroup: changed from func(func(string) Lexer, int, int, ...Emitter) Emitter to func(int, int, ...Emitter) Emitter
2022-01-03 23:51:17 +11:00
|
|
|
"github.com/alecthomas/chroma/v2"
|
2019-09-28 23:27:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Option sets an option of the SVG formatter.
|
|
|
|
type Option func(f *Formatter)
|
|
|
|
|
2019-10-15 14:38:42 +02:00
|
|
|
// FontFamily sets the font-family.
|
|
|
|
func FontFamily(fontFamily string) Option { return func(f *Formatter) { f.fontFamily = fontFamily } }
|
|
|
|
|
|
|
|
// EmbedFontFile embeds given font file
|
|
|
|
func EmbedFontFile(fontFamily string, fileName string) (option Option, err error) {
|
|
|
|
var format FontFormat
|
|
|
|
switch path.Ext(fileName) {
|
|
|
|
case ".woff":
|
|
|
|
format = WOFF
|
|
|
|
case ".woff2":
|
|
|
|
format = WOFF2
|
|
|
|
case ".ttf":
|
|
|
|
format = TRUETYPE
|
|
|
|
default:
|
|
|
|
return nil, errors.New("unexpected font file suffix")
|
|
|
|
}
|
|
|
|
|
|
|
|
var content []byte
|
|
|
|
if content, err = ioutil.ReadFile(fileName); err == nil {
|
|
|
|
option = EmbedFont(fontFamily, base64.StdEncoding.EncodeToString(content), format)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// EmbedFont embeds given base64 encoded font
|
|
|
|
func EmbedFont(fontFamily string, font string, format FontFormat) Option {
|
|
|
|
return func(f *Formatter) { f.fontFamily = fontFamily; f.embeddedFont = font; f.fontFormat = format }
|
|
|
|
}
|
|
|
|
|
2019-09-28 23:27:59 +02:00
|
|
|
// New SVG formatter.
|
|
|
|
func New(options ...Option) *Formatter {
|
2019-10-15 14:38:42 +02:00
|
|
|
f := &Formatter{fontFamily: "Consolas, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace"}
|
2019-09-28 23:27:59 +02:00
|
|
|
for _, option := range options {
|
|
|
|
option(f)
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// Formatter that generates SVG.
|
|
|
|
type Formatter struct {
|
2019-10-15 14:38:42 +02:00
|
|
|
fontFamily string
|
|
|
|
embeddedFont string
|
|
|
|
fontFormat FontFormat
|
2019-09-28 23:27:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Formatter) Format(w io.Writer, style *chroma.Style, iterator chroma.Iterator) (err error) {
|
|
|
|
f.writeSVG(w, style, iterator.Tokens())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var svgEscaper = strings.NewReplacer(
|
|
|
|
`&`, "&",
|
|
|
|
`<`, "<",
|
|
|
|
`>`, ">",
|
|
|
|
`"`, """,
|
|
|
|
` `, " ",
|
|
|
|
` `, "    ",
|
|
|
|
)
|
|
|
|
|
|
|
|
// EscapeString escapes special characters.
|
|
|
|
func escapeString(s string) string {
|
|
|
|
return svgEscaper.Replace(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Formatter) writeSVG(w io.Writer, style *chroma.Style, tokens []chroma.Token) { // nolint: gocyclo
|
|
|
|
svgStyles := f.styleToSVG(style)
|
|
|
|
lines := chroma.SplitTokensIntoLines(tokens)
|
|
|
|
|
|
|
|
fmt.Fprint(w, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
|
|
|
|
fmt.Fprint(w, "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n")
|
2019-10-16 11:25:29 +02:00
|
|
|
fmt.Fprintf(w, "<svg width=\"%dpx\" height=\"%dpx\" xmlns=\"http://www.w3.org/2000/svg\">\n", 8*maxLineWidth(lines), 10+int(16.8*float64(len(lines)+1)))
|
2019-10-15 14:38:42 +02:00
|
|
|
|
|
|
|
if f.embeddedFont != "" {
|
|
|
|
f.writeFontStyle(w)
|
|
|
|
}
|
|
|
|
|
2019-09-28 23:27:59 +02:00
|
|
|
fmt.Fprintf(w, "<rect width=\"100%%\" height=\"100%%\" fill=\"%s\"/>\n", style.Get(chroma.Background).Background.String())
|
2019-10-15 14:38:42 +02:00
|
|
|
fmt.Fprintf(w, "<g font-family=\"%s\" font-size=\"14px\" fill=\"%s\">\n", f.fontFamily, style.Get(chroma.Text).Colour.String())
|
2019-09-28 23:27:59 +02:00
|
|
|
|
|
|
|
f.writeTokenBackgrounds(w, lines, style)
|
|
|
|
|
|
|
|
for index, tokens := range lines {
|
2019-10-16 11:25:29 +02:00
|
|
|
fmt.Fprintf(w, "<text x=\"0\" y=\"%fem\" xml:space=\"preserve\">", 1.2*float64(index+1))
|
2019-09-28 23:27:59 +02:00
|
|
|
|
|
|
|
for _, token := range tokens {
|
|
|
|
text := escapeString(token.String())
|
|
|
|
attr := f.styleAttr(svgStyles, token.Type)
|
|
|
|
if attr != "" {
|
|
|
|
text = fmt.Sprintf("<tspan %s>%s</tspan>", attr, text)
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, text)
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, "</text>")
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprint(w, "\n</g>\n")
|
|
|
|
fmt.Fprint(w, "</svg>\n")
|
|
|
|
}
|
|
|
|
|
2019-10-16 11:25:29 +02:00
|
|
|
func maxLineWidth(lines [][]chroma.Token) int {
|
|
|
|
maxWidth := 0
|
|
|
|
for _, tokens := range lines {
|
|
|
|
length := 0
|
|
|
|
for _, token := range tokens {
|
2021-04-29 12:07:50 +10:00
|
|
|
length += len(strings.ReplaceAll(token.String(), ` `, " "))
|
2019-10-16 11:25:29 +02:00
|
|
|
}
|
|
|
|
if length > maxWidth {
|
|
|
|
maxWidth = length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return maxWidth
|
|
|
|
}
|
|
|
|
|
2019-09-28 23:27:59 +02:00
|
|
|
// There is no background attribute for text in SVG so simply calculate the position and text
|
|
|
|
// of tokens with a background color that differs from the default and add a rectangle for each before
|
|
|
|
// adding the token.
|
|
|
|
func (f *Formatter) writeTokenBackgrounds(w io.Writer, lines [][]chroma.Token, style *chroma.Style) {
|
|
|
|
for index, tokens := range lines {
|
|
|
|
lineLength := 0
|
|
|
|
for _, token := range tokens {
|
2021-04-29 12:07:50 +10:00
|
|
|
length := len(strings.ReplaceAll(token.String(), ` `, " "))
|
2019-09-28 23:27:59 +02:00
|
|
|
tokenBackground := style.Get(token.Type).Background
|
|
|
|
if tokenBackground.IsSet() && tokenBackground != style.Get(chroma.Background).Background {
|
2019-10-16 11:25:29 +02:00
|
|
|
fmt.Fprintf(w, "<rect id=\"%s\" x=\"%dch\" y=\"%fem\" width=\"%dch\" height=\"1.2em\" fill=\"%s\" />\n", escapeString(token.String()), lineLength, 1.2*float64(index)+0.25, length, style.Get(token.Type).Background.String())
|
2019-09-28 23:27:59 +02:00
|
|
|
}
|
|
|
|
lineLength += length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-15 14:38:42 +02:00
|
|
|
type FontFormat int
|
|
|
|
|
|
|
|
// https://transfonter.org/formats
|
|
|
|
const (
|
|
|
|
WOFF FontFormat = iota
|
|
|
|
WOFF2
|
|
|
|
TRUETYPE
|
|
|
|
)
|
|
|
|
|
|
|
|
var fontFormats = [...]string{
|
|
|
|
"woff",
|
|
|
|
"woff2",
|
|
|
|
"truetype",
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Formatter) writeFontStyle(w io.Writer) {
|
|
|
|
fmt.Fprintf(w, `<style>
|
|
|
|
@font-face {
|
|
|
|
font-family: '%s';
|
|
|
|
src: url(data:application/x-font-%s;charset=utf-8;base64,%s) format('%s');'
|
|
|
|
font-weight: normal;
|
|
|
|
font-style: normal;
|
|
|
|
}
|
|
|
|
</style>`, f.fontFamily, fontFormats[f.fontFormat], f.embeddedFont, fontFormats[f.fontFormat])
|
|
|
|
}
|
|
|
|
|
2019-09-28 23:27:59 +02:00
|
|
|
func (f *Formatter) styleAttr(styles map[chroma.TokenType]string, tt chroma.TokenType) string {
|
|
|
|
if _, ok := styles[tt]; !ok {
|
|
|
|
tt = tt.SubCategory()
|
|
|
|
if _, ok := styles[tt]; !ok {
|
|
|
|
tt = tt.Category()
|
|
|
|
if _, ok := styles[tt]; !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return styles[tt]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Formatter) styleToSVG(style *chroma.Style) map[chroma.TokenType]string {
|
|
|
|
converted := map[chroma.TokenType]string{}
|
|
|
|
bg := style.Get(chroma.Background)
|
|
|
|
// Convert the style.
|
|
|
|
for t := range chroma.StandardTypes {
|
|
|
|
entry := style.Get(t)
|
|
|
|
if t != chroma.Background {
|
|
|
|
entry = entry.Sub(bg)
|
|
|
|
}
|
|
|
|
if entry.IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
converted[t] = StyleEntryToSVG(entry)
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
|
|
|
// StyleEntryToSVG converts a chroma.StyleEntry to SVG attributes.
|
|
|
|
func StyleEntryToSVG(e chroma.StyleEntry) string {
|
|
|
|
var styles []string
|
|
|
|
|
|
|
|
if e.Colour.IsSet() {
|
|
|
|
styles = append(styles, "fill=\""+e.Colour.String()+"\"")
|
|
|
|
}
|
|
|
|
if e.Bold == chroma.Yes {
|
|
|
|
styles = append(styles, "font-weight=\"bold\"")
|
|
|
|
}
|
|
|
|
if e.Italic == chroma.Yes {
|
|
|
|
styles = append(styles, "font-style=\"italic\"")
|
|
|
|
}
|
|
|
|
if e.Underline == chroma.Yes {
|
|
|
|
styles = append(styles, "text-decoration=\"underline\"")
|
|
|
|
}
|
|
|
|
return strings.Join(styles, " ")
|
|
|
|
}
|