[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 1 | // Copyright 2014 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_IMPL_H_ |
| 6 | #define UI_GFX_FONT_LIST_IMPL_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/memory/ref_counted.h" |
mboc | 998e890 | 2016-06-02 11:40:35 | [diff] [blame] | 12 | #include "ui/gfx/font.h" |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 13 | |
| 14 | namespace gfx { |
| 15 | |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 16 | // FontListImpl is designed to provide the implementation of FontList and |
| 17 | // intended to be used only from FontList. You must not use this class |
| 18 | // directly. |
| 19 | // |
| 20 | // FontListImpl represents a list of fonts either in the form of Font vector or |
| 21 | // in the form of a string representing font names, styles, and size. |
| 22 | // |
| 23 | // FontListImpl could be initialized either way without conversion to the other |
| 24 | // form. The conversion to the other form is done only when asked to get the |
| 25 | // other form. |
| 26 | // |
| 27 | // For the format of font description string, see font_list.h for details. |
| 28 | class FontListImpl : public base::RefCounted<FontListImpl> { |
| 29 | public: |
| 30 | // Creates a font list from a string representing font names, styles, and |
| 31 | // size. |
| 32 | explicit FontListImpl(const std::string& font_description_string); |
| 33 | |
mboc | 998e890 | 2016-06-02 11:40:35 | [diff] [blame] | 34 | // Creates a font list from font names, styles, size and weight. |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 35 | FontListImpl(const std::vector<std::string>& font_names, |
| 36 | int font_style, |
mboc | 998e890 | 2016-06-02 11:40:35 | [diff] [blame] | 37 | int font_size, |
| 38 | Font::Weight font_weight); |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 39 | |
| 40 | // Creates a font list from a Font vector. |
| 41 | // All fonts in this vector should have the same style and size. |
| 42 | explicit FontListImpl(const std::vector<Font>& fonts); |
| 43 | |
| 44 | // Creates a font list from a Font. |
| 45 | explicit FontListImpl(const Font& font); |
| 46 | |
| 47 | // Returns a new FontListImpl with the same font names but resized and the |
mboc | 998e890 | 2016-06-02 11:40:35 | [diff] [blame] | 48 | // given style and weight. |size_delta| is the size in pixels to add to the |
| 49 | // current font size. |font_style| specifies the new style, which is a |
| 50 | // bitmask of the values: Font::ITALIC and Font::UNDERLINE. |
| 51 | FontListImpl* Derive(int size_delta, |
| 52 | int font_style, |
| 53 | Font::Weight weight) const; |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 54 | |
| 55 | // Returns the height of this font list, which is max(ascent) + max(descent) |
| 56 | // for all the fonts in the font list. |
| 57 | int GetHeight() const; |
| 58 | |
| 59 | // Returns the baseline of this font list, which is max(baseline) for all the |
| 60 | // fonts in the font list. |
| 61 | int GetBaseline() const; |
| 62 | |
| 63 | // Returns the cap height of this font list. |
| 64 | // Currently returns the cap height of the primary font. |
| 65 | int GetCapHeight() const; |
| 66 | |
| 67 | // Returns the expected number of horizontal pixels needed to display the |
| 68 | // specified length of characters. Call GetStringWidth() to retrieve the |
| 69 | // actual number. |
| 70 | int GetExpectedTextWidth(int length) const; |
| 71 | |
mboc | 998e890 | 2016-06-02 11:40:35 | [diff] [blame] | 72 | // Returns the |Font::FontStyle| style flags for this font list. |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 73 | int GetFontStyle() const; |
| 74 | |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 75 | // Returns the font size in pixels. |
| 76 | int GetFontSize() const; |
| 77 | |
mboc | 998e890 | 2016-06-02 11:40:35 | [diff] [blame] | 78 | // Returns the font weight. |
| 79 | Font::Weight GetFontWeight() const; |
| 80 | |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 81 | // Returns the Font vector. |
| 82 | const std::vector<Font>& GetFonts() const; |
| 83 | |
| 84 | // Returns the first font in the list. |
| 85 | const Font& GetPrimaryFont() const; |
| 86 | |
| 87 | private: |
| 88 | friend class base::RefCounted<FontListImpl>; |
| 89 | |
| 90 | ~FontListImpl(); |
| 91 | |
| 92 | // Extracts common font height and baseline into |common_height_| and |
| 93 | // |common_baseline_|. |
| 94 | void CacheCommonFontHeightAndBaseline() const; |
| 95 | |
| 96 | // Extracts font style and size into |font_style_| and |font_size_|. |
| 97 | void CacheFontStyleAndSize() const; |
| 98 | |
| 99 | // A vector of Font. If FontListImpl is constructed with font description |
| 100 | // string, |fonts_| is not initialized during construction. Instead, it is |
| 101 | // computed lazily when user asked to get the font vector. |
| 102 | mutable std::vector<Font> fonts_; |
| 103 | |
| 104 | // A string representing font names, styles, and sizes. |
| 105 | // Please refer to the comments before class declaration for details on string |
| 106 | // format. |
| 107 | // If FontListImpl is constructed with a vector of font, |
| 108 | // |font_description_string_| is not initialized during construction. Instead, |
| 109 | // it is computed lazily when user asked to get the font description string. |
derat | 23144a6 | 2015-02-10 14:48:25 | [diff] [blame] | 110 | // |
| 111 | // TODO(derat): Remove laziness so that this can be removed. |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 112 | mutable std::string font_description_string_; |
| 113 | |
| 114 | // The cached common height and baseline of the fonts in the font list. |
| 115 | mutable int common_height_; |
| 116 | mutable int common_baseline_; |
| 117 | |
| 118 | // Cached font style and size. |
| 119 | mutable int font_style_; |
| 120 | mutable int font_size_; |
mboc | 998e890 | 2016-06-02 11:40:35 | [diff] [blame] | 121 | mutable Font::Weight font_weight_; |
[email protected] | 2a25d802 | 2014-02-09 03:12:55 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | } // namespace gfx |
| 125 | |
| 126 | #endif // UI_GFX_FONT_LIST_IMPL_H_ |