15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef UI_GFX_PLATFORM_FONT_WIN_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UI_GFX_PLATFORM_FONT_WIN_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/ui_export.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/platform_font.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace gfx {
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class UI_EXPORT PlatformFontWin : public PlatformFont {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PlatformFontWin();
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit PlatformFontWin(NativeFont native_font);
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PlatformFontWin(const std::string& font_name, int font_size);
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Dialog units to pixels conversion.
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // See http://support.microsoft.com/kb/145994 for details.
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int horizontal_dlus_to_pixels(int dlus) const {
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return dlus * font_ref_->GetDluBaseX() / 4;
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int vertical_dlus_to_pixels(int dlus)  const {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return dlus * font_ref_->height() / 8;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Callback that returns the minimum height that should be used for
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // gfx::Fonts. Optional. If not specified, the minimum font size is 0.
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef int (*GetMinimumFontSizeCallback)();
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static GetMinimumFontSizeCallback get_minimum_font_size_callback;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Callback that adjusts a LOGFONT to meet suitability requirements of the
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // embedding application. Optional. If not specified, no adjustments are
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // performed other than clamping to a minimum font height if
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |get_minimum_font_size_callback| is specified.
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef void (*AdjustFontCallback)(LOGFONT* lf);
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static AdjustFontCallback adjust_font_callback;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the font name for the system locale. Some fonts, particularly
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // East Asian fonts, have different names per locale. If the localized font
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // name could not be retrieved, returns GetFontName().
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string GetLocalizedFontName() const;
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a derived Font with the specified |style| and with height at most
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |height|. If the height and style of the receiver already match, it is
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returned. Otherwise, the returned Font will have the largest size such that
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // its height is less than or equal to |height| (since there may not exist a
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // size that matches the exact |height| specified).
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Font DeriveFontWithHeight(int height, int style);
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from PlatformFont:
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual Font DeriveFont(int size_delta, int style) const OVERRIDE;
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int GetHeight() const OVERRIDE;
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int GetBaseline() const OVERRIDE;
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int GetAverageCharacterWidth() const OVERRIDE;
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual int GetStringWidth(const base::string16& text) const OVERRIDE;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int GetExpectedTextWidth(int length) const OVERRIDE;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int GetStyle() const OVERRIDE;
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual std::string GetFontName() const OVERRIDE;
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int GetFontSize() const OVERRIDE;
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual NativeFont GetNativeFont() const OVERRIDE;
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~PlatformFontWin() {}
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Chrome text drawing bottoms out in the Windows GDI functions that take an
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // HFONT (an opaque handle into Windows). To avoid lots of GDI object
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // allocation and destruction, Font indirectly refers to the HFONT by way of
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // an HFontRef. That is, every Font has an HFontRef, which has an HFONT.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // HFontRef is reference counted. Upon deletion, it deletes the HFONT.
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // By making HFontRef maintain the reference to the HFONT, multiple
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // HFontRefs can share the same HFONT, and Font can provide value semantics.
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class HFontRef : public base::RefCounted<HFontRef> {
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // This constructor takes control of the HFONT, and will delete it when
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // the HFontRef is deleted.
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    HFontRef(HFONT hfont,
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             int font_size,
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             int height,
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             int baseline,
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             int ave_char_width,
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             int style);
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Accessors
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    HFONT hfont() const { return hfont_; }
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int height() const { return height_; }
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int baseline() const { return baseline_; }
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int ave_char_width() const { return ave_char_width_; }
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int style() const { return style_; }
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& font_name() const { return font_name_; }
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int font_size() const { return font_size_; }
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int requested_font_size() const { return requested_font_size_; }
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Returns the average character width in dialog units.
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int GetDluBaseX();
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   private:
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    friend class base::RefCounted<HFontRef>;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ~HFontRef();
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const HFONT hfont_;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const int font_size_;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const int height_;
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const int baseline_;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const int ave_char_width_;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const int style_;
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Average character width in dialog units. This is queried lazily from the
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // system, with an initial value of -1 meaning it hasn't yet been queried.
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int dlu_base_x_;
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string font_name_;
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // If the requested font size is not possible for the font, |font_size_|
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // will be different than |requested_font_size_|. This is stored separately
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // so that code that increases the font size in a loop will not cause the
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // loop to get stuck on the same size.
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int requested_font_size_;
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(HFontRef);
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes this object with a copy of the specified HFONT.
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void InitWithCopyOfHFONT(HFONT hfont);
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes this object with the specified font name and size.
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void InitWithFontNameAndSize(const std::string& font_name,
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               int font_size);
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the base font ref. This should ONLY be invoked on the
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // UI thread.
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static HFontRef* GetBaseFontRef();
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates and returns a new HFONTRef from the specified HFONT.
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static HFontRef* CreateHFontRef(HFONT font);
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a new PlatformFontWin with the specified HFontRef. Used when
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // constructing a Font from a HFONT we don't want to copy.
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit PlatformFontWin(HFontRef* hfont_ref);
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Reference to the base font all fonts are derived from.
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static HFontRef* base_font_ref_;
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Indirect reference to the HFontRef, which references the underlying HFONT.
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<HFontRef> font_ref_;
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(PlatformFontWin);
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace gfx
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // UI_GFX_PLATFORM_FONT_WIN_H_
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
159