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_H_ 6 #define UI_GFX_FONT_H_ 7 8 #include <string> 9 10 #include "base/memory/ref_counted.h" 11 #include "base/strings/string16.h" 12 #include "ui/gfx/gfx_export.h" 13 #include "ui/gfx/native_widget_types.h" 14 15 namespace gfx { 16 17 class PlatformFont; 18 19 // Font provides a wrapper around an underlying font. Copy and assignment 20 // operators are explicitly allowed, and cheap. 21 // 22 // Figure of font metrics: 23 // +--------+-------------------+------------------+ 24 // | | | internal leading | 25 // | | ascent (baseline) +------------------+ 26 // | height | | cap height | 27 // | |-------------------+------------------+ 28 // | | descent (height - baseline) | 29 // +--------+--------------------------------------+ 30 class GFX_EXPORT Font { 31 public: 32 // The following constants indicate the font style. 33 enum FontStyle { 34 NORMAL = 0, 35 BOLD = 1, 36 ITALIC = 2, 37 UNDERLINE = 4, 38 }; 39 40 // Creates a font with the default name and style. 41 Font(); 42 43 // Creates a font that is a clone of another font object. 44 Font(const Font& other); 45 gfx::Font& operator=(const Font& other); 46 47 // Creates a font from the specified native font. 48 explicit Font(NativeFont native_font); 49 50 // Constructs a Font object with the specified PlatformFont object. The Font 51 // object takes ownership of the PlatformFont object. 52 explicit Font(PlatformFont* platform_font); 53 54 // Creates a font with the specified name in UTF-8 and size in pixels. 55 Font(const std::string& font_name, int font_size); 56 57 ~Font(); 58 59 // Returns a new Font derived from the existing font. 60 // |size_deta| is the size in pixels to add to the current font. For example, 61 // a value of 5 results in a font 5 pixels bigger than this font. 62 Font DeriveFont(int size_delta) const; 63 64 // Returns a new Font derived from the existing font. 65 // |size_delta| is the size in pixels to add to the current font. See the 66 // single argument version of this method for an example. 67 // The style parameter specifies the new style for the font, and is a 68 // bitmask of the values: BOLD, ITALIC and UNDERLINE. 69 Font DeriveFont(int size_delta, int style) const; 70 71 // Returns the number of vertical pixels needed to display characters from 72 // the specified font. This may include some leading, i.e. height may be 73 // greater than just ascent + descent. Specifically, the Windows and Mac 74 // implementations include leading and the Linux one does not. This may 75 // need to be revisited in the future. 76 int GetHeight() const; 77 78 // Returns the baseline, or ascent, of the font. 79 int GetBaseline() const; 80 81 // Returns the cap height of the font. 82 int GetCapHeight() const; 83 84 // Returns the average character width for the font. 85 int GetAverageCharacterWidth() const; 86 87 // Returns the number of horizontal pixels needed to display the specified 88 // string. 89 int GetStringWidth(const base::string16& text) const; 90 91 // Returns the expected number of horizontal pixels needed to display the 92 // specified length of characters. Call GetStringWidth() to retrieve the 93 // actual number. 94 int GetExpectedTextWidth(int length) const; 95 96 // Returns the style of the font. 97 int GetStyle() const; 98 99 // Returns the specified font name in UTF-8. 100 std::string GetFontName() const; 101 102 // Returns the actually used font name in UTF-8. 103 std::string GetActualFontNameForTesting() const; 104 105 // Returns the font size in pixels. 106 int GetFontSize() const; 107 108 // Returns the native font handle. 109 // Lifetime lore: 110 // Windows: This handle is owned by the Font object, and should not be 111 // destroyed by the caller. 112 // Mac: The object is owned by the system and should not be released. 113 // Gtk: This handle is created on demand, and must be freed by calling 114 // pango_font_description_free() when the caller is done using it or 115 // by using ScopedPangoFontDescription. 116 NativeFont GetNativeFont() const; 117 118 // Raw access to the underlying platform font implementation. Can be 119 // static_cast to a known implementation type if needed. platform_font()120 PlatformFont* platform_font() const { return platform_font_.get(); } 121 122 private: 123 // Wrapped platform font implementation. 124 scoped_refptr<PlatformFont> platform_font_; 125 }; 126 127 } // namespace gfx 128 129 #endif // UI_GFX_FONT_H_ 130