Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(316)

Side by Side Diff: ui/gfx/font_list.cc

Issue 124693003: Makes the default ctor of gfx::FontList copy the pre-calculated metrics. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Synced. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/gfx/font_list.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 if the underlying PlatformFont is supported.
101 // Note that not all the platforms support PlatformFont.
msw 2014/01/10 17:54:51 What happens when the metics caching functions run
Yuki 2014/01/11 09:29:07 If PlatformFont::CreateDefault() returns NULL, all
102 if (default_font_list.GetPrimaryFont().platform_font()) {
103 default_font_list.CacheCommonFontHeightAndBaseline();
104 default_font_list.CacheFontStyleAndSize();
105 }
106 g_default_font_list = &default_font_list;
107 }
108
109 // Copy the default font list specification and its pre-calculated metrics.
110 *this = *g_default_font_list;
91 } 111 }
92 112
93 FontList::FontList(const std::string& font_description_string) 113 FontList::FontList(const std::string& font_description_string)
94 : font_description_string_(font_description_string), 114 : font_description_string_(font_description_string),
95 common_height_(-1), 115 common_height_(-1),
96 common_baseline_(-1), 116 common_baseline_(-1),
97 font_style_(-1), 117 font_style_(-1),
98 font_size_(-1) { 118 font_size_(-1) {
99 DCHECK(!font_description_string.empty()); 119 DCHECK(!font_description_string.empty());
100 // DCHECK description string ends with "px" for size in pixel. 120 // DCHECK description string ends with "px" for size in pixel.
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 } 163 }
144 164
145 // static 165 // static
146 void FontList::SetDefaultFontDescription(const std::string& font_description) { 166 void FontList::SetDefaultFontDescription(const std::string& font_description) {
147 // The description string must end with "px" for size in pixel, or must be 167 // 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. 168 // the empty string, which specifies to use a single default font.
149 DCHECK(font_description.empty() || 169 DCHECK(font_description.empty() ||
150 EndsWith(font_description, "px", true)); 170 EndsWith(font_description, "px", true));
151 171
152 g_default_font_description.Get() = font_description; 172 g_default_font_description.Get() = font_description;
173 g_default_font_list = NULL;
153 } 174 }
154 175
155 FontList FontList::DeriveFontList(int font_style) const { 176 FontList FontList::DeriveFontList(int font_style) const {
156 return DeriveFontListWithSizeDeltaAndStyle(0, font_style); 177 return DeriveFontListWithSizeDeltaAndStyle(0, font_style);
157 } 178 }
158 179
159 FontList FontList::DeriveFontListWithSize(int size) const { 180 FontList FontList::DeriveFontListWithSize(int size) const {
160 DCHECK_GT(size, 0); 181 DCHECK_GT(size, 0);
161 return DeriveFontListWithSizeDeltaAndStyle(size - GetFontSize(), 182 return DeriveFontListWithSizeDeltaAndStyle(size - GetFontSize(),
162 GetFontStyle()); 183 GetFontStyle());
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 font_style_ = fonts_[0].GetStyle(); 319 font_style_ = fonts_[0].GetStyle();
299 font_size_ = fonts_[0].GetFontSize(); 320 font_size_ = fonts_[0].GetFontSize();
300 } else { 321 } else {
301 std::vector<std::string> font_names; 322 std::vector<std::string> font_names;
302 ParseFontDescriptionString(font_description_string_, &font_names, 323 ParseFontDescriptionString(font_description_string_, &font_names,
303 &font_style_, &font_size_); 324 &font_style_, &font_size_);
304 } 325 }
305 } 326 }
306 327
307 } // namespace gfx 328 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/font_list.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698