diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 931433ee9..66e87b07d 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -4,17 +4,19 @@ #### Improvements 🧹 -- d2js: Support `d2-config`. Support additional options: [#2343](https://github.com/terrastruct/d2/pull/2343) - - `themeID` - - `darkThemeID` - - `center` - - `pad` - - `scale` - - `forceAppendix` - - `target` - - `animateInterval` - - `salt` - - `noXMLTag` +- d2js: + - Support `d2-config`. Support additional options: [#2343](https://github.com/terrastruct/d2/pull/2343) + - `themeID` + - `darkThemeID` + - `center` + - `pad` + - `scale` + - `forceAppendix` + - `target` + - `animateInterval` + - `salt` + - `noXMLTag` + - Support fonts (`fontRegular`, `fontItalic`, `fontBold`, `fontSemiBold`): (PR Pending) #### Bugfixes ⛑️ diff --git a/d2js/d2wasm/functions.go b/d2js/d2wasm/functions.go index 192ac068e..4df3882c2 100644 --- a/d2js/d2wasm/functions.go +++ b/d2js/d2wasm/functions.go @@ -29,7 +29,6 @@ import ( "oss.terrastruct.com/d2/lib/textmeasure" "oss.terrastruct.com/d2/lib/urlenc" "oss.terrastruct.com/d2/lib/version" - "oss.terrastruct.com/util-go/go2" ) func GetParentID(args []js.Value) (interface{}, error) { @@ -194,6 +193,30 @@ func Compile(args []js.Value) (interface{}, error) { return nil, &WASMError{Message: fmt.Sprintf("invalid fs input: %s", err.Error()), Code: 400} } + var fontRegular []byte + var fontItalic []byte + var fontBold []byte + var fontSemibold []byte + if input.Opts != nil && (input.Opts.FontRegular != nil) { + fontRegular = *input.Opts.FontRegular + } + if input.Opts != nil && (input.Opts.FontItalic != nil) { + fontItalic = *input.Opts.FontItalic + } + if input.Opts != nil && (input.Opts.FontBold != nil) { + fontBold = *input.Opts.FontBold + } + if input.Opts != nil && (input.Opts.FontSemibold != nil) { + fontSemibold = *input.Opts.FontSemibold + } + if fontRegular != nil || fontItalic != nil || fontBold != nil || fontSemibold != nil { + fontFamily, err := d2fonts.AddFontFamily("custom", fontRegular, fontItalic, fontBold, fontSemibold) + if err != nil { + return nil, &WASMError{Message: fmt.Sprintf("custom fonts could not be initialized: %s", err.Error()), Code: 500} + } + compileOpts.FontFamily = fontFamily + } + compileOpts.Ruler, err = textmeasure.NewRuler() if err != nil { return nil, &WASMError{Message: fmt.Sprintf("text ruler cannot be initialized: %s", err.Error()), Code: 500} @@ -206,9 +229,6 @@ func Compile(args []js.Value) (interface{}, error) { renderOpts := &d2svg.RenderOpts{} if input.Opts != nil && input.Opts.Sketch != nil { renderOpts.Sketch = input.Opts.Sketch - if *input.Opts.Sketch { - compileOpts.FontFamily = go2.Pointer(d2fonts.HandDrawn) - } } if input.Opts != nil && input.Opts.Pad != nil { renderOpts.Pad = input.Opts.Pad diff --git a/d2js/d2wasm/types.go b/d2js/d2wasm/types.go index 70b3e1154..a74624610 100644 --- a/d2js/d2wasm/types.go +++ b/d2js/d2wasm/types.go @@ -52,7 +52,11 @@ type RenderOptions struct { type CompileOptions struct { RenderOptions - Layout *string `json:"layout"` + Layout *string `json:"layout"` + FontRegular *[]byte `json:"FontRegular"` + FontItalic *[]byte `json:"FontItalic"` + FontBold *[]byte `json:"FontBold"` + FontSemibold *[]byte `json:"FontSemibold"` } type CompileResponse struct { diff --git a/d2js/js/README.md b/d2js/js/README.md index 411586036..a72545a3e 100644 --- a/d2js/js/README.md +++ b/d2js/js/README.md @@ -77,6 +77,10 @@ Renders a compiled diagram to SVG. All [RenderOptions](#renderoptions) properties in addition to: - `layout`: Layout engine to use ('dagre' | 'elk') [default: 'dagre'] +- `fontRegular` A byte array containing .ttf file to use for the regular font. If none provided, Source Sans Pro Regular is used. +- `fontItalic` A byte array containing .ttf file to use for the italic font. If none provided, Source Sans Pro Italic is used. +- `fontBold` A byte array containing .ttf file to use for the bold font. If none provided, Source Sans Pro Bold is used. +- `fontSemibold` A byte array containing .ttf file to use for the semibold font. If none provided, Source Sans Pro Semibold is used. ### `RenderOptions` diff --git a/d2js/js/examples/customizable.html b/d2js/js/examples/customizable.html index 54df63a49..afa6e60a3 100644 --- a/d2js/js/examples/customizable.html +++ b/d2js/js/examples/customizable.html @@ -319,6 +319,53 @@ +