2017-07-15 20:25:57 +02:00
|
|
|
// Copyright 2017 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Package text offers functions to draw texts on an Ebiten's image.
|
2017-07-16 23:39:06 +02:00
|
|
|
//
|
2017-10-07 20:14:33 +02:00
|
|
|
// For the example using a TTF font, see font package in the examples.
|
2017-07-15 20:25:57 +02:00
|
|
|
package text
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"math"
|
2018-02-11 08:05:27 +01:00
|
|
|
"reflect"
|
2017-07-15 20:25:57 +02:00
|
|
|
|
|
|
|
"golang.org/x/image/font"
|
|
|
|
"golang.org/x/image/math/fixed"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
2017-08-06 13:05:14 +02:00
|
|
|
emath "github.com/hajimehoshi/ebiten/internal/math"
|
2017-07-15 20:25:57 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
monotonicClock int64
|
|
|
|
)
|
|
|
|
|
|
|
|
func now() int64 {
|
|
|
|
monotonicClock++
|
|
|
|
return monotonicClock
|
|
|
|
}
|
|
|
|
|
2018-02-11 08:05:27 +01:00
|
|
|
type face struct {
|
|
|
|
f font.Face
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
faces = map[font.Face]face{}
|
|
|
|
)
|
|
|
|
|
|
|
|
func fontFaceToFace(f font.Face) face {
|
|
|
|
if fa, ok := faces[f]; ok {
|
|
|
|
return fa
|
|
|
|
}
|
|
|
|
// If the (DeepEqual-ly) same font exists,
|
|
|
|
// reuse this to avoid to consume a lot of cache (#498).
|
|
|
|
for key, value := range faces {
|
|
|
|
if reflect.DeepEqual(key, f) {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
2018-02-11 08:17:59 +01:00
|
|
|
fa := face{f}
|
|
|
|
faces[f] = fa
|
|
|
|
return fa
|
2018-02-11 08:05:27 +01:00
|
|
|
}
|
|
|
|
|
2017-07-17 19:16:39 +02:00
|
|
|
var (
|
2018-02-11 16:07:25 +01:00
|
|
|
charBounds = map[font.Face]map[rune]fixed.Rectangle26_6{}
|
2017-07-17 19:16:39 +02:00
|
|
|
)
|
|
|
|
|
2017-07-15 20:25:57 +02:00
|
|
|
type char struct {
|
2018-02-11 08:05:27 +01:00
|
|
|
face face
|
2017-07-15 20:25:57 +02:00
|
|
|
rune rune
|
|
|
|
}
|
|
|
|
|
2017-07-17 19:16:39 +02:00
|
|
|
func (c *char) bounds() fixed.Rectangle26_6 {
|
2018-02-11 16:07:25 +01:00
|
|
|
if m, ok := charBounds[c.face.f]; ok {
|
|
|
|
if b, ok := m[c.rune]; ok {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
charBounds[c.face.f] = map[rune]fixed.Rectangle26_6{}
|
2017-07-17 19:16:39 +02:00
|
|
|
}
|
2018-02-11 08:05:27 +01:00
|
|
|
b, _, _ := c.face.f.GlyphBounds(c.rune)
|
2018-02-11 16:07:25 +01:00
|
|
|
charBounds[c.face.f][c.rune] = b
|
2017-07-17 19:16:39 +02:00
|
|
|
return b
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
|
2017-07-17 19:16:39 +02:00
|
|
|
func (c *char) size() fixed.Point26_6 {
|
|
|
|
b := c.bounds()
|
|
|
|
return b.Max.Sub(b.Min)
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
|
2017-07-17 19:16:39 +02:00
|
|
|
func (c *char) empty() bool {
|
|
|
|
s := c.size()
|
2017-07-17 10:00:51 +02:00
|
|
|
return s.X == 0 || s.Y == 0
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
|
2017-07-17 19:16:39 +02:00
|
|
|
func (c *char) atlasGroup() int {
|
|
|
|
s := c.size()
|
2017-07-17 10:00:51 +02:00
|
|
|
w, h := s.X.Ceil(), s.Y.Ceil()
|
2017-07-15 20:25:57 +02:00
|
|
|
t := w
|
|
|
|
if t < h {
|
|
|
|
t = h
|
|
|
|
}
|
|
|
|
|
|
|
|
// Different images for small runes are inefficient.
|
|
|
|
// Let's use a same texture atlas for typical character sizes.
|
|
|
|
if t < 32 {
|
|
|
|
return 32
|
|
|
|
}
|
2017-08-06 13:05:14 +02:00
|
|
|
return emath.NextPowerOf2Int(t)
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
|
2017-07-17 19:16:39 +02:00
|
|
|
type glyph struct {
|
|
|
|
char char
|
|
|
|
index int
|
|
|
|
atime int64
|
|
|
|
}
|
|
|
|
|
2017-07-17 10:00:51 +02:00
|
|
|
func fixed26_6ToFloat64(x fixed.Int26_6) float64 {
|
|
|
|
return float64(x) / (1 << 6)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *glyph) draw(dst *ebiten.Image, x, y fixed.Int26_6, clr color.Color) {
|
2017-07-15 20:25:57 +02:00
|
|
|
cr, cg, cb, ca := clr.RGBA()
|
|
|
|
if ca == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-17 19:16:39 +02:00
|
|
|
b := g.char.bounds()
|
2017-07-15 20:25:57 +02:00
|
|
|
op := &ebiten.DrawImageOptions{}
|
2017-07-17 10:00:51 +02:00
|
|
|
op.GeoM.Translate(fixed26_6ToFloat64(x), fixed26_6ToFloat64(y))
|
2017-07-17 19:16:39 +02:00
|
|
|
op.GeoM.Translate(fixed26_6ToFloat64(b.Min.X), fixed26_6ToFloat64(b.Min.Y))
|
2017-07-15 20:25:57 +02:00
|
|
|
|
|
|
|
rf := float64(cr) / float64(ca)
|
|
|
|
gf := float64(cg) / float64(ca)
|
|
|
|
bf := float64(cb) / float64(ca)
|
|
|
|
af := float64(ca) / 0xffff
|
|
|
|
op.ColorM.Scale(rf, gf, bf, af)
|
|
|
|
|
2018-02-11 16:07:25 +01:00
|
|
|
a := atlases[g.char.face.f][g.char.atlasGroup()]
|
2017-07-17 10:00:51 +02:00
|
|
|
sx, sy := a.at(g)
|
2017-07-18 19:52:38 +02:00
|
|
|
r := image.Rect(sx, sy, sx+a.glyphSize, sy+a.glyphSize)
|
2017-07-15 20:25:57 +02:00
|
|
|
op.SourceRect = &r
|
|
|
|
|
|
|
|
dst.DrawImage(a.image, op)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2018-02-11 16:07:25 +01:00
|
|
|
atlases = map[font.Face]map[int]*atlas{}
|
2017-07-15 20:25:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type atlas struct {
|
2017-07-16 18:54:10 +02:00
|
|
|
// image is the back-end image to hold glyph cache.
|
|
|
|
image *ebiten.Image
|
|
|
|
|
|
|
|
// tmpImage is the temporary image as a renderer source for glyph.
|
2017-07-15 20:25:57 +02:00
|
|
|
tmpImage *ebiten.Image
|
2017-07-16 18:54:10 +02:00
|
|
|
|
2017-07-18 19:52:38 +02:00
|
|
|
// glyphSize is the size of one glyph in the cache.
|
2017-07-16 18:54:10 +02:00
|
|
|
// This value is always power of 2.
|
2017-07-18 19:52:38 +02:00
|
|
|
glyphSize int
|
2017-07-16 18:54:10 +02:00
|
|
|
|
2018-02-11 16:07:25 +01:00
|
|
|
runeToGlyph map[rune]*glyph
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *atlas) at(glyph *glyph) (int, int) {
|
2017-07-18 19:52:38 +02:00
|
|
|
if a.glyphSize != glyph.char.atlasGroup() {
|
2017-07-15 20:25:57 +02:00
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
w, _ := a.image.Size()
|
2017-07-18 19:52:38 +02:00
|
|
|
xnum := w / a.glyphSize
|
2017-07-15 20:25:57 +02:00
|
|
|
x, y := glyph.index%xnum, glyph.index/xnum
|
2017-07-18 19:52:38 +02:00
|
|
|
return x * a.glyphSize, y * a.glyphSize
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
|
2017-07-18 19:50:55 +02:00
|
|
|
func (a *atlas) maxGlyphNum() int {
|
|
|
|
w, h := a.image.Size()
|
2017-07-18 19:52:38 +02:00
|
|
|
xnum := w / a.glyphSize
|
|
|
|
ynum := h / a.glyphSize
|
2017-07-18 19:50:55 +02:00
|
|
|
return xnum * ynum
|
|
|
|
}
|
|
|
|
|
2018-02-11 08:05:27 +01:00
|
|
|
func (a *atlas) appendGlyph(face face, rune rune, now int64) *glyph {
|
2017-07-18 19:57:49 +02:00
|
|
|
g := &glyph{
|
|
|
|
char: char{face, rune},
|
|
|
|
atime: now,
|
|
|
|
}
|
2018-02-11 16:07:25 +01:00
|
|
|
if len(a.runeToGlyph) == a.maxGlyphNum() {
|
2017-07-18 19:50:55 +02:00
|
|
|
var oldest *glyph
|
2017-07-15 20:25:57 +02:00
|
|
|
t := int64(math.MaxInt64)
|
2018-02-11 16:07:25 +01:00
|
|
|
for _, g := range a.runeToGlyph {
|
2017-07-15 20:25:57 +02:00
|
|
|
if g.atime < t {
|
|
|
|
t = g.atime
|
2017-07-18 19:50:55 +02:00
|
|
|
oldest = g
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-18 19:50:55 +02:00
|
|
|
if oldest == nil {
|
2017-07-15 20:25:57 +02:00
|
|
|
panic("not reached")
|
|
|
|
}
|
2017-07-18 19:50:55 +02:00
|
|
|
idx := oldest.index
|
2018-02-11 16:07:25 +01:00
|
|
|
delete(a.runeToGlyph, oldest.char.rune)
|
2017-07-15 20:25:57 +02:00
|
|
|
|
2017-07-18 19:50:55 +02:00
|
|
|
g.index = idx
|
|
|
|
} else {
|
2018-02-11 16:07:25 +01:00
|
|
|
g.index = len(a.runeToGlyph)
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
2018-02-11 16:07:25 +01:00
|
|
|
a.runeToGlyph[g.char.rune] = g
|
2017-07-18 19:50:55 +02:00
|
|
|
a.draw(g)
|
2017-07-18 19:57:49 +02:00
|
|
|
return g
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *atlas) draw(glyph *glyph) {
|
|
|
|
if a.tmpImage == nil {
|
2017-07-18 19:52:38 +02:00
|
|
|
a.tmpImage, _ = ebiten.NewImage(a.glyphSize, a.glyphSize, ebiten.FilterNearest)
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
|
2017-07-18 19:52:38 +02:00
|
|
|
dst := image.NewRGBA(image.Rect(0, 0, a.glyphSize, a.glyphSize))
|
2017-07-15 20:25:57 +02:00
|
|
|
d := font.Drawer{
|
|
|
|
Dst: dst,
|
|
|
|
Src: image.White,
|
2018-02-11 08:05:27 +01:00
|
|
|
Face: glyph.char.face.f,
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
2017-07-17 19:16:39 +02:00
|
|
|
b := glyph.char.bounds()
|
|
|
|
d.Dot = fixed.Point26_6{-b.Min.X, -b.Min.Y}
|
2017-07-15 20:25:57 +02:00
|
|
|
d.DrawString(string(glyph.char.rune))
|
|
|
|
a.tmpImage.ReplacePixels(dst.Pix)
|
|
|
|
|
|
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
x, y := a.at(glyph)
|
|
|
|
op.GeoM.Translate(float64(x), float64(y))
|
|
|
|
op.CompositeMode = ebiten.CompositeModeCopy
|
|
|
|
a.image.DrawImage(a.tmpImage, op)
|
|
|
|
|
|
|
|
a.tmpImage.Clear()
|
|
|
|
}
|
|
|
|
|
2018-02-11 08:17:59 +01:00
|
|
|
func getGlyphFromCache(face face, r rune, now int64) *glyph {
|
|
|
|
ch := char{face, r}
|
2018-02-11 16:07:25 +01:00
|
|
|
var at *atlas
|
|
|
|
if m, ok := atlases[face.f]; ok {
|
|
|
|
a, ok := m[ch.atlasGroup()]
|
2017-07-18 03:49:12 +02:00
|
|
|
if ok {
|
2018-02-11 16:07:25 +01:00
|
|
|
g, ok := a.runeToGlyph[r]
|
|
|
|
if ok {
|
|
|
|
g.atime = now
|
|
|
|
return g
|
|
|
|
}
|
2017-07-18 03:49:12 +02:00
|
|
|
}
|
2018-02-11 16:07:25 +01:00
|
|
|
at = a
|
|
|
|
} else {
|
|
|
|
atlases[face.f] = map[int]*atlas{}
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
|
2017-07-17 19:16:39 +02:00
|
|
|
if ch.empty() {
|
2017-07-20 17:44:37 +02:00
|
|
|
// The glyph doesn't have its size but might have valid 'advance' parameter
|
|
|
|
// when ch is e.g. space (U+0020).
|
|
|
|
return &glyph{
|
|
|
|
char: ch,
|
|
|
|
atime: now,
|
|
|
|
}
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
|
2018-02-11 16:07:25 +01:00
|
|
|
if at == nil {
|
2017-07-15 20:25:57 +02:00
|
|
|
// Don't use ebiten.MaxImageSize here.
|
|
|
|
// It's because the back-end image pixels will be restored from GPU
|
|
|
|
// whenever a new glyph is rendered on the image, and restoring cost is
|
|
|
|
// expensive if the image is big.
|
|
|
|
// The back-end image is updated a temporary image, and the temporary image is
|
|
|
|
// always cleared after used. This means that there is no clue to restore
|
|
|
|
// the back-end image without reading from GPU
|
|
|
|
// (see the package 'restorable' implementation).
|
|
|
|
//
|
|
|
|
// TODO: How about making a new function for 'flagile' image?
|
|
|
|
const size = 1024
|
|
|
|
i, _ := ebiten.NewImage(size, size, ebiten.FilterNearest)
|
2018-02-11 16:07:25 +01:00
|
|
|
at = &atlas{
|
2017-07-18 03:49:12 +02:00
|
|
|
image: i,
|
2017-07-18 19:57:49 +02:00
|
|
|
glyphSize: ch.atlasGroup(),
|
2018-02-11 16:07:25 +01:00
|
|
|
runeToGlyph: map[rune]*glyph{},
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
2018-02-11 16:07:25 +01:00
|
|
|
atlases[face.f][ch.atlasGroup()] = at
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
|
2018-02-11 16:07:25 +01:00
|
|
|
return at.appendGlyph(ch.face, ch.rune, now)
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var textM sync.Mutex
|
|
|
|
|
2017-07-17 09:17:01 +02:00
|
|
|
// Draw draws a given text on a given destination image dst.
|
2017-07-15 20:25:57 +02:00
|
|
|
//
|
|
|
|
// face is the font for text rendering.
|
2017-10-01 13:48:28 +02:00
|
|
|
// (x, y) represents a 'dot' (period) position.
|
|
|
|
// Be careful that this doesn't represent left-upper corner position.
|
2017-07-15 20:25:57 +02:00
|
|
|
// clr is the color for text rendering.
|
|
|
|
//
|
2017-07-16 19:15:00 +02:00
|
|
|
// Glyphs used for rendering are cached in least-recently-used way.
|
2017-10-01 13:48:28 +02:00
|
|
|
// It is OK to call this function with a same text and a same face at every frame in terms of performance.
|
2017-07-16 19:13:30 +02:00
|
|
|
//
|
2017-07-15 20:25:57 +02:00
|
|
|
// This function is concurrent-safe.
|
2017-07-19 19:20:15 +02:00
|
|
|
func Draw(dst *ebiten.Image, text string, face font.Face, x, y int, clr color.Color) {
|
2017-07-15 20:25:57 +02:00
|
|
|
textM.Lock()
|
|
|
|
|
|
|
|
n := now()
|
|
|
|
fx := fixed.I(x)
|
|
|
|
prevC := rune(-1)
|
|
|
|
|
|
|
|
runes := []rune(text)
|
|
|
|
for _, c := range runes {
|
|
|
|
if prevC >= 0 {
|
|
|
|
fx += face.Kern(prevC, c)
|
|
|
|
}
|
2018-02-11 08:17:59 +01:00
|
|
|
fa := fontFaceToFace(face)
|
|
|
|
if g := getGlyphFromCache(fa, c, n); g != nil {
|
2017-07-17 19:16:39 +02:00
|
|
|
if !g.char.empty() {
|
2017-07-17 10:00:51 +02:00
|
|
|
g.draw(dst, fx, fixed.I(y), clr)
|
2017-07-16 18:54:10 +02:00
|
|
|
}
|
|
|
|
a, _ := face.GlyphAdvance(c)
|
|
|
|
fx += a
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
prevC = c
|
|
|
|
}
|
|
|
|
|
|
|
|
textM.Unlock()
|
|
|
|
}
|