...

Source file src/gonum.org/v1/plot/vg/vgsvg/font_test.go

Documentation: gonum.org/v1/plot/vg/vgsvg

     1  // Copyright ©2021 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package vgsvg
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/go-fonts/latin-modern/lmroman10regular"
    11  	"github.com/go-fonts/liberation/liberationmonoregular"
    12  	"github.com/go-fonts/liberation/liberationsansregular"
    13  	"github.com/go-fonts/liberation/liberationserifbold"
    14  	"github.com/go-fonts/liberation/liberationserifbolditalic"
    15  	"github.com/go-fonts/liberation/liberationserifitalic"
    16  	"github.com/go-fonts/liberation/liberationserifregular"
    17  	xfnt "golang.org/x/image/font"
    18  	"golang.org/x/image/font/sfnt"
    19  
    20  	"gonum.org/v1/plot/font"
    21  )
    22  
    23  func TestSVGFontDescr(t *testing.T) {
    24  	newFace := func(fnt font.Font, raw []byte) font.Face {
    25  		ttf, err := sfnt.Parse(raw)
    26  		if err != nil {
    27  			t.Fatalf("could not parse %q: %+v", fnt.Typeface, err)
    28  		}
    29  		return font.Face{Font: fnt, Face: ttf}
    30  	}
    31  
    32  	for i, tc := range []struct {
    33  		fnt  font.Face
    34  		want string
    35  	}{
    36  		// typefaces
    37  		{
    38  			fnt: newFace(
    39  				font.Font{Typeface: "Liberation"},
    40  				liberationserifregular.TTF,
    41  			),
    42  			want: "font-family:Liberation Serif;font-variant:none;font-weight:normal;font-style:normal",
    43  		},
    44  		{
    45  			fnt: newFace(
    46  				font.Font{
    47  					Typeface: "Liberation",
    48  					Variant:  "",
    49  					Style:    xfnt.StyleNormal,
    50  					Weight:   xfnt.WeightNormal,
    51  				},
    52  				liberationserifregular.TTF,
    53  			),
    54  			want: "font-family:Liberation Serif;font-variant:none;font-weight:normal;font-style:normal",
    55  		},
    56  		{
    57  			fnt: newFace(
    58  				font.Font{
    59  					Typeface: "Latin Modern",
    60  					Variant:  "",
    61  					Style:    xfnt.StyleNormal,
    62  					Weight:   xfnt.WeightNormal,
    63  				},
    64  				lmroman10regular.TTF,
    65  			),
    66  			want: "font-family:Latin Modern Roman;font-variant:none;font-weight:normal;font-style:normal",
    67  		},
    68  		// variants
    69  		{
    70  			fnt: newFace(
    71  				font.Font{
    72  					Typeface: "Liberation",
    73  					Variant:  "Mono",
    74  					Style:    xfnt.StyleNormal,
    75  					Weight:   xfnt.WeightNormal,
    76  				},
    77  				liberationmonoregular.TTF,
    78  			),
    79  			want: "font-family:Liberation Mono;font-variant:normal;font-weight:normal;font-style:normal",
    80  		},
    81  		{
    82  			fnt: newFace(
    83  				font.Font{
    84  					Typeface: "Liberation",
    85  					Variant:  "Serif",
    86  					Style:    xfnt.StyleNormal,
    87  					Weight:   xfnt.WeightNormal,
    88  				},
    89  				liberationserifregular.TTF,
    90  			),
    91  			want: "font-family:Liberation Serif;font-variant:normal;font-weight:normal;font-style:normal",
    92  		},
    93  		{
    94  			fnt: newFace(
    95  				font.Font{
    96  					Typeface: "Liberation",
    97  					Variant:  "Sans",
    98  					Style:    xfnt.StyleNormal,
    99  					Weight:   xfnt.WeightNormal,
   100  				},
   101  				liberationsansregular.TTF,
   102  			),
   103  			want: "font-family:Liberation Sans;font-variant:normal;font-weight:normal;font-style:normal",
   104  		},
   105  		{
   106  			fnt: newFace(
   107  				font.Font{
   108  					Typeface: "Liberation",
   109  					Variant:  "SansSerif",
   110  					Style:    xfnt.StyleNormal,
   111  					Weight:   xfnt.WeightNormal,
   112  				},
   113  				liberationsansregular.TTF,
   114  			),
   115  			want: "font-family:Liberation Sans;font-variant:normal;font-weight:normal;font-style:normal",
   116  		},
   117  		{
   118  			fnt: newFace(
   119  				font.Font{
   120  					Typeface: "Liberation",
   121  					Variant:  "Sans-Serif",
   122  					Style:    xfnt.StyleNormal,
   123  					Weight:   xfnt.WeightNormal,
   124  				},
   125  				liberationsansregular.TTF,
   126  			),
   127  			want: "font-family:Liberation Sans;font-variant:normal;font-weight:normal;font-style:normal",
   128  		},
   129  		{
   130  			fnt: newFace(
   131  				font.Font{
   132  					Typeface: "Liberation",
   133  					Variant:  "Smallcaps",
   134  					Style:    xfnt.StyleNormal,
   135  					Weight:   xfnt.WeightNormal,
   136  				},
   137  				liberationserifregular.TTF,
   138  			),
   139  			want: "font-family:Liberation Serif;font-variant:small-caps;font-weight:normal;font-style:normal",
   140  		},
   141  		// styles
   142  		{
   143  			fnt: newFace(
   144  				font.Font{
   145  					Typeface: "Liberation",
   146  					Variant:  "",
   147  					Style:    xfnt.StyleItalic,
   148  					Weight:   xfnt.WeightNormal,
   149  				},
   150  				liberationserifitalic.TTF,
   151  			),
   152  			want: "font-family:Liberation Serif;font-variant:none;font-weight:normal;font-style:italic",
   153  		},
   154  		{
   155  			fnt: newFace(
   156  				font.Font{
   157  					Typeface: "Liberation",
   158  					Variant:  "",
   159  					Style:    xfnt.StyleOblique,
   160  					Weight:   xfnt.WeightNormal,
   161  				},
   162  				liberationserifitalic.TTF,
   163  			),
   164  			want: "font-family:Liberation Serif;font-variant:none;font-weight:normal;font-style:oblique",
   165  		},
   166  		// weights
   167  		{
   168  			fnt: newFace(
   169  				font.Font{
   170  					Typeface: "Liberation",
   171  					Variant:  "",
   172  					Style:    xfnt.StyleNormal,
   173  					Weight:   xfnt.WeightThin,
   174  				},
   175  				liberationserifregular.TTF,
   176  			),
   177  			want: "font-family:Liberation Serif;font-variant:none;font-weight:100;font-style:normal",
   178  		},
   179  		{
   180  			fnt: newFace(
   181  				font.Font{
   182  					Typeface: "Liberation",
   183  					Variant:  "",
   184  					Style:    xfnt.StyleNormal,
   185  					Weight:   xfnt.WeightBold,
   186  				},
   187  				liberationserifbold.TTF,
   188  			),
   189  			want: "font-family:Liberation Serif;font-variant:none;font-weight:bold;font-style:normal",
   190  		},
   191  		{
   192  			fnt: newFace(
   193  				font.Font{
   194  					Typeface: "Liberation",
   195  					Variant:  "",
   196  					Style:    xfnt.StyleNormal,
   197  				},
   198  				liberationserifregular.TTF,
   199  			),
   200  			want: "font-family:Liberation Serif;font-variant:none;font-weight:normal;font-style:normal",
   201  		},
   202  		{
   203  			fnt: newFace(
   204  				font.Font{
   205  					Typeface: "Liberation",
   206  					Variant:  "",
   207  					Style:    xfnt.StyleNormal,
   208  					Weight:   xfnt.WeightExtraBold,
   209  				},
   210  				liberationserifbold.TTF,
   211  			),
   212  			want: "font-family:Liberation Serif;font-variant:none;font-weight:800;font-style:normal",
   213  		},
   214  		// weights+styles
   215  		{
   216  			fnt: newFace(
   217  				font.Font{
   218  					Typeface: "Liberation",
   219  					Variant:  "",
   220  					Style:    xfnt.StyleItalic,
   221  					Weight:   xfnt.WeightBold,
   222  				},
   223  				liberationserifbolditalic.TTF,
   224  			),
   225  			want: "font-family:Liberation Serif;font-variant:none;font-weight:bold;font-style:italic",
   226  		},
   227  	} {
   228  		got := svgFontDescr(tc.fnt)
   229  		if got != tc.want {
   230  			t.Errorf(
   231  				"invalid SVG font[%d] description:\ngot= %s\nwant=%s",
   232  				i, got, tc.want,
   233  			)
   234  		}
   235  	}
   236  }
   237  

View as plain text