errors that make more sense

This commit is contained in:
Bernard Xie 2023-03-24 15:36:51 -07:00
parent c01beb6089
commit 54b501dc92
No known key found for this signature in database
GPG key ID: 3C3E0036CE0F892C
2 changed files with 14 additions and 7 deletions

View file

@ -286,7 +286,8 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, sketc
if !sketch {
fontFamily, err = d2fonts.AddFont(font)
if err != nil {
ms.Log.Error.Printf("failed to load font %v, rendering with default font.", font)
ms.Log.Error.Printf("failed to load font %v: %v.", font, err)
ms.Log.Info.Printf("rendering with default font.")
}
}

View file

@ -222,14 +222,20 @@ func AddFont(fontLoc string) (FontFamily, error) {
if fontLoc == "" {
return "", nil
}
fontBuf, err := os.ReadFile(fontLoc)
if err != nil {
return "", fmt.Errorf("failed to read font: %v", err)
}
splitFont := strings.Split(fontLoc, "/")
fontFileName := splitFont[len(splitFont)-1]
fontName := strings.TrimSuffix(fontFileName, filepath.Ext(fontFileName))
ext := filepath.Ext(fontFileName)
if ext != ".ttf" {
return "", fmt.Errorf("cannot open non .ttf fonts")
}
fontBuf, err := os.ReadFile(fontLoc)
if err != nil {
return "", fmt.Errorf("failed to read font at location %v", err)
}
fontName := strings.TrimSuffix(fontFileName, ext)
fontFamily := FontFamily(fontName)
woffFont, err := fontlib.Sfnt2Woff(fontBuf)
if err != nil {