Merge pull request #65 from stanim/master

Fixes for text in draw2dpdf
This commit is contained in:
Stani 2015-07-12 02:43:32 +02:00
commit 339f012445
5 changed files with 16 additions and 6 deletions

View file

@ -45,6 +45,7 @@ var (
func NewPdf(orientationStr, unitStr, sizeStr string) *gofpdf.Fpdf { func NewPdf(orientationStr, unitStr, sizeStr string) *gofpdf.Fpdf {
pdf := gofpdf.New(orientationStr, unitStr, sizeStr, draw2d.GetFontFolder()) pdf := gofpdf.New(orientationStr, unitStr, sizeStr, draw2d.GetFontFolder())
// to be compatible with draw2d // to be compatible with draw2d
pdf.SetMargins(0, 0, 0)
pdf.SetDrawColor(0, 0, 0) pdf.SetDrawColor(0, 0, 0)
pdf.SetFillColor(255, 255, 255) pdf.SetFillColor(255, 255, 255)
pdf.SetLineCapStyle("round") 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. // GetStringBounds returns the approximate pixel bounds of the string s at x, y.
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()
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. // 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) { 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() 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.CellFormat(w, h, text, "", 0, "BL", false, 0, "")
// gc.pdf.Cell(w, h, text)
return w return w
} }
@ -250,7 +255,9 @@ func (gc *GraphicContext) SetFontData(fontData draw2d.FontData) {
} }
fn := draw2d.FontFileName(fontData) fn := draw2d.FontFileName(fontData)
fn = fn[:len(fn)-4] 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''). // SetFontSize sets the font size in points (as in ``a 12 point font'').

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

View file

@ -210,8 +210,9 @@ func FillString(gc draw2d.GraphicContext, x, y, width, height float64) {
left, top, right, bottom := gc.GetStringBounds("cou") left, top, right, bottom := gc.GetStringBounds("cou")
gc.SetStrokeColor(color.NRGBA{255, 0x33, 0x33, 0x80}) gc.SetStrokeColor(color.NRGBA{255, 0x33, 0x33, 0x80})
draw2d.Rect(gc, left, top, right, bottom) draw2d.Rect(gc, left, top, right, bottom)
gc.SetLineWidth(height / 20) gc.SetLineWidth(height / 50)
gc.Stroke() gc.Stroke()
gc.SetFillColor(color.NRGBA{0x33, 0x33, 0xff, 0xff})
gc.SetStrokeColor(color.NRGBA{0x33, 0x33, 0xff, 0xff}) gc.SetStrokeColor(color.NRGBA{0x33, 0x33, 0xff, 0xff})
gc.SetLineWidth(height / 100) gc.SetLineWidth(height / 100)
gc.StrokeString("cou") gc.StrokeString("cou")

View file

@ -46,10 +46,12 @@ func Draw(gc draw2d.GraphicContext, text string) {
gc.Save() gc.Save()
gc.SetFillColor(color.NRGBA{0xFF, 0x33, 0x33, 0xFF}) gc.SetFillColor(color.NRGBA{0xFF, 0x33, 0x33, 0xFF})
gc.SetStrokeColor(color.NRGBA{0xFF, 0x33, 0x33, 0xFF})
gc.Translate(145, 85) gc.Translate(145, 85)
gc.StrokeStringAt(text, -50, 0) gc.StrokeStringAt(text, -50, 0)
gc.Rotate(math.Pi / 4) gc.Rotate(math.Pi / 4)
gc.SetFillColor(color.NRGBA{0x33, 0x33, 0xFF, 0xFF}) gc.SetFillColor(color.NRGBA{0x33, 0x33, 0xFF, 0xFF})
gc.SetStrokeColor(color.NRGBA{0x33, 0x33, 0xFF, 0xFF})
gc.StrokeString(text) gc.StrokeString(text)
gc.Restore() gc.Restore()
} }