go-hep.org/x/hep@v0.38.1/hplot/style.go (about)

     1  // Copyright ©2017 The go-hep 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 hplot
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"gonum.org/v1/plot"
    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/draw"
    15  )
    16  
    17  var (
    18  	// DefaultStyle is the default style used for hplot plots.
    19  	DefaultStyle Style
    20  )
    21  
    22  // Style stores a given plot style.
    23  type Style struct {
    24  	Fonts struct {
    25  		Name    string    // font name of this style
    26  		Default font.Font // font used for the plot
    27  		Title   font.Font // font used for the plot title
    28  		Label   font.Font // font used for the plot labels
    29  		Legend  font.Font // font used for the plot legend
    30  		Tick    font.Font // font used for the plot's axes' ticks
    31  
    32  		Cache *font.Cache // cache of fonts for this plot.
    33  	}
    34  	TextHandler text.Handler
    35  }
    36  
    37  // Apply setups the plot p with the current style.
    38  func (s *Style) Apply(p *Plot) {
    39  	p.Plot.Title.TextStyle.Font = s.Fonts.Title
    40  	p.Plot.X.Label.TextStyle.Font = s.Fonts.Label
    41  	p.Plot.Y.Label.TextStyle.Font = s.Fonts.Label
    42  	p.Plot.X.Tick.Label.Font = s.Fonts.Tick
    43  	p.Plot.Y.Tick.Label.Font = s.Fonts.Tick
    44  	p.Plot.Legend.TextStyle.Font = s.Fonts.Legend
    45  	p.Plot.Legend.YPosition = draw.PosCenter
    46  	p.Plot.TextHandler = s.TextHandler
    47  }
    48  
    49  func (s *Style) reset(fnt font.Font) {
    50  	plot.DefaultFont = fnt
    51  }
    52  
    53  // NewStyle creates a new style with the given font cache.
    54  func NewStyle(fnt font.Font, cache *font.Cache) (Style, error) {
    55  	var sty Style
    56  
    57  	sty.Fonts.Name = fnt.Name()
    58  	sty.Fonts.Default = fnt
    59  	sty.Fonts.Title = fnt
    60  	sty.Fonts.Label = fnt
    61  	sty.Fonts.Legend = fnt
    62  	sty.Fonts.Tick = fnt
    63  	sty.Fonts.Cache = cache
    64  
    65  	err := sty.init(fnt.Name())
    66  	if err != nil {
    67  		return sty, err
    68  	}
    69  
    70  	sty.TextHandler = text.Plain{
    71  		Fonts: sty.Fonts.Cache,
    72  	}
    73  
    74  	return sty, nil
    75  }
    76  
    77  func (sty *Style) init(name string) error {
    78  	sty.Fonts.Name = name
    79  	for _, t := range []struct {
    80  		ft   *font.Font
    81  		size font.Length
    82  	}{
    83  		{&sty.Fonts.Title, 12},
    84  		{&sty.Fonts.Label, 12},
    85  		{&sty.Fonts.Legend, 12},
    86  		{&sty.Fonts.Tick, 10},
    87  	} {
    88  		if !sty.Fonts.Cache.Has(*t.ft) {
    89  			return fmt.Errorf("hplot: no font %v in cache", *t.ft)
    90  		}
    91  		t.ft.Size = t.size
    92  	}
    93  
    94  	return nil
    95  }
    96  
    97  func init() {
    98  	var (
    99  		cache = font.NewCache(liberation.Collection())
   100  		err   error
   101  	)
   102  	DefaultStyle, err = NewStyle(
   103  		font.Font{
   104  			Typeface: "Liberation",
   105  			Variant:  "Sans",
   106  		},
   107  		cache,
   108  	)
   109  	if err != nil {
   110  		panic(err)
   111  	}
   112  }