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" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "third_party/skia/include/core/SkSurface.h" | 16 #include "third_party/skia/include/core/SkSurface.h" |
17 #include "ui/gfx/break_list.h" | 17 #include "ui/gfx/break_list.h" |
18 #include "ui/gfx/canvas.h" | 18 #include "ui/gfx/canvas.h" |
| 19 #include "ui/gfx/color_utils.h" |
19 #include "ui/gfx/font.h" | 20 #include "ui/gfx/font.h" |
20 #include "ui/gfx/render_text_harfbuzz.h" | 21 #include "ui/gfx/render_text_harfbuzz.h" |
21 | 22 |
22 #if defined(OS_WIN) | 23 #if defined(OS_WIN) |
23 #include "base/win/windows_version.h" | 24 #include "base/win/windows_version.h" |
24 #include "ui/gfx/platform_font_win.h" | 25 #include "ui/gfx/platform_font_win.h" |
25 #include "ui/gfx/render_text_win.h" | 26 #include "ui/gfx/render_text_win.h" |
26 #endif | 27 #endif |
27 | 28 |
28 #if defined(OS_LINUX) && !defined(USE_OZONE) | 29 #if defined(OS_LINUX) && !defined(USE_OZONE) |
(...skipping 2344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2373 render_text.EnsureLayout(); | 2374 render_text.EnsureLayout(); |
2374 ASSERT_EQ(1U, render_text.runs_.size()); | 2375 ASSERT_EQ(1U, render_text.runs_.size()); |
2375 EXPECT_EQ(0U, render_text.runs_[0]->CountMissingGlyphs()); | 2376 EXPECT_EQ(0U, render_text.runs_[0]->CountMissingGlyphs()); |
2376 } | 2377 } |
2377 #endif // defined(OS_WIN) | 2378 #endif // defined(OS_WIN) |
2378 | 2379 |
2379 // Ensure that the width reported by RenderText is sufficient for drawing. Draws | 2380 // Ensure that the width reported by RenderText is sufficient for drawing. Draws |
2380 // to a canvas and checks whether any pixel beyond the width is colored. | 2381 // to a canvas and checks whether any pixel beyond the width is colored. |
2381 TEST_F(RenderTextTest, TextDoesntClip) { | 2382 TEST_F(RenderTextTest, TextDoesntClip) { |
2382 const wchar_t* kTestStrings[] = { L"Save", L"Remove", L"TEST", L"W", L"WWW" }; | 2383 const wchar_t* kTestStrings[] = { L"Save", L"Remove", L"TEST", L"W", L"WWW" }; |
| 2384 const Size kCanvasSize(300, 50); |
| 2385 const int kTestWidth = 10; |
2383 | 2386 |
2384 skia::RefPtr<SkSurface> surface = | 2387 skia::RefPtr<SkSurface> surface = skia::AdoptRef( |
2385 skia::AdoptRef(SkSurface::NewRasterPMColor(300, 50)); | 2388 SkSurface::NewRasterPMColor(kCanvasSize.width(), kCanvasSize.height())); |
2386 scoped_ptr<Canvas> canvas( | 2389 scoped_ptr<Canvas> canvas( |
2387 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f)); | 2390 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f)); |
2388 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 2391 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
2389 render_text->SetDisplayRect(Rect(300, 50)); | 2392 render_text->SetDisplayRect(Rect(kCanvasSize)); |
2390 render_text->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 2393 render_text->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
2391 render_text->SetColor(SK_ColorBLACK); | 2394 render_text->SetColor(SK_ColorBLACK); |
2392 | 2395 |
2393 for (size_t i = 0; i < arraysize(kTestStrings); ++i) { | 2396 for (auto string : kTestStrings) { |
2394 surface->getCanvas()->clear(SK_ColorWHITE); | 2397 surface->getCanvas()->clear(SK_ColorWHITE); |
2395 render_text->SetText(WideToUTF16(kTestStrings[i])); | 2398 render_text->SetText(WideToUTF16(string)); |
2396 render_text->SetStyle(BOLD, true); | 2399 render_text->SetStyle(BOLD, true); |
2397 render_text->Draw(canvas.get()); | 2400 render_text->Draw(canvas.get()); |
2398 int width = render_text->GetStringSize().width(); | 2401 int width = render_text->GetStringSize().width(); |
2399 ASSERT_LT(width, 300); | 2402 ASSERT_LT(width + kTestWidth, kCanvasSize.width()); |
2400 const uint32* buffer = static_cast<const uint32*>( | 2403 const uint32* buffer = static_cast<const uint32*>( |
2401 surface->peekPixels(NULL, NULL)); | 2404 surface->peekPixels(NULL, NULL)); |
2402 ASSERT_NE(nullptr, buffer); | 2405 ASSERT_NE(nullptr, buffer); |
2403 for (int y = 0; y < 50; ++y) { | 2406 |
2404 EXPECT_EQ(SK_ColorWHITE, buffer[width + y * 300]) | 2407 for (int y = 0; y < kCanvasSize.height(); ++y) { |
2405 << "String: " << kTestStrings[i]; | 2408 // Allow one column of anti-aliased pixels past the expected width. |
| 2409 SkColor color = buffer[width + y * kCanvasSize.width()]; |
| 2410 EXPECT_LT(230U, color_utils::GetLuminanceForColor(color)) << string; |
| 2411 for (int x = 1; x < kTestWidth; ++x) { |
| 2412 color = buffer[width + x + y * kCanvasSize.width()]; |
| 2413 EXPECT_EQ(SK_ColorWHITE, color) << string; |
| 2414 } |
2406 } | 2415 } |
2407 } | 2416 } |
2408 } | 2417 } |
2409 | 2418 |
2410 } // namespace gfx | 2419 } // namespace gfx |
OLD | NEW |