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"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
2020-06-29 15:18:40 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/colormcache"
|
2017-07-15 20:25:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
monotonicClock int64
|
|
|
|
)
|
|
|
|
|
|
|
|
func now() int64 {
|
|
|
|
monotonicClock++
|
|
|
|
return monotonicClock
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-03 13:53:09 +01:00
|
|
|
const (
|
|
|
|
cacheLimit = 512 // This is an arbitrary number.
|
|
|
|
)
|
|
|
|
|
2018-04-23 15:42:38 +02:00
|
|
|
func drawGlyph(dst *ebiten.Image, face font.Face, r rune, img *glyphImage, 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{}
|
2018-02-11 17:08:53 +01:00
|
|
|
op.GeoM.Translate(fixed26_6ToFloat64(x+b.Min.X), fixed26_6ToFloat64(y+b.Min.Y))
|
2018-04-23 15:42:38 +02:00
|
|
|
op.ColorM = clr
|
2018-10-23 16:27:27 +02:00
|
|
|
_ = dst.DrawImage(img.image.SubImage(image.Rect(img.x, img.y, img.x+img.width, img.y+img.height)).(*ebiten.Image), op)
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
|
2018-03-03 13:53:09 +01:00
|
|
|
var (
|
|
|
|
// Use pointers to avoid copying on browsers.
|
|
|
|
glyphBoundsCache = map[font.Face]map[rune]*fixed.Rectangle26_6{}
|
|
|
|
)
|
2017-07-18 19:50:55 +02:00
|
|
|
|
2018-03-03 13:53:09 +01:00
|
|
|
func getGlyphBounds(face font.Face, r rune) *fixed.Rectangle26_6 {
|
|
|
|
if _, ok := glyphBoundsCache[face]; !ok {
|
|
|
|
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)
|
|
|
|
glyphBoundsCache[face][r] = &b
|
|
|
|
return &b
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 11:43:29 +02:00
|
|
|
type glyphImage struct {
|
|
|
|
image *ebiten.Image
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
}
|
|
|
|
|
2018-03-03 13:53:09 +01:00
|
|
|
type glyphImageCacheEntry struct {
|
2018-04-23 11:43:29 +02:00
|
|
|
image *glyphImage
|
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{}
|
|
|
|
emptyGlyphs = map[font.Face]map[rune]struct{}{}
|
|
|
|
)
|
2017-07-15 20:25:57 +02:00
|
|
|
|
2018-04-23 11:43:29 +02:00
|
|
|
func getGlyphImages(face font.Face, runes []rune) []*glyphImage {
|
2018-03-03 13:53:09 +01:00
|
|
|
if _, ok := emptyGlyphs[face]; !ok {
|
|
|
|
emptyGlyphs[face] = map[rune]struct{}{}
|
|
|
|
}
|
|
|
|
if _, ok := glyphImageCache[face]; !ok {
|
|
|
|
glyphImageCache[face] = map[rune]*glyphImageCacheEntry{}
|
|
|
|
}
|
2017-07-15 20:25:57 +02:00
|
|
|
|
2018-04-23 11:43:29 +02:00
|
|
|
imgs := make([]*glyphImage, len(runes))
|
2018-04-28 15:50:00 +02:00
|
|
|
glyphBounds := map[rune]*fixed.Rectangle26_6{}
|
|
|
|
neededGlyphIndices := map[int]rune{}
|
2018-04-23 11:43:29 +02:00
|
|
|
for i, r := range runes {
|
|
|
|
if _, ok := emptyGlyphs[face][r]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2017-07-15 20:25:57 +02:00
|
|
|
|
2018-04-23 11:43:29 +02:00
|
|
|
if e, ok := glyphImageCache[face][r]; ok {
|
|
|
|
e.atime = now()
|
|
|
|
imgs[i] = e.image
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
emptyGlyphs[face][r] = struct{}{}
|
|
|
|
continue
|
|
|
|
}
|
2017-07-15 20:25:57 +02:00
|
|
|
|
2018-04-23 11:43:29 +02:00
|
|
|
// TODO: What if len(runes) > cacheLimit?
|
|
|
|
if len(glyphImageCache[face]) > cacheLimit {
|
|
|
|
oldest := int64(math.MaxInt64)
|
|
|
|
oldestKey := rune(-1)
|
|
|
|
for r, e := range glyphImageCache[face] {
|
|
|
|
if e.atime < oldest {
|
|
|
|
oldestKey = r
|
|
|
|
oldest = e.atime
|
|
|
|
}
|
2018-03-03 13:53:09 +01:00
|
|
|
}
|
2018-04-23 11:43:29 +02:00
|
|
|
delete(glyphImageCache[face], oldestKey)
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
2018-04-23 11:43:29 +02:00
|
|
|
|
2018-04-28 15:50:00 +02:00
|
|
|
glyphBounds[r] = b
|
|
|
|
neededGlyphIndices[i] = r
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|
|
|
|
|
2018-04-28 15:50:00 +02:00
|
|
|
if len(neededGlyphIndices) > 0 {
|
2018-04-25 19:11:45 +02:00
|
|
|
// TODO: What if w2 is too big (e.g. > 4096)?
|
|
|
|
w2 := 0
|
|
|
|
h2 := 0
|
2018-04-28 15:50:00 +02:00
|
|
|
for _, b := range glyphBounds {
|
2018-04-25 19:11:45 +02:00
|
|
|
w, h := (b.Max.X - b.Min.X).Ceil(), (b.Max.Y - b.Min.Y).Ceil()
|
|
|
|
w2 += w
|
|
|
|
if h2 < h {
|
|
|
|
h2 = h
|
|
|
|
}
|
2018-04-23 11:43:29 +02:00
|
|
|
}
|
2018-04-25 19:11:45 +02:00
|
|
|
rgba := image.NewRGBA(image.Rect(0, 0, w2, h2))
|
2018-04-23 11:43:29 +02:00
|
|
|
|
2018-04-25 19:11:45 +02:00
|
|
|
x := 0
|
2018-04-28 15:50:00 +02:00
|
|
|
xs := map[rune]int{}
|
|
|
|
for r, b := range glyphBounds {
|
|
|
|
w := (b.Max.X - b.Min.X).Ceil()
|
2018-04-23 11:43:29 +02:00
|
|
|
|
2018-04-25 19:11:45 +02:00
|
|
|
d := font.Drawer{
|
|
|
|
Dst: rgba,
|
|
|
|
Src: image.White,
|
|
|
|
Face: face,
|
|
|
|
}
|
2019-07-22 13:02:37 +02:00
|
|
|
d.Dot = fixed.Point26_6{X: fixed.I(x) - b.Min.X, Y: -b.Min.Y}
|
2018-04-25 19:11:45 +02:00
|
|
|
d.DrawString(string(r))
|
2018-04-28 15:50:00 +02:00
|
|
|
xs[r] = x
|
|
|
|
|
|
|
|
x += w
|
|
|
|
}
|
|
|
|
|
|
|
|
img, _ := ebiten.NewImageFromImage(rgba, ebiten.FilterDefault)
|
|
|
|
for i, r := range neededGlyphIndices {
|
|
|
|
b := glyphBounds[r]
|
|
|
|
w, h := (b.Max.X - b.Min.X).Ceil(), (b.Max.Y - b.Min.Y).Ceil()
|
2018-04-25 19:11:45 +02:00
|
|
|
|
|
|
|
g := &glyphImage{
|
2018-04-28 15:50:00 +02:00
|
|
|
image: img,
|
|
|
|
x: xs[r],
|
2018-04-25 19:11:45 +02:00
|
|
|
y: 0,
|
|
|
|
width: w,
|
|
|
|
height: h,
|
|
|
|
}
|
2018-04-28 15:50:00 +02:00
|
|
|
if _, ok := glyphImageCache[face][r]; !ok {
|
|
|
|
glyphImageCache[face][r] = &glyphImageCacheEntry{
|
|
|
|
image: g,
|
|
|
|
atime: now(),
|
|
|
|
}
|
2018-04-25 19:11:45 +02:00
|
|
|
}
|
|
|
|
imgs[i] = g
|
2018-04-25 19:40:43 +02:00
|
|
|
}
|
2018-02-12 14:02:05 +01:00
|
|
|
}
|
2018-04-23 11:43:29 +02:00
|
|
|
return imgs
|
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.
|
|
|
|
// 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.
|
|
|
|
//
|
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
|
|
|
|
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
|
|
|
|
|
2017-07-15 20:25:57 +02:00
|
|
|
runes := []rune(text)
|
2018-04-23 11:43:29 +02:00
|
|
|
glyphImgs := getGlyphImages(face, runes)
|
2020-06-29 15:18:40 +02:00
|
|
|
colorm := colormcache.ColorToColorM(clr)
|
2018-04-23 15:42:38 +02:00
|
|
|
|
2018-04-23 11:43:29 +02:00
|
|
|
for i, r := range runes {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
drawGlyph(dst, face, r, glyphImgs[i], 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-05-13 11:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MeasureString returns the measured size of a given string using a given font.
|
|
|
|
// This method will return the exact size in pixels that a string drawn by Draw will be.
|
|
|
|
//
|
|
|
|
// text is the string that's being measured.
|
|
|
|
// face is the font for text rendering.
|
|
|
|
//
|
|
|
|
// Be careful that the passed font face is held by this package and is never released.
|
|
|
|
// This is a known issue (#498).
|
|
|
|
//
|
|
|
|
// MeasureString is concurrent-safe.
|
|
|
|
func MeasureString(text string, face font.Face) image.Point {
|
|
|
|
textM.Lock()
|
|
|
|
defer textM.Unlock()
|
|
|
|
|
|
|
|
var w, h fixed.Int26_6
|
|
|
|
|
|
|
|
m := face.Metrics()
|
|
|
|
faceHeight := m.Height
|
|
|
|
faceDescent := m.Descent
|
|
|
|
|
|
|
|
fx, fy := fixed.I(0), fixed.I(0)
|
|
|
|
prevR := rune(-1)
|
|
|
|
|
|
|
|
runes := []rune(text)
|
|
|
|
|
|
|
|
for _, r := range runes {
|
|
|
|
if prevR >= 0 {
|
|
|
|
fx += face.Kern(prevR, r)
|
|
|
|
}
|
|
|
|
if r == '\n' {
|
|
|
|
fx = fixed.I(0)
|
|
|
|
fy += faceHeight
|
|
|
|
prevR = rune(-1)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fx += glyphAdvance(face, r)
|
|
|
|
|
|
|
|
if fx > w {
|
|
|
|
w = fx
|
|
|
|
}
|
2020-05-13 16:31:17 +02:00
|
|
|
if (fy + faceHeight) > h {
|
|
|
|
h = fy + faceHeight
|
2020-05-13 11:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
prevR = r
|
|
|
|
}
|
|
|
|
|
|
|
|
bounds := image.Point{
|
|
|
|
X: int(math.Ceil(fixed26_6ToFloat64(w))),
|
2020-05-13 16:31:17 +02:00
|
|
|
Y: int(math.Ceil(fixed26_6ToFloat64(h + faceDescent))),
|
2020-05-13 11:03:39 +02:00
|
|
|
}
|
2017-07-15 20:25:57 +02:00
|
|
|
|
2020-05-13 11:03:39 +02:00
|
|
|
return bounds
|
2017-07-15 20:25:57 +02:00
|
|
|
}
|