Revert of Output closure-compiled JavaScript files (patchset #10 id:180001 of https...
[chromium-blink-merge.git] / ui / gfx / platform_font_win.h
blob6f7bc21b2081c957189e8e136c30678534f77221
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 virtual Font DeriveFont(int size_delta, int style) const override;
60 virtual int GetHeight() const override;
61 virtual int GetBaseline() const override;
62 virtual int GetCapHeight() const override;
63 virtual int GetExpectedTextWidth(int length) const override;
64 virtual int GetStyle() const override;
65 virtual std::string GetFontName() const override;
66 virtual std::string GetActualFontNameForTesting() const override;
67 virtual int GetFontSize() const override;
68 virtual const FontRenderParams& GetFontRenderParams() override;
69 virtual 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);
87 virtual ~PlatformFontWin() {}
89 // Chrome text drawing bottoms out in the Windows GDI functions that take an
90 // HFONT (an opaque handle into Windows). To avoid lots of GDI object
91 // allocation and destruction, Font indirectly refers to the HFONT by way of
92 // an HFontRef. That is, every Font has an HFontRef, which has an HFONT.
94 // HFontRef is reference counted. Upon deletion, it deletes the HFONT.
95 // By making HFontRef maintain the reference to the HFONT, multiple
96 // HFontRefs can share the same HFONT, and Font can provide value semantics.
97 class GFX_EXPORT HFontRef : public base::RefCounted<HFontRef> {
98 public:
99 // This constructor takes control of the HFONT, and will delete it when
100 // the HFontRef is deleted.
101 HFontRef(HFONT hfont,
102 int font_size,
103 int height,
104 int baseline,
105 int cap_height,
106 int ave_char_width,
107 int style);
109 // Accessors
110 HFONT hfont() const { return hfont_; }
111 int height() const { return height_; }
112 int baseline() const { return baseline_; }
113 int cap_height() const { return cap_height_; }
114 int ave_char_width() const { return ave_char_width_; }
115 int style() const { return style_; }
116 const std::string& font_name() const { return font_name_; }
117 int font_size() const { return font_size_; }
118 int requested_font_size() const { return requested_font_size_; }
120 // Returns the average character width in dialog units.
121 int GetDluBaseX();
123 // Helper to return the average character width using the text extent
124 // technique mentioned here. http://support.microsoft.com/kb/125681.
125 static int GetAverageCharWidthInDialogUnits(HFONT gdi_font);
127 private:
128 friend class base::RefCounted<HFontRef>;
129 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_UniscribeFallback);
130 FRIEND_TEST_ALL_PREFIXES(PlatformFontWinTest, Metrics_SkiaVersusGDI);
132 ~HFontRef();
134 const HFONT hfont_;
135 const int font_size_;
136 const int height_;
137 const int baseline_;
138 const int cap_height_;
139 const int ave_char_width_;
140 const int style_;
141 // Average character width in dialog units. This is queried lazily from the
142 // system, with an initial value of -1 meaning it hasn't yet been queried.
143 int dlu_base_x_;
144 std::string font_name_;
146 // If the requested font size is not possible for the font, |font_size_|
147 // will be different than |requested_font_size_|. This is stored separately
148 // so that code that increases the font size in a loop will not cause the
149 // loop to get stuck on the same size.
150 int requested_font_size_;
152 DISALLOW_COPY_AND_ASSIGN(HFontRef);
155 // Initializes this object with a copy of the specified HFONT.
156 void InitWithCopyOfHFONT(HFONT hfont);
158 // Initializes this object with the specified font name and size.
159 void InitWithFontNameAndSize(const std::string& font_name,
160 int font_size);
162 // Returns the base font ref. This should ONLY be invoked on the
163 // UI thread.
164 static HFontRef* GetBaseFontRef();
166 // Creates and returns a new HFontRef from the specified HFONT.
167 static HFontRef* CreateHFontRef(HFONT font);
169 // Creates and returns a new HFontRef from the specified HFONT. Uses provided
170 // |font_metrics| instead of calculating new one.
171 static HFontRef* CreateHFontRefFromGDI(HFONT font,
172 const TEXTMETRIC& font_metrics);
174 // Creates and returns a new HFontRef from the specified HFONT using metrics
175 // from skia. Currently this is only used if we use DirectWrite for font
176 // metrics.
177 // |gdi_font| : Handle to the GDI font created via CreateFontIndirect.
178 // |font_metrics| : The GDI font metrics retrieved via the GetTextMetrics
179 // API. This is currently used to calculate the correct height of the font
180 // in case we get a font created with a positive height.
181 // A positive height represents the cell height (ascent + descent).
182 // A negative height represents the character Em height which is cell
183 // height minus the internal leading value.
184 static PlatformFontWin::HFontRef* CreateHFontRefFromSkia(
185 HFONT gdi_font,
186 const TEXTMETRIC& font_metrics);
188 // Creates a new PlatformFontWin with the specified HFontRef. Used when
189 // constructing a Font from a HFONT we don't want to copy.
190 explicit PlatformFontWin(HFontRef* hfont_ref);
192 // Reference to the base font all fonts are derived from.
193 static HFontRef* base_font_ref_;
195 // Indirect reference to the HFontRef, which references the underlying HFONT.
196 scoped_refptr<HFontRef> font_ref_;
198 // Pointer to the global IDWriteFactory interface.
199 static IDWriteFactory* direct_write_factory_;
201 DISALLOW_COPY_AND_ASSIGN(PlatformFontWin);
204 } // namespace gfx
206 #endif // UI_GFX_PLATFORM_FONT_WIN_H_