Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / gfx / font_list.cc
blob920204bf7a74dcc0c7bd58e73242846852b989d0
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
3 // found in the LICENSE file.
5 #include "ui/gfx/font_list.h"
7 #include "base/lazy_instance.h"
8 #include "base/strings/string_util.h"
9 #include "ui/gfx/font_list_impl.h"
11 namespace {
13 // Font description of the default font set.
14 base::LazyInstance<std::string>::Leaky g_default_font_description =
15 LAZY_INSTANCE_INITIALIZER;
17 // The default instance of gfx::FontListImpl.
18 base::LazyInstance<scoped_refptr<gfx::FontListImpl> >::Leaky g_default_impl =
19 LAZY_INSTANCE_INITIALIZER;
20 bool g_default_impl_initialized = false;
22 } // namespace
24 namespace gfx {
26 FontList::FontList() : impl_(GetDefaultImpl()) {}
28 FontList::FontList(const FontList& other) : impl_(other.impl_) {}
30 FontList::FontList(const std::string& font_description_string)
31 : impl_(new FontListImpl(font_description_string)) {}
33 FontList::FontList(const std::vector<std::string>& font_names,
34 int font_style,
35 int font_size)
36 : impl_(new FontListImpl(font_names, font_style, font_size)) {}
38 FontList::FontList(const std::vector<Font>& fonts)
39 : impl_(new FontListImpl(fonts)) {}
41 FontList::FontList(const Font& font) : impl_(new FontListImpl(font)) {}
43 FontList::~FontList() {}
45 FontList& FontList::operator=(const FontList& other) {
46 impl_ = other.impl_;
47 return *this;
50 // static
51 void FontList::SetDefaultFontDescription(const std::string& font_description) {
52 // The description string must end with "px" for size in pixel, or must be
53 // the empty string, which specifies to use a single default font.
54 DCHECK(font_description.empty() ||
55 EndsWith(font_description, "px", true));
57 g_default_font_description.Get() = font_description;
58 g_default_impl_initialized = false;
61 FontList FontList::Derive(int size_delta, int font_style) const {
62 return FontList(impl_->Derive(size_delta, font_style));
65 FontList FontList::DeriveWithSizeDelta(int size_delta) const {
66 return Derive(size_delta, GetFontStyle());
69 FontList FontList::DeriveWithStyle(int font_style) const {
70 return Derive(0, font_style);
73 int FontList::GetHeight() const {
74 return impl_->GetHeight();
77 int FontList::GetBaseline() const {
78 return impl_->GetBaseline();
81 int FontList::GetCapHeight() const {
82 return impl_->GetCapHeight();
85 int FontList::GetExpectedTextWidth(int length) const {
86 return impl_->GetExpectedTextWidth(length);
89 int FontList::GetFontStyle() const {
90 return impl_->GetFontStyle();
93 const std::string& FontList::GetFontDescriptionString() const {
94 return impl_->GetFontDescriptionString();
97 int FontList::GetFontSize() const {
98 return impl_->GetFontSize();
101 const std::vector<Font>& FontList::GetFonts() const {
102 return impl_->GetFonts();
105 const Font& FontList::GetPrimaryFont() const {
106 return impl_->GetPrimaryFont();
109 FontList::FontList(FontListImpl* impl) : impl_(impl) {}
111 // static
112 const scoped_refptr<FontListImpl>& FontList::GetDefaultImpl() {
113 // SetDefaultFontDescription() must be called and the default font description
114 // must be set earlier than any call of this function.
115 DCHECK(!(g_default_font_description == NULL)) // != is not overloaded.
116 << "SetDefaultFontDescription has not been called.";
118 if (!g_default_impl_initialized) {
119 g_default_impl.Get() =
120 g_default_font_description.Get().empty() ?
121 new FontListImpl(Font()) :
122 new FontListImpl(g_default_font_description.Get());
123 g_default_impl_initialized = true;
126 return g_default_impl.Get();
129 } // namespace gfx