added support for loading fonts and rendering text (highly unoptimized so far)
This commit is contained in:
parent
67142c3c8d
commit
85667e8f57
4 changed files with 140 additions and 2 deletions
|
@ -1,6 +1,7 @@
|
|||
package canvas
|
||||
|
||||
import (
|
||||
"image"
|
||||
"unsafe"
|
||||
|
||||
"github.com/tfriedel6/lm"
|
||||
|
@ -20,6 +21,12 @@ type Canvas struct {
|
|||
lineWidth float32
|
||||
}
|
||||
path []pathPoint
|
||||
text struct {
|
||||
font *Font
|
||||
size float32
|
||||
target *image.RGBA
|
||||
tex uint32
|
||||
}
|
||||
}
|
||||
|
||||
type pathPoint struct {
|
||||
|
|
33
color.go
33
color.go
|
@ -7,7 +7,7 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
func colorToGL(color color.Color) (r, g, b, a float32) {
|
||||
func colorGoToGL(color color.Color) (r, g, b, a float32) {
|
||||
ir, ig, ib, ia := color.RGBA()
|
||||
r = float32(ir) / 65535
|
||||
g = float32(ig) / 65535
|
||||
|
@ -16,6 +16,35 @@ func colorToGL(color color.Color) (r, g, b, a float32) {
|
|||
return
|
||||
}
|
||||
|
||||
func colorGLToGo(r, g, b, a float32) color.Color {
|
||||
if r < 0 {
|
||||
r = 0
|
||||
} else if r > 1 {
|
||||
r = 1
|
||||
}
|
||||
if g < 0 {
|
||||
g = 0
|
||||
} else if g > 1 {
|
||||
g = 1
|
||||
}
|
||||
if b < 0 {
|
||||
b = 0
|
||||
} else if b > 1 {
|
||||
b = 1
|
||||
}
|
||||
if a < 0 {
|
||||
a = 0
|
||||
} else if a > 1 {
|
||||
a = 1
|
||||
}
|
||||
return color.RGBA{
|
||||
R: uint8(r * 255),
|
||||
G: uint8(g * 255),
|
||||
B: uint8(b * 255),
|
||||
A: uint8(a * 255),
|
||||
}
|
||||
}
|
||||
|
||||
func parseHexRune(rn rune) (int, bool) {
|
||||
switch {
|
||||
case rn >= '0' && rn <= '9':
|
||||
|
@ -93,7 +122,7 @@ func parseColor(value ...interface{}) (r, g, b, a float32, ok bool) {
|
|||
if len(value) == 1 {
|
||||
switch v := value[0].(type) {
|
||||
case color.Color:
|
||||
r, g, b, a = colorToGL(v)
|
||||
r, g, b, a = colorGoToGL(v)
|
||||
ok = true
|
||||
return
|
||||
case [3]float32:
|
||||
|
|
|
@ -43,6 +43,7 @@ func LoadImage(src interface{}) (*Image, error) {
|
|||
func loadImageRGBA(src *image.RGBA) (*Image, error) {
|
||||
img := &Image{w: src.Bounds().Dx(), h: src.Bounds().Dy()}
|
||||
gli.GenTextures(1, &img.tex)
|
||||
gli.ActiveTexture(gl_TEXTURE0)
|
||||
gli.BindTexture(gl_TEXTURE_2D, img.tex)
|
||||
gli.TexParameteri(gl_TEXTURE_2D, gl_TEXTURE_MIN_FILTER, gl_LINEAR_MIPMAP_LINEAR)
|
||||
gli.TexParameteri(gl_TEXTURE_2D, gl_TEXTURE_MAG_FILTER, gl_LINEAR)
|
||||
|
@ -75,6 +76,7 @@ func loadImageRGBA(src *image.RGBA) (*Image, error) {
|
|||
func loadImageGray(src *image.Gray) (*Image, error) {
|
||||
img := &Image{w: src.Bounds().Dx(), h: src.Bounds().Dy()}
|
||||
gli.GenTextures(1, &img.tex)
|
||||
gli.ActiveTexture(gl_TEXTURE0)
|
||||
gli.BindTexture(gl_TEXTURE_2D, img.tex)
|
||||
gli.TexParameteri(gl_TEXTURE_2D, gl_TEXTURE_MIN_FILTER, gl_LINEAR_MIPMAP_LINEAR)
|
||||
gli.TexParameteri(gl_TEXTURE_2D, gl_TEXTURE_MAG_FILTER, gl_LINEAR)
|
||||
|
@ -107,6 +109,7 @@ func loadImageGray(src *image.Gray) (*Image, error) {
|
|||
func loadImageConverted(src image.Image) (*Image, error) {
|
||||
img := &Image{w: src.Bounds().Dx(), h: src.Bounds().Dy()}
|
||||
gli.GenTextures(1, &img.tex)
|
||||
gli.ActiveTexture(gl_TEXTURE0)
|
||||
gli.BindTexture(gl_TEXTURE_2D, img.tex)
|
||||
gli.TexParameteri(gl_TEXTURE_2D, gl_TEXTURE_MIN_FILTER, gl_LINEAR_MIPMAP_LINEAR)
|
||||
gli.TexParameteri(gl_TEXTURE_2D, gl_TEXTURE_MAG_FILTER, gl_LINEAR)
|
||||
|
|
99
text.go
Normal file
99
text.go
Normal file
|
@ -0,0 +1,99 @@
|
|||
package canvas
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"image"
|
||||
"io/ioutil"
|
||||
"unsafe"
|
||||
|
||||
"github.com/golang/freetype"
|
||||
"github.com/golang/freetype/truetype"
|
||||
"golang.org/x/image/math/fixed"
|
||||
)
|
||||
|
||||
var fontRenderingContext = freetype.NewContext()
|
||||
|
||||
type Font struct {
|
||||
font *truetype.Font
|
||||
}
|
||||
|
||||
func LoadFont(src interface{}) (*Font, error) {
|
||||
switch v := src.(type) {
|
||||
case *truetype.Font:
|
||||
return &Font{font: v}, nil
|
||||
case string:
|
||||
data, err := ioutil.ReadFile(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
font, err := freetype.ParseFont(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Font{font: font}, nil
|
||||
case []byte:
|
||||
font, err := freetype.ParseFont(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Font{font: font}, nil
|
||||
}
|
||||
return nil, errors.New("Unsupported source type")
|
||||
}
|
||||
|
||||
func (cv *Canvas) SetFont(font *Font, size float32) {
|
||||
cv.text.font = font
|
||||
cv.text.size = size
|
||||
}
|
||||
|
||||
func (cv *Canvas) FillText(str string, x, y float32) {
|
||||
cv.activate()
|
||||
|
||||
if cv.text.target == nil || cv.text.target.Bounds().Dx() != cv.w || cv.text.target.Bounds().Dy() != cv.h {
|
||||
if cv.text.tex != 0 {
|
||||
gli.DeleteTextures(1, &cv.text.tex)
|
||||
}
|
||||
cv.text.target = image.NewRGBA(image.Rect(0, 0, cv.w, cv.h))
|
||||
gli.GenTextures(1, &cv.text.tex)
|
||||
gli.ActiveTexture(gl_TEXTURE0)
|
||||
gli.BindTexture(gl_TEXTURE_2D, cv.text.tex)
|
||||
gli.TexParameteri(gl_TEXTURE_2D, gl_TEXTURE_MIN_FILTER, gl_NEAREST)
|
||||
gli.TexParameteri(gl_TEXTURE_2D, gl_TEXTURE_MAG_FILTER, gl_NEAREST)
|
||||
gli.TexParameteri(gl_TEXTURE_2D, gl_TEXTURE_WRAP_S, gl_CLAMP_TO_EDGE)
|
||||
gli.TexParameteri(gl_TEXTURE_2D, gl_TEXTURE_WRAP_T, gl_CLAMP_TO_EDGE)
|
||||
}
|
||||
|
||||
for i := range cv.text.target.Pix {
|
||||
cv.text.target.Pix[i] = 0
|
||||
}
|
||||
|
||||
fontRenderingContext.SetFont(cv.text.font.font)
|
||||
fontRenderingContext.SetFontSize(float64(cv.text.size))
|
||||
fontRenderingContext.SetSrc(image.NewUniform(colorGLToGo(cv.fill.r, cv.fill.g, cv.fill.b, cv.fill.a)))
|
||||
fontRenderingContext.SetDst(cv.text.target)
|
||||
fontRenderingContext.SetClip(cv.text.target.Bounds())
|
||||
fontRenderingContext.DrawString(str, fixed.Point26_6{X: fixed.Int26_6(x*64 + 0.5), Y: fixed.Int26_6(y*64 + 0.5)})
|
||||
|
||||
gli.BlendFunc(gl_ONE, gl_ONE_MINUS_SRC_ALPHA)
|
||||
|
||||
gli.ActiveTexture(gl_TEXTURE0)
|
||||
gli.BindTexture(gl_TEXTURE_2D, cv.text.tex)
|
||||
gli.TexImage2D(gl_TEXTURE_2D, 0, gl_RGBA, int32(cv.w), int32(cv.h), 0, gl_RGBA, gl_UNSIGNED_BYTE, gli.Ptr(&cv.text.target.Pix[0]))
|
||||
|
||||
gli.UseProgram(tr.id)
|
||||
gli.Uniform1i(tr.image, 0)
|
||||
|
||||
gli.BindBuffer(gl_ARRAY_BUFFER, buf)
|
||||
data := [16]float32{-1, -1, -1, 1, 1, 1, 1, -1, 0, 1, 0, 0, 1, 0, 1, 1}
|
||||
gli.BufferData(gl_ARRAY_BUFFER, len(data)*4, unsafe.Pointer(&data[0]), gl_STREAM_DRAW)
|
||||
|
||||
gli.VertexAttribPointer(tr.vertex, 2, gl_FLOAT, false, 0, nil)
|
||||
gli.VertexAttribPointer(tr.texCoord, 2, gl_FLOAT, false, 0, gli.PtrOffset(8*4))
|
||||
gli.EnableVertexAttribArray(tr.vertex)
|
||||
gli.EnableVertexAttribArray(tr.texCoord)
|
||||
gli.DrawArrays(gl_TRIANGLE_FAN, 0, 4)
|
||||
gli.DisableVertexAttribArray(tr.vertex)
|
||||
gli.DisableVertexAttribArray(tr.texCoord)
|
||||
|
||||
gli.BlendFunc(gl_SRC_ALPHA, gl_ONE_MINUS_SRC_ALPHA)
|
||||
}
|
Loading…
Reference in a new issue