-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrender_test.go
More file actions
242 lines (198 loc) · 8.07 KB
/
render_test.go
File metadata and controls
242 lines (198 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package render_test
import (
"bytes"
"image"
"image/color"
"image/draw"
"image/png"
"io"
"os"
"strings"
"testing"
"github.com/go-text/render"
"github.com/go-text/typesetting/font"
"github.com/go-text/typesetting/shaping"
"golang.org/x/image/math/fixed"
ot "github.com/go-text/typesetting-utils/opentype"
)
func Test_Render(t *testing.T) {
img := image.NewNRGBA(image.Rect(0, 0, 425, 250))
draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.Point{}, draw.Src)
data, _ := os.Open("testdata/NotoSans-Regular.ttf")
f1, _ := font.ParseTTF(data)
r := &render.Renderer{
FontSize: 48,
Color: color.Black,
}
str := "Hello! ± ज्या"
r.DrawString(str, img, f1)
r.DrawStringAt(str, img, 0, 100, f1)
r.PixScale = 2
r.Color = color.Gray{Y: 0xcc}
r.DrawStringAt("baseline", img, 0, 180, f1)
data, _ = os.Open("testdata/NotoSans-Bold.ttf")
f2, _ := font.ParseTTF(data)
r.FontSize = 36
r.Color = color.NRGBA{R: 0xcc, G: 0, B: 0x33, A: 0x99}
x := r.DrawStringAt("Red", img, 60, 140, f2)
r.DrawStringAt("Bold", img, x, 140, f2)
// from https://github.com/adobe-fonts/emojione-color, MIT license
data, _ = os.Open("testdata/EmojiOneColor.otf")
f3, _ := font.ParseTTF(data)
r.FontSize = 36
r.DrawStringAt("🚀🖥️", img, 270, 80, f3)
data, _ = os.Open("testdata/Greybeard-22px.ttf")
f4, _ := font.ParseTTF(data)
r.FontSize = 22
r.Color = color.NRGBA{R: 0xcc, G: 0x66, B: 0x33, A: 0xcc}
r.DrawStringAt("\uE0A2░", img, 366, 164, f4)
content, _ := ot.Files.ReadFile("bitmap/cherry-10-r.otb")
f5, _ := font.ParseTTF(bytes.NewReader(content))
(&render.Renderer{FontSize: 10, PixScale: 1, Color: color.Black}).DrawStringAt("Hello, world!", img, 6, 10, f5)
str = "Hello ज्या 😀! 🎁 fin."
rs := []rune(str)
sh := &shaping.HarfbuzzShaper{}
in := shaping.Input{
Text: rs,
RunStart: 0,
RunEnd: len(rs),
Size: fixed.I(int(r.FontSize)),
}
seg := shaping.Segmenter{}
runs := seg.Split(in, fixedFontmap([]*font.Face{f1, f2, f3}))
line := make(shaping.Line, len(runs))
for i, run := range runs {
line[i] = sh.Shape(run)
}
x = 0
r.Color = color.NRGBA{R: 0x33, G: 0x99, B: 0x33, A: 0xcc}
for _, run := range line {
x = r.DrawShapedRunAt(run, img, x, 232)
}
w, _ := os.Create("testdata/out.png")
png.Encode(w, img)
w.Close()
}
func TestRender_PixScaleAdvance(t *testing.T) {
img := image.NewNRGBA(image.Rect(0, 0, 350, 180))
data, _ := os.Open("testdata/NotoSans-Regular.ttf")
f, _ := font.ParseTTF(data)
r := &render.Renderer{
FontSize: 48,
Color: color.Black,
}
str := "Testing"
adv0 := r.DrawString(str, img, f)
r.PixScale = 1 // instead of the zero value
adv1 := r.DrawString(str, img, f)
if adv0 != adv1 {
t.Error("unscaled font did not advance as default")
}
r.PixScale = 2
adv2 := r.DrawString(str, img, f)
if adv2 <= int(float32(adv1)*1.9) || adv2 >= int(float32(adv1)*2.1) {
t.Error("scaled font did not advance proportionately")
}
}
func TestRenderHindi(t *testing.T) {
text := "नमस्ते"
r := &render.Renderer{
FontSize: 30,
Color: color.Black,
}
img := image.NewNRGBA(image.Rect(0, 0, 120, 50))
draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.Point{}, draw.Src)
data, _ := os.Open("testdata/NotoSans-Regular.ttf")
face, _ := font.ParseTTF(data)
r.DrawString(text, img, face)
w, _ := os.Create("testdata/out_hindi.png")
png.Encode(w, img)
w.Close()
}
type fixedFontmap []*font.Face
// ResolveFace panics if the slice is empty
func (ff fixedFontmap) ResolveFace(r rune) *font.Face {
for _, f := range ff {
if _, has := f.NominalGlyph(r); has {
return f
}
}
return ff[0]
}
func TestBitmapBaseline(t *testing.T) {
text := "\U0001F615\U0001F618\U0001F616"
r := &render.Renderer{
FontSize: 40,
Color: color.Black,
}
img := image.NewNRGBA(image.Rect(0, 0, 150, 100))
draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.Point{}, draw.Src)
data, _ := ot.Files.ReadFile("bitmap/NotoColorEmoji.ttf")
face, _ := font.ParseTTF(bytes.NewReader(data))
r.DrawString(text, img, face)
// w, _ := os.Create("testdata/bitmap_emoji.png")
// png.Encode(w, img)
// w.Close()
// compare against the reference
var pngBytes bytes.Buffer
png.Encode(&pngBytes, img)
reference, _ := os.ReadFile("testdata/bitmap_emoji.png")
if !bytes.Equal(pngBytes.Bytes(), reference) {
t.Error("unexpected image output")
}
}
func TestMixedLTR_RTL(t *testing.T) {
s := "وَرَسُولِهِ وَإِنْ تُبْتُمْ فَلَكُمْ رُءُوسُ hello أَمْوَالِكُمْ لَا تَظْلِمُونَ وَلَا تُظْلَمُونَ ( 279 ) وَإِنْ كَانَ ذُو عُسْرَةٍ فَنَظِرَةٌ إِلَى مَيْسَرَةٍ"
data, _ := ot.Files.ReadFile("common/NotoSansArabic.ttf")
face, _ := font.ParseTTF(bytes.NewReader(data))
img := image.NewNRGBA(image.Rect(0, 0, 800, 40))
draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.Point{}, draw.Src)
r := render.Renderer{FontSize: 15, Color: color.Black}
r.DrawString(s, img, face)
// w, _ := os.Create("testdata/mixed_ltr_rtl.png")
// _ = png.Encode(w, img)
// w.Close()
// compare against the reference
var pngBytes bytes.Buffer
png.Encode(&pngBytes, img)
reference, _ := os.ReadFile("testdata/mixed_ltr_rtl.png")
if !bytes.Equal(pngBytes.Bytes(), reference) {
t.Error("unexpected image output")
}
}
func BenchmarkDrawString(b *testing.B) {
for name, bb := range map[string]struct {
text string
fontFile string
}{
"Hello World": {
text: "Hello, world!",
fontFile: "testdata/NotoSans-Regular.ttf",
},
"Lorem Ipsum": {
text: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
fontFile: "testdata/NotoSans-Regular.ttf",
},
"Arabic Ipsum": {
text: "هنالك العديد من الأنواع المتوفرة لنصوص لوريم إيبسوم، ولكن الغالبية تم تعديلها بشكل ما عبر إدخال بعض النوادر أو الكلمات العشوائية إلى النص. إن كنت تريد أن تستخدم نص لوريم إيبسوم ما، عليك أن تتحقق أولاً أن ليس هناك أي كلمات أو عبارات محرجة أو غير لائقة مخبأة في هذا النص. بينما تعمل جميع مولّدات نصوص لوريم إيبسوم على الإنترنت على إعادة تكرار مقاطع من نص لوريم إيبسوم نفسه عدة مرات بما تتطلبه الحاجة، يقوم مولّدنا هذا باستخدام كلمات من قاموس يحوي على أكثر من 200 كلمة لا تينية، مضاف إليها مجموعة من الجمل النموذجية، لتكوين نص لوريم إيبسوم ذو شكل منطقي قريب إلى النص الحقيقي. وبالتالي يكون النص الناتح خالي من التكرار، أو أي كلمات أو عبارات غير لائقة أو ما شابه. وهذا ما يجعله أول مولّد نص لوريم إيبسوم حقيقي على الإنترنت. ",
fontFile: "common/NotoSansArabic.ttf",
},
} {
b.Run(name, func(b *testing.B) {
var data []byte
if strings.HasPrefix(bb.fontFile, "testdata") {
f, _ := os.Open(bb.fontFile)
data, _ = io.ReadAll(f)
} else {
data, _ = ot.Files.ReadFile(bb.fontFile)
}
face, _ := font.ParseTTF(bytes.NewReader(data))
img := image.NewRGBA(image.Rect(0, 0, 800, 40))
draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.Point{}, draw.Src)
r := render.Renderer{FontSize: 15, Color: color.Black}
for i := 0; i < b.N; i++ {
r.DrawString(bb.text, img, face)
}
})
}
}