2007-01-20 00:23:38 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2007-01-20 00:23:38 +03:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsDependentString.h"
|
|
|
|
|
2011-06-12 06:30:16 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2007-01-20 00:23:38 +03:00
|
|
|
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxFont.h"
|
|
|
|
#include "gfxPlatform.h"
|
|
|
|
|
|
|
|
#include "gfxFontTest.h"
|
|
|
|
|
2007-04-02 23:06:16 +04:00
|
|
|
#if defined(XP_MACOSX)
|
2007-01-20 00:23:38 +03:00
|
|
|
#include "gfxTestCocoaHelper.h"
|
|
|
|
#endif
|
|
|
|
|
2012-06-28 04:15:32 +04:00
|
|
|
#ifdef MOZ_WIDGET_GTK
|
2007-03-20 06:46:17 +03:00
|
|
|
#include "gtk/gtk.h"
|
|
|
|
#endif
|
|
|
|
|
2011-06-12 06:30:16 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2007-01-20 00:23:38 +03:00
|
|
|
enum {
|
|
|
|
S_UTF8 = 0,
|
|
|
|
S_ASCII = 1
|
|
|
|
};
|
|
|
|
|
2007-06-20 23:49:33 +04:00
|
|
|
class FrameTextRunCache;
|
|
|
|
|
2007-01-20 00:23:38 +03:00
|
|
|
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 {
|
2009-08-18 19:48:32 +04:00
|
|
|
TestEntry (const char *aUTF8FamilyString,
|
2007-01-20 00:23:38 +03:00
|
|
|
const gfxFontStyle& aFontStyle,
|
2009-08-18 19:48:32 +04:00
|
|
|
const char *aString)
|
2007-01-20 00:23:38 +03:00
|
|
|
: utf8FamilyString(aUTF8FamilyString),
|
|
|
|
fontStyle(aFontStyle),
|
|
|
|
stringType(S_ASCII),
|
2007-03-20 06:46:17 +03:00
|
|
|
string(aString),
|
2011-10-17 18:59:28 +04:00
|
|
|
isRTL(false)
|
2007-01-20 00:23:38 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-08-18 19:48:32 +04:00
|
|
|
TestEntry (const char *aUTF8FamilyString,
|
2007-01-20 00:23:38 +03:00
|
|
|
const gfxFontStyle& aFontStyle,
|
|
|
|
int stringType,
|
2009-08-18 19:48:32 +04:00
|
|
|
const char *aString)
|
2007-01-20 00:23:38 +03:00
|
|
|
: utf8FamilyString(aUTF8FamilyString),
|
|
|
|
fontStyle(aFontStyle),
|
|
|
|
stringType(stringType),
|
2007-03-20 06:46:17 +03:00
|
|
|
string(aString),
|
2011-10-17 18:59:28 +04:00
|
|
|
isRTL(false)
|
2007-01-20 00:23:38 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ExpectItem {
|
|
|
|
ExpectItem(const nsCString& aFontName,
|
|
|
|
const LiteralArray& aGlyphs)
|
|
|
|
: fontName(aFontName), glyphs(aGlyphs)
|
|
|
|
{ }
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Compare(const nsCString& aFontName,
|
2007-01-20 00:23:38 +03:00
|
|
|
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))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-01-20 00:23:38 +03:00
|
|
|
|
2007-03-20 06:46:17 +03:00
|
|
|
if (num_glyphs != int(glyphs.data.Length()))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-01-20 00:23:38 +03:00
|
|
|
|
|
|
|
for (int j = 0; j < num_glyphs; j++) {
|
|
|
|
if (glyphs.data[j] != aGlyphs[j].index)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-01-20 00:23:38 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2007-01-20 00:23:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCString fontName;
|
|
|
|
LiteralArray glyphs;
|
|
|
|
};
|
2007-03-20 06:46:17 +03:00
|
|
|
|
|
|
|
void SetRTL()
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
isRTL = true;
|
2007-03-20 06:46:17 +03:00
|
|
|
}
|
2007-01-20 00:23:38 +03:00
|
|
|
|
|
|
|
// empty/NULL 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;
|
2008-01-23 11:27:47 +03:00
|
|
|
#elif defined(XP_UNIX)
|
2007-03-20 06:46:17 +03:00
|
|
|
if (strcmp(platform, "gtk2-pango"))
|
|
|
|
return;
|
2007-01-20 00:23:38 +03:00
|
|
|
#else
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
expectItems.AppendElement(ExpectItem(fontName, glyphs));
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Check (gfxFontTestStore *store) {
|
2007-01-20 00:23:38 +03:00
|
|
|
if (expectItems.Length() == 0 ||
|
|
|
|
store->items.Length() != expectItems.Length())
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-01-20 00:23:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < expectItems.Length(); i++) {
|
|
|
|
if (!expectItems[i].Compare(store->items[i].platformFont,
|
|
|
|
store->items[i].glyphs,
|
|
|
|
store->items[i].num_glyphs))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-01-20 00:23:38 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2007-01-20 00:23:38 +03:00
|
|
|
}
|
|
|
|
|
2009-08-18 19:48:32 +04:00
|
|
|
const char *utf8FamilyString;
|
2007-01-20 00:23:38 +03:00
|
|
|
gfxFontStyle fontStyle;
|
|
|
|
|
|
|
|
int stringType;
|
2009-08-18 19:48:32 +04:00
|
|
|
const char *string;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isRTL;
|
2007-01-20 00:23:38 +03:00
|
|
|
|
|
|
|
nsTArray<ExpectItem> expectItems;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsTArray<TestEntry> testList;
|
|
|
|
|
|
|
|
already_AddRefed<gfxContext>
|
|
|
|
MakeContext ()
|
|
|
|
{
|
|
|
|
const int size = 200;
|
|
|
|
|
|
|
|
nsRefPtr<gfxASurface> surface;
|
|
|
|
|
2010-09-17 01:34:53 +04:00
|
|
|
surface = gfxPlatform::GetPlatform()->
|
|
|
|
CreateOffscreenSurface(gfxIntSize(size, size),
|
|
|
|
gfxASurface::ContentFromFormat(gfxASurface::ImageFormatRGB24));
|
2007-01-20 00:23:38 +03:00
|
|
|
gfxContext *ctx = new gfxContext(surface);
|
|
|
|
NS_IF_ADDREF(ctx);
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestEntry*
|
2009-08-18 19:48:32 +04:00
|
|
|
AddTest (const char *utf8FamilyString,
|
2007-01-20 00:23:38 +03:00
|
|
|
const gfxFontStyle& fontStyle,
|
|
|
|
int stringType,
|
2009-08-18 19:48:32 +04:00
|
|
|
const char *string)
|
2007-01-20 00:23:38 +03:00
|
|
|
{
|
|
|
|
TestEntry te (utf8FamilyString,
|
|
|
|
fontStyle,
|
|
|
|
stringType,
|
|
|
|
string);
|
|
|
|
|
|
|
|
testList.AppendElement(te);
|
|
|
|
|
|
|
|
return &(testList[testList.Length()-1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetupTests();
|
|
|
|
|
|
|
|
void
|
|
|
|
DumpStore (gfxFontTestStore *store) {
|
|
|
|
if (store->items.Length() == 0) {
|
|
|
|
printf ("(empty)\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (PRUint32 i = 0;
|
|
|
|
i < store->items.Length();
|
|
|
|
i++)
|
|
|
|
{
|
|
|
|
printf ("Run[% 2d]: '%s' ", i, nsPromiseFlatCString(store->items[i].platformFont).get());
|
|
|
|
|
|
|
|
for (int j = 0; j < store->items[i].num_glyphs; j++)
|
2007-03-20 06:46:17 +03:00
|
|
|
printf ("%d ", int(store->items[i].glyphs[j].index));
|
2007-01-20 00:23:38 +03:00
|
|
|
|
|
|
|
printf ("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DumpTestExpect (TestEntry *test) {
|
|
|
|
for (PRUint32 i = 0; i < test->expectItems.Length(); i++) {
|
|
|
|
printf ("Run[% 2d]: '%s' ", i, nsPromiseFlatCString(test->expectItems[i].fontName).get());
|
|
|
|
for (PRUint32 j = 0; j < test->expectItems[i].glyphs.data.Length(); j++)
|
2007-03-20 06:46:17 +03:00
|
|
|
printf ("%d ", int(test->expectItems[i].glyphs.data[j]));
|
2007-01-20 00:23:38 +03:00
|
|
|
|
|
|
|
printf ("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2007-01-20 00:23:38 +03:00
|
|
|
RunTest (TestEntry *test, gfxContext *ctx) {
|
|
|
|
nsRefPtr<gfxFontGroup> fontGroup;
|
|
|
|
|
2008-10-01 07:01:53 +04:00
|
|
|
fontGroup = gfxPlatform::GetPlatform()->CreateFontGroup(NS_ConvertUTF8toUTF16(test->utf8FamilyString), &test->fontStyle, nsnull);
|
2007-01-20 00:23:38 +03:00
|
|
|
|
2007-03-20 06:46:17 +03:00
|
|
|
nsAutoPtr<gfxTextRun> textRun;
|
|
|
|
gfxTextRunFactory::Parameters params = {
|
2007-05-09 02:46:14 +04:00
|
|
|
ctx, nsnull, nsnull, nsnull, 0, 60
|
2007-03-20 06:46:17 +03:00
|
|
|
};
|
2007-05-09 02:46:14 +04:00
|
|
|
PRUint32 flags = gfxTextRunFactory::TEXT_IS_PERSISTENT;
|
2007-03-20 06:46:17 +03:00
|
|
|
if (test->isRTL) {
|
2007-05-09 02:46:14 +04:00
|
|
|
flags |= gfxTextRunFactory::TEXT_IS_RTL;
|
2007-03-20 06:46:17 +03:00
|
|
|
}
|
|
|
|
PRUint32 length;
|
2007-01-20 00:23:38 +03:00
|
|
|
if (test->stringType == S_ASCII) {
|
2007-05-09 02:46:14 +04:00
|
|
|
flags |= gfxTextRunFactory::TEXT_IS_ASCII | gfxTextRunFactory::TEXT_IS_8BIT;
|
2007-03-20 06:46:17 +03:00
|
|
|
length = strlen(test->string);
|
2011-12-06 16:39:18 +04:00
|
|
|
textRun = fontGroup->MakeTextRun(reinterpret_cast<const PRUint8*>(test->string), length, ¶ms, flags);
|
2007-01-20 00:23:38 +03:00
|
|
|
} else {
|
2007-03-20 06:46:17 +03:00
|
|
|
NS_ConvertUTF8toUTF16 str(nsDependentCString(test->string));
|
|
|
|
length = str.Length();
|
2011-12-06 16:39:18 +04:00
|
|
|
textRun = fontGroup->MakeTextRun(str.get(), length, ¶ms, flags);
|
2007-01-20 00:23:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxFontTestStore::NewStore();
|
2011-01-11 14:17:00 +03:00
|
|
|
textRun->Draw(ctx, gfxPoint(0,0), 0, length, nsnull, nsnull);
|
2007-01-20 00:23:38 +03:00
|
|
|
gfxFontTestStore *s = gfxFontTestStore::CurrentStore();
|
|
|
|
|
2007-06-20 23:49:33 +04:00
|
|
|
gTextRunCache->RemoveTextRun(textRun);
|
|
|
|
|
2007-01-20 00:23:38 +03:00
|
|
|
if (!test->Check(s)) {
|
|
|
|
DumpStore(s);
|
|
|
|
printf (" expected:\n");
|
|
|
|
DumpTestExpect(test);
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-01-20 00:23:38 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2007-01-20 00:23:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv) {
|
|
|
|
int passed = 0;
|
|
|
|
int failed = 0;
|
|
|
|
|
2012-06-28 04:15:32 +04:00
|
|
|
#ifdef MOZ_WIDGET_GTK
|
2007-03-20 06:46:17 +03:00
|
|
|
gtk_init(&argc, &argv);
|
|
|
|
#endif
|
2007-03-30 01:48:46 +04:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
CocoaPoolInit();
|
|
|
|
#endif
|
2007-03-20 06:46:17 +03:00
|
|
|
|
2007-01-20 00:23:38 +03:00
|
|
|
// Initialize XPCOM
|
|
|
|
nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return -1;
|
|
|
|
|
2012-03-22 03:14:09 +04:00
|
|
|
if (!gfxPlatform::GetPlatform())
|
2007-03-30 01:48:46 +04:00
|
|
|
return -1;
|
|
|
|
|
2007-01-20 00:23:38 +03:00
|
|
|
// let's get all the xpcom goop out of the system
|
|
|
|
fflush (stderr);
|
|
|
|
fflush (stdout);
|
|
|
|
|
|
|
|
// don't need to query, we might need to set up some prefs later
|
|
|
|
if (0) {
|
|
|
|
nsresult rv;
|
|
|
|
|
2011-06-12 06:30:16 +04:00
|
|
|
nsAdoptingCString str = Preferences::GetCString("font.name.sans-serif.x-western");
|
2007-01-20 00:23:38 +03:00
|
|
|
printf ("sans-serif.x-western: %s\n", nsPromiseFlatCString(str).get());
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up the tests
|
|
|
|
SetupTests();
|
|
|
|
|
|
|
|
nsRefPtr<gfxContext> context = MakeContext();
|
|
|
|
|
|
|
|
for (uint test = 0;
|
|
|
|
test < testList.Length();
|
|
|
|
test++)
|
|
|
|
{
|
|
|
|
printf ("==== Test %d\n", test);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool result = RunTest (&testList[test], context);
|
2007-01-20 00:23:38 +03:00
|
|
|
if (result) {
|
2007-06-11 05:31:27 +04:00
|
|
|
printf ("Test %d succeeded\n", test);
|
2007-01-20 00:23:38 +03:00
|
|
|
passed++;
|
|
|
|
} else {
|
|
|
|
printf ("Test %d failed\n", test);
|
|
|
|
failed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("PASSED: %d FAILED: %d\n", passed, failed);
|
|
|
|
fflush (stderr);
|
|
|
|
fflush (stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The tests themselves
|
|
|
|
|
|
|
|
#include "gfxFontSelectionTests.h"
|