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