1/*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26// Tests for the Font class.
27
28#include "config.h"
29
30#include "platform/fonts/Character.h"
31#include "platform/fonts/Font.h"
32
33#include <gtest/gtest.h>
34
35namespace blink {
36
37static void TestSpecificUCharRange(UChar rangeStart, UChar rangeEnd)
38{
39    UChar below[1];
40    UChar start[1];
41    UChar midway[1];
42    UChar end[1];
43    UChar above[1];
44
45    below[0] = rangeStart - 1;
46    start[0] = rangeStart;
47    midway[0] = ((int)rangeStart + (int)rangeEnd) / 2;
48    end[0] = rangeEnd;
49    above[0] = rangeEnd + 1;
50
51    EXPECT_EQ(SimplePath, Character::characterRangeCodePath(below, 1));
52    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(start, 1));
53    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(midway, 1));
54    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(end, 1));
55    EXPECT_EQ(SimplePath, Character::characterRangeCodePath(above, 1));
56}
57
58TEST(FontTest, TestCharacterRangeCodePath)
59{
60    static UChar c1[] = { 0x0 };
61    EXPECT_EQ(SimplePath, Character::characterRangeCodePath(c1, 1));
62
63    TestSpecificUCharRange(0x2E5, 0x2E9);
64    TestSpecificUCharRange(0x300, 0x36F);
65    TestSpecificUCharRange(0x0591, 0x05BD);
66    TestSpecificUCharRange(0x05BF, 0x05CF);
67    TestSpecificUCharRange(0x0600, 0x109F);
68    TestSpecificUCharRange(0x1100, 0x11FF);
69    TestSpecificUCharRange(0x135D, 0x135F);
70    TestSpecificUCharRange(0x1700, 0x18AF);
71    TestSpecificUCharRange(0x1900, 0x194F);
72    TestSpecificUCharRange(0x1980, 0x19DF);
73    TestSpecificUCharRange(0x1A00, 0x1CFF);
74
75    static UChar c2[] = { 0x1DBF };
76    EXPECT_EQ(SimplePath, Character::characterRangeCodePath(c2, 1));
77    static UChar c3[] = { 0x1DC0 };
78    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(c3, 1));
79    static UChar c4[] = { 0x1DD0 };
80    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(c4, 1));
81    static UChar c5[] = { 0x1DFF };
82    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(c5, 1));
83    static UChar c6[] = { 0x1E00 };
84    EXPECT_EQ(SimpleWithGlyphOverflowPath, Character::characterRangeCodePath(c6, 1));
85    static UChar c7[] = { 0x2000 };
86    EXPECT_EQ(SimpleWithGlyphOverflowPath, Character::characterRangeCodePath(c7, 1));
87    static UChar c8[] = { 0x2001 };
88    EXPECT_EQ(SimplePath, Character::characterRangeCodePath(c8, 1));
89
90    TestSpecificUCharRange(0x20D0, 0x20FF);
91    TestSpecificUCharRange(0x2CEF, 0x2CF1);
92    TestSpecificUCharRange(0x302A, 0x302F);
93
94    TestSpecificUCharRange(0xA67C, 0xA67D);
95    TestSpecificUCharRange(0xA6F0, 0xA6F1);
96    TestSpecificUCharRange(0xA800, 0xABFF);
97
98    TestSpecificUCharRange(0xD7B0, 0xD7FF);
99    TestSpecificUCharRange(0xFE00, 0xFE0F);
100    TestSpecificUCharRange(0xFE20, 0xFE2F);
101}
102
103TEST(FontTest, TestCharacterRangeCodePathSurrogate1)
104{
105    /* To be surrogate ... */
106    /* 1st character must be 0xD800 .. 0xDBFF */
107    /* 2nd character must be 0xdc00 .. 0xdfff */
108
109    /* The following 5 should all be Simple because they are not surrogate. */
110    static UChar c1[] = { 0xD800, 0xDBFE };
111    EXPECT_EQ(SimplePath, Character::characterRangeCodePath(c1, 2));
112    static UChar c2[] = { 0xD800, 0xE000 };
113    EXPECT_EQ(SimplePath, Character::characterRangeCodePath(c2, 2));
114    static UChar c3[] = { 0xDBFF, 0xDBFE };
115    EXPECT_EQ(SimplePath, Character::characterRangeCodePath(c3, 2));
116    static UChar c4[] = { 0xDBFF, 0xE000 };
117    EXPECT_EQ(SimplePath, Character::characterRangeCodePath(c4, 2));
118    static UChar c5[] = { 0xDC00, 0xDBFF };
119    EXPECT_EQ(SimplePath, Character::characterRangeCodePath(c5, 2));
120
121    /* To be Complex, the Supplementary Character must be in either */
122    /* U+1F1E6 through U+1F1FF or U+E0100 through U+E01EF. */
123    /* That is, a lead of 0xD83C with trail 0xDDE6 .. 0xDDFF or */
124    /* a lead of 0xDB40 with trail 0xDD00 .. 0xDDEF. */
125    static UChar c6[] = { 0xD83C, 0xDDE5 };
126    EXPECT_EQ(SimplePath, Character::characterRangeCodePath(c6, 2));
127    static UChar c7[] = { 0xD83C, 0xDDE6 };
128    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(c7, 2));
129    static UChar c8[] = { 0xD83C, 0xDDF0 };
130    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(c8, 2));
131    static UChar c9[] = { 0xD83C, 0xDDFF };
132    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(c9, 2));
133    static UChar c10[] = { 0xD83C, 0xDE00 };
134    EXPECT_EQ(SimplePath, Character::characterRangeCodePath(c10, 2));
135
136    static UChar c11[] = { 0xDB40, 0xDCFF };
137    EXPECT_EQ(SimplePath, Character::characterRangeCodePath(c11, 2));
138    static UChar c12[] = { 0xDB40, 0xDD00 };
139    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(c12, 2));
140    static UChar c13[] = { 0xDB40, 0xDDED };
141    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(c13, 2));
142    static UChar c14[] = { 0xDB40, 0xDDEF };
143    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(c14, 2));
144    static UChar c15[] = { 0xDB40, 0xDDF0 };
145    EXPECT_EQ(SimplePath, Character::characterRangeCodePath(c15, 2));
146}
147
148TEST(FontTest, TestCharacterRangeCodePathString)
149{
150    // Simple-Simple is still simple
151    static UChar c1[] = { 0x2FF, 0x2FF };
152    EXPECT_EQ(SimplePath, Character::characterRangeCodePath(c1, 2));
153    // Complex-Simple is Complex
154    static UChar c2[] = { 0x300, 0x2FF };
155    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(c2, 2));
156    // Simple-Complex is Complex
157    static UChar c3[] = { 0x2FF, 0x330 };
158    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(c3, 2));
159    // Complex-Complex is Complex
160    static UChar c4[] = { 0x36F, 0x330 };
161    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(c4, 2));
162    // SimpleWithGlyphOverflow-Simple is SimpleWithGlyphOverflow
163    static UChar c5[] = { 0x1E00, 0x2FF };
164    EXPECT_EQ(SimpleWithGlyphOverflowPath, Character::characterRangeCodePath(c5, 2));
165    // Simple-SimpleWithGlyphOverflow is SimpleWithGlyphOverflow
166    static UChar c6[] = { 0x2FF, 0x2000 };
167    EXPECT_EQ(SimpleWithGlyphOverflowPath, Character::characterRangeCodePath(c6, 2));
168    // SimpleWithGlyphOverflow-Complex is Complex
169    static UChar c7[] = { 0x1E00, 0x330 };
170    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(c7, 2));
171    // Complex-SimpleWithGlyphOverflow is Complex
172    static UChar c8[] = { 0x330, 0x2000 };
173    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(c8, 2));
174    // Surrogate-Complex is Complex
175    static UChar c9[] = { 0xD83C, 0xDDE5, 0x330 };
176    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(c9, 3));
177    // Complex-Surrogate is Complex
178    static UChar c10[] = { 0x330, 0xD83C, 0xDDE5 };
179    EXPECT_EQ(ComplexPath, Character::characterRangeCodePath(c10, 3));
180}
181
182static void TestSpecificUChar32RangeIdeograph(UChar32 rangeStart, UChar32 rangeEnd)
183{
184    EXPECT_FALSE(Character::isCJKIdeograph(rangeStart - 1));
185    EXPECT_TRUE(Character::isCJKIdeograph(rangeStart));
186    EXPECT_TRUE(Character::isCJKIdeograph((UChar32)((uint64_t)rangeStart + (uint64_t)rangeEnd) / 2));
187    EXPECT_TRUE(Character::isCJKIdeograph(rangeEnd));
188    EXPECT_FALSE(Character::isCJKIdeograph(rangeEnd + 1));
189}
190
191TEST(FontTest, TestIsCJKIdeograph)
192{
193    // The basic CJK Unified Ideographs block.
194    TestSpecificUChar32RangeIdeograph(0x4E00, 0x9FFF);
195    // CJK Unified Ideographs Extension A.
196    TestSpecificUChar32RangeIdeograph(0x3400, 0x4DBF);
197    // CJK Unified Ideographs Extension A and Kangxi Radicals.
198    TestSpecificUChar32RangeIdeograph(0x2E80, 0x2FDF);
199    // CJK Strokes.
200    TestSpecificUChar32RangeIdeograph(0x31C0, 0x31EF);
201    // CJK Compatibility Ideographs.
202    TestSpecificUChar32RangeIdeograph(0xF900, 0xFAFF);
203    // CJK Unified Ideographs Extension B.
204    TestSpecificUChar32RangeIdeograph(0x20000, 0x2A6DF);
205    // CJK Unified Ideographs Extension C.
206    // CJK Unified Ideographs Extension D.
207    TestSpecificUChar32RangeIdeograph(0x2A700, 0x2B81F);
208    // CJK Compatibility Ideographs Supplement.
209    TestSpecificUChar32RangeIdeograph(0x2F800, 0x2FA1F);
210}
211
212static void TestSpecificUChar32RangeIdeographSymbol(UChar32 rangeStart, UChar32 rangeEnd)
213{
214    EXPECT_FALSE(Character::isCJKIdeographOrSymbol(rangeStart - 1));
215    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(rangeStart));
216    EXPECT_TRUE(Character::isCJKIdeographOrSymbol((UChar32)((uint64_t)rangeStart + (uint64_t)rangeEnd) / 2));
217    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(rangeEnd));
218    EXPECT_FALSE(Character::isCJKIdeographOrSymbol(rangeEnd + 1));
219}
220
221TEST(FontTest, TestIsCJKIdeographOrSymbol)
222{
223    // CJK Compatibility Ideographs Supplement.
224    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2C7));
225    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2CA));
226    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2CB));
227    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2D9));
228
229    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2020));
230    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2021));
231    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2030));
232    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x203B));
233    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x203C));
234    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2042));
235    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2047));
236    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2048));
237    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2049));
238    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2051));
239    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x20DD));
240    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x20DE));
241    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2100));
242    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2103));
243    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2105));
244    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2109));
245    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x210A));
246    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2113));
247    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2116));
248    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2121));
249    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x212B));
250    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x213B));
251    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2150));
252    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2151));
253    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2152));
254
255    TestSpecificUChar32RangeIdeographSymbol(0x2156, 0x215A);
256    TestSpecificUChar32RangeIdeographSymbol(0x2160, 0x216B);
257    TestSpecificUChar32RangeIdeographSymbol(0x2170, 0x217B);
258
259    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x217F));
260    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2189));
261    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2307));
262    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2312));
263
264    EXPECT_FALSE(Character::isCJKIdeographOrSymbol(0x23BD));
265    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x23BE));
266    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x23C4));
267    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x23CC));
268    EXPECT_FALSE(Character::isCJKIdeographOrSymbol(0x23CD));
269    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x23CE));
270    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2423));
271
272    TestSpecificUChar32RangeIdeographSymbol(0x2460, 0x2492);
273    TestSpecificUChar32RangeIdeographSymbol(0x249C, 0x24FF);
274
275    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25A0));
276    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25A1));
277    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25A2));
278    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25AA));
279    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25AB));
280    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25B1));
281    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25B2));
282    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25B3));
283    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25B6));
284    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25B7));
285    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25BC));
286    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25BD));
287    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25C0));
288    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25C1));
289    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25C6));
290    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25C7));
291    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25C9));
292    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25CB));
293    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25CC));
294
295    TestSpecificUChar32RangeIdeographSymbol(0x25CE, 0x25D3);
296    TestSpecificUChar32RangeIdeographSymbol(0x25E2, 0x25E6);
297
298    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x25EF));
299
300    TestSpecificUChar32RangeIdeographSymbol(0x2600, 0x2603);
301
302    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2605));
303    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2606));
304    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x260E));
305    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2616));
306    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2617));
307    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2640));
308    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2642));
309
310    TestSpecificUChar32RangeIdeographSymbol(0x2660, 0x266F);
311    TestSpecificUChar32RangeIdeographSymbol(0x2672, 0x267D);
312
313    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x26A0));
314    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x26BD));
315    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x26BE));
316    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2713));
317    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x271A));
318    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x273F));
319    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2740));
320    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2756));
321
322    TestSpecificUChar32RangeIdeographSymbol(0x2776, 0x277F);
323
324    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x2B1A));
325
326    TestSpecificUChar32RangeIdeographSymbol(0x2FF0, 0x302F);
327    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x3031));
328    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x312F));
329    EXPECT_FALSE(Character::isCJKIdeographOrSymbol(0x3130));
330
331    EXPECT_FALSE(Character::isCJKIdeographOrSymbol(0x318F));
332    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x3190));
333    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x319F));
334    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x31BF));
335
336    EXPECT_FALSE(Character::isCJKIdeographOrSymbol(0x31FF));
337    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x3200));
338    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x3300));
339    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x33FF));
340
341    TestSpecificUChar32RangeIdeographSymbol(0xF860, 0xF862);
342    TestSpecificUChar32RangeIdeographSymbol(0xFE30, 0xFE4F);
343
344    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0xFE10));
345    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0xFE11));
346    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0xFE12));
347    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0xFE19));
348
349    EXPECT_FALSE(Character::isCJKIdeographOrSymbol(0xFF0D));
350    EXPECT_FALSE(Character::isCJKIdeographOrSymbol(0xFF1B));
351    EXPECT_FALSE(Character::isCJKIdeographOrSymbol(0xFF1C));
352    EXPECT_FALSE(Character::isCJKIdeographOrSymbol(0xFF1E));
353
354    TestSpecificUChar32RangeIdeographSymbol(0xFF00, 0xFFEF);
355
356    EXPECT_TRUE(Character::isCJKIdeographOrSymbol(0x1F100));
357
358    TestSpecificUChar32RangeIdeographSymbol(0x1F110, 0x1F129);
359    TestSpecificUChar32RangeIdeographSymbol(0x1F130, 0x1F149);
360    TestSpecificUChar32RangeIdeographSymbol(0x1F150, 0x1F169);
361    TestSpecificUChar32RangeIdeographSymbol(0x1F170, 0x1F189);
362    TestSpecificUChar32RangeIdeographSymbol(0x1F200, 0x1F6FF);
363}
364
365} // namespace blink
366
367