summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorasvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-01-10 16:10:27 +0000
committerasvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-01-10 16:10:27 +0000
commit7aba39cb5325f773053a90dfa7e6b87eb2a1cda1 (patch)
treed7692a12eb85935e540dd2f782b53aad03cb75ff
parent2e2929bd78578081b5665fcea6d085793df2c233 (diff)
downloadchromium_src-7aba39cb5325f773053a90dfa7e6b87eb2a1cda1.zip
chromium_src-7aba39cb5325f773053a90dfa7e6b87eb2a1cda1.tar.gz
chromium_src-7aba39cb5325f773053a90dfa7e6b87eb2a1cda1.tar.bz2
Enable bold and italic text styles in RenderText*.
To do this, the following changes were made: Added functions GetStyle() and DeriveFontList(style) to FontList. Changed font list to use strings "Bold" and "Italic" instead of PANGO_STYLE_ITALIC and PANGO_WEIGHT_BOLD - where were actually compile constants for setting attributes and weren't recognized by Pango in a font string. (Whereas "Bold" and "Italic" are recognized). Add RenderText test that checks that the width of a bold string > the width of a non-bold string. Add FontList tests for the new functions. BUG=107893 TEST=Run views_examples_exe "Text Styles" examples and try bold and italic styles (this depends on an unreleased CL). Also, new tests in FontListTest and RenderTextTest. Review URL: http://codereview.chromium.org/8963027 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@117034 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--ui/gfx/font_list.cc143
-rw-r--r--ui/gfx/font_list.h6
-rw-r--r--ui/gfx/font_list_unittest.cc173
-rw-r--r--ui/gfx/render_text.cc31
-rw-r--r--ui/gfx/render_text.h9
-rw-r--r--ui/gfx/render_text_linux.cc32
-rw-r--r--ui/gfx/render_text_linux.h3
-rw-r--r--ui/gfx/render_text_unittest.cc16
-rw-r--r--ui/gfx/render_text_win.cc5
-rw-r--r--ui/gfx/render_text_win.h4
10 files changed, 279 insertions, 143 deletions
diff --git a/ui/gfx/font_list.cc b/ui/gfx/font_list.cc
index 0101887..24bfde2 100644
--- a/ui/gfx/font_list.cc
+++ b/ui/gfx/font_list.cc
@@ -10,6 +10,55 @@
#include "base/string_util.h"
#include "ui/gfx/font_list.h"
+namespace {
+
+// Parses font description into |font_names|, |font_style| and |font_size|.
+void ParseFontDescriptionString(const std::string& font_description_string,
+ std::vector<std::string>* font_names,
+ int* font_style,
+ int* font_size) {
+ base::SplitString(font_description_string, ',', font_names);
+ DCHECK_GT(font_names->size(), 1U);
+
+ // The last item is [STYLE_OPTIONS] SIZE.
+ std::vector<std::string> styles_size;
+ base::SplitString(font_names->back(), ' ', &styles_size);
+ DCHECK(!styles_size.empty());
+ base::StringToInt(styles_size.back(), font_size);
+ DCHECK_GT(*font_size, 0);
+ font_names->pop_back();
+
+ // Besides underline (which is supported through StyleRange), Font only
+ // supports BOLD and ITALIC, but not other styles.
+ *font_style = 0;
+ for (size_t i = 0; i < styles_size.size() - 1; ++i) {
+ // Styles are separated by white spaces. base::SplitString splits styles
+ // by space, and it inserts empty string for continuous spaces.
+ if (styles_size[i].empty())
+ continue;
+ if (!styles_size[i].compare("Bold"))
+ *font_style |= gfx::Font::BOLD;
+ else if (!styles_size[i].compare("Italic"))
+ *font_style |= gfx::Font::ITALIC;
+ else
+ NOTREACHED();
+ }
+}
+
+// Returns the font style and size as a string.
+std::string FontStyleAndSizeToString(int font_style, int font_size) {
+ std::string result;
+ if (font_style & gfx::Font::BOLD)
+ result += "Bold ";
+ if (font_style & gfx::Font::ITALIC)
+ result += "Italic ";
+ result += base::IntToString(font_size);
+ result += "px";
+ return result;
+}
+
+} // namespace
+
namespace gfx {
FontList::FontList() {
@@ -43,6 +92,38 @@ FontList::FontList(const Font& font) {
FontList::~FontList() {
}
+FontList FontList::DeriveFontList(int font_style) const {
+ // If there is a font vector, derive from that.
+ if (!fonts_.empty()) {
+ std::vector<Font> fonts = fonts_;
+ for (size_t i = 0; i < fonts.size(); ++i)
+ fonts[i] = fonts[i].DeriveFont(0, font_style);
+ return FontList(fonts);
+ }
+
+ // Otherwise, parse the font description string to derive from it.
+ std::vector<std::string> font_names;
+ int old_style;
+ int font_size;
+ ParseFontDescriptionString(font_description_string_, &font_names,
+ &old_style, &font_size);
+ std::string description = JoinString(font_names, ',');
+ description += "," + FontStyleAndSizeToString(font_style, font_size);
+ return FontList(description);
+}
+
+int FontList::GetFontStyle() const {
+ if (!fonts_.empty())
+ return fonts_[0].GetStyle();
+
+ std::vector<std::string> font_names;
+ int font_style;
+ int font_size;
+ ParseFontDescriptionString(font_description_string_, &font_names,
+ &font_style, &font_size);
+ return font_style;
+}
+
const std::string& FontList::GetFontDescriptionString() const {
if (font_description_string_.empty()) {
DCHECK(!fonts_.empty());
@@ -52,17 +133,8 @@ const std::string& FontList::GetFontDescriptionString() const {
font_description_string_ += ',';
}
// All fonts have the same style and size.
- // TODO(xji): add style for Windows.
-#if defined(OS_LINUX)
- int style = fonts_[0].GetStyle();
- if (style & Font::BOLD)
- font_description_string_ += "PANGO_WEIGHT_BOLD ";
- if (style & Font::ITALIC)
- font_description_string_ += "PANGO_STYLE_ITALIC ";
-#endif
- int size = fonts_[0].GetFontSize();
- font_description_string_ += base::IntToString(size);
- font_description_string_ += "px";
+ font_description_string_ +=
+ FontStyleAndSizeToString(fonts_[0].GetStyle(), fonts_[0].GetFontSize());
}
return font_description_string_;
}
@@ -71,48 +143,19 @@ const std::vector<Font>& FontList::GetFonts() const {
if (fonts_.empty()) {
DCHECK(!font_description_string_.empty());
- std::vector<std::string> name_style_size;
- base::SplitString(font_description_string_, ',', &name_style_size);
- int item_count = static_cast<int>(name_style_size.size());
- DCHECK_GT(item_count, 1);
-
- // The last item is [STYLE_OPTIONS] SIZE.
- std::vector<std::string> styles_size;
- base::SplitString(name_style_size[item_count - 1], ' ', &styles_size);
- DCHECK(!styles_size.empty());
-
- int style = 0;
- // TODO(xji): parse style for Windows.
-#if defined(OS_LINUX)
- // Besides underline (which is supported through StyleRange), Font only
- // supports BOLD and ITALIC styles, not other Pango styles.
- for (size_t i = 0; i < styles_size.size() - 1; ++i) {
- // Styles are separated by white spaces. base::SplitString splits styles
- // by space, and it inserts empty string for continuous spaces.
- if (styles_size[i].empty())
- continue;
- if (!styles_size[i].compare("PANGO_WEIGHT_BOLD"))
- style |= Font::BOLD;
- else if (!styles_size[i].compare("PANGO_STYLE_ITALIC"))
- style |= Font::ITALIC;
- else
- NOTREACHED();
- }
-#endif
-
- std::string font_size = styles_size[styles_size.size() - 1];
- int size_in_pixels;
- base::StringToInt(font_size, &size_in_pixels);
- DCHECK_GT(size_in_pixels, 0);
-
- for (int i = 0; i < item_count - 1; ++i) {
- DCHECK(!name_style_size[i].empty());
+ std::vector<std::string> font_names;
+ int font_style;
+ int font_size;
+ ParseFontDescriptionString(font_description_string_, &font_names,
+ &font_style, &font_size);
+ for (size_t i = 0; i < font_names.size(); ++i) {
+ DCHECK(!font_names[i].empty());
- Font font(name_style_size[i], size_in_pixels);
- if (style == Font::NORMAL)
+ Font font(font_names[i], font_size);
+ if (font_style == Font::NORMAL)
fonts_.push_back(font);
else
- fonts_.push_back(font.DeriveFont(0, style));
+ fonts_.push_back(font.DeriveFont(0, font_style));
}
}
return fonts_;
diff --git a/ui/gfx/font_list.h b/ui/gfx/font_list.h
index 757d8cf..5391d08 100644
--- a/ui/gfx/font_list.h
+++ b/ui/gfx/font_list.h
@@ -52,6 +52,12 @@ class UI_EXPORT FontList {
~FontList();
+ // Returns a new FontList with the given |font_style| flags.
+ FontList DeriveFontList(int font_style) const;
+
+ // Returns the |gfx::Font::FontStyle| style flags for this font list.
+ int GetFontStyle() const;
+
// Returns a string representing font names, styles, and size. If the FontList
// is initialized by a vector of Font, use the first font's style and size
// for the description.
diff --git a/ui/gfx/font_list_unittest.cc b/ui/gfx/font_list_unittest.cc
index 342de96..4b88afb 100644
--- a/ui/gfx/font_list_unittest.cc
+++ b/ui/gfx/font_list_unittest.cc
@@ -7,8 +7,26 @@
#include <string>
#include <vector>
+#include "base/string_number_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
+namespace {
+
+// Helper function for comparing fonts for equality.
+std::string FontToString(const gfx::Font& font) {
+ std::string font_string = font.GetFontName();
+ font_string += "|";
+ font_string += base::IntToString(font.GetFontSize());
+ int style = font.GetStyle();
+ if (style & gfx::Font::BOLD)
+ font_string += "|bold";
+ if (style & gfx::Font::ITALIC)
+ font_string += "|italic";
+ return font_string;
+}
+
+} // namespace
+
namespace gfx {
typedef testing::Test FontListTest;
@@ -32,20 +50,10 @@ TEST_F(FontListTest, FontDescString_FromFontWithNonNormalStyle) {
// Test init from Font with non-normal style.
Font font("Arial", 8);
FontList font_list = FontList(font.DeriveFont(2, Font::BOLD));
- const std::string& font_str = font_list.GetFontDescriptionString();
-#if defined(OS_LINUX)
- EXPECT_EQ("Arial,PANGO_WEIGHT_BOLD 10px", font_str);
-#else
- EXPECT_EQ("Arial,10px", font_str);
-#endif
+ EXPECT_EQ("Arial,Bold 10px", font_list.GetFontDescriptionString());
font_list = FontList(font.DeriveFont(-2, Font::ITALIC));
- const std::string& font_str_1 = font_list.GetFontDescriptionString();
-#if defined(OS_LINUX)
- EXPECT_EQ("Arial,PANGO_STYLE_ITALIC 6px", font_str_1);
-#else
- EXPECT_EQ("Arial,6px", font_str_1);
-#endif
+ EXPECT_EQ("Arial,Italic 6px", font_list.GetFontDescriptionString());
}
TEST_F(FontListTest, FontDescString_FromFontVector) {
@@ -57,11 +65,7 @@ TEST_F(FontListTest, FontDescString_FromFontVector) {
fonts.push_back(font_1.DeriveFont(-2, Font::BOLD));
FontList font_list = FontList(fonts);
const std::string& font_str = font_list.GetFontDescriptionString();
-#if defined(OS_LINUX)
- EXPECT_EQ("Arial,Sans serif,PANGO_WEIGHT_BOLD 8px", font_str);
-#else
- EXPECT_EQ("Arial,Sans serif,8px", font_str);
-#endif
+ EXPECT_EQ("Arial,Sans serif,Bold 8px", font_str);
}
TEST_F(FontListTest, Fonts_FromDescString) {
@@ -69,12 +73,8 @@ TEST_F(FontListTest, Fonts_FromDescString) {
FontList font_list = FontList("serif,Sans serif, 13px");
const std::vector<Font>& fonts = font_list.GetFonts();
EXPECT_EQ(2U, fonts.size());
- EXPECT_EQ("serif", fonts[0].GetFontName());
- EXPECT_EQ(13, fonts[0].GetFontSize());
- EXPECT_EQ(Font::NORMAL, fonts[0].GetStyle());
- EXPECT_EQ("Sans serif", fonts[1].GetFontName());
- EXPECT_EQ(13, fonts[1].GetFontSize());
- EXPECT_EQ(Font::NORMAL, fonts[1].GetStyle());
+ EXPECT_EQ("serif|13", FontToString(fonts[0]));
+ EXPECT_EQ("Sans serif|13", FontToString(fonts[1]));
}
TEST_F(FontListTest, Fonts_FromDescStringInFlexibleFormat) {
@@ -82,34 +82,18 @@ TEST_F(FontListTest, Fonts_FromDescStringInFlexibleFormat) {
FontList font_list = FontList(" serif , Sans serif , 13px");
const std::vector<Font>& fonts = font_list.GetFonts();
EXPECT_EQ(2U, fonts.size());
- EXPECT_EQ("serif", fonts[0].GetFontName());
- EXPECT_EQ(13, fonts[0].GetFontSize());
- EXPECT_EQ(Font::NORMAL, fonts[0].GetStyle());
- EXPECT_EQ("Sans serif", fonts[1].GetFontName());
- EXPECT_EQ(13, fonts[1].GetFontSize());
- EXPECT_EQ(Font::NORMAL, fonts[1].GetStyle());
+ EXPECT_EQ("serif|13", FontToString(fonts[0]));
+ EXPECT_EQ("Sans serif|13", FontToString(fonts[1]));
}
TEST_F(FontListTest, Fonts_FromDescStringWithStyleInFlexibleFormat) {
// Test init from font name style size string with flexible format.
- FontList font_list = FontList(" serif , Sans serif , PANGO_WEIGHT_BOLD "
- " PANGO_STYLE_ITALIC 13px");
+ FontList font_list = FontList(" serif , Sans serif , Bold "
+ " Italic 13px");
const std::vector<Font>& fonts = font_list.GetFonts();
EXPECT_EQ(2U, fonts.size());
- EXPECT_EQ("serif", fonts[0].GetFontName());
- EXPECT_EQ(13, fonts[0].GetFontSize());
-#if defined(OS_LINUX)
- EXPECT_EQ(Font::BOLD | Font::ITALIC, fonts[0].GetStyle());
-#else
- EXPECT_EQ(Font::NORMAL, fonts[0].GetStyle());
-#endif
- EXPECT_EQ("Sans serif", fonts[1].GetFontName());
- EXPECT_EQ(13, fonts[1].GetFontSize());
-#if defined(OS_LINUX)
- EXPECT_EQ(Font::BOLD | Font::ITALIC, fonts[1].GetStyle());
-#else
- EXPECT_EQ(Font::NORMAL, fonts[1].GetStyle());
-#endif
+ EXPECT_EQ("serif|13|bold|italic", FontToString(fonts[0]));
+ EXPECT_EQ("Sans serif|13|bold|italic", FontToString(fonts[1]));
}
TEST_F(FontListTest, Fonts_FromFont) {
@@ -118,27 +102,21 @@ TEST_F(FontListTest, Fonts_FromFont) {
FontList font_list = FontList(font);
const std::vector<Font>& fonts = font_list.GetFonts();
EXPECT_EQ(1U, fonts.size());
- EXPECT_EQ("Arial", fonts[0].GetFontName());
- EXPECT_EQ(8, fonts[0].GetFontSize());
- EXPECT_EQ(Font::NORMAL, fonts[0].GetStyle());
+ EXPECT_EQ("Arial|8", FontToString(fonts[0]));
}
TEST_F(FontListTest, Fonts_FromFontWithNonNormalStyle) {
// Test init from Font with non-normal style.
Font font("Arial", 8);
FontList font_list = FontList(font.DeriveFont(2, Font::BOLD));
- const std::vector<Font>& fonts = font_list.GetFonts();
+ std::vector<Font> fonts = font_list.GetFonts();
EXPECT_EQ(1U, fonts.size());
- EXPECT_EQ("Arial", fonts[0].GetFontName());
- EXPECT_EQ(10, fonts[0].GetFontSize());
- EXPECT_EQ(Font::BOLD, fonts[0].GetStyle());
+ EXPECT_EQ("Arial|10|bold", FontToString(fonts[0]));
font_list = FontList(font.DeriveFont(-2, Font::ITALIC));
- const std::vector<Font>& fonts_1 = font_list.GetFonts();
- EXPECT_EQ(1U, fonts_1.size());
- EXPECT_EQ("Arial", fonts_1[0].GetFontName());
- EXPECT_EQ(6, fonts_1[0].GetFontSize());
- EXPECT_EQ(Font::ITALIC, fonts_1[0].GetStyle());
+ fonts = font_list.GetFonts();
+ EXPECT_EQ(1U, fonts.size());
+ EXPECT_EQ("Arial|6|italic", FontToString(fonts[0]));
}
TEST_F(FontListTest, Fonts_FromFontVector) {
@@ -151,30 +129,21 @@ TEST_F(FontListTest, Fonts_FromFontVector) {
FontList font_list = FontList(input_fonts);
const std::vector<Font>& fonts = font_list.GetFonts();
EXPECT_EQ(2U, fonts.size());
- EXPECT_EQ("Arial", fonts[0].GetFontName());
- EXPECT_EQ(8, fonts[0].GetFontSize());
- EXPECT_EQ(Font::BOLD, fonts[0].GetStyle());
- EXPECT_EQ("Sans serif", fonts[1].GetFontName());
- EXPECT_EQ(8, fonts[1].GetFontSize());
- EXPECT_EQ(Font::BOLD, fonts[1].GetStyle());
+ EXPECT_EQ("Arial|8|bold", FontToString(fonts[0]));
+ EXPECT_EQ("Sans serif|8|bold", FontToString(fonts[1]));
}
TEST_F(FontListTest, Fonts_DescStringWithStyleInFlexibleFormat_RoundTrip) {
// Test round trip from font description string to font vector to
// font description string.
- FontList font_list = FontList(" serif , Sans serif , PANGO_WEIGHT_BOLD "
- " PANGO_STYLE_ITALIC 13px");
+ FontList font_list = FontList(" serif , Sans serif , Bold "
+ " Italic 13px");
const std::vector<Font>& fonts = font_list.GetFonts();
FontList font_list_1 = FontList(fonts);
const std::string& desc_str = font_list_1.GetFontDescriptionString();
-#if defined(OS_LINUX)
- EXPECT_EQ("serif,Sans serif,PANGO_WEIGHT_BOLD PANGO_STYLE_ITALIC 13px",
- desc_str);
-#else
- EXPECT_EQ("serif,Sans serif,13px", desc_str);
-#endif
+ EXPECT_EQ("serif,Sans serif,Bold Italic 13px", desc_str);
}
TEST_F(FontListTest, Fonts_FontVector_RoundTrip) {
@@ -191,18 +160,56 @@ TEST_F(FontListTest, Fonts_FontVector_RoundTrip) {
const std::vector<Font>& round_trip_fonts = font_list_1.GetFonts();
EXPECT_EQ(2U, round_trip_fonts.size());
- EXPECT_EQ("Arial", round_trip_fonts[0].GetFontName());
- EXPECT_EQ(8, round_trip_fonts[0].GetFontSize());
- EXPECT_EQ("Sans serif", round_trip_fonts[1].GetFontName());
- EXPECT_EQ(8, round_trip_fonts[1].GetFontSize());
-#if defined(OS_LINUX)
- EXPECT_EQ(Font::BOLD, round_trip_fonts[0].GetStyle());
- EXPECT_EQ(Font::BOLD, round_trip_fonts[1].GetStyle());
-#else
- // Style is ignored.
- EXPECT_EQ(Font::NORMAL, round_trip_fonts[0].GetStyle());
- EXPECT_EQ(Font::NORMAL, round_trip_fonts[1].GetStyle());
-#endif
+ EXPECT_EQ("Arial|8|bold", FontToString(round_trip_fonts[0]));
+ EXPECT_EQ("Sans serif|8|bold", FontToString(round_trip_fonts[1]));
+}
+
+TEST_F(FontListTest, FontDescString_GetStyle) {
+ FontList font_list = FontList("Arial,Sans serif, 8px");
+ EXPECT_EQ(Font::NORMAL, font_list.GetFontStyle());
+
+ font_list = FontList("Arial,Sans serif,Bold 8px");
+ EXPECT_EQ(Font::BOLD, font_list.GetFontStyle());
+
+ font_list = FontList("Arial,Sans serif,Italic 8px");
+ EXPECT_EQ(Font::ITALIC, font_list.GetFontStyle());
+
+ font_list = FontList("Arial,Italic Bold 8px");
+ EXPECT_EQ(Font::BOLD | Font::ITALIC, font_list.GetFontStyle());
+}
+
+TEST_F(FontListTest, Fonts_GetStyle) {
+ std::vector<Font> fonts;
+ fonts.push_back(gfx::Font("Arial", 8));
+ fonts.push_back(gfx::Font("Sans serif", 8));
+ FontList font_list = FontList(fonts);
+ EXPECT_EQ(Font::NORMAL, font_list.GetFontStyle());
+ fonts[0] = fonts[0].DeriveFont(0, Font::ITALIC | Font::BOLD);
+ fonts[1] = fonts[1].DeriveFont(0, Font::ITALIC | Font::BOLD);
+ font_list = FontList(fonts);
+ EXPECT_EQ(Font::ITALIC | Font::BOLD, font_list.GetFontStyle());
+}
+
+TEST_F(FontListTest, FontDescString_DeriveFontList) {
+ FontList font_list = FontList("Arial,Sans serif, 8px");
+
+ FontList derived = font_list.DeriveFontList(Font::BOLD | Font::ITALIC);
+ EXPECT_EQ("Arial,Sans serif,Bold Italic 8px",
+ derived.GetFontDescriptionString());
+}
+
+TEST_F(FontListTest, Fonts_DeriveFontList) {
+ std::vector<Font> fonts;
+ fonts.push_back(gfx::Font("Arial", 8));
+ fonts.push_back(gfx::Font("Sans serif", 8));
+ FontList font_list = FontList(fonts);
+
+ FontList derived = font_list.DeriveFontList(Font::BOLD | Font::ITALIC);
+ const std::vector<Font>& derived_fonts = derived.GetFonts();
+
+ EXPECT_EQ(2U, derived_fonts.size());
+ EXPECT_EQ("Arial|8|bold|italic", FontToString(derived_fonts[0]));
+ EXPECT_EQ("Sans serif|8|bold|italic", FontToString(derived_fonts[1]));
}
} // namespace gfx
diff --git a/ui/gfx/render_text.cc b/ui/gfx/render_text.cc
index d058501..4695245 100644
--- a/ui/gfx/render_text.cc
+++ b/ui/gfx/render_text.cc
@@ -77,6 +77,16 @@ void ApplyStyleRangeImpl(gfx::StyleRanges* style_ranges,
style_ranges->insert(i, style_range);
}
+// Converts |gfx::Font::FontStyle| flags to |SkTypeface::Style| flags.
+SkTypeface::Style ConvertFontStyleToSkiaTypefaceStyle(int font_style) {
+ int skia_style = SkTypeface::kNormal;
+ if (font_style & gfx::Font::BOLD)
+ skia_style |= SkTypeface::kBold;
+ if (font_style & gfx::Font::ITALIC)
+ skia_style |= SkTypeface::kItalic;
+ return static_cast<SkTypeface::Style>(skia_style);
+}
+
} // namespace
namespace gfx {
@@ -104,10 +114,26 @@ void SkiaTextRenderer::SetTextSize(int size) {
paint_.setTextSize(size);
}
+void SkiaTextRenderer::SetFontStyle(int style) {
+ SkTypeface::Style skia_style = ConvertFontStyleToSkiaTypefaceStyle(style);
+ SkTypeface* current_typeface = paint_.getTypeface();
+
+ if (current_typeface->style() == skia_style)
+ return;
+
+ SkAutoTUnref<SkTypeface> typeface(
+ SkTypeface::CreateFromTypeface(current_typeface, skia_style));
+ if (typeface.get()) {
+ // |paint_| adds its own ref. So don't |release()| it from the ref ptr here.
+ SetTypeface(typeface.get());
+ }
+}
+
void SkiaTextRenderer::SetFont(const gfx::Font& font) {
+ SkTypeface::Style skia_style =
+ ConvertFontStyleToSkiaTypefaceStyle(font.GetStyle());
SkAutoTUnref<SkTypeface> typeface(
- SkTypeface::CreateFromName(font.GetFontName().c_str(),
- SkTypeface::kNormal));
+ SkTypeface::CreateFromName(font.GetFontName().c_str(), skia_style));
if (typeface.get()) {
// |paint_| adds its own ref. So don't |release()| it from the ref ptr here.
SetTypeface(typeface.get());
@@ -164,6 +190,7 @@ void SkiaTextRenderer::DrawDecorations(int x, int y, int width,
StyleRange::StyleRange()
: foreground(SK_ColorBLACK),
+ font_style(gfx::Font::NORMAL),
strike(false),
underline(false) {
}
diff --git a/ui/gfx/render_text.h b/ui/gfx/render_text.h
index 2a69ed0..217f846 100644
--- a/ui/gfx/render_text.h
+++ b/ui/gfx/render_text.h
@@ -39,13 +39,13 @@ class SkiaTextRenderer {
void SetTypeface(SkTypeface* typeface);
void SetTextSize(int size);
void SetFont(const gfx::Font& font);
+ void SetFontStyle(int font_style);
void SetForegroundColor(SkColor foreground);
void DrawSelection(const std::vector<Rect>& selection, SkColor color);
void DrawPosText(const SkPoint* pos,
const uint16* glyphs,
size_t glyph_count);
void DrawDecorations(int x, int y, int width, bool underline, bool strike);
- void DrawCursor(const gfx::Rect& bounds);
private:
SkCanvas* canvas_skia_;
@@ -70,12 +70,9 @@ const SkColor kCursorColor = SK_ColorBLACK;
struct UI_EXPORT StyleRange {
StyleRange();
- // TODO(asvitkine): Add RenderText support for font weight. Add a |bold| style
- // flag here, to be handled in RenderText's layout phase. For example, in
- // RenderTextLinux, generate the new font description with font weight style
- // option, create a Pango attribute from it, and append the attribute to
- // layout.
SkColor foreground;
+ // A gfx::Font::FontStyle flag to specify bold and italic styles.
+ int font_style;
bool strike;
bool underline;
ui::Range range;
diff --git a/ui/gfx/render_text_linux.cc b/ui/gfx/render_text_linux.cc
index 4ebac11..044fafa 100644
--- a/ui/gfx/render_text_linux.cc
+++ b/ui/gfx/render_text_linux.cc
@@ -258,6 +258,7 @@ void RenderTextLinux::EnsureLayout() {
// TODO(xji): If RenderText will be used for displaying purpose, such as
// label, we will need to remove the single-line-mode setting.
pango_layout_set_single_paragraph_mode(layout_, true);
+ SetupPangoAttributes(layout_);
current_line_ = pango_layout_get_line_readonly(layout_, 0);
pango_layout_line_ref(current_line_);
@@ -269,6 +270,35 @@ void RenderTextLinux::EnsureLayout() {
}
}
+void RenderTextLinux::SetupPangoAttributes(PangoLayout* layout) {
+ PangoAttrList* attrs = pango_attr_list_new();
+
+ int default_font_style = font_list().GetFontStyle();
+ for (StyleRanges::const_iterator i = style_ranges().begin();
+ i < style_ranges().end(); ++i) {
+ // In Pango, different fonts means different runs, and it breaks Arabic
+ // shaping across run boundaries. So, set font only when it is different
+ // from the default font.
+ // TODO(xji): We'll eventually need to split up StyleRange into components
+ // (ColorRange, FontRange, etc.) so that we can combine adjacent ranges
+ // with the same Fonts (to avoid unnecessarily splitting up runs).
+ if (i->font_style != default_font_style) {
+ FontList derived_font_list = font_list().DeriveFontList(i->font_style);
+ PangoFontDescription* desc = pango_font_description_from_string(
+ derived_font_list.GetFontDescriptionString().c_str());
+
+ PangoAttribute* pango_attr = pango_attr_font_desc_new(desc);
+ pango_attr->start_index = Utf16IndexToUtf8Index(i->range.start());
+ pango_attr->end_index = Utf16IndexToUtf8Index(i->range.end());
+ pango_attr_list_insert(attrs, pango_attr);
+ pango_font_description_free(desc);
+ }
+ }
+
+ pango_layout_set_attributes(layout, attrs);
+ pango_attr_list_unref(attrs);
+}
+
void RenderTextLinux::DrawVisualText(Canvas* canvas) {
TRACE_EVENT0("gfx", "RenderTextLinux::DrawVisualText");
DCHECK(layout_);
@@ -353,6 +383,7 @@ void RenderTextLinux::DrawVisualText(Canvas* canvas) {
// styles evenly over the glyph. We can do this too by
// clipping and drawing the glyph several times.
renderer.SetForegroundColor(styles[style].foreground);
+ renderer.SetFontStyle(styles[style].font_style);
renderer.DrawPosText(&pos[start], &glyphs[start], i - start);
if (styles[style].underline || styles[style].strike) {
renderer.DrawDecorations(start_x, y, glyph_x - start_x,
@@ -372,6 +403,7 @@ void RenderTextLinux::DrawVisualText(Canvas* canvas) {
// Draw the remaining glyphs.
renderer.SetForegroundColor(styles[style].foreground);
+ renderer.SetFontStyle(styles[style].font_style);
renderer.DrawPosText(&pos[start], &glyphs[start], glyph_count - start);
if (styles[style].underline || styles[style].strike) {
renderer.DrawDecorations(start_x, y, glyph_x - start_x,
diff --git a/ui/gfx/render_text_linux.h b/ui/gfx/render_text_linux.h
index 8bb6cc5..8b81a9a 100644
--- a/ui/gfx/render_text_linux.h
+++ b/ui/gfx/render_text_linux.h
@@ -78,6 +78,9 @@ class RenderTextLinux : public RenderText {
// Unref |layout_| and |pango_line_|. Set them to NULL.
void ResetLayout();
+ // Setup pango attribute: foreground, background, font, strike.
+ void SetupPangoAttributes(PangoLayout* layout);
+
// Returns |run|'s visually previous run.
// The complexity is O(n) since it is a single-linked list.
PangoLayoutRun* GetPreviousRun(PangoLayoutRun* run) const;
diff --git a/ui/gfx/render_text_unittest.cc b/ui/gfx/render_text_unittest.cc
index bddfe3b8..40f872b 100644
--- a/ui/gfx/render_text_unittest.cc
+++ b/ui/gfx/render_text_unittest.cc
@@ -865,6 +865,22 @@ TEST_F(RenderTextTest, MoveLeftRightByWordInChineseText) {
EXPECT_EQ(6U, render_text->GetCursorPosition());
}
+TEST_F(RenderTextTest, StringWidthTest) {
+ scoped_ptr<RenderText> render_text(RenderText::CreateRenderText());
+ render_text->SetText(UTF8ToUTF16("Hello World"));
+
+ // Check that width is valid
+ int width = render_text->GetStringWidth();
+ EXPECT_GT(width, 0);
+
+ // Apply a bold style and check that the new width is greater.
+ StyleRange bold;
+ bold.font_style |= gfx::Font::BOLD;
+ render_text->set_default_style(bold);
+ render_text->ApplyDefaultStyle();
+ EXPECT_GT(render_text->GetStringWidth(), width);
+}
+
#endif
} // namespace gfx
diff --git a/ui/gfx/render_text_win.cc b/ui/gfx/render_text_win.cc
index b821a44..d982c3d 100644
--- a/ui/gfx/render_text_win.cc
+++ b/ui/gfx/render_text_win.cc
@@ -74,7 +74,10 @@ bool ChooseFallbackFont(HDC hdc,
log_font.lfFaceName[0] = 0;
EnumEnhMetaFile(0, meta_file, MetaFileEnumProc, &log_font, NULL);
if (log_font.lfFaceName[0]) {
+ int font_style = font.GetStyle();
*result = gfx::Font(UTF16ToUTF8(log_font.lfFaceName), font.GetFontSize());
+ if (result->GetStyle() != font_style)
+ *result = result->DeriveFont(0, font_style);
found_fallback = true;
}
}
@@ -510,7 +513,7 @@ void RenderTextWin::ItemizeLogicalText() {
for (int run_break = 0; run_break < text_length;) {
internal::TextRun* run = new internal::TextRun();
run->range.set_start(run_break);
- run->font = GetFont();
+ run->font = GetFont().DeriveFont(0, style->font_style);
run->foreground = style->foreground;
run->strike = style->strike;
run->underline = style->underline;
diff --git a/ui/gfx/render_text_win.h b/ui/gfx/render_text_win.h
index ac04c9f..4cbcb35 100644
--- a/ui/gfx/render_text_win.h
+++ b/ui/gfx/render_text_win.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -27,6 +27,8 @@ struct TextRun {
// Otherwise, this breaks the glyph shaping process.
// See the example at: http://www.catch22.net/tuts/neatpad/12.
SkColor foreground;
+ // A gfx::Font::FontStyle flag to specify bold and italic styles.
+ int font_style;
bool strike;
bool underline;