Skip to content

Commit ac0b2ba

Browse files
committed
fix: Sheet.Print does not print text
pifont.Sheet.Print does not print text when the current color is equal to Sheet.BgColor
1 parent 2ac048d commit ac0b2ba

File tree

6 files changed

+112
-7
lines changed

6 files changed

+112
-7
lines changed

pifont/internal/test/font.png

144 Bytes
Loading
117 Bytes
Loading
119 Bytes
Loading
117 Bytes
Loading

pifont/pifont.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Sheet struct {
1717
BgColor pi.Color // background color on sprites
1818
}
1919

20-
var intermediateCanvas pi.Canvas // text is first rendered here to change its color
20+
var intermediateCanvas pi.Canvas // text is first rendered here to change its color from FgColor to selected color
2121

2222
var prevFgColorTable [pi.MaxColors]pi.Color
2323
var prevBgColorTable [pi.MaxColors]pi.Color
@@ -33,27 +33,41 @@ func (s Sheet) Print(str string, x, y int) (currentX, currentY int) {
3333

3434
currentColor := pi.GetColor()
3535

36-
copy(prevFgColorTable[:], pi.ColorTables[0][s.FgColor][:])
37-
copy(prevBgColorTable[:], pi.ColorTables[0][s.BgColor][:])
36+
prevFgColorTable = pi.ColorTables[0][s.FgColor]
37+
prevBgColorTable = pi.ColorTables[0][s.BgColor]
38+
39+
// create fake bg color to avoid a situation when fg and bg colors are the same
40+
bgColor := (currentColor + 1) % pi.MaxColors
3841
intermediateCanvas.Clear(s.BgColor)
3942
pi.Pal(s.FgColor, currentColor)
40-
pi.Palt(s.BgColor, true)
43+
pi.Pal(s.BgColor, bgColor)
4144
pi.SetDrawTarget(intermediateCanvas)
4245

4346
// first draw text in selected color on intermediateCanvas
4447
currentX, currentY = s.PrintOriginal(str, x, y)
4548

46-
copy(pi.ColorTables[0][s.FgColor][:], prevFgColorTable[:])
49+
// revert color tables
50+
pi.ColorTables[0][s.FgColor] = prevFgColorTable
51+
pi.ColorTables[0][s.BgColor] = prevBgColorTable
52+
53+
// make bgColor transparent
54+
prevBgColorTable = pi.ColorTables[0][bgColor]
55+
pi.Palt(bgColor, true)
4756

4857
// now copy text in target color on original draw target
4958
coloredText := pi.Sprite{
50-
Area: pi.Area[int]{X: x - pi.Camera.X, Y: y - pi.Camera.Y, W: currentX - x, H: currentY - y + s.Height},
59+
Area: pi.Area[int]{
60+
X: x - pi.Camera.X,
61+
Y: y - pi.Camera.Y,
62+
W: currentX - x,
63+
H: currentY - y + s.Height,
64+
},
5165
Source: intermediateCanvas,
5266
}
5367
pi.SetDrawTarget(originalDrawTarget)
5468
pi.Spr(coloredText, x, y)
5569

56-
copy(pi.ColorTables[0][s.BgColor][:], prevBgColorTable[:])
70+
pi.ColorTables[0][bgColor] = prevBgColorTable
5771

5872
return
5973
}

pifont/pifont_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,103 @@
44
package pifont_test
55

66
import (
7+
_ "embed"
8+
"github.com/elgopher/pi/pitest"
79
"testing"
810

911
"github.com/elgopher/pi"
1012
"github.com/elgopher/pi/pifont"
1113
)
1214

15+
//go:embed internal/test/font.png
16+
var fontPNG []byte
17+
18+
var fontSheet pifont.Sheet
19+
20+
func init() {
21+
prevPalette := pi.Palette
22+
defer func() {
23+
pi.Palette = prevPalette
24+
}()
25+
26+
pi.Palette = pi.DecodePalette(fontPNG)
27+
fontCanvas := pi.DecodeCanvas(fontPNG)
28+
fontSheet = pifont.Sheet{
29+
Chars: map[rune]pi.Sprite{
30+
'S': {
31+
Area: pi.Area[int]{X: 0, Y: 0, W: 8, H: 8},
32+
Source: fontCanvas,
33+
},
34+
'T': {
35+
Area: pi.Area[int]{X: 8, Y: 0, W: 8, H: 8},
36+
Source: fontCanvas,
37+
},
38+
'⬤': {
39+
Area: pi.Area[int]{X: 0, Y: 8, W: 8, H: 8},
40+
Source: fontCanvas,
41+
},
42+
'❤': {
43+
Area: pi.Area[int]{X: 8, Y: 8, W: 8, H: 8},
44+
Source: fontCanvas,
45+
},
46+
},
47+
Height: 8,
48+
FgColor: 1,
49+
BgColor: 0,
50+
}
51+
}
52+
53+
var (
54+
//go:embed internal/test/text-color-equal-to-bg.png
55+
textColorEqualToBg []byte
56+
//go:embed internal/test/text-color-equal-to-fg.png
57+
textColorEqualToFg []byte
58+
//go:embed internal/test/text-color-different.png
59+
textColorDifferent []byte
60+
)
61+
62+
func TestSheet_Print(t *testing.T) {
63+
t.Run("should print with different colors", func(t *testing.T) {
64+
tests := map[string]struct {
65+
bgColor pi.Color
66+
textColor pi.Color
67+
png []byte
68+
}{
69+
"text color different than Bg and Fg": {
70+
bgColor: 0,
71+
textColor: 2,
72+
png: textColorDifferent,
73+
},
74+
"text color equal to Bg": {
75+
bgColor: 2,
76+
textColor: 0,
77+
png: textColorEqualToBg,
78+
},
79+
"text color equal to Fg": {
80+
bgColor: 0,
81+
textColor: 1,
82+
png: textColorEqualToFg,
83+
},
84+
}
85+
86+
for testName, testCase := range tests {
87+
t.Run(testName, func(t *testing.T) {
88+
pi.Palette = pi.DecodePalette(testCase.png)
89+
expectedCanvas := pi.DecodeCanvas(testCase.png)
90+
pi.SetScreenSize(8, 8)
91+
92+
pi.Screen().Clear(testCase.bgColor)
93+
pi.SetColor(testCase.textColor)
94+
pi.Palt(testCase.textColor, false)
95+
// when
96+
fontSheet.Print("S", 0, 0)
97+
// then
98+
pitest.AssertSurfaceEqual(t, expectedCanvas, pi.Screen())
99+
})
100+
}
101+
})
102+
}
103+
13104
func BenchmarkSheet_Print(b *testing.B) {
14105
sheet := pifont.Sheet{
15106
Chars: map[rune]pi.Sprite{

0 commit comments

Comments
 (0)