diff options
author | binji@chromium.org <binji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-09 23:21:11 +0000 |
---|---|---|
committer | binji@chromium.org <binji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-09 23:21:11 +0000 |
commit | 4909ecaf28e4b3bbc6171814548cf51e249ad45a (patch) | |
tree | 9b5220653638c509a1cece937d817578455bf174 /ui/gfx/font_list.cc | |
parent | 106b09d97d773b9c0fff77ce2b44f32f43cad34c (diff) | |
download | chromium_src-4909ecaf28e4b3bbc6171814548cf51e249ad45a.zip chromium_src-4909ecaf28e4b3bbc6171814548cf51e249ad45a.tar.gz chromium_src-4909ecaf28e4b3bbc6171814548cf51e249ad45a.tar.bz2 |
Revert 116945 - 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
TBR=asvitkine@chromium.org
Review URL: http://codereview.chromium.org/9147016
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@116948 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx/font_list.cc')
-rw-r--r-- | ui/gfx/font_list.cc | 143 |
1 files changed, 50 insertions, 93 deletions
diff --git a/ui/gfx/font_list.cc b/ui/gfx/font_list.cc index 24bfde2..0101887 100644 --- a/ui/gfx/font_list.cc +++ b/ui/gfx/font_list.cc @@ -10,55 +10,6 @@ #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() { @@ -92,38 +43,6 @@ 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()); @@ -133,8 +52,17 @@ const std::string& FontList::GetFontDescriptionString() const { font_description_string_ += ','; } // All fonts have the same style and size. - font_description_string_ += - FontStyleAndSizeToString(fonts_[0].GetStyle(), fonts_[0].GetFontSize()); + // 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"; } return font_description_string_; } @@ -143,19 +71,48 @@ const std::vector<Font>& FontList::GetFonts() const { if (fonts_.empty()) { DCHECK(!font_description_string_.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()); + 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()); - Font font(font_names[i], font_size); - if (font_style == Font::NORMAL) + Font font(name_style_size[i], size_in_pixels); + if (style == Font::NORMAL) fonts_.push_back(font); else - fonts_.push_back(font.DeriveFont(0, font_style)); + fonts_.push_back(font.DeriveFont(0, style)); } } return fonts_; |