summaryrefslogtreecommitdiffstats
path: root/ui/gfx/font_list.cc
diff options
context:
space:
mode:
authorasvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-01-09 23:05:30 +0000
committerasvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-01-09 23:05:30 +0000
commitab96da42340f2828d7bce6ac2510b2ad5fa29ab2 (patch)
tree3e50e0eb02089b495e5715148d50405bd7ea1240 /ui/gfx/font_list.cc
parent60b122deac5637b413268a9b67eb89e0cac1e135 (diff)
downloadchromium_src-ab96da42340f2828d7bce6ac2510b2ad5fa29ab2.zip
chromium_src-ab96da42340f2828d7bce6ac2510b2ad5fa29ab2.tar.gz
chromium_src-ab96da42340f2828d7bce6ac2510b2ad5fa29ab2.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@116945 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx/font_list.cc')
-rw-r--r--ui/gfx/font_list.cc143
1 files changed, 93 insertions, 50 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_;