Pass localized font name of prefs to font settings.
[chromium-blink-merge.git] / ui / gfx / platform_font_win.h
blobffdde09ca0798bf90d5d25e6610c6bc706c35ab3
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_
7 #pragma once
9 #include <string>
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "ui/base/ui_export.h"
14 #include "ui/gfx/platform_font.h"
16 namespace gfx {
18 class UI_EXPORT PlatformFontWin : public PlatformFont {
19 public:
20 PlatformFontWin();
21 explicit PlatformFontWin(const Font& other);
22 explicit PlatformFontWin(NativeFont native_font);
23 PlatformFontWin(const std::string& font_name, int font_size);
25 // Dialog units to pixels conversion.
26 // See http://support.microsoft.com/kb/145994 for details.
27 int horizontal_dlus_to_pixels(int dlus) const {
28 return dlus * font_ref_->GetDluBaseX() / 4;
30 int vertical_dlus_to_pixels(int dlus) const {
31 return dlus * font_ref_->height() / 8;
34 // Callback that returns the minimum height that should be used for
35 // gfx::Fonts. Optional. If not specified, the minimum font size is 0.
36 typedef int (*GetMinimumFontSizeCallback)();
37 static GetMinimumFontSizeCallback get_minimum_font_size_callback;
39 // Callback that adjusts a LOGFONT to meet suitability requirements of the
40 // embedding application. Optional. If not specified, no adjustments are
41 // performed other than clamping to a minimum font height if
42 // |get_minimum_font_size_callback| is specified.
43 typedef void (*AdjustFontCallback)(LOGFONT* lf);
44 static AdjustFontCallback adjust_font_callback;
46 // Returns the font name for the system locale. Some fonts, particularly
47 // East Asian fonts, have different names per locale. If the localized font
48 // name could not be retrieved, returns GetFontName().
49 std::string GetLocalizedFontName() const;
51 // Overridden from PlatformFont:
52 virtual Font DeriveFont(int size_delta, int style) const OVERRIDE;
53 virtual int GetHeight() const OVERRIDE;
54 virtual int GetBaseline() const OVERRIDE;
55 virtual int GetAverageCharacterWidth() const OVERRIDE;
56 virtual int GetStringWidth(const string16& text) const OVERRIDE;
57 virtual int GetExpectedTextWidth(int length) const OVERRIDE;
58 virtual int GetStyle() const OVERRIDE;
59 virtual std::string GetFontName() const OVERRIDE;
60 virtual int GetFontSize() const OVERRIDE;
61 virtual NativeFont GetNativeFont() const OVERRIDE;
63 private:
64 virtual ~PlatformFontWin() {}
66 // Chrome text drawing bottoms out in the Windows GDI functions that take an
67 // HFONT (an opaque handle into Windows). To avoid lots of GDI object
68 // allocation and destruction, Font indirectly refers to the HFONT by way of
69 // an HFontRef. That is, every Font has an HFontRef, which has an HFONT.
71 // HFontRef is reference counted. Upon deletion, it deletes the HFONT.
72 // By making HFontRef maintain the reference to the HFONT, multiple
73 // HFontRefs can share the same HFONT, and Font can provide value semantics.
74 class HFontRef : public base::RefCounted<HFontRef> {
75 public:
76 // This constructor takes control of the HFONT, and will delete it when
77 // the HFontRef is deleted.
78 HFontRef(HFONT hfont,
79 int height,
80 int baseline,
81 int ave_char_width,
82 int style);
84 // Accessors
85 HFONT hfont() const { return hfont_; }
86 int height() const { return height_; }
87 int baseline() const { return baseline_; }
88 int ave_char_width() const { return ave_char_width_; }
89 int style() const { return style_; }
90 const std::string& font_name() const { return font_name_; }
91 int font_size() const { return font_size_; }
93 // Returns the average character width in dialog units.
94 int GetDluBaseX();
96 private:
97 friend class base::RefCounted<HFontRef>;
99 ~HFontRef();
101 const HFONT hfont_;
102 const int height_;
103 const int baseline_;
104 const int ave_char_width_;
105 const int style_;
106 // Average character width in dialog units. This is queried lazily from the
107 // system, with an initial value of -1 meaning it hasn't yet been queried.
108 int dlu_base_x_;
109 std::string font_name_;
110 int font_size_;
112 DISALLOW_COPY_AND_ASSIGN(HFontRef);
115 // Initializes this object with a copy of the specified HFONT.
116 void InitWithCopyOfHFONT(HFONT hfont);
118 // Initializes this object with the specified font name and size.
119 void InitWithFontNameAndSize(const std::string& font_name,
120 int font_size);
122 // Returns the base font ref. This should ONLY be invoked on the
123 // UI thread.
124 static HFontRef* GetBaseFontRef();
126 // Creates and returns a new HFONTRef from the specified HFONT.
127 static HFontRef* CreateHFontRef(HFONT font);
129 // Creates a new PlatformFontWin with the specified HFontRef. Used when
130 // constructing a Font from a HFONT we don't want to copy.
131 explicit PlatformFontWin(HFontRef* hfont_ref);
133 // Reference to the base font all fonts are derived from.
134 static HFontRef* base_font_ref_;
136 // Indirect reference to the HFontRef, which references the underlying HFONT.
137 scoped_refptr<HFontRef> font_ref_;
140 } // namespace gfx
142 #endif // UI_GFX_PLATFORM_FONT_WIN_H_