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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/i18n/break_iterator.h" | 10 #include "base/i18n/break_iterator.h" |
(...skipping 18 matching lines...) Expand all Loading... | |
29 | 29 |
30 using base::ASCIIToUTF16; | 30 using base::ASCIIToUTF16; |
31 using base::UTF8ToUTF16; | 31 using base::UTF8ToUTF16; |
32 using base::WideToUTF16; | 32 using base::WideToUTF16; |
33 using base::WideToUTF8; | 33 using base::WideToUTF8; |
34 | 34 |
35 namespace gfx { | 35 namespace gfx { |
36 | 36 |
37 namespace { | 37 namespace { |
38 | 38 |
39 class TestRectangleBuffer { | |
msw
2015/02/27 01:28:19
nit: move to the end of the namespace, or at least
dschuyler
2015/02/27 03:05:35
Done.
| |
40 public: | |
41 TestRectangleBuffer(const wchar_t* string, | |
42 const SkColor* buffer, | |
43 uint32_t stride, | |
44 uint32_t row_count) | |
45 : string_(string), | |
46 buffer_(buffer), | |
47 stride_(stride), | |
48 row_count_(row_count) {} | |
49 | |
50 void EnsureSolidRect(SkColor color, | |
51 int top, | |
52 int left, | |
53 int width, | |
54 int height) const; | |
55 | |
56 protected: | |
57 const wchar_t* string_; | |
58 const SkColor* buffer_; | |
59 int stride_; | |
60 int row_count_; | |
61 }; | |
msw
2015/02/27 01:28:19
nit: disallow copy and assign
dschuyler
2015/02/27 03:05:35
Done.
| |
62 | |
63 // Test whether any values in the rectangular area are anything other than | |
64 // |color|. | |
65 void TestRectangleBuffer::EnsureSolidRect(SkColor color, | |
msw
2015/02/27 01:28:19
nit: define this inline with the declaration above
dschuyler
2015/02/27 03:05:35
Done.
| |
66 int left, | |
67 int top, | |
68 int width, | |
69 int height) const { | |
70 ASSERT_LT(top, row_count_) << string_; | |
71 ASSERT_LE(top + height, row_count_) << string_; | |
72 ASSERT_LT(left, stride_) << string_; | |
73 ASSERT_LE(left + width, stride_) << string_; | |
74 for (int y = top; y < top + height; ++y) { | |
75 for (int x = left; x < left + width; ++x) { | |
76 SkColor color = buffer_[x + y * stride_]; | |
77 EXPECT_EQ(SK_ColorWHITE, color) << string_ << " at " << x << ", " << y; | |
78 } | |
79 } | |
80 } | |
81 | |
39 // Various weak, LTR, RTL, and Bidi string cases with three characters each. | 82 // Various weak, LTR, RTL, and Bidi string cases with three characters each. |
40 const wchar_t kWeak[] = L" . "; | 83 const wchar_t kWeak[] = L" . "; |
41 const wchar_t kLtr[] = L"abc"; | 84 const wchar_t kLtr[] = L"abc"; |
42 const wchar_t kRtl[] = L"\x5d0\x5d1\x5d2"; | 85 const wchar_t kRtl[] = L"\x5d0\x5d1\x5d2"; |
43 const wchar_t kLtrRtl[] = L"a" L"\x5d0\x5d1"; | 86 const wchar_t kLtrRtl[] = L"a" L"\x5d0\x5d1"; |
44 const wchar_t kLtrRtlLtr[] = L"a" L"\x5d1" L"b"; | 87 const wchar_t kLtrRtlLtr[] = L"a" L"\x5d1" L"b"; |
45 const wchar_t kRtlLtr[] = L"\x5d0\x5d1" L"a"; | 88 const wchar_t kRtlLtr[] = L"\x5d0\x5d1" L"a"; |
46 const wchar_t kRtlLtrRtl[] = L"\x5d0" L"a" L"\x5d1"; | 89 const wchar_t kRtlLtrRtl[] = L"\x5d0" L"a" L"\x5d1"; |
47 | 90 |
48 // Checks whether |range| contains |index|. This is not the same as calling | 91 // Checks whether |range| contains |index|. This is not the same as calling |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
167 class RenderTextTest : public testing::Test { | 210 class RenderTextTest : public testing::Test { |
168 }; | 211 }; |
169 | 212 |
170 TEST_F(RenderTextTest, DefaultStyle) { | 213 TEST_F(RenderTextTest, DefaultStyle) { |
171 // Check the default styles applied to new instances and adjusted text. | 214 // Check the default styles applied to new instances and adjusted text. |
172 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 215 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
173 EXPECT_TRUE(render_text->text().empty()); | 216 EXPECT_TRUE(render_text->text().empty()); |
174 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" }; | 217 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" }; |
175 for (size_t i = 0; i < arraysize(cases); ++i) { | 218 for (size_t i = 0; i < arraysize(cases); ++i) { |
176 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLACK)); | 219 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLACK)); |
220 EXPECT_TRUE( | |
221 render_text->baselines().EqualsValueForTesting(NORMAL_BASELINE)); | |
177 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style) | 222 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style) |
178 EXPECT_TRUE(render_text->styles()[style].EqualsValueForTesting(false)); | 223 EXPECT_TRUE(render_text->styles()[style].EqualsValueForTesting(false)); |
179 render_text->SetText(WideToUTF16(cases[i])); | 224 render_text->SetText(WideToUTF16(cases[i])); |
180 } | 225 } |
181 } | 226 } |
182 | 227 |
183 TEST_F(RenderTextTest, SetColorAndStyle) { | 228 TEST_F(RenderTextTest, SetColorAndStyle) { |
184 // Ensure custom default styles persist across setting and clearing text. | 229 // Ensure custom default styles persist across setting and clearing text. |
185 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 230 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
186 const SkColor color = SK_ColorRED; | 231 const SkColor color = SK_ColorRED; |
187 render_text->SetColor(color); | 232 render_text->SetColor(color); |
233 render_text->SetBaselineStyle(SUPERSCRIPT); | |
188 render_text->SetStyle(BOLD, true); | 234 render_text->SetStyle(BOLD, true); |
189 render_text->SetStyle(UNDERLINE, false); | 235 render_text->SetStyle(UNDERLINE, false); |
190 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" }; | 236 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" }; |
191 for (size_t i = 0; i < arraysize(cases); ++i) { | 237 for (size_t i = 0; i < arraysize(cases); ++i) { |
192 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(color)); | 238 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(color)); |
239 EXPECT_TRUE(render_text->baselines().EqualsValueForTesting(SUPERSCRIPT)); | |
193 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(true)); | 240 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(true)); |
194 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsValueForTesting(false)); | 241 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsValueForTesting(false)); |
195 render_text->SetText(WideToUTF16(cases[i])); | 242 render_text->SetText(WideToUTF16(cases[i])); |
196 | 243 |
197 // Ensure custom default styles can be applied after text has been set. | 244 // Ensure custom default styles can be applied after text has been set. |
198 if (i == 1) | 245 if (i == 1) |
199 render_text->SetStyle(STRIKE, true); | 246 render_text->SetStyle(STRIKE, true); |
200 if (i >= 1) | 247 if (i >= 1) |
201 EXPECT_TRUE(render_text->styles()[STRIKE].EqualsValueForTesting(true)); | 248 EXPECT_TRUE(render_text->styles()[STRIKE].EqualsValueForTesting(true)); |
202 } | 249 } |
203 } | 250 } |
204 | 251 |
205 TEST_F(RenderTextTest, ApplyColorAndStyle) { | 252 TEST_F(RenderTextTest, ApplyColorAndStyle) { |
206 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 253 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
207 render_text->SetText(ASCIIToUTF16("012345678")); | 254 render_text->SetText(ASCIIToUTF16("012345678")); |
208 | 255 |
209 // Apply a ranged color and style and check the resulting breaks. | 256 // Apply a ranged color and style and check the resulting breaks. |
210 render_text->ApplyColor(SK_ColorRED, Range(1, 4)); | 257 render_text->ApplyColor(SK_ColorRED, Range(1, 4)); |
258 render_text->ApplyBaselineStyle(SUPERIOR, Range(2, 4)); | |
211 render_text->ApplyStyle(BOLD, true, Range(2, 5)); | 259 render_text->ApplyStyle(BOLD, true, Range(2, 5)); |
212 std::vector<std::pair<size_t, SkColor> > expected_color; | 260 std::vector<std::pair<size_t, SkColor> > expected_color; |
213 expected_color.push_back(std::pair<size_t, SkColor>(0, SK_ColorBLACK)); | 261 expected_color.push_back(std::pair<size_t, SkColor>(0, SK_ColorBLACK)); |
214 expected_color.push_back(std::pair<size_t, SkColor>(1, SK_ColorRED)); | 262 expected_color.push_back(std::pair<size_t, SkColor>(1, SK_ColorRED)); |
215 expected_color.push_back(std::pair<size_t, SkColor>(4, SK_ColorBLACK)); | 263 expected_color.push_back(std::pair<size_t, SkColor>(4, SK_ColorBLACK)); |
216 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color)); | 264 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color)); |
265 std::vector<std::pair<size_t, BaselineStyle>> expected_baseline_style; | |
266 expected_baseline_style.push_back( | |
267 std::pair<size_t, BaselineStyle>(0, NORMAL_BASELINE)); | |
268 expected_baseline_style.push_back( | |
269 std::pair<size_t, BaselineStyle>(2, SUPERIOR)); | |
270 expected_baseline_style.push_back( | |
271 std::pair<size_t, BaselineStyle>(4, NORMAL_BASELINE)); | |
272 EXPECT_TRUE( | |
273 render_text->baselines().EqualsForTesting(expected_baseline_style)); | |
217 std::vector<std::pair<size_t, bool> > expected_style; | 274 std::vector<std::pair<size_t, bool> > expected_style; |
218 expected_style.push_back(std::pair<size_t, bool>(0, false)); | 275 expected_style.push_back(std::pair<size_t, bool>(0, false)); |
219 expected_style.push_back(std::pair<size_t, bool>(2, true)); | 276 expected_style.push_back(std::pair<size_t, bool>(2, true)); |
220 expected_style.push_back(std::pair<size_t, bool>(5, false)); | 277 expected_style.push_back(std::pair<size_t, bool>(5, false)); |
221 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style)); | 278 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style)); |
222 | 279 |
223 // Ensure setting a color and style overrides the ranged colors and styles. | 280 // Ensure setting a color, baseline, and style overrides the ranged colors, |
281 // baseline, and styles. | |
224 render_text->SetColor(SK_ColorBLUE); | 282 render_text->SetColor(SK_ColorBLUE); |
225 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLUE)); | 283 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLUE)); |
284 render_text->SetBaselineStyle(SUBSCRIPT); | |
285 EXPECT_TRUE(render_text->baselines().EqualsValueForTesting(SUBSCRIPT)); | |
226 render_text->SetStyle(BOLD, false); | 286 render_text->SetStyle(BOLD, false); |
227 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(false)); | 287 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(false)); |
228 | 288 |
229 // Apply a color and style over the text end and check the resulting breaks. | 289 // Apply a color, baseline, and style over the text end and check the |
230 // (INT_MAX should be used instead of the text length for the range end) | 290 // resulting breaks (INT_MAX should be used instead of the text length for |
291 // the range end) | |
231 const size_t text_length = render_text->text().length(); | 292 const size_t text_length = render_text->text().length(); |
232 render_text->ApplyColor(SK_ColorRED, Range(0, text_length)); | 293 render_text->ApplyColor(SK_ColorRED, Range(0, text_length)); |
294 render_text->ApplyBaselineStyle(SUPERIOR, Range(0, text_length)); | |
233 render_text->ApplyStyle(BOLD, true, Range(2, text_length)); | 295 render_text->ApplyStyle(BOLD, true, Range(2, text_length)); |
234 std::vector<std::pair<size_t, SkColor> > expected_color_end; | 296 std::vector<std::pair<size_t, SkColor> > expected_color_end; |
235 expected_color_end.push_back(std::pair<size_t, SkColor>(0, SK_ColorRED)); | 297 expected_color_end.push_back(std::pair<size_t, SkColor>(0, SK_ColorRED)); |
236 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color_end)); | 298 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color_end)); |
299 std::vector<std::pair<size_t, BaselineStyle>> expected_baseline_end; | |
300 expected_baseline_end.push_back( | |
301 std::pair<size_t, BaselineStyle>(0, SUPERIOR)); | |
302 EXPECT_TRUE(render_text->baselines().EqualsForTesting(expected_baseline_end)); | |
237 std::vector<std::pair<size_t, bool> > expected_style_end; | 303 std::vector<std::pair<size_t, bool> > expected_style_end; |
238 expected_style_end.push_back(std::pair<size_t, bool>(0, false)); | 304 expected_style_end.push_back(std::pair<size_t, bool>(0, false)); |
239 expected_style_end.push_back(std::pair<size_t, bool>(2, true)); | 305 expected_style_end.push_back(std::pair<size_t, bool>(2, true)); |
240 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style_end)); | 306 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style_end)); |
241 | 307 |
242 // Ensure ranged values adjust to accommodate text length changes. | 308 // Ensure ranged values adjust to accommodate text length changes. |
243 render_text->ApplyStyle(ITALIC, true, Range(0, 2)); | 309 render_text->ApplyStyle(ITALIC, true, Range(0, 2)); |
244 render_text->ApplyStyle(ITALIC, true, Range(3, 6)); | 310 render_text->ApplyStyle(ITALIC, true, Range(3, 6)); |
245 render_text->ApplyStyle(ITALIC, true, Range(7, text_length)); | 311 render_text->ApplyStyle(ITALIC, true, Range(7, text_length)); |
246 std::vector<std::pair<size_t, bool> > expected_italic; | 312 std::vector<std::pair<size_t, bool> > expected_italic; |
(...skipping 2266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2513 // Korean character "han". | 2579 // Korean character "han". |
2514 render_text.SetText(WideToUTF16(L"\xd55c")); | 2580 render_text.SetText(WideToUTF16(L"\xd55c")); |
2515 render_text.EnsureLayout(); | 2581 render_text.EnsureLayout(); |
2516 internal::TextRunList* run_list = render_text.GetRunList(); | 2582 internal::TextRunList* run_list = render_text.GetRunList(); |
2517 ASSERT_EQ(1U, run_list->size()); | 2583 ASSERT_EQ(1U, run_list->size()); |
2518 EXPECT_EQ(0U, run_list->runs()[0]->CountMissingGlyphs()); | 2584 EXPECT_EQ(0U, run_list->runs()[0]->CountMissingGlyphs()); |
2519 } | 2585 } |
2520 #endif // defined(OS_WIN) | 2586 #endif // defined(OS_WIN) |
2521 | 2587 |
2522 // Ensure that the width reported by RenderText is sufficient for drawing. Draws | 2588 // Ensure that the width reported by RenderText is sufficient for drawing. Draws |
2523 // to a canvas and checks whether any pixel beyond the width is colored. | 2589 // to a canvas and checks whether any pixel beyond the bounding rectangle is |
2590 // colored. | |
2524 TEST_F(RenderTextTest, TextDoesntClip) { | 2591 TEST_F(RenderTextTest, TextDoesntClip) { |
2525 const wchar_t* kTestStrings[] = { L"Save", L"Remove", L"TEST", L"W", L"WWW" }; | 2592 const wchar_t* kTestStrings[] = { |
2593 L"TEST_______", | |
2594 L"WWWWWWWWWW", | |
2595 L"gAXAXAXAXAXAXA", | |
2596 L"g\x00C5X\x00C5X\x00C5X\x00C5X\x00C5X\x00C5X\x00C5", | |
2597 L"\x0647\x0654\x0647\x0654\x0647\x0654\x0647\x0654\x0645\x0631\x062D" | |
2598 L"\x0628\x0627"}; | |
2526 const Size kCanvasSize(300, 50); | 2599 const Size kCanvasSize(300, 50); |
2527 const int kTestWidth = 10; | 2600 const int kTestSize = 10; |
2528 | 2601 |
2529 skia::RefPtr<SkSurface> surface = skia::AdoptRef( | 2602 skia::RefPtr<SkSurface> surface = skia::AdoptRef( |
2530 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height())); | 2603 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height())); |
2531 scoped_ptr<Canvas> canvas( | 2604 scoped_ptr<Canvas> canvas( |
2532 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f)); | 2605 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f)); |
2533 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 2606 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
2534 render_text->SetDisplayRect(Rect(kCanvasSize)); | 2607 render_text->SetDisplayRect(Rect(kCanvasSize)); |
2535 render_text->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 2608 render_text->SetHorizontalAlignment(ALIGN_LEFT); |
2536 render_text->SetColor(SK_ColorBLACK); | 2609 render_text->SetColor(SK_ColorBLACK); |
2537 | 2610 |
2538 for (auto string : kTestStrings) { | 2611 for (auto string : kTestStrings) { |
2539 surface->getCanvas()->clear(SK_ColorWHITE); | 2612 surface->getCanvas()->clear(SK_ColorWHITE); |
2540 render_text->SetText(WideToUTF16(string)); | 2613 render_text->SetText(WideToUTF16(string)); |
2614 render_text->ApplyBaselineStyle(SUPERSCRIPT, Range(1, 2)); | |
2615 render_text->ApplyBaselineStyle(SUPERIOR, Range(3, 4)); | |
2616 render_text->ApplyBaselineStyle(INFERIOR, Range(5, 6)); | |
2617 render_text->ApplyBaselineStyle(SUBSCRIPT, Range(7, 8)); | |
2541 render_text->SetStyle(BOLD, true); | 2618 render_text->SetStyle(BOLD, true); |
2619 render_text->SetDisplayRect(Rect(kTestSize, kTestSize, | |
2620 render_text->GetStringSize().width(), | |
2621 render_text->GetStringSize().height())); | |
2622 #if 0 | |
2623 // ADD(dschuyler): This should be added because without it there is a fake | |
msw
2015/02/27 01:28:19
We shouldn't land this as-is... If we can't make a
dschuyler
2015/02/27 03:05:35
I've added in some test print code for this CL (wh
msw
2015/02/27 03:48:05
Remove print debugging code, but feel free to shar
| |
2624 // success for this test by clipping the text. | |
2625 | |
2626 // Allow the RenderText to paint outside of its display rect. | |
2627 render_text->set_clip_to_display_rect(false); | |
2628 #endif | |
2542 render_text->Draw(canvas.get()); | 2629 render_text->Draw(canvas.get()); |
2543 int width = render_text->GetStringSize().width(); | 2630 int width = render_text->GetStringSize().width(); |
2544 ASSERT_LT(width + kTestWidth, kCanvasSize.width()); | 2631 ASSERT_LT(width + kTestSize, kCanvasSize.width()); |
2545 const uint32* buffer = static_cast<const uint32*>( | 2632 const uint32* buffer = |
2546 surface->peekPixels(NULL, NULL)); | 2633 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr)); |
2547 ASSERT_NE(nullptr, buffer); | 2634 ASSERT_NE(nullptr, buffer); |
2548 | 2635 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(), |
2549 for (int y = 0; y < kCanvasSize.height(); ++y) { | 2636 kCanvasSize.height()); |
2550 // Allow one column of anti-aliased pixels past the expected width. | 2637 // Top side. |
2551 SkColor color = buffer[width + y * kCanvasSize.width()]; | 2638 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(), |
2552 EXPECT_LT(220U, color_utils::GetLuminanceForColor(color)) << string; | 2639 kTestSize); |
2553 for (int x = 1; x < kTestWidth; ++x) { | 2640 // Bottom side. |
2554 color = buffer[width + x + y * kCanvasSize.width()]; | 2641 rect_buffer.EnsureSolidRect( |
2555 EXPECT_EQ(SK_ColorWHITE, color) << string; | 2642 SK_ColorWHITE, 0, kTestSize + render_text->GetStringSize().height(), |
2556 } | 2643 kCanvasSize.width(), kTestSize); |
2557 } | 2644 // Left side. |
2645 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize, kTestSize, | |
2646 render_text->GetStringSize().height()); | |
2647 // Right side. | |
2648 rect_buffer.EnsureSolidRect( | |
2649 SK_ColorWHITE, kTestSize + render_text->GetStringSize().width(), | |
2650 kTestSize, kTestSize, render_text->GetStringSize().height()); | |
2558 } | 2651 } |
2559 } | 2652 } |
2560 | 2653 |
2654 // Ensure that the text will clip to the display rect. Draws to a canvas and | |
2655 // checks whether any pixel beyond the bounding rectangle is colored. | |
2656 TEST_F(RenderTextTest, TextDoesClip) { | |
2657 const wchar_t* kTestStrings[] = {L"TEST", L"W", L"WWWW", L"gAXAXWWWW"}; | |
2658 const Size kCanvasSize(300, 50); | |
2659 const int kTestSize = 10; | |
2660 | |
2661 skia::RefPtr<SkSurface> surface = skia::AdoptRef( | |
2662 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height())); | |
2663 scoped_ptr<Canvas> canvas( | |
2664 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f)); | |
2665 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | |
2666 render_text->SetDisplayRect(Rect(kCanvasSize)); | |
2667 render_text->SetHorizontalAlignment(ALIGN_LEFT); | |
2668 render_text->SetColor(SK_ColorBLACK); | |
2669 | |
2670 for (auto string : kTestStrings) { | |
2671 surface->getCanvas()->clear(SK_ColorWHITE); | |
2672 render_text->SetText(WideToUTF16(string)); | |
2673 int fake_width = render_text->GetStringSize().width() / 2; | |
2674 int fake_height = render_text->GetStringSize().height() / 2; | |
2675 render_text->SetDisplayRect( | |
2676 Rect(kTestSize, kTestSize, fake_width, fake_height)); | |
2677 render_text->set_clip_to_display_rect(true); | |
2678 render_text->Draw(canvas.get()); | |
2679 int width = render_text->GetStringSize().width(); | |
2680 ASSERT_LT(width + kTestSize, kCanvasSize.width()); | |
2681 const uint32* buffer = | |
2682 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr)); | |
2683 ASSERT_NE(nullptr, buffer); | |
2684 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(), | |
2685 kCanvasSize.height()); | |
2686 // Top side. | |
2687 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(), | |
2688 kTestSize); | |
2689 // Bottom side. | |
2690 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize + fake_height, | |
2691 kCanvasSize.width(), kTestSize); | |
2692 // Left side. | |
2693 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize, kTestSize, | |
2694 fake_height); | |
2695 // Right side. | |
2696 rect_buffer.EnsureSolidRect(SK_ColorWHITE, kTestSize + fake_width, | |
2697 kTestSize, kTestSize, fake_height); | |
2698 } | |
2699 } | |
2700 | |
2561 } // namespace gfx | 2701 } // namespace gfx |
OLD | NEW |