mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-27 11:12:44 +01:00
text: Unify draw calls for multiple runes (#535)
This commit is contained in:
parent
1571e04753
commit
5257e6f9a7
82
text/text.go
82
text/text.go
@ -57,7 +57,11 @@ var (
|
|||||||
colorMCache = map[colorMCacheKey]*colorMCacheEntry{}
|
colorMCache = map[colorMCacheKey]*colorMCacheEntry{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func drawGlyph(dst *ebiten.Image, face font.Face, r rune, x, y fixed.Int26_6, clr color.Color) {
|
func drawGlyph(dst *ebiten.Image, face font.Face, r rune, img *glyphImage, x, y fixed.Int26_6, clr color.Color) {
|
||||||
|
if img == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// RGBA() is in [0 - 0xffff]. Adjust them in [0 - 0xff].
|
// RGBA() is in [0 - 0xffff]. Adjust them in [0 - 0xff].
|
||||||
cr, cg, cb, ca := clr.RGBA()
|
cr, cg, cb, ca := clr.RGBA()
|
||||||
cr >>= 8
|
cr >>= 8
|
||||||
@ -68,11 +72,6 @@ func drawGlyph(dst *ebiten.Image, face font.Face, r rune, x, y fixed.Int26_6, cl
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
img := getGlyphImage(face, r)
|
|
||||||
if img == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
b := getGlyphBounds(face, r)
|
b := getGlyphBounds(face, r)
|
||||||
op := &ebiten.DrawImageOptions{}
|
op := &ebiten.DrawImageOptions{}
|
||||||
op.GeoM.Translate(fixed26_6ToFloat64(x+b.Min.X), fixed26_6ToFloat64(y+b.Min.Y))
|
op.GeoM.Translate(fixed26_6ToFloat64(x+b.Min.X), fixed26_6ToFloat64(y+b.Min.Y))
|
||||||
@ -107,8 +106,10 @@ func drawGlyph(dst *ebiten.Image, face font.Face, r rune, x, y fixed.Int26_6, cl
|
|||||||
colorMCache[key] = e
|
colorMCache[key] = e
|
||||||
}
|
}
|
||||||
op.ColorM = e.m
|
op.ColorM = e.m
|
||||||
|
re := image.Rect(img.x, img.y, img.x+img.width, img.y+img.height)
|
||||||
|
op.SourceRect = &re
|
||||||
|
|
||||||
_ = dst.DrawImage(img, op)
|
_ = dst.DrawImage(img.image, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -128,8 +129,16 @@ func getGlyphBounds(face font.Face, r rune) *fixed.Rectangle26_6 {
|
|||||||
return &b
|
return &b
|
||||||
}
|
}
|
||||||
|
|
||||||
type glyphImageCacheEntry struct {
|
type glyphImage struct {
|
||||||
image *ebiten.Image
|
image *ebiten.Image
|
||||||
|
x int
|
||||||
|
y int
|
||||||
|
width int
|
||||||
|
height int
|
||||||
|
}
|
||||||
|
|
||||||
|
type glyphImageCacheEntry struct {
|
||||||
|
image *glyphImage
|
||||||
atime int64
|
atime int64
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,7 +147,7 @@ var (
|
|||||||
emptyGlyphs = map[font.Face]map[rune]struct{}{}
|
emptyGlyphs = map[font.Face]map[rune]struct{}{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func getGlyphImage(face font.Face, r rune) *ebiten.Image {
|
func getGlyphImages(face font.Face, runes []rune) []*glyphImage {
|
||||||
if _, ok := emptyGlyphs[face]; !ok {
|
if _, ok := emptyGlyphs[face]; !ok {
|
||||||
emptyGlyphs[face] = map[rune]struct{}{}
|
emptyGlyphs[face] = map[rune]struct{}{}
|
||||||
}
|
}
|
||||||
@ -146,21 +155,27 @@ func getGlyphImage(face font.Face, r rune) *ebiten.Image {
|
|||||||
glyphImageCache[face] = map[rune]*glyphImageCacheEntry{}
|
glyphImageCache[face] = map[rune]*glyphImageCacheEntry{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
imgs := make([]*glyphImage, len(runes))
|
||||||
|
neededGlyphs := map[int]*fixed.Rectangle26_6{}
|
||||||
|
for i, r := range runes {
|
||||||
if _, ok := emptyGlyphs[face][r]; ok {
|
if _, ok := emptyGlyphs[face][r]; ok {
|
||||||
return nil
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if e, ok := glyphImageCache[face][r]; ok {
|
if e, ok := glyphImageCache[face][r]; ok {
|
||||||
e.atime = now()
|
e.atime = now()
|
||||||
return e.image
|
imgs[i] = e.image
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
b := getGlyphBounds(face, r)
|
b := getGlyphBounds(face, r)
|
||||||
w, h := (b.Max.X - b.Min.X).Ceil(), (b.Max.Y - b.Min.Y).Ceil()
|
w, h := (b.Max.X - b.Min.X).Ceil(), (b.Max.Y - b.Min.Y).Ceil()
|
||||||
if w == 0 || h == 0 {
|
if w == 0 || h == 0 {
|
||||||
emptyGlyphs[face][r] = struct{}{}
|
emptyGlyphs[face][r] = struct{}{}
|
||||||
return nil
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: What if len(runes) > cacheLimit?
|
||||||
if len(glyphImageCache[face]) > cacheLimit {
|
if len(glyphImageCache[face]) > cacheLimit {
|
||||||
oldest := int64(math.MaxInt64)
|
oldest := int64(math.MaxInt64)
|
||||||
oldestKey := rune(-1)
|
oldestKey := rune(-1)
|
||||||
@ -170,25 +185,53 @@ func getGlyphImage(face font.Face, r rune) *ebiten.Image {
|
|||||||
oldest = e.atime
|
oldest = e.atime
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
glyphImageCache[face][oldestKey].image.Dispose()
|
|
||||||
delete(glyphImageCache[face], oldestKey)
|
delete(glyphImageCache[face], oldestKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
rgba := image.NewRGBA(image.Rect(0, 0, w, h))
|
neededGlyphs[i] = b
|
||||||
|
}
|
||||||
|
|
||||||
|
w2 := 0
|
||||||
|
h2 := 0
|
||||||
|
for _, b := range neededGlyphs {
|
||||||
|
w, h := (b.Max.X - b.Min.X).Ceil(), (b.Max.Y - b.Min.Y).Ceil()
|
||||||
|
w2 += w
|
||||||
|
if h2 < h {
|
||||||
|
h2 = h
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rgba := image.NewRGBA(image.Rect(0, 0, w2, h2))
|
||||||
|
|
||||||
|
x := 0
|
||||||
|
for i, b := range neededGlyphs {
|
||||||
|
w, h := (b.Max.X - b.Min.X).Ceil(), (b.Max.Y - b.Min.Y).Ceil()
|
||||||
|
|
||||||
|
r := runes[i]
|
||||||
d := font.Drawer{
|
d := font.Drawer{
|
||||||
Dst: rgba,
|
Dst: rgba,
|
||||||
Src: image.White,
|
Src: image.White,
|
||||||
Face: face,
|
Face: face,
|
||||||
}
|
}
|
||||||
d.Dot = fixed.Point26_6{-b.Min.X, -b.Min.Y}
|
d.Dot = fixed.Point26_6{fixed.I(x) - b.Min.X, -b.Min.Y}
|
||||||
d.DrawString(string(r))
|
d.DrawString(string(r))
|
||||||
|
|
||||||
img, _ := ebiten.NewImageFromImage(rgba, ebiten.FilterDefault)
|
img, _ := ebiten.NewImageFromImage(rgba, ebiten.FilterDefault)
|
||||||
glyphImageCache[face][r] = &glyphImageCacheEntry{
|
g := &glyphImage{
|
||||||
image: img,
|
image: img,
|
||||||
|
x: x,
|
||||||
|
y: 0,
|
||||||
|
width: w,
|
||||||
|
height: h,
|
||||||
|
}
|
||||||
|
glyphImageCache[face][r] = &glyphImageCacheEntry{
|
||||||
|
image: g,
|
||||||
atime: now(),
|
atime: now(),
|
||||||
}
|
}
|
||||||
return img
|
imgs[i] = g
|
||||||
|
|
||||||
|
x += w
|
||||||
|
}
|
||||||
|
return imgs
|
||||||
}
|
}
|
||||||
|
|
||||||
var textM sync.Mutex
|
var textM sync.Mutex
|
||||||
@ -214,11 +257,12 @@ func Draw(dst *ebiten.Image, text string, face font.Face, x, y int, clr color.Co
|
|||||||
prevR := rune(-1)
|
prevR := rune(-1)
|
||||||
|
|
||||||
runes := []rune(text)
|
runes := []rune(text)
|
||||||
for _, r := range runes {
|
glyphImgs := getGlyphImages(face, runes)
|
||||||
|
for i, r := range runes {
|
||||||
if prevR >= 0 {
|
if prevR >= 0 {
|
||||||
fx += face.Kern(prevR, r)
|
fx += face.Kern(prevR, r)
|
||||||
}
|
}
|
||||||
drawGlyph(dst, face, r, fx, fixed.I(y), clr)
|
drawGlyph(dst, face, r, glyphImgs[i], fx, fixed.I(y), clr)
|
||||||
fx += glyphAdvance(face, r)
|
fx += glyphAdvance(face, r)
|
||||||
|
|
||||||
prevR = r
|
prevR = r
|
||||||
|
Loading…
Reference in New Issue
Block a user