• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef UI_GFX_FONT_LIST_H_
6 #define UI_GFX_FONT_LIST_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/memory/ref_counted.h"
12 #include "ui/gfx/font.h"
13 #include "ui/gfx/gfx_export.h"
14 
15 namespace gfx {
16 
17 class FontListImpl;
18 
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.
21 //
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"
30 //     "Arial, 14px"
31 class GFX_EXPORT FontList {
32  public:
33   // Creates a font list with default font names, size and style, which are
34   // specified by SetDefaultFontDescription().
35   FontList();
36 
37   // Creates a font list that is a clone of another font list.
38   FontList(const FontList& other);
39 
40   // Creates a font list from a string representing font names, styles, and
41   // size.
42   explicit FontList(const std::string& font_description_string);
43 
44   // Creates a font list from font names, styles and size.
45   FontList(const std::vector<std::string>& font_names,
46            int font_style,
47            int font_size);
48 
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);
52 
53   // Creates a font list from a Font.
54   explicit FontList(const Font& font);
55 
56   ~FontList();
57 
58   // Copies the given font list into this object.
59   FontList& operator=(const FontList& other);
60 
61   // Sets the description string for default FontList construction. If it's
62   // empty, FontList will initialize using the default Font constructor.
63   //
64   // The client code must call this function before any call of the default
65   // constructor. This should be done on the UI thread.
66   //
67   // ui::ResourceBundle may call this function more than once when UI language
68   // is changed.
69   static void SetDefaultFontDescription(const std::string& font_description);
70 
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;
76 
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;
80 
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;
85 
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.
89   //
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   //   +--------+-----------------------------------------------+------------+
102   // Goal:
103   //     center of box height == center of cap height
104   //     (i.e. space above cap height == space below cap height)
105   // Restrictions:
106   //     y offset >= 0
107   //     space at bottom >= 0
108   //     (i.e. Entire font must be visible inside the box.)
109   gfx::FontList DeriveWithHeightUpperBound(int height) const;
110 
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;
114 
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;
118 
119   // Returns the cap height of this font list.
120   // Currently returns the cap height of the primary font.
121   int GetCapHeight() const;
122 
123   // Returns the expected number of horizontal pixels needed to display the
124   // specified length of characters. Call GetStringWidth() to retrieve the
125   // actual number.
126   int GetExpectedTextWidth(int length) const;
127 
128   // Returns the |gfx::Font::FontStyle| style flags for this font list.
129   int GetFontStyle() const;
130 
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;
135 
136   // Returns the font size in pixels.
137   int GetFontSize() const;
138 
139   // Returns the Font vector.
140   const std::vector<Font>& GetFonts() const;
141 
142   // Returns the first font in the list.
143   const Font& GetPrimaryFont() const;
144 
145  private:
146   explicit FontList(FontListImpl* impl);
147 
148   static const scoped_refptr<FontListImpl>& GetDefaultImpl();
149 
150   scoped_refptr<FontListImpl> impl_;
151 };
152 
153 }  // namespace gfx
154 
155 #endif  // UI_GFX_FONT_LIST_H_
156