| 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 2780 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2791 L"gAXAXAXAXAXAXA", | 2791 L"gAXAXAXAXAXAXA", |
| 2792 // TODO(dschuyler): A-Ring draws outside GetStringSize; crbug.com/459812. | 2792 // TODO(dschuyler): A-Ring draws outside GetStringSize; crbug.com/459812. |
| 2793 // L"g\x00C5X\x00C5X\x00C5X\x00C5X\x00C5X\x00C5X\x00C5", | 2793 // L"g\x00C5X\x00C5X\x00C5X\x00C5X\x00C5X\x00C5X\x00C5", |
| 2794 L"\x0647\x0654\x0647\x0654\x0647\x0654\x0647\x0654\x0645\x0631\x062D" | 2794 L"\x0647\x0654\x0647\x0654\x0647\x0654\x0647\x0654\x0645\x0631\x062D" |
| 2795 L"\x0628\x0627"}; | 2795 L"\x0628\x0627"}; |
| 2796 const Size kCanvasSize(300, 50); | 2796 const Size kCanvasSize(300, 50); |
| 2797 const int kTestSize = 10; | 2797 const int kTestSize = 10; |
| 2798 | 2798 |
| 2799 skia::RefPtr<SkSurface> surface = skia::AdoptRef( | 2799 skia::RefPtr<SkSurface> surface = skia::AdoptRef( |
| 2800 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height())); | 2800 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height())); |
| 2801 scoped_ptr<Canvas> canvas( | 2801 Canvas canvas(surface->getCanvas(), 1.0f); |
| 2802 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f)); | |
| 2803 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 2802 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
| 2804 render_text->SetHorizontalAlignment(ALIGN_LEFT); | 2803 render_text->SetHorizontalAlignment(ALIGN_LEFT); |
| 2805 render_text->SetColor(SK_ColorBLACK); | 2804 render_text->SetColor(SK_ColorBLACK); |
| 2806 | 2805 |
| 2807 for (auto string : kTestStrings) { | 2806 for (auto string : kTestStrings) { |
| 2808 surface->getCanvas()->clear(SK_ColorWHITE); | 2807 surface->getCanvas()->clear(SK_ColorWHITE); |
| 2809 render_text->SetText(WideToUTF16(string)); | 2808 render_text->SetText(WideToUTF16(string)); |
| 2810 const Size string_size = render_text->GetStringSize(); | 2809 const Size string_size = render_text->GetStringSize(); |
| 2811 render_text->ApplyBaselineStyle(SUPERSCRIPT, Range(1, 2)); | 2810 render_text->ApplyBaselineStyle(SUPERSCRIPT, Range(1, 2)); |
| 2812 render_text->ApplyBaselineStyle(SUPERIOR, Range(3, 4)); | 2811 render_text->ApplyBaselineStyle(SUPERIOR, Range(3, 4)); |
| 2813 render_text->ApplyBaselineStyle(INFERIOR, Range(5, 6)); | 2812 render_text->ApplyBaselineStyle(INFERIOR, Range(5, 6)); |
| 2814 render_text->ApplyBaselineStyle(SUBSCRIPT, Range(7, 8)); | 2813 render_text->ApplyBaselineStyle(SUBSCRIPT, Range(7, 8)); |
| 2815 render_text->SetStyle(BOLD, true); | 2814 render_text->SetStyle(BOLD, true); |
| 2816 render_text->SetDisplayRect( | 2815 render_text->SetDisplayRect( |
| 2817 Rect(kTestSize, kTestSize, string_size.width(), string_size.height())); | 2816 Rect(kTestSize, kTestSize, string_size.width(), string_size.height())); |
| 2818 // Allow the RenderText to paint outside of its display rect. | 2817 // Allow the RenderText to paint outside of its display rect. |
| 2819 render_text->set_clip_to_display_rect(false); | 2818 render_text->set_clip_to_display_rect(false); |
| 2820 ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width()); | 2819 ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width()); |
| 2821 | 2820 |
| 2822 render_text->Draw(canvas.get()); | 2821 render_text->Draw(&canvas); |
| 2823 ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width()); | 2822 ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width()); |
| 2824 const uint32* buffer = | 2823 const uint32* buffer = |
| 2825 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr)); | 2824 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr)); |
| 2826 ASSERT_NE(nullptr, buffer); | 2825 ASSERT_NE(nullptr, buffer); |
| 2827 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(), | 2826 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(), |
| 2828 kCanvasSize.height()); | 2827 kCanvasSize.height()); |
| 2829 { | 2828 { |
| 2830 #if !defined(OS_CHROMEOS) | 2829 #if !defined(OS_CHROMEOS) |
| 2831 // TODO(dschuyler): On ChromeOS text draws above the GetStringSize rect. | 2830 // TODO(dschuyler): On ChromeOS text draws above the GetStringSize rect. |
| 2832 SCOPED_TRACE("TextDoesntClip Top Side"); | 2831 SCOPED_TRACE("TextDoesntClip Top Side"); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2874 | 2873 |
| 2875 // Ensure that the text will clip to the display rect. Draws to a canvas and | 2874 // Ensure that the text will clip to the display rect. Draws to a canvas and |
| 2876 // checks whether any pixel beyond the bounding rectangle is colored. | 2875 // checks whether any pixel beyond the bounding rectangle is colored. |
| 2877 TEST_F(RenderTextTest, TextDoesClip) { | 2876 TEST_F(RenderTextTest, TextDoesClip) { |
| 2878 const wchar_t* kTestStrings[] = {L"TEST", L"W", L"WWWW", L"gAXAXWWWW"}; | 2877 const wchar_t* kTestStrings[] = {L"TEST", L"W", L"WWWW", L"gAXAXWWWW"}; |
| 2879 const Size kCanvasSize(300, 50); | 2878 const Size kCanvasSize(300, 50); |
| 2880 const int kTestSize = 10; | 2879 const int kTestSize = 10; |
| 2881 | 2880 |
| 2882 skia::RefPtr<SkSurface> surface = skia::AdoptRef( | 2881 skia::RefPtr<SkSurface> surface = skia::AdoptRef( |
| 2883 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height())); | 2882 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height())); |
| 2884 scoped_ptr<Canvas> canvas( | 2883 Canvas > canvas(surface->getCanvas(), 1.0f); |
| 2885 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f)); | |
| 2886 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 2884 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
| 2887 render_text->SetHorizontalAlignment(ALIGN_LEFT); | 2885 render_text->SetHorizontalAlignment(ALIGN_LEFT); |
| 2888 render_text->SetColor(SK_ColorBLACK); | 2886 render_text->SetColor(SK_ColorBLACK); |
| 2889 | 2887 |
| 2890 for (auto string : kTestStrings) { | 2888 for (auto string : kTestStrings) { |
| 2891 surface->getCanvas()->clear(SK_ColorWHITE); | 2889 surface->getCanvas()->clear(SK_ColorWHITE); |
| 2892 render_text->SetText(WideToUTF16(string)); | 2890 render_text->SetText(WideToUTF16(string)); |
| 2893 const Size string_size = render_text->GetStringSize(); | 2891 const Size string_size = render_text->GetStringSize(); |
| 2894 int fake_width = string_size.width() / 2; | 2892 int fake_width = string_size.width() / 2; |
| 2895 int fake_height = string_size.height() / 2; | 2893 int fake_height = string_size.height() / 2; |
| 2896 render_text->SetDisplayRect( | 2894 render_text->SetDisplayRect( |
| 2897 Rect(kTestSize, kTestSize, fake_width, fake_height)); | 2895 Rect(kTestSize, kTestSize, fake_width, fake_height)); |
| 2898 render_text->set_clip_to_display_rect(true); | 2896 render_text->set_clip_to_display_rect(true); |
| 2899 render_text->Draw(canvas.get()); | 2897 render_text->Draw(&canvas); |
| 2900 ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width()); | 2898 ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width()); |
| 2901 const uint32* buffer = | 2899 const uint32* buffer = |
| 2902 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr)); | 2900 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr)); |
| 2903 ASSERT_NE(nullptr, buffer); | 2901 ASSERT_NE(nullptr, buffer); |
| 2904 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(), | 2902 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(), |
| 2905 kCanvasSize.height()); | 2903 kCanvasSize.height()); |
| 2906 { | 2904 { |
| 2907 SCOPED_TRACE("TextDoesClip Top Side"); | 2905 SCOPED_TRACE("TextDoesClip Top Side"); |
| 2908 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(), | 2906 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(), |
| 2909 kTestSize); | 2907 kTestSize); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2943 string_size.set_width(string_size.width() / 2); | 2941 string_size.set_width(string_size.width() / 2); |
| 2944 render_text.SetDisplayRect(gfx::Rect(string_size)); | 2942 render_text.SetDisplayRect(gfx::Rect(string_size)); |
| 2945 render_text.EnsureLayout(); | 2943 render_text.EnsureLayout(); |
| 2946 CFIndex glyph_count = CTLineGetGlyphCount(render_text.line_); | 2944 CFIndex glyph_count = CTLineGetGlyphCount(render_text.line_); |
| 2947 EXPECT_GT(text.size(), static_cast<size_t>(glyph_count)); | 2945 EXPECT_GT(text.size(), static_cast<size_t>(glyph_count)); |
| 2948 EXPECT_NE(0, glyph_count); | 2946 EXPECT_NE(0, glyph_count); |
| 2949 } | 2947 } |
| 2950 #endif | 2948 #endif |
| 2951 | 2949 |
| 2952 } // namespace gfx | 2950 } // namespace gfx |
| OLD | NEW |