Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: ui/gfx/render_text_unittest.cc

Issue 2379033004: Optimize some code in RenderTextHarfbuzz. (Closed)
Patch Set: Nits & git cl format Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gfx/render_text_harfbuzz.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "ui/gfx/render_text.h" 5 #include "ui/gfx/render_text.h"
6 6
7 #include <limits.h> 7 #include <limits.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
(...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 GetRenderTextHarfBuzz()->set_glyph_width_for_test(test_width); 416 GetRenderTextHarfBuzz()->set_glyph_width_for_test(test_width);
417 } 417 }
418 418
419 bool ShapeRunWithFont(const base::string16& text, 419 bool ShapeRunWithFont(const base::string16& text,
420 const Font& font, 420 const Font& font,
421 const FontRenderParams& params, 421 const FontRenderParams& params,
422 internal::TextRunHarfBuzz* run) { 422 internal::TextRunHarfBuzz* run) {
423 return GetRenderTextHarfBuzz()->ShapeRunWithFont(text, font, params, run); 423 return GetRenderTextHarfBuzz()->ShapeRunWithFont(text, font, params, run);
424 } 424 }
425 425
426 base::i18n::BreakIterator* GetGraphemeIterator() {
427 return GetRenderTextHarfBuzz()->GetGraphemeIterator();
428 }
429
430 private: 426 private:
431 DISALLOW_COPY_AND_ASSIGN(RenderTextHarfBuzzTest); 427 DISALLOW_COPY_AND_ASSIGN(RenderTextHarfBuzzTest);
432 }; 428 };
433 429
434 #if defined(OS_MACOSX) 430 #if defined(OS_MACOSX)
435 // Test fixture class. Use for tests which are only to be run for RenderTextMac. 431 // Test fixture class. Use for tests which are only to be run for RenderTextMac.
436 class RenderTextMacTest : public RenderTextTest { 432 class RenderTextMacTest : public RenderTextTest {
437 public: 433 public:
438 RenderTextMacTest() {} 434 RenderTextMacTest() {}
439 435
(...skipping 2771 matching lines...) Expand 10 before | Expand all | Expand 10 after
3211 for (size_t i = 0; i < arraysize(cases); ++i) { 3207 for (size_t i = 0; i < arraysize(cases); ++i) {
3212 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS, i)); 3208 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS, i));
3213 3209
3214 base::string16 text = WideToUTF16(cases[i]); 3210 base::string16 text = WideToUTF16(cases[i]);
3215 render_text->SetText(text); 3211 render_text->SetText(text);
3216 test_api()->EnsureLayout(); 3212 test_api()->EnsureLayout();
3217 internal::TextRunList* run_list = GetHarfBuzzRunList(); 3213 internal::TextRunList* run_list = GetHarfBuzzRunList();
3218 ASSERT_EQ(1U, run_list->size()); 3214 ASSERT_EQ(1U, run_list->size());
3219 internal::TextRunHarfBuzz* run = run_list->runs()[0]; 3215 internal::TextRunHarfBuzz* run = run_list->runs()[0];
3220 3216
3221 base::i18n::BreakIterator* iter = GetGraphemeIterator(); 3217 auto first_grapheme_bounds = run->GetGraphemeBounds(render_text, 0);
3222 auto first_grapheme_bounds = run->GetGraphemeBounds(iter, 0); 3218 EXPECT_EQ(first_grapheme_bounds, run->GetGraphemeBounds(render_text, 1));
3223 EXPECT_EQ(first_grapheme_bounds, run->GetGraphemeBounds(iter, 1)); 3219 auto second_grapheme_bounds = run->GetGraphemeBounds(render_text, 2);
3224 auto second_grapheme_bounds = run->GetGraphemeBounds(iter, 2);
3225 EXPECT_EQ(first_grapheme_bounds.end(), second_grapheme_bounds.start()); 3220 EXPECT_EQ(first_grapheme_bounds.end(), second_grapheme_bounds.start());
3226 } 3221 }
3227 } 3222 }
3228 3223
3229 // Test the partition of a multi-grapheme cluster into grapheme ranges. 3224 // Test the partition of a multi-grapheme cluster into grapheme ranges.
3230 TEST_P(RenderTextHarfBuzzTest, HarfBuzz_SubglyphGraphemePartition) { 3225 TEST_P(RenderTextHarfBuzzTest, HarfBuzz_SubglyphGraphemePartition) {
3231 struct { 3226 struct {
3232 uint32_t glyph_to_char[2]; 3227 uint32_t glyph_to_char[2];
3233 Range bounds[4]; 3228 Range bounds[4];
3234 bool is_rtl; 3229 bool is_rtl;
(...skipping 20 matching lines...) Expand all
3255 }, 3250 },
3256 }; 3251 };
3257 3252
3258 internal::TextRunHarfBuzz run((Font())); 3253 internal::TextRunHarfBuzz run((Font()));
3259 run.range = Range(0, 4); 3254 run.range = Range(0, 4);
3260 run.glyph_count = 2; 3255 run.glyph_count = 2;
3261 run.glyph_to_char.resize(2); 3256 run.glyph_to_char.resize(2);
3262 run.positions.reset(new SkPoint[4]); 3257 run.positions.reset(new SkPoint[4]);
3263 run.width = 20; 3258 run.width = 20;
3264 3259
3265 const base::string16 kString = ASCIIToUTF16("abcd"); 3260 RenderTextHarfBuzz* render_text = GetRenderTextHarfBuzz();
3266 std::unique_ptr<base::i18n::BreakIterator> iter(new base::i18n::BreakIterator( 3261 render_text->SetText(ASCIIToUTF16("abcd"));
3267 kString, base::i18n::BreakIterator::BREAK_CHARACTER));
3268 ASSERT_TRUE(iter->Init());
3269 3262
3270 for (size_t i = 0; i < arraysize(cases); ++i) { 3263 for (size_t i = 0; i < arraysize(cases); ++i) {
3271 std::copy(cases[i].glyph_to_char, cases[i].glyph_to_char + 2, 3264 std::copy(cases[i].glyph_to_char, cases[i].glyph_to_char + 2,
3272 run.glyph_to_char.begin()); 3265 run.glyph_to_char.begin());
3273 run.is_rtl = cases[i].is_rtl; 3266 run.is_rtl = cases[i].is_rtl;
3274 for (int j = 0; j < 2; ++j) 3267 for (int j = 0; j < 2; ++j)
3275 run.positions[j].set(j * 10, 0); 3268 run.positions[j].set(j * 10, 0);
3276 3269
3277 for (size_t j = 0; j < 4; ++j) { 3270 for (size_t j = 0; j < 4; ++j) {
3278 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS ", char %" PRIuS, i, j)); 3271 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS ", char %" PRIuS, i, j));
3279 EXPECT_EQ(cases[i].bounds[j], 3272 EXPECT_EQ(cases[i].bounds[j],
3280 run.GetGraphemeBounds(iter.get(), j).Round()); 3273 run.GetGraphemeBounds(render_text, j).Round());
3281 } 3274 }
3282 } 3275 }
3283 } 3276 }
3284 3277
3285 TEST_P(RenderTextHarfBuzzTest, HarfBuzz_RunDirection) { 3278 TEST_P(RenderTextHarfBuzzTest, HarfBuzz_RunDirection) {
3286 RenderTextHarfBuzz* render_text = GetRenderTextHarfBuzz(); 3279 RenderTextHarfBuzz* render_text = GetRenderTextHarfBuzz();
3287 const base::string16 mixed = WideToUTF16( 3280 const base::string16 mixed = WideToUTF16(
3288 L"\x05D0\x05D1" 3281 L"\x05D0\x05D1"
3289 L"1234" 3282 L"1234"
3290 L"\x05D2\x05D3" 3283 L"\x05D2\x05D3"
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
3399 internal::TextRunList* run_list = GetHarfBuzzRunList(); 3392 internal::TextRunList* run_list = GetHarfBuzzRunList();
3400 ASSERT_EQ(1U, run_list->size()); 3393 ASSERT_EQ(1U, run_list->size());
3401 internal::TextRunHarfBuzz* run = run_list->runs()[0]; 3394 internal::TextRunHarfBuzz* run = run_list->runs()[0];
3402 ShapeRunWithFont(render_text->text(), Font("TheFontThatDoesntExist", 13), 3395 ShapeRunWithFont(render_text->text(), Font("TheFontThatDoesntExist", 13),
3403 FontRenderParams(), run); 3396 FontRenderParams(), run);
3404 } 3397 }
3405 3398
3406 // Ensure an empty run returns sane values to queries. 3399 // Ensure an empty run returns sane values to queries.
3407 TEST_P(RenderTextHarfBuzzTest, HarfBuzz_EmptyRun) { 3400 TEST_P(RenderTextHarfBuzzTest, HarfBuzz_EmptyRun) {
3408 internal::TextRunHarfBuzz run((Font())); 3401 internal::TextRunHarfBuzz run((Font()));
3409 const base::string16 kString = ASCIIToUTF16("abcdefgh"); 3402 RenderTextHarfBuzz* render_text = GetRenderTextHarfBuzz();
3410 std::unique_ptr<base::i18n::BreakIterator> iter(new base::i18n::BreakIterator( 3403 render_text->SetText(ASCIIToUTF16("abcdefgh"));
3411 kString, base::i18n::BreakIterator::BREAK_CHARACTER));
3412 ASSERT_TRUE(iter->Init());
3413 3404
3414 run.range = Range(3, 8); 3405 run.range = Range(3, 8);
3415 run.glyph_count = 0; 3406 run.glyph_count = 0;
3416 EXPECT_EQ(Range(0, 0), run.CharRangeToGlyphRange(Range(4, 5))); 3407 EXPECT_EQ(Range(0, 0), run.CharRangeToGlyphRange(Range(4, 5)));
3417 EXPECT_EQ(Range(0, 0), run.GetGraphemeBounds(iter.get(), 4).Round()); 3408 EXPECT_EQ(Range(0, 0), run.GetGraphemeBounds(render_text, 4).Round());
3418 Range chars; 3409 Range chars;
3419 Range glyphs; 3410 Range glyphs;
3420 run.GetClusterAt(4, &chars, &glyphs); 3411 run.GetClusterAt(4, &chars, &glyphs);
3421 EXPECT_EQ(Range(3, 8), chars); 3412 EXPECT_EQ(Range(3, 8), chars);
3422 EXPECT_EQ(Range(0, 0), glyphs); 3413 EXPECT_EQ(Range(0, 0), glyphs);
3423 } 3414 }
3424 3415
3425 // Ensure the line breaker doesn't compute the word's width bigger than the 3416 // Ensure the line breaker doesn't compute the word's width bigger than the
3426 // actual size. See http://crbug.com/470073 3417 // actual size. See http://crbug.com/470073
3427 TEST_P(RenderTextHarfBuzzTest, HarfBuzz_WordWidthWithDiacritics) { 3418 TEST_P(RenderTextHarfBuzzTest, HarfBuzz_WordWidthWithDiacritics) {
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after
3804 ::testing::Values(RENDER_TEXT_HARFBUZZ), 3795 ::testing::Values(RENDER_TEXT_HARFBUZZ),
3805 PrintRenderTextBackend()); 3796 PrintRenderTextBackend());
3806 #endif 3797 #endif
3807 3798
3808 INSTANTIATE_TEST_CASE_P(, 3799 INSTANTIATE_TEST_CASE_P(,
3809 RenderTextHarfBuzzTest, 3800 RenderTextHarfBuzzTest,
3810 ::testing::Values(RENDER_TEXT_HARFBUZZ), 3801 ::testing::Values(RENDER_TEXT_HARFBUZZ),
3811 PrintRenderTextBackend()); 3802 PrintRenderTextBackend());
3812 3803
3813 } // namespace gfx 3804 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/render_text_harfbuzz.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698