Disable flaky test ExtensionActionContextMenuTest.RunInspectPopup
[chromium-blink-merge.git] / ui / gfx / platform_font_win.h
blobbe8d77ba7dcc394d069776e1d33c525621a23ae0
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/memory/ref_counted.h"
12 #include "ui/base/ui_export.h"
13 #include "ui/gfx/platform_font.h"
15 namespace gfx {
17 class UI_EXPORT PlatformFontWin : public PlatformFont {
18 public:
19 PlatformFontWin();
20 explicit PlatformFontWin(NativeFont native_font);
21 PlatformFontWin(const std::string& font_name, int font_size);
23 // Dialog units to pixels conversion.
24 // See http://support.microsoft.com/kb/145994 for details.
25 int horizontal_dlus_to_pixels(int dlus) const {
26 return dlus * font_ref_->GetDluBaseX() / 4;
28 int vertical_dlus_to_pixels(int dlus) const {
29 return dlus * font_ref_->height() / 8;
32 // Callback that returns the minimum height that should be used for
33 // gfx::Fonts. Optional. If not specified, the minimum font size is 0.
34 typedef int (*GetMinimumFontSizeCallback)();
35 static GetMinimumFontSizeCallback get_minimum_font_size_callback;
37 // Callback that adjusts a LOGFONT to meet suitability requirements of the
38 // embedding application. Optional. If not specified, no adjustments are
39 // performed other than clamping to a minimum font height if
40 // |get_minimum_font_size_callback| is specified.
41 typedef void (*AdjustFontCallback)(LOGFONT* lf);
42 static AdjustFontCallback adjust_font_callback;
44 // Returns the font name for the system locale. Some fonts, particularly
45 // East Asian fonts, have different names per locale. If the localized font
46 // name could not be retrieved, returns GetFontName().
47 std::string GetLocalizedFontName() const;
49 // Returns a derived Font with the specified |style| and with height at most
50 // |height|. If the height and style of the receiver already match, it is
51 // returned. Otherwise, the returned Font will have the largest size such that
52 // its height is less than or equal to |height| (since there may not exist a
53 // size that matches the exact |height| specified).
54 Font DeriveFontWithHeight(int height, int style);
56 // Overridden from PlatformFont:
57 virtual Font DeriveFont(int size_delta, int style) const OVERRIDE;
58 virtual int GetHeight() const OVERRIDE;
59 virtual int GetBaseline() const OVERRIDE;
60 virtual int GetAverageCharacterWidth() const OVERRIDE;
61 virtual int GetStringWidth(const string16& text) 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 int GetFontSize() const OVERRIDE;
66 virtual NativeFont GetNativeFont() const OVERRIDE;
68 private:
69 virtual ~PlatformFontWin() {}
71 // Chrome text drawing bottoms out in the Windows GDI functions that take an
72 // HFONT (an opaque handle into Windows). To avoid lots of GDI object
73 // allocation and destruction, Font indirectly refers to the HFONT by way of
74 // an HFontRef. That is, every Font has an HFontRef, which has an HFONT.
76 // HFontRef is reference counted. Upon deletion, it deletes the HFONT.
77 // By making HFontRef maintain the reference to the HFONT, multiple
78 // HFontRefs can share the same HFONT, and Font can provide value semantics.
79 class HFontRef : public base::RefCounted<HFontRef> {
80 public:
81 // This constructor takes control of the HFONT, and will delete it when
82 // the HFontRef is deleted.
83 HFontRef(HFONT hfont,
84 int font_size,
85 int height,
86 int baseline,
87 int ave_char_width,
88 int style);
90 // Accessors
91 HFONT hfont() const { return hfont_; }
92 int height() const { return height_; }
93 int baseline() const { return baseline_; }
94 int ave_char_width() const { return ave_char_width_; }
95 int style() const { return style_; }
96 const std::string& font_name() const { return font_name_; }
97 int font_size() const { return font_size_; }
98 int requested_font_size() const { return requested_font_size_; }
100 // Returns the average character width in dialog units.
101 int GetDluBaseX();
103 private:
104 friend class base::RefCounted<HFontRef>;
106 ~HFontRef();
108 const HFONT hfont_;
109 const int font_size_;
110 const int height_;
111 const int baseline_;
112 const int ave_char_width_;
113 const int style_;
114 // Average character width in dialog units. This is queried lazily from the
115 // system, with an initial value of -1 meaning it hasn't yet been queried.
116 int dlu_base_x_;
117 std::string font_name_;
119 // If the requested font size is not possible for the font, |font_size_|
120 // will be different than |requested_font_size_|. This is stored separately
121 // so that code that increases the font size in a loop will not cause the
122 // loop to get stuck on the same size.
123 int requested_font_size_;
125 DISALLOW_COPY_AND_ASSIGN(HFontRef);
128 // Initializes this object with a copy of the specified HFONT.
129 void InitWithCopyOfHFONT(HFONT hfont);
131 // Initializes this object with the specified font name and size.
132 void InitWithFontNameAndSize(const std::string& font_name,
133 int font_size);
135 // Returns the base font ref. This should ONLY be invoked on the
136 // UI thread.
137 static HFontRef* GetBaseFontRef();
139 // Creates and returns a new HFONTRef from the specified HFONT.
140 static HFontRef* CreateHFontRef(HFONT font);
142 // Creates a new PlatformFontWin with the specified HFontRef. Used when
143 // constructing a Font from a HFONT we don't want to copy.
144 explicit PlatformFontWin(HFontRef* hfont_ref);
146 // Reference to the base font all fonts are derived from.
147 static HFontRef* base_font_ref_;
149 // Indirect reference to the HFontRef, which references the underlying HFONT.
150 scoped_refptr<HFontRef> font_ref_;
152 DISALLOW_COPY_AND_ASSIGN(PlatformFontWin);
155 } // namespace gfx
157 #endif // UI_GFX_PLATFORM_FONT_WIN_H_