Skip to content

Commit

Permalink
all: correctly handle descent in glyphbox of text symbols
Browse files Browse the repository at this point in the history
Fixes #649.
  • Loading branch information
sbinet committed Dec 18, 2020
1 parent 35049c6 commit 89f7b11
Show file tree
Hide file tree
Showing 67 changed files with 927 additions and 336 deletions.
4 changes: 2 additions & 2 deletions legend.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (l *Legend) Draw(c draw.Canvas) {
iconx += l.XOffs

descent := sty.Font.Extents().Descent
enth := l.entryHeight() + descent
enth := l.entryHeight()
y := c.Max.Y - enth
if !l.Top {
y = c.Min.Y + (enth+l.Padding)*(vg.Length(len(l.entries))-1)
Expand All @@ -131,7 +131,7 @@ func (l *Legend) Draw(c draw.Canvas) {
for _, t := range e.thumbs {
t.Thumbnail(icon)
}
yoffs := (enth - sty.Rectangle(e.text).Max.Y) / 2
yoffs := (enth + descent - sty.Rectangle(e.text).Max.Y) / 2
yoffs += yoff
c.FillText(sty, vg.Point{X: textx, Y: icon.Min.Y + yoffs}, e.text)
icon.Min.Y -= enth + l.Padding
Expand Down
File renamed without changes.
113 changes: 113 additions & 0 deletions plotter/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,125 @@
package plotter_test

import (
"image/color"
"testing"

"gonum.org/v1/plot"
"gonum.org/v1/plot/cmpimg"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
)

func TestLabels(t *testing.T) {
cmpimg.CheckPlot(ExampleLabels, t, "labels.png")
cmpimg.CheckPlot(ExampleLabels_inCanvasCoordinates, t, "labels_cnv_coords.png")
}

// TestLabelsWithGlyphBoxes tests the position of the glyphbox around
// a block of text, checking whether we correctly take into account
// the descent+ascent of a glyph.
func TestLabelsWithGlyphBoxes(t *testing.T) {
cmpimg.CheckPlot(
func() {
const fontSize = 24

p, err := plot.New()
if err != nil {
t.Fatalf("could not create plot: %+v", err)
}

p.Title.Text = "Labels"
p.X.Min = -1
p.X.Max = +1
p.Y.Min = -1
p.Y.Max = +1

const (
left = 0.00
middle = 0.02
right = 0.04
)

labels, err := plotter.NewLabels(plotter.XYLabels{
XYs: []plotter.XY{
{X: -0.8 + left, Y: -0.5}, // Aq + y-align bottom
{X: -0.6 + middle, Y: -0.5}, // Aq + y-align center
{X: -0.4 + right, Y: -0.5}, // Aq + y-align top

{X: -0.8 + left, Y: +0.5}, // ditto for Aq\nAq
{X: -0.6 + middle, Y: +0.5},
{X: -0.4 + right, Y: +0.5},

{X: +0.0 + left, Y: +0}, // ditto for Bg\nBg\nBg
{X: +0.2 + middle, Y: +0},
{X: +0.4 + right, Y: +0},
},
Labels: []string{
"Aq", "Aq", "Aq",
"Aq\nAq", "Aq\nAq", "Aq\nAq",

"Bg\nBg\nBg",
"Bg\nBg\nBg",
"Bg\nBg\nBg",
},
})
if err != nil {
t.Fatalf("could not creates labels plotter: %+v", err)
}
for i := range labels.TextStyle {
sty := &labels.TextStyle[i]
sty.Font.Size = vg.Length(fontSize)
}
labels.TextStyle[0].YAlign = draw.YBottom
labels.TextStyle[1].YAlign = draw.YCenter
labels.TextStyle[2].YAlign = draw.YTop

labels.TextStyle[3].YAlign = draw.YBottom
labels.TextStyle[4].YAlign = draw.YCenter
labels.TextStyle[5].YAlign = draw.YTop

labels.TextStyle[6].YAlign = draw.YBottom
labels.TextStyle[7].YAlign = draw.YCenter
labels.TextStyle[8].YAlign = draw.YTop

lred, err := plotter.NewLabels(plotter.XYLabels{
XYs: []plotter.XY{
{X: -0.8 + left, Y: +0.5},
{X: +0.0 + left, Y: +0},
},
Labels: []string{
"Aq", "Bg",
},
})
if err != nil {
t.Fatalf("could not creates labels plotter: %+v", err)
}
for i := range lred.TextStyle {
sty := &lred.TextStyle[i]
sty.Font.Size = vg.Length(fontSize)
sty.Color = color.RGBA{R: 255, A: 255}
sty.YAlign = draw.YBottom
}

m5 := plotter.NewFunction(func(float64) float64 { return -0.5 })
m5.LineStyle.Color = color.RGBA{R: 255, A: 255}

l0 := plotter.NewFunction(func(float64) float64 { return 0 })
l0.LineStyle.Color = color.RGBA{G: 255, A: 255}

p5 := plotter.NewFunction(func(float64) float64 { return +0.5 })
p5.LineStyle.Color = color.RGBA{B: 255, A: 255}

p.Add(labels, lred, m5, l0, p5)
p.Add(plotter.NewGrid())
p.Add(plotter.NewGlyphBoxes())

err = p.Save(10*vg.Centimeter, 10*vg.Centimeter, "testdata/labels_glyphboxes.png")
if err != nil {
t.Fatalf("could save plot: %+v", err)
}
},
t, "labels_glyphboxes.png",
)
}
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/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/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.
477 changes: 477 additions & 0 deletions plotter/testdata/contour_golden.svg
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/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/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/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
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/labels_cnv_coords_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 added plotter/testdata/labels_glyphboxes_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/labels_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/plotLogo_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/polygon_hexagons_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 35 additions & 35 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-11-02 12:45:40.941971207 +1030 ACDT m=+2.016279260
%%CreationDate: 2020-12-14 16:10:53.813225989 +0100 CET m=+3.425219129
%%Orientation: Portrait
%%EndComments

Expand Down Expand Up @@ -57,79 +57,79 @@ stroke
gsave
90 rotate
/Times-Roman findfont 12 scalefont setfont
57.323 -11.555 moveto
58.839 -11.555 moveto
(Y) show
grestore
19.055 41.946 moveto
(0) show
19.055 58.45 moveto
19.055 59.967 moveto
(2) show
19.055 74.955 moveto
19.055 77.987 moveto
(4) show
newpath
26.555 45.151 moveto
34.555 45.151 lineto
stroke
newpath
26.555 61.656 moveto
34.555 61.656 lineto
26.555 63.172 moveto
34.555 63.172 lineto
stroke
newpath
26.555 78.161 moveto
34.555 78.161 lineto
26.555 81.193 moveto
34.555 81.193 lineto
stroke
newpath
30.555 53.404 moveto
34.555 53.404 lineto
30.555 54.162 moveto
34.555 54.162 lineto
stroke
newpath
30.555 69.908 moveto
34.555 69.908 lineto
30.555 72.182 moveto
34.555 72.182 lineto
stroke
newpath
34.555 45.151 moveto
34.555 78.161 lineto
34.555 81.193 lineto
stroke
0 0 1 setrgbcolor
newpath
40.305 45.151 moveto
97.5 45.151 lineto
97.5 78.161 lineto
40.305 78.161 lineto
97.5 81.193 lineto
40.305 81.193 lineto
closepath
47.454 49.278 moveto
61.753 49.278 lineto
61.753 57.53 lineto
47.454 57.53 lineto
47.454 49.657 moveto
61.753 49.657 lineto
61.753 58.667 lineto
47.454 58.667 lineto
closepath
90.351 65.782 moveto
76.052 65.782 lineto
76.052 74.034 lineto
90.351 74.034 lineto
90.351 67.677 moveto
76.052 67.677 lineto
76.052 76.688 lineto
90.351 76.688 lineto
closepath
fill
0 0 0 setrgbcolor
1 setlinewidth
newpath
40.305 45.151 moveto
97.5 45.151 lineto
97.5 78.161 lineto
40.305 78.161 lineto
97.5 81.193 lineto
40.305 81.193 lineto
40.305 45.151 lineto
stroke
newpath
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
47.454 49.657 moveto
61.753 49.657 lineto
61.753 58.667 lineto
47.454 58.667 lineto
47.454 49.657 lineto
stroke
newpath
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
90.351 67.677 moveto
76.052 67.677 lineto
76.052 76.688 lineto
90.351 76.688 lineto
90.351 67.677 lineto
stroke
0 0 1 setrgbcolor
newpath
Expand Down
Binary file modified plotter/testdata/polygon_holes_golden.pdf
Binary file not shown.
Binary file modified plotter/testdata/polygon_holes_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 12 additions & 12 deletions plotter/testdata/polygon_holes_golden.svg
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/precision_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/rasterHeatMap_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/rotation_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/sankeyGrouped_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/sankeySimple_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/step_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/verticalBarChart_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/verticalBoxPlot_golden.png
Binary file modified plotter/testdata/verticalQuartPlot_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/legend_standalone_golden.png
5 changes: 3 additions & 2 deletions vg/draw/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,17 @@ 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 - e.Descent
return e.Height * vg.Length(nl)
}

// Rectangle returns a rectangle giving the bounds of
// this text assuming that it is drawn at (0, 0).
func (sty TextStyle) Rectangle(txt string) vg.Rectangle {
e := sty.Font.Extents()
w := sty.Width(txt)
h := sty.Height(txt)
xoff := vg.Length(sty.XAlign) * w
yoff := vg.Length(sty.YAlign) * h
yoff := vg.Length(sty.YAlign)*h + e.Descent
// lower left corner
p1 := rotatePoint(sty.Rotation, vg.Point{X: xoff, Y: yoff})
// upper left corner
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
28 changes: 14 additions & 14 deletions vg/testdata/width_-1.svg
Binary file modified vg/testdata/width_-1.tiff
Binary file not shown.
Binary file modified vg/testdata/width_0.jpg
Binary file modified vg/testdata/width_0.pdf
Binary file not shown.
Binary file modified vg/testdata/width_0.png
Loading

0 comments on commit 89f7b11

Please sign in to comment.