• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef UI_GFX_PLATFORM_FONT_WIN_H_
6 #define UI_GFX_PLATFORM_FONT_WIN_H_
7 
8 #include <string>
9 
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"
14 
15 namespace gfx {
16 
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);
22 
23   // Dialog units to pixels conversion.
24   // See http://support.microsoft.com/kb/145994 for details.
horizontal_dlus_to_pixels(int dlus)25   int horizontal_dlus_to_pixels(int dlus) const {
26     return dlus * font_ref_->GetDluBaseX() / 4;
27   }
vertical_dlus_to_pixels(int dlus)28   int vertical_dlus_to_pixels(int dlus)  const {
29     return dlus * font_ref_->height() / 8;
30   }
31 
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;
36 
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;
43 
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;
48 
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);
55 
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 GetAverageCharacterWidth() const OVERRIDE;
62   virtual int GetStringWidth(const base::string16& text) 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 NativeFont GetNativeFont() const OVERRIDE;
69 
70  private:
~PlatformFontWin()71   virtual ~PlatformFontWin() {}
72 
73   // Chrome text drawing bottoms out in the Windows GDI functions that take an
74   // HFONT (an opaque handle into Windows). To avoid lots of GDI object
75   // allocation and destruction, Font indirectly refers to the HFONT by way of
76   // an HFontRef. That is, every Font has an HFontRef, which has an HFONT.
77   //
78   // HFontRef is reference counted. Upon deletion, it deletes the HFONT.
79   // By making HFontRef maintain the reference to the HFONT, multiple
80   // HFontRefs can share the same HFONT, and Font can provide value semantics.
81   class HFontRef : public base::RefCounted<HFontRef> {
82    public:
83     // This constructor takes control of the HFONT, and will delete it when
84     // the HFontRef is deleted.
85     HFontRef(HFONT hfont,
86              int font_size,
87              int height,
88              int baseline,
89              int cap_height,
90              int ave_char_width,
91              int style);
92 
93     // Accessors
hfont()94     HFONT hfont() const { return hfont_; }
height()95     int height() const { return height_; }
baseline()96     int baseline() const { return baseline_; }
cap_height()97     int cap_height() const { return cap_height_; }
ave_char_width()98     int ave_char_width() const { return ave_char_width_; }
style()99     int style() const { return style_; }
font_name()100     const std::string& font_name() const { return font_name_; }
font_size()101     int font_size() const { return font_size_; }
requested_font_size()102     int requested_font_size() const { return requested_font_size_; }
103 
104     // Returns the average character width in dialog units.
105     int GetDluBaseX();
106 
107    private:
108     friend class base::RefCounted<HFontRef>;
109 
110     ~HFontRef();
111 
112     const HFONT hfont_;
113     const int font_size_;
114     const int height_;
115     const int baseline_;
116     const int cap_height_;
117     const int ave_char_width_;
118     const int style_;
119     // Average character width in dialog units. This is queried lazily from the
120     // system, with an initial value of -1 meaning it hasn't yet been queried.
121     int dlu_base_x_;
122     std::string font_name_;
123 
124     // If the requested font size is not possible for the font, |font_size_|
125     // will be different than |requested_font_size_|. This is stored separately
126     // so that code that increases the font size in a loop will not cause the
127     // loop to get stuck on the same size.
128     int requested_font_size_;
129 
130     DISALLOW_COPY_AND_ASSIGN(HFontRef);
131   };
132 
133   // Initializes this object with a copy of the specified HFONT.
134   void InitWithCopyOfHFONT(HFONT hfont);
135 
136   // Initializes this object with the specified font name and size.
137   void InitWithFontNameAndSize(const std::string& font_name,
138                                int font_size);
139 
140   // Returns the base font ref. This should ONLY be invoked on the
141   // UI thread.
142   static HFontRef* GetBaseFontRef();
143 
144   // Creates and returns a new HFONTRef from the specified HFONT.
145   static HFontRef* CreateHFontRef(HFONT font);
146 
147   // Creates a new PlatformFontWin with the specified HFontRef. Used when
148   // constructing a Font from a HFONT we don't want to copy.
149   explicit PlatformFontWin(HFontRef* hfont_ref);
150 
151     // Reference to the base font all fonts are derived from.
152   static HFontRef* base_font_ref_;
153 
154   // Indirect reference to the HFontRef, which references the underlying HFONT.
155   scoped_refptr<HFontRef> font_ref_;
156 
157   DISALLOW_COPY_AND_ASSIGN(PlatformFontWin);
158 };
159 
160 }  // namespace gfx
161 
162 #endif  // UI_GFX_PLATFORM_FONT_WIN_H_
163