...

Source file src/gonum.org/v1/plot/text/latex_test.go

Documentation: gonum.org/v1/plot/text

     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 text_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	stdfnt "golang.org/x/image/font"
    11  	"gonum.org/v1/plot/font"
    12  	"gonum.org/v1/plot/font/liberation"
    13  	"gonum.org/v1/plot/text"
    14  	"gonum.org/v1/plot/vg"
    15  )
    16  
    17  func TestLatexText(t *testing.T) {
    18  	type box struct{ w, h, d vg.Length }
    19  
    20  	fonts := font.NewCache(liberation.Collection())
    21  
    22  	tr12 := font.Font{Variant: "Serif", Size: 12}
    23  	ti12 := font.Font{Variant: "Serif", Size: 12, Style: stdfnt.StyleItalic}
    24  	tr42 := font.Font{Variant: "Serif", Size: 42}
    25  
    26  	for _, tc := range []struct {
    27  		txt string
    28  		fnt font.Font
    29  		box []box
    30  		w   vg.Length
    31  		h   vg.Length
    32  	}{
    33  		{
    34  			txt: "",
    35  			box: []box{{0, 0, 0}},
    36  		},
    37  		{
    38  			txt: " ",
    39  			box: []box{{3, 0, 0}},
    40  			w:   3,
    41  		},
    42  		{
    43  			txt: "hello",
    44  			box: []box{{23.994140625, 8.328125, 0.634765625}},
    45  			w:   23.994140625,
    46  			h:   8.962890625,
    47  		},
    48  		{
    49  			txt: "hello",
    50  			fnt: ti12,
    51  			box: []box{{23.994140625, 8.328125, 0.634765625}},
    52  			w:   23.994140625,
    53  			h:   8.962890625,
    54  		},
    55  		{
    56  			txt: `$hello$`,
    57  			box: []box{{23.994140625, 8.328125, 0.634765625}},
    58  			w:   23.994140625,
    59  			h:   8.962890625,
    60  		},
    61  		{
    62  			txt: "hello",
    63  			fnt: tr42,
    64  			box: []box{{83.9794921875, 29.1484375, 2.2216796875}},
    65  			w:   83.9794921875,
    66  			h:   31.3701171875,
    67  		},
    68  		{
    69  			txt: "Agg",
    70  			box: []box{{20.666015625, 7.921875, 3.103515625}},
    71  			w:   20.666015625,
    72  			h:   11.025390625,
    73  		},
    74  		{
    75  			txt: "Agg",
    76  			fnt: ti12, // italics correctly ignored.
    77  			box: []box{{20.666015625, 7.921875, 3.103515625}},
    78  			w:   20.666015625,
    79  			h:   11.025390625,
    80  		},
    81  		{
    82  			txt: `$Agg$`,
    83  			box: []box{{19.330078125, 7.921875, 3.072265625}},
    84  			w:   19.330078125,
    85  			h:   10.994140625,
    86  		},
    87  		{
    88  			txt: "Agg",
    89  			fnt: tr42,
    90  			box: []box{{72.3310546875, 27.7265625, 10.8623046875}},
    91  			w:   72.3310546875,
    92  			h:   38.5888671875,
    93  		},
    94  		{
    95  			txt: `VA`,
    96  			box: []box{{13.20703125, 7.921875, 0.697265625}},
    97  			w:   13.20703125,
    98  			h:   8.619140625,
    99  		},
   100  		{
   101  			txt: `$V\Delta$`,
   102  			box: []box{{14.373046875, 7.921875, 0.697265625}},
   103  			w:   14.373046875,
   104  			h:   8.619140625,
   105  		},
   106  	} {
   107  		t.Run(tc.txt, func(t *testing.T) {
   108  			fnt := tc.fnt
   109  			if fnt == (font.Font{}) {
   110  				fnt = tr12
   111  			}
   112  
   113  			sty := text.Style{
   114  				Font:    fnt,
   115  				Handler: &text.Latex{Fonts: fonts},
   116  			}
   117  
   118  			lines := sty.Handler.Lines(tc.txt)
   119  			if got, want := len(lines), len(tc.box); got != want {
   120  				t.Errorf("invalid number of lines: got=%d, want=%d", got, want)
   121  			}
   122  
   123  			for i, line := range lines {
   124  				var b box
   125  				b.w, b.h, b.d = sty.Handler.Box(line, sty.Font)
   126  
   127  				if got, want := b, tc.box[i]; got != want {
   128  					t.Errorf("invalid box[%d]: got=%v, want=%v", i, got, want)
   129  				}
   130  			}
   131  
   132  			w := sty.Width(tc.txt)
   133  			if got, want := w, tc.w; got != want {
   134  				t.Errorf("invalid width: got=%v, want=%v", got, want)
   135  			}
   136  
   137  			h := sty.Height(tc.txt)
   138  			if got, want := h, tc.h; got != want {
   139  				t.Errorf("invalid height: got=%v, want=%v", got, want)
   140  			}
   141  		})
   142  	}
   143  }
   144  

View as plain text