From cb5c2df26307f3c9b39e63167be59c5d00e54eef Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Sun, 10 Jul 2016 23:55:57 -0700 Subject: [PATCH] worked without scaling!! --- drawing/raster_graphic_context.go | 3 +- raster_renderer.go | 6 ++-- testserver/main.go | 50 +++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/drawing/raster_graphic_context.go b/drawing/raster_graphic_context.go index cdf894e..1ca0c30 100644 --- a/drawing/raster_graphic_context.go +++ b/drawing/raster_graphic_context.go @@ -149,8 +149,7 @@ func (rgc *RasterGraphicContext) CreateStringPath(s string, x, y float64) (curso return } -// GetStringBounds returns the approximate points bounds of a string. -// It will be required to convert points to pixels for measurement. +// GetStringBounds returns the approximate pixel bounds of a string. func (rgc *RasterGraphicContext) GetStringBounds(s string) (left, top, right, bottom float64, err error) { f := rgc.GetFont() if f == nil { diff --git a/raster_renderer.go b/raster_renderer.go index 4f0e0cc..fa94050 100644 --- a/raster_renderer.go +++ b/raster_renderer.go @@ -132,10 +132,8 @@ func (rr *rasterRenderer) MeasureText(body string) (width int, height int) { if err != nil { return } - dw := r - l - dh := b - t - width = int(drawing.PointsToPixels(rr.gc.GetDPI(), dw)) - height = int(drawing.PointsToPixels(rr.gc.GetDPI(), dh)) + width = int(r - l) + height = int(b - t) return } diff --git a/testserver/main.go b/testserver/main.go index e39d446..00c652e 100644 --- a/testserver/main.go +++ b/testserver/main.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/wcharczuk/go-chart" + "github.com/wcharczuk/go-chart/drawing" "github.com/wcharczuk/go-web" ) @@ -83,6 +84,54 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult { return nil } +func measureTestHandler(rc *web.RequestContext) web.ControllerResult { + format, err := rc.RouteParameter("format") + if err != nil { + format = "png" + } + + if format == "png" { + rc.Response.Header().Set("Content-Type", "image/png") + } else { + rc.Response.Header().Set("Content-Type", "image/svg+xml") + } + + var r chart.Renderer + if format == "png" { + r, err = chart.PNG(512, 512) + } else { + r, err = chart.SVG(512, 512) + } + if err != nil { + return rc.API().InternalError(err) + } + + f, err := chart.GetDefaultFont() + if err != nil { + return rc.API().InternalError(err) + } + r.SetDPI(96) + r.SetFont(f) + r.SetFontSize(24.0) + r.SetFontColor(drawing.ColorBlack) + r.SetStrokeColor(drawing.ColorBlack) + + tx, ty := 64, 64 + + tw, th := r.MeasureText("test") + r.MoveTo(tx, ty) + r.LineTo(tx+tw, ty) + r.LineTo(tx+tw, ty-th) + r.LineTo(tx, ty-th) + r.LineTo(tx, ty) + r.Stroke() + + r.Text("test", tx, ty) + + r.Save(rc.Response) + return nil +} + func main() { app := web.New() app.SetName("Chart Test Server") @@ -92,5 +141,6 @@ func main() { app.GET("/favico.ico", func(rc *web.RequestContext) web.ControllerResult { return rc.Raw([]byte{}) }) + app.GET("/measure", measureTestHandler) log.Fatal(app.Start()) }