platform_font_skia.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_SKIA_H_
  5. #define UI_GFX_PLATFORM_FONT_SKIA_H_
  6. #include <memory>
  7. #include <string>
  8. #include "build/build_config.h"
  9. #include "third_party/skia/include/core/SkTypeface.h"
  10. #include "ui/gfx/font_render_params.h"
  11. #include "ui/gfx/platform_font.h"
  12. namespace gfx {
  13. class GFX_EXPORT PlatformFontSkia : public PlatformFont {
  14. public:
  15. // TODO(derat): Get rid of the default constructor in favor of using
  16. // FontList (which also has the concept of a default font but may contain
  17. // multiple font families) everywhere. See http://crbug.com/398885#c16.
  18. PlatformFontSkia();
  19. PlatformFontSkia(const std::string& font_name, int font_size_pixels);
  20. // Wraps the provided SkTypeface without triggering a font rematch.
  21. PlatformFontSkia(sk_sp<SkTypeface> typeface,
  22. int font_size_pixels,
  23. const absl::optional<FontRenderParams>& params);
  24. PlatformFontSkia(const PlatformFontSkia&) = delete;
  25. PlatformFontSkia& operator=(const PlatformFontSkia&) = delete;
  26. // Initializes the default PlatformFont.
  27. static void EnsuresDefaultFontIsInitialized();
  28. // Resets and reloads the cached system font used by the default constructor.
  29. // This function is useful when the system font has changed, for example, when
  30. // the locale has changed.
  31. static void ReloadDefaultFont();
  32. // Sets the default font. |font_description| is a FontList font description;
  33. // only the first family will be used.
  34. // TODO(sergeyu): Remove this function. Currently it is used only on ChromeOS
  35. // to set the default font to the one loaded from resources.
  36. //
  37. static void SetDefaultFontDescription(const std::string& font_description);
  38. // Overridden from PlatformFont:
  39. Font DeriveFont(int size_delta,
  40. int style,
  41. Font::Weight weight) const override;
  42. int GetHeight() override;
  43. Font::Weight GetWeight() const override;
  44. int GetBaseline() override;
  45. int GetCapHeight() override;
  46. int GetExpectedTextWidth(int length) override;
  47. int GetStyle() const override;
  48. const std::string& GetFontName() const override;
  49. std::string GetActualFontName() const override;
  50. int GetFontSize() const override;
  51. const FontRenderParams& GetFontRenderParams() override;
  52. sk_sp<SkTypeface> GetNativeSkTypeface() const override;
  53. private:
  54. // Create a new instance of this object with the specified properties. Called
  55. // from DeriveFont.
  56. PlatformFontSkia(sk_sp<SkTypeface> typeface,
  57. const std::string& family,
  58. int size_pixels,
  59. int style,
  60. Font::Weight weight,
  61. const FontRenderParams& params);
  62. ~PlatformFontSkia() override;
  63. // Initializes this object based on the passed-in details. If |typeface| is
  64. // empty, a new typeface will be loaded.
  65. void InitFromDetails(sk_sp<SkTypeface> typeface,
  66. const std::string& font_family,
  67. int font_size_pixels,
  68. int style,
  69. Font::Weight weight,
  70. const FontRenderParams& params);
  71. // Initializes this object as a copy of another PlatformFontSkia.
  72. void InitFromPlatformFont(const PlatformFontSkia* other);
  73. // Computes the metrics if they have not yet been computed.
  74. void ComputeMetricsIfNecessary();
  75. sk_sp<SkTypeface> typeface_;
  76. // Additional information about the face.
  77. // Skia actually expects a family name and not a font name.
  78. std::string font_family_;
  79. int font_size_pixels_;
  80. int style_;
  81. float device_scale_factor_;
  82. // Information describing how the font should be rendered.
  83. FontRenderParams font_render_params_;
  84. // Cached metrics, generated on demand.
  85. bool metrics_need_computation_ = true;
  86. int ascent_pixels_;
  87. int height_pixels_;
  88. int cap_height_pixels_;
  89. double average_width_pixels_;
  90. Font::Weight weight_;
  91. // A font description string of the format used by FontList.
  92. static std::string* default_font_description_;
  93. };
  94. } // namespace gfx
  95. #endif // UI_GFX_PLATFORM_FONT_SKIA_H_