Files
ngzz-mc/internal/textr/text_test.go

67 lines
1.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package textr
import (
"image/color"
"path/filepath"
"testing"
"mc/internal/assets"
"mc/internal/font"
)
// newTestDrawer 从真实素材包构建字体图集与绘制器。
func newTestDrawer(t *testing.T) *Drawer {
t.Helper()
m := assets.New()
if err := m.Load(filepath.Join("..", "..", "assets"), filepath.Join("..", "..", "resourcepacks")); err != nil {
t.Fatalf("加载内容包失败: %v", err)
}
at, err := font.Build(m, "default")
if err != nil {
t.Fatalf("构建字体失败: %v", err)
}
return New(at)
}
// TestWidth 文本宽度 = 字符数 × 8。
func TestWidth(t *testing.T) {
d := newTestDrawer(t)
if w := d.Width("AB"); w != 16 {
t.Fatalf("宽度期望 16,实际 %d", w)
}
if w := d.Width(""); w != 0 {
t.Fatalf("空串宽度期望 0,实际 %d", w)
}
}
// TestDraw 渲染 'A' 得到 8×8 且含非透明像素。
func TestDraw(t *testing.T) {
d := newTestDrawer(t)
img := d.Draw("A", color.RGBA{255, 255, 255, 255}, false)
b := img.Bounds()
if b.Dx() != 8 || b.Dy() != 8 {
t.Fatalf("图像尺寸期望 8×8,实际 %dx%d", b.Dx(), b.Dy())
}
opaque := 0
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
if _, _, _, a := img.At(x, y).RGBA(); a > 0 {
opaque++
}
}
}
if opaque == 0 {
t.Fatal("渲染结果全透明")
}
}
// TestDrawShadow 阴影模式:图像加 1px 且含黑色像素。
func TestDrawShadow(t *testing.T) {
d := newTestDrawer(t)
img := d.Draw("A", color.RGBA{255, 255, 255, 255}, true)
b := img.Bounds()
if b.Dx() != 9 || b.Dy() != 9 {
t.Fatalf("阴影图像尺寸期望 9×9,实际 %dx%d", b.Dx(), b.Dy())
}
}