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" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // Font description of the default font set. | 17 // Font description of the default font set. |
| 18 base::LazyInstance<std::string>::Leaky g_default_font_description = | 18 base::LazyInstance<std::string>::Leaky g_default_font_description = |
| 19 LAZY_INSTANCE_INITIALIZER; | 19 LAZY_INSTANCE_INITIALIZER; |
| 20 | 20 |
| 21 // The default instance of gfx::FontList, whose metrics are pre-calculated. | |
| 22 // The default ctor of gfx::FontList copies the metrics so it won't need to | |
| 23 // scan the whole font list to calculate the metrics. | |
| 24 const gfx::FontList* g_default_font_list = NULL; | |
| 25 | |
| 21 // Parses font description into |font_names|, |font_style| and |font_size|. | 26 // Parses font description into |font_names|, |font_style| and |font_size|. |
| 22 void ParseFontDescriptionString(const std::string& font_description_string, | 27 void ParseFontDescriptionString(const std::string& font_description_string, |
| 23 std::vector<std::string>* font_names, | 28 std::vector<std::string>* font_names, |
| 24 int* font_style, | 29 int* font_style, |
| 25 int* font_size) { | 30 int* font_size) { |
| 26 base::SplitString(font_description_string, ',', font_names); | 31 base::SplitString(font_description_string, ',', font_names); |
| 27 DCHECK_GT(font_names->size(), 1U); | 32 DCHECK_GT(font_names->size(), 1U); |
| 28 | 33 |
| 29 // The last item is [STYLE_OPTIONS] SIZE. | 34 // The last item is [STYLE_OPTIONS] SIZE. |
| 30 std::vector<std::string> styles_size; | 35 std::vector<std::string> styles_size; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 78 FontList::FontList() | 83 FontList::FontList() |
| 79 : common_height_(-1), | 84 : common_height_(-1), |
| 80 common_baseline_(-1), | 85 common_baseline_(-1), |
| 81 font_style_(-1), | 86 font_style_(-1), |
| 82 font_size_(-1) { | 87 font_size_(-1) { |
| 83 // SetDefaultFontDescription() must be called and the default font description | 88 // SetDefaultFontDescription() must be called and the default font description |
| 84 // must be set earlier than any call of the default constructor. | 89 // must be set earlier than any call of the default constructor. |
| 85 DCHECK(!(g_default_font_description == NULL)) // != is not overloaded. | 90 DCHECK(!(g_default_font_description == NULL)) // != is not overloaded. |
| 86 << "SetDefaultFontDescription has not been called."; | 91 << "SetDefaultFontDescription has not been called."; |
| 87 | 92 |
| 88 font_description_string_ = g_default_font_description.Get(); | 93 // Allocate the global instance of FontList for |g_default_font_list| without |
| 89 if (font_description_string_.empty()) | 94 // calling the default ctor. |
| 90 fonts_.push_back(Font()); | 95 static FontList default_font_list((Font())); |
| 96 | |
| 97 if (!g_default_font_list) { | |
| 98 default_font_list = g_default_font_description.Get().empty() ? | |
| 99 FontList(Font()) : FontList(g_default_font_description.Get()); | |
| 100 // Pre-calculate the metrics. | |
| 101 if (default_font_list.GetPrimaryFont().platform_font()) { | |
|
Alexei Svitkine (slow)
2014/01/09 20:58:13
Can you add a comment explaining this if statement
Yuki
2014/01/10 04:57:10
Added a comment.
Android and Linux + Ozone don't
| |
| 102 default_font_list.CacheCommonFontHeightAndBaseline(); | |
| 103 default_font_list.CacheFontStyleAndSize(); | |
| 104 } | |
| 105 g_default_font_list = &default_font_list; | |
| 106 } | |
| 107 | |
| 108 // Copy the default font list specification and its pre-calculated metrics. | |
| 109 *this = *g_default_font_list; | |
| 91 } | 110 } |
| 92 | 111 |
| 93 FontList::FontList(const std::string& font_description_string) | 112 FontList::FontList(const std::string& font_description_string) |
| 94 : font_description_string_(font_description_string), | 113 : font_description_string_(font_description_string), |
| 95 common_height_(-1), | 114 common_height_(-1), |
| 96 common_baseline_(-1), | 115 common_baseline_(-1), |
| 97 font_style_(-1), | 116 font_style_(-1), |
| 98 font_size_(-1) { | 117 font_size_(-1) { |
| 99 DCHECK(!font_description_string.empty()); | 118 DCHECK(!font_description_string.empty()); |
| 100 // DCHECK description string ends with "px" for size in pixel. | 119 // DCHECK description string ends with "px" for size in pixel. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 } | 162 } |
| 144 | 163 |
| 145 // static | 164 // static |
| 146 void FontList::SetDefaultFontDescription(const std::string& font_description) { | 165 void FontList::SetDefaultFontDescription(const std::string& font_description) { |
| 147 // The description string must end with "px" for size in pixel, or must be | 166 // The description string must end with "px" for size in pixel, or must be |
| 148 // the empty string, which specifies to use a single default font. | 167 // the empty string, which specifies to use a single default font. |
| 149 DCHECK(font_description.empty() || | 168 DCHECK(font_description.empty() || |
| 150 EndsWith(font_description, "px", true)); | 169 EndsWith(font_description, "px", true)); |
| 151 | 170 |
| 152 g_default_font_description.Get() = font_description; | 171 g_default_font_description.Get() = font_description; |
| 172 g_default_font_list = NULL; | |
| 153 } | 173 } |
| 154 | 174 |
| 155 FontList FontList::DeriveFontList(int font_style) const { | 175 FontList FontList::DeriveFontList(int font_style) const { |
| 156 return DeriveFontListWithSizeDeltaAndStyle(0, font_style); | 176 return DeriveFontListWithSizeDeltaAndStyle(0, font_style); |
| 157 } | 177 } |
| 158 | 178 |
| 159 FontList FontList::DeriveFontListWithSize(int size) const { | 179 FontList FontList::DeriveFontListWithSize(int size) const { |
| 160 DCHECK_GT(size, 0); | 180 DCHECK_GT(size, 0); |
| 161 return DeriveFontListWithSizeDeltaAndStyle(size - GetFontSize(), | 181 return DeriveFontListWithSizeDeltaAndStyle(size - GetFontSize(), |
| 162 GetFontStyle()); | 182 GetFontStyle()); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 298 font_style_ = fonts_[0].GetStyle(); | 318 font_style_ = fonts_[0].GetStyle(); |
| 299 font_size_ = fonts_[0].GetFontSize(); | 319 font_size_ = fonts_[0].GetFontSize(); |
| 300 } else { | 320 } else { |
| 301 std::vector<std::string> font_names; | 321 std::vector<std::string> font_names; |
| 302 ParseFontDescriptionString(font_description_string_, &font_names, | 322 ParseFontDescriptionString(font_description_string_, &font_names, |
| 303 &font_style_, &font_size_); | 323 &font_style_, &font_size_); |
| 304 } | 324 } |
| 305 } | 325 } |
| 306 | 326 |
| 307 } // namespace gfx | 327 } // namespace gfx |
| OLD | NEW |