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.
4
5 #include "ui/gfx/font_list.h"
6
7 #include "base/lazy_instance.h"
8 #include "base/strings/string_util.h"
9 #include "ui/gfx/font_list_impl.h"
10
11 namespace {
12
13 // Font description of the default font set.
14 base::LazyInstance<std::string>::Leaky g_default_font_description =
15 LAZY_INSTANCE_INITIALIZER;
16
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;
21
22 } // namespace
23
24 namespace gfx {
25
FontList()26 FontList::FontList() : impl_(GetDefaultImpl()) {}
27
FontList(const FontList & other)28 FontList::FontList(const FontList& other) : impl_(other.impl_) {}
29
FontList(const std::string & font_description_string)30 FontList::FontList(const std::string& font_description_string)
31 : impl_(new FontListImpl(font_description_string)) {}
32
FontList(const std::vector<std::string> & font_names,int font_style,int font_size)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)) {}
37
FontList(const std::vector<Font> & fonts)38 FontList::FontList(const std::vector<Font>& fonts)
39 : impl_(new FontListImpl(fonts)) {}
40
FontList(const Font & font)41 FontList::FontList(const Font& font) : impl_(new FontListImpl(font)) {}
42
~FontList()43 FontList::~FontList() {}
44
operator =(const FontList & other)45 FontList& FontList::operator=(const FontList& other) {
46 impl_ = other.impl_;
47 return *this;
48 }
49
50 // static
SetDefaultFontDescription(const std::string & font_description)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));
56
57 g_default_font_description.Get() = font_description;
58 g_default_impl_initialized = false;
59 }
60
Derive(int size_delta,int font_style) const61 FontList FontList::Derive(int size_delta, int font_style) const {
62 return FontList(impl_->Derive(size_delta, font_style));
63 }
64
DeriveWithSizeDelta(int size_delta) const65 FontList FontList::DeriveWithSizeDelta(int size_delta) const {
66 return Derive(size_delta, GetFontStyle());
67 }
68
DeriveWithStyle(int font_style) const69 FontList FontList::DeriveWithStyle(int font_style) const {
70 return Derive(0, font_style);
71 }
72
GetHeight() const73 int FontList::GetHeight() const {
74 return impl_->GetHeight();
75 }
76
GetBaseline() const77 int FontList::GetBaseline() const {
78 return impl_->GetBaseline();
79 }
80
GetCapHeight() const81 int FontList::GetCapHeight() const {
82 return impl_->GetCapHeight();
83 }
84
GetExpectedTextWidth(int length) const85 int FontList::GetExpectedTextWidth(int length) const {
86 return impl_->GetExpectedTextWidth(length);
87 }
88
GetFontStyle() const89 int FontList::GetFontStyle() const {
90 return impl_->GetFontStyle();
91 }
92
GetFontDescriptionString() const93 const std::string& FontList::GetFontDescriptionString() const {
94 return impl_->GetFontDescriptionString();
95 }
96
GetFontSize() const97 int FontList::GetFontSize() const {
98 return impl_->GetFontSize();
99 }
100
GetFonts() const101 const std::vector<Font>& FontList::GetFonts() const {
102 return impl_->GetFonts();
103 }
104
GetPrimaryFont() const105 const Font& FontList::GetPrimaryFont() const {
106 return impl_->GetPrimaryFont();
107 }
108
FontList(FontListImpl * impl)109 FontList::FontList(FontListImpl* impl) : impl_(impl) {}
110
111 // static
GetDefaultImpl()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.";
117
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;
124 }
125
126 return g_default_impl.Get();
127 }
128
129 } // namespace gfx
130