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_
11 #include "base/memory/ref_counted.h"
12 #include "ui/gfx/font.h"
13 #include "ui/gfx/gfx_export.h"
19 // FontList represents a list of fonts and provides metrics which are common
20 // in the fonts. FontList is copyable and it's quite cheap to copy.
22 // The format of font description string complies with that of Pango detailed at
23 // http://developer.gnome.org/pango/stable/pango-Fonts.html#pango-font-description-from-string
24 // The format is "<FONT_FAMILY_LIST>,[STYLES] <SIZE>" where
25 // FONT_FAMILY_LIST is a comma-separated list of font family names,
26 // STYLES is a space-separated list of style names ("Bold" and "Italic"),
27 // SIZE is a font size in pixel with the suffix "px".
28 // Here are examples of font description string:
29 // "Arial, Helvetica, Bold Italic 14px"
31 class GFX_EXPORT FontList
{
33 // Creates a font list with default font names, size and style, which are
34 // specified by SetDefaultFontDescription().
37 // Creates a font list that is a clone of another font list.
38 FontList(const FontList
& other
);
40 // Creates a font list from a string representing font names, styles, and
42 explicit FontList(const std::string
& font_description_string
);
44 // Creates a font list from font names, styles and size.
45 FontList(const std::vector
<std::string
>& font_names
,
49 // Creates a font list from a Font vector.
50 // All fonts in this vector should have the same style and size.
51 explicit FontList(const std::vector
<Font
>& fonts
);
53 // Creates a font list from a Font.
54 explicit FontList(const Font
& font
);
58 // Copies the given font list into this object.
59 FontList
& operator=(const FontList
& other
);
61 // Sets the description string for default FontList construction. If it's
62 // empty, FontList will initialize using the default Font constructor.
64 // The client code must call this function before any call of the default
65 // constructor. This should be done on the UI thread.
67 // ui::ResourceBundle may call this function more than once when UI language
69 static void SetDefaultFontDescription(const std::string
& font_description
);
71 // Returns a new FontList with the same font names but resized and the given
72 // style. |size_delta| is the size in pixels to add to the current font size.
73 // |font_style| specifies the new style, which is a bitmask of the values:
74 // Font::BOLD, Font::ITALIC and Font::UNDERLINE.
75 FontList
Derive(int size_delta
, int font_style
) const;
77 // Returns a new FontList with the same font names and style but resized.
78 // |size_delta| is the size in pixels to add to the current font size.
79 FontList
DeriveWithSizeDelta(int size_delta
) const;
81 // Returns a new FontList with the same font names and size but the given
82 // style. |font_style| specifies the new style, which is a bitmask of the
83 // values: Font::BOLD, Font::ITALIC and Font::UNDERLINE.
84 FontList
DeriveWithStyle(int font_style
) const;
86 // Shrinks the font size until the font list fits within |height| while
87 // having its cap height vertically centered. Returns a new FontList with
88 // the correct height.
90 // The expected layout:
91 // +--------+-----------------------------------------------+------------+
92 // | | y offset | space |
93 // | +--------+-------------------+------------------+ above |
94 // | | | | internal leading | cap height |
95 // | box | font | ascent (baseline) +------------------+------------+
96 // | height | height | | cap height |
97 // | | |-------------------+------------------+------------+
98 // | | | descent (height - baseline) | space |
99 // | +--------+--------------------------------------+ below |
100 // | | space at bottom | cap height |
101 // +--------+-----------------------------------------------+------------+
103 // center of box height == center of cap height
104 // (i.e. space above cap height == space below cap height)
107 // space at bottom >= 0
108 // (i.e. Entire font must be visible inside the box.)
109 gfx::FontList
DeriveWithHeightUpperBound(int height
) const;
111 // Returns the height of this font list, which is max(ascent) + max(descent)
112 // for all the fonts in the font list.
113 int GetHeight() const;
115 // Returns the baseline of this font list, which is max(baseline) for all the
116 // fonts in the font list.
117 int GetBaseline() const;
119 // Returns the cap height of this font list.
120 // Currently returns the cap height of the primary font.
121 int GetCapHeight() const;
123 // Returns the expected number of horizontal pixels needed to display the
124 // specified length of characters. Call GetStringWidth() to retrieve the
126 int GetExpectedTextWidth(int length
) const;
128 // Returns the |gfx::Font::FontStyle| style flags for this font list.
129 int GetFontStyle() const;
131 // Returns a string representing font names, styles, and size. If the FontList
132 // is initialized by a vector of Font, use the first font's style and size
133 // for the description.
134 const std::string
& GetFontDescriptionString() const;
136 // Returns the font size in pixels.
137 int GetFontSize() const;
139 // Returns the Font vector.
140 const std::vector
<Font
>& GetFonts() const;
142 // Returns the first font in the list.
143 const Font
& GetPrimaryFont() const;
146 explicit FontList(FontListImpl
* impl
);
148 static const scoped_refptr
<FontListImpl
>& GetDefaultImpl();
150 scoped_refptr
<FontListImpl
> impl_
;
155 #endif // UI_GFX_FONT_LIST_H_