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.
10 #include "base/memory/ref_counted.h"
11 #include "base/string16.h"
12 #include "ui/base/ui_export.h"
13 #include "ui/gfx/native_widget_types.h"
19 // Font provides a wrapper around an underlying font. Copy and assignment
20 // operators are explicitly allowed, and cheap.
21 class UI_EXPORT Font
{
23 // The following constants indicate the font style.
31 // Creates a font with the default name and style.
34 // Creates a font that is a clone of another font object.
35 Font(const Font
& other
);
36 gfx::Font
& operator=(const Font
& other
);
38 // Creates a font from the specified native font.
39 explicit Font(NativeFont native_font
);
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
);
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
);
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;
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;
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;
69 // Returns the baseline, or ascent, of the font.
70 int GetBaseline() const;
72 // Returns the average character width for the font.
73 int GetAverageCharacterWidth() const;
75 // Returns the number of horizontal pixels needed to display the specified
77 int GetStringWidth(const string16
& text
) const;
79 // Returns the expected number of horizontal pixels needed to display the
80 // specified length of characters. Call GetStringWidth() to retrieve the
82 int GetExpectedTextWidth(int length
) const;
84 // Returns the style of the font.
87 // Returns the font name in UTF-8.
88 std::string
GetFontName() const;
90 // Returns the font size in pixels.
91 int GetFontSize() const;
93 // Returns the native font handle.
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;
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(); }
108 // Wrapped platform font implementation.
109 scoped_refptr
<PlatformFont
> platform_font_
;
114 #endif // UI_GFX_FONT_H_