implemented textAlign; unknown characters now act like a space

This commit is contained in:
Thomas Friedel 2018-04-19 17:02:28 +02:00
parent 30c6c8a270
commit 0489833474
2 changed files with 24 additions and 1 deletions

View file

@ -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) {

View file

@ -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)