Skip to content

Commit

Permalink
all: text.TextStyle -> text.Style
Browse files Browse the repository at this point in the history
  • Loading branch information
sbinet committed Jan 29, 2021
1 parent ecd5c17 commit a04713a
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 61 deletions.
34 changes: 17 additions & 17 deletions axis.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Axis struct {
// For the vertical axis, one quarter turn
// counterclockwise will be added to the label
// text before drawing.
text.TextStyle
TextStyle text.Style

// Position is where the axis label string should be drawn.
// The default value is draw.PosCenter, displaying the label
Expand All @@ -67,7 +67,7 @@ type Axis struct {

Tick struct {
// Label is the TextStyle on the tick labels.
Label text.TextStyle
Label text.Style

// LineStyle is the LineStyle of the tick lines.
draw.LineStyle
Expand Down Expand Up @@ -114,7 +114,7 @@ func makeAxis(o orientation) (Axis, error) {
Padding: vg.Points(5),
Scale: LinearScale{},
}
a.Label.TextStyle = text.TextStyle{
a.Label.TextStyle = text.Style{
Color: color.Black,
Font: labelFont,
XAlign: draw.XCenter,
Expand All @@ -136,7 +136,7 @@ func makeAxis(o orientation) (Axis, error) {
yalign = draw.YTop
}

a.Tick.Label = text.TextStyle{
a.Tick.Label = text.Style{
Color: color.Black,
Font: tickFont,
XAlign: xalign,
Expand Down Expand Up @@ -231,8 +231,8 @@ type horizontalAxis struct {
// size returns the height of the axis.
func (a horizontalAxis) size() (h vg.Length) {
if a.Label.Text != "" { // We assume that the label isn't rotated.
h += a.Label.Font.Extents().Descent
h += a.Label.Height(a.Label.Text)
h += a.Label.TextStyle.Font.Extents().Descent
h += a.Label.TextStyle.Height(a.Label.Text)
h += a.Label.Padding
}

Expand Down Expand Up @@ -260,12 +260,12 @@ func (a horizontalAxis) draw(c draw.Canvas) {
x = c.Center().X
case draw.PosRight:
x = c.Max.X
x -= a.Label.Font.Width(a.Label.Text) / 2
x -= a.Label.TextStyle.Font.Width(a.Label.Text) / 2
}
if a.Label.Text != "" {
descent := a.Label.Font.Extents().Descent
descent := a.Label.TextStyle.Font.Extents().Descent
c.FillText(a.Label.TextStyle, vg.Point{X: x, Y: y + descent}, a.Label.Text)
y += a.Label.Height(a.Label.Text)
y += a.Label.TextStyle.Height(a.Label.Text)
y += a.Label.Padding
}

Expand Down Expand Up @@ -326,16 +326,16 @@ type verticalAxis struct {
// size returns the width of the axis.
func (a verticalAxis) size() (w vg.Length) {
if a.Label.Text != "" { // We assume that the label isn't rotated.
w += a.Label.Font.Extents().Descent
w += a.Label.Height(a.Label.Text)
w += a.Label.TextStyle.Font.Extents().Descent
w += a.Label.TextStyle.Height(a.Label.Text)
w += a.Label.Padding
}

marks := a.Tick.Marker.Ticks(a.Min, a.Max)
if len(marks) > 0 {
if lwidth := tickLabelWidth(a.Tick.Label, marks); lwidth > 0 {
w += lwidth
w += a.Label.Width(" ")
w += a.Label.TextStyle.Width(" ")
}
if a.drawTicks() {
w += a.Tick.Length
Expand All @@ -356,15 +356,15 @@ func (a verticalAxis) draw(c draw.Canvas) {
if a.Label.Text != "" {
sty := a.Label.TextStyle
sty.Rotation += math.Pi / 2
x += a.Label.Height(a.Label.Text)
x += a.Label.TextStyle.Height(a.Label.Text)
switch a.Label.Position {
case draw.PosCenter:
y = c.Center().Y
case draw.PosTop:
y = c.Max.Y
y -= a.Label.Font.Width(a.Label.Text) / 2
y -= a.Label.TextStyle.Font.Width(a.Label.Text) / 2
}
descent := a.Label.Font.Extents().Descent
descent := a.Label.TextStyle.Font.Extents().Descent
c.FillText(sty, vg.Point{X: x - descent, Y: y}, a.Label.Text)
x += descent
x += a.Label.Padding
Expand Down Expand Up @@ -633,7 +633,7 @@ func (t Tick) lengthOffset(len vg.Length) vg.Length {
}

// tickLabelHeight returns height of the tick mark labels.
func tickLabelHeight(sty text.TextStyle, ticks []Tick) vg.Length {
func tickLabelHeight(sty text.Style, ticks []Tick) vg.Length {
maxHeight := vg.Length(0)
for _, t := range ticks {
if t.IsMinor() {
Expand All @@ -649,7 +649,7 @@ func tickLabelHeight(sty text.TextStyle, ticks []Tick) vg.Length {
}

// tickLabelWidth returns the width of the widest tick mark label.
func tickLabelWidth(sty text.TextStyle, ticks []Tick) vg.Length {
func tickLabelWidth(sty text.Style, ticks []Tick) vg.Length {
maxWidth := vg.Length(0)
for _, t := range ticks {
if t.IsMinor() {
Expand Down
4 changes: 2 additions & 2 deletions legend.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
type Legend struct {
// TextStyle is the style given to the legend
// entry texts.
text.TextStyle
TextStyle text.Style

// Padding is the amount of padding to add
// between each entry in the legend. If Padding
Expand Down Expand Up @@ -85,7 +85,7 @@ func NewLegend() (Legend, error) {
return Legend{
YPosition: draw.PosBottom,
ThumbnailWidth: vg.Points(20),
TextStyle: text.TextStyle{
TextStyle: text.Style{
Font: font,
Handler: DefaultTextHandler,
},
Expand Down
9 changes: 5 additions & 4 deletions plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ type Plot struct {
// the top of the plot.
Padding vg.Length

text.TextStyle
// TextStyle specifies how the plot title text should be displayed.
TextStyle text.Style
}

// BackgroundColor is the background color of the plot.
Expand Down Expand Up @@ -106,7 +107,7 @@ func New() (*Plot, error) {
Y: y,
Legend: legend,
}
p.Title.TextStyle = text.TextStyle{
p.Title.TextStyle = text.Style{
Color: color.Black,
Font: titleFont,
XAlign: draw.XCenter,
Expand Down Expand Up @@ -154,7 +155,7 @@ func (p *Plot) Draw(c draw.Canvas) {
if p.Title.Text != "" {
descent := p.Title.TextStyle.Font.Extents().Descent
c.FillText(p.Title.TextStyle, vg.Point{X: c.Center().X, Y: c.Max.Y + descent}, p.Title.Text)
_, h, d := p.Title.Handler.Box(p.Title.Text, p.Title.Font)
_, h, d := p.Title.TextStyle.Handler.Box(p.Title.Text, p.Title.TextStyle.Font)
c.Max.Y -= h + d
c.Max.Y -= p.Title.Padding
}
Expand Down Expand Up @@ -183,7 +184,7 @@ func (p *Plot) Draw(c draw.Canvas) {
// the plot data will be drawn.
func (p *Plot) DataCanvas(da draw.Canvas) draw.Canvas {
if p.Title.Text != "" {
da.Max.Y -= p.Title.Height(p.Title.Text) + p.Title.Font.Extents().Descent
da.Max.Y -= p.Title.TextStyle.Height(p.Title.Text) + p.Title.TextStyle.Font.Extents().Descent
da.Max.Y -= p.Title.Padding
}
p.X.sanitizeRange()
Expand Down
2 changes: 1 addition & 1 deletion plot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestLegendAlignment(t *testing.T) {
}
l := plot.Legend{
ThumbnailWidth: vg.Points(20),
TextStyle: text.TextStyle{
TextStyle: text.Style{
Font: font,
Handler: plot.DefaultTextHandler,
},
Expand Down
2 changes: 1 addition & 1 deletion plotter/barchart_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func ExampleBarChart_positiveNegative() {
p.Legend.Add("Sum", netDots)
p.Legend.Top = true
p.Legend.ThumbnailWidth = 2 * vg.Millimeter
p.Legend.Font.Size = 2 * vg.Millimeter
p.Legend.TextStyle.Font.Size = 2 * vg.Millimeter

err = p.Save(100, 100, "testdata/barChart_positiveNegative.png")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion plotter/heat_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func ExampleHeatMap() {
// Calculate the width of the legend.
r := l.Rectangle(dc)
legendWidth := r.Max.X - r.Min.X
l.YOffs = -p.Title.Font.Extents().Height // Adjust the legend down a little.
l.YOffs = -p.Title.TextStyle.Font.Extents().Height // Adjust the legend down a little.

l.Draw(dc)
dc = draw.Crop(dc, 0, -legendWidth-vg.Millimeter, 0, 0) // Make space for the legend.
Expand Down
6 changes: 3 additions & 3 deletions plotter/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Labels struct {

// TextStyle is the style of the label text. Each label
// can have a different text style.
TextStyle []text.TextStyle
TextStyle []text.Style

// XOffset and YOffset are added directly to the final
// label X and Y location respectively.
Expand Down Expand Up @@ -61,9 +61,9 @@ func NewLabels(d XYLabeller) (*Labels, error) {
return nil, err
}

styles := make([]text.TextStyle, d.Len())
styles := make([]text.Style, d.Len())
for i := range styles {
styles[i] = text.TextStyle{
styles[i] = text.Style{
Font: fnt,
Handler: plot.DefaultTextHandler,
}
Expand Down
2 changes: 1 addition & 1 deletion plotter/polygon_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func ExamplePolygon_holes() {
p.Y.Label.Text = "Y"
p.Add(poly)
p.Legend.Add("key", poly)
p.Legend.Font.Size = vg.Points(8)
p.Legend.TextStyle.Font.Size = vg.Points(8)
p.Legend.TextStyle.Color = color.White
p.Legend.ThumbnailWidth = vg.Points(10)

Expand Down
8 changes: 4 additions & 4 deletions plotter/sankey.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Sankey struct {
// TextStyle specifies the default stock label
// text style. Styles can be modified for
// individual stocks.
TextStyle text.TextStyle
TextStyle text.Style

flows []Flow

Expand All @@ -61,7 +61,7 @@ type Sankey struct {
// The default function uses the default TextStyle, color and LineStyle
// specified above for all stocks; zero horizontal and vertical offsets;
// and the stock label as the text to be printed on the plot.
StockStyle func(label string, category int) (lbl string, ts text.TextStyle, xOff, yOff vg.Length, c color.Color, ls draw.LineStyle)
StockStyle func(label string, category int) (lbl string, ts text.Style, xOff, yOff vg.Length, c color.Color, ls draw.LineStyle)

// stocks arranges the stocks by category.
// The first key is the category and the seond
Expand Down Expand Up @@ -179,7 +179,7 @@ func NewSankey(flows ...Flow) (*Sankey, error) {
if err != nil {
return nil, err
}
s.TextStyle = text.TextStyle{
s.TextStyle = text.Style{
Font: fnt,
Rotation: math.Pi / 2,
XAlign: draw.XCenter,
Expand All @@ -192,7 +192,7 @@ func NewSankey(flows ...Flow) (*Sankey, error) {
return s.Color, s.LineStyle
}

s.StockStyle = func(label string, category int) (string, text.TextStyle, vg.Length, vg.Length, color.Color, draw.LineStyle) {
s.StockStyle = func(label string, category int) (string, text.Style, vg.Length, vg.Length, color.Color, draw.LineStyle) {
return label, s.TextStyle, 0, 0, s.Color, s.LineStyle
}

Expand Down
2 changes: 1 addition & 1 deletion plotter/sankey_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func ExampleSankey_grouped() {

// Here we set the StockStyle function to give an example of
// setting a custom style for one of the stocks.
sankey.StockStyle = func(label string, category int) (string, text.TextStyle, vg.Length, vg.Length, color.Color, draw.LineStyle) {
sankey.StockStyle = func(label string, category int) (string, text.Style, vg.Length, vg.Length, color.Color, draw.LineStyle) {
if label == "Small" && category == treeType {
// Here we demonstrate how to rotate the label text
// and change the style of the stock bar.
Expand Down
4 changes: 2 additions & 2 deletions text/latex.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (hdlr Latex) Box(txt string, fnt vg.Font) (width, height, depth vg.Length)

// Draw renders the given text with the provided style and position
// on the canvas.
func (hdlr Latex) Draw(c vg.Canvas, txt string, sty TextStyle, pt vg.Point) {
func (hdlr Latex) Draw(c vg.Canvas, txt string, sty Style, pt vg.Point) {
cnv := drawtex.New()
fnts := &ttf.Fonts{
Rm: sty.Font.Font(),
Expand Down Expand Up @@ -136,7 +136,7 @@ func (hdlr Latex) dpi() float64 {

type latex struct {
cnv vg.Canvas
sty TextStyle
sty Style
pt vg.Point

w vg.Length
Expand Down
2 changes: 1 addition & 1 deletion text/latex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestLatexText(t *testing.T) {
fnt = tr12
}

sty := text.TextStyle{
sty := text.Style{
Font: fnt,
Handler: text.Latex{},
}
Expand Down
2 changes: 1 addition & 1 deletion text/plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (hdlr Plain) Box(txt string, fnt vg.Font) (width, height, depth vg.Length)

// Draw renders the given text with the provided style and position
// on the canvas.
func (hdlr Plain) Draw(c vg.Canvas, txt string, sty TextStyle, pt vg.Point) {
func (hdlr Plain) Draw(c vg.Canvas, txt string, sty Style, pt vg.Point) {
txt = strings.TrimRight(txt, "\n")
if len(txt) == 0 {
return
Expand Down
2 changes: 1 addition & 1 deletion text/plain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestPlainText(t *testing.T) {
fnt = tr12
}

sty := text.TextStyle{
sty := text.Style{
Font: fnt,
Handler: text.Plain{},
}
Expand Down
Loading

0 comments on commit a04713a

Please sign in to comment.