d2fonts.FontFaces sync.Map

This commit is contained in:
Gavin Nishizawa 2023-10-24 16:40:56 -07:00
parent 6b255c61da
commit a67c186cae
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
4 changed files with 58 additions and 42 deletions

View file

@ -44,8 +44,13 @@ func (f Font) GetEncodedSubset(corpus string) string {
FontFamiliesMu.Lock() FontFamiliesMu.Lock()
defer FontFamiliesMu.Unlock() defer FontFamiliesMu.Unlock()
fontBuf := make([]byte, len(FontFaces[f])) var face []byte
copy(fontBuf, FontFaces[f]) {
ff, _ := FontFaces.Load(f)
face = ff.([]byte)
}
fontBuf := make([]byte, len(face))
copy(fontBuf, face)
fontBuf = font.UTF8CutFont(fontBuf, uniqueChars) fontBuf = font.UTF8CutFont(fontBuf, uniqueChars)
fontBuf, err := fontlib.Sfnt2Woff(fontBuf) fontBuf, err := fontlib.Sfnt2Woff(fontBuf)
@ -138,8 +143,8 @@ var fontFacesFS embed.FS
// FontEncodings map[Font]string // FontEncodings map[Font]string
var FontEncodings sync.Map var FontEncodings sync.Map
// TODO FontFaces map[Font][]byte // FontFaces map[Font][]byte
var FontFaces map[Font][]byte var FontFaces sync.Map
func init() { func init() {
FontEncodings.Store( FontEncodings.Store(
@ -228,104 +233,103 @@ func init() {
return true return true
}) })
FontFaces = map[Font][]byte{}
b, err := fontFacesFS.ReadFile("ttf/SourceSansPro-Regular.ttf") b, err := fontFacesFS.ReadFile("ttf/SourceSansPro-Regular.ttf")
if err != nil { if err != nil {
panic(err) panic(err)
} }
FontFaces[Font{ FontFaces.Store(Font{
Family: SourceSansPro, Family: SourceSansPro,
Style: FONT_STYLE_REGULAR, Style: FONT_STYLE_REGULAR,
}] = b }, b)
b, err = fontFacesFS.ReadFile("ttf/SourceCodePro-Regular.ttf") b, err = fontFacesFS.ReadFile("ttf/SourceCodePro-Regular.ttf")
if err != nil { if err != nil {
panic(err) panic(err)
} }
FontFaces[Font{ FontFaces.Store(Font{
Family: SourceCodePro, Family: SourceCodePro,
Style: FONT_STYLE_REGULAR, Style: FONT_STYLE_REGULAR,
}] = b }, b)
b, err = fontFacesFS.ReadFile("ttf/SourceCodePro-Bold.ttf") b, err = fontFacesFS.ReadFile("ttf/SourceCodePro-Bold.ttf")
if err != nil { if err != nil {
panic(err) panic(err)
} }
FontFaces[Font{ FontFaces.Store(Font{
Family: SourceCodePro, Family: SourceCodePro,
Style: FONT_STYLE_BOLD, Style: FONT_STYLE_BOLD,
}] = b }, b)
b, err = fontFacesFS.ReadFile("ttf/SourceCodePro-Semibold.ttf") b, err = fontFacesFS.ReadFile("ttf/SourceCodePro-Semibold.ttf")
if err != nil { if err != nil {
panic(err) panic(err)
} }
FontFaces[Font{ FontFaces.Store(Font{
Family: SourceCodePro, Family: SourceCodePro,
Style: FONT_STYLE_SEMIBOLD, Style: FONT_STYLE_SEMIBOLD,
}] = b }, b)
b, err = fontFacesFS.ReadFile("ttf/SourceCodePro-Italic.ttf") b, err = fontFacesFS.ReadFile("ttf/SourceCodePro-Italic.ttf")
if err != nil { if err != nil {
panic(err) panic(err)
} }
FontFaces[Font{ FontFaces.Store(Font{
Family: SourceCodePro, Family: SourceCodePro,
Style: FONT_STYLE_ITALIC, Style: FONT_STYLE_ITALIC,
}] = b }, b)
b, err = fontFacesFS.ReadFile("ttf/SourceSansPro-Bold.ttf") b, err = fontFacesFS.ReadFile("ttf/SourceSansPro-Bold.ttf")
if err != nil { if err != nil {
panic(err) panic(err)
} }
FontFaces[Font{ FontFaces.Store(Font{
Family: SourceSansPro, Family: SourceSansPro,
Style: FONT_STYLE_BOLD, Style: FONT_STYLE_BOLD,
}] = b }, b)
b, err = fontFacesFS.ReadFile("ttf/SourceSansPro-Semibold.ttf") b, err = fontFacesFS.ReadFile("ttf/SourceSansPro-Semibold.ttf")
if err != nil { if err != nil {
panic(err) panic(err)
} }
FontFaces[Font{ FontFaces.Store(Font{
Family: SourceSansPro, Family: SourceSansPro,
Style: FONT_STYLE_SEMIBOLD, Style: FONT_STYLE_SEMIBOLD,
}] = b }, b)
b, err = fontFacesFS.ReadFile("ttf/SourceSansPro-Italic.ttf") b, err = fontFacesFS.ReadFile("ttf/SourceSansPro-Italic.ttf")
if err != nil { if err != nil {
panic(err) panic(err)
} }
FontFaces[Font{ FontFaces.Store(Font{
Family: SourceSansPro, Family: SourceSansPro,
Style: FONT_STYLE_ITALIC, Style: FONT_STYLE_ITALIC,
}] = b }, b)
b, err = fontFacesFS.ReadFile("ttf/FuzzyBubbles-Regular.ttf") b, err = fontFacesFS.ReadFile("ttf/FuzzyBubbles-Regular.ttf")
if err != nil { if err != nil {
panic(err) panic(err)
} }
FontFaces[Font{ FontFaces.Store(Font{
Family: HandDrawn, Family: HandDrawn,
Style: FONT_STYLE_REGULAR, Style: FONT_STYLE_REGULAR,
}] = b }, b)
FontFaces[Font{ FontFaces.Store(Font{
Family: HandDrawn, Family: HandDrawn,
Style: FONT_STYLE_ITALIC, Style: FONT_STYLE_ITALIC,
}] = b }, b)
b, err = fontFacesFS.ReadFile("ttf/FuzzyBubbles-Bold.ttf") b, err = fontFacesFS.ReadFile("ttf/FuzzyBubbles-Bold.ttf")
if err != nil { if err != nil {
panic(err) panic(err)
} }
FontFaces[Font{ FontFaces.Store(Font{
Family: HandDrawn, Family: HandDrawn,
Style: FONT_STYLE_BOLD, Style: FONT_STYLE_BOLD,
}] = b }, b)
FontFaces[Font{ FontFaces.Store(Font{
Family: HandDrawn, Family: HandDrawn,
Style: FONT_STYLE_SEMIBOLD, Style: FONT_STYLE_SEMIBOLD,
}] = b }, b)
} }
var D2_FONT_TO_FAMILY = map[string]FontFamily{ var D2_FONT_TO_FAMILY = map[string]FontFamily{
@ -334,7 +338,7 @@ var D2_FONT_TO_FAMILY = map[string]FontFamily{
} }
func AddFontStyle(font Font, style FontStyle, ttf []byte) error { func AddFontStyle(font Font, style FontStyle, ttf []byte) error {
FontFaces[font] = ttf FontFaces.Store(font, ttf)
woff, err := fontlib.Sfnt2Woff(ttf) woff, err := fontlib.Sfnt2Woff(ttf)
if err != nil { if err != nil {
@ -365,9 +369,10 @@ func AddFontFamily(name string, regularTTF, italicTTF, boldTTF, semiboldTTF []by
Family: SourceSansPro, Family: SourceSansPro,
Style: FONT_STYLE_REGULAR, Style: FONT_STYLE_REGULAR,
} }
FontFaces[regularFont] = FontFaces[fallbackFont] f, _ := FontFaces.Load(fallbackFont)
fb, _ := FontEncodings.Load(fallbackFont) FontFaces.Store(regularFont, f)
FontEncodings.Store(regularFont, fb) e, _ := FontEncodings.Load(fallbackFont)
FontEncodings.Store(regularFont, e)
} }
italicFont := Font{ italicFont := Font{
@ -384,7 +389,8 @@ func AddFontFamily(name string, regularTTF, italicTTF, boldTTF, semiboldTTF []by
Family: SourceSansPro, Family: SourceSansPro,
Style: FONT_STYLE_ITALIC, Style: FONT_STYLE_ITALIC,
} }
FontFaces[italicFont] = FontFaces[fallbackFont] f, _ := FontFaces.Load(fallbackFont)
FontFaces.Store(italicFont, f)
fb, _ := FontEncodings.Load(fallbackFont) fb, _ := FontEncodings.Load(fallbackFont)
FontEncodings.Store(italicFont, fb) FontEncodings.Store(italicFont, fb)
} }
@ -403,7 +409,8 @@ func AddFontFamily(name string, regularTTF, italicTTF, boldTTF, semiboldTTF []by
Family: SourceSansPro, Family: SourceSansPro,
Style: FONT_STYLE_BOLD, Style: FONT_STYLE_BOLD,
} }
FontFaces[boldFont] = FontFaces[fallbackFont] f, _ := FontFaces.Load(fallbackFont)
FontFaces.Store(boldFont, f)
fb, _ := FontEncodings.Load(fallbackFont) fb, _ := FontEncodings.Load(fallbackFont)
FontEncodings.Store(boldFont, fb) FontEncodings.Store(boldFont, fb)
} }
@ -422,7 +429,8 @@ func AddFontFamily(name string, regularTTF, italicTTF, boldTTF, semiboldTTF []by
Family: SourceSansPro, Family: SourceSansPro,
Style: FONT_STYLE_SEMIBOLD, Style: FONT_STYLE_SEMIBOLD,
} }
FontFaces[semiboldFont] = FontFaces[fallbackFont] f, _ := FontFaces.Load(fallbackFont)
FontFaces.Store(semiboldFont, f)
fb, _ := FontEncodings.Load(fallbackFont) fb, _ := FontEncodings.Load(fallbackFont)
FontEncodings.Store(semiboldFont, fb) FontEncodings.Store(semiboldFont, fb)
} }

View file

@ -14,8 +14,13 @@ func TestCutFont(t *testing.T) {
Family: SourceCodePro, Family: SourceCodePro,
Style: FONT_STYLE_BOLD, Style: FONT_STYLE_BOLD,
} }
fontBuf := make([]byte, len(FontFaces[f])) var face []byte
copy(fontBuf, FontFaces[f]) {
ff, _ := FontFaces.Load(f)
face = ff.([]byte)
}
fontBuf := make([]byte, len(face))
copy(fontBuf, face)
fontBuf = font.UTF8CutFont(fontBuf, " 1") fontBuf = font.UTF8CutFont(fontBuf, " 1")
err := diff.Testdata(filepath.Join("testdata", "d2fonts", "cut"), ".txt", fontBuf) err := diff.Testdata(filepath.Join("testdata", "d2fonts", "cut"), ".txt", fontBuf)
assert.Success(t, err) assert.Success(t, err)

View file

@ -31,8 +31,10 @@ func Init() *GoFPDF {
UnitStr: "pt", UnitStr: "pt",
}) })
newGofPDF.AddUTF8FontFromBytes("source", "", d2fonts.FontFaces[d2fonts.SourceSansPro.Font(0, d2fonts.FONT_STYLE_REGULAR)]) reg, _ := d2fonts.FontFaces.Load(d2fonts.SourceSansPro.Font(0, d2fonts.FONT_STYLE_REGULAR))
newGofPDF.AddUTF8FontFromBytes("source", "B", d2fonts.FontFaces[d2fonts.SourceSansPro.Font(0, d2fonts.FONT_STYLE_BOLD)]) bold, _ := d2fonts.FontFaces.Load(d2fonts.SourceSansPro.Font(0, d2fonts.FONT_STYLE_BOLD))
newGofPDF.AddUTF8FontFromBytes("source", "", reg.([]byte))
newGofPDF.AddUTF8FontFromBytes("source", "B", bold.([]byte))
newGofPDF.SetAutoPageBreak(false, 0) newGofPDF.SetAutoPageBreak(false, 0)
newGofPDF.SetLineWidth(2) newGofPDF.SetLineWidth(2)
newGofPDF.SetMargins(0, 0, 0) newGofPDF.SetMargins(0, 0, 0)

View file

@ -126,11 +126,12 @@ func NewRuler() (*Ruler, error) {
Style: fontStyle, Style: fontStyle,
} }
// Note: FontFaces lookup is size-agnostic // Note: FontFaces lookup is size-agnostic
if _, ok := d2fonts.FontFaces[font]; !ok { face, has := d2fonts.FontFaces.Load(font)
if !has {
continue continue
} }
if _, loaded := r.ttfs[font]; !loaded { if _, loaded := r.ttfs[font]; !loaded {
ttf, err := truetype.Parse(d2fonts.FontFaces[font]) ttf, err := truetype.Parse(face.([]byte))
if err != nil { if err != nil {
return nil, err return nil, err
} }