Add a basic settings page with a Polymer paper element which loads at chrome://md...
[chromium-blink-merge.git] / ui / gfx / font_list.cc
blobca0c8ade58410c5604dbde0de45f5539439ded6e
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 gfx::FontList FontList::DeriveWithHeightUpperBound(int height) const {
74 gfx::FontList font_list(*this);
75 for (int font_size = font_list.GetFontSize(); font_size > 1; --font_size) {
76 const int internal_leading =
77 font_list.GetBaseline() - font_list.GetCapHeight();
78 // Some platforms don't support getting the cap height, and simply return
79 // the entire font ascent from GetCapHeight(). Centering the ascent makes
80 // the font look too low, so if GetCapHeight() returns the ascent, center
81 // the entire font height instead.
82 const int space =
83 height - ((internal_leading != 0) ?
84 font_list.GetCapHeight() : font_list.GetHeight());
85 const int y_offset = space / 2 - internal_leading;
86 const int space_at_bottom = height - (y_offset + font_list.GetHeight());
87 if ((y_offset >= 0) && (space_at_bottom >= 0))
88 break;
89 font_list = font_list.DeriveWithSizeDelta(-1);
91 return font_list;
94 int FontList::GetHeight() const {
95 return impl_->GetHeight();
98 int FontList::GetBaseline() const {
99 return impl_->GetBaseline();
102 int FontList::GetCapHeight() const {
103 return impl_->GetCapHeight();
106 int FontList::GetExpectedTextWidth(int length) const {
107 return impl_->GetExpectedTextWidth(length);
110 int FontList::GetFontStyle() const {
111 return impl_->GetFontStyle();
114 const std::string& FontList::GetFontDescriptionString() const {
115 return impl_->GetFontDescriptionString();
118 int FontList::GetFontSize() const {
119 return impl_->GetFontSize();
122 const std::vector<Font>& FontList::GetFonts() const {
123 return impl_->GetFonts();
126 const Font& FontList::GetPrimaryFont() const {
127 return impl_->GetPrimaryFont();
130 FontList::FontList(FontListImpl* impl) : impl_(impl) {}
132 // static
133 const scoped_refptr<FontListImpl>& FontList::GetDefaultImpl() {
134 // SetDefaultFontDescription() must be called and the default font description
135 // must be set earlier than any call of this function.
136 DCHECK(!(g_default_font_description == NULL)) // != is not overloaded.
137 << "SetDefaultFontDescription has not been called.";
139 if (!g_default_impl_initialized) {
140 g_default_impl.Get() =
141 g_default_font_description.Get().empty() ?
142 new FontListImpl(Font()) :
143 new FontListImpl(g_default_font_description.Get());
144 g_default_impl_initialized = true;
147 return g_default_impl.Get();
150 } // namespace gfx