2023-07-11 19:54:29 -06:00
|
|
|
package typeset
|
2023-07-06 23:49:32 -06:00
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "unicode"
|
|
|
|
import "image/draw"
|
|
|
|
import "image/color"
|
|
|
|
import "golang.org/x/image/math/fixed"
|
|
|
|
|
|
|
|
// Drawer is an extended TypeSetter that is able to draw text. Much like
|
|
|
|
// TypeSetter, It has no constructor and its zero value can be used safely.
|
|
|
|
type Drawer struct { TypeSetter }
|
|
|
|
|
|
|
|
// Draw draws the drawer's text onto the specified canvas at the given offset.
|
|
|
|
func (drawer Drawer) Draw (
|
|
|
|
destination draw.Image,
|
2023-07-11 20:25:44 -06:00
|
|
|
color color.Color,
|
2023-07-06 23:49:32 -06:00
|
|
|
offset image.Point,
|
|
|
|
) (
|
|
|
|
updatedRegion image.Rectangle,
|
|
|
|
) {
|
2023-07-11 20:25:44 -06:00
|
|
|
source := image.NewUniform(color)
|
|
|
|
|
2023-07-06 23:49:32 -06:00
|
|
|
drawer.For (func (
|
|
|
|
index int,
|
|
|
|
char rune,
|
|
|
|
position fixed.Point26_6,
|
|
|
|
) bool {
|
2024-09-03 15:59:07 -06:00
|
|
|
// leave empty space for space characters
|
|
|
|
if unicode.IsSpace(char){
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
dot := fixed.P (
|
|
|
|
offset.X + position.X.Round(),
|
|
|
|
offset.Y + position.Y.Round())
|
2023-07-06 23:49:32 -06:00
|
|
|
destinationRectangle,
|
2024-09-03 15:59:07 -06:00
|
|
|
mask, maskPoint, _, ok := drawer.face.Glyph(dot, char)
|
|
|
|
// tofu
|
|
|
|
if !ok {
|
|
|
|
drawer.drawTofu(char, destination, color, dot)
|
2023-07-06 23:49:32 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME:? clip destination rectangle if we are on the cusp of
|
|
|
|
// the maximum height.
|
|
|
|
|
|
|
|
draw.DrawMask (
|
|
|
|
destination,
|
|
|
|
destinationRectangle,
|
2023-07-11 20:25:44 -06:00
|
|
|
source, image.Point { },
|
2023-07-06 23:49:32 -06:00
|
|
|
mask, maskPoint,
|
|
|
|
draw.Over)
|
|
|
|
|
|
|
|
updatedRegion = updatedRegion.Union(destinationRectangle)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2024-09-03 15:59:07 -06:00
|
|
|
|
|
|
|
func (drawer Drawer) drawTofu (
|
|
|
|
char rune,
|
|
|
|
destination draw.Image,
|
|
|
|
col color.Color,
|
|
|
|
position fixed.Point26_6,
|
|
|
|
) {
|
|
|
|
bounds, _ := tofuBounds(drawer.face)
|
|
|
|
rectBounds := image.Rect (
|
|
|
|
bounds.Min.X.Round(),
|
|
|
|
bounds.Min.Y.Round(),
|
|
|
|
bounds.Max.X.Round(),
|
|
|
|
bounds.Max.Y.Round()).Add(image.Pt(
|
|
|
|
position.X.Round(),
|
|
|
|
position.Y.Round()))
|
|
|
|
for x := rectBounds.Min.X; x < rectBounds.Max.X; x ++ {
|
|
|
|
destination.Set(x, rectBounds.Min.Y, col)
|
|
|
|
}
|
|
|
|
for y := rectBounds.Min.Y; y < rectBounds.Max.Y; y ++ {
|
|
|
|
destination.Set(rectBounds.Min.X, y, col)
|
|
|
|
destination.Set(rectBounds.Max.X - 1, y, col)
|
|
|
|
}
|
|
|
|
for x := rectBounds.Min.X; x < rectBounds.Max.X; x ++ {
|
|
|
|
destination.Set(x, rectBounds.Max.Y - 1, col)
|
|
|
|
}
|
|
|
|
}
|