[email protected] | 7c61375 | 2012-01-24 21:08:51 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | c8703bb | 2011-10-19 14:35:15 | [diff] [blame] | 5 | #ifndef UI_GFX_PLATFORM_FONT_PANGO_H_ |
| 6 | #define UI_GFX_PLATFORM_FONT_PANGO_H_ |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 7 | |
[email protected] | c96388356 | 2013-09-18 08:15:22 | [diff] [blame] | 8 | #include <string> |
| 9 | |
[email protected] | c9243403 | 2011-11-15 23:42:50 | [diff] [blame] | 10 | #include "base/compiler_specific.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 11 | #include "base/memory/scoped_ptr.h" |
[email protected] | 3699ae5 | 2012-12-05 01:00:54 | [diff] [blame] | 12 | #include "skia/ext/refptr.h" |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 13 | #include "third_party/skia/include/core/SkRefCnt.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 14 | #include "ui/gfx/platform_font.h" |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 15 | |
| 16 | class SkTypeface; |
| 17 | class SkPaint; |
| 18 | |
| 19 | namespace gfx { |
| 20 | |
[email protected] | 4ffa789 | 2013-09-27 16:56:06 | [diff] [blame] | 21 | class GFX_EXPORT PlatformFontPango : public PlatformFont { |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 22 | public: |
[email protected] | b6bb36e2 | 2011-09-21 17:52:54 | [diff] [blame] | 23 | PlatformFontPango(); |
[email protected] | b6bb36e2 | 2011-09-21 17:52:54 | [diff] [blame] | 24 | explicit PlatformFontPango(NativeFont native_font); |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 25 | PlatformFontPango(const std::string& font_name, int font_size); |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 26 | |
| 27 | // Converts |gfx_font| to a new pango font. Free the returned font with |
| 28 | // pango_font_description_free(). |
| 29 | static PangoFontDescription* PangoFontFromGfxFont(const gfx::Font& gfx_font); |
| 30 | |
[email protected] | 28e466c | 2011-04-06 05:01:20 | [diff] [blame] | 31 | // Resets and reloads the cached system font used by the default constructor. |
| 32 | // This function is useful when the system font has changed, for example, when |
| 33 | // the locale has changed. |
| 34 | static void ReloadDefaultFont(); |
| 35 | |
[email protected] | c96388356 | 2013-09-18 08:15:22 | [diff] [blame] | 36 | #if defined(OS_CHROMEOS) |
| 37 | // Sets the default font. |
| 38 | static void SetDefaultFontDescription(const std::string& font_description); |
| 39 | #endif |
| 40 | |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 41 | // Position as an offset from the height of the drawn text, used to draw |
| 42 | // an underline. This is a negative number, so the underline would be |
[email protected] | 354ee47 | 2012-08-30 21:08:15 | [diff] [blame] | 43 | // drawn at y + height + underline_position. |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 44 | double underline_position() const; |
| 45 | // The thickness to draw the underline. |
| 46 | double underline_thickness() const; |
| 47 | |
| 48 | // Overridden from PlatformFont: |
[email protected] | c9243403 | 2011-11-15 23:42:50 | [diff] [blame] | 49 | virtual Font DeriveFont(int size_delta, int style) const OVERRIDE; |
| 50 | virtual int GetHeight() const OVERRIDE; |
| 51 | virtual int GetBaseline() const OVERRIDE; |
[email protected] | c831ea9c | 2013-10-01 10:52:24 | [diff] [blame] | 52 | virtual int GetCapHeight() const OVERRIDE; |
[email protected] | c9243403 | 2011-11-15 23:42:50 | [diff] [blame] | 53 | virtual int GetExpectedTextWidth(int length) const OVERRIDE; |
| 54 | virtual int GetStyle() const OVERRIDE; |
| 55 | virtual std::string GetFontName() const OVERRIDE; |
[email protected] | 2fb2657 | 2013-12-11 08:43:06 | [diff] [blame] | 56 | virtual std::string GetActualFontNameForTesting() const OVERRIDE; |
[email protected] | c9243403 | 2011-11-15 23:42:50 | [diff] [blame] | 57 | virtual int GetFontSize() const OVERRIDE; |
| 58 | virtual NativeFont GetNativeFont() const OVERRIDE; |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 59 | |
| 60 | private: |
| 61 | // Create a new instance of this object with the specified properties. Called |
| 62 | // from DeriveFont. |
[email protected] | 3699ae5 | 2012-12-05 01:00:54 | [diff] [blame] | 63 | PlatformFontPango(const skia::RefPtr<SkTypeface>& typeface, |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 64 | const std::string& name, |
| 65 | int size, |
| 66 | int style); |
[email protected] | b6bb36e2 | 2011-09-21 17:52:54 | [diff] [blame] | 67 | virtual ~PlatformFontPango(); |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 68 | |
[email protected] | c96388356 | 2013-09-18 08:15:22 | [diff] [blame] | 69 | // Returns a Pango font description (suitable for parsing by |
| 70 | // pango_font_description_from_string()) for the default UI font. |
| 71 | static std::string GetDefaultFont(); |
| 72 | |
[email protected] | d90c0cd | 2014-07-08 02:31:34 | [diff] [blame^] | 73 | // Initializes this object based on the passed-in details. If |typeface| is |
| 74 | // empty, a new typeface will be loaded. |
| 75 | void InitFromDetails( |
[email protected] | 3699ae5 | 2012-12-05 01:00:54 | [diff] [blame] | 76 | const skia::RefPtr<SkTypeface>& typeface, |
[email protected] | 2fb2657 | 2013-12-11 08:43:06 | [diff] [blame] | 77 | const std::string& font_family, |
| 78 | int font_size, |
[email protected] | 3699ae5 | 2012-12-05 01:00:54 | [diff] [blame] | 79 | int style); |
[email protected] | d90c0cd | 2014-07-08 02:31:34 | [diff] [blame^] | 80 | |
| 81 | // Initializes this object as a copy of another PlatformFontPango. |
[email protected] | b6bb36e2 | 2011-09-21 17:52:54 | [diff] [blame] | 82 | void InitFromPlatformFont(const PlatformFontPango* other); |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 83 | |
| 84 | // Potentially slow call to get pango metrics (average width, underline info). |
| 85 | void InitPangoMetrics(); |
| 86 | |
[email protected] | 354ee47 | 2012-08-30 21:08:15 | [diff] [blame] | 87 | // Setup a Skia context to use the current typeface. |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 88 | void PaintSetup(SkPaint* paint) const; |
| 89 | |
| 90 | // Make |this| a copy of |other|. |
| 91 | void CopyFont(const Font& other); |
| 92 | |
| 93 | // The average width of a character, initialized and cached if needed. |
| 94 | double GetAverageWidth() const; |
| 95 | |
[email protected] | 3699ae5 | 2012-12-05 01:00:54 | [diff] [blame] | 96 | skia::RefPtr<SkTypeface> typeface_; |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 97 | |
| 98 | // Additional information about the face |
| 99 | // Skia actually expects a family name and not a font name. |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 100 | std::string font_family_; |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 101 | int font_size_pixels_; |
| 102 | int style_; |
| 103 | |
[email protected] | 354ee47 | 2012-08-30 21:08:15 | [diff] [blame] | 104 | // Cached metrics, generated at construction. |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 105 | int ascent_pixels_; |
[email protected] | d90c0cd | 2014-07-08 02:31:34 | [diff] [blame^] | 106 | int height_pixels_; |
[email protected] | 0fbd8de | 2013-12-24 15:18:55 | [diff] [blame] | 107 | int cap_height_pixels_; |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 108 | |
| 109 | // The pango metrics are much more expensive so we wait until we need them |
| 110 | // to compute them. |
| 111 | bool pango_metrics_inited_; |
| 112 | double average_width_pixels_; |
| 113 | double underline_position_pixels_; |
| 114 | double underline_thickness_pixels_; |
| 115 | |
| 116 | // The default font, used for the default constructor. |
| 117 | static Font* default_font_; |
[email protected] | 354ee47 | 2012-08-30 21:08:15 | [diff] [blame] | 118 | |
[email protected] | c96388356 | 2013-09-18 08:15:22 | [diff] [blame] | 119 | #if defined(OS_CHROMEOS) |
| 120 | static std::string* default_font_description_; |
| 121 | #endif |
| 122 | |
[email protected] | 354ee47 | 2012-08-30 21:08:15 | [diff] [blame] | 123 | DISALLOW_COPY_AND_ASSIGN(PlatformFontPango); |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | } // namespace gfx |
| 127 | |
[email protected] | c8703bb | 2011-10-19 14:35:15 | [diff] [blame] | 128 | #endif // UI_GFX_PLATFORM_FONT_PANGO_H_ |