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-05-09 16:41:06 +02:00
|
|
|
"sync"
|
2017-07-15 20:25:57 +02:00
|
|
|
|
|
|
|
"golang.org/x/image/font"
|
|
|
|
"golang.org/x/image/math/fixed"
|
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2020-11-03 09:12:49 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
|
2017-07-15 20:25:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
monotonicClock int64
|
|
|
|
)
|
|
|
|
|
|
|
|
func now() int64 {
|
|
|
|
return monotonicClock
|
|
|
|
}
|
|
|
|
|
2020-11-03 09:12:49 +01:00
|
|
|
func init() {
|
|
|
|
hooks.AppendHookOnBeforeUpdate(func() error {
|
|
|
|
monotonicClock++
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-17 10:00:51 +02:00
|
|
|
func fixed26_6ToFloat64(x fixed.Int26_6) float64 {
|
2020-05-13 16:31:17 +02:00
|
|
|
return float64(x>>6) + float64(x&((1<<6)-1))/float64(1<<6)
|
2017-07-17 10:00:51 +02:00
|
|
|
}
|
|
|
|
|
2020-10-03 13:00:12 +02:00
|
|
|
func drawGlyph(dst *ebiten.Image, face font.Face, r rune, img *ebiten.Image, x, y fixed.Int26_6, clr ebiten.ColorM) {
|
2018-04-23 11:43:29 +02:00
|
|
|
if img == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-03 13:53:09 +01:00
|
|
|
b := getGlyphBounds(face, r)
|
2017-07-15 20:25:57 +02:00
|
|
|
op := &ebiten.DrawImageOptions{}
|
2020-10-03 16:04:46 +02:00
|
|
|
op.GeoM.Translate(float64((x+b.Min.X)>>6), float64((y+b.Min.Y)>>6))
|
2018-04-23 15:42:38 +02:00
|
|
|
op.ColorM = clr
|
2020-10-05 17:21:11 +02:00
|
|
|
dst.DrawImage(img, op)
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
|
2018-03-03 13:53:09 +01:00
|
|
|
var (
|
2020-10-06 18:13:31 +02:00
|
|
|
glyphBoundsCache = map[font.Face]map[rune]fixed.Rectangle26_6{}
|
2018-03-03 13:53:09 +01:00
|
|
|
)
|
2017-07-18 19:50:55 +02:00
|
|
|
|
2020-10-06 18:13:31 +02:00
|
|
|
func getGlyphBounds(face font.Face, r rune) fixed.Rectangle26_6 {
|
2018-03-03 13:53:09 +01:00
|
|
|
if _, ok := glyphBoundsCache[face]; !ok {
|
2020-10-06 18:13:31 +02:00
|
|
|
glyphBoundsCache[face] = map[rune]fixed.Rectangle26_6{}
|
2017-07-18 19:57:49 +02:00
|
|
|
}
|
2018-03-03 13:53:09 +01:00
|
|
|
if b, ok := glyphBoundsCache[face][r]; ok {
|
|
|
|
return b
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
2018-03-03 13:53:09 +01:00
|
|
|
b, _, _ := face.GlyphBounds(r)
|
2020-10-06 18:13:31 +02:00
|
|
|
glyphBoundsCache[face][r] = b
|
|
|
|
return b
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
|
2018-03-03 13:53:09 +01:00
|
|
|
type glyphImageCacheEntry struct {
|
2020-10-03 13:00:12 +02:00
|
|
|
image *ebiten.Image
|
2018-03-03 13:53:09 +01:00
|
|
|
atime int64
|
|
|
|
}
|
2017-07-15 20:25:57 +02:00
|
|
|
|
2018-03-03 13:53:09 +01:00
|
|
|
var (
|
|
|
|
glyphImageCache = map[font.Face]map[rune]*glyphImageCacheEntry{}
|
|
|
|
)
|
2017-07-15 20:25:57 +02:00
|
|
|
|
2021-01-05 04:21:41 +01:00
|
|
|
func getGlyphImage(face font.Face, r rune) *ebiten.Image {
|
2018-03-03 13:53:09 +01:00
|
|
|
if _, ok := glyphImageCache[face]; !ok {
|
|
|
|
glyphImageCache[face] = map[rune]*glyphImageCacheEntry{}
|
|
|
|
}
|
2017-07-15 20:25:57 +02:00
|
|
|
|
2021-01-05 04:21:41 +01:00
|
|
|
if e, ok := glyphImageCache[face][r]; ok {
|
|
|
|
e.atime = now()
|
|
|
|
return e.image
|
|
|
|
}
|
2017-07-15 20:25:57 +02:00
|
|
|
|
2021-01-05 04:21:41 +01:00
|
|
|
b := getGlyphBounds(face, r)
|
|
|
|
w, h := (b.Max.X - b.Min.X).Ceil(), (b.Max.Y - b.Min.Y).Ceil()
|
|
|
|
if w == 0 || h == 0 {
|
2021-01-05 05:12:46 +01:00
|
|
|
glyphImageCache[face][r] = &glyphImageCacheEntry{
|
|
|
|
image: nil,
|
|
|
|
atime: now(),
|
|
|
|
}
|
2021-01-05 04:21:41 +01:00
|
|
|
return nil
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
|
2021-01-05 04:21:41 +01:00
|
|
|
if b.Min.X&((1<<6)-1) != 0 {
|
|
|
|
w++
|
|
|
|
}
|
|
|
|
if b.Min.Y&((1<<6)-1) != 0 {
|
|
|
|
h++
|
|
|
|
}
|
|
|
|
rgba := image.NewRGBA(image.Rect(0, 0, w, h))
|
2018-04-23 11:43:29 +02:00
|
|
|
|
2021-01-05 04:21:41 +01:00
|
|
|
d := font.Drawer{
|
|
|
|
Dst: rgba,
|
|
|
|
Src: image.White,
|
|
|
|
Face: face,
|
|
|
|
}
|
|
|
|
x, y := -b.Min.X, -b.Min.Y
|
|
|
|
x, y = fixed.I(x.Ceil()), fixed.I(y.Ceil())
|
|
|
|
d.Dot = fixed.Point26_6{X: x, Y: y}
|
|
|
|
d.DrawString(string(r))
|
|
|
|
|
|
|
|
img := ebiten.NewImageFromImage(rgba)
|
|
|
|
if _, ok := glyphImageCache[face][r]; !ok {
|
|
|
|
glyphImageCache[face][r] = &glyphImageCacheEntry{
|
|
|
|
image: img,
|
|
|
|
atime: now(),
|
2018-04-25 19:40:43 +02:00
|
|
|
}
|
2018-02-12 14:02:05 +01:00
|
|
|
}
|
2021-01-05 04:21:41 +01:00
|
|
|
|
|
|
|
return img
|
2018-02-12 14:02:05 +01:00
|
|
|
}
|
|
|
|
|
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.
|
2020-07-29 17:02:32 +02:00
|
|
|
// This means that if the given text consisted of a single character ".",
|
|
|
|
// it would be positioned at the given position (x, y).
|
2017-10-01 13:48:28 +02:00
|
|
|
// Be careful that this doesn't represent left-upper corner position.
|
2020-07-29 17:02:32 +02:00
|
|
|
//
|
2017-07-15 20:25:57 +02:00
|
|
|
// clr is the color for text rendering.
|
|
|
|
//
|
2020-07-29 17:02:32 +02:00
|
|
|
// If you want to adjust the position of the text, these functions are useful:
|
|
|
|
//
|
2020-07-29 17:19:08 +02:00
|
|
|
// * text.BoundString: the rendered bounds of the given text.
|
2020-07-29 17:02:32 +02:00
|
|
|
// * golang.org/x/image/font.Face.Metrics: the metrics of the face.
|
|
|
|
//
|
2020-02-12 15:15:57 +01:00
|
|
|
// The '\n' newline character puts the following text on the next line.
|
|
|
|
// Line height is based on Metrics().Height of the font.
|
|
|
|
//
|
2017-07-16 19:15:00 +02:00
|
|
|
// Glyphs used for rendering are cached in least-recently-used way.
|
2018-07-14 18:04:46 +02:00
|
|
|
// It is OK to call Draw with a same text and a same face at every frame in terms of performance.
|
2017-07-16 19:13:30 +02:00
|
|
|
//
|
2018-03-15 15:00:23 +01:00
|
|
|
// Be careful that the passed font face is held by this package and is never released.
|
|
|
|
// This is a known issue (#498).
|
|
|
|
//
|
2018-07-14 18:04:46 +02:00
|
|
|
// Draw 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()
|
2020-05-13 11:03:39 +02:00
|
|
|
defer textM.Unlock()
|
2017-07-15 20:25:57 +02:00
|
|
|
|
2021-01-27 17:44:43 +01:00
|
|
|
cr, cg, cb, ca := clr.RGBA()
|
|
|
|
if ca == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var colorm ebiten.ColorM
|
|
|
|
colorm.Scale(float64(cr)/float64(ca), float64(cg)/float64(ca), float64(cb)/float64(ca), float64(ca)/0xffff)
|
|
|
|
|
2020-02-12 15:15:57 +01:00
|
|
|
fx, fy := fixed.I(x), fixed.I(y)
|
2018-03-03 13:53:09 +01:00
|
|
|
prevR := rune(-1)
|
2017-07-15 20:25:57 +02:00
|
|
|
|
2020-05-13 11:03:39 +02:00
|
|
|
faceHeight := face.Metrics().Height
|
|
|
|
|
2021-01-05 04:21:41 +01:00
|
|
|
for _, r := range text {
|
2018-03-03 13:53:09 +01:00
|
|
|
if prevR >= 0 {
|
|
|
|
fx += face.Kern(prevR, r)
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
2020-02-12 15:15:57 +01:00
|
|
|
if r == '\n' {
|
|
|
|
fx = fixed.I(x)
|
2020-05-13 11:03:39 +02:00
|
|
|
fy += faceHeight
|
2020-02-12 15:15:57 +01:00
|
|
|
prevR = rune(-1)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-05 04:21:41 +01:00
|
|
|
img := getGlyphImage(face, r)
|
|
|
|
drawGlyph(dst, face, r, img, fx, fy, colorm)
|
2018-03-15 15:00:23 +01:00
|
|
|
fx += glyphAdvance(face, r)
|
2018-03-03 13:53:09 +01:00
|
|
|
|
|
|
|
prevR = r
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
2020-11-03 07:17:04 +01:00
|
|
|
|
2020-11-03 09:12:49 +01:00
|
|
|
// cacheSoftLimit indicates the soft limit of the number of glyphs in the cache.
|
|
|
|
// If the number of glyphs exceeds this soft limits, old glyphs are removed.
|
|
|
|
// Even after clearning up the cache, the number of glyphs might still exceeds the soft limit, but
|
|
|
|
// this is fine.
|
|
|
|
const cacheSoftLimit = 512
|
2020-11-03 08:36:10 +01:00
|
|
|
|
|
|
|
// Clean up the cache.
|
2020-11-03 09:12:49 +01:00
|
|
|
if len(glyphImageCache[face]) > cacheSoftLimit {
|
2020-11-03 07:17:04 +01:00
|
|
|
for r, e := range glyphImageCache[face] {
|
2020-11-03 09:12:49 +01:00
|
|
|
// 60 is an arbitrary number.
|
|
|
|
if e.atime < now()-60 {
|
|
|
|
delete(glyphImageCache[face], r)
|
2020-11-03 07:17:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 11:03:39 +02:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:19:08 +02:00
|
|
|
// BoundString returns the measured size of a given string using a given font.
|
2020-05-13 11:03:39 +02:00
|
|
|
// This method will return the exact size in pixels that a string drawn by Draw will be.
|
2020-07-29 17:54:57 +02:00
|
|
|
// The bound's origin point indicates the dot (period) position.
|
|
|
|
// This means that if the text consists of one character '.', this dot is rendered at (0, 0).
|
2020-07-29 17:02:32 +02:00
|
|
|
//
|
2020-07-29 17:19:08 +02:00
|
|
|
// This is very similar to golang.org/x/image/font's BoundString,
|
|
|
|
// but this BoundString calculates the actual rendered area considering multiple lines and space characters.
|
2020-05-13 11:03:39 +02:00
|
|
|
//
|
|
|
|
// face is the font for text rendering.
|
2020-07-29 17:54:57 +02:00
|
|
|
// text is the string that's being measured.
|
2020-05-13 11:03:39 +02:00
|
|
|
//
|
|
|
|
// Be careful that the passed font face is held by this package and is never released.
|
|
|
|
// This is a known issue (#498).
|
|
|
|
//
|
2020-07-29 17:19:08 +02:00
|
|
|
// BoundString is concurrent-safe.
|
|
|
|
func BoundString(face font.Face, text string) image.Rectangle {
|
2020-05-13 11:03:39 +02:00
|
|
|
textM.Lock()
|
|
|
|
defer textM.Unlock()
|
|
|
|
|
|
|
|
m := face.Metrics()
|
|
|
|
faceHeight := m.Height
|
|
|
|
|
|
|
|
fx, fy := fixed.I(0), fixed.I(0)
|
|
|
|
prevR := rune(-1)
|
|
|
|
|
2020-07-29 17:19:08 +02:00
|
|
|
var bounds fixed.Rectangle26_6
|
2021-01-05 04:21:41 +01:00
|
|
|
for _, r := range text {
|
2020-05-13 11:03:39 +02:00
|
|
|
if prevR >= 0 {
|
|
|
|
fx += face.Kern(prevR, r)
|
|
|
|
}
|
|
|
|
if r == '\n' {
|
|
|
|
fx = fixed.I(0)
|
|
|
|
fy += faceHeight
|
|
|
|
prevR = rune(-1)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:13:31 +02:00
|
|
|
b := getGlyphBounds(face, r)
|
2020-07-29 17:19:08 +02:00
|
|
|
b.Min.X += fx
|
|
|
|
b.Max.X += fx
|
|
|
|
b.Min.Y += fy
|
|
|
|
b.Max.Y += fy
|
|
|
|
bounds = bounds.Union(b)
|
2020-05-13 11:03:39 +02:00
|
|
|
|
2020-07-29 17:19:08 +02:00
|
|
|
fx += glyphAdvance(face, r)
|
2020-05-13 11:03:39 +02:00
|
|
|
prevR = r
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:19:08 +02:00
|
|
|
return image.Rect(
|
|
|
|
int(math.Floor(fixed26_6ToFloat64(bounds.Min.X))),
|
|
|
|
int(math.Floor(fixed26_6ToFloat64(bounds.Min.Y))),
|
|
|
|
int(math.Ceil(fixed26_6ToFloat64(bounds.Max.X))),
|
|
|
|
int(math.Ceil(fixed26_6ToFloat64(bounds.Max.Y))),
|
|
|
|
)
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
2020-11-03 09:12:49 +01:00
|
|
|
|
|
|
|
// CacheGlyphs precaches the glyphs for the given text and the given font face into the cache.
|
|
|
|
//
|
|
|
|
// Draw automatically creates and caches necessary glyphs, so usually you don't have to call CacheGlyphs
|
|
|
|
// explicitly. However, for example, when you call Draw for each rune of one big text, Draw tries to create the glyph
|
2020-11-03 11:54:51 +01:00
|
|
|
// cache and render it for each rune. This is very inefficient because creating a glyph image and rendering it are
|
|
|
|
// different operations and can never be merged as one draw call. CacheGlyphs creates necessary glyphs without
|
|
|
|
// rendering them so that these operations are likely merged into one draw call.
|
2020-11-03 12:35:55 +01:00
|
|
|
//
|
|
|
|
// If a rune's glyph is already cached, CacheGlyphs does nothing for the rune.
|
2020-11-03 09:12:49 +01:00
|
|
|
func CacheGlyphs(face font.Face, text string) {
|
|
|
|
textM.Lock()
|
|
|
|
defer textM.Unlock()
|
|
|
|
|
2021-01-05 04:21:41 +01:00
|
|
|
for _, r := range text {
|
|
|
|
getGlyphImage(face, r)
|
|
|
|
}
|
2020-11-03 09:12:49 +01:00
|
|
|
}
|