[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ui/gfx/font_list_impl.h" |
| 6 | |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
| 11 | #include "base/logging.h" |
| 12 | #include "base/strings/string_number_conversions.h" |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 13 | #include "base/strings/string_util.h" |
| 14 | #include "ui/gfx/font.h" |
derat | 23144a6 | 2015-02-10 14:48:25 | [diff] [blame] | 15 | #include "ui/gfx/font_list.h" |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 16 | |
| 17 | namespace { |
| 18 | |
derat | 23144a6 | 2015-02-10 14:48:25 | [diff] [blame] | 19 | // Returns a font description from |families|, |style|, and |size_pixels|. |
| 20 | std::string BuildDescription(const std::vector<std::string>& families, |
| 21 | int style, |
| 22 | int size_pixels) { |
brettw | d94a2214 | 2015-07-15 05:19:26 | [diff] [blame] | 23 | std::string description = base::JoinString(families, ","); |
derat | 23144a6 | 2015-02-10 14:48:25 | [diff] [blame] | 24 | description += ","; |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 25 | |
derat | 23144a6 | 2015-02-10 14:48:25 | [diff] [blame] | 26 | if (style & gfx::Font::BOLD) |
| 27 | description += "Bold "; |
| 28 | if (style & gfx::Font::ITALIC) |
| 29 | description += "Italic "; |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 30 | |
derat | 23144a6 | 2015-02-10 14:48:25 | [diff] [blame] | 31 | description += base::IntToString(size_pixels); |
| 32 | description += "px"; |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 33 | |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 34 | return description; |
| 35 | } |
| 36 | |
| 37 | } // namespace |
| 38 | |
| 39 | namespace gfx { |
| 40 | |
| 41 | FontListImpl::FontListImpl(const std::string& font_description_string) |
| 42 | : font_description_string_(font_description_string), |
| 43 | common_height_(-1), |
| 44 | common_baseline_(-1), |
| 45 | font_style_(-1), |
| 46 | font_size_(-1) { |
| 47 | DCHECK(!font_description_string.empty()); |
| 48 | // DCHECK description string ends with "px" for size in pixel. |
brettw | a7ff1b29 | 2015-07-16 17:49:29 | [diff] [blame] | 49 | DCHECK(base::EndsWith(font_description_string, "px", |
| 50 | base::CompareCase::SENSITIVE)); |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | FontListImpl::FontListImpl(const std::vector<std::string>& font_names, |
| 54 | int font_style, |
| 55 | int font_size) |
derat | 23144a6 | 2015-02-10 14:48:25 | [diff] [blame] | 56 | : font_description_string_(BuildDescription(font_names, font_style, |
| 57 | font_size)), |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 58 | common_height_(-1), |
| 59 | common_baseline_(-1), |
| 60 | font_style_(font_style), |
| 61 | font_size_(font_size) { |
| 62 | DCHECK(!font_names.empty()); |
| 63 | DCHECK(!font_names[0].empty()); |
| 64 | } |
| 65 | |
| 66 | FontListImpl::FontListImpl(const std::vector<Font>& fonts) |
| 67 | : fonts_(fonts), |
| 68 | common_height_(-1), |
| 69 | common_baseline_(-1), |
| 70 | font_style_(-1), |
| 71 | font_size_(-1) { |
| 72 | DCHECK(!fonts.empty()); |
| 73 | font_style_ = fonts[0].GetStyle(); |
| 74 | font_size_ = fonts[0].GetFontSize(); |
danakj | e649f57 | 2015-01-08 23:35:58 | [diff] [blame] | 75 | #if DCHECK_IS_ON() |
[email protected] | c02cb801 | 2014-03-14 18:39:53 | [diff] [blame] | 76 | for (size_t i = 1; i < fonts.size(); ++i) { |
| 77 | DCHECK_EQ(fonts[i].GetStyle(), font_style_); |
| 78 | DCHECK_EQ(fonts[i].GetFontSize(), font_size_); |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 79 | } |
[email protected] | c02cb801 | 2014-03-14 18:39:53 | [diff] [blame] | 80 | #endif |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | FontListImpl::FontListImpl(const Font& font) |
| 84 | : common_height_(-1), |
| 85 | common_baseline_(-1), |
| 86 | font_style_(-1), |
| 87 | font_size_(-1) { |
| 88 | fonts_.push_back(font); |
| 89 | } |
| 90 | |
| 91 | FontListImpl* FontListImpl::Derive(int size_delta, int font_style) const { |
| 92 | // If there is a font vector, derive from that. |
| 93 | if (!fonts_.empty()) { |
| 94 | std::vector<Font> fonts = fonts_; |
| 95 | for (size_t i = 0; i < fonts.size(); ++i) |
[email protected] | 0143082 | 2014-02-13 15:41:45 | [diff] [blame] | 96 | fonts[i] = fonts[i].Derive(size_delta, font_style); |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 97 | return new FontListImpl(fonts); |
| 98 | } |
| 99 | |
| 100 | // Otherwise, parse the font description string to derive from it. |
| 101 | std::vector<std::string> font_names; |
| 102 | int old_size; |
| 103 | int old_style; |
derat | 23144a6 | 2015-02-10 14:48:25 | [diff] [blame] | 104 | CHECK(FontList::ParseDescription(font_description_string_, &font_names, |
| 105 | &old_style, &old_size)); |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 106 | const int size = std::max(1, old_size + size_delta); |
| 107 | return new FontListImpl(font_names, font_style, size); |
| 108 | } |
| 109 | |
| 110 | int FontListImpl::GetHeight() const { |
| 111 | if (common_height_ == -1) |
| 112 | CacheCommonFontHeightAndBaseline(); |
| 113 | return common_height_; |
| 114 | } |
| 115 | |
| 116 | int FontListImpl::GetBaseline() const { |
| 117 | if (common_baseline_ == -1) |
| 118 | CacheCommonFontHeightAndBaseline(); |
| 119 | return common_baseline_; |
| 120 | } |
| 121 | |
| 122 | int FontListImpl::GetCapHeight() const { |
| 123 | // Assume the primary font is used to render Latin characters. |
| 124 | return GetPrimaryFont().GetCapHeight(); |
| 125 | } |
| 126 | |
| 127 | int FontListImpl::GetExpectedTextWidth(int length) const { |
| 128 | // Rely on the primary font metrics for the time being. |
| 129 | return GetPrimaryFont().GetExpectedTextWidth(length); |
| 130 | } |
| 131 | |
| 132 | int FontListImpl::GetFontStyle() const { |
| 133 | if (font_style_ == -1) |
| 134 | CacheFontStyleAndSize(); |
| 135 | return font_style_; |
| 136 | } |
| 137 | |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 138 | int FontListImpl::GetFontSize() const { |
| 139 | if (font_size_ == -1) |
| 140 | CacheFontStyleAndSize(); |
| 141 | return font_size_; |
| 142 | } |
| 143 | |
| 144 | const std::vector<Font>& FontListImpl::GetFonts() const { |
| 145 | if (fonts_.empty()) { |
| 146 | DCHECK(!font_description_string_.empty()); |
| 147 | |
| 148 | std::vector<std::string> font_names; |
| 149 | // It's possible that gfx::Font::UNDERLINE is specified and it's already |
| 150 | // stored in |font_style_| but |font_description_string_| doesn't have the |
| 151 | // underline info. So we should respect |font_style_| as long as it's |
| 152 | // valid. |
| 153 | int style = 0; |
derat | 23144a6 | 2015-02-10 14:48:25 | [diff] [blame] | 154 | CHECK(FontList::ParseDescription(font_description_string_, &font_names, |
| 155 | &style, &font_size_)); |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 156 | if (font_style_ == -1) |
| 157 | font_style_ = style; |
| 158 | for (size_t i = 0; i < font_names.size(); ++i) { |
| 159 | DCHECK(!font_names[i].empty()); |
| 160 | |
| 161 | Font font(font_names[i], font_size_); |
| 162 | if (font_style_ == Font::NORMAL) |
| 163 | fonts_.push_back(font); |
| 164 | else |
[email protected] | 0143082 | 2014-02-13 15:41:45 | [diff] [blame] | 165 | fonts_.push_back(font.Derive(0, font_style_)); |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | return fonts_; |
| 169 | } |
| 170 | |
| 171 | const Font& FontListImpl::GetPrimaryFont() const { |
| 172 | return GetFonts()[0]; |
| 173 | } |
| 174 | |
| 175 | FontListImpl::~FontListImpl() {} |
| 176 | |
| 177 | void FontListImpl::CacheCommonFontHeightAndBaseline() const { |
| 178 | int ascent = 0; |
| 179 | int descent = 0; |
| 180 | const std::vector<Font>& fonts = GetFonts(); |
| 181 | for (std::vector<Font>::const_iterator i = fonts.begin(); |
| 182 | i != fonts.end(); ++i) { |
| 183 | ascent = std::max(ascent, i->GetBaseline()); |
| 184 | descent = std::max(descent, i->GetHeight() - i->GetBaseline()); |
| 185 | } |
| 186 | common_height_ = ascent + descent; |
| 187 | common_baseline_ = ascent; |
| 188 | } |
| 189 | |
| 190 | void FontListImpl::CacheFontStyleAndSize() const { |
| 191 | if (!fonts_.empty()) { |
| 192 | font_style_ = fonts_[0].GetStyle(); |
| 193 | font_size_ = fonts_[0].GetFontSize(); |
| 194 | } else { |
| 195 | std::vector<std::string> font_names; |
derat | 23144a6 | 2015-02-10 14:48:25 | [diff] [blame] | 196 | CHECK(FontList::ParseDescription(font_description_string_, &font_names, |
| 197 | &font_style_, &font_size_)); |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | } // namespace gfx |