2017-05-03 16:59:37 -05:00
|
|
|
package text
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"image/draw"
|
2017-05-09 07:20:34 -05:00
|
|
|
"sort"
|
2017-05-03 16:59:37 -05:00
|
|
|
"unicode"
|
|
|
|
|
|
|
|
"github.com/faiface/pixel"
|
|
|
|
"golang.org/x/image/font"
|
|
|
|
"golang.org/x/image/math/fixed"
|
|
|
|
)
|
|
|
|
|
2018-01-19 15:20:54 -06:00
|
|
|
// Atlas7x13 is an Atlas using basicfont.Face7x13 with the ASCII rune set
|
|
|
|
var Atlas7x13 *Atlas
|
|
|
|
|
2017-05-09 08:25:08 -05:00
|
|
|
// Glyph describes one glyph in an Atlas.
|
2017-05-03 16:59:37 -05:00
|
|
|
type Glyph struct {
|
2017-05-07 14:49:26 -05:00
|
|
|
Dot pixel.Vec
|
2017-05-03 16:59:37 -05:00
|
|
|
Frame pixel.Rect
|
|
|
|
Advance float64
|
|
|
|
}
|
|
|
|
|
2017-05-09 08:25:08 -05:00
|
|
|
// Atlas is a set of pre-drawn glyphs of a fixed set of runes. This allows for efficient text drawing.
|
2017-05-03 16:59:37 -05:00
|
|
|
type Atlas struct {
|
2017-05-09 08:10:35 -05:00
|
|
|
face font.Face
|
2017-05-03 16:59:37 -05:00
|
|
|
pic pixel.Picture
|
|
|
|
mapping map[rune]Glyph
|
2017-05-05 09:02:47 -05:00
|
|
|
ascent float64
|
|
|
|
descent float64
|
2017-05-03 16:59:37 -05:00
|
|
|
lineHeight float64
|
|
|
|
}
|
|
|
|
|
2017-05-10 10:56:09 -05:00
|
|
|
// NewAtlas creates a new Atlas containing glyphs of the union of the given sets of runes (plus
|
|
|
|
// unicode.ReplacementChar) from the given font face.
|
2017-05-09 09:39:03 -05:00
|
|
|
//
|
2017-05-10 10:56:09 -05:00
|
|
|
// Creating an Atlas is rather expensive, do not create a new Atlas each frame.
|
|
|
|
//
|
|
|
|
// Do not destroy or close the font.Face after creating the Atlas. Atlas still uses it.
|
|
|
|
func NewAtlas(face font.Face, runeSets ...[]rune) *Atlas {
|
|
|
|
seen := make(map[rune]bool)
|
|
|
|
runes := []rune{unicode.ReplacementChar}
|
|
|
|
for _, set := range runeSets {
|
|
|
|
for _, r := range set {
|
|
|
|
if !seen[r] {
|
|
|
|
runes = append(runes, r)
|
|
|
|
seen[r] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 07:20:34 -05:00
|
|
|
fixedMapping, fixedBounds := makeSquareMapping(face, runes, fixed.I(2))
|
2017-05-03 16:59:37 -05:00
|
|
|
|
|
|
|
atlasImg := image.NewRGBA(image.Rect(
|
2017-05-09 07:20:34 -05:00
|
|
|
fixedBounds.Min.X.Floor(),
|
|
|
|
fixedBounds.Min.Y.Floor(),
|
|
|
|
fixedBounds.Max.X.Ceil(),
|
|
|
|
fixedBounds.Max.Y.Ceil(),
|
2017-05-03 16:59:37 -05:00
|
|
|
))
|
|
|
|
|
2017-05-09 07:20:34 -05:00
|
|
|
for r, fg := range fixedMapping {
|
2020-08-13 04:57:30 -05:00
|
|
|
if dr, mask, maskp, _, ok := face.Glyph(fg.dot, r); ok {
|
|
|
|
draw.Draw(atlasImg, dr, mask, maskp, draw.Src)
|
|
|
|
}
|
2017-05-09 07:20:34 -05:00
|
|
|
}
|
2017-05-03 16:59:37 -05:00
|
|
|
|
2017-05-09 08:10:35 -05:00
|
|
|
bounds := pixel.R(
|
|
|
|
i2f(fixedBounds.Min.X),
|
|
|
|
i2f(fixedBounds.Min.Y),
|
|
|
|
i2f(fixedBounds.Max.X),
|
|
|
|
i2f(fixedBounds.Max.Y),
|
|
|
|
)
|
2017-05-03 16:59:37 -05:00
|
|
|
|
2017-05-09 07:20:34 -05:00
|
|
|
mapping := make(map[rune]Glyph)
|
|
|
|
for r, fg := range fixedMapping {
|
|
|
|
mapping[r] = Glyph{
|
|
|
|
Dot: pixel.V(
|
|
|
|
i2f(fg.dot.X),
|
2017-05-21 12:25:06 -05:00
|
|
|
bounds.Max.Y-(i2f(fg.dot.Y)-bounds.Min.Y),
|
2017-05-09 07:20:34 -05:00
|
|
|
),
|
|
|
|
Frame: pixel.R(
|
|
|
|
i2f(fg.frame.Min.X),
|
2017-05-21 12:25:06 -05:00
|
|
|
bounds.Max.Y-(i2f(fg.frame.Min.Y)-bounds.Min.Y),
|
2017-05-09 07:20:34 -05:00
|
|
|
i2f(fg.frame.Max.X),
|
2017-05-21 12:25:06 -05:00
|
|
|
bounds.Max.Y-(i2f(fg.frame.Max.Y)-bounds.Min.Y),
|
2017-05-09 07:20:34 -05:00
|
|
|
).Norm(),
|
|
|
|
Advance: i2f(fg.advance),
|
|
|
|
}
|
2017-05-03 16:59:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Atlas{
|
2017-05-09 08:10:35 -05:00
|
|
|
face: face,
|
2017-05-05 09:02:47 -05:00
|
|
|
pic: pixel.PictureDataFromImage(atlasImg),
|
|
|
|
mapping: mapping,
|
2017-05-09 07:20:34 -05:00
|
|
|
ascent: i2f(face.Metrics().Ascent),
|
|
|
|
descent: i2f(face.Metrics().Descent),
|
|
|
|
lineHeight: i2f(face.Metrics().Height),
|
2017-05-03 16:59:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 08:25:08 -05:00
|
|
|
// Picture returns the underlying Picture containing an arrangement of all the glyphs contained
|
|
|
|
// within the Atlas.
|
2017-05-03 16:59:37 -05:00
|
|
|
func (a *Atlas) Picture() pixel.Picture {
|
|
|
|
return a.pic
|
|
|
|
}
|
|
|
|
|
2017-05-09 08:25:08 -05:00
|
|
|
// Contains reports wheter r in contained within the Atlas.
|
2017-05-03 16:59:37 -05:00
|
|
|
func (a *Atlas) Contains(r rune) bool {
|
|
|
|
_, ok := a.mapping[r]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2017-05-09 08:25:08 -05:00
|
|
|
// Glyph returns the description of r within the Atlas.
|
2017-05-03 16:59:37 -05:00
|
|
|
func (a *Atlas) Glyph(r rune) Glyph {
|
|
|
|
return a.mapping[r]
|
|
|
|
}
|
|
|
|
|
2017-05-09 09:32:29 -05:00
|
|
|
// Kern returns the kerning distance between runes r0 and r1. Positive distance means that the
|
2017-05-09 08:25:08 -05:00
|
|
|
// glyphs should be further apart.
|
2017-05-03 16:59:37 -05:00
|
|
|
func (a *Atlas) Kern(r0, r1 rune) float64 {
|
2017-05-09 08:10:35 -05:00
|
|
|
return i2f(a.face.Kern(r0, r1))
|
2017-05-03 16:59:37 -05:00
|
|
|
}
|
|
|
|
|
2017-05-09 08:25:08 -05:00
|
|
|
// Ascent returns the distance from the top of the line to the baseline.
|
2017-05-05 09:02:47 -05:00
|
|
|
func (a *Atlas) Ascent() float64 {
|
|
|
|
return a.ascent
|
|
|
|
}
|
|
|
|
|
2017-05-09 08:25:08 -05:00
|
|
|
// Descent returns the distance from the baseline to the bottom of the line.
|
2017-05-05 09:02:47 -05:00
|
|
|
func (a *Atlas) Descent() float64 {
|
|
|
|
return a.descent
|
|
|
|
}
|
|
|
|
|
2017-05-09 08:25:08 -05:00
|
|
|
// LineHeight returns the recommended vertical distance between two lines of text.
|
2017-05-03 16:59:37 -05:00
|
|
|
func (a *Atlas) LineHeight() float64 {
|
|
|
|
return a.lineHeight
|
|
|
|
}
|
2017-05-07 13:59:41 -05:00
|
|
|
|
2017-05-09 08:25:08 -05:00
|
|
|
// DrawRune returns parameters necessary for drawing a rune glyph.
|
|
|
|
//
|
|
|
|
// Rect is a rectangle where the glyph should be positioned. Frame is the glyph frame inside the
|
|
|
|
// Atlas's Picture. NewDot is the new position of the dot.
|
2017-05-07 13:59:41 -05:00
|
|
|
func (a *Atlas) DrawRune(prevR, r rune, dot pixel.Vec) (rect, frame, bounds pixel.Rect, newDot pixel.Vec) {
|
|
|
|
if !a.Contains(r) {
|
|
|
|
r = unicode.ReplacementChar
|
|
|
|
}
|
|
|
|
if !a.Contains(unicode.ReplacementChar) {
|
|
|
|
return pixel.Rect{}, pixel.Rect{}, pixel.Rect{}, dot
|
|
|
|
}
|
|
|
|
if !a.Contains(prevR) {
|
|
|
|
prevR = unicode.ReplacementChar
|
|
|
|
}
|
|
|
|
|
|
|
|
if prevR >= 0 {
|
2017-05-21 12:25:06 -05:00
|
|
|
dot.X += a.Kern(prevR, r)
|
2017-05-07 13:59:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
glyph := a.Glyph(r)
|
|
|
|
|
2017-05-21 12:25:06 -05:00
|
|
|
rect = glyph.Frame.Moved(dot.Sub(glyph.Dot))
|
2017-05-07 13:59:41 -05:00
|
|
|
bounds = rect
|
|
|
|
|
|
|
|
if bounds.W()*bounds.H() != 0 {
|
|
|
|
bounds = pixel.R(
|
2017-05-21 12:25:06 -05:00
|
|
|
bounds.Min.X,
|
|
|
|
dot.Y-a.Descent(),
|
|
|
|
bounds.Max.X,
|
|
|
|
dot.Y+a.Ascent(),
|
2017-05-07 13:59:41 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-05-21 12:25:06 -05:00
|
|
|
dot.X += glyph.Advance
|
2017-05-07 13:59:41 -05:00
|
|
|
|
|
|
|
return rect, glyph.Frame, bounds, dot
|
|
|
|
}
|
2017-05-09 07:20:34 -05:00
|
|
|
|
|
|
|
type fixedGlyph struct {
|
|
|
|
dot fixed.Point26_6
|
|
|
|
frame fixed.Rectangle26_6
|
|
|
|
advance fixed.Int26_6
|
|
|
|
}
|
|
|
|
|
2017-05-09 08:25:08 -05:00
|
|
|
// makeSquareMapping finds an optimal glyph arrangement of the given runes, so that their common
|
|
|
|
// bounding box is as square as possible.
|
2017-05-09 07:20:34 -05:00
|
|
|
func makeSquareMapping(face font.Face, runes []rune, padding fixed.Int26_6) (map[rune]fixedGlyph, fixed.Rectangle26_6) {
|
|
|
|
width := sort.Search(int(fixed.I(1024*1024)), func(i int) bool {
|
|
|
|
width := fixed.Int26_6(i)
|
|
|
|
_, bounds := makeMapping(face, runes, padding, width)
|
|
|
|
return bounds.Max.X-bounds.Min.X >= bounds.Max.Y-bounds.Min.Y
|
|
|
|
})
|
|
|
|
return makeMapping(face, runes, padding, fixed.Int26_6(width))
|
|
|
|
}
|
|
|
|
|
2017-05-09 08:25:08 -05:00
|
|
|
// makeMapping arranges glyphs of the given runes into rows in such a way, that no glyph is located
|
|
|
|
// fully to the right of the specified width. Specifically, it places glyphs in a row one by one and
|
|
|
|
// once it reaches the specified width, it starts a new row.
|
2017-05-09 07:20:34 -05:00
|
|
|
func makeMapping(face font.Face, runes []rune, padding, width fixed.Int26_6) (map[rune]fixedGlyph, fixed.Rectangle26_6) {
|
|
|
|
mapping := make(map[rune]fixedGlyph)
|
|
|
|
bounds := fixed.Rectangle26_6{}
|
|
|
|
|
|
|
|
dot := fixed.P(0, 0)
|
|
|
|
|
|
|
|
for _, r := range runes {
|
2017-05-11 12:48:43 -05:00
|
|
|
b, advance, ok := face.GlyphBounds(r)
|
2017-05-09 07:20:34 -05:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-05-11 12:48:43 -05:00
|
|
|
// this is important for drawing, artifacts arise otherwise
|
2017-05-09 07:20:34 -05:00
|
|
|
frame := fixed.Rectangle26_6{
|
2017-05-11 12:48:43 -05:00
|
|
|
Min: fixed.P(b.Min.X.Floor(), b.Min.Y.Floor()),
|
|
|
|
Max: fixed.P(b.Max.X.Ceil(), b.Max.Y.Ceil()),
|
2017-05-09 07:20:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
dot.X -= frame.Min.X
|
|
|
|
frame = frame.Add(dot)
|
|
|
|
|
|
|
|
mapping[r] = fixedGlyph{
|
|
|
|
dot: dot,
|
|
|
|
frame: frame,
|
|
|
|
advance: advance,
|
|
|
|
}
|
|
|
|
bounds = bounds.Union(frame)
|
|
|
|
|
|
|
|
dot.X = frame.Max.X
|
|
|
|
|
|
|
|
// padding + align to integer
|
|
|
|
dot.X += padding
|
|
|
|
dot.X = fixed.I(dot.X.Ceil())
|
|
|
|
|
|
|
|
// width exceeded, new row
|
|
|
|
if frame.Max.X >= width {
|
|
|
|
dot.X = 0
|
|
|
|
dot.Y += face.Metrics().Ascent + face.Metrics().Descent
|
|
|
|
|
|
|
|
// padding + align to integer
|
|
|
|
dot.Y += padding
|
|
|
|
dot.Y = fixed.I(dot.Y.Ceil())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mapping, bounds
|
|
|
|
}
|
|
|
|
|
|
|
|
func i2f(i fixed.Int26_6) float64 {
|
|
|
|
return float64(i) / (1 << 6)
|
|
|
|
}
|