OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 GFX_PLATFORM_FONT_WIN_ |
| 6 #define GFX_PLATFORM_FONT_WIN_ |
| 7 #pragma once |
| 8 |
| 9 #include "base/ref_counted.h" |
| 10 #include "gfx/platform_font.h" |
| 11 |
| 12 namespace gfx { |
| 13 |
| 14 class PlatformFontWin : public PlatformFont { |
| 15 public: |
| 16 PlatformFontWin(); |
| 17 explicit PlatformFontWin(const Font& other); |
| 18 explicit PlatformFontWin(NativeFont native_font); |
| 19 PlatformFontWin(const std::wstring& font_name, |
| 20 int font_size); |
| 21 |
| 22 // Dialog units to pixels conversion. |
| 23 // See http://support.microsoft.com/kb/145994 for details. |
| 24 int horizontal_dlus_to_pixels(int dlus) const { |
| 25 return dlus * font_ref_->dlu_base_x() / 4; |
| 26 } |
| 27 int vertical_dlus_to_pixels(int dlus) const { |
| 28 return dlus * font_ref_->height() / 8; |
| 29 } |
| 30 |
| 31 // Callback that returns the minimum height that should be used for |
| 32 // gfx::Fonts. Optional. If not specified, the minimum font size is 0. |
| 33 typedef int (*GetMinimumFontSizeCallback)(); |
| 34 static GetMinimumFontSizeCallback get_minimum_font_size_callback; |
| 35 |
| 36 // Callback that adjusts a LOGFONT to meet suitability requirements of the |
| 37 // embedding application. Optional. If not specified, no adjustments are |
| 38 // performed other than clamping to a minimum font height if |
| 39 // |get_minimum_font_size_callback| is specified. |
| 40 typedef void (*AdjustFontCallback)(LOGFONT* lf); |
| 41 static AdjustFontCallback adjust_font_callback; |
| 42 |
| 43 // Overridden from PlatformFont: |
| 44 virtual Font DeriveFont(int size_delta, int style) const; |
| 45 virtual int GetHeight() const; |
| 46 virtual int GetBaseline() const; |
| 47 virtual int GetAverageCharacterWidth() const; |
| 48 virtual int GetStringWidth(const std::wstring& text) const; |
| 49 virtual int GetExpectedTextWidth(int length) const; |
| 50 virtual int GetStyle() const; |
| 51 virtual const std::wstring& GetFontName() const; |
| 52 virtual int GetFontSize() const; |
| 53 virtual NativeFont GetNativeFont() const; |
| 54 |
| 55 private: |
| 56 virtual ~PlatformFontWin() {} |
| 57 |
| 58 // Chrome text drawing bottoms out in the Windows GDI functions that take an |
| 59 // HFONT (an opaque handle into Windows). To avoid lots of GDI object |
| 60 // allocation and destruction, Font indirectly refers to the HFONT by way of |
| 61 // an HFontRef. That is, every Font has an HFontRef, which has an HFONT. |
| 62 // |
| 63 // HFontRef is reference counted. Upon deletion, it deletes the HFONT. |
| 64 // By making HFontRef maintain the reference to the HFONT, multiple |
| 65 // HFontRefs can share the same HFONT, and Font can provide value semantics. |
| 66 class HFontRef : public base::RefCounted<HFontRef> { |
| 67 public: |
| 68 // This constructor takes control of the HFONT, and will delete it when |
| 69 // the HFontRef is deleted. |
| 70 HFontRef(HFONT hfont, |
| 71 int height, |
| 72 int baseline, |
| 73 int ave_char_width, |
| 74 int style, |
| 75 int dlu_base_x); |
| 76 |
| 77 // Accessors |
| 78 HFONT hfont() const { return hfont_; } |
| 79 int height() const { return height_; } |
| 80 int baseline() const { return baseline_; } |
| 81 int ave_char_width() const { return ave_char_width_; } |
| 82 int style() const { return style_; } |
| 83 int dlu_base_x() const { return dlu_base_x_; } |
| 84 const std::wstring& font_name() const { return font_name_; } |
| 85 |
| 86 private: |
| 87 friend class base::RefCounted<HFontRef>; |
| 88 |
| 89 ~HFontRef(); |
| 90 |
| 91 const HFONT hfont_; |
| 92 const int height_; |
| 93 const int baseline_; |
| 94 const int ave_char_width_; |
| 95 const int style_; |
| 96 // Constants used in converting dialog units to pixels. |
| 97 const int dlu_base_x_; |
| 98 std::wstring font_name_; |
| 99 |
| 100 DISALLOW_COPY_AND_ASSIGN(HFontRef); |
| 101 }; |
| 102 |
| 103 // Initializes this object with a copy of the specified HFONT. |
| 104 void InitWithCopyOfHFONT(HFONT hfont); |
| 105 |
| 106 // Initializes this object with the specified font name and size. |
| 107 void InitWithFontNameAndSize(const std::wstring& font_name, |
| 108 int font_size); |
| 109 |
| 110 // Returns the base font ref. This should ONLY be invoked on the |
| 111 // UI thread. |
| 112 static HFontRef* GetBaseFontRef(); |
| 113 |
| 114 // Creates and returns a new HFONTRef from the specified HFONT. |
| 115 static HFontRef* CreateHFontRef(HFONT font); |
| 116 |
| 117 // Creates a new PlatformFontWin with the specified HFontRef. Used when |
| 118 // constructing a Font from a HFONT we don't want to copy. |
| 119 explicit PlatformFontWin(HFontRef* hfont_ref); |
| 120 |
| 121 // Reference to the base font all fonts are derived from. |
| 122 static HFontRef* base_font_ref_; |
| 123 |
| 124 // Indirect reference to the HFontRef, which references the underlying HFONT. |
| 125 scoped_refptr<HFontRef> font_ref_; |
| 126 }; |
| 127 |
| 128 } // namespace gfx |
| 129 |
| 130 #endif // GFX_PLATFORM_FONT_WIN_ |
| 131 |
OLD | NEW |