font_service_app.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2015 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 COMPONENTS_SERVICES_FONT_FONT_SERVICE_APP_H_
  5. #define COMPONENTS_SERVICES_FONT_FONT_SERVICE_APP_H_
  6. #include <stdint.h>
  7. #include <tuple>
  8. #include <vector>
  9. #include "base/containers/lru_cache.h"
  10. #include "base/files/file_path.h"
  11. #include "components/services/font/public/mojom/font_service.mojom.h"
  12. #include "mojo/public/cpp/bindings/receiver_set.h"
  13. #include "pdf/buildflags.h"
  14. #include "third_party/skia/include/core/SkFontStyle.h"
  15. namespace font_service {
  16. // This class is instantiated in the browser process.
  17. class FontServiceApp : public mojom::FontService {
  18. public:
  19. FontServiceApp();
  20. FontServiceApp(const FontServiceApp&) = delete;
  21. FontServiceApp& operator=(const FontServiceApp&) = delete;
  22. ~FontServiceApp() override;
  23. void BindReceiver(mojo::PendingReceiver<mojom::FontService> receiver);
  24. private:
  25. // FontService:
  26. void MatchFamilyName(const std::string& family_name,
  27. mojom::TypefaceStylePtr requested_style,
  28. MatchFamilyNameCallback callback) override;
  29. void OpenStream(uint32_t id_number, OpenStreamCallback callback) override;
  30. void FallbackFontForCharacter(
  31. uint32_t character,
  32. const std::string& locale,
  33. FallbackFontForCharacterCallback callback) override;
  34. void FontRenderStyleForStrike(
  35. const std::string& family,
  36. uint32_t size,
  37. bool italic,
  38. bool bold,
  39. float device_scale_factor,
  40. FontRenderStyleForStrikeCallback callback) override;
  41. void MatchFontByPostscriptNameOrFullFontName(
  42. const std::string& family,
  43. MatchFontByPostscriptNameOrFullFontNameCallback callback) override;
  44. #if BUILDFLAG(ENABLE_PDF)
  45. void MatchFontWithFallback(const std::string& family,
  46. bool is_bold,
  47. bool is_italic,
  48. uint32_t charset,
  49. uint32_t fallbackFamilyType,
  50. MatchFontWithFallbackCallback callback) override;
  51. #endif // BUILDFLAG(ENABLE_PDF)
  52. size_t FindOrAddPath(const base::FilePath& path);
  53. mojo::ReceiverSet<mojom::FontService> receivers_;
  54. // We don't want to leak paths to our callers; we thus enumerate the paths of
  55. // fonts.
  56. std::vector<base::FilePath> paths_;
  57. // On some platforms, font matching is very expensive, ranging from 2-30+ms.
  58. // We keep a cache of results to speed this up.
  59. struct MatchCacheKey {
  60. std::string family_name;
  61. SkFontStyle font_style;
  62. bool operator==(const MatchCacheKey& other) const {
  63. return family_name == other.family_name && font_style == other.font_style;
  64. }
  65. };
  66. struct MatchCacheKeyCompare {
  67. bool operator()(const MatchCacheKey& lhs, const MatchCacheKey& rhs) const {
  68. return std::make_tuple(lhs.family_name, lhs.font_style.weight(),
  69. lhs.font_style.width(), lhs.font_style.slant()) <
  70. std::make_tuple(rhs.family_name, rhs.font_style.weight(),
  71. rhs.font_style.width(), rhs.font_style.slant());
  72. }
  73. };
  74. struct MatchCacheValue {
  75. MatchCacheValue();
  76. ~MatchCacheValue();
  77. MatchCacheValue(MatchCacheValue&&);
  78. std::string family_name;
  79. mojom::FontIdentityPtr identity;
  80. mojom::TypefaceStylePtr style;
  81. };
  82. base::LRUCache<MatchCacheKey, MatchCacheValue, MatchCacheKeyCompare>
  83. match_cache_;
  84. };
  85. } // namespace font_service
  86. #endif // COMPONENTS_SERVICES_FONT_FONT_SERVICE_APP_H_