worked without scaling!!

This commit is contained in:
Will Charczuk 2016-07-10 23:55:57 -07:00
parent 7cb416041d
commit cb5c2df263
3 changed files with 53 additions and 6 deletions

View File

@ -149,8 +149,7 @@ func (rgc *RasterGraphicContext) CreateStringPath(s string, x, y float64) (curso
return return
} }
// GetStringBounds returns the approximate points bounds of a string. // GetStringBounds returns the approximate pixel bounds of a string.
// It will be required to convert points to pixels for measurement.
func (rgc *RasterGraphicContext) GetStringBounds(s string) (left, top, right, bottom float64, err error) { func (rgc *RasterGraphicContext) GetStringBounds(s string) (left, top, right, bottom float64, err error) {
f := rgc.GetFont() f := rgc.GetFont()
if f == nil { if f == nil {

View File

@ -132,10 +132,8 @@ func (rr *rasterRenderer) MeasureText(body string) (width int, height int) {
if err != nil { if err != nil {
return return
} }
dw := r - l width = int(r - l)
dh := b - t height = int(b - t)
width = int(drawing.PointsToPixels(rr.gc.GetDPI(), dw))
height = int(drawing.PointsToPixels(rr.gc.GetDPI(), dh))
return return
} }

View File

@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"github.com/wcharczuk/go-chart" "github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/drawing"
"github.com/wcharczuk/go-web" "github.com/wcharczuk/go-web"
) )
@ -83,6 +84,54 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
return nil 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() { func main() {
app := web.New() app := web.New()
app.SetName("Chart Test Server") app.SetName("Chart Test Server")
@ -92,5 +141,6 @@ func main() {
app.GET("/favico.ico", func(rc *web.RequestContext) web.ControllerResult { app.GET("/favico.ico", func(rc *web.RequestContext) web.ControllerResult {
return rc.Raw([]byte{}) return rc.Raw([]byte{})
}) })
app.GET("/measure", measureTestHandler)
log.Fatal(app.Start()) log.Fatal(app.Start())
} }