From 6a8f37453e1a788c1991f6cdd31299a88b644cd2 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 5 Dec 2023 21:40:53 +0900 Subject: [PATCH] text/v2: bug fix: MultiFace should use the last face as the final resort --- text/v2/multi.go | 7 ++--- text/v2/multi_test.go | 63 +++++++++++++++++++++++++++++++++++++++++++ text/v2/text_test.go | 14 ---------- 3 files changed, 67 insertions(+), 17 deletions(-) create mode 100644 text/v2/multi_test.go diff --git a/text/v2/multi.go b/text/v2/multi.go index 92d517c29..d84ace585 100644 --- a/text/v2/multi.go +++ b/text/v2/multi.go @@ -160,17 +160,18 @@ func (m *MultiFace) splitText(text string) []textChunk { var chunks []textChunk for ri, r := range text { - // -1 indicates the default face index. -1 is used when no face is found for the glyph. fi := -1 - _, l := utf8.DecodeRuneInString(text[ri:]) for i, f := range m.faces { - if !f.hasGlyph(r) { + if !f.hasGlyph(r) && i < len(m.faces)-1 { continue } fi = i break } + if fi == -1 { + panic("text: a face was not selected correctly") + } var s int if len(chunks) > 0 { diff --git a/text/v2/multi_test.go b/text/v2/multi_test.go new file mode 100644 index 000000000..894447d8b --- /dev/null +++ b/text/v2/multi_test.go @@ -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)) + } +} diff --git a/text/v2/text_test.go b/text/v2/text_test.go index 01e123d62..21b258012 100644 --- a/text/v2/text_test.go +++ b/text/v2/text_test.go @@ -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) -}