ebiten/text/text.go

247 lines
5.5 KiB
Go
Raw Normal View History

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"
"reflect"
2017-07-15 20:25:57 +02:00
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/sync"
)
var (
monotonicClock int64
)
func now() int64 {
monotonicClock++
return monotonicClock
}
func fixed26_6ToFloat64(x fixed.Int26_6) float64 {
return float64(x) / (1 << 6)
}
const (
cacheLimit = 512 // This is an arbitrary number.
)
type colorMCacheKey uint32
2018-02-11 20:07:59 +01:00
type colorMCacheEntry struct {
m ebiten.ColorM
atime int64
}
var (
colorMCache = map[colorMCacheKey]*colorMCacheEntry{}
)
func drawGlyph(dst *ebiten.Image, face font.Face, r rune, x, y fixed.Int26_6, clr color.Color) {
// RGBA() is in [0 - 0xffff]. Adjust them in [0 - 0xff].
2017-07-15 20:25:57 +02:00
cr, cg, cb, ca := clr.RGBA()
cr >>= 8
cg >>= 8
cb >>= 8
ca >>= 8
2017-07-15 20:25:57 +02:00
if ca == 0 {
return
}
img := getGlyphImage(face, r)
if img == nil {
return
}
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))
2017-07-15 20:25:57 +02:00
key := colorMCacheKey(uint32(cr) | (uint32(cg) << 8) | (uint32(cb) << 16) | (uint32(ca) << 24))
e, ok := colorMCache[key]
2018-02-11 20:07:59 +01:00
if ok {
e.atime = now()
} else {
if len(colorMCache) > cacheLimit {
oldest := int64(math.MaxInt64)
oldestKey := colorMCacheKey(0)
for key, c := range colorMCache {
if c.atime < oldest {
oldestKey = key
oldest = c.atime
2018-02-11 20:07:59 +01:00
}
}
delete(colorMCache, oldestKey)
2018-02-11 20:07:59 +01:00
}
2018-02-11 20:07:59 +01:00
cm := ebiten.ColorM{}
rf := float64(cr) / float64(ca)
gf := float64(cg) / float64(ca)
bf := float64(cb) / float64(ca)
af := float64(ca) / 0xff
cm.Scale(rf, gf, bf, af)
2018-02-11 20:07:59 +01:00
e = &colorMCacheEntry{
m: cm,
atime: now(),
}
colorMCache[key] = e
}
2018-02-11 20:07:59 +01:00
op.ColorM = e.m
2017-07-15 20:25:57 +02:00
_ = dst.DrawImage(img, op)
2017-07-15 20:25:57 +02:00
}
var (
fontFaces = map[font.Face]struct{}{}
)
2017-07-15 20:25:57 +02:00
func uniqFace(f font.Face) font.Face {
if _, ok := fontFaces[f]; ok {
return f
}
// If the (DeepEqual-ly) same font exists,
// reuse this to avoid to consume a lot of cache (#498).
for key := range fontFaces {
if reflect.DeepEqual(key, f) {
return key
}
2017-07-15 20:25:57 +02:00
}
fontFaces[f] = struct{}{}
return f
2017-07-15 20:25:57 +02:00
}
var (
// Use pointers to avoid copying on browsers.
glyphBoundsCache = map[font.Face]map[rune]*fixed.Rectangle26_6{}
)
func getGlyphBounds(face font.Face, r rune) *fixed.Rectangle26_6 {
if _, ok := glyphBoundsCache[face]; !ok {
glyphBoundsCache[face] = map[rune]*fixed.Rectangle26_6{}
}
if b, ok := glyphBoundsCache[face][r]; ok {
return b
2017-07-15 20:25:57 +02:00
}
b, _, _ := face.GlyphBounds(r)
glyphBoundsCache[face][r] = &b
return &b
2017-07-15 20:25:57 +02:00
}
type glyphImageCacheEntry struct {
image *ebiten.Image
atime int64
}
2017-07-15 20:25:57 +02:00
var (
glyphImageCache = map[font.Face]map[rune]*glyphImageCacheEntry{}
emptyGlyphs = map[font.Face]map[rune]struct{}{}
)
2017-07-15 20:25:57 +02:00
func getGlyphImage(face font.Face, r rune) *ebiten.Image {
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
if _, ok := emptyGlyphs[face][r]; ok {
return nil
2018-02-11 17:08:53 +01:00
}
if e, ok := glyphImageCache[face][r]; ok {
e.atime = now()
return e.image
2017-07-15 20:25:57 +02: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 {
emptyGlyphs[face][r] = struct{}{}
return nil
2017-07-15 20:25:57 +02:00
}
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
}
2017-07-15 20:25:57 +02:00
}
glyphImageCache[face][oldestKey].image.Dispose()
delete(glyphImageCache[face], oldestKey)
2017-07-15 20:25:57 +02:00
}
rgba := image.NewRGBA(image.Rect(0, 0, w, h))
d := font.Drawer{
Dst: rgba,
Src: image.White,
Face: face,
2018-02-12 14:02:05 +01:00
}
d.Dot = fixed.Point26_6{-b.Min.X, -b.Min.Y}
d.DrawString(string(r))
img, _ := ebiten.NewImageFromImage(rgba, ebiten.FilterDefault)
glyphImageCache[face][r] = &glyphImageCacheEntry{
image: img,
atime: now(),
2018-02-12 14:02:05 +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.
// 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()
fx := fixed.I(x)
prevR := rune(-1)
2017-07-15 20:25:57 +02:00
runes := []rune(text)
for _, r := range runes {
if prevR >= 0 {
fx += face.Kern(prevR, r)
2017-07-15 20:25:57 +02:00
}
2018-02-12 14:02:05 +01:00
fa := uniqFace(face)
drawGlyph(dst, fa, r, fx, fixed.I(y), clr)
fx += glyphAdvance(fa, r)
prevR = r
2017-07-15 20:25:57 +02:00
}
textM.Unlock()
}