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_
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"
18 class GFX_EXPORT PlatformFontWin : public PlatformFont {
21 explicit PlatformFontWin(NativeFont native_font);
22 PlatformFontWin(const std::string& font_name, int font_size);
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;
29 int vertical_dlus_to_pixels(int dlus) const {
30 return dlus * font_ref_->height() / 8;
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;
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;
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;
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);
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;
70 // Called once during initialization if should be retrieving font metrics
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;
77 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_UniscribeFallback);
79 virtual ~PlatformFontWin() {}
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.
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> {
91 // This constructor takes control of the HFONT, and will delete it when
92 // the HFontRef is deleted.
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_; }
112 // Returns the average character width in dialog units.
116 friend class base::RefCounted<HFontRef>;
117 FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_UniscribeFallback);
122 const int font_size_;
125 const int cap_height_;
126 const int ave_char_width_;
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.
131 std::string font_name_;
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_;
139 DISALLOW_COPY_AND_ASSIGN(HFontRef);
142 // Initializes this object with a copy of the specified HFONT.
143 void InitWithCopyOfHFONT(HFONT hfont);
145 // Initializes this object with the specified font name and size.
146 void InitWithFontNameAndSize(const std::string& font_name,
149 // Returns the base font ref. This should ONLY be invoked on the
151 static HFontRef* GetBaseFontRef();
153 // Creates and returns a new HFontRef from the specified HFONT.
154 static HFontRef* CreateHFontRef(HFONT font);
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);
160 // Returns a largest derived Font whose height does not exceed the height of
162 static Font DeriveWithCorrectedSize(HFONT base_font);
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
167 static PlatformFontWin::HFontRef* CreateHFontRefFromSkia(
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);
174 // Reference to the base font all fonts are derived from.
175 static HFontRef* base_font_ref_;
177 // Indirect reference to the HFontRef, which references the underlying HFONT.
178 scoped_refptr<HFontRef> font_ref_;
180 // Set to true if font metrics are to be retrieved from skia. Defaults to
182 static bool use_skia_for_font_metrics_;
184 DISALLOW_COPY_AND_ASSIGN(PlatformFontWin);
189 #endif // UI_GFX_PLATFORM_FONT_WIN_H_