Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / font_list.h
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 default font names, size and style, which are
38   // specified by SetDefaultFontDescription().
39   FontList();
40
41   // Creates a font list from a string representing font names, styles, and
42   // size.
43   explicit FontList(const std::string& font_description_string);
44
45   // Creates a font list from font names, styles and size.
46   FontList(const std::vector<std::string>& font_names,
47            int font_style,
48            int font_size);
49
50   // Creates a font list from a Font vector.
51   // All fonts in this vector should have the same style and size.
52   explicit FontList(const std::vector<Font>& fonts);
53
54   // Creates a font list from a Font.
55   explicit FontList(const Font& font);
56
57   ~FontList();
58
59   // Sets the description string for default FontList construction. If it's
60   // empty, FontList will initialize using the default Font constructor.
61   //
62   // The client code must call this function before any call of the default
63   // constructor. This should be done on the UI thread.
64   //
65   // ui::ResourceBundle may call this function more than once when UI language
66   // is changed.
67   static void SetDefaultFontDescription(const std::string& font_description);
68
69   // Returns a new FontList with the given |font_style| flags.
70   FontList DeriveFontList(int font_style) const;
71
72   // Returns a new FontList with the same font names and style but with the
73   // given font |size| in pixels.
74   FontList DeriveFontListWithSize(int size) const;
75
76   // Returns a new FontList with the same font names and style but resized.
77   // |size_delta| is the size in pixels to add to the current font size.
78   FontList DeriveFontListWithSizeDelta(int size_delta) const;
79
80   // Returns a new FontList with the same font names but resized and the given
81   // style. |size_delta| is the size in pixels to add to the current font size.
82   // |font_style| specifies the new style, which is a bitmask of the values:
83   // Font::BOLD, Font::ITALIC and Font::UNDERLINE.
84   FontList DeriveFontListWithSizeDeltaAndStyle(int size_delta,
85                                                int font_style) const;
86
87   // Returns the height of this font list, which is max(ascent) + max(descent)
88   // for all the fonts in the font list.
89   int GetHeight() const;
90
91   // Returns the baseline of this font list, which is max(baseline) for all the
92   // fonts in the font list.
93   int GetBaseline() const;
94
95   // Returns the cap height of this font list.
96   // Currently returns the cap height of the primary font.
97   int GetCapHeight() const;
98
99   // Returns the expected number of horizontal pixels needed to display the
100   // specified length of characters. Call GetStringWidth() to retrieve the
101   // actual number.
102   int GetExpectedTextWidth(int length) const;
103
104   // Returns the |gfx::Font::FontStyle| style flags for this font list.
105   int GetFontStyle() const;
106
107   // Returns a string representing font names, styles, and size. If the FontList
108   // is initialized by a vector of Font, use the first font's style and size
109   // for the description.
110   const std::string& GetFontDescriptionString() const;
111
112   // Returns the font size in pixels.
113   int GetFontSize() const;
114
115   // Returns the Font vector.
116   const std::vector<Font>& GetFonts() const;
117
118   // Returns the first font in the list.
119   const Font& GetPrimaryFont() const;
120
121  private:
122   // Extracts common font height and baseline into |common_height_| and
123   // |common_baseline_|.
124   void CacheCommonFontHeightAndBaseline() const;
125
126   // Extracts font style and size into |font_style_| and |font_size_|.
127   void CacheFontStyleAndSize() const;
128
129   // A vector of Font. If FontList is constructed with font description string,
130   // |fonts_| is not initialized during construction. Instead, it is computed
131   // lazily when user asked to get the font vector.
132   mutable std::vector<Font> fonts_;
133
134   // A string representing font names, styles, and sizes.
135   // Please refer to the comments before class declaration for details on string
136   // format.
137   // If FontList is constructed with a vector of font,
138   // |font_description_string_| is not initialized during construction. Instead,
139   // it is computed lazily when user asked to get the font description string.
140   mutable std::string font_description_string_;
141
142   // The cached common height and baseline of the fonts in the font list.
143   mutable int common_height_;
144   mutable int common_baseline_;
145
146   // Cached font style and size.
147   mutable int font_style_;
148   mutable int font_size_;
149 };
150
151 }  // namespace gfx
152
153 #endif  // UI_GFX_FONT_LIST_H_