MCPcopy Index your code
hub / github.com/gonum/plot / Example_addFont

Function Example_addFont

vg/example_test.go:30–83  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

28)
29
30func Example_addFont() {
31 // download font from debian
32 const url = "http://http.debian.net/debian/pool/main/f/fonts-ipafont/fonts-ipafont_00303.orig.tar.gz"
33
34 resp, err := http.Get(url)
35 if err != nil {
36 log.Fatalf("could not download IPA font file: %+v", err)
37 }
38 defer resp.Body.Close()
39
40 ttf, err := untargz("IPAfont00303/ipam.ttf", resp.Body)
41 if err != nil {
42 log.Fatalf("could not untar archive: %+v", err)
43 }
44
45 fontTTF, err := opentype.Parse(ttf)
46 if err != nil {
47 log.Fatal(err)
48 }
49 mincho := font.Font{Typeface: "Mincho"}
50 font.DefaultCache.Add([]font.Face{
51 {
52 Font: mincho,
53 Face: fontTTF,
54 },
55 })
56 if !font.DefaultCache.Has(mincho) {
57 log.Fatalf("no font %q!", mincho.Typeface)
58 }
59 plot.DefaultFont = mincho
60 plotter.DefaultFont = mincho
61
62 p := plot.New()
63 p.Title.Text = "Hello, 世界!"
64 p.X.Label.Text = "世"
65 p.Y.Label.Text = "界"
66
67 labels, err := plotter.NewLabels(
68 plotter.XYLabels{
69 XYs: make(plotter.XYs, 1),
70 Labels: []string{"こんにちは世界"},
71 },
72 )
73 if err != nil {
74 log.Fatalf("could not create labels: %+v", err)
75 }
76 p.Add(labels)
77 p.Add(plotter.NewGrid())
78
79 err = p.Save(10*vg.Centimeter, 10*vg.Centimeter, "mincho-font.png")
80 if err != nil {
81 log.Fatalf("could not save plot: %+v", err)
82 }
83}
84
85func untargz(name string, r io.Reader) ([]byte, error) {
86 gr, err := gzip.NewReader(r)

Callers

nothing calls this directly

Calls 8

NewFunction · 0.92
NewLabelsFunction · 0.92
NewGridFunction · 0.92
untargzFunction · 0.85
CloseMethod · 0.80
HasMethod · 0.80
SaveMethod · 0.80
AddMethod · 0.45

Tested by

no test coverage detected