| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/fonts/shaping/HarfBuzzShaper.h" | 5 #include "platform/fonts/shaping/HarfBuzzShaper.h" |
| 6 | 6 |
| 7 #include "platform/fonts/Font.h" | 7 #include "platform/fonts/Font.h" |
| 8 #include "platform/fonts/FontCache.h" | 8 #include "platform/fonts/FontCache.h" |
| 9 #include "platform/fonts/shaping/ShapeResultTestInfo.h" | 9 #include "platform/fonts/shaping/ShapeResultTestInfo.h" |
| 10 #include "platform/text/TextRun.h" | 10 #include "platform/text/TextRun.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 unsigned numGlyphs = 0; | 31 unsigned numGlyphs = 0; |
| 32 hb_script_t script = HB_SCRIPT_INVALID; | 32 hb_script_t script = HB_SCRIPT_INVALID; |
| 33 }; | 33 }; |
| 34 | 34 |
| 35 static inline ShapeResultTestInfo* testInfo(RefPtr<ShapeResult>& result) { | 35 static inline ShapeResultTestInfo* testInfo(RefPtr<ShapeResult>& result) { |
| 36 return static_cast<ShapeResultTestInfo*>(result.get()); | 36 return static_cast<ShapeResultTestInfo*>(result.get()); |
| 37 } | 37 } |
| 38 | 38 |
| 39 TEST_F(HarfBuzzShaperTest, ResolveCandidateRunsLatin) { | 39 TEST_F(HarfBuzzShaperTest, ResolveCandidateRunsLatin) { |
| 40 TextRun latinCommon(reinterpret_cast<const LChar*>("ABC DEF."), 8); | 40 TextRun latinCommon(reinterpret_cast<const LChar*>("ABC DEF."), 8); |
| 41 HarfBuzzShaper shaper(&font, latinCommon); | 41 HarfBuzzShaper shaper(latinCommon); |
| 42 RefPtr<ShapeResult> result = shaper.shapeResult(); | 42 RefPtr<ShapeResult> result = shaper.shapeResult(&font); |
| 43 | 43 |
| 44 ASSERT_EQ(1u, testInfo(result)->numberOfRunsForTesting()); | 44 ASSERT_EQ(1u, testInfo(result)->numberOfRunsForTesting()); |
| 45 ASSERT_TRUE( | 45 ASSERT_TRUE( |
| 46 testInfo(result)->runInfoForTesting(0, startIndex, numGlyphs, script)); | 46 testInfo(result)->runInfoForTesting(0, startIndex, numGlyphs, script)); |
| 47 EXPECT_EQ(0u, startIndex); | 47 EXPECT_EQ(0u, startIndex); |
| 48 EXPECT_EQ(8u, numGlyphs); | 48 EXPECT_EQ(8u, numGlyphs); |
| 49 EXPECT_EQ(HB_SCRIPT_LATIN, script); | 49 EXPECT_EQ(HB_SCRIPT_LATIN, script); |
| 50 } | 50 } |
| 51 | 51 |
| 52 TEST_F(HarfBuzzShaperTest, ResolveCandidateRunsLeadingCommon) { | 52 TEST_F(HarfBuzzShaperTest, ResolveCandidateRunsLeadingCommon) { |
| 53 TextRun leadingCommon(reinterpret_cast<const LChar*>("... test"), 8); | 53 TextRun leadingCommon(reinterpret_cast<const LChar*>("... test"), 8); |
| 54 HarfBuzzShaper shaper(&font, leadingCommon); | 54 HarfBuzzShaper shaper(leadingCommon); |
| 55 RefPtr<ShapeResult> result = shaper.shapeResult(); | 55 RefPtr<ShapeResult> result = shaper.shapeResult(&font); |
| 56 | 56 |
| 57 ASSERT_EQ(1u, testInfo(result)->numberOfRunsForTesting()); | 57 ASSERT_EQ(1u, testInfo(result)->numberOfRunsForTesting()); |
| 58 ASSERT_TRUE( | 58 ASSERT_TRUE( |
| 59 testInfo(result)->runInfoForTesting(0, startIndex, numGlyphs, script)); | 59 testInfo(result)->runInfoForTesting(0, startIndex, numGlyphs, script)); |
| 60 EXPECT_EQ(0u, startIndex); | 60 EXPECT_EQ(0u, startIndex); |
| 61 EXPECT_EQ(8u, numGlyphs); | 61 EXPECT_EQ(8u, numGlyphs); |
| 62 EXPECT_EQ(HB_SCRIPT_LATIN, script); | 62 EXPECT_EQ(HB_SCRIPT_LATIN, script); |
| 63 } | 63 } |
| 64 | 64 |
| 65 TEST_F(HarfBuzzShaperTest, ResolveCandidateRunsUnicodeVariants) { | 65 TEST_F(HarfBuzzShaperTest, ResolveCandidateRunsUnicodeVariants) { |
| 66 struct { | 66 struct { |
| 67 const char* name; | 67 const char* name; |
| 68 UChar string[4]; | 68 UChar string[4]; |
| 69 hb_script_t script; | 69 hb_script_t script; |
| 70 } testlist[] = { | 70 } testlist[] = { |
| 71 {"Standard Variants text style", {0x30, 0xFE0E}, HB_SCRIPT_COMMON}, | 71 {"Standard Variants text style", {0x30, 0xFE0E}, HB_SCRIPT_COMMON}, |
| 72 {"Standard Variants emoji style", {0x203C, 0xFE0F}, HB_SCRIPT_COMMON}, | 72 {"Standard Variants emoji style", {0x203C, 0xFE0F}, HB_SCRIPT_COMMON}, |
| 73 {"Standard Variants of Ideograph", {0x4FAE, 0xFE00}, HB_SCRIPT_HAN}, | 73 {"Standard Variants of Ideograph", {0x4FAE, 0xFE00}, HB_SCRIPT_HAN}, |
| 74 {"Ideographic Variants", {0x3402, 0xDB40, 0xDD00}, HB_SCRIPT_HAN}, | 74 {"Ideographic Variants", {0x3402, 0xDB40, 0xDD00}, HB_SCRIPT_HAN}, |
| 75 {"Not-defined Variants", {0x41, 0xDB40, 0xDDEF}, HB_SCRIPT_LATIN}, | 75 {"Not-defined Variants", {0x41, 0xDB40, 0xDDEF}, HB_SCRIPT_LATIN}, |
| 76 }; | 76 }; |
| 77 for (auto& test : testlist) { | 77 for (auto& test : testlist) { |
| 78 String str(test.string); | 78 String str(test.string); |
| 79 TextRun run(str); | 79 TextRun run(str); |
| 80 HarfBuzzShaper shaper(&font, run); | 80 HarfBuzzShaper shaper(run); |
| 81 RefPtr<ShapeResult> result = shaper.shapeResult(); | 81 RefPtr<ShapeResult> result = shaper.shapeResult(&font); |
| 82 | 82 |
| 83 EXPECT_EQ(1u, testInfo(result)->numberOfRunsForTesting()) << test.name; | 83 EXPECT_EQ(1u, testInfo(result)->numberOfRunsForTesting()) << test.name; |
| 84 ASSERT_TRUE( | 84 ASSERT_TRUE( |
| 85 testInfo(result)->runInfoForTesting(0, startIndex, numGlyphs, script)) | 85 testInfo(result)->runInfoForTesting(0, startIndex, numGlyphs, script)) |
| 86 << test.name; | 86 << test.name; |
| 87 EXPECT_EQ(0u, startIndex) << test.name; | 87 EXPECT_EQ(0u, startIndex) << test.name; |
| 88 if (numGlyphs == 2) { | 88 if (numGlyphs == 2) { |
| 89 // If the specified VS is not in the font, it's mapped to .notdef. | 89 // If the specified VS is not in the font, it's mapped to .notdef. |
| 90 // then hb_ot_hide_default_ignorables() swaps it to a space with zero-advance. | 90 // then hb_ot_hide_default_ignorables() swaps it to a space with zero-advance. |
| 91 // http://lists.freedesktop.org/archives/harfbuzz/2015-May/004888.html | 91 // http://lists.freedesktop.org/archives/harfbuzz/2015-May/004888.html |
| 92 // OpenType recommends Glyph ID 3 for a space; not a hard requirement though. | 92 // OpenType recommends Glyph ID 3 for a space; not a hard requirement though. |
| 93 // https://www.microsoft.com/typography/otspec/recom.htm | 93 // https://www.microsoft.com/typography/otspec/recom.htm |
| 94 #if !OS(MACOSX) | 94 #if !OS(MACOSX) |
| 95 EXPECT_EQ(3u, testInfo(result)->glyphForTesting(0, 1)) << test.name; | 95 EXPECT_EQ(3u, testInfo(result)->glyphForTesting(0, 1)) << test.name; |
| 96 #endif | 96 #endif |
| 97 EXPECT_EQ(0.f, testInfo(result)->advanceForTesting(0, 1)) << test.name; | 97 EXPECT_EQ(0.f, testInfo(result)->advanceForTesting(0, 1)) << test.name; |
| 98 } else { | 98 } else { |
| 99 EXPECT_EQ(1u, numGlyphs) << test.name; | 99 EXPECT_EQ(1u, numGlyphs) << test.name; |
| 100 } | 100 } |
| 101 EXPECT_EQ(test.script, script) << test.name; | 101 EXPECT_EQ(test.script, script) << test.name; |
| 102 } | 102 } |
| 103 } | 103 } |
| 104 | 104 |
| 105 TEST_F(HarfBuzzShaperTest, ResolveCandidateRunsDevanagariCommon) { | 105 TEST_F(HarfBuzzShaperTest, ResolveCandidateRunsDevanagariCommon) { |
| 106 UChar devanagariCommonString[] = {0x915, 0x94d, 0x930, 0x28, 0x20, 0x29}; | 106 UChar devanagariCommonString[] = {0x915, 0x94d, 0x930, 0x28, 0x20, 0x29}; |
| 107 TextRun devanagariCommonLatin(devanagariCommonString, 6); | 107 TextRun devanagariCommonLatin(devanagariCommonString, 6); |
| 108 HarfBuzzShaper shaper(&font, devanagariCommonLatin); | 108 HarfBuzzShaper shaper(devanagariCommonLatin); |
| 109 RefPtr<ShapeResult> result = shaper.shapeResult(); | 109 RefPtr<ShapeResult> result = shaper.shapeResult(&font); |
| 110 | 110 |
| 111 ASSERT_EQ(2u, testInfo(result)->numberOfRunsForTesting()); | 111 ASSERT_EQ(2u, testInfo(result)->numberOfRunsForTesting()); |
| 112 ASSERT_TRUE( | 112 ASSERT_TRUE( |
| 113 testInfo(result)->runInfoForTesting(0, startIndex, numGlyphs, script)); | 113 testInfo(result)->runInfoForTesting(0, startIndex, numGlyphs, script)); |
| 114 EXPECT_EQ(0u, startIndex); | 114 EXPECT_EQ(0u, startIndex); |
| 115 EXPECT_EQ(1u, numGlyphs); | 115 EXPECT_EQ(1u, numGlyphs); |
| 116 EXPECT_EQ(HB_SCRIPT_DEVANAGARI, script); | 116 EXPECT_EQ(HB_SCRIPT_DEVANAGARI, script); |
| 117 | 117 |
| 118 ASSERT_TRUE( | 118 ASSERT_TRUE( |
| 119 testInfo(result)->runInfoForTesting(1, startIndex, numGlyphs, script)); | 119 testInfo(result)->runInfoForTesting(1, startIndex, numGlyphs, script)); |
| 120 EXPECT_EQ(3u, startIndex); | 120 EXPECT_EQ(3u, startIndex); |
| 121 EXPECT_EQ(3u, numGlyphs); | 121 EXPECT_EQ(3u, numGlyphs); |
| 122 EXPECT_EQ(HB_SCRIPT_DEVANAGARI, script); | 122 EXPECT_EQ(HB_SCRIPT_DEVANAGARI, script); |
| 123 } | 123 } |
| 124 | 124 |
| 125 TEST_F(HarfBuzzShaperTest, ResolveCandidateRunsDevanagariCommonLatinCommon) { | 125 TEST_F(HarfBuzzShaperTest, ResolveCandidateRunsDevanagariCommonLatinCommon) { |
| 126 UChar devanagariCommonLatinString[] = {0x915, 0x94d, 0x930, 0x20, | 126 UChar devanagariCommonLatinString[] = {0x915, 0x94d, 0x930, 0x20, |
| 127 0x61, 0x62, 0x2E}; | 127 0x61, 0x62, 0x2E}; |
| 128 TextRun devanagariCommonLatin(devanagariCommonLatinString, 7); | 128 TextRun devanagariCommonLatin(devanagariCommonLatinString, 7); |
| 129 HarfBuzzShaper shaper(&font, devanagariCommonLatin); | 129 HarfBuzzShaper shaper(devanagariCommonLatin); |
| 130 RefPtr<ShapeResult> result = shaper.shapeResult(); | 130 RefPtr<ShapeResult> result = shaper.shapeResult(&font); |
| 131 | 131 |
| 132 ASSERT_EQ(3u, testInfo(result)->numberOfRunsForTesting()); | 132 ASSERT_EQ(3u, testInfo(result)->numberOfRunsForTesting()); |
| 133 ASSERT_TRUE( | 133 ASSERT_TRUE( |
| 134 testInfo(result)->runInfoForTesting(0, startIndex, numGlyphs, script)); | 134 testInfo(result)->runInfoForTesting(0, startIndex, numGlyphs, script)); |
| 135 EXPECT_EQ(0u, startIndex); | 135 EXPECT_EQ(0u, startIndex); |
| 136 EXPECT_EQ(1u, numGlyphs); | 136 EXPECT_EQ(1u, numGlyphs); |
| 137 EXPECT_EQ(HB_SCRIPT_DEVANAGARI, script); | 137 EXPECT_EQ(HB_SCRIPT_DEVANAGARI, script); |
| 138 | 138 |
| 139 ASSERT_TRUE( | 139 ASSERT_TRUE( |
| 140 testInfo(result)->runInfoForTesting(1, startIndex, numGlyphs, script)); | 140 testInfo(result)->runInfoForTesting(1, startIndex, numGlyphs, script)); |
| 141 EXPECT_EQ(3u, startIndex); | 141 EXPECT_EQ(3u, startIndex); |
| 142 EXPECT_EQ(1u, numGlyphs); | 142 EXPECT_EQ(1u, numGlyphs); |
| 143 EXPECT_EQ(HB_SCRIPT_DEVANAGARI, script); | 143 EXPECT_EQ(HB_SCRIPT_DEVANAGARI, script); |
| 144 | 144 |
| 145 ASSERT_TRUE( | 145 ASSERT_TRUE( |
| 146 testInfo(result)->runInfoForTesting(2, startIndex, numGlyphs, script)); | 146 testInfo(result)->runInfoForTesting(2, startIndex, numGlyphs, script)); |
| 147 EXPECT_EQ(4u, startIndex); | 147 EXPECT_EQ(4u, startIndex); |
| 148 EXPECT_EQ(3u, numGlyphs); | 148 EXPECT_EQ(3u, numGlyphs); |
| 149 EXPECT_EQ(HB_SCRIPT_LATIN, script); | 149 EXPECT_EQ(HB_SCRIPT_LATIN, script); |
| 150 } | 150 } |
| 151 | 151 |
| 152 TEST_F(HarfBuzzShaperTest, ResolveCandidateRunsArabicThaiHanLatin) { | 152 TEST_F(HarfBuzzShaperTest, ResolveCandidateRunsArabicThaiHanLatin) { |
| 153 UChar mixedString[] = {0x628, 0x64A, 0x629, 0xE20, 0x65E5, 0x62}; | 153 UChar mixedString[] = {0x628, 0x64A, 0x629, 0xE20, 0x65E5, 0x62}; |
| 154 TextRun mixed(mixedString, 6); | 154 TextRun mixed(mixedString, 6); |
| 155 HarfBuzzShaper shaper(&font, mixed); | 155 HarfBuzzShaper shaper(mixed); |
| 156 RefPtr<ShapeResult> result = shaper.shapeResult(); | 156 RefPtr<ShapeResult> result = shaper.shapeResult(&font); |
| 157 | 157 |
| 158 ASSERT_EQ(4u, testInfo(result)->numberOfRunsForTesting()); | 158 ASSERT_EQ(4u, testInfo(result)->numberOfRunsForTesting()); |
| 159 ASSERT_TRUE( | 159 ASSERT_TRUE( |
| 160 testInfo(result)->runInfoForTesting(0, startIndex, numGlyphs, script)); | 160 testInfo(result)->runInfoForTesting(0, startIndex, numGlyphs, script)); |
| 161 EXPECT_EQ(0u, startIndex); | 161 EXPECT_EQ(0u, startIndex); |
| 162 EXPECT_EQ(3u, numGlyphs); | 162 EXPECT_EQ(3u, numGlyphs); |
| 163 EXPECT_EQ(HB_SCRIPT_ARABIC, script); | 163 EXPECT_EQ(HB_SCRIPT_ARABIC, script); |
| 164 | 164 |
| 165 ASSERT_TRUE( | 165 ASSERT_TRUE( |
| 166 testInfo(result)->runInfoForTesting(1, startIndex, numGlyphs, script)); | 166 testInfo(result)->runInfoForTesting(1, startIndex, numGlyphs, script)); |
| 167 EXPECT_EQ(3u, startIndex); | 167 EXPECT_EQ(3u, startIndex); |
| 168 EXPECT_EQ(1u, numGlyphs); | 168 EXPECT_EQ(1u, numGlyphs); |
| 169 EXPECT_EQ(HB_SCRIPT_THAI, script); | 169 EXPECT_EQ(HB_SCRIPT_THAI, script); |
| 170 | 170 |
| 171 ASSERT_TRUE( | 171 ASSERT_TRUE( |
| 172 testInfo(result)->runInfoForTesting(2, startIndex, numGlyphs, script)); | 172 testInfo(result)->runInfoForTesting(2, startIndex, numGlyphs, script)); |
| 173 EXPECT_EQ(4u, startIndex); | 173 EXPECT_EQ(4u, startIndex); |
| 174 EXPECT_EQ(1u, numGlyphs); | 174 EXPECT_EQ(1u, numGlyphs); |
| 175 EXPECT_EQ(HB_SCRIPT_HAN, script); | 175 EXPECT_EQ(HB_SCRIPT_HAN, script); |
| 176 | 176 |
| 177 ASSERT_TRUE( | 177 ASSERT_TRUE( |
| 178 testInfo(result)->runInfoForTesting(3, startIndex, numGlyphs, script)); | 178 testInfo(result)->runInfoForTesting(3, startIndex, numGlyphs, script)); |
| 179 EXPECT_EQ(5u, startIndex); | 179 EXPECT_EQ(5u, startIndex); |
| 180 EXPECT_EQ(1u, numGlyphs); | 180 EXPECT_EQ(1u, numGlyphs); |
| 181 EXPECT_EQ(HB_SCRIPT_LATIN, script); | 181 EXPECT_EQ(HB_SCRIPT_LATIN, script); |
| 182 } | 182 } |
| 183 | 183 |
| 184 TEST_F(HarfBuzzShaperTest, ResolveCandidateRunsArabicThaiHanLatinTwice) { |
| 185 UChar mixedString[] = {0x628, 0x64A, 0x629, 0xE20, 0x65E5, 0x62}; |
| 186 TextRun mixed(mixedString, 6); |
| 187 HarfBuzzShaper shaper(mixed); |
| 188 RefPtr<ShapeResult> result = shaper.shapeResult(&font); |
| 189 ASSERT_EQ(4u, testInfo(result)->numberOfRunsForTesting()); |
| 190 |
| 191 // Shape again on the same shape object and check the number of runs. |
| 192 // Should be equal if no state was retained between shape calls. |
| 193 RefPtr<ShapeResult> result2 = shaper.shapeResult(&font); |
| 194 ASSERT_EQ(4u, testInfo(result2)->numberOfRunsForTesting()); |
| 195 } |
| 196 |
| 184 TEST_F(HarfBuzzShaperTest, ResolveCandidateRunsArabic) { | 197 TEST_F(HarfBuzzShaperTest, ResolveCandidateRunsArabic) { |
| 185 UChar arabicString[] = {0x628, 0x64A, 0x629}; | 198 UChar arabicString[] = {0x628, 0x64A, 0x629}; |
| 186 TextRun arabic(arabicString, 3); | 199 TextRun arabic(arabicString, 3); |
| 187 HarfBuzzShaper shaper(&font, arabic); | 200 HarfBuzzShaper shaper(arabic); |
| 188 RefPtr<ShapeResult> result = shaper.shapeResult(); | 201 RefPtr<ShapeResult> result = shaper.shapeResult(&font); |
| 189 | 202 |
| 190 ASSERT_EQ(1u, testInfo(result)->numberOfRunsForTesting()); | 203 ASSERT_EQ(1u, testInfo(result)->numberOfRunsForTesting()); |
| 191 ASSERT_TRUE( | 204 ASSERT_TRUE( |
| 192 testInfo(result)->runInfoForTesting(0, startIndex, numGlyphs, script)); | 205 testInfo(result)->runInfoForTesting(0, startIndex, numGlyphs, script)); |
| 193 EXPECT_EQ(0u, startIndex); | 206 EXPECT_EQ(0u, startIndex); |
| 194 EXPECT_EQ(3u, numGlyphs); | 207 EXPECT_EQ(3u, numGlyphs); |
| 195 EXPECT_EQ(HB_SCRIPT_ARABIC, script); | 208 EXPECT_EQ(HB_SCRIPT_ARABIC, script); |
| 196 } | 209 } |
| 197 | 210 |
| 198 } // namespace blink | 211 } // namespace blink |
| OLD | NEW |