| 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 <limits.h> | 7 #include <limits.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 23 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "third_party/skia/include/core/SkRefCnt.h" | 25 #include "third_party/skia/include/core/SkRefCnt.h" |
| 26 #include "third_party/skia/include/core/SkSurface.h" | 26 #include "third_party/skia/include/core/SkSurface.h" |
| 27 #include "third_party/skia/include/core/SkTypeface.h" | 27 #include "third_party/skia/include/core/SkTypeface.h" |
| 28 #include "ui/gfx/break_list.h" | 28 #include "ui/gfx/break_list.h" |
| 29 #include "ui/gfx/canvas.h" | 29 #include "ui/gfx/canvas.h" |
| 30 #include "ui/gfx/color_utils.h" | 30 #include "ui/gfx/color_utils.h" |
| 31 #include "ui/gfx/font.h" | 31 #include "ui/gfx/font.h" |
| 32 #include "ui/gfx/font_names_testing.h" |
| 32 #include "ui/gfx/geometry/point.h" | 33 #include "ui/gfx/geometry/point.h" |
| 33 #include "ui/gfx/geometry/point_f.h" | 34 #include "ui/gfx/geometry/point_f.h" |
| 34 #include "ui/gfx/range/range.h" | 35 #include "ui/gfx/range/range.h" |
| 35 #include "ui/gfx/range/range_f.h" | 36 #include "ui/gfx/range/range_f.h" |
| 36 #include "ui/gfx/render_text_harfbuzz.h" | 37 #include "ui/gfx/render_text_harfbuzz.h" |
| 37 #include "ui/gfx/text_utils.h" | 38 #include "ui/gfx/text_utils.h" |
| 38 | 39 |
| 39 #if defined(OS_WIN) | 40 #if defined(OS_WIN) |
| 40 #include "base/win/windows_version.h" | 41 #include "base/win/windows_version.h" |
| 41 #include "ui/gfx/platform_font_win.h" | 42 #include "ui/gfx/platform_font_win.h" |
| (...skipping 1917 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1959 render_text->SetText(base::string16(length, 'a')); | 1960 render_text->SetText(base::string16(length, 'a')); |
| 1960 const Size string_size = render_text->GetStringSize(); | 1961 const Size string_size = render_text->GetStringSize(); |
| 1961 EXPECT_GT(string_size.width(), previous_string_size.width()); | 1962 EXPECT_GT(string_size.width(), previous_string_size.width()); |
| 1962 EXPECT_GT(string_size.height(), 0); | 1963 EXPECT_GT(string_size.height(), 0); |
| 1963 previous_string_size = string_size; | 1964 previous_string_size = string_size; |
| 1964 } | 1965 } |
| 1965 } | 1966 } |
| 1966 | 1967 |
| 1967 TEST_P(RenderTextTest, StringSizeEmptyString) { | 1968 TEST_P(RenderTextTest, StringSizeEmptyString) { |
| 1968 // Ascent and descent of Arial and Symbol are different on most platforms. | 1969 // Ascent and descent of Arial and Symbol are different on most platforms. |
| 1969 const FontList font_list("Arial,Symbol, 16px"); | 1970 const FontList font_list( |
| 1971 base::StringPrintf("Arial,%s, 16px", kSymbolFontName)); |
| 1970 RenderText* render_text = GetRenderText(); | 1972 RenderText* render_text = GetRenderText(); |
| 1971 render_text->SetFontList(font_list); | 1973 render_text->SetFontList(font_list); |
| 1972 render_text->SetDisplayRect(Rect(0, 0, 0, font_list.GetHeight())); | 1974 render_text->SetDisplayRect(Rect(0, 0, 0, font_list.GetHeight())); |
| 1973 | 1975 |
| 1974 // The empty string respects FontList metrics for non-zero height | 1976 // The empty string respects FontList metrics for non-zero height |
| 1975 // and baseline. | 1977 // and baseline. |
| 1976 render_text->SetText(base::string16()); | 1978 render_text->SetText(base::string16()); |
| 1977 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height()); | 1979 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height()); |
| 1978 EXPECT_EQ(0, render_text->GetStringSize().width()); | 1980 EXPECT_EQ(0, render_text->GetStringSize().width()); |
| 1979 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline()); | 1981 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline()); |
| 1980 | 1982 |
| 1981 render_text->SetText(UTF8ToUTF16(" ")); | 1983 render_text->SetText(UTF8ToUTF16(" ")); |
| 1982 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height()); | 1984 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height()); |
| 1983 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline()); | 1985 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline()); |
| 1984 } | 1986 } |
| 1985 | 1987 |
| 1986 TEST_P(RenderTextTest, StringSizeRespectsFontListMetrics) { | 1988 TEST_P(RenderTextTest, StringSizeRespectsFontListMetrics) { |
| 1987 // Check that Arial and Symbol have different font metrics. | 1989 // Check that Arial and Symbol have different font metrics. |
| 1988 Font arial_font("Arial", 16); | 1990 Font arial_font("Arial", 16); |
| 1989 ASSERT_EQ("arial", | 1991 ASSERT_EQ("arial", |
| 1990 base::ToLowerASCII(arial_font.GetActualFontNameForTesting())); | 1992 base::ToLowerASCII(arial_font.GetActualFontNameForTesting())); |
| 1991 Font symbol_font("Symbol", 16); | 1993 Font symbol_font(kSymbolFontName, 16); |
| 1992 ASSERT_EQ("symbol", | 1994 ASSERT_EQ(base::ToLowerASCII(kSymbolFontName), |
| 1993 base::ToLowerASCII(symbol_font.GetActualFontNameForTesting())); | 1995 base::ToLowerASCII(symbol_font.GetActualFontNameForTesting())); |
| 1994 EXPECT_NE(arial_font.GetHeight(), symbol_font.GetHeight()); | 1996 EXPECT_NE(arial_font.GetHeight(), symbol_font.GetHeight()); |
| 1995 EXPECT_NE(arial_font.GetBaseline(), symbol_font.GetBaseline()); | 1997 EXPECT_NE(arial_font.GetBaseline(), symbol_font.GetBaseline()); |
| 1996 // "a" should be rendered with Arial, not with Symbol. | 1998 // "a" should be rendered with Arial, not with Symbol. |
| 1997 const char* arial_font_text = "a"; | 1999 const char* arial_font_text = "a"; |
| 1998 // "®" (registered trademark symbol) should be rendered with Symbol, | 2000 // "⊕" CIRCLED PLUS SIGN should be rendered with Symbol, |
| 1999 // not with Arial. | 2001 // not with Arial. |
| 2000 const char* symbol_font_text = "\xC2\xAE"; | 2002 const char* symbol_font_text = u8"⊕"; |
| 2001 | 2003 |
| 2002 Font smaller_font = arial_font; | 2004 Font smaller_font = arial_font; |
| 2003 Font larger_font = symbol_font; | 2005 Font larger_font = symbol_font; |
| 2004 const char* smaller_font_text = arial_font_text; | 2006 const char* smaller_font_text = arial_font_text; |
| 2005 const char* larger_font_text = symbol_font_text; | 2007 const char* larger_font_text = symbol_font_text; |
| 2006 if (symbol_font.GetHeight() < arial_font.GetHeight() && | 2008 if (symbol_font.GetHeight() < arial_font.GetHeight() && |
| 2007 symbol_font.GetBaseline() < arial_font.GetBaseline()) { | 2009 symbol_font.GetBaseline() < arial_font.GetBaseline()) { |
| 2008 std::swap(smaller_font, larger_font); | 2010 std::swap(smaller_font, larger_font); |
| 2009 std::swap(smaller_font_text, larger_font_text); | 2011 std::swap(smaller_font_text, larger_font_text); |
| 2010 } | 2012 } |
| 2011 ASSERT_LT(smaller_font.GetHeight(), larger_font.GetHeight()); | 2013 ASSERT_LT(smaller_font.GetHeight(), larger_font.GetHeight()); |
| 2012 ASSERT_LT(smaller_font.GetBaseline(), larger_font.GetBaseline()); | 2014 ASSERT_LT(smaller_font.GetBaseline(), larger_font.GetBaseline()); |
| 2013 | 2015 |
| 2014 // Check |smaller_font_text| is rendered with the smaller font. | 2016 // Check |smaller_font_text| is rendered with the smaller font. |
| 2015 RenderText* render_text = GetRenderText(); | 2017 RenderText* render_text = GetRenderText(); |
| 2016 render_text->SetText(UTF8ToUTF16(smaller_font_text)); | 2018 render_text->SetText(UTF8ToUTF16(smaller_font_text)); |
| 2017 render_text->SetFontList(FontList(smaller_font)); | 2019 render_text->SetFontList(FontList(smaller_font)); |
| 2018 render_text->SetDisplayRect(Rect(0, 0, 0, | 2020 render_text->SetDisplayRect(Rect(0, 0, 0, |
| 2019 render_text->font_list().GetHeight())); | 2021 render_text->font_list().GetHeight())); |
| 2020 EXPECT_EQ(smaller_font.GetHeight(), render_text->GetStringSize().height()); | 2022 EXPECT_STRCASEEQ( |
| 2021 EXPECT_EQ(smaller_font.GetBaseline(), render_text->GetBaseline()); | 2023 render_text->GetFontSpansForTesting()[0].first.GetFontName().c_str(), |
| 2024 smaller_font.GetFontName().c_str()); |
| 2022 | 2025 |
| 2023 // Layout the same text with mixed fonts. The text should be rendered with | 2026 // Layout the same text with mixed fonts. The text should be rendered with |
| 2024 // the smaller font, but the height and baseline are determined with the | 2027 // the smaller font, but the height and baseline are determined with the |
| 2025 // metrics of the font list, which is equal to the larger font. | 2028 // metrics of the font list, which is equal to the larger font. |
| 2026 std::vector<Font> fonts; | 2029 std::vector<Font> fonts; |
| 2027 fonts.push_back(smaller_font); // The primary font is the smaller font. | 2030 fonts.push_back(smaller_font); // The primary font is the smaller font. |
| 2028 fonts.push_back(larger_font); | 2031 fonts.push_back(larger_font); |
| 2029 const FontList font_list(fonts); | 2032 const FontList font_list(fonts); |
| 2030 render_text->SetFontList(font_list); | 2033 render_text->SetFontList(font_list); |
| 2031 render_text->SetDisplayRect(Rect(0, 0, 0, | 2034 render_text->SetDisplayRect(Rect(0, 0, 0, |
| 2032 render_text->font_list().GetHeight())); | 2035 render_text->font_list().GetHeight())); |
| 2036 EXPECT_STRCASEEQ( |
| 2037 render_text->GetFontSpansForTesting()[0].first.GetFontName().c_str(), |
| 2038 smaller_font.GetFontName().c_str()); |
| 2033 EXPECT_LT(smaller_font.GetHeight(), render_text->GetStringSize().height()); | 2039 EXPECT_LT(smaller_font.GetHeight(), render_text->GetStringSize().height()); |
| 2034 EXPECT_LT(smaller_font.GetBaseline(), render_text->GetBaseline()); | 2040 EXPECT_LT(smaller_font.GetBaseline(), render_text->GetBaseline()); |
| 2035 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height()); | 2041 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height()); |
| 2036 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline()); | 2042 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline()); |
| 2037 } | 2043 } |
| 2038 | 2044 |
| 2039 TEST_P(RenderTextTest, MinLineHeight) { | 2045 TEST_P(RenderTextTest, MinLineHeight) { |
| 2040 RenderText* render_text = GetRenderText(); | 2046 RenderText* render_text = GetRenderText(); |
| 2041 render_text->SetText(ASCIIToUTF16("Hello!")); | 2047 render_text->SetText(ASCIIToUTF16("Hello!")); |
| 2042 SizeF default_size = render_text->GetStringSizeF(); | 2048 SizeF default_size = render_text->GetStringSizeF(); |
| 2043 ASSERT_NE(0, default_size.height()); | 2049 ASSERT_NE(0, default_size.height()); |
| 2044 ASSERT_NE(0, default_size.width()); | 2050 ASSERT_NE(0, default_size.width()); |
| 2045 | 2051 |
| 2046 render_text->SetMinLineHeight(default_size.height() / 2); | 2052 render_text->SetMinLineHeight(default_size.height() / 2); |
| 2047 EXPECT_EQ(default_size.ToString(), render_text->GetStringSizeF().ToString()); | 2053 EXPECT_EQ(default_size.ToString(), render_text->GetStringSizeF().ToString()); |
| 2048 | 2054 |
| 2049 render_text->SetMinLineHeight(default_size.height() * 2); | 2055 render_text->SetMinLineHeight(default_size.height() * 2); |
| 2050 SizeF taller_size = render_text->GetStringSizeF(); | 2056 SizeF taller_size = render_text->GetStringSizeF(); |
| 2051 EXPECT_EQ(default_size.height() * 2, taller_size.height()); | 2057 EXPECT_EQ(default_size.height() * 2, taller_size.height()); |
| 2052 EXPECT_EQ(default_size.width(), taller_size.width()); | 2058 EXPECT_EQ(default_size.width(), taller_size.width()); |
| 2053 } | 2059 } |
| 2054 | 2060 |
| 2055 TEST_P(RenderTextTest, SetFontList) { | 2061 TEST_P(RenderTextTest, SetFontList) { |
| 2056 RenderText* render_text = GetRenderText(); | 2062 RenderText* render_text = GetRenderText(); |
| 2057 render_text->SetFontList(FontList("Arial,Symbol, 13px")); | 2063 render_text->SetFontList( |
| 2064 FontList(base::StringPrintf("Arial,%s, 13px", kSymbolFontName))); |
| 2058 const std::vector<Font>& fonts = render_text->font_list().GetFonts(); | 2065 const std::vector<Font>& fonts = render_text->font_list().GetFonts(); |
| 2059 ASSERT_EQ(2U, fonts.size()); | 2066 ASSERT_EQ(2U, fonts.size()); |
| 2060 EXPECT_EQ("Arial", fonts[0].GetFontName()); | 2067 EXPECT_EQ("Arial", fonts[0].GetFontName()); |
| 2061 EXPECT_EQ("Symbol", fonts[1].GetFontName()); | 2068 EXPECT_EQ(kSymbolFontName, fonts[1].GetFontName()); |
| 2062 EXPECT_EQ(13, render_text->font_list().GetFontSize()); | 2069 EXPECT_EQ(13, render_text->font_list().GetFontSize()); |
| 2063 } | 2070 } |
| 2064 | 2071 |
| 2065 // http://crbug/624513 | 2072 // http://crbug/624513 |
| 2066 #if !defined(OS_WIN) | 2073 #if !defined(OS_WIN) |
| 2067 TEST_P(RenderTextTest, StringSizeBoldWidth) { | 2074 TEST_P(RenderTextTest, StringSizeBoldWidth) { |
| 2068 // TODO(mboc): Add some unittests for other weights (currently not | 2075 // TODO(mboc): Add some unittests for other weights (currently not |
| 2069 // implemented because of test system font configuration). | 2076 // implemented because of test system font configuration). |
| 2070 RenderText* render_text = GetRenderText(); | 2077 RenderText* render_text = GetRenderText(); |
| 2071 render_text->SetText(UTF8ToUTF16("Hello World")); | 2078 render_text->SetText(UTF8ToUTF16("Hello World")); |
| (...skipping 1315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3387 render_text->SetDisplayRect(Rect(0, 0, render_text->GetContentWidth(), 100)); | 3394 render_text->SetDisplayRect(Rect(0, 0, render_text->GetContentWidth(), 100)); |
| 3388 EXPECT_EQ(kString, render_text->GetDisplayText()); | 3395 EXPECT_EQ(kString, render_text->GetDisplayText()); |
| 3389 } | 3396 } |
| 3390 | 3397 |
| 3391 // TODO(derat): Figure out why this fails on Windows: http://crbug.com/427184 | 3398 // TODO(derat): Figure out why this fails on Windows: http://crbug.com/427184 |
| 3392 #if !defined(OS_WIN) | 3399 #if !defined(OS_WIN) |
| 3393 // Ensure that RenderText examines all of the fonts in its FontList before | 3400 // Ensure that RenderText examines all of the fonts in its FontList before |
| 3394 // falling back to other fonts. | 3401 // falling back to other fonts. |
| 3395 TEST_P(RenderTextHarfBuzzTest, HarfBuzz_FontListFallback) { | 3402 TEST_P(RenderTextHarfBuzzTest, HarfBuzz_FontListFallback) { |
| 3396 // Double-check that the requested fonts are present. | 3403 // Double-check that the requested fonts are present. |
| 3397 FontList font_list("Arial, Symbol, 12px"); | 3404 FontList font_list(base::StringPrintf("Arial, %s, 12px", kSymbolFontName)); |
| 3398 const std::vector<Font>& fonts = font_list.GetFonts(); | 3405 const std::vector<Font>& fonts = font_list.GetFonts(); |
| 3399 ASSERT_EQ(2u, fonts.size()); | 3406 ASSERT_EQ(2u, fonts.size()); |
| 3400 ASSERT_EQ("arial", | 3407 ASSERT_EQ("arial", |
| 3401 base::ToLowerASCII(fonts[0].GetActualFontNameForTesting())); | 3408 base::ToLowerASCII(fonts[0].GetActualFontNameForTesting())); |
| 3402 ASSERT_EQ("symbol", | 3409 ASSERT_EQ(base::ToLowerASCII(kSymbolFontName), |
| 3403 base::ToLowerASCII(fonts[1].GetActualFontNameForTesting())); | 3410 base::ToLowerASCII(fonts[1].GetActualFontNameForTesting())); |
| 3404 | 3411 |
| 3405 // "⊕" (CIRCLED PLUS) should be rendered with Symbol rather than falling back | 3412 // "⊕" (CIRCLED PLUS) should be rendered with Symbol rather than falling back |
| 3406 // to some other font that's present on the system. | 3413 // to some other font that's present on the system. |
| 3407 RenderTextHarfBuzz* render_text = GetRenderTextHarfBuzz(); | 3414 RenderTextHarfBuzz* render_text = GetRenderTextHarfBuzz(); |
| 3408 render_text->SetFontList(font_list); | 3415 render_text->SetFontList(font_list); |
| 3409 render_text->SetText(UTF8ToUTF16("\xE2\x8A\x95")); | 3416 render_text->SetText(UTF8ToUTF16("\xE2\x8A\x95")); |
| 3410 const std::vector<RenderText::FontSpan> spans = | 3417 const std::vector<RenderText::FontSpan> spans = |
| 3411 render_text->GetFontSpansForTesting(); | 3418 render_text->GetFontSpansForTesting(); |
| 3412 ASSERT_EQ(static_cast<size_t>(1), spans.size()); | 3419 ASSERT_EQ(static_cast<size_t>(1), spans.size()); |
| 3413 EXPECT_EQ("Symbol", spans[0].first.GetFontName()); | 3420 EXPECT_STRCASEEQ(kSymbolFontName, spans[0].first.GetFontName().c_str()); |
| 3414 } | 3421 } |
| 3415 #endif // !defined(OS_WIN) | 3422 #endif // !defined(OS_WIN) |
| 3416 | 3423 |
| 3417 // Ensure that the fallback fonts of the Uniscribe font are tried for shaping. | 3424 // Ensure that the fallback fonts of the Uniscribe font are tried for shaping. |
| 3418 #if defined(OS_WIN) | 3425 #if defined(OS_WIN) |
| 3419 TEST_P(RenderTextHarfBuzzTest, HarfBuzz_UniscribeFallback) { | 3426 TEST_P(RenderTextHarfBuzzTest, HarfBuzz_UniscribeFallback) { |
| 3420 RenderTextHarfBuzz* render_text = GetRenderTextHarfBuzz(); | 3427 RenderTextHarfBuzz* render_text = GetRenderTextHarfBuzz(); |
| 3421 PlatformFontWin* font_win = new PlatformFontWin("Meiryo", 12); | 3428 PlatformFontWin* font_win = new PlatformFontWin("Meiryo", 12); |
| 3422 // Japanese name for Meiryo. This name won't be found in the system's linked | 3429 // Japanese name for Meiryo. This name won't be found in the system's linked |
| 3423 // fonts, forcing RTHB to try the Uniscribe font and its fallbacks. | 3430 // fonts, forcing RTHB to try the Uniscribe font and its fallbacks. |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3738 ::testing::Values(RENDER_TEXT_HARFBUZZ), | 3745 ::testing::Values(RENDER_TEXT_HARFBUZZ), |
| 3739 PrintRenderTextBackend()); | 3746 PrintRenderTextBackend()); |
| 3740 #endif | 3747 #endif |
| 3741 | 3748 |
| 3742 INSTANTIATE_TEST_CASE_P(, | 3749 INSTANTIATE_TEST_CASE_P(, |
| 3743 RenderTextHarfBuzzTest, | 3750 RenderTextHarfBuzzTest, |
| 3744 ::testing::Values(RENDER_TEXT_HARFBUZZ), | 3751 ::testing::Values(RENDER_TEXT_HARFBUZZ), |
| 3745 PrintRenderTextBackend()); | 3752 PrintRenderTextBackend()); |
| 3746 | 3753 |
| 3747 } // namespace gfx | 3754 } // namespace gfx |
| OLD | NEW |