Merge pull request #67 from stanim/text

Fix text alignment
This commit is contained in:
Stani 2015-07-12 20:50:14 +02:00
commit bc823442ba
3 changed files with 19 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. // 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) { func (gc *GraphicContext) GetStringBounds(s string) (left, top, right, bottom float64) {
_, h := gc.pdf.GetFontSize() _, 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() 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. // 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 w := right - left
h := bottom - top h := bottom - top
// gc.pdf.SetXY(x, y-h) do not use this as y-h might be negative // gc.pdf.SetXY(x, y-h) do not use this as y-h might be negative
margin := gc.pdf.GetCellMargin() gc.pdf.MoveTo(x+left, y+top)
gc.pdf.MoveTo(x-margin, y-margin-0.82*h)
gc.pdf.CellFormat(w, h, text, "", 0, "BL", false, 0, "") gc.pdf.CellFormat(w, h, text, "", 0, "BL", false, 0, "")
return w 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. // 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. // and the baseline intersect at 0, 0 in the returned coordinates.
// Therefore the top and left coordinates may well be negative. // Therefore the top and left coordinates may well be negative.
func (gc *ImageGraphicContext) GetStringBounds(s string) (left, top, right, bottom float64) { func (gc *ImageGraphicContext) GetStringBounds(s string) (left, top, right, bottom float64) {

View file

@ -41,6 +41,11 @@ func Draw(gc draw2d.GraphicContext, text string) {
gc.SetDPI(72) gc.SetDPI(72)
gc.SetFontSize(14) gc.SetFontSize(14)
// Display Hello World // Display Hello World
gc.SetStrokeColor(color.NRGBA{0x33, 0xFF, 0x33, 0xFF})
gc.MoveTo(8, 0)
gc.LineTo(8, 52)
gc.LineTo(297, 52)
gc.Stroke()
gc.FillString(text) gc.FillString(text)
gc.FillStringAt(text, 8, 52) gc.FillStringAt(text, 8, 52)