diff --git a/draw2dpdf/gc.go b/draw2dpdf/gc.go index 2eb5ed8..1d3f068 100644 --- a/draw2dpdf/gc.go +++ b/draw2dpdf/gc.go @@ -45,6 +45,7 @@ var ( func NewPdf(orientationStr, unitStr, sizeStr string) *gofpdf.Fpdf { pdf := gofpdf.New(orientationStr, unitStr, sizeStr, draw2d.GetFontFolder()) // to be compatible with draw2d + pdf.SetMargins(0, 0, 0) pdf.SetDrawColor(0, 0, 0) pdf.SetFillColor(255, 255, 255) pdf.SetLineCapStyle("round") @@ -139,16 +140,20 @@ func (gc *GraphicContext) GetDPI() int { // GetStringBounds returns the approximate pixel bounds of the string s at x, y. func (gc *GraphicContext) GetStringBounds(s string) (left, top, right, bottom float64) { _, h := gc.pdf.GetFontSize() - return 0, 0, gc.pdf.GetStringWidth(s), h + margin := gc.pdf.GetCellMargin() + return margin, -h, margin + gc.pdf.GetStringWidth(s), 0 } // CreateStringPath creates a path from the string s at x, y, and returns the string width. func (gc *GraphicContext) CreateStringPath(text string, x, y float64) (cursor float64) { - _, _, w, h := gc.GetStringBounds(text) + //fpdf uses the top left corner + left, top, right, bottom := gc.GetStringBounds(text) + 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-margin, y-margin-0.82*h) gc.pdf.CellFormat(w, h, text, "", 0, "BL", false, 0, "") - // gc.pdf.Cell(w, h, text) return w } @@ -250,7 +255,9 @@ func (gc *GraphicContext) SetFontData(fontData draw2d.FontData) { } fn := draw2d.FontFileName(fontData) fn = fn[:len(fn)-4] - gc.pdf.AddFont(fn, style, fn+".json") + size, _ := gc.pdf.GetFontSize() + gc.pdf.AddFont(fontData.Name, style, fn+".json") + gc.pdf.SetFont(fontData.Name, style, size) } // SetFontSize sets the font size in points (as in ``a 12 point font''). diff --git a/output/samples/geometry.png b/output/samples/geometry.png index 216541a..99494ff 100644 Binary files a/output/samples/geometry.png and b/output/samples/geometry.png differ diff --git a/resource/image/geometry.pdf b/resource/image/geometry.pdf index ee16da2..6c5c1bc 100644 Binary files a/resource/image/geometry.pdf and b/resource/image/geometry.pdf differ diff --git a/samples/geometry/geometry.go b/samples/geometry/geometry.go index 692c43a..8fb105f 100644 --- a/samples/geometry/geometry.go +++ b/samples/geometry/geometry.go @@ -210,8 +210,9 @@ func FillString(gc draw2d.GraphicContext, x, y, width, height float64) { left, top, right, bottom := gc.GetStringBounds("cou") gc.SetStrokeColor(color.NRGBA{255, 0x33, 0x33, 0x80}) draw2d.Rect(gc, left, top, right, bottom) - gc.SetLineWidth(height / 20) + gc.SetLineWidth(height / 50) gc.Stroke() + gc.SetFillColor(color.NRGBA{0x33, 0x33, 0xff, 0xff}) gc.SetStrokeColor(color.NRGBA{0x33, 0x33, 0xff, 0xff}) gc.SetLineWidth(height / 100) gc.StrokeString("cou") diff --git a/samples/helloworld/helloworld.go b/samples/helloworld/helloworld.go index 301edcd..1c1eba1 100644 --- a/samples/helloworld/helloworld.go +++ b/samples/helloworld/helloworld.go @@ -46,10 +46,12 @@ func Draw(gc draw2d.GraphicContext, text string) { gc.Save() gc.SetFillColor(color.NRGBA{0xFF, 0x33, 0x33, 0xFF}) + gc.SetStrokeColor(color.NRGBA{0xFF, 0x33, 0x33, 0xFF}) gc.Translate(145, 85) gc.StrokeStringAt(text, -50, 0) gc.Rotate(math.Pi / 4) gc.SetFillColor(color.NRGBA{0x33, 0x33, 0xFF, 0xFF}) + gc.SetStrokeColor(color.NRGBA{0x33, 0x33, 0xFF, 0xFF}) gc.StrokeString(text) gc.Restore() }