subset fonts
This commit is contained in:
parent
488ad25093
commit
7c146dd213
3 changed files with 38 additions and 11 deletions
|
|
@ -32,6 +32,15 @@ func (f FontFamily) Font(size int, style FontStyle) Font {
|
|||
}
|
||||
}
|
||||
|
||||
func (f Font) GetEncodedSubset(cutSet string) string {
|
||||
fontString, err := fontlib.Subset(FontFaces[f], cutSet)
|
||||
if err != nil {
|
||||
// If subset fails, return full encoding
|
||||
fontString = FontEncodings[f]
|
||||
}
|
||||
return fontString
|
||||
}
|
||||
|
||||
const (
|
||||
FONT_SIZE_XS = 13
|
||||
FONT_SIZE_S = 14
|
||||
|
|
|
|||
|
|
@ -1382,7 +1382,7 @@ func RenderText(text string, x, height float64) string {
|
|||
return strings.Join(rendered, "")
|
||||
}
|
||||
|
||||
func embedFonts(buf *bytes.Buffer, diagramHash, source string, fontFamily *d2fonts.FontFamily) {
|
||||
func embedFonts(buf *bytes.Buffer, diagramHash, source string, fontFamily *d2fonts.FontFamily, cutSet string) {
|
||||
fmt.Fprint(buf, `<style type="text/css"><![CDATA[`)
|
||||
|
||||
appendOnTrigger(
|
||||
|
|
@ -1404,7 +1404,7 @@ func embedFonts(buf *bytes.Buffer, diagramHash, source string, fontFamily *d2fon
|
|||
diagramHash,
|
||||
diagramHash,
|
||||
diagramHash,
|
||||
d2fonts.FontEncodings[fontFamily.Font(0, d2fonts.FONT_STYLE_REGULAR)],
|
||||
fontFamily.Font(0, d2fonts.FONT_STYLE_REGULAR).GetEncodedSubset(cutSet),
|
||||
),
|
||||
)
|
||||
|
||||
|
|
@ -1466,7 +1466,7 @@ func embedFonts(buf *bytes.Buffer, diagramHash, source string, fontFamily *d2fon
|
|||
diagramHash,
|
||||
diagramHash,
|
||||
diagramHash,
|
||||
d2fonts.FontEncodings[fontFamily.Font(0, d2fonts.FONT_STYLE_BOLD)],
|
||||
fontFamily.Font(0, d2fonts.FONT_STYLE_BOLD).GetEncodedSubset(cutSet),
|
||||
),
|
||||
)
|
||||
|
||||
|
|
@ -1490,7 +1490,7 @@ func embedFonts(buf *bytes.Buffer, diagramHash, source string, fontFamily *d2fon
|
|||
diagramHash,
|
||||
diagramHash,
|
||||
diagramHash,
|
||||
d2fonts.FontEncodings[fontFamily.Font(0, d2fonts.FONT_STYLE_ITALIC)],
|
||||
fontFamily.Font(0, d2fonts.FONT_STYLE_ITALIC).GetEncodedSubset(cutSet),
|
||||
),
|
||||
)
|
||||
|
||||
|
|
@ -1515,7 +1515,7 @@ func embedFonts(buf *bytes.Buffer, diagramHash, source string, fontFamily *d2fon
|
|||
diagramHash,
|
||||
diagramHash,
|
||||
diagramHash,
|
||||
d2fonts.FontEncodings[d2fonts.SourceCodePro.Font(0, d2fonts.FONT_STYLE_REGULAR)],
|
||||
d2fonts.SourceCodePro.Font(0, d2fonts.FONT_STYLE_REGULAR).GetEncodedSubset(cutSet),
|
||||
),
|
||||
)
|
||||
|
||||
|
|
@ -1536,7 +1536,7 @@ func embedFonts(buf *bytes.Buffer, diagramHash, source string, fontFamily *d2fon
|
|||
diagramHash,
|
||||
diagramHash,
|
||||
diagramHash,
|
||||
d2fonts.FontEncodings[d2fonts.SourceCodePro.Font(0, d2fonts.FONT_STYLE_BOLD)],
|
||||
d2fonts.SourceCodePro.Font(0, d2fonts.FONT_STYLE_BOLD).GetEncodedSubset(cutSet),
|
||||
),
|
||||
)
|
||||
|
||||
|
|
@ -1557,7 +1557,7 @@ func embedFonts(buf *bytes.Buffer, diagramHash, source string, fontFamily *d2fon
|
|||
diagramHash,
|
||||
diagramHash,
|
||||
diagramHash,
|
||||
d2fonts.FontEncodings[d2fonts.SourceCodePro.Font(0, d2fonts.FONT_STYLE_ITALIC)],
|
||||
d2fonts.SourceCodePro.Font(0, d2fonts.FONT_STYLE_ITALIC).GetEncodedSubset(cutSet),
|
||||
),
|
||||
)
|
||||
|
||||
|
|
@ -1720,7 +1720,15 @@ func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
|
|||
|
||||
// generate style elements that will be appended to the SVG tag
|
||||
upperBuf := &bytes.Buffer{}
|
||||
embedFonts(upperBuf, diagramHash, buf.String(), diagram.FontFamily) // embedFonts *must* run before `d2sketch.DefineFillPatterns`, but after all elements are appended to `buf`
|
||||
var allLabels string
|
||||
for _, s := range diagram.Shapes {
|
||||
allLabels += s.Text.Label
|
||||
}
|
||||
for _, c := range diagram.Connections {
|
||||
allLabels += c.Text.Label
|
||||
}
|
||||
|
||||
embedFonts(upperBuf, diagramHash, buf.String(), diagram.FontFamily, allLabels) // embedFonts *must* run before `d2sketch.DefineFillPatterns`, but after all elements are appended to `buf`
|
||||
themeStylesheet, err := themeCSS(diagramHash, themeID, darkThemeID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
|
|
@ -3,10 +3,13 @@ package font
|
|||
import (
|
||||
"bytes"
|
||||
"compress/zlib"
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
|
||||
"github.com/jung-kurt/gofpdf"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -209,6 +212,13 @@ func Sfnt2Woff(fontBuf []byte) ([]byte, error) {
|
|||
}
|
||||
|
||||
// gofpdf subset only accepts .ttf fonts
|
||||
// func Subset(fontBuf []byte, cutset string) []byte {
|
||||
// return gofpdf.UTF8CutFont(fontBuf, cutset)
|
||||
// }
|
||||
func Subset(fontBuf []byte, cutset string) (string, error) {
|
||||
subsetFont := gofpdf.UTF8CutFont(fontBuf, cutset)
|
||||
subsetWoff, err := Sfnt2Woff(subsetFont)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to convert to woff font: %v", err)
|
||||
}
|
||||
|
||||
encodedWoff := fmt.Sprintf("data:application/font-woff;base64,%v", base64.StdEncoding.EncodeToString(subsetWoff))
|
||||
return encodedWoff, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue