From bbb2a4b372ed4b1fdf902c42d42ff036d754709f Mon Sep 17 00:00:00 2001 From: Stani Date: Sun, 12 Jul 2015 20:47:35 +0200 Subject: [PATCH 1/2] add extra lines to check baseline --- samples/helloworld/helloworld.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/samples/helloworld/helloworld.go b/samples/helloworld/helloworld.go index 1c1eba1..9287fc2 100644 --- a/samples/helloworld/helloworld.go +++ b/samples/helloworld/helloworld.go @@ -41,6 +41,11 @@ func Draw(gc draw2d.GraphicContext, text string) { gc.SetDPI(72) gc.SetFontSize(14) // 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.FillStringAt(text, 8, 52) From 1f0b304e2e07fe006a09a0a1cc29a992cb80126f Mon Sep 17 00:00:00 2001 From: Stani Date: Sun, 12 Jul 2015 20:48:52 +0200 Subject: [PATCH 2/2] fix string alignment with new gofpdf api --- draw2dpdf/gc.go | 16 +++++++++++++--- image.go | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/draw2dpdf/gc.go b/draw2dpdf/gc.go index 1d3f068..9314986 100644 --- a/draw2dpdf/gc.go +++ b/draw2dpdf/gc.go @@ -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 } diff --git a/image.go b/image.go index 8e5fa72..2030a32 100644 --- a/image.go +++ b/image.go @@ -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) {