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 stroke drawStyle
font *Font font *Font
fontSize float64 fontSize float64
textAlign textAlign
lineWidth float64 lineWidth float64
lineJoin lineJoin lineJoin lineJoin
lineEnd lineEnd lineEnd lineEnd
@ -75,6 +76,16 @@ const (
Butt Butt
) )
type textAlign uint8
const (
Left = iota
Center
Right
Start
End
)
// New creates a new canvas with the given viewport coordinates. // New creates a new canvas with the given viewport coordinates.
// While all functions on the canvas use the top left point as // While all functions on the canvas use the top left point as
// the origin, since GL uses the bottom left coordinate, the // 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 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. // SetLineJoin sets the style of line joints for rendering a path with Stroke.
// The value can be Miter, Bevel, or Round // The value can be Miter, Bevel, or Round
func (cv *Canvas) SetLineJoin(join lineJoin) { 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 { for i, rn := range str {
idx := fnt.Index(rn) idx := fnt.Index(rn)
if idx == 0 { if idx == 0 {
continue idx = fnt.Index(' ')
} }
bounds, err := frc.glyphBounds(idx, p) bounds, err := frc.glyphBounds(idx, p)
if err != nil { if err != nil {
@ -175,6 +175,12 @@ func (cv *Canvas) FillText(str string, x, y float64) {
curX += float64(advance) / 64 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) gli.BindBuffer(gl_ARRAY_BUFFER, buf)
vertex, alphaTexCoord := cv.useAlphaShader(&cv.state.fill, 1) vertex, alphaTexCoord := cv.useAlphaShader(&cv.state.fill, 1)