Merge pull request #1529 from alixander/fix-race

fix race
This commit is contained in:
Alexander Wang 2023-08-02 19:56:22 -07:00 committed by GitHub
commit f139d80814
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,6 +9,7 @@ import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"strings" "strings"
"sync"
"oss.terrastruct.com/d2/lib/font" "oss.terrastruct.com/d2/lib/font"
fontlib "oss.terrastruct.com/d2/lib/font" fontlib "oss.terrastruct.com/d2/lib/font"
@ -41,6 +42,8 @@ func (f Font) GetEncodedSubset(corpus string) string {
} }
} }
FontFamiliesMu.Lock()
defer FontFamiliesMu.Unlock()
fontBuf := make([]byte, len(FontFaces[f])) fontBuf := make([]byte, len(FontFaces[f]))
copy(fontBuf, FontFaces[f]) copy(fontBuf, FontFaces[f])
fontBuf = font.UTF8CutFont(fontBuf, uniqueChars) fontBuf = font.UTF8CutFont(fontBuf, uniqueChars)
@ -96,6 +99,8 @@ var FontFamilies = []FontFamily{
HandDrawn, HandDrawn,
} }
var FontFamiliesMu sync.Mutex
//go:embed encoded/SourceSansPro-Regular.txt //go:embed encoded/SourceSansPro-Regular.txt
var sourceSansProRegularBase64 string var sourceSansProRegularBase64 string
@ -309,6 +314,8 @@ func AddFontStyle(font Font, style FontStyle, ttf []byte) error {
} }
func AddFontFamily(name string, regularTTF, italicTTF, boldTTF, semiboldTTF []byte) (*FontFamily, error) { func AddFontFamily(name string, regularTTF, italicTTF, boldTTF, semiboldTTF []byte) (*FontFamily, error) {
FontFamiliesMu.Lock()
defer FontFamiliesMu.Unlock()
customFontFamily := FontFamily(name) customFontFamily := FontFamily(name)
regularFont := Font{ regularFont := Font{