font_list.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #ifndef UI_GFX_FONT_LIST_H_
  5. #define UI_GFX_FONT_LIST_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/memory/ref_counted.h"
  9. #include "ui/gfx/font.h"
  10. #include "ui/gfx/gfx_export.h"
  11. namespace gfx {
  12. class FontListImpl;
  13. // FontList represents a list of fonts and provides metrics which are common
  14. // across the fonts. FontList is copyable and quite cheap to copy.
  15. //
  16. // The format of font description strings is a subset of that used by Pango, as
  17. // described at
  18. // http://developer.gnome.org/pango/stable/pango-Fonts.html#pango-font-description-from-string
  19. //
  20. // Pango font description strings should not be passed directly into FontLists.
  21. //
  22. // The format is "<FONT_FAMILY_LIST>,[STYLES] <SIZE>", where:
  23. // - FONT_FAMILY_LIST is a comma-separated list of font family names,
  24. // - STYLES is an optional space-separated list of style names (case-sensitive
  25. // "Italic" "Ultra-Light" "Light" "Normal" "Semi-Bold" "Bold" "Ultra-Bold"
  26. // "Heavy" are supported), and
  27. // - SIZE is an integer font size in pixels with the suffix "px"
  28. //
  29. // Here are examples of valid font description strings:
  30. // - "Arial, Helvetica, Italic Semi-Bold 14px"
  31. // - "Arial, 14px"
  32. class GFX_EXPORT FontList {
  33. public:
  34. // Parses a FontList description string into |families_out|, |style_out| (a
  35. // bitfield of gfx::Font::Style values), |size_pixels_out| and |weight_out|.
  36. // Returns true if the string is properly-formed.
  37. static bool ParseDescription(const std::string& description,
  38. std::vector<std::string>* families_out,
  39. int* style_out,
  40. int* size_pixels_out,
  41. Font::Weight* weight_out);
  42. // Creates a font list with default font names, size and style, which are
  43. // specified by SetDefaultFontDescription().
  44. FontList();
  45. // Creates a font list that is a clone of another font list.
  46. FontList(const FontList& other);
  47. // Creates a font list from a string representing font names, styles, and
  48. // size.
  49. explicit FontList(const std::string& font_description_string);
  50. // Creates a font list from font names, styles, size and weight.
  51. FontList(const std::vector<std::string>& font_names,
  52. int font_style,
  53. int font_size,
  54. Font::Weight font_weight);
  55. // Creates a font list from a Font vector.
  56. // All fonts in this vector should have the same style and size.
  57. explicit FontList(const std::vector<Font>& fonts);
  58. // Creates a font list from a Font.
  59. explicit FontList(const Font& font);
  60. ~FontList();
  61. // Copies the given font list into this object.
  62. FontList& operator=(const FontList& other);
  63. // Sets the description string for default FontList construction. If it's
  64. // empty, FontList will initialize using the default Font constructor.
  65. //
  66. // The client code must call this function before any call of the default
  67. // constructor. This should be done on the UI thread.
  68. //
  69. // ui::ResourceBundle may call this function more than once when UI language
  70. // is changed.
  71. //
  72. // Unit Tests should use ScopedDefaultFontDescription instead of calling this
  73. // directly, to avoid leaving the default font description in an unexpected
  74. // state for tests that run in the same process.
  75. static void SetDefaultFontDescription(const std::string& font_description);
  76. // Returns a new FontList with the same font names but resized and the given
  77. // style and weight. |size_delta| is the size in pixels to add to the current
  78. // font size. |font_style| specifies the new style, which is a bitmask of the
  79. // values: Font::ITALIC and Font::UNDERLINE. |weight| is the requested font
  80. // weight.
  81. FontList Derive(int size_delta,
  82. int font_style,
  83. Font::Weight weight) const;
  84. // Returns a new FontList with the same font names and style but resized.
  85. // |size_delta| is the size in pixels to add to the current font size.
  86. FontList DeriveWithSizeDelta(int size_delta) const;
  87. // Returns a new FontList with the same font names, weight and size but the
  88. // given style. |font_style| specifies the new style, which is a bitmask of
  89. // the values: Font::ITALIC and Font::UNDERLINE.
  90. FontList DeriveWithStyle(int font_style) const;
  91. // Returns a new FontList with the same font name, size and style but with
  92. // the given weight.
  93. FontList DeriveWithWeight(Font::Weight weight) const;
  94. // Shrinks the font size until the font list fits within |height| while
  95. // having its cap height vertically centered. Returns a new FontList with
  96. // the correct height.
  97. //
  98. // The expected layout:
  99. // +--------+-----------------------------------------------+------------+
  100. // | | y offset | space |
  101. // | +--------+-------------------+------------------+ above |
  102. // | | | | internal leading | cap height |
  103. // | box | font | ascent (baseline) +------------------+------------+
  104. // | height | height | | cap height |
  105. // | | |-------------------+------------------+------------+
  106. // | | | descent (height - baseline) | space |
  107. // | +--------+--------------------------------------+ below |
  108. // | | space at bottom | cap height |
  109. // +--------+-----------------------------------------------+------------+
  110. // Goal:
  111. // center of box height == center of cap height
  112. // (i.e. space above cap height == space below cap height)
  113. // Restrictions:
  114. // y offset >= 0
  115. // space at bottom >= 0
  116. // (i.e. Entire font must be visible inside the box.)
  117. FontList DeriveWithHeightUpperBound(int height) const;
  118. // Returns the height of this font list, which is max(ascent) + max(descent)
  119. // for all the fonts in the font list.
  120. int GetHeight() const;
  121. // Returns the baseline of this font list, which is max(baseline) for all the
  122. // fonts in the font list.
  123. int GetBaseline() const;
  124. // Returns the cap height of this font list.
  125. // Currently returns the cap height of the primary font.
  126. int GetCapHeight() const;
  127. // Returns the expected number of horizontal pixels needed to display the
  128. // specified length of characters. Call GetStringWidth() to retrieve the
  129. // actual number.
  130. int GetExpectedTextWidth(int length) const;
  131. // Returns the |Font::FontStyle| style flags for this font list.
  132. int GetFontStyle() const;
  133. // Returns the font size in pixels.
  134. int GetFontSize() const;
  135. // Returns the font weight.
  136. Font::Weight GetFontWeight() const;
  137. // Returns the Font vector.
  138. const std::vector<Font>& GetFonts() const;
  139. // Returns the first font in the list.
  140. const Font& GetPrimaryFont() const;
  141. // Returns the first available font name. If there is no available font,
  142. // returns the first font name. Empty entries are ignored.
  143. // Used by Blink and webui to pick the primary standard/serif/sans/fixed/etc.
  144. // fonts from region-specific IDS lists.
  145. static std::string FirstAvailableOrFirst(const std::string& font_name_list);
  146. private:
  147. explicit FontList(FontListImpl* impl);
  148. static const scoped_refptr<FontListImpl>& GetDefaultImpl();
  149. scoped_refptr<FontListImpl> impl_;
  150. };
  151. } // namespace gfx
  152. #endif // UI_GFX_FONT_LIST_H_