From be91a632c9466a0f142e2c821de37d9653a35bfe Mon Sep 17 00:00:00 2001 From: Stani Date: Sat, 27 Jun 2015 19:41:34 +0200 Subject: [PATCH] fixed the SetFontData method --- font.go | 6 +++--- pdf2d/graphiccontext.go | 29 +++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/font.go b/font.go index b02de34..e62ef28 100644 --- a/font.go +++ b/font.go @@ -38,7 +38,7 @@ type FontData struct { Style FontStyle } -func fontFileName(fontData FontData) string { +func FontFileName(fontData FontData) string { fontFileName := fontData.Name switch fontData.Family { case FontFamilySans: @@ -62,11 +62,11 @@ func fontFileName(fontData FontData) string { } func RegisterFont(fontData FontData, font *truetype.Font) { - fonts[fontFileName(fontData)] = font + fonts[FontFileName(fontData)] = font } func GetFont(fontData FontData) *truetype.Font { - fontFileName := fontFileName(fontData) + fontFileName := FontFileName(fontData) font := fonts[fontFileName] if font != nil { return font diff --git a/pdf2d/graphiccontext.go b/pdf2d/graphiccontext.go index 1ded040..ec1ae28 100644 --- a/pdf2d/graphiccontext.go +++ b/pdf2d/graphiccontext.go @@ -191,12 +191,33 @@ func (gc *GraphicContext) SetFillColor(c color.Color) { gc.pdf.SetFillColor(rgb(c)) } -// SetFont sets the font used to draw text. +// SetFont is unsupported by the pdf graphic context, use SetFontData +// instead. +func (gc *GraphicContext) SetFont(font *truetype.Font) { + // TODO: what to do with this api conflict between draw2d and gofpdf?! +} + +// SetFontData sets the current font used to draw text. Always use +// this method, as SetFont is unsupported by the pdf graphic context. // It is mandatory to call this method at least once before printing // text or the resulting document will not be valid. -func (gc *GraphicContext) SetFont(font *truetype.Font) { - // TODO: this api conflict needs to be fixed - gc.pdf.SetFont("Helvetica", "", 12) +// It is necessary to generate a font definition file first with the +// makefont utility. It is not necessary to call this function for the +// core PDF fonts (courier, helvetica, times, zapfdingbats). +// go get github.com/jung-kurt/gofpdf/makefont +// http://godoc.org/github.com/jung-kurt/gofpdf#Fpdf.AddFont +func (gc *GraphicContext) SetFontData(fontData draw2d.FontData) { + gc.StackGraphicContext.SetFontData(fontData) + var style string + if fontData.Style&draw2d.FontStyleBold != 0 { + style += "B" + } + if fontData.Style&draw2d.FontStyleItalic != 0 { + style += "I" + } + fn := draw2d.FontFileName(fontData) + fn = fn[:len(fn)-4] + gc.pdf.AddFont(fn, style, fn+".json") } // SetFontSize sets the font size in points (as in ``a 12 point font'').