1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Corporation code.
17 * The Initial Developer of the Original Code is Mozilla Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2007
19 * the Initial Developer. All Rights Reserved.
22 * Vladimir Vukicevic <vladimir@pobox.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
41 #include "nsDependentString.h"
43 #include "nsServiceManagerUtils.h"
44 #include "nsIPrefService.h"
45 #include "nsIPrefBranch.h"
47 #include "gfxContext.h"
49 #include "gfxPlatform.h"
50 #include "gfxTextRunWordCache.h"
52 #include "gfxFontTest.h"
54 #if defined(XP_MACOSX)
55 #include "gfxTestCocoaHelper.h"
58 #ifdef MOZ_WIDGET_GTK2
67 class FrameTextRunCache
;
69 static gfxTextRunWordCache
*gTextRunCache
;
72 LiteralArray (unsigned long l1
) {
73 data
.AppendElement(l1
);
75 LiteralArray (unsigned long l1
, unsigned long l2
) {
76 data
.AppendElement(l1
);
77 data
.AppendElement(l2
);
79 LiteralArray (unsigned long l1
, unsigned long l2
, unsigned long l3
) {
80 data
.AppendElement(l1
);
81 data
.AppendElement(l2
);
82 data
.AppendElement(l3
);
84 LiteralArray (unsigned long l1
, unsigned long l2
, unsigned long l3
, unsigned long l4
) {
85 data
.AppendElement(l1
);
86 data
.AppendElement(l2
);
87 data
.AppendElement(l3
);
88 data
.AppendElement(l4
);
90 LiteralArray (unsigned long l1
, unsigned long l2
, unsigned long l3
, unsigned long l4
, unsigned long l5
) {
91 data
.AppendElement(l1
);
92 data
.AppendElement(l2
);
93 data
.AppendElement(l3
);
94 data
.AppendElement(l4
);
95 data
.AppendElement(l5
);
98 LiteralArray (const LiteralArray
& other
) {
102 nsTArray
<unsigned long> data
;
105 #define GLYPHS LiteralArray
108 TestEntry (char *aUTF8FamilyString
,
109 const gfxFontStyle
& aFontStyle
,
111 : utf8FamilyString(aUTF8FamilyString
),
112 fontStyle(aFontStyle
),
119 TestEntry (char *aUTF8FamilyString
,
120 const gfxFontStyle
& aFontStyle
,
123 : utf8FamilyString(aUTF8FamilyString
),
124 fontStyle(aFontStyle
),
125 stringType(stringType
),
132 ExpectItem(const nsCString
& aFontName
,
133 const LiteralArray
& aGlyphs
)
134 : fontName(aFontName
), glyphs(aGlyphs
)
137 PRBool
Compare(const nsCString
& aFontName
,
138 cairo_glyph_t
*aGlyphs
,
141 // bit that allowed for empty fontname to match all is commented
143 if (/*!fontName.IsEmpty() &&*/ !fontName
.Equals(aFontName
))
146 if (num_glyphs
!= int(glyphs
.data
.Length()))
149 for (int j
= 0; j
< num_glyphs
; j
++) {
150 if (glyphs
.data
[j
] != aGlyphs
[j
].index
)
166 // empty/NULL fontName means ignore font name
167 void Expect (const char *platform
,
168 const char *fontName
,
169 const LiteralArray
& glyphs
)
172 Expect (platform
, nsDependentCString(fontName
), glyphs
);
174 Expect (platform
, nsCString(), glyphs
);
177 void Expect (const char *platform
,
178 const nsCString
& fontName
,
179 const LiteralArray
& glyphs
)
182 if (strcmp(platform
, "win32"))
184 #elif defined(XP_MACOSX)
185 if (strcmp(platform
, "macosx"))
187 #elif defined(XP_UNIX)
188 if (strcmp(platform
, "gtk2-pango"))
194 expectItems
.AppendElement(ExpectItem(fontName
, glyphs
));
197 PRBool
Check (gfxFontTestStore
*store
) {
198 if (expectItems
.Length() == 0 ||
199 store
->items
.Length() != expectItems
.Length())
204 for (PRUint32 i
= 0; i
< expectItems
.Length(); i
++) {
205 if (!expectItems
[i
].Compare(store
->items
[i
].platformFont
,
206 store
->items
[i
].glyphs
,
207 store
->items
[i
].num_glyphs
))
214 char *utf8FamilyString
;
215 gfxFontStyle fontStyle
;
221 nsTArray
<ExpectItem
> expectItems
;
224 nsTArray
<TestEntry
> testList
;
226 already_AddRefed
<gfxContext
>
229 const int size
= 200;
231 nsRefPtr
<gfxASurface
> surface
;
233 surface
= gfxPlatform::GetPlatform()->CreateOffscreenSurface(gfxIntSize(size
, size
), gfxASurface::ImageFormatRGB24
);
234 gfxContext
*ctx
= new gfxContext(surface
);
240 AddTest (char *utf8FamilyString
,
241 const gfxFontStyle
& fontStyle
,
245 TestEntry
te (utf8FamilyString
,
250 testList
.AppendElement(te
);
252 return &(testList
[testList
.Length()-1]);
258 DumpStore (gfxFontTestStore
*store
) {
259 if (store
->items
.Length() == 0) {
260 printf ("(empty)\n");
264 i
< store
->items
.Length();
267 printf ("Run[% 2d]: '%s' ", i
, nsPromiseFlatCString(store
->items
[i
].platformFont
).get());
269 for (int j
= 0; j
< store
->items
[i
].num_glyphs
; j
++)
270 printf ("%d ", int(store
->items
[i
].glyphs
[j
].index
));
277 DumpTestExpect (TestEntry
*test
) {
278 for (PRUint32 i
= 0; i
< test
->expectItems
.Length(); i
++) {
279 printf ("Run[% 2d]: '%s' ", i
, nsPromiseFlatCString(test
->expectItems
[i
].fontName
).get());
280 for (PRUint32 j
= 0; j
< test
->expectItems
[i
].glyphs
.data
.Length(); j
++)
281 printf ("%d ", int(test
->expectItems
[i
].glyphs
.data
[j
]));
288 RunTest (TestEntry
*test
, gfxContext
*ctx
) {
289 nsRefPtr
<gfxFontGroup
> fontGroup
;
291 fontGroup
= gfxPlatform::GetPlatform()->CreateFontGroup(NS_ConvertUTF8toUTF16(test
->utf8FamilyString
), &test
->fontStyle
, nsnull
);
293 nsAutoPtr
<gfxTextRun
> textRun
;
294 gfxTextRunFactory::Parameters params
= {
295 ctx
, nsnull
, nsnull
, nsnull
, 0, 60
297 PRUint32 flags
= gfxTextRunFactory::TEXT_IS_PERSISTENT
;
299 flags
|= gfxTextRunFactory::TEXT_IS_RTL
;
302 if (test
->stringType
== S_ASCII
) {
303 flags
|= gfxTextRunFactory::TEXT_IS_ASCII
| gfxTextRunFactory::TEXT_IS_8BIT
;
304 length
= strlen(test
->string
);
305 textRun
= gfxTextRunWordCache::MakeTextRun(reinterpret_cast<PRUint8
*>(test
->string
), length
, fontGroup
, ¶ms
, flags
);
307 flags
|= gfxTextRunFactory::TEXT_HAS_SURROGATES
; // just in case
308 NS_ConvertUTF8toUTF16
str(nsDependentCString(test
->string
));
309 length
= str
.Length();
310 textRun
= gfxTextRunWordCache::MakeTextRun(str
.get(), length
, fontGroup
, ¶ms
, flags
);
313 gfxFontTestStore::NewStore();
314 textRun
->Draw(ctx
, gfxPoint(0,0), 0, length
, nsnull
, nsnull
, nsnull
);
315 gfxFontTestStore
*s
= gfxFontTestStore::CurrentStore();
317 gTextRunCache
->RemoveTextRun(textRun
);
319 if (!test
->Check(s
)) {
321 printf (" expected:\n");
322 DumpTestExpect(test
);
330 main (int argc
, char **argv
) {
334 #ifdef MOZ_WIDGET_GTK2
335 gtk_init(&argc
, &argv
);
342 nsresult rv
= NS_InitXPCOM2(nsnull
, nsnull
, nsnull
);
346 rv
= gfxPlatform::Init();
350 gTextRunCache
= new gfxTextRunWordCache();
352 // let's get all the xpcom goop out of the system
356 // don't need to query, we might need to set up some prefs later
360 nsCOMPtr
<nsIPrefService
> prefsvc
= do_GetService(NS_PREFSERVICE_CONTRACTID
);
362 printf ("Pref svc get failed!\n");
365 nsCOMPtr
<nsIPrefBranch
> branch
;
366 rv
= prefsvc
->GetBranch(nsnull
, getter_AddRefs(branch
));
368 printf ("Failed 0x%08x\n", rv
);
371 rv
= branch
->GetCharPref("font.name.sans-serif.x-western", getter_Copies(str
));
373 printf ("Failed[2] 0x%08x\n", rv
);
375 printf ("sans-serif.x-western: %s\n", nsPromiseFlatCString(str
).get());
381 nsRefPtr
<gfxContext
> context
= MakeContext();
384 test
< testList
.Length();
387 printf ("==== Test %d\n", test
);
388 PRBool result
= RunTest (&testList
[test
], context
);
390 printf ("Test %d succeeded\n", test
);
393 printf ("Test %d failed\n", test
);
398 printf ("PASSED: %d FAILED: %d\n", passed
, failed
);
403 // The tests themselves
405 #include "gfxFontSelectionTests.h"