fix string alignment with new gofpdf api

This commit is contained in:
Stani 2015-07-12 20:48:52 +02:00
parent bbb2a4b372
commit 1f0b304e2e
2 changed files with 14 additions and 4 deletions

View file

@ -138,10 +138,21 @@ func (gc *GraphicContext) GetDPI() int {
}
// GetStringBounds returns the approximate pixel bounds of the string s at x, y.
// The left edge of the em square of the first character of s
// and the baseline intersect at 0, 0 in the returned coordinates.
// Therefore the top and left coordinates may well be negative.
func (gc *GraphicContext) GetStringBounds(s string) (left, top, right, bottom float64) {
_, h := gc.pdf.GetFontSize()
d := gc.pdf.GetFontDesc("", "")
if d.Ascent == 0 {
// not defined (standard font?), use average of 81%
top = 0.81 * h
} else {
top = -float64(d.Ascent) * h / float64(d.Ascent-d.Descent)
}
bottom = top + h
margin := gc.pdf.GetCellMargin()
return margin, -h, margin + gc.pdf.GetStringWidth(s), 0
return -margin, top, -margin + gc.pdf.GetStringWidth(s), bottom
}
// CreateStringPath creates a path from the string s at x, y, and returns the string width.
@ -151,8 +162,7 @@ func (gc *GraphicContext) CreateStringPath(text string, x, y float64) (cursor fl
w := right - left
h := bottom - top
// gc.pdf.SetXY(x, y-h) do not use this as y-h might be negative
margin := gc.pdf.GetCellMargin()
gc.pdf.MoveTo(x-margin, y-margin-0.82*h)
gc.pdf.MoveTo(x+left, y+top)
gc.pdf.CellFormat(w, h, text, "", 0, "BL", false, 0, "")
return w
}

View file

@ -204,7 +204,7 @@ func (gc *ImageGraphicContext) CreateStringPath(s string, x, y float64) float64
}
// GetStringBounds returns the approximate pixel bounds of the string s at x, y.
// The the left edge of the em square of the first character of s
// The left edge of the em square of the first character of s
// and the baseline intersect at 0, 0 in the returned coordinates.
// Therefore the top and left coordinates may well be negative.
func (gc *ImageGraphicContext) GetStringBounds(s string) (left, top, right, bottom float64) {