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