custom semibold font option

This commit is contained in:
Gavin Nishizawa 2023-04-10 14:55:07 -07:00
parent 07faf97941
commit e51429749a
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
2 changed files with 31 additions and 5 deletions

View file

@ -100,6 +100,7 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
fontRegularFlag := ms.Opts.String("D2_FONT_REGULAR", "font-regular", "", "", "path to .ttf file to use for the regular font. If none provided, Source Sans Pro Regular is used.")
fontItalicFlag := ms.Opts.String("D2_FONT_ITALIC", "font-italic", "", "", "path to .ttf file to use for the italic font. If none provided, Source Sans Pro Regular-Italic is used.")
fontBoldFlag := ms.Opts.String("D2_FONT_BOLD", "font-bold", "", "", "path to .ttf file to use for the bold font. If none provided, Source Sans Pro Bold is used.")
fontSemiboldFlag := ms.Opts.String("D2_FONT_SEMIBOLD", "font-semibold", "", "", "path to .ttf file to use for the semibold font. If none provided, Source Sans Pro Semibold is used.")
ps, err := d2plugin.ListPlugins(ctx)
if err != nil {
@ -120,7 +121,7 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
return nil
}
fontFamily, err := loadFonts(ms, *fontRegularFlag, *fontItalicFlag, *fontBoldFlag)
fontFamily, err := loadFonts(ms, *fontRegularFlag, *fontItalicFlag, *fontBoldFlag, *fontSemiboldFlag)
if err != nil {
return xmain.UsageErrorf("failed to load specified fonts: %v", err)
}
@ -883,14 +884,15 @@ func loadFont(ms *xmain.State, path string) ([]byte, error) {
return ttf, nil
}
func loadFonts(ms *xmain.State, pathToRegular, pathToItalic, pathToBold string) (*d2fonts.FontFamily, error) {
if pathToRegular == "" && pathToItalic == "" && pathToBold == "" {
func loadFonts(ms *xmain.State, pathToRegular, pathToItalic, pathToBold, pathToSemibold string) (*d2fonts.FontFamily, error) {
if pathToRegular == "" && pathToItalic == "" && pathToBold == "" && pathToSemibold == "" {
return nil, nil
}
var regularTTF []byte
var italicTTF []byte
var boldTTF []byte
var semiboldTTF []byte
var err error
if pathToRegular != "" {
@ -911,6 +913,12 @@ func loadFonts(ms *xmain.State, pathToRegular, pathToItalic, pathToBold string)
return nil, err
}
}
if pathToSemibold != "" {
semiboldTTF, err = loadFont(ms, pathToSemibold)
if err != nil {
return nil, err
}
}
return d2fonts.AddFontFamily("custom", regularTTF, italicTTF, boldTTF)
return d2fonts.AddFontFamily("custom", regularTTF, italicTTF, boldTTF, semiboldTTF)
}

View file

@ -291,7 +291,7 @@ func AddFontStyle(font Font, style FontStyle, ttf []byte) error {
return nil
}
func AddFontFamily(name string, regularTTF, italicTTF, boldTTF []byte) (*FontFamily, error) {
func AddFontFamily(name string, regularTTF, italicTTF, boldTTF, semiboldTTF []byte) (*FontFamily, error) {
customFontFamily := FontFamily(name)
regularFont := Font{
@ -348,6 +348,24 @@ func AddFontFamily(name string, regularTTF, italicTTF, boldTTF []byte) (*FontFam
FontEncodings[boldFont] = FontEncodings[fallbackFont]
}
semiboldFont := Font{
Family: customFontFamily,
Style: FONT_STYLE_SEMIBOLD,
}
if semiboldTTF != nil {
err := AddFontStyle(semiboldFont, FONT_STYLE_SEMIBOLD, semiboldTTF)
if err != nil {
return nil, err
}
} else {
fallbackFont := Font{
Family: SourceSansPro,
Style: FONT_STYLE_SEMIBOLD,
}
FontFaces[semiboldFont] = FontFaces[fallbackFont]
FontEncodings[semiboldFont] = FontEncodings[fallbackFont]
}
FontFamilies = append(FontFamilies, customFontFamily)
return &customFontFamily, nil