FontCollectionItemizeTest.cpp 60.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>

19
#include "FontLanguageListCache.h"
20
#include "FontLanguage.h"
21
#include "FontTestUtils.h"
22
#include "ICUTestBase.h"
23
#include "MinikinFontForTest.h"
24
#include "MinikinInternal.h"
25
#include "UnicodeUtils.h"
26
#include "minikin/FontFamily.h"
27

S
Seigo Nonaka 已提交
28
namespace minikin {
29

30
const char kItemizeFontXml[] = kTestFontDir "itemize.xml";
31 32 33 34 35 36 37 38 39
const char kEmojiFont[] = kTestFontDir "Emoji.ttf";
const char kJAFont[] = kTestFontDir "Ja.ttf";
const char kKOFont[] = kTestFontDir "Ko.ttf";
const char kLatinBoldFont[] = kTestFontDir "Bold.ttf";
const char kLatinBoldItalicFont[] = kTestFontDir "BoldItalic.ttf";
const char kLatinFont[] = kTestFontDir "Regular.ttf";
const char kLatinItalicFont[] = kTestFontDir "Italic.ttf";
const char kZH_HansFont[] = kTestFontDir "ZhHans.ttf";
const char kZH_HantFont[] = kTestFontDir "ZhHant.ttf";
40

41 42 43 44 45 46
const char kEmojiXmlFile[] = kTestFontDir "emoji.xml";
const char kNoGlyphFont[] =  kTestFontDir "NoGlyphFont.ttf";
const char kColorEmojiFont[] = kTestFontDir "ColorEmojiFont.ttf";
const char kTextEmojiFont[] = kTestFontDir "TextEmojiFont.ttf";
const char kMixedEmojiFont[] = kTestFontDir "ColorTextMixedEmojiFont.ttf";

47 48
typedef ICUTestBase FontCollectionItemizeTest;

49 50 51 52 53 54 55 56 57
// Utility function for calling itemize function.
void itemize(FontCollection* collection, const char* str, FontStyle style,
        std::vector<FontCollection::Run>* result) {
    const size_t BUF_SIZE = 256;
    uint16_t buf[BUF_SIZE];
    size_t len;

    result->clear();
    ParseUnicode(buf, BUF_SIZE, str, &len, NULL);
S
Seigo Nonaka 已提交
58
    android::AutoMutex _l(gMinikinLock);
59 60 61 62 63
    collection->itemize(buf, len, style, result);
}

// Utility function to obtain font path associated with run.
const std::string& getFontPath(const FontCollection::Run& run) {
64
    EXPECT_NE(nullptr, run.fakedFont.font);
65 66 67
    return ((MinikinFontForTest*)run.fakedFont.font)->fontPath();
}

68 69
// Utility function to obtain FontLanguages from string.
const FontLanguages& registerAndGetFontLanguages(const std::string& lang_string) {
S
Seigo Nonaka 已提交
70
    android::AutoMutex _l(gMinikinLock);
71 72 73
    return FontLanguageListCache::getById(FontLanguageListCache::getId(lang_string));
}

74
TEST_F(FontCollectionItemizeTest, itemize_latin) {
75
    MinikinAutoUnref<FontCollection> collection(getFontCollection(kTestFontDir, kItemizeFontXml));
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
    std::vector<FontCollection::Run> runs;

    const FontStyle kRegularStyle = FontStyle();
    const FontStyle kItalicStyle = FontStyle(4, true);
    const FontStyle kBoldStyle = FontStyle(7, false);
    const FontStyle kBoldItalicStyle = FontStyle(7, true);

    itemize(collection.get(), "'a' 'b' 'c' 'd' 'e'", kRegularStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(5, runs[0].end);
    EXPECT_EQ(kLatinFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());

    itemize(collection.get(), "'a' 'b' 'c' 'd' 'e'", kItalicStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(5, runs[0].end);
    EXPECT_EQ(kLatinItalicFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());

    itemize(collection.get(), "'a' 'b' 'c' 'd' 'e'", kBoldStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(5, runs[0].end);
    EXPECT_EQ(kLatinBoldFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());

    itemize(collection.get(), "'a' 'b' 'c' 'd' 'e'", kBoldItalicStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(5, runs[0].end);
    EXPECT_EQ(kLatinBoldItalicFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());

    // Continue if the specific characters (e.g. hyphen, comma, etc.) is
    // followed.
    itemize(collection.get(), "'a' ',' '-' 'd' '!'", kRegularStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(5, runs[0].end);
    EXPECT_EQ(kLatinFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());

    itemize(collection.get(), "'a' ',' '-' 'd' '!'", kRegularStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(5, runs[0].end);
    EXPECT_EQ(kLatinFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());

    // U+0301(COMBINING ACUTE ACCENT) must be in the same run with preceding
    // chars if the font supports it.
    itemize(collection.get(), "'a' U+0301", kRegularStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kLatinFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());
}

144
TEST_F(FontCollectionItemizeTest, itemize_emoji) {
145
    MinikinAutoUnref<FontCollection> collection(getFontCollection(kTestFontDir, kItemizeFontXml));
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    std::vector<FontCollection::Run> runs;

    itemize(collection.get(), "U+1F469 U+1F467", FontStyle(), &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(4, runs[0].end);
    EXPECT_EQ(kEmojiFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());

    // U+20E3(COMBINING ENCLOSING KEYCAP) must be in the same run with preceding
    // character if the font supports.
    itemize(collection.get(), "'0' U+20E3", FontStyle(), &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kEmojiFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());

166 167 168 169 170 171 172 173 174 175 176 177
    itemize(collection.get(), "U+1F470 U+20E3", FontStyle(), &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(3, runs[0].end);
    EXPECT_EQ(kEmojiFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());

    itemize(collection.get(), "U+242EE U+1F470 U+20E3", FontStyle(), &runs);
    ASSERT_EQ(2U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
178
    EXPECT_EQ(kJAFont, getFontPath(runs[0]));
179 180 181 182 183 184 185 186 187
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());

    EXPECT_EQ(2, runs[1].start);
    EXPECT_EQ(5, runs[1].end);
    EXPECT_EQ(kEmojiFont, getFontPath(runs[1]));
    EXPECT_FALSE(runs[1].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[1].fakedFont.fakery.isFakeItalic());

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    // Currently there is no fonts which has a glyph for 'a' + U+20E3, so they
    // are splitted into two.
    itemize(collection.get(), "'a' U+20E3", FontStyle(), &runs);
    ASSERT_EQ(2U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(1, runs[0].end);
    EXPECT_EQ(kLatinFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());

    EXPECT_EQ(1, runs[1].start);
    EXPECT_EQ(2, runs[1].end);
    EXPECT_EQ(kEmojiFont, getFontPath(runs[1]));
    EXPECT_FALSE(runs[1].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[1].fakedFont.fakery.isFakeItalic());
}

205
TEST_F(FontCollectionItemizeTest, itemize_non_latin) {
206
    MinikinAutoUnref<FontCollection> collection(getFontCollection(kTestFontDir, kItemizeFontXml));
207 208
    std::vector<FontCollection::Run> runs;

S
Seigo Nonaka 已提交
209 210 211
    FontStyle kJAStyle = FontStyle(FontStyle::registerLanguageList("ja_JP"));
    FontStyle kUSStyle = FontStyle(FontStyle::registerLanguageList("en_US"));
    FontStyle kZH_HansStyle = FontStyle(FontStyle::registerLanguageList("zh_Hans"));
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

    // All Japanese Hiragana characters.
    itemize(collection.get(), "U+3042 U+3044 U+3046 U+3048 U+304A", kUSStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(5, runs[0].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());

    // All Korean Hangul characters.
    itemize(collection.get(), "U+B300 U+D55C U+BBFC U+AD6D", kUSStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(4, runs[0].end);
    EXPECT_EQ(kKOFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());

    // All Han characters ja, zh-Hans font having.
    // Japanese font should be selected if the specified language is Japanese.
    itemize(collection.get(), "U+81ED U+82B1 U+5FCD", kJAStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(3, runs[0].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());

    // Simplified Chinese font should be selected if the specified language is Simplified
    // Chinese.
    itemize(collection.get(), "U+81ED U+82B1 U+5FCD", kZH_HansStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(3, runs[0].end);
    EXPECT_EQ(kZH_HansFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());

    // Fallbacks to other fonts if there is no glyph in the specified language's
    // font. There is no character U+4F60 in Japanese.
    itemize(collection.get(), "U+81ED U+4F60 U+5FCD", kJAStyle, &runs);
    ASSERT_EQ(3U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(1, runs[0].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());

    EXPECT_EQ(1, runs[1].start);
    EXPECT_EQ(2, runs[1].end);
    EXPECT_EQ(kZH_HansFont, getFontPath(runs[1]));
    EXPECT_FALSE(runs[1].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[1].fakedFont.fakery.isFakeItalic());

    EXPECT_EQ(2, runs[2].start);
    EXPECT_EQ(3, runs[2].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[2]));
    EXPECT_FALSE(runs[2].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[2].fakedFont.fakery.isFakeItalic());

    // Tone mark.
    itemize(collection.get(), "U+4444 U+302D", FontStyle(), &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kZH_HansFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());
281 282 283 284 285 286 287 288 289 290 291

    // Both zh-Hant and ja fonts support U+242EE, but zh-Hans doesn't.
    // Here, ja and zh-Hant font should have the same score but ja should be selected since it is
    // listed before zh-Hant.
    itemize(collection.get(), "U+242EE", kZH_HansStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());
292 293
}

294
TEST_F(FontCollectionItemizeTest, itemize_mixed) {
295
    MinikinAutoUnref<FontCollection> collection(getFontCollection(kTestFontDir, kItemizeFontXml));
296 297
    std::vector<FontCollection::Run> runs;

S
Seigo Nonaka 已提交
298
    FontStyle kUSStyle = FontStyle(FontStyle::registerLanguageList("en_US"));
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332

    itemize(collection.get(), "'a' U+4F60 'b' U+4F60 'c'", kUSStyle, &runs);
    ASSERT_EQ(5U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(1, runs[0].end);
    EXPECT_EQ(kLatinFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());

    EXPECT_EQ(1, runs[1].start);
    EXPECT_EQ(2, runs[1].end);
    EXPECT_EQ(kZH_HansFont, getFontPath(runs[1]));
    EXPECT_FALSE(runs[1].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[1].fakedFont.fakery.isFakeItalic());

    EXPECT_EQ(2, runs[2].start);
    EXPECT_EQ(3, runs[2].end);
    EXPECT_EQ(kLatinFont, getFontPath(runs[2]));
    EXPECT_FALSE(runs[2].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[2].fakedFont.fakery.isFakeItalic());

    EXPECT_EQ(3, runs[3].start);
    EXPECT_EQ(4, runs[3].end);
    EXPECT_EQ(kZH_HansFont, getFontPath(runs[3]));
    EXPECT_FALSE(runs[3].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[3].fakedFont.fakery.isFakeItalic());

    EXPECT_EQ(4, runs[4].start);
    EXPECT_EQ(5, runs[4].end);
    EXPECT_EQ(kLatinFont, getFontPath(runs[4]));
    EXPECT_FALSE(runs[4].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[4].fakedFont.fakery.isFakeItalic());
}

333
TEST_F(FontCollectionItemizeTest, itemize_variationSelector) {
334
    MinikinAutoUnref<FontCollection> collection(getFontCollection(kTestFontDir, kItemizeFontXml));
335 336 337 338 339 340
    std::vector<FontCollection::Run> runs;

    // A glyph for U+4FAE is provided by both Japanese font and Simplified
    // Chinese font. Also a glyph for U+242EE is provided by both Japanese and
    // Traditional Chinese font.  To avoid effects of device default locale,
    // explicitly specify the locale.
S
Seigo Nonaka 已提交
341 342
    FontStyle kZH_HansStyle = FontStyle(FontStyle::registerLanguageList("zh_Hans"));
    FontStyle kZH_HantStyle = FontStyle(FontStyle::registerLanguageList("zh_Hant"));
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

    // U+4FAE is available in both zh_Hans and ja font, but U+4FAE,U+FE00 is
    // only available in ja font.
    itemize(collection.get(), "U+4FAE", kZH_HansStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(1, runs[0].end);
    EXPECT_EQ(kZH_HansFont, getFontPath(runs[0]));

    itemize(collection.get(), "U+4FAE U+FE00", kZH_HansStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[0]));

    itemize(collection.get(), "U+4FAE U+4FAE U+FE00", kZH_HansStyle, &runs);
    ASSERT_EQ(2U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(1, runs[0].end);
    EXPECT_EQ(kZH_HansFont, getFontPath(runs[0]));
    EXPECT_EQ(1, runs[1].start);
    EXPECT_EQ(3, runs[1].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[1]));

    itemize(collection.get(), "U+4FAE U+4FAE U+FE00 U+4FAE", kZH_HansStyle, &runs);
    ASSERT_EQ(3U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(1, runs[0].end);
    EXPECT_EQ(kZH_HansFont, getFontPath(runs[0]));
    EXPECT_EQ(1, runs[1].start);
    EXPECT_EQ(3, runs[1].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[1]));
    EXPECT_EQ(3, runs[2].start);
    EXPECT_EQ(4, runs[2].end);
    EXPECT_EQ(kZH_HansFont, getFontPath(runs[2]));

    // Validation selector after validation selector.
    itemize(collection.get(), "U+4FAE U+FE00 U+FE00", kZH_HansStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(3, runs[0].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[1]));

    // No font supports U+242EE U+FE0E.
    itemize(collection.get(), "U+4FAE U+FE0E", kZH_HansStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kZH_HansFont, getFontPath(runs[0]));

    // Surrogate pairs handling.
    // U+242EE is available in ja font and zh_Hant font.
    // U+242EE U+FE00 is available only in ja font.
    itemize(collection.get(), "U+242EE", kZH_HantStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kZH_HantFont, getFontPath(runs[0]));

    itemize(collection.get(), "U+242EE U+FE00", kZH_HantStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(3, runs[0].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[0]));

    itemize(collection.get(), "U+242EE U+242EE U+FE00", kZH_HantStyle, &runs);
    ASSERT_EQ(2U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kZH_HantFont, getFontPath(runs[0]));
    EXPECT_EQ(2, runs[1].start);
    EXPECT_EQ(5, runs[1].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[1]));

    itemize(collection.get(), "U+242EE U+242EE U+FE00 U+242EE", kZH_HantStyle, &runs);
    ASSERT_EQ(3U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kZH_HantFont, getFontPath(runs[0]));
    EXPECT_EQ(2, runs[1].start);
    EXPECT_EQ(5, runs[1].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[1]));
    EXPECT_EQ(5, runs[2].start);
    EXPECT_EQ(7, runs[2].end);
    EXPECT_EQ(kZH_HantFont, getFontPath(runs[2]));

    // Validation selector after validation selector.
    itemize(collection.get(), "U+242EE U+FE00 U+FE00", kZH_HansStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(4, runs[0].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[0]));

    // No font supports U+242EE U+FE0E
    itemize(collection.get(), "U+242EE U+FE0E", kZH_HantStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(3, runs[0].end);
    EXPECT_EQ(kZH_HantFont, getFontPath(runs[0]));

    // Isolated variation selector supplement.
    itemize(collection.get(), "U+FE00", FontStyle(), &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(1, runs[0].end);
448
    EXPECT_TRUE(runs[0].fakedFont.font == nullptr || kLatinFont == getFontPath(runs[0]));
449 450 451 452 453

    itemize(collection.get(), "U+FE00", kZH_HantStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(1, runs[0].end);
454
    EXPECT_TRUE(runs[0].fakedFont.font == nullptr || kLatinFont == getFontPath(runs[0]));
455 456

    // First font family (Regular.ttf) supports U+203C but doesn't support U+203C U+FE0F.
457
    // Emoji.ttf font supports U+203C U+FE0F.  Emoji.ttf should be selected.
458 459 460 461 462 463 464 465 466 467 468 469
    itemize(collection.get(), "U+203C U+FE0F", kZH_HantStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kEmojiFont, getFontPath(runs[0]));

    // First font family (Regular.ttf) supports U+203C U+FE0E.
    itemize(collection.get(), "U+203C U+FE0E", kZH_HantStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kLatinFont, getFontPath(runs[0]));
470 471
}

472
TEST_F(FontCollectionItemizeTest, itemize_variationSelectorSupplement) {
473
    MinikinAutoUnref<FontCollection> collection(getFontCollection(kTestFontDir, kItemizeFontXml));
474 475 476 477 478 479
    std::vector<FontCollection::Run> runs;

    // A glyph for U+845B is provided by both Japanese font and Simplified
    // Chinese font. Also a glyph for U+242EE is provided by both Japanese and
    // Traditional Chinese font.  To avoid effects of device default locale,
    // explicitly specify the locale.
S
Seigo Nonaka 已提交
480 481
    FontStyle kZH_HansStyle = FontStyle(FontStyle::registerLanguageList("zh_Hans"));
    FontStyle kZH_HantStyle = FontStyle(FontStyle::registerLanguageList("zh_Hant"));
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587

    // U+845B is available in both zh_Hans and ja font, but U+845B,U+E0100 is
    // only available in ja font.
    itemize(collection.get(), "U+845B", kZH_HansStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(1, runs[0].end);
    EXPECT_EQ(kZH_HansFont, getFontPath(runs[0]));

    itemize(collection.get(), "U+845B U+E0100", kZH_HansStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(3, runs[0].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[0]));

    itemize(collection.get(), "U+845B U+845B U+E0100", kZH_HansStyle, &runs);
    ASSERT_EQ(2U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(1, runs[0].end);
    EXPECT_EQ(kZH_HansFont, getFontPath(runs[0]));
    EXPECT_EQ(1, runs[1].start);
    EXPECT_EQ(4, runs[1].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[1]));

    itemize(collection.get(), "U+845B U+845B U+E0100 U+845B", kZH_HansStyle, &runs);
    ASSERT_EQ(3U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(1, runs[0].end);
    EXPECT_EQ(kZH_HansFont, getFontPath(runs[0]));
    EXPECT_EQ(1, runs[1].start);
    EXPECT_EQ(4, runs[1].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[1]));
    EXPECT_EQ(4, runs[2].start);
    EXPECT_EQ(5, runs[2].end);
    EXPECT_EQ(kZH_HansFont, getFontPath(runs[2]));

    // Validation selector after validation selector.
    itemize(collection.get(), "U+845B U+E0100 U+E0100", kZH_HansStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(5, runs[0].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[0]));

    // No font supports U+845B U+E01E0.
    itemize(collection.get(), "U+845B U+E01E0", kZH_HansStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(3, runs[0].end);
    EXPECT_EQ(kZH_HansFont, getFontPath(runs[0]));

    // Isolated variation selector supplement
    // Surrogate pairs handling.
    // U+242EE is available in ja font and zh_Hant font.
    // U+242EE U+E0100 is available only in ja font.
    itemize(collection.get(), "U+242EE", kZH_HantStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kZH_HantFont, getFontPath(runs[0]));

    itemize(collection.get(), "U+242EE U+E0101", kZH_HantStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(4, runs[0].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[0]));

    itemize(collection.get(), "U+242EE U+242EE U+E0101", kZH_HantStyle, &runs);
    ASSERT_EQ(2U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kZH_HantFont, getFontPath(runs[0]));
    EXPECT_EQ(2, runs[1].start);
    EXPECT_EQ(6, runs[1].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[1]));

    itemize(collection.get(), "U+242EE U+242EE U+E0101 U+242EE", kZH_HantStyle, &runs);
    ASSERT_EQ(3U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kZH_HantFont, getFontPath(runs[0]));
    EXPECT_EQ(2, runs[1].start);
    EXPECT_EQ(6, runs[1].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[1]));
    EXPECT_EQ(6, runs[2].start);
    EXPECT_EQ(8, runs[2].end);
    EXPECT_EQ(kZH_HantFont, getFontPath(runs[2]));

    // Validation selector after validation selector.
    itemize(collection.get(), "U+242EE U+E0100 U+E0100", kZH_HantStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(6, runs[0].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[0]));

    // No font supports U+242EE U+E01E0.
    itemize(collection.get(), "U+242EE U+E01E0", kZH_HantStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(4, runs[0].end);
    EXPECT_EQ(kZH_HantFont, getFontPath(runs[0]));

    // Isolated variation selector supplement.
    itemize(collection.get(), "U+E0100", FontStyle(), &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
588
    EXPECT_TRUE(runs[0].fakedFont.font == nullptr || kLatinFont == getFontPath(runs[0]));
589 590 591 592 593

    itemize(collection.get(), "U+E0100", kZH_HantStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
594
    EXPECT_TRUE(runs[0].fakedFont.font == nullptr || kLatinFont == getFontPath(runs[0]));
595 596
}

597
TEST_F(FontCollectionItemizeTest, itemize_no_crash) {
598
    MinikinAutoUnref<FontCollection> collection(getFontCollection(kTestFontDir, kItemizeFontXml));
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
    std::vector<FontCollection::Run> runs;

    // Broken Surrogate pairs. Check only not crashing.
    itemize(collection.get(), "'a' U+D83D 'a'", FontStyle(), &runs);
    itemize(collection.get(), "'a' U+DC69 'a'", FontStyle(), &runs);
    itemize(collection.get(), "'a' U+D83D U+D83D 'a'", FontStyle(), &runs);
    itemize(collection.get(), "'a' U+DC69 U+DC69 'a'", FontStyle(), &runs);

    // Isolated variation selector. Check only not crashing.
    itemize(collection.get(), "U+FE00 U+FE00", FontStyle(), &runs);
    itemize(collection.get(), "U+E0100 U+E0100", FontStyle(), &runs);
    itemize(collection.get(), "U+FE00 U+E0100", FontStyle(), &runs);
    itemize(collection.get(), "U+E0100 U+FE00", FontStyle(), &runs);

    // Tone mark only. Check only not crashing.
    itemize(collection.get(), "U+302D", FontStyle(), &runs);
    itemize(collection.get(), "U+302D U+302D", FontStyle(), &runs);

    // Tone mark and variation selector mixed. Check only not crashing.
    itemize(collection.get(), "U+FE00 U+302D U+E0100", FontStyle(), &runs);
}

621
TEST_F(FontCollectionItemizeTest, itemize_fakery) {
622
    MinikinAutoUnref<FontCollection> collection(getFontCollection(kTestFontDir, kItemizeFontXml));
623 624
    std::vector<FontCollection::Run> runs;

S
Seigo Nonaka 已提交
625 626 627 628
    FontStyle kJABoldStyle = FontStyle(FontStyle::registerLanguageList("ja_JP"), 0, 7, false);
    FontStyle kJAItalicStyle = FontStyle(FontStyle::registerLanguageList("ja_JP"), 0, 5, true);
    FontStyle kJABoldItalicStyle =
           FontStyle(FontStyle::registerLanguageList("ja_JP"), 0, 7, true);
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660

    // Currently there is no italic or bold font for Japanese. FontFakery has
    // the differences between desired and actual font style.

    // All Japanese Hiragana characters.
    itemize(collection.get(), "U+3042 U+3044 U+3046 U+3048 U+304A", kJABoldStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(5, runs[0].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[0]));
    EXPECT_TRUE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeItalic());

    // All Japanese Hiragana characters.
    itemize(collection.get(), "U+3042 U+3044 U+3046 U+3048 U+304A", kJAItalicStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(5, runs[0].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[0]));
    EXPECT_FALSE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_TRUE(runs[0].fakedFont.fakery.isFakeItalic());

    // All Japanese Hiragana characters.
    itemize(collection.get(), "U+3042 U+3044 U+3046 U+3048 U+304A", kJABoldItalicStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(5, runs[0].end);
    EXPECT_EQ(kJAFont, getFontPath(runs[0]));
    EXPECT_TRUE(runs[0].fakedFont.fakery.isFakeBold());
    EXPECT_TRUE(runs[0].fakedFont.fakery.isFakeItalic());
}

661
TEST_F(FontCollectionItemizeTest, itemize_vs_sequence_but_no_base_char) {
662 663 664 665 666
    // kVSTestFont supports U+717D U+FE02 but doesn't support U+717D.
    // kVSTestFont should be selected for U+717D U+FE02 even if it does not support the base code
    // point.
    const std::string kVSTestFont = kTestFontDir "VarioationSelectorTest-Regular.ttf";

S
Seigo Nonaka 已提交
667 668
    std::vector<FontFamily*> families;
    FontFamily* family1 = new FontFamily(VARIANT_DEFAULT);
669
    MinikinAutoUnref<MinikinFont> font(new MinikinFontForTest(kLatinFont));
S
Seigo Nonaka 已提交
670
    family1->addFont(font.get());
671 672
    families.push_back(family1);

S
Seigo Nonaka 已提交
673
    FontFamily* family2 = new FontFamily(VARIANT_DEFAULT);
674
    MinikinAutoUnref<MinikinFont> font2(new MinikinFontForTest(kVSTestFont));
S
Seigo Nonaka 已提交
675
    family2->addFont(font2.get());
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
    families.push_back(family2);

    FontCollection collection(families);

    std::vector<FontCollection::Run> runs;

    itemize(&collection, "U+717D U+FE02", FontStyle(), &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kVSTestFont, getFontPath(runs[0]));

    family1->Unref();
    family2->Unref();
}
691

692 693 694
TEST_F(FontCollectionItemizeTest, itemize_LanguageScore) {
    struct TestCase {
        std::string userPreferredLanguages;
695
        std::vector<std::string> fontLanguages;
696 697
        int selectedFontIndex;
    } testCases[] = {
698 699 700 701 702 703
        // Font can specify empty language.
        { "und", { "", "" }, 0 },
        { "und", { "", "en-Latn" }, 0 },
        { "en-Latn", { "", "" }, 0 },
        { "en-Latn", { "", "en-Latn" }, 1 },

704 705
        // Single user preferred language.
        // Exact match case
706 707 708 709 710 711
        { "en-Latn", { "en-Latn", "ja-Jpan" }, 0 },
        { "ja-Jpan", { "en-Latn", "ja-Jpan" }, 1 },
        { "en-Latn", { "en-Latn", "nl-Latn", "es-Latn" }, 0 },
        { "nl-Latn", { "en-Latn", "nl-Latn", "es-Latn" }, 1 },
        { "es-Latn", { "en-Latn", "nl-Latn", "es-Latn" }, 2 },
        { "es-Latn", { "en-Latn", "en-Latn", "nl-Latn" }, 0 },
712 713

        // Exact script match case
714 715 716 717 718 719 720 721
        { "en-Latn", { "nl-Latn", "e-Latn" }, 0 },
        { "en-Arab", { "nl-Latn", "ar-Arab" }, 1 },
        { "en-Latn", { "be-Latn", "ar-Arab", "d-Beng" }, 0 },
        { "en-Arab", { "be-Latn", "ar-Arab", "d-Beng" }, 1 },
        { "en-Beng", { "be-Latn", "ar-Arab", "d-Beng" }, 2 },
        { "en-Beng", { "be-Latn", "ar-Beng", "d-Beng" }, 1 },
        { "zh-Hant", { "zh-Hant", "zh-Hans" }, 0 },
        { "zh-Hans", { "zh-Hant", "zh-Hans" }, 1 },
722 723

        // Subscript match case, e.g. Jpan supports Hira.
724 725 726 727
        { "en-Hira", { "ja-Jpan" }, 0 },
        { "zh-Hani", { "zh-Hans", "zh-Hant" }, 0 },
        { "zh-Hani", { "zh-Hant", "zh-Hans" }, 0 },
        { "en-Hira", { "zh-Hant", "ja-Jpan", "ja-Jpan" }, 1 },
728 729

        // Language match case
730 731 732 733
        { "ja-Latn", { "zh-Latn", "ja-Latn" }, 1 },
        { "zh-Latn", { "zh-Latn", "ja-Latn" }, 0 },
        { "ja-Latn", { "zh-Latn", "ja-Latn" }, 1 },
        { "ja-Latn", { "zh-Latn", "ja-Latn", "ja-Latn" }, 1 },
734 735 736

        // Mixed case
        // Script/subscript match is strongest.
737 738 739
        { "ja-Jpan", { "en-Latn", "ja-Latn", "en-Jpan" }, 2 },
        { "ja-Hira", { "en-Latn", "ja-Latn", "en-Jpan" }, 2 },
        { "ja-Hira", { "en-Latn", "ja-Latn", "en-Jpan", "en-Jpan" }, 2 },
740 741

        // Language match only happens if the script matches.
742 743
        { "ja-Hira", { "en-Latn", "ja-Latn" }, 0 },
        { "ja-Hira", { "en-Jpan", "ja-Jpan" }, 1 },
744 745 746

        // Multiple languages.
        // Even if all fonts have the same score, use the 2nd language for better selection.
747 748 749 750
        { "en-Latn,ja-Jpan", { "zh-Hant", "zh-Hans", "ja-Jpan" }, 2 },
        { "en-Latn,nl-Latn", { "es-Latn", "be-Latn", "nl-Latn" }, 2 },
        { "en-Latn,br-Latn,nl-Latn", { "es-Latn", "be-Latn", "nl-Latn" }, 2 },
        { "en-Latn,br-Latn,nl-Latn", { "es-Latn", "be-Latn", "nl-Latn", "nl-Latn" }, 2 },
751 752

        // Script score.
753 754
        { "en-Latn,ja-Jpan", { "en-Arab", "en-Jpan" }, 1 },
        { "en-Latn,ja-Jpan", { "en-Arab", "en-Jpan", "en-Jpan" }, 1 },
755 756

        // Language match case
757 758
        { "en-Latn,ja-Latn", { "bd-Latn", "ja-Latn" }, 1 },
        { "en-Latn,ja-Latn", { "bd-Latn", "ja-Latn", "ja-Latn" }, 1 },
759 760

        // Language match only happens if the script matches.
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
        { "en-Latn,ar-Arab", { "en-Beng", "ar-Arab" }, 1 },

        // Multiple languages in the font settings.
        { "ko-Jamo", { "ja-Jpan", "ko-Kore", "ko-Kore,ko-Jamo"}, 2 },
        { "en-Latn", { "ja-Jpan", "en-Latn,ja-Jpan"}, 1 },
        { "en-Latn", { "ja-Jpan", "ja-Jpan,en-Latn"}, 1 },
        { "en-Latn", { "ja-Jpan,zh-Hant", "en-Latn,ja-Jpan", "en-Latn"}, 1 },
        { "en-Latn", { "zh-Hant,ja-Jpan", "ja-Jpan,en-Latn", "en-Latn"}, 1 },

        // Kore = Hang + Hani, etc.
        { "ko-Kore", { "ko-Hang", "ko-Jamo,ko-Hani", "ko-Hang,ko-Hani"}, 2 },
        { "ja-Hrkt", { "ja-Hira", "ja-Kana", "ja-Hira,ja-Kana"}, 2 },
        { "ja-Jpan", { "ja-Hira", "ja-Kana", "ja-Hani", "ja-Hira,ja-Kana,ja-Hani"}, 3 },
        { "zh-Hanb", { "zh-Hant", "zh-Bopo", "zh-Hant,zh-Bopo"}, 2 },
        { "zh-Hanb", { "ja-Hanb", "zh-Hant,zh-Bopo"}, 1 },

        // Language match with unified subscript bits.
        { "zh-Hanb", { "zh-Hant", "zh-Bopo", "ja-Hant,ja-Bopo", "zh-Hant,zh-Bopo"}, 3 },
        { "zh-Hanb", { "zh-Hant", "zh-Bopo", "ja-Hant,zh-Bopo", "zh-Hant,zh-Bopo"}, 3 },
780 781 782
    };

    for (auto testCase : testCases) {
783 784 785 786 787 788 789 790
        std::string fontLanguagesStr = "{";
        for (size_t i = 0; i < testCase.fontLanguages.size(); ++i) {
            if (i != 0) {
                fontLanguagesStr += ", ";
            }
            fontLanguagesStr += "\"" + testCase.fontLanguages[i] + "\"";
        }
        fontLanguagesStr += "}";
791
        SCOPED_TRACE("Test of user preferred languages: \"" + testCase.userPreferredLanguages +
792
                     "\" with font languages: " + fontLanguagesStr);
793 794 795 796 797 798

        std::vector<FontFamily*> families;

        // Prepare first font which doesn't supports U+9AA8
        FontFamily* firstFamily = new FontFamily(
                FontStyle::registerLanguageList("und"), 0 /* variant */);
S
Seigo Nonaka 已提交
799
        MinikinAutoUnref<MinikinFont> firstFamilyMinikinFont(
800
                new MinikinFontForTest(kNoGlyphFont));
S
Seigo Nonaka 已提交
801
        firstFamily->addFont(firstFamilyMinikinFont.get());
802 803 804 805 806 807 808
        families.push_back(firstFamily);

        // Prepare font families
        // Each font family is associated with a specified language. All font families except for
        // the first font support U+9AA8.
        std::unordered_map<MinikinFont*, int> fontLangIdxMap;

809
        for (size_t i = 0; i < testCase.fontLanguages.size(); ++i) {
810
            FontFamily* family = new FontFamily(
811
                    FontStyle::registerLanguageList(testCase.fontLanguages[i]), 0 /* variant */);
812
            MinikinAutoUnref<MinikinFont> minikin_font(new MinikinFontForTest(kJAFont));
S
Seigo Nonaka 已提交
813
            family->addFont(minikin_font.get());
814
            families.push_back(family);
S
Seigo Nonaka 已提交
815
            fontLangIdxMap.insert(std::make_pair(minikin_font.get(), i));
816
        }
S
Seigo Nonaka 已提交
817
        MinikinAutoUnref<FontCollection> collection(new FontCollection(families));
818 819 820 821 822 823 824 825
        for (auto family : families) {
            family->Unref();
        }

        // Do itemize
        const FontStyle style = FontStyle(
                FontStyle::registerLanguageList(testCase.userPreferredLanguages));
        std::vector<FontCollection::Run> runs;
S
Seigo Nonaka 已提交
826
        itemize(collection.get(), "U+9AA8", style, &runs);
827 828 829 830 831
        ASSERT_EQ(1U, runs.size());
        ASSERT_NE(nullptr, runs[0].fakedFont.font);

        // First family doesn't support U+9AA8 and others support it, so the first font should not
        // be selected.
S
Seigo Nonaka 已提交
832
        EXPECT_NE(firstFamilyMinikinFont.get(), runs[0].fakedFont.font);
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131

        // Lookup used font family by MinikinFont*.
        const int usedLangIndex = fontLangIdxMap[runs[0].fakedFont.font];
        EXPECT_EQ(testCase.selectedFontIndex, usedLangIndex);
    }
}

TEST_F(FontCollectionItemizeTest, itemize_LanguageAndCoverage) {
    struct TestCase {
        std::string testString;
        std::string requestedLanguages;
        std::string expectedFont;
    } testCases[] = {
        // Following test cases verify that following rules in font fallback chain.
        // - If the first font in the collection supports the given character or variation sequence,
        //   it should be selected.
        // - If the font doesn't support the given character, variation sequence or its base
        //   character, it should not be selected.
        // - If two or more fonts match the requested languages, the font matches with the highest
        //   priority language should be selected.
        // - If two or more fonts get the same score, the font listed earlier in the XML file
        //   (here, kItemizeFontXml) should be selected.

        // Regardless of language, the first font is always selected if it covers the code point.
        { "'a'", "", kLatinFont},
        { "'a'", "en-Latn", kLatinFont},
        { "'a'", "ja-Jpan", kLatinFont},
        { "'a'", "ja-Jpan,en-Latn", kLatinFont},
        { "'a'", "zh-Hans,zh-Hant,en-Latn,ja-Jpan,fr-Latn", kLatinFont},

        // U+81ED is supported by both the ja font and zh-Hans font.
        { "U+81ED", "", kZH_HansFont },  // zh-Hans font is listed before ja font.
        { "U+81ED", "en-Latn", kZH_HansFont },  // zh-Hans font is listed before ja font.
        { "U+81ED", "ja-Jpan", kJAFont },
        { "U+81ED", "zh-Hans", kZH_HansFont },

        { "U+81ED", "ja-Jpan,en-Latn", kJAFont },
        { "U+81ED", "en-Latn,ja-Jpan", kJAFont },
        { "U+81ED", "en-Latn,zh-Hans", kZH_HansFont },
        { "U+81ED", "zh-Hans,en-Latn", kZH_HansFont },
        { "U+81ED", "ja-Jpan,zh-Hans", kJAFont },
        { "U+81ED", "zh-Hans,ja-Jpan", kZH_HansFont },

        { "U+81ED", "en-Latn,zh-Hans,ja-Jpan", kZH_HansFont },
        { "U+81ED", "en-Latn,ja-Jpan,zh-Hans", kJAFont },
        { "U+81ED", "en-Latn,zh-Hans,ja-Jpan", kZH_HansFont },
        { "U+81ED", "ja-Jpan,en-Latn,zh-Hans", kJAFont },
        { "U+81ED", "ja-Jpan,zh-Hans,en-Latn", kJAFont },
        { "U+81ED", "zh-Hans,en-Latn,ja-Jpan", kZH_HansFont },
        { "U+81ED", "zh-Hans,ja-Jpan,en-Latn", kZH_HansFont },

        // U+304A is only supported by ja font.
        { "U+304A", "", kJAFont },
        { "U+304A", "ja-Jpan", kJAFont },
        { "U+304A", "zh-Hant", kJAFont },
        { "U+304A", "zh-Hans", kJAFont },

        { "U+304A", "ja-Jpan,zh-Hant", kJAFont },
        { "U+304A", "zh-Hant,ja-Jpan", kJAFont },
        { "U+304A", "zh-Hans,zh-Hant", kJAFont },
        { "U+304A", "zh-Hant,zh-Hans", kJAFont },
        { "U+304A", "zh-Hans,ja-Jpan", kJAFont },
        { "U+304A", "ja-Jpan,zh-Hans", kJAFont },

        { "U+304A", "zh-Hans,ja-Jpan,zh-Hant", kJAFont },
        { "U+304A", "zh-Hans,zh-Hant,ja-Jpan", kJAFont },
        { "U+304A", "ja-Jpan,zh-Hans,zh-Hant", kJAFont },
        { "U+304A", "ja-Jpan,zh-Hant,zh-Hans", kJAFont },
        { "U+304A", "zh-Hant,zh-Hans,ja-Jpan", kJAFont },
        { "U+304A", "zh-Hant,ja-Jpan,zh-Hans", kJAFont },

        // U+242EE is supported by both ja font and zh-Hant fonts but not by zh-Hans font.
        { "U+242EE", "", kJAFont },  // ja font is listed before zh-Hant font.
        { "U+242EE", "ja-Jpan", kJAFont },
        { "U+242EE", "zh-Hans", kJAFont },
        { "U+242EE", "zh-Hant", kZH_HantFont },

        { "U+242EE", "ja-Jpan,zh-Hant", kJAFont },
        { "U+242EE", "zh-Hant,ja-Jpan", kZH_HantFont },
        { "U+242EE", "zh-Hans,zh-Hant", kZH_HantFont },
        { "U+242EE", "zh-Hant,zh-Hans", kZH_HantFont },
        { "U+242EE", "zh-Hans,ja-Jpan", kJAFont },
        { "U+242EE", "ja-Jpan,zh-Hans", kJAFont },

        { "U+242EE", "zh-Hans,ja-Jpan,zh-Hant", kJAFont },
        { "U+242EE", "zh-Hans,zh-Hant,ja-Jpan", kZH_HantFont },
        { "U+242EE", "ja-Jpan,zh-Hans,zh-Hant", kJAFont },
        { "U+242EE", "ja-Jpan,zh-Hant,zh-Hans", kJAFont },
        { "U+242EE", "zh-Hant,zh-Hans,ja-Jpan", kZH_HantFont },
        { "U+242EE", "zh-Hant,ja-Jpan,zh-Hans", kZH_HantFont },

        // U+9AA8 is supported by all ja-Jpan, zh-Hans, zh-Hant fonts.
        { "U+9AA8", "", kZH_HansFont },  // zh-Hans font is listed before ja and zh-Hant fonts.
        { "U+9AA8", "ja-Jpan", kJAFont },
        { "U+9AA8", "zh-Hans", kZH_HansFont },
        { "U+9AA8", "zh-Hant", kZH_HantFont },

        { "U+9AA8", "ja-Jpan,zh-Hant", kJAFont },
        { "U+9AA8", "zh-Hant,ja-Jpan", kZH_HantFont },
        { "U+9AA8", "zh-Hans,zh-Hant", kZH_HansFont },
        { "U+9AA8", "zh-Hant,zh-Hans", kZH_HantFont },
        { "U+9AA8", "zh-Hans,ja-Jpan", kZH_HansFont },
        { "U+9AA8", "ja-Jpan,zh-Hans", kJAFont },

        { "U+9AA8", "zh-Hans,ja-Jpan,zh-Hant", kZH_HansFont },
        { "U+9AA8", "zh-Hans,zh-Hant,ja-Jpan", kZH_HansFont },
        { "U+9AA8", "ja-Jpan,zh-Hans,zh-Hant", kJAFont },
        { "U+9AA8", "ja-Jpan,zh-Hant,zh-Hans", kJAFont },
        { "U+9AA8", "zh-Hant,zh-Hans,ja-Jpan", kZH_HantFont },
        { "U+9AA8", "zh-Hant,ja-Jpan,zh-Hans", kZH_HantFont },

        // U+242EE U+FE00 is supported by ja font but not by zh-Hans or zh-Hant fonts.
        { "U+242EE U+FE00", "", kJAFont },
        { "U+242EE U+FE00", "ja-Jpan", kJAFont },
        { "U+242EE U+FE00", "zh-Hant", kJAFont },
        { "U+242EE U+FE00", "zh-Hans", kJAFont },

        { "U+242EE U+FE00", "ja-Jpan,zh-Hant", kJAFont },
        { "U+242EE U+FE00", "zh-Hant,ja-Jpan", kJAFont },
        { "U+242EE U+FE00", "zh-Hans,zh-Hant", kJAFont },
        { "U+242EE U+FE00", "zh-Hant,zh-Hans", kJAFont },
        { "U+242EE U+FE00", "zh-Hans,ja-Jpan", kJAFont },
        { "U+242EE U+FE00", "ja-Jpan,zh-Hans", kJAFont },

        { "U+242EE U+FE00", "zh-Hans,ja-Jpan,zh-Hant", kJAFont },
        { "U+242EE U+FE00", "zh-Hans,zh-Hant,ja-Jpan", kJAFont },
        { "U+242EE U+FE00", "ja-Jpan,zh-Hans,zh-Hant", kJAFont },
        { "U+242EE U+FE00", "ja-Jpan,zh-Hant,zh-Hans", kJAFont },
        { "U+242EE U+FE00", "zh-Hant,zh-Hans,ja-Jpan", kJAFont },
        { "U+242EE U+FE00", "zh-Hant,ja-Jpan,zh-Hans", kJAFont },

        // U+3402 U+E0100 is supported by both zh-Hans and zh-Hant but not by ja font.
        { "U+3402 U+E0100", "", kZH_HansFont },  // zh-Hans font is listed before zh-Hant font.
        { "U+3402 U+E0100", "ja-Jpan", kZH_HansFont },  // zh-Hans font is listed before zh-Hant font.
        { "U+3402 U+E0100", "zh-Hant", kZH_HantFont },
        { "U+3402 U+E0100", "zh-Hans", kZH_HansFont },

        { "U+3402 U+E0100", "ja-Jpan,zh-Hant", kZH_HantFont },
        { "U+3402 U+E0100", "zh-Hant,ja-Jpan", kZH_HantFont },
        { "U+3402 U+E0100", "zh-Hans,zh-Hant", kZH_HansFont },
        { "U+3402 U+E0100", "zh-Hant,zh-Hans", kZH_HantFont },
        { "U+3402 U+E0100", "zh-Hans,ja-Jpan", kZH_HansFont },
        { "U+3402 U+E0100", "ja-Jpan,zh-Hans", kZH_HansFont },

        { "U+3402 U+E0100", "zh-Hans,ja-Jpan,zh-Hant", kZH_HansFont },
        { "U+3402 U+E0100", "zh-Hans,zh-Hant,ja-Jpan", kZH_HansFont },
        { "U+3402 U+E0100", "ja-Jpan,zh-Hans,zh-Hant", kZH_HansFont },
        { "U+3402 U+E0100", "ja-Jpan,zh-Hant,zh-Hans", kZH_HantFont },
        { "U+3402 U+E0100", "zh-Hant,zh-Hans,ja-Jpan", kZH_HantFont },
        { "U+3402 U+E0100", "zh-Hant,ja-Jpan,zh-Hans", kZH_HantFont },

        // No font supports U+4444 U+FE00 but only zh-Hans supports its base character U+4444.
        { "U+4444 U+FE00", "", kZH_HansFont },
        { "U+4444 U+FE00", "ja-Jpan", kZH_HansFont },
        { "U+4444 U+FE00", "zh-Hant", kZH_HansFont },
        { "U+4444 U+FE00", "zh-Hans", kZH_HansFont },

        { "U+4444 U+FE00", "ja-Jpan,zh-Hant", kZH_HansFont },
        { "U+4444 U+FE00", "zh-Hant,ja-Jpan", kZH_HansFont },
        { "U+4444 U+FE00", "zh-Hans,zh-Hant", kZH_HansFont },
        { "U+4444 U+FE00", "zh-Hant,zh-Hans", kZH_HansFont },
        { "U+4444 U+FE00", "zh-Hans,ja-Jpan", kZH_HansFont },
        { "U+4444 U+FE00", "ja-Jpan,zh-Hans", kZH_HansFont },

        { "U+4444 U+FE00", "zh-Hans,ja-Jpan,zh-Hant", kZH_HansFont },
        { "U+4444 U+FE00", "zh-Hans,zh-Hant,ja-Jpan", kZH_HansFont },
        { "U+4444 U+FE00", "ja-Jpan,zh-Hans,zh-Hant", kZH_HansFont },
        { "U+4444 U+FE00", "ja-Jpan,zh-Hant,zh-Hans", kZH_HansFont },
        { "U+4444 U+FE00", "zh-Hant,zh-Hans,ja-Jpan", kZH_HansFont },
        { "U+4444 U+FE00", "zh-Hant,ja-Jpan,zh-Hans", kZH_HansFont },

        // No font supports U+81ED U+E0100 but ja and zh-Hans support its base character U+81ED.
        // zh-Hans font is listed before ja font.
        { "U+81ED U+E0100", "", kZH_HansFont },
        { "U+81ED U+E0100", "ja-Jpan", kJAFont },
        { "U+81ED U+E0100", "zh-Hant", kZH_HansFont },
        { "U+81ED U+E0100", "zh-Hans", kZH_HansFont },

        { "U+81ED U+E0100", "ja-Jpan,zh-Hant", kJAFont },
        { "U+81ED U+E0100", "zh-Hant,ja-Jpan", kJAFont },
        { "U+81ED U+E0100", "zh-Hans,zh-Hant", kZH_HansFont },
        { "U+81ED U+E0100", "zh-Hant,zh-Hans", kZH_HansFont },
        { "U+81ED U+E0100", "zh-Hans,ja-Jpan", kZH_HansFont },
        { "U+81ED U+E0100", "ja-Jpan,zh-Hans", kJAFont },

        { "U+81ED U+E0100", "zh-Hans,ja-Jpan,zh-Hant", kZH_HansFont },
        { "U+81ED U+E0100", "zh-Hans,zh-Hant,ja-Jpan", kZH_HansFont },
        { "U+81ED U+E0100", "ja-Jpan,zh-Hans,zh-Hant", kJAFont },
        { "U+81ED U+E0100", "ja-Jpan,zh-Hant,zh-Hans", kJAFont },
        { "U+81ED U+E0100", "zh-Hant,zh-Hans,ja-Jpan", kZH_HansFont },
        { "U+81ED U+E0100", "zh-Hant,ja-Jpan,zh-Hans", kJAFont },

        // No font supports U+9AA8 U+E0100 but all zh-Hans zh-hant ja fonts support its base
        // character U+9AA8.
        // zh-Hans font is listed before ja and zh-Hant fonts.
        { "U+9AA8 U+E0100", "", kZH_HansFont },
        { "U+9AA8 U+E0100", "ja-Jpan", kJAFont },
        { "U+9AA8 U+E0100", "zh-Hans", kZH_HansFont },
        { "U+9AA8 U+E0100", "zh-Hant", kZH_HantFont },

        { "U+9AA8 U+E0100", "ja-Jpan,zh-Hant", kJAFont },
        { "U+9AA8 U+E0100", "zh-Hant,ja-Jpan", kZH_HantFont },
        { "U+9AA8 U+E0100", "zh-Hans,zh-Hant", kZH_HansFont },
        { "U+9AA8 U+E0100", "zh-Hant,zh-Hans", kZH_HantFont },
        { "U+9AA8 U+E0100", "zh-Hans,ja-Jpan", kZH_HansFont },
        { "U+9AA8 U+E0100", "ja-Jpan,zh-Hans", kJAFont },

        { "U+9AA8 U+E0100", "zh-Hans,ja-Jpan,zh-Hant", kZH_HansFont },
        { "U+9AA8 U+E0100", "zh-Hans,zh-Hant,ja-Jpan", kZH_HansFont },
        { "U+9AA8 U+E0100", "ja-Jpan,zh-Hans,zh-Hant", kJAFont },
        { "U+9AA8 U+E0100", "ja-Jpan,zh-Hant,zh-Hans", kJAFont },
        { "U+9AA8 U+E0100", "zh-Hant,zh-Hans,ja-Jpan", kZH_HantFont },
        { "U+9AA8 U+E0100", "zh-Hant,ja-Jpan,zh-Hans", kZH_HantFont },

        // All zh-Hans,zh-Hant,ja fonts support U+35A8 U+E0100 and its base character U+35A8.
        // zh-Hans font is listed before ja and zh-Hant fonts.
        { "U+35A8", "", kZH_HansFont },
        { "U+35A8", "ja-Jpan", kJAFont },
        { "U+35A8", "zh-Hans", kZH_HansFont },
        { "U+35A8", "zh-Hant", kZH_HantFont },

        { "U+35A8", "ja-Jpan,zh-Hant", kJAFont },
        { "U+35A8", "zh-Hant,ja-Jpan", kZH_HantFont },
        { "U+35A8", "zh-Hans,zh-Hant", kZH_HansFont },
        { "U+35A8", "zh-Hant,zh-Hans", kZH_HantFont },
        { "U+35A8", "zh-Hans,ja-Jpan", kZH_HansFont },
        { "U+35A8", "ja-Jpan,zh-Hans", kJAFont },

        { "U+35A8", "zh-Hans,ja-Jpan,zh-Hant", kZH_HansFont },
        { "U+35A8", "zh-Hans,zh-Hant,ja-Jpan", kZH_HansFont },
        { "U+35A8", "ja-Jpan,zh-Hans,zh-Hant", kJAFont },
        { "U+35A8", "ja-Jpan,zh-Hant,zh-Hans", kJAFont },
        { "U+35A8", "zh-Hant,zh-Hans,ja-Jpan", kZH_HantFont },
        { "U+35A8", "zh-Hant,ja-Jpan,zh-Hans", kZH_HantFont },

        // All zh-Hans,zh-Hant,ja fonts support U+35B6 U+E0100, but zh-Hant and ja fonts support its
        // base character U+35B6.
        // ja font is listed before zh-Hant font.
        { "U+35B6", "", kJAFont },
        { "U+35B6", "ja-Jpan", kJAFont },
        { "U+35B6", "zh-Hant", kZH_HantFont },
        { "U+35B6", "zh-Hans", kJAFont },

        { "U+35B6", "ja-Jpan,zh-Hant", kJAFont },
        { "U+35B6", "zh-Hant,ja-Jpan", kZH_HantFont },
        { "U+35B6", "zh-Hans,zh-Hant", kZH_HantFont },
        { "U+35B6", "zh-Hant,zh-Hans", kZH_HantFont },
        { "U+35B6", "zh-Hans,ja-Jpan", kJAFont },
        { "U+35B6", "ja-Jpan,zh-Hans", kJAFont },

        { "U+35B6", "zh-Hans,ja-Jpan,zh-Hant", kJAFont },
        { "U+35B6", "zh-Hans,zh-Hant,ja-Jpan", kZH_HantFont },
        { "U+35B6", "ja-Jpan,zh-Hans,zh-Hant", kJAFont },
        { "U+35B6", "ja-Jpan,zh-Hant,zh-Hans", kJAFont },
        { "U+35B6", "zh-Hant,zh-Hans,ja-Jpan", kZH_HantFont },
        { "U+35B6", "zh-Hant,ja-Jpan,zh-Hans", kZH_HantFont },

        // All zh-Hans,zh-Hant,ja fonts support U+35C5 U+E0100, but only ja font supports its base
        // character U+35C5.
        { "U+35C5", "", kJAFont },
        { "U+35C5", "ja-Jpan", kJAFont },
        { "U+35C5", "zh-Hant", kJAFont },
        { "U+35C5", "zh-Hans", kJAFont },

        { "U+35C5", "ja-Jpan,zh-Hant", kJAFont },
        { "U+35C5", "zh-Hant,ja-Jpan", kJAFont },
        { "U+35C5", "zh-Hans,zh-Hant", kJAFont },
        { "U+35C5", "zh-Hant,zh-Hans", kJAFont },
        { "U+35C5", "zh-Hans,ja-Jpan", kJAFont },
        { "U+35C5", "ja-Jpan,zh-Hans", kJAFont },

        { "U+35C5", "zh-Hans,ja-Jpan,zh-Hant", kJAFont },
        { "U+35C5", "zh-Hans,zh-Hant,ja-Jpan", kJAFont },
        { "U+35C5", "ja-Jpan,zh-Hans,zh-Hant", kJAFont },
        { "U+35C5", "ja-Jpan,zh-Hant,zh-Hans", kJAFont },
        { "U+35C5", "zh-Hant,zh-Hans,ja-Jpan", kJAFont },
        { "U+35C5", "zh-Hant,ja-Jpan,zh-Hans", kJAFont },

        // None of ja-Jpan, zh-Hant, zh-Hans font supports U+1F469. Emoji font supports it.
        { "U+1F469", "", kEmojiFont },
        { "U+1F469", "ja-Jpan", kEmojiFont },
        { "U+1F469", "zh-Hant", kEmojiFont },
        { "U+1F469", "zh-Hans", kEmojiFont },

        { "U+1F469", "ja-Jpan,zh-Hant", kEmojiFont },
        { "U+1F469", "zh-Hant,ja-Jpan", kEmojiFont },
        { "U+1F469", "zh-Hans,zh-Hant", kEmojiFont },
        { "U+1F469", "zh-Hant,zh-Hans", kEmojiFont },
        { "U+1F469", "zh-Hans,ja-Jpan", kEmojiFont },
        { "U+1F469", "ja-Jpan,zh-Hans", kEmojiFont },

        { "U+1F469", "zh-Hans,ja-Jpan,zh-Hant", kEmojiFont },
        { "U+1F469", "zh-Hans,zh-Hant,ja-Jpan", kEmojiFont },
        { "U+1F469", "ja-Jpan,zh-Hans,zh-Hant", kEmojiFont },
        { "U+1F469", "ja-Jpan,zh-Hant,zh-Hans", kEmojiFont },
        { "U+1F469", "zh-Hant,zh-Hans,ja-Jpan", kEmojiFont },
        { "U+1F469", "zh-Hant,ja-Jpan,zh-Hans", kEmojiFont },
    };

1132
    MinikinAutoUnref<FontCollection> collection(getFontCollection(kTestFontDir, kItemizeFontXml));
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146

    for (auto testCase : testCases) {
        SCOPED_TRACE("Test for \"" + testCase.testString + "\" with languages " +
                     testCase.requestedLanguages);

        std::vector<FontCollection::Run> runs;
        const FontStyle style =
                FontStyle(FontStyle::registerLanguageList(testCase.requestedLanguages));
        itemize(collection.get(), testCase.testString.c_str(), style, &runs);
        ASSERT_EQ(1U, runs.size());
        EXPECT_EQ(testCase.expectedFont, getFontPath(runs[0]));
    }
}

1147
TEST_F(FontCollectionItemizeTest, itemize_emojiSelection_withFE0E) {
1148
    MinikinAutoUnref<FontCollection> collection(getFontCollection(kTestFontDir, kEmojiXmlFile));
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
    std::vector<FontCollection::Run> runs;

    const FontStyle kDefaultFontStyle;

    // U+00A9 is a text default emoji which is only available in TextEmojiFont.ttf.
    // TextEmojiFont.ttf should be selected.
    itemize(collection.get(), "U+00A9 U+FE0E", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kTextEmojiFont, getFontPath(runs[0]));

    // U+00A9 is a text default emoji which is only available in ColorEmojiFont.ttf.
    // ColorEmojiFont.ttf should be selected.
    itemize(collection.get(), "U+00AE U+FE0E", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    // Text emoji is specified but it is not available. Use color emoji instead.
    EXPECT_EQ(kColorEmojiFont, getFontPath(runs[0]));

    // U+203C is a text default emoji which is available in both TextEmojiFont.ttf and
    // ColorEmojiFont.ttf. TextEmojiFont.ttf should be selected.
    itemize(collection.get(), "U+203C U+FE0E", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kTextEmojiFont, getFontPath(runs[0]));

    // U+2049 is a text default emoji which is not available either TextEmojiFont.ttf or
    // ColorEmojiFont.ttf. No font should be selected.
    itemize(collection.get(), "U+2049 U+FE0E", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
1184
    EXPECT_EQ(kNoGlyphFont, getFontPath(runs[0]));
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217

    // U+231A is a emoji default emoji which is available only in TextEmojifFont.
    // TextEmojiFont.ttf sohuld be selected.
    itemize(collection.get(), "U+231A U+FE0E", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kTextEmojiFont, getFontPath(runs[0]));

    // U+231B is a emoji default emoji which is available only in ColorEmojiFont.ttf.
    // ColorEmojiFont.ttf should be selected.
    itemize(collection.get(), "U+231B U+FE0E", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    // Text emoji is specified but it is not available. Use color emoji instead.
    EXPECT_EQ(kColorEmojiFont, getFontPath(runs[0]));

    // U+23E9 is a emoji default emoji which is available in both TextEmojiFont.ttf and
    // ColorEmojiFont.ttf. TextEmojiFont.ttf should be selected even if U+23E9 is emoji default
    // emoji since U+FE0E is appended.
    itemize(collection.get(), "U+23E9 U+FE0E", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kTextEmojiFont, getFontPath(runs[0]));

    // U+23EA is a emoji default emoji but which is not available in either TextEmojiFont.ttf or
    // ColorEmojiFont.ttf. No font should be selected.
    itemize(collection.get(), "U+23EA U+FE0E", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
1218
    EXPECT_EQ(kNoGlyphFont, getFontPath(runs[0]));
1219

1220
    // U+26FA U+FE0E is specified but ColorTextMixedEmojiFont has a variation sequence U+26F9 U+FE0F
1221 1222 1223 1224 1225 1226 1227 1228
    // in its cmap, so ColorTextMixedEmojiFont should be selected instaed of ColorEmojiFont.
    itemize(collection.get(), "U+26FA U+FE0E", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kMixedEmojiFont, getFontPath(runs[0]));
}

1229
TEST_F(FontCollectionItemizeTest, itemize_emojiSelection_withFE0F) {
1230
    MinikinAutoUnref<FontCollection> collection(getFontCollection(kTestFontDir, kEmojiXmlFile));
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
    std::vector<FontCollection::Run> runs;

    const FontStyle kDefaultFontStyle;

    // U+00A9 is a text default emoji which is available only in TextEmojiFont.ttf.
    // TextEmojiFont.ttf shoudl be selected.
    itemize(collection.get(), "U+00A9 U+FE0F", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    // Color emoji is specified but it is not available. Use text representaion instead.
    EXPECT_EQ(kTextEmojiFont, getFontPath(runs[0]));

    // U+00AE is a text default emoji which is available only in ColorEmojiFont.ttf.
    // ColorEmojiFont.ttf should be selected.
    itemize(collection.get(), "U+00AE U+FE0F", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kColorEmojiFont, getFontPath(runs[0]));

    // U+203C is a text default emoji which is available in both TextEmojiFont.ttf and
    // ColorEmojiFont.ttf. ColorEmojiFont.ttf should be selected even if U+203C is a text default
    // emoji since U+FF0F is appended.
    itemize(collection.get(), "U+203C U+FE0F", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kColorEmojiFont, getFontPath(runs[0]));

    // U+2049 is a text default emoji which is not available in either TextEmojiFont.ttf or
    // ColorEmojiFont.ttf. No font should be selected.
    itemize(collection.get(), "U+2049 U+FE0F", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
1267
    EXPECT_EQ(kNoGlyphFont, getFontPath(runs[0]));
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299

    // U+231A is a emoji default emoji which is available only in TextEmojiFont.ttf.
    // TextEmojiFont.ttf should be selected.
    itemize(collection.get(), "U+231A U+FE0F", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    // Color emoji is specified but it is not available. Use text representation instead.
    EXPECT_EQ(kTextEmojiFont, getFontPath(runs[0]));

    // U+231B is a emoji default emoji which is available only in ColorEmojiFont.ttf.
    // ColorEmojiFont.ttf should be selected.
    itemize(collection.get(), "U+231B U+FE0F", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kColorEmojiFont, getFontPath(runs[0]));

    // U+23E9 is a emoji default emoji which is available in both TextEmojiFont.ttf and
    // ColorEmojiFont.ttf. ColorEmojiFont.ttf should be selected.
    itemize(collection.get(), "U+23E9 U+FE0F", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kColorEmojiFont, getFontPath(runs[0]));

    // U+23EA is a emoji default emoji which is not available in either TextEmojiFont.ttf or
    // ColorEmojiFont.ttf. No font should be selected.
    itemize(collection.get(), "U+23EA U+FE0F", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
1300
    EXPECT_EQ(kNoGlyphFont, getFontPath(runs[0]));
1301

1302
    // U+26F9 U+FE0F is specified but ColorTextMixedEmojiFont has a variation sequence U+26F9 U+FE0F
1303
    // in its cmap, so ColorTextMixedEmojiFont should be selected instaed of ColorEmojiFont.
1304
    itemize(collection.get(), "U+26F9 U+FE0F", kDefaultFontStyle, &runs);
1305 1306 1307 1308 1309 1310
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kMixedEmojiFont, getFontPath(runs[0]));
}

1311
TEST_F(FontCollectionItemizeTest, itemize_emojiSelection_with_skinTone) {
1312
    MinikinAutoUnref<FontCollection> collection(getFontCollection(kTestFontDir, kEmojiXmlFile));
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
    std::vector<FontCollection::Run> runs;

    const FontStyle kDefaultFontStyle;

    // TextEmoji font is selected since it is listed before ColorEmoji font.
    itemize(collection.get(), "U+261D", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(1, runs[0].end);
    EXPECT_EQ(kTextEmojiFont, getFontPath(runs[0]));

    // If skin tone is specified, it should be colored.
    itemize(collection.get(), "U+261D U+1F3FD", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(3, runs[0].end);
    EXPECT_EQ(kColorEmojiFont, getFontPath(runs[0]));

    // Still color font is selected if an emoji variation selector is specified.
    itemize(collection.get(), "U+261D U+FE0F U+1F3FD", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(4, runs[0].end);
    EXPECT_EQ(kColorEmojiFont, getFontPath(runs[0]));

    // Text font should be selected if a text variation selector is specified and skin tone is
    // rendered by itself.
    itemize(collection.get(), "U+261D U+FE0E U+1F3FD", kDefaultFontStyle, &runs);
    ASSERT_EQ(2U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kTextEmojiFont, getFontPath(runs[0]));
    EXPECT_EQ(2, runs[1].start);
    EXPECT_EQ(4, runs[1].end);
    EXPECT_EQ(kColorEmojiFont, getFontPath(runs[1]));
}
1349 1350

TEST_F(FontCollectionItemizeTest, itemize_PrivateUseArea) {
1351
    MinikinAutoUnref<FontCollection> collection(getFontCollection(kTestFontDir, kEmojiXmlFile));
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
    std::vector<FontCollection::Run> runs;

    const FontStyle kDefaultFontStyle;

    // Should not set nullptr to the result run. (Issue 26808815)
    itemize(collection.get(), "U+FEE10", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(2, runs[0].end);
    EXPECT_EQ(kNoGlyphFont, getFontPath(runs[0]));

    itemize(collection.get(), "U+FEE40 U+FE4C5", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(4, runs[0].end);
    EXPECT_EQ(kNoGlyphFont, getFontPath(runs[0]));
}
S
Seigo Nonaka 已提交
1369

1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
TEST_F(FontCollectionItemizeTest, itemize_genderBalancedEmoji) {
    MinikinAutoUnref<FontCollection> collection(getFontCollection(kTestFontDir, kEmojiXmlFile));
    std::vector<FontCollection::Run> runs;

    const FontStyle kDefaultFontStyle;

    itemize(collection.get(), "U+1F469 U+200D U+1F373", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(5, runs[0].end);
    EXPECT_EQ(kColorEmojiFont, getFontPath(runs[0]));

    itemize(collection.get(), "U+1F469 U+200D U+2695 U+FE0F", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(5, runs[0].end);
    EXPECT_EQ(kColorEmojiFont, getFontPath(runs[0]));

    itemize(collection.get(), "U+1F469 U+200D U+2695", kDefaultFontStyle, &runs);
    ASSERT_EQ(1U, runs.size());
    EXPECT_EQ(0, runs[0].start);
    EXPECT_EQ(4, runs[0].end);
    EXPECT_EQ(kColorEmojiFont, getFontPath(runs[0]));
}

S
Seigo Nonaka 已提交
1395
}  // namespace minikin