Skip to content

Commit

Permalink
plot: fix handling of font descents
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak committed Nov 3, 2020
1 parent cf58149 commit a51a196
Show file tree
Hide file tree
Showing 89 changed files with 694 additions and 689 deletions.
15 changes: 9 additions & 6 deletions axis.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,20 +262,21 @@ func (a horizontalAxis) draw(c draw.Canvas) {
x -= a.Label.Font.Width(a.Label.Text) / 2
}
if a.Label.Text != "" {
y -= a.Label.Font.Extents().Descent
c.FillText(a.Label.TextStyle, vg.Point{X: x, Y: y}, a.Label.Text)
descent := a.Label.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.Padding
}

marks := a.Tick.Marker.Ticks(a.Min, a.Max)
ticklabelheight := tickLabelHeight(a.Tick.Label, marks)
descent := a.Tick.Label.Font.Extents().Descent
for _, t := range marks {
x := c.X(a.Norm(t.Value))
if !c.ContainsX(x) || t.IsMinor() {
continue
}
c.FillText(a.Tick.Label, vg.Point{X: x, Y: y + ticklabelheight}, t.Label)
c.FillText(a.Tick.Label, vg.Point{X: x, Y: y + ticklabelheight - descent}, t.Label)
}

if len(marks) > 0 {
Expand Down Expand Up @@ -362,8 +363,9 @@ func (a verticalAxis) draw(c draw.Canvas) {
y = c.Max.Y
y -= a.Label.Font.Width(a.Label.Text) / 2
}
c.FillText(sty, vg.Point{X: x, Y: y}, a.Label.Text)
x += -a.Label.Font.Extents().Descent
descent := a.Label.Font.Extents().Descent
c.FillText(sty, vg.Point{X: x + descent, Y: y}, a.Label.Text)
x -= descent
x += a.Label.Padding
}
marks := a.Tick.Marker.Ticks(a.Min, a.Max)
Expand All @@ -372,12 +374,13 @@ func (a verticalAxis) draw(c draw.Canvas) {
}

major := false
descent := a.Tick.Label.Font.Extents().Descent
for _, t := range marks {
y := c.Y(a.Norm(t.Value))
if !c.ContainsY(y) || t.IsMinor() {
continue
}
c.FillText(a.Tick.Label, vg.Point{X: x, Y: y}, t.Label)
c.FillText(a.Tick.Label, vg.Point{X: x, Y: y - descent}, t.Label)
major = true
}
if major {
Expand Down
5 changes: 3 additions & 2 deletions legend.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ func (l *Legend) Draw(c draw.Canvas) {
textx += l.XOffs
iconx += l.XOffs

enth := l.entryHeight()
descent := sty.Font.Extents().Descent
enth := l.entryHeight() + descent
y := c.Max.Y - enth
if !l.Top {
y = c.Min.Y + (enth+l.Padding)*(vg.Length(len(l.entries))-1)
Expand All @@ -124,7 +125,7 @@ func (l *Legend) Draw(c draw.Canvas) {
panic("plot: invalid vertical offset for the legend's entries")
}
yoff := vg.Length(l.YPosition-draw.PosBottom) / 2
yoff *= -sty.Font.Extents().Descent
yoff -= descent

for _, e := range l.entries {
for _, t := range e.thumbs {
Expand Down
Binary file modified palette/moreland/testdata/moreland_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified palette/testdata/reverse_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified palette/testdata/reverse_palette_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ func (p *Plot) Draw(c draw.Canvas) {
c.Fill(c.Rectangle.Path())
}
if p.Title.Text != "" {
c.FillText(p.Title.TextStyle, vg.Point{X: c.Center().X, Y: c.Max.Y}, 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)
c.Max.Y -= h + d
c.Max.Y -= p.Title.Padding
Expand Down
Binary file modified plotter/testdata/barChart2_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/barChart_positiveNegative_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/bubbles_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/clippedFilledLine_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/colorBarHorizontalLog_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/colorBarHorizontal_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/colorBarVertical_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/color_field_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/errorBars_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/field_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/filledLine_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/functions_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/gopher_field_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/groupedBoxPlot_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/groupedQuartPlot_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/heatMap_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/histogram_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/histogram_logy_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/horizontalBarChart_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/horizontalBoxPlot_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/horizontalQuartPlot_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/image_plot_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/image_plot_log_golden.png
Binary file modified plotter/testdata/invertedlogscale_golden.png
Binary file modified plotter/testdata/labels_cnv_coords_golden.png
Binary file modified plotter/testdata/labels_golden.png
Binary file modified plotter/testdata/logscale_golden.png
Binary file modified plotter/testdata/plotLogo_golden.png
Binary file modified plotter/testdata/polygon_hexagons_golden.png
140 changes: 70 additions & 70 deletions plotter/testdata/polygon_holes_golden.eps
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
%%Creator gonum.org/v1/plot/vg/vgeps
%%Title:
%%BoundingBox: 0 0 100 100
%%CreationDate: 2020-05-18 15:26:47.780474519 +0200 CEST m=+5.317328037
%%CreationDate: 2020-11-02 12:45:40.941971207 +1030 ACDT m=+2.016279260
%%Orientation: Portrait
%%EndComments

Expand All @@ -20,135 +20,135 @@ fill
/Times-Roman findfont 12 scalefont setfont
3.6641 88.445 moveto
(Polygon with holes) show
62.75 3.8613 moveto
64.569 3.8613 moveto
(X) show
/Times-Roman findfont 10 scalefont setfont
34.166 15.602 moveto
37.805 18.634 moveto
(0) show
64.583 15.602 moveto
66.402 18.634 moveto
(2) show
95 15.602 moveto
95 18.634 moveto
(4) show
0.5 setlinewidth
newpath
36.666 25.23 moveto
36.666 33.23 lineto
40.305 28.263 moveto
40.305 36.263 lineto
stroke
newpath
67.083 25.23 moveto
67.083 33.23 lineto
68.902 28.263 moveto
68.902 36.263 lineto
stroke
newpath
97.5 25.23 moveto
97.5 33.23 lineto
97.5 28.263 moveto
97.5 36.263 lineto
stroke
newpath
51.875 29.23 moveto
51.875 33.23 lineto
54.604 32.263 moveto
54.604 36.263 lineto
stroke
newpath
82.292 29.23 moveto
82.292 33.23 lineto
83.201 32.263 moveto
83.201 36.263 lineto
stroke
newpath
36.666 33.23 moveto
97.5 33.23 lineto
40.305 36.263 moveto
97.5 36.263 lineto
stroke
gsave
90 rotate
/Times-Roman findfont 12 scalefont setfont
54.746 -11.555 moveto
57.323 -11.555 moveto
(Y) show
grestore
15.416 33.759 moveto
19.055 41.946 moveto
(0) show
15.416 54.357 moveto
19.055 58.45 moveto
(2) show
15.416 74.955 moveto
19.055 74.955 moveto
(4) show
newpath
22.916 38.48 moveto
30.916 38.48 lineto
26.555 45.151 moveto
34.555 45.151 lineto
stroke
newpath
22.916 59.079 moveto
30.916 59.079 lineto
26.555 61.656 moveto
34.555 61.656 lineto
stroke
newpath
22.916 79.677 moveto
30.916 79.677 lineto
26.555 78.161 moveto
34.555 78.161 lineto
stroke
newpath
26.916 48.78 moveto
30.916 48.78 lineto
30.555 53.404 moveto
34.555 53.404 lineto
stroke
newpath
26.916 69.378 moveto
30.916 69.378 lineto
30.555 69.908 moveto
34.555 69.908 lineto
stroke
newpath
30.916 38.48 moveto
30.916 79.677 lineto
34.555 45.151 moveto
34.555 78.161 lineto
stroke
0 0 1 setrgbcolor
newpath
36.666 38.48 moveto
97.5 38.48 lineto
97.5 79.677 lineto
36.666 79.677 lineto
40.305 45.151 moveto
97.5 45.151 lineto
97.5 78.161 lineto
40.305 78.161 lineto
closepath
44.27 43.63 moveto
59.479 43.63 lineto
59.479 53.929 lineto
44.27 53.929 lineto
47.454 49.278 moveto
61.753 49.278 lineto
61.753 57.53 lineto
47.454 57.53 lineto
closepath
89.896 64.228 moveto
74.687 64.228 lineto
74.687 74.527 lineto
89.896 74.527 lineto
90.351 65.782 moveto
76.052 65.782 lineto
76.052 74.034 lineto
90.351 74.034 lineto
closepath
fill
0 0 0 setrgbcolor
1 setlinewidth
newpath
36.666 38.48 moveto
97.5 38.48 lineto
97.5 79.677 lineto
36.666 79.677 lineto
36.666 38.48 lineto
40.305 45.151 moveto
97.5 45.151 lineto
97.5 78.161 lineto
40.305 78.161 lineto
40.305 45.151 lineto
stroke
newpath
44.27 43.63 moveto
59.479 43.63 lineto
59.479 53.929 lineto
44.27 53.929 lineto
44.27 43.63 lineto
47.454 49.278 moveto
61.753 49.278 lineto
61.753 57.53 lineto
47.454 57.53 lineto
47.454 49.278 lineto
stroke
newpath
89.896 64.228 moveto
74.687 64.228 lineto
74.687 74.527 lineto
89.896 74.527 lineto
89.896 64.228 lineto
90.351 65.782 moveto
76.052 65.782 lineto
76.052 74.034 lineto
90.351 74.034 lineto
90.351 65.782 lineto
stroke
0 0 1 setrgbcolor
newpath
90 38.48 moveto
90 46.332 lineto
100 46.332 lineto
100 38.48 lineto
90 45.151 moveto
90 53.003 lineto
100 53.003 lineto
100 45.151 lineto
closepath
fill
0 0 0 setrgbcolor
newpath
90 38.48 moveto
90 46.332 lineto
100 46.332 lineto
100 38.48 lineto
90 38.48 lineto
90 45.151 moveto
90 53.003 lineto
100 53.003 lineto
100 45.151 lineto
90 45.151 lineto
stroke
1 1 1 setrgbcolor
/Times-Roman findfont 8 scalefont setfont
76.449 38.629 moveto
76.449 46.513 moveto
(key) show
showpage
Binary file modified plotter/testdata/polygon_holes_golden.pdf
Binary file not shown.
Binary file modified plotter/testdata/polygon_holes_golden.png
54 changes: 27 additions & 27 deletions plotter/testdata/polygon_holes_golden.svg
Binary file modified plotter/testdata/precision_golden.png
Binary file modified plotter/testdata/rasterHeatMap_golden.png
Binary file modified plotter/testdata/rotation_golden.png
Binary file modified plotter/testdata/sankeyGrouped_golden.png
Binary file modified plotter/testdata/sankeySimple_golden.png
Binary file modified plotter/testdata/scatterColor_golden.png
Binary file modified plotter/testdata/scatter_golden.png
Binary file modified plotter/testdata/stackedBarChart_golden.png
Binary file modified plotter/testdata/step_golden.png
Binary file modified plotter/testdata/timeseries_golden.png
Binary file modified plotter/testdata/verticalBarChart_golden.png
Binary file modified plotter/testdata/verticalBoxPlot_golden.png
Binary file modified plotter/testdata/verticalQuartPlot_golden.png
Binary file modified plotter/testdata/volcano_golden.png
Binary file modified testdata/align_golden.png
Binary file modified testdata/axis_labels_golden.png
Binary file modified testdata/axis_padding_00_golden.png
Binary file modified testdata/axis_padding_05_golden.png
Binary file modified testdata/axis_padding_10_golden.png
Binary file modified testdata/legendAlignment_golden.png
Binary file modified testdata/legend_standalone_golden.png
2 changes: 1 addition & 1 deletion vg/draw/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (sty TextStyle) Height(txt string) vg.Length {
return vg.Length(0)
}
e := sty.Font.Extents()
return e.Height*vg.Length(nl-1) + e.Ascent
return e.Height*vg.Length(nl-1) + e.Ascent - e.Descent
}

// Rectangle returns a rectangle giving the bounds of
Expand Down
Binary file modified vg/testdata/cosine_golden.png
Binary file modified vg/testdata/sine_golden.png
Binary file modified vg/testdata/width_-1.jpg
Binary file modified vg/testdata/width_-1.pdf
Binary file not shown.
Binary file modified vg/testdata/width_-1.png
Loading

0 comments on commit a51a196

Please sign in to comment.