font_list_impl.h 4.4 KB

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