forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont_list.cc
255 lines (211 loc) · 8.07 KB
/
font_list.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/font_list.h"
#include "base/lazy_instance.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "third_party/skia/include/core/SkTypeface.h"
#include "third_party/skia/include/ports/SkFontMgr.h"
#include "ui/gfx/font_list_impl.h"
namespace {
// Font description of the default font set.
base::LazyInstance<std::string>::Leaky g_default_font_description =
LAZY_INSTANCE_INITIALIZER;
// The default instance of gfx::FontListImpl.
base::LazyInstance<scoped_refptr<gfx::FontListImpl>>::Leaky g_default_impl =
LAZY_INSTANCE_INITIALIZER;
bool g_default_impl_initialized = false;
bool IsFontFamilyAvailable(const std::string& family, SkFontMgr* fontManager) {
#if defined(OS_LINUX)
sk_sp<SkTypeface> typeface(
fontManager->legacyCreateTypeface(family.c_str(), SkFontStyle()));
return typeface;
#else
sk_sp<SkFontStyleSet> set(fontManager->matchFamily(family.c_str()));
return set && set->count();
#endif
}
} // namespace
namespace gfx {
// static
bool FontList::ParseDescription(const std::string& description,
std::vector<std::string>* families_out,
int* style_out,
int* size_pixels_out,
Font::Weight* weight_out) {
DCHECK(families_out);
DCHECK(style_out);
DCHECK(size_pixels_out);
DCHECK(weight_out);
*families_out = base::SplitString(
description, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
if (families_out->empty())
return false;
for (auto& family : *families_out)
base::TrimWhitespaceASCII(family, base::TRIM_ALL, &family);
// The last item is "[STYLE1] [STYLE2] [...] SIZE".
std::vector<std::string> styles = base::SplitString(
families_out->back(), base::kWhitespaceASCII,
base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
families_out->pop_back();
if (styles.empty())
return false;
// The size takes the form "<INT>px".
std::string size_string = styles.back();
styles.pop_back();
if (!base::EndsWith(size_string, "px", base::CompareCase::SENSITIVE))
return false;
size_string.resize(size_string.size() - 2);
if (!base::StringToInt(size_string, size_pixels_out) ||
*size_pixels_out <= 0)
return false;
// Font supports ITALIC and weights; underline is supported via RenderText.
*style_out = Font::NORMAL;
*weight_out = Font::Weight::NORMAL;
for (const auto& style_string : styles) {
if (style_string == "Italic")
*style_out |= Font::ITALIC;
else if (style_string == "Thin")
*weight_out = Font::Weight::THIN;
else if (style_string == "Ultra-Light")
*weight_out = Font::Weight::EXTRA_LIGHT;
else if (style_string == "Light")
*weight_out = Font::Weight::LIGHT;
else if (style_string == "Normal")
*weight_out = Font::Weight::NORMAL;
else if (style_string == "Medium")
*weight_out = Font::Weight::MEDIUM;
else if (style_string == "Semi-Bold")
*weight_out = Font::Weight::SEMIBOLD;
else if (style_string == "Bold")
*weight_out = Font::Weight::BOLD;
else if (style_string == "Ultra-Bold")
*weight_out = Font::Weight::EXTRA_BOLD;
else if (style_string == "Heavy")
*weight_out = Font::Weight::BLACK;
else
return false;
}
return true;
}
FontList::FontList() : impl_(GetDefaultImpl()) {}
FontList::FontList(const FontList& other) : impl_(other.impl_) {}
FontList::FontList(const std::string& font_description_string)
: impl_(new FontListImpl(font_description_string)) {}
FontList::FontList(const std::vector<std::string>& font_names,
int font_style,
int font_size,
Font::Weight font_weight)
: impl_(new FontListImpl(font_names, font_style, font_size, font_weight)) {}
FontList::FontList(const std::vector<Font>& fonts)
: impl_(new FontListImpl(fonts)) {}
FontList::FontList(const Font& font) : impl_(new FontListImpl(font)) {}
FontList::~FontList() {}
FontList& FontList::operator=(const FontList& other) {
impl_ = other.impl_;
return *this;
}
// static
void FontList::SetDefaultFontDescription(const std::string& font_description) {
// The description string must end with "px" for size in pixel, or must be
// the empty string, which specifies to use a single default font.
DCHECK(font_description.empty() ||
base::EndsWith(font_description, "px", base::CompareCase::SENSITIVE));
g_default_font_description.Get() = font_description;
g_default_impl_initialized = false;
}
FontList FontList::Derive(int size_delta,
int font_style,
Font::Weight weight) const {
return FontList(impl_->Derive(size_delta, font_style, weight));
}
FontList FontList::DeriveWithSizeDelta(int size_delta) const {
return Derive(size_delta, GetFontStyle(), GetFontWeight());
}
FontList FontList::DeriveWithStyle(int font_style) const {
return Derive(0, font_style, GetFontWeight());
}
FontList FontList::DeriveWithWeight(Font::Weight weight) const {
return Derive(0, GetFontStyle(), weight);
}
FontList FontList::DeriveWithHeightUpperBound(int height) const {
FontList font_list(*this);
for (int font_size = font_list.GetFontSize(); font_size > 1; --font_size) {
const int internal_leading =
font_list.GetBaseline() - font_list.GetCapHeight();
// Some platforms don't support getting the cap height, and simply return
// the entire font ascent from GetCapHeight(). Centering the ascent makes
// the font look too low, so if GetCapHeight() returns the ascent, center
// the entire font height instead.
const int space =
height - ((internal_leading != 0) ?
font_list.GetCapHeight() : font_list.GetHeight());
const int y_offset = space / 2 - internal_leading;
const int space_at_bottom = height - (y_offset + font_list.GetHeight());
if ((y_offset >= 0) && (space_at_bottom >= 0))
break;
font_list = font_list.DeriveWithSizeDelta(-1);
}
return font_list;
}
int FontList::GetHeight() const {
return impl_->GetHeight();
}
int FontList::GetBaseline() const {
return impl_->GetBaseline();
}
int FontList::GetCapHeight() const {
return impl_->GetCapHeight();
}
int FontList::GetExpectedTextWidth(int length) const {
return impl_->GetExpectedTextWidth(length);
}
int FontList::GetFontStyle() const {
return impl_->GetFontStyle();
}
int FontList::GetFontSize() const {
return impl_->GetFontSize();
}
Font::Weight FontList::GetFontWeight() const {
return impl_->GetFontWeight();
}
const std::vector<Font>& FontList::GetFonts() const {
return impl_->GetFonts();
}
const Font& FontList::GetPrimaryFont() const {
return impl_->GetPrimaryFont();
}
FontList::FontList(FontListImpl* impl) : impl_(impl) {}
// static
const scoped_refptr<FontListImpl>& FontList::GetDefaultImpl() {
// SetDefaultFontDescription() must be called and the default font description
// must be set earlier than any call of this function.
DCHECK(!(g_default_font_description == NULL)) // != is not overloaded.
<< "SetDefaultFontDescription has not been called.";
if (!g_default_impl_initialized) {
g_default_impl.Get() =
g_default_font_description.Get().empty() ?
new FontListImpl(Font()) :
new FontListImpl(g_default_font_description.Get());
g_default_impl_initialized = true;
}
return g_default_impl.Get();
}
// static
std::string FontList::FirstAvailableOrFirst(const std::string& font_name_list) {
std::vector<std::string> families = base::SplitString(
font_name_list, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
if (families.empty())
return std::string();
if (families.size() == 1)
return families[0];
sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
for (const auto& family : families) {
if (IsFontFamilyAvailable(family, fm.get()))
return family;
}
return families[0];
}
} // namespace gfx