Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / platform_font_win.h
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_PLATFORM_FONT_WIN_H_
6 #define UI_GFX_PLATFORM_FONT_WIN_H_
7
8 #include <string>
9
10 #include "base/compiler_specific.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "ui/gfx/gfx_export.h"
14 #include "ui/gfx/platform_font.h"
15
16 namespace gfx {
17
18 class GFX_EXPORT PlatformFontWin : public PlatformFont {
19  public:
20   PlatformFontWin();
21   explicit PlatformFontWin(NativeFont native_font);
22   PlatformFontWin(const std::string& font_name, int font_size);
23
24   // Dialog units to pixels conversion.
25   // See http://support.microsoft.com/kb/145994 for details.
26   int horizontal_dlus_to_pixels(int dlus) const {
27     return dlus * font_ref_->GetDluBaseX() / 4;
28   }
29   int vertical_dlus_to_pixels(int dlus)  const {
30     return dlus * font_ref_->height() / 8;
31   }
32
33   // Callback that returns the minimum height that should be used for
34   // gfx::Fonts. Optional. If not specified, the minimum font size is 0.
35   typedef int (*GetMinimumFontSizeCallback)();
36   static GetMinimumFontSizeCallback get_minimum_font_size_callback;
37
38   // Callback that adjusts a LOGFONT to meet suitability requirements of the
39   // embedding application. Optional. If not specified, no adjustments are
40   // performed other than clamping to a minimum font height if
41   // |get_minimum_font_size_callback| is specified.
42   typedef void (*AdjustFontCallback)(LOGFONT* lf);
43   static AdjustFontCallback adjust_font_callback;
44
45   // Returns the font name for the system locale. Some fonts, particularly
46   // East Asian fonts, have different names per locale. If the localized font
47   // name could not be retrieved, returns GetFontName().
48   std::string GetLocalizedFontName() const;
49
50   // Returns a derived Font with the specified |style| and with height at most
51   // |height|. If the height and style of the receiver already match, it is
52   // returned. Otherwise, the returned Font will have the largest size such that
53   // its height is less than or equal to |height| (since there may not exist a
54   // size that matches the exact |height| specified).
55   Font DeriveFontWithHeight(int height, int style);
56
57   // Overridden from PlatformFont:
58   virtual Font DeriveFont(int size_delta, int style) const override;
59   virtual int GetHeight() const override;
60   virtual int GetBaseline() const override;
61   virtual int GetCapHeight() const override;
62   virtual int GetExpectedTextWidth(int length) const override;
63   virtual int GetStyle() const override;
64   virtual std::string GetFontName() const override;
65   virtual std::string GetActualFontNameForTesting() const override;
66   virtual int GetFontSize() const override;
67   virtual const FontRenderParams& GetFontRenderParams() const override;
68   virtual NativeFont GetNativeFont() const override;
69
70   // Called once during initialization if should be retrieving font metrics
71   // from skia.
72   static void set_use_skia_for_font_metrics(bool use_skia_for_font_metrics) {
73     use_skia_for_font_metrics_ = use_skia_for_font_metrics;
74   }
75
76  private:
77   FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_UniscribeFallback);
78
79   virtual ~PlatformFontWin() {}
80
81   // Chrome text drawing bottoms out in the Windows GDI functions that take an
82   // HFONT (an opaque handle into Windows). To avoid lots of GDI object
83   // allocation and destruction, Font indirectly refers to the HFONT by way of
84   // an HFontRef. That is, every Font has an HFontRef, which has an HFONT.
85   //
86   // HFontRef is reference counted. Upon deletion, it deletes the HFONT.
87   // By making HFontRef maintain the reference to the HFONT, multiple
88   // HFontRefs can share the same HFONT, and Font can provide value semantics.
89   class GFX_EXPORT HFontRef : public base::RefCounted<HFontRef> {
90    public:
91     // This constructor takes control of the HFONT, and will delete it when
92     // the HFontRef is deleted.
93     HFontRef(HFONT hfont,
94              int font_size,
95              int height,
96              int baseline,
97              int cap_height,
98              int ave_char_width,
99              int style);
100
101     // Accessors
102     HFONT hfont() const { return hfont_; }
103     int height() const { return height_; }
104     int baseline() const { return baseline_; }
105     int cap_height() const { return cap_height_; }
106     int ave_char_width() const { return ave_char_width_; }
107     int style() const { return style_; }
108     const std::string& font_name() const { return font_name_; }
109     int font_size() const { return font_size_; }
110     int requested_font_size() const { return requested_font_size_; }
111
112     // Returns the average character width in dialog units.
113     int GetDluBaseX();
114
115    private:
116     friend class base::RefCounted<HFontRef>;
117     FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_UniscribeFallback);
118
119     ~HFontRef();
120
121     const HFONT hfont_;
122     const int font_size_;
123     const int height_;
124     const int baseline_;
125     const int cap_height_;
126     const int ave_char_width_;
127     const int style_;
128     // Average character width in dialog units. This is queried lazily from the
129     // system, with an initial value of -1 meaning it hasn't yet been queried.
130     int dlu_base_x_;
131     std::string font_name_;
132
133     // If the requested font size is not possible for the font, |font_size_|
134     // will be different than |requested_font_size_|. This is stored separately
135     // so that code that increases the font size in a loop will not cause the
136     // loop to get stuck on the same size.
137     int requested_font_size_;
138
139     DISALLOW_COPY_AND_ASSIGN(HFontRef);
140   };
141
142   // Initializes this object with a copy of the specified HFONT.
143   void InitWithCopyOfHFONT(HFONT hfont);
144
145   // Initializes this object with the specified font name and size.
146   void InitWithFontNameAndSize(const std::string& font_name,
147                                int font_size);
148
149   // Returns the base font ref. This should ONLY be invoked on the
150   // UI thread.
151   static HFontRef* GetBaseFontRef();
152
153   // Creates and returns a new HFontRef from the specified HFONT.
154   static HFontRef* CreateHFontRef(HFONT font);
155
156   // Creates and returns a new HFontRef from the specified HFONT. Uses provided
157   // |font_metrics| instead of calculating new one.
158   static HFontRef* CreateHFontRef(HFONT font, const TEXTMETRIC& font_metrics);
159
160   // Returns a largest derived Font whose height does not exceed the height of
161   // |base_font|.
162   static Font DeriveWithCorrectedSize(HFONT base_font);
163
164   // Creates and returns a new HFontRef from the specified HFONT using metrics
165   // from skia. Currently this is only used if we use DirectWrite for font
166   // metrics.
167   static PlatformFontWin::HFontRef* CreateHFontRefFromSkia(
168       HFONT gdi_font);
169
170   // Creates a new PlatformFontWin with the specified HFontRef. Used when
171   // constructing a Font from a HFONT we don't want to copy.
172   explicit PlatformFontWin(HFontRef* hfont_ref);
173
174     // Reference to the base font all fonts are derived from.
175   static HFontRef* base_font_ref_;
176
177   // Indirect reference to the HFontRef, which references the underlying HFONT.
178   scoped_refptr<HFontRef> font_ref_;
179
180   // Set to true if font metrics are to be retrieved from skia. Defaults to
181   // false.
182   static bool use_skia_for_font_metrics_;
183
184   DISALLOW_COPY_AND_ASSIGN(PlatformFontWin);
185 };
186
187 }  // namespace gfx
188
189 #endif  // UI_GFX_PLATFORM_FONT_WIN_H_