Chromium Code Reviews| 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/font_list.h" | 5 #include "ui/gfx/font_list.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | |
| 10 #include "base/logging.h" | 9 #include "base/logging.h" |
| 11 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
| 13 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 14 | 13 |
| 15 namespace { | 14 namespace { |
| 16 | 15 |
| 17 // Font description of the default font set. | 16 // The default instance of gfx::FontList, whose metrics are pre-calculated. |
| 18 base::LazyInstance<std::string>::Leaky g_default_font_description = | 17 // The default ctor of gfx::FontList copies the metrics so it won't need to |
| 19 LAZY_INSTANCE_INITIALIZER; | 18 // scan the whole font list to calculate the metrics. |
| 19 gfx::FontList* g_default_font_list = NULL; | |
| 20 | 20 |
| 21 // Parses font description into |font_names|, |font_style| and |font_size|. | 21 // Parses font description into |font_names|, |font_style| and |font_size|. |
| 22 void ParseFontDescriptionString(const std::string& font_description_string, | 22 void ParseFontDescriptionString(const std::string& font_description_string, |
| 23 std::vector<std::string>* font_names, | 23 std::vector<std::string>* font_names, |
| 24 int* font_style, | 24 int* font_style, |
| 25 int* font_size) { | 25 int* font_size) { |
| 26 base::SplitString(font_description_string, ',', font_names); | 26 base::SplitString(font_description_string, ',', font_names); |
| 27 DCHECK_GT(font_names->size(), 1U); | 27 DCHECK_GT(font_names->size(), 1U); |
| 28 | 28 |
| 29 // The last item is [STYLE_OPTIONS] SIZE. | 29 // The last item is [STYLE_OPTIONS] SIZE. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 | 73 |
| 74 } // namespace | 74 } // namespace |
| 75 | 75 |
| 76 namespace gfx { | 76 namespace gfx { |
| 77 | 77 |
| 78 FontList::FontList() | 78 FontList::FontList() |
| 79 : common_height_(-1), | 79 : common_height_(-1), |
| 80 common_baseline_(-1), | 80 common_baseline_(-1), |
| 81 font_style_(-1), | 81 font_style_(-1), |
| 82 font_size_(-1) { | 82 font_size_(-1) { |
| 83 // SetDefaultFontDescription() must be called and the default font description | 83 // SetDefaultFontDescription() must be called earlier than any call of the |
| 84 // must be set earlier than any call of the default constructor. | 84 // default constructor. |
| 85 DCHECK(!(g_default_font_description == NULL)) // != is not overloaded. | 85 DCHECK(g_default_font_list) |
| 86 << "SetDefaultFontDescription has not been called."; | 86 << "SetDefaultFontDescription has not been called."; |
| 87 | 87 |
| 88 font_description_string_ = g_default_font_description.Get(); | 88 // Copy the default font list specification as well as the pre-calculated |
|
msw
2014/01/07 17:51:38
nit: s/as well as the/and its/
Yuki
2014/01/08 06:32:19
Done.
| |
| 89 if (font_description_string_.empty()) | 89 // metrics. |
| 90 fonts_.push_back(Font()); | 90 *this = *g_default_font_list; |
| 91 } | 91 } |
| 92 | 92 |
| 93 FontList::FontList(const std::string& font_description_string) | 93 FontList::FontList(const std::string& font_description_string) |
| 94 : font_description_string_(font_description_string), | 94 : font_description_string_(font_description_string), |
| 95 common_height_(-1), | 95 common_height_(-1), |
| 96 common_baseline_(-1), | 96 common_baseline_(-1), |
| 97 font_style_(-1), | 97 font_style_(-1), |
| 98 font_size_(-1) { | 98 font_size_(-1) { |
| 99 DCHECK(!font_description_string.empty()); | 99 DCHECK(!font_description_string.empty()); |
| 100 // DCHECK description string ends with "px" for size in pixel. | 100 // DCHECK description string ends with "px" for size in pixel. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 font_style_(-1), | 137 font_style_(-1), |
| 138 font_size_(-1) { | 138 font_size_(-1) { |
| 139 fonts_.push_back(font); | 139 fonts_.push_back(font); |
| 140 } | 140 } |
| 141 | 141 |
| 142 FontList::~FontList() { | 142 FontList::~FontList() { |
| 143 } | 143 } |
| 144 | 144 |
| 145 // static | 145 // static |
| 146 void FontList::SetDefaultFontDescription(const std::string& font_description) { | 146 void FontList::SetDefaultFontDescription(const std::string& font_description) { |
| 147 // The description string must end with "px" for size in pixel, or must be | 147 static FontList default_font_list((Font())); |
|
msw
2014/01/07 17:51:38
nit: remove extra parens around Font().
Yuki
2014/01/08 06:32:19
This is necessary. Without them, it's ambiguous i
| |
| 148 // the empty string, which specifies to use a single default font. | 148 g_default_font_list = &default_font_list; |
| 149 DCHECK(font_description.empty() || | |
| 150 EndsWith(font_description, "px", true)); | |
| 151 | 149 |
| 152 g_default_font_description.Get() = font_description; | 150 default_font_list = font_description.empty() ? |
|
msw
2014/01/07 17:51:38
nit: make this if (!font_description.empty()) defa
Yuki
2014/01/08 06:32:19
When ui::ResourceBundle reloads the resources incl
| |
| 151 FontList(Font()) : FontList(font_description); | |
| 152 // Pre-calculate the metrics. | |
| 153 default_font_list.CacheCommonFontHeightAndBaseline(); | |
| 154 default_font_list.CacheFontStyleAndSize(); | |
| 153 } | 155 } |
| 154 | 156 |
| 155 FontList FontList::DeriveFontList(int font_style) const { | 157 FontList FontList::DeriveFontList(int font_style) const { |
| 156 return DeriveFontListWithSizeDeltaAndStyle(0, font_style); | 158 return DeriveFontListWithSizeDeltaAndStyle(0, font_style); |
| 157 } | 159 } |
| 158 | 160 |
| 159 FontList FontList::DeriveFontListWithSize(int size) const { | 161 FontList FontList::DeriveFontListWithSize(int size) const { |
| 160 DCHECK_GT(size, 0); | 162 DCHECK_GT(size, 0); |
| 161 return DeriveFontListWithSizeDeltaAndStyle(size - GetFontSize(), | 163 return DeriveFontListWithSizeDeltaAndStyle(size - GetFontSize(), |
| 162 GetFontStyle()); | 164 GetFontStyle()); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 298 font_style_ = fonts_[0].GetStyle(); | 300 font_style_ = fonts_[0].GetStyle(); |
| 299 font_size_ = fonts_[0].GetFontSize(); | 301 font_size_ = fonts_[0].GetFontSize(); |
| 300 } else { | 302 } else { |
| 301 std::vector<std::string> font_names; | 303 std::vector<std::string> font_names; |
| 302 ParseFontDescriptionString(font_description_string_, &font_names, | 304 ParseFontDescriptionString(font_description_string_, &font_names, |
| 303 &font_style_, &font_size_); | 305 &font_style_, &font_size_); |
| 304 } | 306 } |
| 305 } | 307 } |
| 306 | 308 |
| 307 } // namespace gfx | 309 } // namespace gfx |
| OLD | NEW |