|
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "gtest/gtest.h" |
|
7 |
|
8 #include "mozilla/ArrayUtils.h" |
|
9 |
|
10 #include "nsCOMPtr.h" |
|
11 #include "nsTArray.h" |
|
12 #include "nsString.h" |
|
13 #include "nsDependentString.h" |
|
14 |
|
15 #include "prinrval.h" |
|
16 |
|
17 #include "gfxContext.h" |
|
18 #include "gfxFont.h" |
|
19 #include "gfxPlatform.h" |
|
20 |
|
21 #include "gfxFontTest.h" |
|
22 |
|
23 using namespace mozilla; |
|
24 |
|
25 struct TestEntry { |
|
26 const char* mFamilies; |
|
27 const char* mString; |
|
28 }; |
|
29 |
|
30 TestEntry testList[] = { |
|
31 #include "per-word-runs.h" |
|
32 { nullptr, nullptr } // terminator |
|
33 }; |
|
34 |
|
35 static already_AddRefed<gfxContext> |
|
36 MakeContext () |
|
37 { |
|
38 const int size = 200; |
|
39 |
|
40 nsRefPtr<gfxASurface> surface; |
|
41 |
|
42 surface = gfxPlatform::GetPlatform()-> |
|
43 CreateOffscreenSurface(IntSize(size, size), |
|
44 gfxASurface::ContentFromFormat(gfxImageFormat::RGB24)); |
|
45 nsRefPtr<gfxContext> ctx = new gfxContext(surface); |
|
46 return ctx.forget(); |
|
47 } |
|
48 |
|
49 const char* lastFamilies = nullptr; |
|
50 |
|
51 static void |
|
52 RunTest (TestEntry *test, gfxContext *ctx) { |
|
53 nsRefPtr<gfxFontGroup> fontGroup; |
|
54 if (!lastFamilies || strcmp(lastFamilies, test->mFamilies)) { |
|
55 gfxFontStyle style_western_normal_16 (mozilla::gfx::FontStyle::NORMAL, |
|
56 400, |
|
57 0, |
|
58 16.0, |
|
59 NS_NewPermanentAtom(NS_LITERAL_STRING("en")), |
|
60 0.0, |
|
61 false, false, |
|
62 NS_LITERAL_STRING("")); |
|
63 |
|
64 fontGroup = gfxPlatform::GetPlatform()->CreateFontGroup(NS_ConvertUTF8toUTF16(test->mFamilies), &style_western_normal_16, nullptr); |
|
65 } |
|
66 |
|
67 nsAutoPtr<gfxTextRun> textRun; |
|
68 uint32_t i; |
|
69 bool isASCII = true; |
|
70 for (i = 0; test->mString[i]; ++i) { |
|
71 if (test->mString[i] & 0x80) { |
|
72 isASCII = false; |
|
73 } |
|
74 } |
|
75 gfxTextRunFactory::Parameters params = { |
|
76 ctx, nullptr, nullptr, nullptr, 0, 60 |
|
77 }; |
|
78 uint32_t flags = gfxTextRunFactory::TEXT_IS_PERSISTENT; |
|
79 uint32_t length; |
|
80 gfxFontTestStore::NewStore(); |
|
81 if (isASCII) { |
|
82 flags |= gfxTextRunFactory::TEXT_IS_ASCII | |
|
83 gfxTextRunFactory::TEXT_IS_8BIT; |
|
84 length = strlen(test->mString); |
|
85 textRun = fontGroup->MakeTextRun(reinterpret_cast<const uint8_t*>(test->mString), length, ¶ms, flags); |
|
86 } else { |
|
87 NS_ConvertUTF8toUTF16 str(nsDependentCString(test->mString)); |
|
88 length = str.Length(); |
|
89 textRun = fontGroup->MakeTextRun(str.get(), length, ¶ms, flags); |
|
90 } |
|
91 |
|
92 // Should we test drawing? |
|
93 // textRun->Draw(ctx, gfxPoint(0,0), 0, length, nullptr, nullptr, nullptr); |
|
94 |
|
95 textRun->GetAdvanceWidth(0, length, nullptr); |
|
96 gfxFontTestStore::DeleteStore(); |
|
97 } |
|
98 |
|
99 uint32_t iterations = 1; |
|
100 |
|
101 TEST(Gfx, TextRunPref) { |
|
102 nsRefPtr<gfxContext> context = MakeContext(); |
|
103 |
|
104 // Start timing |
|
105 PRIntervalTime start = PR_IntervalNow(); |
|
106 |
|
107 for (uint32_t i = 0; i < iterations; ++i) { |
|
108 for (uint test = 0; |
|
109 test < ArrayLength(testList) - 1; |
|
110 test++) |
|
111 { |
|
112 RunTest(&testList[test], context); |
|
113 } |
|
114 } |
|
115 |
|
116 PRIntervalTime end = PR_IntervalNow(); |
|
117 |
|
118 printf("Elapsed time (ms): %d\n", PR_IntervalToMilliseconds(end - start)); |
|
119 |
|
120 } |