From a5b867ba5beba35aec9b65cd11a1c9850af1acd3 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 2 Aug 2023 19:44:36 -0700 Subject: [PATCH] fix race --- d2renderers/d2fonts/d2fonts.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/d2renderers/d2fonts/d2fonts.go b/d2renderers/d2fonts/d2fonts.go index 1a552dc32..51704db23 100644 --- a/d2renderers/d2fonts/d2fonts.go +++ b/d2renderers/d2fonts/d2fonts.go @@ -9,6 +9,7 @@ import ( "encoding/base64" "fmt" "strings" + "sync" "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])) copy(fontBuf, FontFaces[f]) fontBuf = font.UTF8CutFont(fontBuf, uniqueChars) @@ -96,6 +99,8 @@ var FontFamilies = []FontFamily{ HandDrawn, } +var FontFamiliesMu sync.Mutex + //go:embed encoded/SourceSansPro-Regular.txt 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) { + FontFamiliesMu.Lock() + defer FontFamiliesMu.Unlock() customFontFamily := FontFamily(name) regularFont := Font{