Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / font_list.h
blob2b67c4a13e2cf2e5b7bc6e93c93eac128ef5a3fa
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 #ifndef UI_GFX_FONT_LIST_H_
6 #define UI_GFX_FONT_LIST_H_
8 #include <string>
9 #include <vector>
11 #include "base/memory/ref_counted.h"
12 #include "ui/gfx/font.h"
13 #include "ui/gfx/gfx_export.h"
15 namespace gfx {
17 class FontListImpl;
19 // FontList represents a list of fonts and provides metrics which are common
20 // across the fonts. FontList is copyable and quite cheap to copy.
22 // The format of font description strings is a subset of that used by Pango, as
23 // described at
24 // http://developer.gnome.org/pango/stable/pango-Fonts.html#pango-font-description-from-string
26 // Pango font description strings should not be passed directly into FontLists.
28 // The format is "<FONT_FAMILY_LIST>,[STYLES] <SIZE>", where:
29 // - FONT_FAMILY_LIST is a comma-separated list of font family names,
30 // - STYLES is an optional space-separated list of style names (case-sensitive
31 // "Bold" and "Italic" are supported), and
32 // - SIZE is an integer font size in pixels with the suffix "px".
34 // Here are examples of valid font description strings:
35 // - "Arial, Helvetica, Bold Italic 14px"
36 // - "Arial, 14px"
37 class GFX_EXPORT FontList {
38 public:
39 // Parses a FontList description string into |families_out|, |style_out| (a
40 // bitfield of gfx::Font::Style values), and |size_pixels_out|. Returns true
41 // if the string is properly-formed.
42 static bool ParseDescription(const std::string& description,
43 std::vector<std::string>* families_out,
44 int* style_out,
45 int* size_pixels_out);
47 // Creates a font list with default font names, size and style, which are
48 // specified by SetDefaultFontDescription().
49 FontList();
51 // Creates a font list that is a clone of another font list.
52 FontList(const FontList& other);
54 // Creates a font list from a string representing font names, styles, and
55 // size.
56 explicit FontList(const std::string& font_description_string);
58 // Creates a font list from font names, styles and size.
59 FontList(const std::vector<std::string>& font_names,
60 int font_style,
61 int font_size);
63 // Creates a font list from a Font vector.
64 // All fonts in this vector should have the same style and size.
65 explicit FontList(const std::vector<Font>& fonts);
67 // Creates a font list from a Font.
68 explicit FontList(const Font& font);
70 ~FontList();
72 // Copies the given font list into this object.
73 FontList& operator=(const FontList& other);
75 // Sets the description string for default FontList construction. If it's
76 // empty, FontList will initialize using the default Font constructor.
78 // The client code must call this function before any call of the default
79 // constructor. This should be done on the UI thread.
81 // ui::ResourceBundle may call this function more than once when UI language
82 // is changed.
83 static void SetDefaultFontDescription(const std::string& font_description);
85 // Returns a new FontList with the same font names but resized and the given
86 // style. |size_delta| is the size in pixels to add to the current font size.
87 // |font_style| specifies the new style, which is a bitmask of the values:
88 // Font::BOLD, Font::ITALIC and Font::UNDERLINE.
89 FontList Derive(int size_delta, int font_style) const;
91 // Returns a new FontList with the same font names and style but resized.
92 // |size_delta| is the size in pixels to add to the current font size.
93 FontList DeriveWithSizeDelta(int size_delta) const;
95 // Returns a new FontList with the same font names and size but the given
96 // style. |font_style| specifies the new style, which is a bitmask of the
97 // values: Font::BOLD, Font::ITALIC and Font::UNDERLINE.
98 FontList DeriveWithStyle(int font_style) const;
100 // Shrinks the font size until the font list fits within |height| while
101 // having its cap height vertically centered. Returns a new FontList with
102 // the correct height.
104 // The expected layout:
105 // +--------+-----------------------------------------------+------------+
106 // | | y offset | space |
107 // | +--------+-------------------+------------------+ above |
108 // | | | | internal leading | cap height |
109 // | box | font | ascent (baseline) +------------------+------------+
110 // | height | height | | cap height |
111 // | | |-------------------+------------------+------------+
112 // | | | descent (height - baseline) | space |
113 // | +--------+--------------------------------------+ below |
114 // | | space at bottom | cap height |
115 // +--------+-----------------------------------------------+------------+
116 // Goal:
117 // center of box height == center of cap height
118 // (i.e. space above cap height == space below cap height)
119 // Restrictions:
120 // y offset >= 0
121 // space at bottom >= 0
122 // (i.e. Entire font must be visible inside the box.)
123 gfx::FontList DeriveWithHeightUpperBound(int height) const;
125 // Returns the height of this font list, which is max(ascent) + max(descent)
126 // for all the fonts in the font list.
127 int GetHeight() const;
129 // Returns the baseline of this font list, which is max(baseline) for all the
130 // fonts in the font list.
131 int GetBaseline() const;
133 // Returns the cap height of this font list.
134 // Currently returns the cap height of the primary font.
135 int GetCapHeight() const;
137 // Returns the expected number of horizontal pixels needed to display the
138 // specified length of characters. Call GetStringWidth() to retrieve the
139 // actual number.
140 int GetExpectedTextWidth(int length) const;
142 // Returns the |gfx::Font::FontStyle| style flags for this font list.
143 int GetFontStyle() const;
145 // Returns the font size in pixels.
146 int GetFontSize() const;
148 // Returns the Font vector.
149 const std::vector<Font>& GetFonts() const;
151 // Returns the first font in the list.
152 const Font& GetPrimaryFont() const;
154 private:
155 explicit FontList(FontListImpl* impl);
157 static const scoped_refptr<FontListImpl>& GetDefaultImpl();
159 scoped_refptr<FontListImpl> impl_;
162 } // namespace gfx
164 #endif // UI_GFX_FONT_LIST_H_