diff --git a/canvas.go b/canvas.go index 83c2fac..6463f52 100644 --- a/canvas.go +++ b/canvas.go @@ -30,6 +30,7 @@ type drawState struct { stroke drawStyle font *Font fontSize float64 + textAlign textAlign lineWidth float64 lineJoin lineJoin lineEnd lineEnd @@ -75,6 +76,16 @@ const ( Butt ) +type textAlign uint8 + +const ( + Left = iota + Center + Right + Start + End +) + // New creates a new canvas with the given viewport coordinates. // While all functions on the canvas use the top left point as // the origin, since GL uses the bottom left coordinate, the @@ -481,6 +492,12 @@ func (cv *Canvas) SetFont(font interface{}, size float64) { cv.state.fontSize = size } +// SetTextAlign sets the text align for any text drawing calls. +// The value can be Left, Center, Right, Start, or End +func (cv *Canvas) SetTextAlign(align textAlign) { + cv.state.textAlign = align +} + // SetLineJoin sets the style of line joints for rendering a path with Stroke. // The value can be Miter, Bevel, or Round func (cv *Canvas) SetLineJoin(join lineJoin) { diff --git a/text.go b/text.go index e9c2bfb..f17d682 100644 --- a/text.go +++ b/text.go @@ -78,7 +78,7 @@ func (cv *Canvas) FillText(str string, x, y float64) { for i, rn := range str { idx := fnt.Index(rn) if idx == 0 { - continue + idx = fnt.Index(' ') } bounds, err := frc.glyphBounds(idx, p) if err != nil { @@ -175,6 +175,12 @@ func (cv *Canvas) FillText(str string, x, y float64) { curX += float64(advance) / 64 } + if cv.state.textAlign == Center { + x -= float64(strWidth) * 0.5 + } else if cv.state.textAlign == Right || cv.state.textAlign == End { + x -= float64(strWidth) + } + gli.BindBuffer(gl_ARRAY_BUFFER, buf) vertex, alphaTexCoord := cv.useAlphaShader(&cv.state.fill, 1)