OLD | NEW |
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 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 render_text->SetText(WideToUTF16(cases[i])); | 254 render_text->SetText(WideToUTF16(cases[i])); |
255 } | 255 } |
256 } | 256 } |
257 | 257 |
258 TEST_F(RenderTextTest, SetStyles) { | 258 TEST_F(RenderTextTest, SetStyles) { |
259 // Ensure custom default styles persist across setting and clearing text. | 259 // Ensure custom default styles persist across setting and clearing text. |
260 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 260 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
261 const SkColor color = SK_ColorRED; | 261 const SkColor color = SK_ColorRED; |
262 render_text->SetColor(color); | 262 render_text->SetColor(color); |
263 render_text->SetBaselineStyle(SUPERSCRIPT); | 263 render_text->SetBaselineStyle(SUPERSCRIPT); |
264 render_text->SetStyle(BOLD, true); | 264 render_text->SetWeight(gfx::Font::Weight::BOLD); |
265 render_text->SetStyle(UNDERLINE, false); | 265 render_text->SetStyle(UNDERLINE, false); |
266 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" }; | 266 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" }; |
267 for (size_t i = 0; i < arraysize(cases); ++i) { | 267 for (size_t i = 0; i < arraysize(cases); ++i) { |
268 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(color)); | 268 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(color)); |
269 EXPECT_TRUE(render_text->baselines().EqualsValueForTesting(SUPERSCRIPT)); | 269 EXPECT_TRUE(render_text->baselines().EqualsValueForTesting(SUPERSCRIPT)); |
270 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(true)); | 270 EXPECT_TRUE( |
| 271 render_text->weights().EqualsValueForTesting(gfx::Font::Weight::BOLD)); |
271 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsValueForTesting(false)); | 272 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsValueForTesting(false)); |
272 render_text->SetText(WideToUTF16(cases[i])); | 273 render_text->SetText(WideToUTF16(cases[i])); |
273 | 274 |
274 // Ensure custom default styles can be applied after text has been set. | 275 // Ensure custom default styles can be applied after text has been set. |
275 if (i == 1) | 276 if (i == 1) |
276 render_text->SetStyle(STRIKE, true); | 277 render_text->SetStyle(STRIKE, true); |
277 if (i >= 1) | 278 if (i >= 1) |
278 EXPECT_TRUE(render_text->styles()[STRIKE].EqualsValueForTesting(true)); | 279 EXPECT_TRUE(render_text->styles()[STRIKE].EqualsValueForTesting(true)); |
279 } | 280 } |
280 } | 281 } |
281 | 282 |
282 TEST_F(RenderTextTest, ApplyStyles) { | 283 TEST_F(RenderTextTest, ApplyStyles) { |
283 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 284 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
284 render_text->SetText(ASCIIToUTF16("012345678")); | 285 render_text->SetText(ASCIIToUTF16("012345678")); |
285 | 286 |
286 // Apply a ranged color and style and check the resulting breaks. | 287 // Apply a ranged color and style and check the resulting breaks. |
287 render_text->ApplyColor(SK_ColorRED, Range(1, 4)); | 288 render_text->ApplyColor(SK_ColorRED, Range(1, 4)); |
288 render_text->ApplyBaselineStyle(SUPERIOR, Range(2, 4)); | 289 render_text->ApplyBaselineStyle(SUPERIOR, Range(2, 4)); |
289 render_text->ApplyStyle(BOLD, true, Range(2, 5)); | 290 render_text->ApplyWeight(gfx::Font::Weight::BOLD, Range(2, 5)); |
290 std::vector<std::pair<size_t, SkColor> > expected_color; | 291 std::vector<std::pair<size_t, SkColor> > expected_color; |
291 expected_color.push_back(std::pair<size_t, SkColor>(0, SK_ColorBLACK)); | 292 expected_color.push_back(std::pair<size_t, SkColor>(0, SK_ColorBLACK)); |
292 expected_color.push_back(std::pair<size_t, SkColor>(1, SK_ColorRED)); | 293 expected_color.push_back(std::pair<size_t, SkColor>(1, SK_ColorRED)); |
293 expected_color.push_back(std::pair<size_t, SkColor>(4, SK_ColorBLACK)); | 294 expected_color.push_back(std::pair<size_t, SkColor>(4, SK_ColorBLACK)); |
294 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color)); | 295 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color)); |
295 std::vector<std::pair<size_t, BaselineStyle>> expected_baseline_style; | 296 std::vector<std::pair<size_t, BaselineStyle>> expected_baseline_style; |
296 expected_baseline_style.push_back( | 297 expected_baseline_style.push_back( |
297 std::pair<size_t, BaselineStyle>(0, NORMAL_BASELINE)); | 298 std::pair<size_t, BaselineStyle>(0, NORMAL_BASELINE)); |
298 expected_baseline_style.push_back( | 299 expected_baseline_style.push_back( |
299 std::pair<size_t, BaselineStyle>(2, SUPERIOR)); | 300 std::pair<size_t, BaselineStyle>(2, SUPERIOR)); |
300 expected_baseline_style.push_back( | 301 expected_baseline_style.push_back( |
301 std::pair<size_t, BaselineStyle>(4, NORMAL_BASELINE)); | 302 std::pair<size_t, BaselineStyle>(4, NORMAL_BASELINE)); |
302 EXPECT_TRUE( | 303 EXPECT_TRUE( |
303 render_text->baselines().EqualsForTesting(expected_baseline_style)); | 304 render_text->baselines().EqualsForTesting(expected_baseline_style)); |
304 std::vector<std::pair<size_t, bool> > expected_style; | 305 std::vector<std::pair<size_t, gfx::Font::Weight>> expected_weight; |
305 expected_style.push_back(std::pair<size_t, bool>(0, false)); | 306 expected_weight.push_back( |
306 expected_style.push_back(std::pair<size_t, bool>(2, true)); | 307 std::pair<size_t, gfx::Font::Weight>(0, gfx::Font::Weight::NORMAL)); |
307 expected_style.push_back(std::pair<size_t, bool>(5, false)); | 308 expected_weight.push_back( |
308 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style)); | 309 std::pair<size_t, gfx::Font::Weight>(2, gfx::Font::Weight::BOLD)); |
| 310 expected_weight.push_back( |
| 311 std::pair<size_t, gfx::Font::Weight>(5, gfx::Font::Weight::NORMAL)); |
| 312 EXPECT_TRUE(render_text->weights().EqualsForTesting(expected_weight)); |
309 | 313 |
310 // Ensure that setting a value overrides the ranged values. | 314 // Ensure that setting a value overrides the ranged values. |
311 render_text->SetColor(SK_ColorBLUE); | 315 render_text->SetColor(SK_ColorBLUE); |
312 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLUE)); | 316 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLUE)); |
313 render_text->SetBaselineStyle(SUBSCRIPT); | 317 render_text->SetBaselineStyle(SUBSCRIPT); |
314 EXPECT_TRUE(render_text->baselines().EqualsValueForTesting(SUBSCRIPT)); | 318 EXPECT_TRUE(render_text->baselines().EqualsValueForTesting(SUBSCRIPT)); |
315 render_text->SetStyle(BOLD, false); | 319 render_text->SetWeight(gfx::Font::Weight::NORMAL); |
316 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(false)); | 320 EXPECT_TRUE( |
| 321 render_text->weights().EqualsValueForTesting(gfx::Font::Weight::NORMAL)); |
317 | 322 |
318 // Apply a value over the text end and check the resulting breaks (INT_MAX | 323 // Apply a value over the text end and check the resulting breaks (INT_MAX |
319 // should be used instead of the text length for the range end) | 324 // should be used instead of the text length for the range end) |
320 const size_t text_length = render_text->text().length(); | 325 const size_t text_length = render_text->text().length(); |
321 render_text->ApplyColor(SK_ColorRED, Range(0, text_length)); | 326 render_text->ApplyColor(SK_ColorRED, Range(0, text_length)); |
322 render_text->ApplyBaselineStyle(SUPERIOR, Range(0, text_length)); | 327 render_text->ApplyBaselineStyle(SUPERIOR, Range(0, text_length)); |
323 render_text->ApplyStyle(BOLD, true, Range(2, text_length)); | 328 render_text->ApplyWeight(gfx::Font::Weight::BOLD, Range(2, text_length)); |
324 std::vector<std::pair<size_t, SkColor> > expected_color_end; | 329 std::vector<std::pair<size_t, SkColor> > expected_color_end; |
325 expected_color_end.push_back(std::pair<size_t, SkColor>(0, SK_ColorRED)); | 330 expected_color_end.push_back(std::pair<size_t, SkColor>(0, SK_ColorRED)); |
326 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color_end)); | 331 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color_end)); |
327 std::vector<std::pair<size_t, BaselineStyle>> expected_baseline_end; | 332 std::vector<std::pair<size_t, BaselineStyle>> expected_baseline_end; |
328 expected_baseline_end.push_back( | 333 expected_baseline_end.push_back( |
329 std::pair<size_t, BaselineStyle>(0, SUPERIOR)); | 334 std::pair<size_t, BaselineStyle>(0, SUPERIOR)); |
330 EXPECT_TRUE(render_text->baselines().EqualsForTesting(expected_baseline_end)); | 335 EXPECT_TRUE(render_text->baselines().EqualsForTesting(expected_baseline_end)); |
331 std::vector<std::pair<size_t, bool> > expected_style_end; | 336 std::vector<std::pair<size_t, gfx::Font::Weight>> expected_weight_end; |
332 expected_style_end.push_back(std::pair<size_t, bool>(0, false)); | 337 expected_weight_end.push_back( |
333 expected_style_end.push_back(std::pair<size_t, bool>(2, true)); | 338 std::pair<size_t, gfx::Font::Weight>(0, gfx::Font::Weight::NORMAL)); |
334 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style_end)); | 339 expected_weight_end.push_back( |
| 340 std::pair<size_t, gfx::Font::Weight>(2, gfx::Font::Weight::BOLD)); |
| 341 EXPECT_TRUE(render_text->weights().EqualsForTesting(expected_weight_end)); |
335 | 342 |
336 // Ensure ranged values adjust to accommodate text length changes. | 343 // Ensure ranged values adjust to accommodate text length changes. |
337 render_text->ApplyStyle(ITALIC, true, Range(0, 2)); | 344 render_text->ApplyStyle(ITALIC, true, Range(0, 2)); |
338 render_text->ApplyStyle(ITALIC, true, Range(3, 6)); | 345 render_text->ApplyStyle(ITALIC, true, Range(3, 6)); |
339 render_text->ApplyStyle(ITALIC, true, Range(7, text_length)); | 346 render_text->ApplyStyle(ITALIC, true, Range(7, text_length)); |
340 std::vector<std::pair<size_t, bool> > expected_italic; | 347 std::vector<std::pair<size_t, bool> > expected_italic; |
341 expected_italic.push_back(std::pair<size_t, bool>(0, true)); | 348 expected_italic.push_back(std::pair<size_t, bool>(0, true)); |
342 expected_italic.push_back(std::pair<size_t, bool>(2, false)); | 349 expected_italic.push_back(std::pair<size_t, bool>(2, false)); |
343 expected_italic.push_back(std::pair<size_t, bool>(3, true)); | 350 expected_italic.push_back(std::pair<size_t, bool>(3, true)); |
344 expected_italic.push_back(std::pair<size_t, bool>(6, false)); | 351 expected_italic.push_back(std::pair<size_t, bool>(6, false)); |
(...skipping 1270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1615 } | 1622 } |
1616 | 1623 |
1617 TEST_F(RenderTextTest, StringSizeBoldWidth) { | 1624 TEST_F(RenderTextTest, StringSizeBoldWidth) { |
1618 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 1625 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
1619 render_text->SetText(UTF8ToUTF16("Hello World")); | 1626 render_text->SetText(UTF8ToUTF16("Hello World")); |
1620 | 1627 |
1621 const int plain_width = render_text->GetStringSize().width(); | 1628 const int plain_width = render_text->GetStringSize().width(); |
1622 EXPECT_GT(plain_width, 0); | 1629 EXPECT_GT(plain_width, 0); |
1623 | 1630 |
1624 // Apply a bold style and check that the new width is greater. | 1631 // Apply a bold style and check that the new width is greater. |
1625 render_text->SetStyle(BOLD, true); | 1632 render_text->SetWeight(gfx::Font::Weight::BOLD); |
1626 const int bold_width = render_text->GetStringSize().width(); | 1633 const int bold_width = render_text->GetStringSize().width(); |
1627 EXPECT_GT(bold_width, plain_width); | 1634 EXPECT_GT(bold_width, plain_width); |
1628 | 1635 |
1629 // Now, apply a plain style over the first word only. | 1636 // Now, apply a plain style over the first word only. |
1630 render_text->ApplyStyle(BOLD, false, Range(0, 5)); | 1637 render_text->ApplyWeight(gfx::Font::Weight::NORMAL, Range(0, 5)); |
1631 const int plain_bold_width = render_text->GetStringSize().width(); | 1638 const int plain_bold_width = render_text->GetStringSize().width(); |
1632 EXPECT_GT(plain_bold_width, plain_width); | 1639 EXPECT_GT(plain_bold_width, plain_width); |
1633 EXPECT_LT(plain_bold_width, bold_width); | 1640 EXPECT_LT(plain_bold_width, bold_width); |
1634 } | 1641 } |
1635 | 1642 |
1636 TEST_F(RenderTextTest, StringSizeHeight) { | 1643 TEST_F(RenderTextTest, StringSizeHeight) { |
1637 base::string16 cases[] = { | 1644 base::string16 cases[] = { |
1638 WideToUTF16(L"Hello World!"), // English | 1645 WideToUTF16(L"Hello World!"), // English |
1639 WideToUTF16(L"\x6328\x62f6"), // Japanese | 1646 WideToUTF16(L"\x6328\x62f6"), // Japanese |
1640 WideToUTF16(L"\x0915\x093f"), // Hindi | 1647 WideToUTF16(L"\x0915\x093f"), // Hindi |
(...skipping 1276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2917 EXPECT_NEAR(text_size.width(), render_text.GetStringSizeF().width(), 1.0f); | 2924 EXPECT_NEAR(text_size.width(), render_text.GetStringSizeF().width(), 1.0f); |
2918 EXPECT_EQ(text_size.height() * 2, render_text.GetStringSizeF().height()); | 2925 EXPECT_EQ(text_size.height() * 2, render_text.GetStringSizeF().height()); |
2919 } | 2926 } |
2920 | 2927 |
2921 // Ensure a string fits in a display rect with a width equal to the string's. | 2928 // Ensure a string fits in a display rect with a width equal to the string's. |
2922 TEST_F(RenderTextTest, StringFitsOwnWidth) { | 2929 TEST_F(RenderTextTest, StringFitsOwnWidth) { |
2923 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 2930 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
2924 const base::string16 kString = ASCIIToUTF16("www.example.com"); | 2931 const base::string16 kString = ASCIIToUTF16("www.example.com"); |
2925 | 2932 |
2926 render_text->SetText(kString); | 2933 render_text->SetText(kString); |
2927 render_text->ApplyStyle(BOLD, true, Range(0, 3)); | 2934 render_text->ApplyWeight(gfx::Font::Weight::BOLD, Range(0, 3)); |
2928 render_text->SetElideBehavior(ELIDE_TAIL); | 2935 render_text->SetElideBehavior(ELIDE_TAIL); |
2929 | 2936 |
2930 render_text->SetDisplayRect(Rect(0, 0, 500, 100)); | 2937 render_text->SetDisplayRect(Rect(0, 0, 500, 100)); |
2931 EXPECT_EQ(kString, render_text->GetDisplayText()); | 2938 EXPECT_EQ(kString, render_text->GetDisplayText()); |
2932 render_text->SetDisplayRect(Rect(0, 0, render_text->GetContentWidth(), 100)); | 2939 render_text->SetDisplayRect(Rect(0, 0, render_text->GetContentWidth(), 100)); |
2933 EXPECT_EQ(kString, render_text->GetDisplayText()); | 2940 EXPECT_EQ(kString, render_text->GetDisplayText()); |
2934 } | 2941 } |
2935 | 2942 |
2936 // TODO(derat): Figure out why this fails on Windows: http://crbug.com/427184 | 2943 // TODO(derat): Figure out why this fails on Windows: http://crbug.com/427184 |
2937 #if !defined(OS_WIN) | 2944 #if !defined(OS_WIN) |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3025 render_text->SetColor(SK_ColorBLACK); | 3032 render_text->SetColor(SK_ColorBLACK); |
3026 | 3033 |
3027 for (auto string : kTestStrings) { | 3034 for (auto string : kTestStrings) { |
3028 surface->getCanvas()->clear(SK_ColorWHITE); | 3035 surface->getCanvas()->clear(SK_ColorWHITE); |
3029 render_text->SetText(WideToUTF16(string)); | 3036 render_text->SetText(WideToUTF16(string)); |
3030 const Size string_size = render_text->GetStringSize(); | 3037 const Size string_size = render_text->GetStringSize(); |
3031 render_text->ApplyBaselineStyle(SUPERSCRIPT, Range(1, 2)); | 3038 render_text->ApplyBaselineStyle(SUPERSCRIPT, Range(1, 2)); |
3032 render_text->ApplyBaselineStyle(SUPERIOR, Range(3, 4)); | 3039 render_text->ApplyBaselineStyle(SUPERIOR, Range(3, 4)); |
3033 render_text->ApplyBaselineStyle(INFERIOR, Range(5, 6)); | 3040 render_text->ApplyBaselineStyle(INFERIOR, Range(5, 6)); |
3034 render_text->ApplyBaselineStyle(SUBSCRIPT, Range(7, 8)); | 3041 render_text->ApplyBaselineStyle(SUBSCRIPT, Range(7, 8)); |
3035 render_text->SetStyle(BOLD, true); | 3042 render_text->SetWeight(gfx::Font::Weight::BOLD); |
3036 render_text->SetDisplayRect( | 3043 render_text->SetDisplayRect( |
3037 Rect(kTestSize, kTestSize, string_size.width(), string_size.height())); | 3044 Rect(kTestSize, kTestSize, string_size.width(), string_size.height())); |
3038 // Allow the RenderText to paint outside of its display rect. | 3045 // Allow the RenderText to paint outside of its display rect. |
3039 render_text->set_clip_to_display_rect(false); | 3046 render_text->set_clip_to_display_rect(false); |
3040 ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width()); | 3047 ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width()); |
3041 | 3048 |
3042 render_text->Draw(&canvas); | 3049 render_text->Draw(&canvas); |
3043 ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width()); | 3050 ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width()); |
3044 const uint32_t* buffer = | 3051 const uint32_t* buffer = |
3045 static_cast<const uint32_t*>(surface->peekPixels(nullptr, nullptr)); | 3052 static_cast<const uint32_t*>(surface->peekPixels(nullptr, nullptr)); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3209 backend[i]->SetColor(SK_ColorRED); | 3216 backend[i]->SetColor(SK_ColorRED); |
3210 test_api.DrawVisualText(&renderer); | 3217 test_api.DrawVisualText(&renderer); |
3211 | 3218 |
3212 renderer.GetTextLogAndReset(&text_log); | 3219 renderer.GetTextLogAndReset(&text_log); |
3213 EXPECT_EQ(1u, text_log.size()); | 3220 EXPECT_EQ(1u, text_log.size()); |
3214 EXPECT_EQ(SK_ColorRED, text_log[0].color); | 3221 EXPECT_EQ(SK_ColorRED, text_log[0].color); |
3215 } | 3222 } |
3216 } | 3223 } |
3217 | 3224 |
3218 } // namespace gfx | 3225 } // namespace gfx |
OLD | NEW |