text/v2: bug fix: MultiFace should use the last face as the final resort

This commit is contained in:
Hajime Hoshi 2023-12-05 21:40:53 +09:00
parent 668124d25c
commit 6a8f37453e
3 changed files with 67 additions and 17 deletions

View File

@ -160,17 +160,18 @@ func (m *MultiFace) splitText(text string) []textChunk {
var chunks []textChunk var chunks []textChunk
for ri, r := range text { for ri, r := range text {
// -1 indicates the default face index. -1 is used when no face is found for the glyph.
fi := -1 fi := -1
_, l := utf8.DecodeRuneInString(text[ri:]) _, l := utf8.DecodeRuneInString(text[ri:])
for i, f := range m.faces { for i, f := range m.faces {
if !f.hasGlyph(r) { if !f.hasGlyph(r) && i < len(m.faces)-1 {
continue continue
} }
fi = i fi = i
break break
} }
if fi == -1 {
panic("text: a face was not selected correctly")
}
var s int var s int
if len(chunks) > 0 { if len(chunks) > 0 {

63
text/v2/multi_test.go Normal file
View File

@ -0,0 +1,63 @@
// Copyright 2023 The Ebitengine 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_test
import (
"bytes"
"testing"
"github.com/hajimehoshi/bitmapfont/v3"
"golang.org/x/image/font/gofont/goregular"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text/v2"
)
func TestMultiFace(t *testing.T) {
faces := []text.Face{text.NewStdFace(bitmapfont.Face)}
f, err := text.NewMultiFace(faces...)
if err != nil {
t.Fatal(err)
}
img := ebiten.NewImage(30, 30)
text.Draw(img, "Hello", f, nil)
// Confirm that the given slice doesn't cause crash.
faces[0] = nil
text.Draw(img, "World", f, nil)
}
func TestMultiFaceFallback(t *testing.T) {
enFaceSource, err := text.NewGoTextFaceSource(bytes.NewReader(goregular.TTF))
if err != nil {
t.Fatal(err)
}
enFace := &text.GoTextFace{
Source: enFaceSource,
Size: 10,
}
multiFace, err := text.NewMultiFace(enFace)
if err != nil {
t.Fatal(err)
}
// If all the faces in a MultiFace doesn't have a glyph, the last face should be used.
str := "あ"
got := text.AppendGlyphs(nil, str, multiFace, nil)
want := text.AppendGlyphs(nil, str, enFace, nil)
if len(got) != len(want) {
t.Errorf("got: %d, want: %d", len(got), len(want))
}
}

View File

@ -240,17 +240,3 @@ func TestUnhashableFace(t *testing.T) {
} }
} }
} }
func TestMultiFace(t *testing.T) {
faces := []text.Face{text.NewStdFace(bitmapfont.Face)}
f, err := text.NewMultiFace(faces...)
if err != nil {
t.Fatal(err)
}
img := ebiten.NewImage(30, 30)
text.Draw(img, "Hello", f, nil)
// Confirm that the given slice doesn't cause crash.
faces[0] = nil
text.Draw(img, "World", f, nil)
}