SkFontMgr_indirect.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2014 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkFontMgr.h"
  8. #include "include/core/SkFontStyle.h"
  9. #include "include/core/SkRefCnt.h"
  10. #include "include/core/SkStream.h"
  11. #include "include/core/SkString.h"
  12. #include "include/core/SkTypeface.h"
  13. #include "include/core/SkTypes.h"
  14. #include "include/ports/SkFontMgr_indirect.h"
  15. #include "include/ports/SkRemotableFontMgr.h"
  16. #include "include/private/SkMutex.h"
  17. #include "include/private/SkOnce.h"
  18. #include "include/private/SkTArray.h"
  19. #include "include/private/SkTemplates.h"
  20. class SkData;
  21. class SkStyleSet_Indirect : public SkFontStyleSet {
  22. public:
  23. /** Takes ownership of the SkRemotableFontIdentitySet. */
  24. SkStyleSet_Indirect(const SkFontMgr_Indirect* owner, int familyIndex,
  25. SkRemotableFontIdentitySet* data)
  26. : fOwner(SkRef(owner)), fFamilyIndex(familyIndex), fData(data)
  27. { }
  28. int count() override { return fData->count(); }
  29. void getStyle(int index, SkFontStyle* fs, SkString* style) override {
  30. if (fs) {
  31. *fs = fData->at(index).fFontStyle;
  32. }
  33. if (style) {
  34. // TODO: is this useful? Current locale?
  35. style->reset();
  36. }
  37. }
  38. SkTypeface* createTypeface(int index) override {
  39. return fOwner->createTypefaceFromFontId(fData->at(index));
  40. }
  41. SkTypeface* matchStyle(const SkFontStyle& pattern) override {
  42. if (fFamilyIndex >= 0) {
  43. SkFontIdentity id = fOwner->fProxy->matchIndexStyle(fFamilyIndex, pattern);
  44. return fOwner->createTypefaceFromFontId(id);
  45. }
  46. return this->matchStyleCSS3(pattern);
  47. }
  48. private:
  49. sk_sp<const SkFontMgr_Indirect> fOwner;
  50. int fFamilyIndex;
  51. sk_sp<SkRemotableFontIdentitySet> fData;
  52. };
  53. int SkFontMgr_Indirect::onCountFamilies() const {
  54. return 0;
  55. }
  56. void SkFontMgr_Indirect::onGetFamilyName(int index, SkString* familyName) const {
  57. SK_ABORT("Not implemented");
  58. }
  59. SkFontStyleSet* SkFontMgr_Indirect::onCreateStyleSet(int index) const {
  60. SK_ABORT("Not implemented");
  61. return nullptr;
  62. }
  63. SkFontStyleSet* SkFontMgr_Indirect::onMatchFamily(const char familyName[]) const {
  64. return new SkStyleSet_Indirect(this, -1, fProxy->matchName(familyName));
  65. }
  66. SkTypeface* SkFontMgr_Indirect::createTypefaceFromFontId(const SkFontIdentity& id) const {
  67. if (id.fDataId == SkFontIdentity::kInvalidDataId) {
  68. return nullptr;
  69. }
  70. SkAutoMutexExclusive ama(fDataCacheMutex);
  71. sk_sp<SkTypeface> dataTypeface;
  72. int dataTypefaceIndex = 0;
  73. for (int i = 0; i < fDataCache.count(); ++i) {
  74. const DataEntry& entry = fDataCache[i];
  75. if (entry.fDataId == id.fDataId) {
  76. if (entry.fTtcIndex == id.fTtcIndex &&
  77. !entry.fTypeface->weak_expired() && entry.fTypeface->try_ref())
  78. {
  79. return entry.fTypeface;
  80. }
  81. if (dataTypeface.get() == nullptr &&
  82. !entry.fTypeface->weak_expired() && entry.fTypeface->try_ref())
  83. {
  84. dataTypeface.reset(entry.fTypeface);
  85. dataTypefaceIndex = entry.fTtcIndex;
  86. }
  87. }
  88. if (entry.fTypeface->weak_expired()) {
  89. fDataCache.removeShuffle(i);
  90. --i;
  91. }
  92. }
  93. // No exact match, but did find a data match.
  94. if (dataTypeface.get() != nullptr) {
  95. std::unique_ptr<SkStreamAsset> stream(dataTypeface->openStream(nullptr));
  96. if (stream.get() != nullptr) {
  97. return fImpl->makeFromStream(std::move(stream), dataTypefaceIndex).release();
  98. }
  99. }
  100. // No data match, request data and add entry.
  101. std::unique_ptr<SkStreamAsset> stream(fProxy->getData(id.fDataId));
  102. if (stream.get() == nullptr) {
  103. return nullptr;
  104. }
  105. sk_sp<SkTypeface> typeface(fImpl->makeFromStream(std::move(stream), id.fTtcIndex));
  106. if (typeface.get() == nullptr) {
  107. return nullptr;
  108. }
  109. DataEntry& newEntry = fDataCache.push_back();
  110. typeface->weak_ref();
  111. newEntry.fDataId = id.fDataId;
  112. newEntry.fTtcIndex = id.fTtcIndex;
  113. newEntry.fTypeface = typeface.get(); // weak reference passed to new entry.
  114. return typeface.release();
  115. }
  116. SkTypeface* SkFontMgr_Indirect::onMatchFamilyStyle(const char familyName[],
  117. const SkFontStyle& fontStyle) const {
  118. SkFontIdentity id = fProxy->matchNameStyle(familyName, fontStyle);
  119. return this->createTypefaceFromFontId(id);
  120. }
  121. SkTypeface* SkFontMgr_Indirect::onMatchFamilyStyleCharacter(const char familyName[],
  122. const SkFontStyle& style,
  123. const char* bcp47[],
  124. int bcp47Count,
  125. SkUnichar character) const {
  126. SkFontIdentity id = fProxy->matchNameStyleCharacter(familyName, style, bcp47,
  127. bcp47Count, character);
  128. return this->createTypefaceFromFontId(id);
  129. }
  130. SkTypeface* SkFontMgr_Indirect::onMatchFaceStyle(const SkTypeface* familyMember,
  131. const SkFontStyle& fontStyle) const {
  132. SkString familyName;
  133. familyMember->getFamilyName(&familyName);
  134. return this->matchFamilyStyle(familyName.c_str(), fontStyle);
  135. }
  136. sk_sp<SkTypeface> SkFontMgr_Indirect::onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream,
  137. int ttcIndex) const {
  138. return fImpl->makeFromStream(std::move(stream), ttcIndex);
  139. }
  140. sk_sp<SkTypeface> SkFontMgr_Indirect::onMakeFromFile(const char path[], int ttcIndex) const {
  141. return fImpl->makeFromFile(path, ttcIndex);
  142. }
  143. sk_sp<SkTypeface> SkFontMgr_Indirect::onMakeFromData(sk_sp<SkData> data, int ttcIndex) const {
  144. return fImpl->makeFromData(std::move(data), ttcIndex);
  145. }
  146. sk_sp<SkTypeface> SkFontMgr_Indirect::onLegacyMakeTypeface(const char familyName[],
  147. SkFontStyle style) const {
  148. sk_sp<SkTypeface> face(this->matchFamilyStyle(familyName, style));
  149. if (nullptr == face.get()) {
  150. face.reset(this->matchFamilyStyle(nullptr, style));
  151. }
  152. if (nullptr == face.get()) {
  153. SkFontIdentity fontId = this->fProxy->matchIndexStyle(0, style);
  154. face.reset(this->createTypefaceFromFontId(fontId));
  155. }
  156. return face;
  157. }