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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/RefPtr.h"
#include "nsCOMPtr.h"
#include "nsTArray.h"
#include "nsString.h"
#include "nsDependentString.h"
#include "mozilla/Preferences.h"
#include "gfxContext.h"
#include "gfxFont.h"
#include "gfxPlatform.h"
#include "gfxFontTest.h"
using namespace mozilla;
using namespace mozilla::gfx;
enum {
S_UTF8 = 0,
S_ASCII = 1
};
class FrameTextRunCache;
struct LiteralArray {
LiteralArray (unsigned long l1) {
data.AppendElement(l1);
}
LiteralArray (unsigned long l1, unsigned long l2) {
data.AppendElement(l1);
data.AppendElement(l2);
}
LiteralArray (unsigned long l1, unsigned long l2, unsigned long l3) {
data.AppendElement(l1);
data.AppendElement(l2);
data.AppendElement(l3);
}
LiteralArray (unsigned long l1, unsigned long l2, unsigned long l3, unsigned long l4) {
data.AppendElement(l1);
data.AppendElement(l2);
data.AppendElement(l3);
data.AppendElement(l4);
}
LiteralArray (unsigned long l1, unsigned long l2, unsigned long l3, unsigned long l4, unsigned long l5) {
data.AppendElement(l1);
data.AppendElement(l2);
data.AppendElement(l3);
data.AppendElement(l4);
data.AppendElement(l5);
}
LiteralArray (const LiteralArray& other) {
data = other.data;
}
nsTArray<unsigned long> data;
};
#define GLYPHS LiteralArray
struct TestEntry {
TestEntry (const char *aUTF8FamilyString,
const gfxFontStyle& aFontStyle,
const char *aString)
: utf8FamilyString(aUTF8FamilyString),
fontStyle(aFontStyle),
stringType(S_ASCII),
string(aString),
isRTL(false)
{
}
TestEntry (const char *aUTF8FamilyString,
const gfxFontStyle& aFontStyle,
int stringType,
const char *aString)
: utf8FamilyString(aUTF8FamilyString),
fontStyle(aFontStyle),
stringType(stringType),
string(aString),
isRTL(false)
{
}
struct ExpectItem {
ExpectItem(const nsCString& aFontName,
const LiteralArray& aGlyphs)
: fontName(aFontName), glyphs(aGlyphs)
{ }
bool Compare(const nsCString& aFontName,
cairo_glyph_t *aGlyphs,
int num_glyphs)
{
// bit that allowed for empty fontname to match all is commented
// out
if (/*!fontName.IsEmpty() &&*/ !fontName.Equals(aFontName))
return false;
if (num_glyphs != int(glyphs.data.Length()))
return false;
for (int j = 0; j < num_glyphs; j++) {
if (glyphs.data[j] != aGlyphs[j].index)
return false;
}
return true;
}
nsCString fontName;
LiteralArray glyphs;
};
void SetRTL()
{
isRTL = true;
}
// Empty/nullptr fontName means ignore font name
void Expect (const char *platform,
const char *fontName,
const LiteralArray& glyphs)
{
if (fontName)
Expect (platform, nsDependentCString(fontName), glyphs);
else
Expect (platform, nsCString(), glyphs);
}
void Expect (const char *platform,
const nsCString& fontName,
const LiteralArray& glyphs)
{
#if defined(XP_WIN)
if (strcmp(platform, "win32"))
return;
#elif defined(XP_MACOSX)
if (strcmp(platform, "macosx"))
return;
#elif defined(XP_UNIX)
if (strcmp(platform, "gtk2-pango"))
return;
#else
return;
#endif
expectItems.AppendElement(ExpectItem(fontName, glyphs));
}
bool Check (gfxFontTestStore *store) {
if (expectItems.Length() == 0 ||
store->items.Length() != expectItems.Length())
{
return false;
}
for (uint32_t i = 0; i < expectItems.Length(); i++) {
if (!expectItems[i].Compare(store->items[i].platformFont,
store->items[i].glyphs,
store->items[i].num_glyphs))
return false;
}
return true;
}
const char *utf8FamilyString;
gfxFontStyle fontStyle;
int stringType;
const char *string;
bool isRTL;
nsTArray<ExpectItem> expectItems;
};
static already_AddRefed<gfxContext>
MakeContext ()
{
const int size = 200;
RefPtr<DrawTarget> drawTarget = gfxPlatform::GetPlatform()->
CreateOffscreenContentDrawTarget(IntSize(size, size),
SurfaceFormat::B8G8R8X8);
RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(drawTarget);
if (!ctx) {
MOZ_CRASH("gfxContext creation failed");
}
return ctx.forget();
}
TestEntry*
AddTest (nsTArray<TestEntry>& testList,
const char *utf8FamilyString,
const gfxFontStyle& fontStyle,
int stringType,
const char *string)
{
TestEntry te (utf8FamilyString,
fontStyle,
stringType,
string);
testList.AppendElement(te);
return &(testList[testList.Length()-1]);
}
void
DumpStore (gfxFontTestStore *store) {
if (store->items.Length() == 0) {
printf ("(empty)\n");
}
for (uint32_t i = 0;
i < store->items.Length();
i++)
{
printf ("Run[% 2d]: '%s' ", i, store->items[i].platformFont.BeginReading());
for (int j = 0; j < store->items[i].num_glyphs; j++)
printf ("%d ", int(store->items[i].glyphs[j].index));
printf ("\n");
}
}
void
DumpTestExpect (TestEntry *test) {
for (uint32_t i = 0; i < test->expectItems.Length(); i++) {
printf ("Run[% 2d]: '%s' ", i, test->expectItems[i].fontName.BeginReading());
for (uint32_t j = 0; j < test->expectItems[i].glyphs.data.Length(); j++)
printf ("%d ", int(test->expectItems[i].glyphs.data[j]));
printf ("\n");
}
}
void SetupTests(nsTArray<TestEntry>& testList);
static bool
RunTest (TestEntry *test, gfxContext *ctx) {
RefPtr<gfxFontGroup> fontGroup;
fontGroup = gfxPlatform::GetPlatform()->CreateFontGroup(NS_ConvertUTF8toUTF16(test->utf8FamilyString), &test->fontStyle, nullptr, nullptr, 1.0);
RefPtr<gfxTextRun> textRun;
gfxTextRunFactory::Parameters params = {
ctx, nullptr, nullptr, nullptr, 0, 60
};
uint32_t flags = gfxTextRunFactory::TEXT_IS_PERSISTENT;
if (test->isRTL) {
flags |= gfxTextRunFactory::TEXT_IS_RTL;
}
uint32_t length;
if (test->stringType == S_ASCII) {
flags |= gfxTextRunFactory::TEXT_IS_ASCII | gfxTextRunFactory::TEXT_IS_8BIT;
length = strlen(test->string);
textRun = fontGroup->MakeTextRun(
reinterpret_cast<const uint8_t*>(test->string), length, ¶ms, flags);
} else {
NS_ConvertUTF8toUTF16 str(nsDependentCString(test->string));
length = str.Length();
textRun = fontGroup->MakeTextRun(str.get(), length, ¶ms, flags);
}
gfxFontTestStore::NewStore();
textRun->Draw(ctx, gfxPoint(0,0), DrawMode::GLYPH_FILL, 0, length, nullptr, nullptr, nullptr);
gfxFontTestStore *s = gfxFontTestStore::CurrentStore();
if (!test->Check(s)) {
DumpStore(s);
printf (" expected:\n");
DumpTestExpect(test);
gfxFontTestStore::DeleteStore();
return false;
}
gfxFontTestStore::DeleteStore();
return true;
}
TEST(Gfx, FontSelection) {
int passed = 0;
int failed = 0;
// set up the tests
nsTArray<TestEntry> testList;
SetupTests(testList);
RefPtr<gfxContext> context = MakeContext();
for (uint32_t test = 0;
test < testList.Length();
test++)
{
bool result = RunTest (&testList[test], context);
if (result) {
passed++;
} else {
printf ("Test %d failed\n", test);
failed++;
}
}
}
// The tests themselves
#include "gfxFontSelectionTests.h"
|