Remove support for repeat by seconds from Telemetry.
[chromium-blink-merge.git] / ui / gfx / platform_font_win.h
blob2c6f51e0508dbe39374cd9347c6986f5f2d19b67
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/gfx/gfx_export.h"
13 #include "ui/gfx/platform_font.h"
15 namespace gfx {
17 class GFX_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 GetCapHeight() const OVERRIDE;
61 virtual int GetExpectedTextWidth(int length) const OVERRIDE;
62 virtual int GetStyle() const OVERRIDE;
63 virtual std::string GetFontName() const OVERRIDE;
64 virtual std::string GetActualFontNameForTesting() 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 cap_height,
88 int ave_char_width,
89 int style);
91 // Accessors
92 HFONT hfont() const { return hfont_; }
93 int height() const { return height_; }
94 int baseline() const { return baseline_; }
95 int cap_height() const { return cap_height_; }
96 int ave_char_width() const { return ave_char_width_; }
97 int style() const { return style_; }
98 const std::string& font_name() const { return font_name_; }
99 int font_size() const { return font_size_; }
100 int requested_font_size() const { return requested_font_size_; }
102 // Returns the average character width in dialog units.
103 int GetDluBaseX();
105 private:
106 friend class base::RefCounted<HFontRef>;
108 ~HFontRef();
110 const HFONT hfont_;
111 const int font_size_;
112 const int height_;
113 const int baseline_;
114 const int cap_height_;
115 const int ave_char_width_;
116 const int style_;
117 // Average character width in dialog units. This is queried lazily from the
118 // system, with an initial value of -1 meaning it hasn't yet been queried.
119 int dlu_base_x_;
120 std::string font_name_;
122 // If the requested font size is not possible for the font, |font_size_|
123 // will be different than |requested_font_size_|. This is stored separately
124 // so that code that increases the font size in a loop will not cause the
125 // loop to get stuck on the same size.
126 int requested_font_size_;
128 DISALLOW_COPY_AND_ASSIGN(HFontRef);
131 // Initializes this object with a copy of the specified HFONT.
132 void InitWithCopyOfHFONT(HFONT hfont);
134 // Initializes this object with the specified font name and size.
135 void InitWithFontNameAndSize(const std::string& font_name,
136 int font_size);
138 // Returns the base font ref. This should ONLY be invoked on the
139 // UI thread.
140 static HFontRef* GetBaseFontRef();
142 // Creates and returns a new HFONTRef from the specified HFONT.
143 static HFontRef* CreateHFontRef(HFONT font);
145 // Creates a new PlatformFontWin with the specified HFontRef. Used when
146 // constructing a Font from a HFONT we don't want to copy.
147 explicit PlatformFontWin(HFontRef* hfont_ref);
149 // Reference to the base font all fonts are derived from.
150 static HFontRef* base_font_ref_;
152 // Indirect reference to the HFontRef, which references the underlying HFONT.
153 scoped_refptr<HFontRef> font_ref_;
155 DISALLOW_COPY_AND_ASSIGN(PlatformFontWin);
158 } // namespace gfx
160 #endif // UI_GFX_PLATFORM_FONT_WIN_H_