extensions: EventRouter: pass the whole event to WillDispatchCallback
[chromium-blink-merge.git] / ui / gfx / platform_font_win.h
blob91fcb72f1bd9b2f4f56e3c9d52c1358eb6d6935f
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.
5 #ifndef UI_GFX_PLATFORM_FONT_WIN_H_
6 #define UI_GFX_PLATFORM_FONT_WIN_H_
8 #include <string>
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"
16 struct IDWriteFactory;
18 namespace gfx {
20 class GFX_EXPORT PlatformFontWin : public PlatformFont {
21 public:
22 PlatformFontWin();
23 explicit PlatformFontWin(NativeFont native_font);
24 PlatformFontWin(const std::string& font_name, int font_size);
26 // Dialog units to pixels conversion.
27 // See http://support.microsoft.com/kb/145994 for details.
28 int horizontal_dlus_to_pixels(int dlus) const {
29 return dlus * font_ref_->GetDluBaseX() / 4;
31 int vertical_dlus_to_pixels(int dlus) const {
32 return dlus * font_ref_->height() / 8;
35 // Callback that returns the minimum height that should be used for
36 // gfx::Fonts. Optional. If not specified, the minimum font size is 0.
37 typedef int (*GetMinimumFontSizeCallback)();
38 static GetMinimumFontSizeCallback get_minimum_font_size_callback;
40 // Callback that adjusts a LOGFONT to meet suitability requirements of the
41 // embedding application. Optional. If not specified, no adjustments are
42 // performed other than clamping to a minimum font height if
43 // |get_minimum_font_size_callback| is specified.
44 typedef void (*AdjustFontCallback)(LOGFONT* lf);
45 static AdjustFontCallback adjust_font_callback;
47 // Returns the font name for the system locale. Some fonts, particularly
48 // East Asian fonts, have different names per locale. If the localized font
49 // name could not be retrieved, returns GetFontName().
50 std::string GetLocalizedFontName() const;
52 // Returns a derived Font with the specified |style| and maximum |height|.
53 // The returned Font will be the largest font size with a height <= |height|,
54 // since a size with the exact specified |height| may not necessarily exist.
55 // GetMinimumFontSize() may impose a font size that is taller than |height|.
56 Font DeriveFontWithHeight(int height, int style);
58 // Overridden from PlatformFont:
59 Font DeriveFont(int size_delta, int style) const override;
60 int GetHeight() const override;
61 int GetBaseline() const override;
62 int GetCapHeight() const override;
63 int GetExpectedTextWidth(int length) const override;
64 int GetStyle() const override;
65 std::string GetFontName() const override;
66 std::string GetActualFontNameForTesting() const override;
67 int GetFontSize() const override;
68 const FontRenderParams& GetFontRenderParams() override;
69 NativeFont GetNativeFont() const override;
71 // Called once during initialization if we should be retrieving font metrics
72 // from skia and DirectWrite.
73 static void SetDirectWriteFactory(IDWriteFactory* factory);
75 // Returns the GDI metrics for the font passed in.
76 static void GetTextMetricsForFont(HDC hdc,
77 HFONT font,
78 TEXTMETRIC* text_metrics);
80 // Returns the size of the font based on the font information passed in.
81 static int GetFontSize(const LOGFONT& font_info);
83 private:
84 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_UniscribeFallback);
85 FRIEND_TEST_ALL_PREFIXES(PlatformFontWinTest, Metrics_SkiaVersusGDI);
86 FRIEND_TEST_ALL_PREFIXES(PlatformFontWinTest, DirectWriteFontSubstitution);
88 ~PlatformFontWin() override;
90 // Chrome text drawing bottoms out in the Windows GDI functions that take an
91 // HFONT (an opaque handle into Windows). To avoid lots of GDI object
92 // allocation and destruction, Font indirectly refers to the HFONT by way of
93 // an HFontRef. That is, every Font has an HFontRef, which has an HFONT.
95 // HFontRef is reference counted. Upon deletion, it deletes the HFONT.
96 // By making HFontRef maintain the reference to the HFONT, multiple
97 // HFontRefs can share the same HFONT, and Font can provide value semantics.
98 class GFX_EXPORT HFontRef : public base::RefCounted<HFontRef> {
99 public:
100 // This constructor takes control of the HFONT, and will delete it when
101 // the HFontRef is deleted.
102 HFontRef(HFONT hfont,
103 int font_size,
104 int height,
105 int baseline,
106 int cap_height,
107 int ave_char_width,
108 int style);
110 // Accessors
111 HFONT hfont() const { return hfont_; }
112 int height() const { return height_; }
113 int baseline() const { return baseline_; }
114 int cap_height() const { return cap_height_; }
115 int ave_char_width() const { return ave_char_width_; }
116 int style() const { return style_; }
117 const std::string& font_name() const { return font_name_; }
118 int font_size() const { return font_size_; }
119 int requested_font_size() const { return requested_font_size_; }
121 // Returns the average character width in dialog units.
122 int GetDluBaseX();
124 // Helper to return the average character width using the text extent
125 // technique mentioned here. http://support.microsoft.com/kb/125681.
126 static int GetAverageCharWidthInDialogUnits(HFONT gdi_font);
128 private:
129 friend class base::RefCounted<HFontRef>;
130 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_UniscribeFallback);
131 FRIEND_TEST_ALL_PREFIXES(PlatformFontWinTest, Metrics_SkiaVersusGDI);
132 FRIEND_TEST_ALL_PREFIXES(PlatformFontWinTest, DirectWriteFontSubstitution);
134 ~HFontRef();
136 const HFONT hfont_;
137 const int font_size_;
138 const int height_;
139 const int baseline_;
140 const int cap_height_;
141 const int ave_char_width_;
142 const int style_;
143 // Average character width in dialog units. This is queried lazily from the
144 // system, with an initial value of -1 meaning it hasn't yet been queried.
145 int dlu_base_x_;
146 std::string font_name_;
148 // If the requested font size is not possible for the font, |font_size_|
149 // will be different than |requested_font_size_|. This is stored separately
150 // so that code that increases the font size in a loop will not cause the
151 // loop to get stuck on the same size.
152 int requested_font_size_;
154 DISALLOW_COPY_AND_ASSIGN(HFontRef);
157 // Initializes this object with a copy of the specified HFONT.
158 void InitWithCopyOfHFONT(HFONT hfont);
160 // Initializes this object with the specified font name and size.
161 void InitWithFontNameAndSize(const std::string& font_name,
162 int font_size);
164 // Returns the base font ref. This should ONLY be invoked on the
165 // UI thread.
166 static HFontRef* GetBaseFontRef();
168 // Creates and returns a new HFontRef from the specified HFONT.
169 static HFontRef* CreateHFontRef(HFONT font);
171 // Creates and returns a new HFontRef from the specified HFONT. Uses provided
172 // |font_metrics| instead of calculating new one.
173 static HFontRef* CreateHFontRefFromGDI(HFONT font,
174 const TEXTMETRIC& font_metrics);
176 // Creates and returns a new HFontRef from the specified HFONT using metrics
177 // from skia. Currently this is only used if we use DirectWrite for font
178 // metrics.
179 // |gdi_font| : Handle to the GDI font created via CreateFontIndirect.
180 // |font_metrics| : The GDI font metrics retrieved via the GetTextMetrics
181 // API. This is currently used to calculate the correct height of the font
182 // in case we get a font created with a positive height.
183 // A positive height represents the cell height (ascent + descent).
184 // A negative height represents the character Em height which is cell
185 // height minus the internal leading value.
186 static PlatformFontWin::HFontRef* CreateHFontRefFromSkia(
187 HFONT gdi_font,
188 const TEXTMETRIC& font_metrics);
190 // Creates a new PlatformFontWin with the specified HFontRef. Used when
191 // constructing a Font from a HFONT we don't want to copy.
192 explicit PlatformFontWin(HFontRef* hfont_ref);
194 // Reference to the base font all fonts are derived from.
195 static HFontRef* base_font_ref_;
197 // Indirect reference to the HFontRef, which references the underlying HFONT.
198 scoped_refptr<HFontRef> font_ref_;
200 // Pointer to the global IDWriteFactory interface.
201 static IDWriteFactory* direct_write_factory_;
203 DISALLOW_COPY_AND_ASSIGN(PlatformFontWin);
206 } // namespace gfx
208 #endif // UI_GFX_PLATFORM_FONT_WIN_H_