| 28 | ) |
| 29 | |
| 30 | func 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 | |
| 85 | func untargz(name string, r io.Reader) ([]byte, error) { |
| 86 | gr, err := gzip.NewReader(r) |