Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

font: mono #1010

Merged
merged 4 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#### Features 🚀

- `style.font: mono` to use a monospaced font for the text/label [#1010](https://github.com/terrastruct/d2/pull/1010)
- `border-radius` is supported for both `class` and `sql_table` shapes. [#982](https://github.com/terrastruct/d2/pull/982)

#### Improvements 🧹
Expand Down
9 changes: 7 additions & 2 deletions d2graph/d2graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,10 @@ func (s *Style) Apply(key, value string) error {
if s.Font == nil {
break
}
if !go2.Contains(systemFonts, strings.ToUpper(value)) {
if _, ok := d2fonts.D2_FONT_TO_FAMILY[strings.ToLower(value)]; !ok {
return fmt.Errorf(`"%v" is not a valid font in our system`, value)
}
s.Font.Value = strings.ToUpper(value)
s.Font.Value = strings.ToLower(value)
case "font-size":
if s.FontSize == nil {
break
Expand Down Expand Up @@ -793,6 +793,11 @@ func (obj *Object) AppendReferences(ida []string, ref Reference, unresolvedObj *
func (obj *Object) GetLabelSize(mtexts []*d2target.MText, ruler *textmeasure.Ruler, fontFamily *d2fonts.FontFamily) (*d2target.TextDimensions, error) {
shapeType := strings.ToLower(obj.Attributes.Shape.Value)

if obj.Attributes.Style.Font != nil {
f := d2fonts.D2_FONT_TO_FAMILY[obj.Attributes.Style.Font.Value]
fontFamily = &f
}

var dims *d2target.TextDimensions
switch shapeType {
case d2target.ShapeText:
Expand Down
10 changes: 0 additions & 10 deletions d2graph/font_helper.go

This file was deleted.

5 changes: 5 additions & 0 deletions d2renderers/d2fonts/d2fonts.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,8 @@ func init() {
Style: FONT_STYLE_BOLD,
}] = b
}

var D2_FONT_TO_FAMILY = map[string]FontFamily{
"default": SourceSansPro,
"mono": SourceCodePro,
}
19 changes: 13 additions & 6 deletions d2renderers/d2svg/d2svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,9 @@ func drawConnection(writer io.Writer, labelMaskID string, connection d2target.Co

if connection.Label != "" {
fontClass := "text"
if connection.FontFamily == "mono" {
fontClass = "text-mono"
}
if connection.Bold {
fontClass += "-bold"
} else if connection.Italic {
Expand Down Expand Up @@ -1191,10 +1194,14 @@ func drawShape(writer io.Writer, diagramHash string, targetShape d2target.Shape,
)

fontClass := "text"
if targetShape.Bold {
fontClass += "-bold"
} else if targetShape.Italic {
fontClass += "-italic"
if targetShape.FontFamily == "mono" {
fontClass = "text-mono"
} else {
if targetShape.Bold {
fontClass += "-bold"
} else if targetShape.Italic {
fontClass += "-italic"
}
}
if targetShape.Underline {
fontClass += " text-underline"
Expand Down Expand Up @@ -1493,7 +1500,7 @@ func embedFonts(buf *bytes.Buffer, diagramHash, source string, fontFamily *d2fon
buf,
source,
[]string{
`class="text-mono-bold"`,
`class="text-mono-bold`,
},
fmt.Sprintf(`
.%s .text-mono-bold {
Expand All @@ -1514,7 +1521,7 @@ func embedFonts(buf *bytes.Buffer, diagramHash, source string, fontFamily *d2fon
buf,
source,
[]string{
`class="text-mono-italic"`,
`class="text-mono-italic`,
},
fmt.Sprintf(`
.%s .text-mono-italic {
Expand Down
Binary file modified e2etests-cli/testdata/TestCLI_E2E/internal_linked_pdf.exp.pdf
Binary file not shown.
38 changes: 36 additions & 2 deletions e2etests/stable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func testStable(t *testing.T) {

json: jsonb {constraint: unique}
last_updated: timestamp with time zone

style: {
fill: red
border-radius: 10
Expand All @@ -34,7 +34,7 @@ func testStable(t *testing.T) {

field: "[]string"
method(a uint64): (x, y int)

style: {
border-radius: 10
}
Expand Down Expand Up @@ -81,6 +81,40 @@ func testStable(t *testing.T) {
}
`,
},
{
name: "mono-font",
script: `satellites: SATELLITES {
shape: stored_data
style: {
font: mono
fill: white
stroke: black
multiple: true
}
}

transmitter: TRANSMITTER {
style: {
font: mono
fill: white
stroke: black
}
}

satellites -> transmitter: SEND {
style.stroke: black
style.font: mono
}
satellites -> transmitter: SEND {
style.stroke: black
style.font: mono
}
satellites -> transmitter: SEND {
style.stroke: black
style.font: mono
}
`,
},
{
name: "connected_container",
script: `a.b -> c.d -> f.h.g
Expand Down
Loading