• 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 "ui/gfx/font.h"
12 #include "ui/gfx/gfx_export.h"
13 
14 namespace gfx {
15 
16 // FontList represents a list of fonts either in the form of Font vector or in
17 // the form of a string representing font names, styles, and size.
18 //
19 // The string representation is in the form "FAMILY_LIST [STYLE_OPTIONS] SIZE",
20 // where FAMILY_LIST is a comma separated list of families terminated by a
21 // comma, STYLE_OPTIONS is a whitespace separated list of words where each word
22 // describes one of style, variant, weight, stretch, or gravity, and SIZE is
23 // a decimal number followed by "px" for absolute size. STYLE_OPTIONS may be
24 // absent.
25 //
26 // The string format complies with that of Pango detailed at
27 // http://developer.gnome.org/pango/stable/pango-Fonts.html#pango-font-description-from-string
28 //
29 // FontList could be initialized either way without conversion to the other
30 // form. The conversion to the other form is done only when asked to get the
31 // other form.
32 //
33 // FontList allows operator= since FontList is a data member type in RenderText,
34 // and operator= is used in RenderText::SetFontList().
35 class GFX_EXPORT FontList {
36  public:
37   // Creates a font list with a Font with default name and style.
38   FontList();
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   // Sets the description string for default FontList construction. If it's
59   // empty, FontList will initialize using the default Font constructor.
60   //
61   // The client code must call this function before any call of the default
62   // constructor. This should be done on the UI thread.
63   //
64   // ui::ResourceBundle may call this function more than once when UI language
65   // is changed.
66   static void SetDefaultFontDescription(const std::string& font_description);
67 
68   // Returns a new FontList with the given |font_style| flags.
69   FontList DeriveFontList(int font_style) const;
70 
71   // Returns a new FontList with the same font names and style but with the
72   // given font |size| in pixels.
73   FontList DeriveFontListWithSize(int size) const;
74 
75   // Returns a new FontList with the same font names and style but resized.
76   // |size_delta| is the size in pixels to add to the current font size.
77   FontList DeriveFontListWithSizeDelta(int size_delta) const;
78 
79   // Returns a new FontList with the same font names but resized and the given
80   // style. |size_delta| is the size in pixels to add to the current font size.
81   // |font_style| specifies the new style, which is a bitmask of the values:
82   // Font::BOLD, Font::ITALIC and Font::UNDERLINE.
83   FontList DeriveFontListWithSizeDeltaAndStyle(int size_delta,
84                                                int font_style) const;
85 
86   // Returns the height of this font list, which is max(ascent) + max(descent)
87   // for all the fonts in the font list.
88   int GetHeight() const;
89 
90   // Returns the baseline of this font list, which is max(baseline) for all the
91   // fonts in the font list.
92   int GetBaseline() const;
93 
94   // Returns the cap height of this font list.
95   // Currently returns the cap height of the primary font.
96   int GetCapHeight() const;
97 
98   // Returns the number of horizontal pixels needed to display |text|.
99   int GetStringWidth(const base::string16& text) const;
100 
101   // Returns the expected number of horizontal pixels needed to display the
102   // specified length of characters. Call GetStringWidth() to retrieve the
103   // actual number.
104   int GetExpectedTextWidth(int length) const;
105 
106   // Returns the |gfx::Font::FontStyle| style flags for this font list.
107   int GetFontStyle() const;
108 
109   // Returns a string representing font names, styles, and size. If the FontList
110   // is initialized by a vector of Font, use the first font's style and size
111   // for the description.
112   const std::string& GetFontDescriptionString() const;
113 
114   // Returns the font size in pixels.
115   int GetFontSize() const;
116 
117   // Returns the Font vector.
118   const std::vector<Font>& GetFonts() const;
119 
120   // Returns the first font in the list.
121   const Font& GetPrimaryFont() const;
122 
123  private:
124   // Extracts common font height and baseline into |common_height_| and
125   // |common_baseline_|.
126   void CacheCommonFontHeightAndBaseline() const;
127 
128   // Extracts font style and size into |font_style_| and |font_size_|.
129   void CacheFontStyleAndSize() const;
130 
131   // A vector of Font. If FontList is constructed with font description string,
132   // |fonts_| is not initialized during construction. Instead, it is computed
133   // lazily when user asked to get the font vector.
134   mutable std::vector<Font> fonts_;
135 
136   // A string representing font names, styles, and sizes.
137   // Please refer to the comments before class declaration for details on string
138   // format.
139   // If FontList is constructed with a vector of font,
140   // |font_description_string_| is not initialized during construction. Instead,
141   // it is computed lazily when user asked to get the font description string.
142   mutable std::string font_description_string_;
143 
144   // The cached common height and baseline of the fonts in the font list.
145   mutable int common_height_;
146   mutable int common_baseline_;
147 
148   // Cached font style and size.
149   mutable int font_style_;
150   mutable int font_size_;
151 };
152 
153 }  // namespace gfx
154 
155 #endif  // UI_GFX_FONT_LIST_H_
156