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 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 internal::SkiaTextRenderer::DrawDecorations( | 156 internal::SkiaTextRenderer::DrawDecorations( |
157 x, y, width, underline, strike, diagonal_strike); | 157 x, y, width, underline, strike, diagonal_strike); |
158 } | 158 } |
159 | 159 |
160 std::vector<TextLog> text_log_; | 160 std::vector<TextLog> text_log_; |
161 std::vector<DecorationLog> decoration_log_; | 161 std::vector<DecorationLog> decoration_log_; |
162 | 162 |
163 DISALLOW_COPY_AND_ASSIGN(TestSkiaTextRenderer); | 163 DISALLOW_COPY_AND_ASSIGN(TestSkiaTextRenderer); |
164 }; | 164 }; |
165 | 165 |
| 166 // Given a buffer to test against, this can be used to test various areas of the |
| 167 // rectangular buffer against a specific color value. |
| 168 class TestRectangleBuffer { |
| 169 public: |
| 170 TestRectangleBuffer(const wchar_t* string, |
| 171 const SkColor* buffer, |
| 172 uint32_t stride, |
| 173 uint32_t row_count) |
| 174 : string_(string), |
| 175 buffer_(buffer), |
| 176 stride_(stride), |
| 177 row_count_(row_count) {} |
| 178 |
| 179 // Test if any values in the rectangular area are anything other than |color|. |
| 180 void EnsureSolidRect(SkColor color, |
| 181 int left, |
| 182 int top, |
| 183 int width, |
| 184 int height) const { |
| 185 ASSERT_LT(top, row_count_) << string_; |
| 186 ASSERT_LE(top + height, row_count_) << string_; |
| 187 ASSERT_LT(left, stride_) << string_; |
| 188 ASSERT_LE(left + width, stride_) << string_ << ", left " << left |
| 189 << ", width " << width << ", stride_ " |
| 190 << stride_; |
| 191 for (int y = top; y < top + height; ++y) { |
| 192 for (int x = left; x < left + width; ++x) { |
| 193 SkColor color = buffer_[x + y * stride_]; |
| 194 EXPECT_EQ(SK_ColorWHITE, color) << string_ << " at " << x << ", " << y; |
| 195 } |
| 196 } |
| 197 } |
| 198 |
| 199 private: |
| 200 const wchar_t* string_; |
| 201 const SkColor* buffer_; |
| 202 int stride_; |
| 203 int row_count_; |
| 204 |
| 205 DISALLOW_COPY_AND_ASSIGN(TestRectangleBuffer); |
| 206 }; |
| 207 |
166 } // namespace | 208 } // namespace |
167 | 209 |
168 class RenderTextTest : public testing::Test { | 210 class RenderTextTest : public testing::Test { |
169 }; | 211 }; |
170 | 212 |
171 TEST_F(RenderTextTest, DefaultStyle) { | 213 TEST_F(RenderTextTest, DefaultStyle) { |
172 // Check the default styles applied to new instances and adjusted text. | 214 // Check the default styles applied to new instances and adjusted text. |
173 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 215 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
174 EXPECT_TRUE(render_text->text().empty()); | 216 EXPECT_TRUE(render_text->text().empty()); |
175 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"" }; |
176 for (size_t i = 0; i < arraysize(cases); ++i) { | 218 for (size_t i = 0; i < arraysize(cases); ++i) { |
177 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)); |
178 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style) | 222 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style) |
179 EXPECT_TRUE(render_text->styles()[style].EqualsValueForTesting(false)); | 223 EXPECT_TRUE(render_text->styles()[style].EqualsValueForTesting(false)); |
180 render_text->SetText(WideToUTF16(cases[i])); | 224 render_text->SetText(WideToUTF16(cases[i])); |
181 } | 225 } |
182 } | 226 } |
183 | 227 |
184 TEST_F(RenderTextTest, SetColorAndStyle) { | 228 TEST_F(RenderTextTest, SetColorAndStyle) { |
185 // Ensure custom default styles persist across setting and clearing text. | 229 // Ensure custom default styles persist across setting and clearing text. |
186 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 230 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
187 const SkColor color = SK_ColorRED; | 231 const SkColor color = SK_ColorRED; |
188 render_text->SetColor(color); | 232 render_text->SetColor(color); |
| 233 render_text->SetBaselineStyle(SUPERSCRIPT); |
189 render_text->SetStyle(BOLD, true); | 234 render_text->SetStyle(BOLD, true); |
190 render_text->SetStyle(UNDERLINE, false); | 235 render_text->SetStyle(UNDERLINE, false); |
191 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"" }; |
192 for (size_t i = 0; i < arraysize(cases); ++i) { | 237 for (size_t i = 0; i < arraysize(cases); ++i) { |
193 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(color)); | 238 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(color)); |
| 239 EXPECT_TRUE(render_text->baselines().EqualsValueForTesting(SUPERSCRIPT)); |
194 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(true)); | 240 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(true)); |
195 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsValueForTesting(false)); | 241 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsValueForTesting(false)); |
196 render_text->SetText(WideToUTF16(cases[i])); | 242 render_text->SetText(WideToUTF16(cases[i])); |
197 | 243 |
198 // 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. |
199 if (i == 1) | 245 if (i == 1) |
200 render_text->SetStyle(STRIKE, true); | 246 render_text->SetStyle(STRIKE, true); |
201 if (i >= 1) | 247 if (i >= 1) |
202 EXPECT_TRUE(render_text->styles()[STRIKE].EqualsValueForTesting(true)); | 248 EXPECT_TRUE(render_text->styles()[STRIKE].EqualsValueForTesting(true)); |
203 } | 249 } |
204 } | 250 } |
205 | 251 |
206 TEST_F(RenderTextTest, ApplyColorAndStyle) { | 252 TEST_F(RenderTextTest, ApplyColorAndStyle) { |
207 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 253 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
208 render_text->SetText(ASCIIToUTF16("012345678")); | 254 render_text->SetText(ASCIIToUTF16("012345678")); |
209 | 255 |
210 // Apply a ranged color and style and check the resulting breaks. | 256 // Apply a ranged color and style and check the resulting breaks. |
211 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)); |
212 render_text->ApplyStyle(BOLD, true, Range(2, 5)); | 259 render_text->ApplyStyle(BOLD, true, Range(2, 5)); |
213 std::vector<std::pair<size_t, SkColor> > expected_color; | 260 std::vector<std::pair<size_t, SkColor> > expected_color; |
214 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)); |
215 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)); |
216 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)); |
217 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)); |
218 std::vector<std::pair<size_t, bool> > expected_style; | 274 std::vector<std::pair<size_t, bool> > expected_style; |
219 expected_style.push_back(std::pair<size_t, bool>(0, false)); | 275 expected_style.push_back(std::pair<size_t, bool>(0, false)); |
220 expected_style.push_back(std::pair<size_t, bool>(2, true)); | 276 expected_style.push_back(std::pair<size_t, bool>(2, true)); |
221 expected_style.push_back(std::pair<size_t, bool>(5, false)); | 277 expected_style.push_back(std::pair<size_t, bool>(5, false)); |
222 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style)); | 278 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style)); |
223 | 279 |
224 // 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. |
225 render_text->SetColor(SK_ColorBLUE); | 282 render_text->SetColor(SK_ColorBLUE); |
226 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)); |
227 render_text->SetStyle(BOLD, false); | 286 render_text->SetStyle(BOLD, false); |
228 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(false)); | 287 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(false)); |
229 | 288 |
230 // 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 |
231 // (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) |
232 const size_t text_length = render_text->text().length(); | 292 const size_t text_length = render_text->text().length(); |
233 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)); |
234 render_text->ApplyStyle(BOLD, true, Range(2, text_length)); | 295 render_text->ApplyStyle(BOLD, true, Range(2, text_length)); |
235 std::vector<std::pair<size_t, SkColor> > expected_color_end; | 296 std::vector<std::pair<size_t, SkColor> > expected_color_end; |
236 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)); |
237 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)); |
238 std::vector<std::pair<size_t, bool> > expected_style_end; | 303 std::vector<std::pair<size_t, bool> > expected_style_end; |
239 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)); |
240 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)); |
241 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style_end)); | 306 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style_end)); |
242 | 307 |
243 // Ensure ranged values adjust to accommodate text length changes. | 308 // Ensure ranged values adjust to accommodate text length changes. |
244 render_text->ApplyStyle(ITALIC, true, Range(0, 2)); | 309 render_text->ApplyStyle(ITALIC, true, Range(0, 2)); |
245 render_text->ApplyStyle(ITALIC, true, Range(3, 6)); | 310 render_text->ApplyStyle(ITALIC, true, Range(3, 6)); |
246 render_text->ApplyStyle(ITALIC, true, Range(7, text_length)); | 311 render_text->ApplyStyle(ITALIC, true, Range(7, text_length)); |
247 std::vector<std::pair<size_t, bool> > expected_italic; | 312 std::vector<std::pair<size_t, bool> > expected_italic; |
(...skipping 2333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2581 // Korean character "han". | 2646 // Korean character "han". |
2582 render_text.SetText(WideToUTF16(L"\xd55c")); | 2647 render_text.SetText(WideToUTF16(L"\xd55c")); |
2583 render_text.EnsureLayout(); | 2648 render_text.EnsureLayout(); |
2584 internal::TextRunList* run_list = render_text.GetRunList(); | 2649 internal::TextRunList* run_list = render_text.GetRunList(); |
2585 ASSERT_EQ(1U, run_list->size()); | 2650 ASSERT_EQ(1U, run_list->size()); |
2586 EXPECT_EQ(0U, run_list->runs()[0]->CountMissingGlyphs()); | 2651 EXPECT_EQ(0U, run_list->runs()[0]->CountMissingGlyphs()); |
2587 } | 2652 } |
2588 #endif // defined(OS_WIN) || defined(OS_MACOSX) | 2653 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
2589 | 2654 |
2590 // Ensure that the width reported by RenderText is sufficient for drawing. Draws | 2655 // Ensure that the width reported by RenderText is sufficient for drawing. Draws |
2591 // to a canvas and checks whether any pixel beyond the width is colored. | 2656 // to a canvas and checks whether any pixel beyond the bounding rectangle is |
| 2657 // colored. |
2592 TEST_F(RenderTextTest, TextDoesntClip) { | 2658 TEST_F(RenderTextTest, TextDoesntClip) { |
2593 const wchar_t* kTestStrings[] = { L"Save", L"Remove", L"TEST", L"W", L"WWW" }; | 2659 const wchar_t* kTestStrings[] = { |
| 2660 L" ", |
| 2661 // TODO(dschuyler): Underscores draw outside GetStringSize; |
| 2662 // crbug.com/459812. This appears to be a preexisting issue that wasn't |
| 2663 // revealed by the prior unit tests. |
| 2664 // L"TEST_______", |
| 2665 L"TEST some stuff", |
| 2666 L"WWWWWWWWWW", |
| 2667 L"gAXAXAXAXAXAXA", |
| 2668 // TODO(dschuyler): A-Ring draws outside GetStringSize; crbug.com/459812. |
| 2669 // L"g\x00C5X\x00C5X\x00C5X\x00C5X\x00C5X\x00C5X\x00C5", |
| 2670 L"\x0647\x0654\x0647\x0654\x0647\x0654\x0647\x0654\x0645\x0631\x062D" |
| 2671 L"\x0628\x0627"}; |
2594 const Size kCanvasSize(300, 50); | 2672 const Size kCanvasSize(300, 50); |
2595 const int kTestWidth = 10; | 2673 const int kTestSize = 10; |
2596 | 2674 |
2597 skia::RefPtr<SkSurface> surface = skia::AdoptRef( | 2675 skia::RefPtr<SkSurface> surface = skia::AdoptRef( |
2598 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height())); | 2676 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height())); |
2599 scoped_ptr<Canvas> canvas( | 2677 scoped_ptr<Canvas> canvas( |
2600 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f)); | 2678 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f)); |
2601 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 2679 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
2602 render_text->SetDisplayRect(Rect(kCanvasSize)); | 2680 render_text->SetHorizontalAlignment(ALIGN_LEFT); |
2603 render_text->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
2604 render_text->SetColor(SK_ColorBLACK); | 2681 render_text->SetColor(SK_ColorBLACK); |
2605 | 2682 |
2606 for (auto string : kTestStrings) { | 2683 for (auto string : kTestStrings) { |
2607 surface->getCanvas()->clear(SK_ColorWHITE); | 2684 surface->getCanvas()->clear(SK_ColorWHITE); |
2608 render_text->SetText(WideToUTF16(string)); | 2685 render_text->SetText(WideToUTF16(string)); |
| 2686 const Size string_size = render_text->GetStringSize(); |
| 2687 render_text->ApplyBaselineStyle(SUPERSCRIPT, Range(1, 2)); |
| 2688 render_text->ApplyBaselineStyle(SUPERIOR, Range(3, 4)); |
| 2689 render_text->ApplyBaselineStyle(INFERIOR, Range(5, 6)); |
| 2690 render_text->ApplyBaselineStyle(SUBSCRIPT, Range(7, 8)); |
2609 render_text->SetStyle(BOLD, true); | 2691 render_text->SetStyle(BOLD, true); |
| 2692 render_text->SetDisplayRect( |
| 2693 Rect(kTestSize, kTestSize, string_size.width(), string_size.height())); |
| 2694 // Allow the RenderText to paint outside of its display rect. |
| 2695 render_text->set_clip_to_display_rect(false); |
| 2696 ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width()); |
| 2697 |
2610 render_text->Draw(canvas.get()); | 2698 render_text->Draw(canvas.get()); |
2611 int width = render_text->GetStringSize().width(); | 2699 ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width()); |
2612 ASSERT_LT(width + kTestWidth, kCanvasSize.width()); | 2700 const uint32* buffer = |
2613 const uint32* buffer = static_cast<const uint32*>( | 2701 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr)); |
2614 surface->peekPixels(NULL, NULL)); | |
2615 ASSERT_NE(nullptr, buffer); | 2702 ASSERT_NE(nullptr, buffer); |
2616 | 2703 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(), |
2617 for (int y = 0; y < kCanvasSize.height(); ++y) { | 2704 kCanvasSize.height()); |
2618 // Allow one column of anti-aliased pixels past the expected width. | 2705 { |
2619 SkColor color = buffer[width + y * kCanvasSize.width()]; | 2706 #if !defined(OS_CHROMEOS) |
2620 EXPECT_LT(220U, color_utils::GetLuminanceForColor(color)) << string; | 2707 // TODO(dschuyler): On ChromeOS text draws above the GetStringSize rect. |
2621 for (int x = 1; x < kTestWidth; ++x) { | 2708 SCOPED_TRACE("TextDoesntClip Top Side"); |
2622 color = buffer[width + x + y * kCanvasSize.width()]; | 2709 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(), |
2623 EXPECT_EQ(SK_ColorWHITE, color) << string; | 2710 kTestSize); |
2624 } | 2711 #endif |
| 2712 } |
| 2713 { |
| 2714 SCOPED_TRACE("TextDoesntClip Bottom Side"); |
| 2715 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, |
| 2716 kTestSize + string_size.height(), |
| 2717 kCanvasSize.width(), kTestSize); |
| 2718 } |
| 2719 { |
| 2720 SCOPED_TRACE("TextDoesntClip Left Side"); |
| 2721 #if defined(OS_WIN) |
| 2722 // TODO(dschuyler): On Windows XP the Unicode test draws to the left edge |
| 2723 // as if it is ignoring the SetDisplayRect shift by kTestSize. This |
| 2724 // appears to be a preexisting issue that wasn't revealed by the prior |
| 2725 // unit tests. |
| 2726 #elif defined(OS_MACOSX) |
| 2727 // TODO(dschuyler): On Windows (non-XP) and Mac smoothing draws left of |
| 2728 // text. his appears to be a preexisting issue that wasn't revealed by |
| 2729 // the prior unit tests. |
| 2730 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize, kTestSize - 1, |
| 2731 string_size.height()); |
| 2732 #else |
| 2733 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize, kTestSize, |
| 2734 string_size.height()); |
| 2735 #endif |
| 2736 } |
| 2737 { |
| 2738 SCOPED_TRACE("TextDoesntClip Right Side"); |
| 2739 #if !defined(OS_MACOSX) |
| 2740 // TODO(dschuyler): On Mac text draws to right of GetStringSize. This |
| 2741 // appears to be a preexisting issue that wasn't revealed by the prior |
| 2742 // unit tests. |
| 2743 rect_buffer.EnsureSolidRect(SK_ColorWHITE, |
| 2744 kTestSize + string_size.width(), kTestSize, |
| 2745 kTestSize, string_size.height()); |
| 2746 #endif |
2625 } | 2747 } |
2626 } | 2748 } |
2627 } | 2749 } |
| 2750 |
| 2751 // Ensure that the text will clip to the display rect. Draws to a canvas and |
| 2752 // checks whether any pixel beyond the bounding rectangle is colored. |
| 2753 TEST_F(RenderTextTest, TextDoesClip) { |
| 2754 const wchar_t* kTestStrings[] = {L"TEST", L"W", L"WWWW", L"gAXAXWWWW"}; |
| 2755 const Size kCanvasSize(300, 50); |
| 2756 const int kTestSize = 10; |
| 2757 |
| 2758 skia::RefPtr<SkSurface> surface = skia::AdoptRef( |
| 2759 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height())); |
| 2760 scoped_ptr<Canvas> canvas( |
| 2761 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f)); |
| 2762 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
| 2763 render_text->SetHorizontalAlignment(ALIGN_LEFT); |
| 2764 render_text->SetColor(SK_ColorBLACK); |
| 2765 |
| 2766 for (auto string : kTestStrings) { |
| 2767 surface->getCanvas()->clear(SK_ColorWHITE); |
| 2768 render_text->SetText(WideToUTF16(string)); |
| 2769 const Size string_size = render_text->GetStringSize(); |
| 2770 int fake_width = string_size.width() / 2; |
| 2771 int fake_height = string_size.height() / 2; |
| 2772 render_text->SetDisplayRect( |
| 2773 Rect(kTestSize, kTestSize, fake_width, fake_height)); |
| 2774 render_text->set_clip_to_display_rect(true); |
| 2775 render_text->Draw(canvas.get()); |
| 2776 ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width()); |
| 2777 const uint32* buffer = |
| 2778 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr)); |
| 2779 ASSERT_NE(nullptr, buffer); |
| 2780 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(), |
| 2781 kCanvasSize.height()); |
| 2782 { |
| 2783 SCOPED_TRACE("TextDoesClip Top Side"); |
| 2784 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(), |
| 2785 kTestSize); |
| 2786 } |
| 2787 |
| 2788 { |
| 2789 SCOPED_TRACE("TextDoesClip Bottom Side"); |
| 2790 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize + fake_height, |
| 2791 kCanvasSize.width(), kTestSize); |
| 2792 } |
| 2793 { |
| 2794 SCOPED_TRACE("TextDoesClip Left Side"); |
| 2795 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize, kTestSize, |
| 2796 fake_height); |
| 2797 } |
| 2798 { |
| 2799 SCOPED_TRACE("TextDoesClip Right Side"); |
| 2800 rect_buffer.EnsureSolidRect(SK_ColorWHITE, kTestSize + fake_width, |
| 2801 kTestSize, kTestSize, fake_height); |
| 2802 } |
| 2803 } |
| 2804 } |
2628 | 2805 |
2629 } // namespace gfx | 2806 } // namespace gfx |
OLD | NEW |