blooblib/draw_text.go

98 lines
3.4 KiB
Go

package bloob
import "math"
func (image *Image) DrawText(str string, font *Font, pos Vec2i, color Color) {
origX := pos.X
for _, ch := range str {
c := font.chars[ch]
if ch == 10 {
pos.X = origX
pos.Y += font.lineHeight
continue
}
image.DrawColor(c.image, pos, color)
pos.X += c.xForward
}
}
func (image *Image) DrawTextCenter(str string, font *Font, rect Recti, color Color) {
size := font.StringBounds(str)
pos := Add(DivScalar(Sub(rect.Size, size), 2), Add(rect.Pos, Vec2i{X: 0, Y: 1}))
image.DrawText(str, font, pos, color)
}
func (image *Image) DrawTextSin(str string, font *Font, pos Vec2i, color Color, freq, amplitude, phase float64) {
origX := pos.X
for _, ch := range str {
c := font.chars[ch]
if ch == 10 {
pos.X = origX
pos.Y += font.lineHeight
continue
}
image.DrawColor(c.image, Vec2i{X: pos.X, Y: int(math.Sin(float64(pos.X)*math.Pi/4/freq+phase*math.Pi*2)*amplitude) + pos.Y}, color)
pos.X += c.xForward
}
}
func (image *Image) DrawTextOutline(str string, font *Font, pos Vec2i, color, outlineColor Color) {
origX := pos.X
for _, ch := range str {
c := font.chars[ch]
if ch == 10 {
pos.X = origX
pos.Y += font.lineHeight
continue
}
x := pos.X
y := pos.Y
image.DrawColor(c.image, Vec2i{X: x - 1, Y: y}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x + 1, Y: y}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x, Y: y - 1}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x, Y: y + 1}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x - 1, Y: y - 1}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x + 1, Y: y + 1}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x + 1, Y: y - 1}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x - 1, Y: y + 1}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x, Y: y + 2}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x - 1, Y: y + 2}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x + 1, Y: y + 2}, outlineColor)
image.DrawColor(c.image, pos, color)
pos.X += c.xForward
}
}
func (image *Image) DrawTextOutlineCenter(str string, font *Font, rect Recti, color, outlineColor Color) {
size := font.StringBounds(str)
pos := Add(DivScalar(Sub(rect.Size, size), 2), Add(rect.Pos, Vec2i{X: 0, Y: 1}))
image.DrawTextOutline(str, font, pos, color, outlineColor)
}
func (image *Image) DrawTextOutlineSin(str string, font *Font, pos Vec2i, color, outlineColor Color, freq, amplitude, phase float64) {
origX := pos.X
for _, ch := range str {
c := font.chars[ch]
if ch == 10 {
pos.X = origX
pos.Y += font.lineHeight
continue
}
x := pos.X
y := int(math.Sin(float64(pos.X)*math.Pi/4/freq+phase*math.Pi*2)*amplitude) + pos.Y
image.DrawColor(c.image, Vec2i{X: x - 1, Y: y}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x + 1, Y: y}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x, Y: y - 1}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x, Y: y + 1}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x - 1, Y: y - 1}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x + 1, Y: y + 1}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x + 1, Y: y - 1}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x - 1, Y: y + 1}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x, Y: y + 2}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x - 1, Y: y + 2}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x + 1, Y: y + 2}, outlineColor)
image.DrawColor(c.image, Vec2i{X: x, Y: y}, color)
pos.X += c.xForward
}
}