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/memory/ref_counted.h"
12 #include "ui/gfx/gfx_export.h"
13 #include "ui/gfx/platform_font.h"
17 class GFX_EXPORT PlatformFontWin
: public PlatformFont
{
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 const FontRenderParams
& GetFontRenderParams() const OVERRIDE
;
67 virtual NativeFont
GetNativeFont() const OVERRIDE
;
70 virtual ~PlatformFontWin() {}
72 // Chrome text drawing bottoms out in the Windows GDI functions that take an
73 // HFONT (an opaque handle into Windows). To avoid lots of GDI object
74 // allocation and destruction, Font indirectly refers to the HFONT by way of
75 // an HFontRef. That is, every Font has an HFontRef, which has an HFONT.
77 // HFontRef is reference counted. Upon deletion, it deletes the HFONT.
78 // By making HFontRef maintain the reference to the HFONT, multiple
79 // HFontRefs can share the same HFONT, and Font can provide value semantics.
80 class GFX_EXPORT HFontRef
: public base::RefCounted
<HFontRef
> {
82 // This constructor takes control of the HFONT, and will delete it when
83 // the HFontRef is deleted.
93 HFONT
hfont() const { return hfont_
; }
94 int height() const { return height_
; }
95 int baseline() const { return baseline_
; }
96 int cap_height() const { return cap_height_
; }
97 int ave_char_width() const { return ave_char_width_
; }
98 int style() const { return style_
; }
99 const std::string
& font_name() const { return font_name_
; }
100 int font_size() const { return font_size_
; }
101 int requested_font_size() const { return requested_font_size_
; }
103 // Returns the average character width in dialog units.
107 friend class base::RefCounted
<HFontRef
>;
112 const int font_size_
;
115 const int cap_height_
;
116 const int ave_char_width_
;
118 // Average character width in dialog units. This is queried lazily from the
119 // system, with an initial value of -1 meaning it hasn't yet been queried.
121 std::string font_name_
;
123 // If the requested font size is not possible for the font, |font_size_|
124 // will be different than |requested_font_size_|. This is stored separately
125 // so that code that increases the font size in a loop will not cause the
126 // loop to get stuck on the same size.
127 int requested_font_size_
;
129 DISALLOW_COPY_AND_ASSIGN(HFontRef
);
132 // Initializes this object with a copy of the specified HFONT.
133 void InitWithCopyOfHFONT(HFONT hfont
);
135 // Initializes this object with the specified font name and size.
136 void InitWithFontNameAndSize(const std::string
& font_name
,
139 // Returns the base font ref. This should ONLY be invoked on the
141 static HFontRef
* GetBaseFontRef();
143 // Creates and returns a new HFONTRef from the specified HFONT.
144 static HFontRef
* CreateHFontRef(HFONT font
);
146 // Creates and returns a new HFONTRef from the specified HFONT. Uses provided
147 // |font_metrics| instead of calculating new one.
148 static HFontRef
* CreateHFontRef(HFONT font
, const TEXTMETRIC
& font_metrics
);
150 // Returns a largest derived Font whose height does not exceed the height of
152 static Font
DeriveWithCorrectedSize(HFONT base_font
);
154 // Creates a new PlatformFontWin with the specified HFontRef. Used when
155 // constructing a Font from a HFONT we don't want to copy.
156 explicit PlatformFontWin(HFontRef
* hfont_ref
);
158 // Reference to the base font all fonts are derived from.
159 static HFontRef
* base_font_ref_
;
161 // Indirect reference to the HFontRef, which references the underlying HFONT.
162 scoped_refptr
<HFontRef
> font_ref_
;
164 DISALLOW_COPY_AND_ASSIGN(PlatformFontWin
);
169 #endif // UI_GFX_PLATFORM_FONT_WIN_H_