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