platform_font.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #ifndef UI_GFX_PLATFORM_FONT_H_
  5. #define UI_GFX_PLATFORM_FONT_H_
  6. #include <string>
  7. #include "base/memory/ref_counted.h"
  8. #include "build/build_config.h"
  9. #include "third_party/abseil-cpp/absl/types/optional.h"
  10. #include "third_party/skia/include/core/SkRefCnt.h"
  11. #include "third_party/skia/include/core/SkTypeface.h"
  12. #include "ui/gfx/font.h"
  13. #include "ui/gfx/font_render_params.h"
  14. #include "ui/gfx/gfx_export.h"
  15. #include "ui/gfx/native_widget_types.h"
  16. namespace gfx {
  17. class GFX_EXPORT PlatformFont : public base::RefCounted<PlatformFont> {
  18. public:
  19. // The size of the font returned by CreateDefault() on a "default" platform
  20. // configuration. This allows UI that wants to target a particular size of font
  21. // to obtain that size for the majority of users, while still compensating for a
  22. // user preference for a larger or smaller font.
  23. #if BUILDFLAG(IS_APPLE)
  24. static constexpr int kDefaultBaseFontSize = 13;
  25. #else
  26. static constexpr int kDefaultBaseFontSize = 12;
  27. #endif
  28. // Creates an appropriate PlatformFont implementation.
  29. static PlatformFont* CreateDefault();
  30. #if BUILDFLAG(IS_APPLE)
  31. static PlatformFont* CreateFromNativeFont(NativeFont native_font);
  32. #endif
  33. // Creates a PlatformFont implementation with the specified |font_name|
  34. // (encoded in UTF-8) and |font_size| in pixels.
  35. static PlatformFont* CreateFromNameAndSize(const std::string& font_name,
  36. int font_size);
  37. // Creates a PlatformFont instance from the provided SkTypeface, ideally by
  38. // just wrapping it without triggering a new font match. Implemented for
  39. // PlatformFontSkia which provides true wrapping of the provided SkTypeface.
  40. // The FontRenderParams can be provided or they will be determined by using
  41. // gfx::GetFontRenderParams(...) otherwise.
  42. static PlatformFont* CreateFromSkTypeface(
  43. sk_sp<SkTypeface> typeface,
  44. int font_size,
  45. const absl::optional<FontRenderParams>& params);
  46. // Returns a new Font derived from the existing font.
  47. // |size_delta| is the size in pixels to add to the current font.
  48. // The style parameter specifies the new style for the font, and is a
  49. // bitmask of the values: ITALIC and UNDERLINE.
  50. // The weight parameter specifies the desired weight of the font.
  51. virtual Font DeriveFont(int size_delta,
  52. int style,
  53. Font::Weight weight) const = 0;
  54. // Returns the number of vertical pixels needed to display characters from
  55. // the specified font. This may include some leading, i.e. height may be
  56. // greater than just ascent + descent. Specifically, the Windows and Mac
  57. // implementations include leading and the Linux one does not. This may
  58. // need to be revisited in the future.
  59. virtual int GetHeight() = 0;
  60. // Returns the font weight.
  61. virtual Font::Weight GetWeight() const = 0;
  62. // Returns the baseline, or ascent, of the font.
  63. virtual int GetBaseline() = 0;
  64. // Returns the cap height of the font.
  65. virtual int GetCapHeight() = 0;
  66. // Returns the expected number of horizontal pixels needed to display the
  67. // specified length of characters. Call GetStringWidth() to retrieve the
  68. // actual number.
  69. virtual int GetExpectedTextWidth(int length) = 0;
  70. // Returns the style of the font.
  71. virtual int GetStyle() const = 0;
  72. // Returns the specified font name in UTF-8.
  73. virtual const std::string& GetFontName() const = 0;
  74. // Returns the actually used font name in UTF-8.
  75. virtual std::string GetActualFontName() const = 0;
  76. // Returns the font size in pixels.
  77. virtual int GetFontSize() const = 0;
  78. // Returns an object describing how the font should be rendered.
  79. virtual const FontRenderParams& GetFontRenderParams() = 0;
  80. #if BUILDFLAG(IS_APPLE)
  81. // Returns the native font handle.
  82. virtual NativeFont GetNativeFont() const = 0;
  83. #endif
  84. // Returns the underlying Skia typeface. Used in RenderTextHarfBuzz for having
  85. // access to the exact Skia typeface returned by font fallback, as we would
  86. // otherwise lose the handle to the correct platform font instance.
  87. virtual sk_sp<SkTypeface> GetNativeSkTypeface() const = 0;
  88. protected:
  89. virtual ~PlatformFont() {}
  90. private:
  91. friend class base::RefCounted<PlatformFont>;
  92. };
  93. } // namespace gfx
  94. #endif // UI_GFX_PLATFORM_FONT_H_