Skip to content

Commit

Permalink
vg/vgsvg: drop fontMap, use informations from font.Font
Browse files Browse the repository at this point in the history
This CL drops the use of the internal fontMap that was associating some
pre-defined set of fonts with a set of SVG naming schemes (derived from
PostScript).
Instead, use the informations contained in plot/font.Font to derive the
expected SVG font-family (and friends) font style string.

Updates #702.
  • Loading branch information
sbinet committed Jul 1, 2021
1 parent 1c112bc commit 07c51d8
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 55 deletions.
171 changes: 171 additions & 0 deletions vg/vgsvg/font_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// Copyright ©2021 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package vgsvg

import (
"testing"

stdfnt "golang.org/x/image/font"
"gonum.org/v1/plot/font"
)

func TestSVGFontDescr(t *testing.T) {
for i, tc := range []struct {
fnt font.Font
want string
}{
// typefaces
{
fnt: font.Font{Typeface: "Liberation"},
want: "font-family:Liberation;font-variant:normal;font-weight:normal;font-style:normal",
},
{
fnt: font.Font{
Typeface: "Liberation",
Variant: "",
Style: stdfnt.StyleNormal,
Weight: stdfnt.WeightNormal,
},
want: "font-family:Liberation;font-variant:normal;font-weight:normal;font-style:normal",
},
{
fnt: font.Font{
Typeface: "My-Font-Name",
Variant: "",
Style: stdfnt.StyleNormal,
Weight: stdfnt.WeightNormal,
},
want: "font-family:My-Font-Name;font-variant:normal;font-weight:normal;font-style:normal",
},
// variants
{
fnt: font.Font{
Typeface: "Liberation",
Variant: "Mono",
Style: stdfnt.StyleNormal,
Weight: stdfnt.WeightNormal,
},
want: "font-family:Liberation, monospace;font-variant:monospace;font-weight:normal;font-style:normal",
},
{
fnt: font.Font{
Typeface: "Liberation",
Variant: "Serif",
Style: stdfnt.StyleNormal,
Weight: stdfnt.WeightNormal,
},
want: "font-family:Liberation, serif;font-variant:serif;font-weight:normal;font-style:normal",
},
{
fnt: font.Font{
Typeface: "Liberation",
Variant: "Sans",
Style: stdfnt.StyleNormal,
Weight: stdfnt.WeightNormal,
},
want: "font-family:Liberation, sans-serif;font-variant:sans-serif;font-weight:normal;font-style:normal",
},
{
fnt: font.Font{
Typeface: "Liberation",
Variant: "SansSerif",
Style: stdfnt.StyleNormal,
Weight: stdfnt.WeightNormal,
},
want: "font-family:Liberation, sans-serif;font-variant:sans-serif;font-weight:normal;font-style:normal",
},
{
fnt: font.Font{
Typeface: "Liberation",
Variant: "Sans-Serif",
Style: stdfnt.StyleNormal,
Weight: stdfnt.WeightNormal,
},
want: "font-family:Liberation, sans-serif;font-variant:sans-serif;font-weight:normal;font-style:normal",
},
{
fnt: font.Font{
Typeface: "Liberation",
Variant: "Smallcaps",
Style: stdfnt.StyleNormal,
Weight: stdfnt.WeightNormal,
},
want: "font-family:Liberation, small-caps;font-variant:small-caps;font-weight:normal;font-style:normal",
},
// styles
{
fnt: font.Font{
Typeface: "Liberation",
Variant: "",
Style: stdfnt.StyleItalic,
Weight: stdfnt.WeightNormal,
},
want: "font-family:Liberation;font-variant:normal;font-weight:normal;font-style:italic",
},
{
fnt: font.Font{
Typeface: "Liberation",
Variant: "",
Style: stdfnt.StyleOblique,
Weight: stdfnt.WeightNormal,
},
want: "font-family:Liberation;font-variant:normal;font-weight:normal;font-style:oblique",
},
// weights
{
fnt: font.Font{
Typeface: "Liberation",
Variant: "",
Style: stdfnt.StyleNormal,
Weight: stdfnt.WeightThin,
},
want: "font-family:Liberation;font-variant:normal;font-weight:100;font-style:normal",
},
{
fnt: font.Font{
Typeface: "Liberation",
Variant: "",
Style: stdfnt.StyleNormal,
Weight: stdfnt.WeightBold,
},
want: "font-family:Liberation;font-variant:normal;font-weight:bold;font-style:normal",
},
{
fnt: font.Font{
Typeface: "Liberation",
Variant: "",
Style: stdfnt.StyleNormal,
},
want: "font-family:Liberation;font-variant:normal;font-weight:normal;font-style:normal",
},
{
fnt: font.Font{
Typeface: "Liberation",
Variant: "",
Style: stdfnt.StyleNormal,
Weight: stdfnt.WeightExtraBold,
},
want: "font-family:Liberation;font-variant:normal;font-weight:800;font-style:normal",
},
// weights+styles
{
fnt: font.Font{
Typeface: "Times",
Variant: "",
Style: stdfnt.StyleItalic,
Weight: stdfnt.WeightBold,
},
want: "font-family:Times;font-variant:normal;font-weight:bold;font-style:italic",
},
} {
got := svgFontDescr(tc.fnt)
if got != tc.want {
t.Errorf(
"invalid SVG font[%d] description:\ngot= %s\nwant=%s",
i, got, tc.want,
)
}
}
}
18 changes: 9 additions & 9 deletions vg/vgsvg/testdata/scatter_golden.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 9 additions & 9 deletions vg/vgsvg/testdata/scatter_line_golden.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 07c51d8

Please sign in to comment.