SkFontMgr_custom.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  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/SkFontArguments.h"
  8. #include "include/core/SkFontMgr.h"
  9. #include "include/core/SkFontStyle.h"
  10. #include "include/core/SkRefCnt.h"
  11. #include "include/core/SkStream.h"
  12. #include "include/core/SkString.h"
  13. #include "include/core/SkTypeface.h"
  14. #include "include/core/SkTypes.h"
  15. #include "include/private/SkTArray.h"
  16. #include "include/private/SkTemplates.h"
  17. #include "src/core/SkFontDescriptor.h"
  18. #include "src/core/SkMakeUnique.h"
  19. #include "src/ports/SkFontHost_FreeType_common.h"
  20. #include "src/ports/SkFontMgr_custom.h"
  21. #include <limits>
  22. #include <memory>
  23. class SkData;
  24. SkTypeface_Custom::SkTypeface_Custom(const SkFontStyle& style, bool isFixedPitch,
  25. bool sysFont, const SkString familyName, int index)
  26. : INHERITED(style, isFixedPitch)
  27. , fIsSysFont(sysFont), fFamilyName(familyName), fIndex(index)
  28. { }
  29. bool SkTypeface_Custom::isSysFont() const { return fIsSysFont; }
  30. void SkTypeface_Custom::onGetFamilyName(SkString* familyName) const {
  31. *familyName = fFamilyName;
  32. }
  33. void SkTypeface_Custom::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
  34. desc->setFamilyName(fFamilyName.c_str());
  35. desc->setStyle(this->fontStyle());
  36. *isLocal = !this->isSysFont();
  37. }
  38. int SkTypeface_Custom::getIndex() const { return fIndex; }
  39. SkTypeface_Empty::SkTypeface_Empty() : INHERITED(SkFontStyle(), false, true, SkString(), 0) {}
  40. std::unique_ptr<SkStreamAsset> SkTypeface_Empty::onOpenStream(int*) const { return nullptr; }
  41. sk_sp<SkTypeface> SkTypeface_Empty::onMakeClone(const SkFontArguments& args) const {
  42. return sk_ref_sp(this);
  43. }
  44. SkTypeface_Stream::SkTypeface_Stream(std::unique_ptr<SkFontData> fontData,
  45. const SkFontStyle& style, bool isFixedPitch, bool sysFont,
  46. const SkString familyName)
  47. : INHERITED(style, isFixedPitch, sysFont, familyName, fontData->getIndex())
  48. , fData(std::move(fontData))
  49. { }
  50. std::unique_ptr<SkStreamAsset> SkTypeface_Stream::onOpenStream(int* ttcIndex) const {
  51. *ttcIndex = fData->getIndex();
  52. return fData->getStream()->duplicate();
  53. }
  54. std::unique_ptr<SkFontData> SkTypeface_Stream::onMakeFontData() const {
  55. return skstd::make_unique<SkFontData>(*fData);
  56. }
  57. sk_sp<SkTypeface> SkTypeface_Stream::onMakeClone(const SkFontArguments& args) const {
  58. std::unique_ptr<SkFontData> data = this->cloneFontData(args);
  59. if (!data) {
  60. return nullptr;
  61. }
  62. SkString familyName;
  63. this->getFamilyName(&familyName);
  64. return sk_make_sp<SkTypeface_Stream>(std::move(data),
  65. this->fontStyle(),
  66. this->isFixedPitch(),
  67. this->isSysFont(),
  68. familyName);
  69. }
  70. SkTypeface_File::SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
  71. const SkString familyName, const char path[], int index)
  72. : INHERITED(style, isFixedPitch, sysFont, familyName, index)
  73. , fPath(path)
  74. { }
  75. std::unique_ptr<SkStreamAsset> SkTypeface_File::onOpenStream(int* ttcIndex) const {
  76. *ttcIndex = this->getIndex();
  77. return SkStream::MakeFromFile(fPath.c_str());
  78. }
  79. sk_sp<SkTypeface> SkTypeface_File::onMakeClone(const SkFontArguments& args) const {
  80. std::unique_ptr<SkFontData> data = this->cloneFontData(args);
  81. if (!data) {
  82. return nullptr;
  83. }
  84. SkString familyName;
  85. this->getFamilyName(&familyName);
  86. return sk_make_sp<SkTypeface_Stream>(std::move(data),
  87. this->fontStyle(),
  88. this->isFixedPitch(),
  89. this->isSysFont(),
  90. familyName);
  91. }
  92. ///////////////////////////////////////////////////////////////////////////////
  93. SkFontStyleSet_Custom::SkFontStyleSet_Custom(const SkString familyName) : fFamilyName(familyName) {}
  94. void SkFontStyleSet_Custom::appendTypeface(sk_sp<SkTypeface_Custom> typeface) {
  95. fStyles.emplace_back(std::move(typeface));
  96. }
  97. int SkFontStyleSet_Custom::count() {
  98. return fStyles.count();
  99. }
  100. void SkFontStyleSet_Custom::getStyle(int index, SkFontStyle* style, SkString* name) {
  101. SkASSERT(index < fStyles.count());
  102. if (style) {
  103. *style = fStyles[index]->fontStyle();
  104. }
  105. if (name) {
  106. name->reset();
  107. }
  108. }
  109. SkTypeface* SkFontStyleSet_Custom::createTypeface(int index) {
  110. SkASSERT(index < fStyles.count());
  111. return SkRef(fStyles[index].get());
  112. }
  113. SkTypeface* SkFontStyleSet_Custom::matchStyle(const SkFontStyle& pattern) {
  114. return this->matchStyleCSS3(pattern);
  115. }
  116. SkString SkFontStyleSet_Custom::getFamilyName() { return fFamilyName; }
  117. SkFontMgr_Custom::SkFontMgr_Custom(const SystemFontLoader& loader) : fDefaultFamily(nullptr) {
  118. loader.loadSystemFonts(fScanner, &fFamilies);
  119. // Try to pick a default font.
  120. static const char* defaultNames[] = {
  121. "Arial", "Verdana", "Times New Roman", "Droid Sans", nullptr
  122. };
  123. for (size_t i = 0; i < SK_ARRAY_COUNT(defaultNames); ++i) {
  124. sk_sp<SkFontStyleSet_Custom> set(this->onMatchFamily(defaultNames[i]));
  125. if (nullptr == set) {
  126. continue;
  127. }
  128. sk_sp<SkTypeface> tf(set->matchStyle(SkFontStyle(SkFontStyle::kNormal_Weight,
  129. SkFontStyle::kNormal_Width,
  130. SkFontStyle::kUpright_Slant)));
  131. if (nullptr == tf) {
  132. continue;
  133. }
  134. fDefaultFamily = set.get();
  135. break;
  136. }
  137. if (nullptr == fDefaultFamily) {
  138. fDefaultFamily = fFamilies[0].get();
  139. }
  140. }
  141. int SkFontMgr_Custom::onCountFamilies() const {
  142. return fFamilies.count();
  143. }
  144. void SkFontMgr_Custom::onGetFamilyName(int index, SkString* familyName) const {
  145. SkASSERT(index < fFamilies.count());
  146. familyName->set(fFamilies[index]->getFamilyName());
  147. }
  148. SkFontStyleSet_Custom* SkFontMgr_Custom::onCreateStyleSet(int index) const {
  149. SkASSERT(index < fFamilies.count());
  150. return SkRef(fFamilies[index].get());
  151. }
  152. SkFontStyleSet_Custom* SkFontMgr_Custom::onMatchFamily(const char familyName[]) const {
  153. for (int i = 0; i < fFamilies.count(); ++i) {
  154. if (fFamilies[i]->getFamilyName().equals(familyName)) {
  155. return SkRef(fFamilies[i].get());
  156. }
  157. }
  158. return nullptr;
  159. }
  160. SkTypeface* SkFontMgr_Custom::onMatchFamilyStyle(const char familyName[],
  161. const SkFontStyle& fontStyle) const
  162. {
  163. sk_sp<SkFontStyleSet> sset(this->matchFamily(familyName));
  164. return sset->matchStyle(fontStyle);
  165. }
  166. SkTypeface* SkFontMgr_Custom::onMatchFamilyStyleCharacter(const char familyName[],
  167. const SkFontStyle&,
  168. const char* bcp47[], int bcp47Count,
  169. SkUnichar character) const
  170. {
  171. return nullptr;
  172. }
  173. SkTypeface* SkFontMgr_Custom::onMatchFaceStyle(const SkTypeface* familyMember,
  174. const SkFontStyle& fontStyle) const
  175. {
  176. for (int i = 0; i < fFamilies.count(); ++i) {
  177. for (int j = 0; j < fFamilies[i]->fStyles.count(); ++j) {
  178. if (fFamilies[i]->fStyles[j].get() == familyMember) {
  179. return fFamilies[i]->matchStyle(fontStyle);
  180. }
  181. }
  182. }
  183. return nullptr;
  184. }
  185. sk_sp<SkTypeface> SkFontMgr_Custom::onMakeFromData(sk_sp<SkData> data, int ttcIndex) const {
  186. return this->makeFromStream(skstd::make_unique<SkMemoryStream>(std::move(data)), ttcIndex);
  187. }
  188. sk_sp<SkTypeface> SkFontMgr_Custom::onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream,
  189. int ttcIndex) const {
  190. return this->makeFromStream(std::move(stream), SkFontArguments().setCollectionIndex(ttcIndex));
  191. }
  192. sk_sp<SkTypeface> SkFontMgr_Custom::onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream,
  193. const SkFontArguments& args) const {
  194. using Scanner = SkTypeface_FreeType::Scanner;
  195. bool isFixedPitch;
  196. SkFontStyle style;
  197. SkString name;
  198. Scanner::AxisDefinitions axisDefinitions;
  199. if (!fScanner.scanFont(stream.get(), args.getCollectionIndex(),
  200. &name, &style, &isFixedPitch, &axisDefinitions))
  201. {
  202. return nullptr;
  203. }
  204. const SkFontArguments::VariationPosition position = args.getVariationDesignPosition();
  205. SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
  206. Scanner::computeAxisValues(axisDefinitions, position, axisValues, name);
  207. auto data = skstd::make_unique<SkFontData>(std::move(stream), args.getCollectionIndex(),
  208. axisValues.get(), axisDefinitions.count());
  209. return sk_sp<SkTypeface>(new SkTypeface_Stream(std::move(data), style, isFixedPitch, false, name));
  210. }
  211. sk_sp<SkTypeface> SkFontMgr_Custom::onMakeFromFontData(std::unique_ptr<SkFontData> data) const {
  212. bool isFixedPitch;
  213. SkFontStyle style;
  214. SkString name;
  215. if (!fScanner.scanFont(data->getStream(), data->getIndex(),
  216. &name, &style, &isFixedPitch, nullptr)) {
  217. return nullptr;
  218. }
  219. return sk_sp<SkTypeface>(new SkTypeface_Stream(std::move(data), style, isFixedPitch, false, name));
  220. }
  221. sk_sp<SkTypeface> SkFontMgr_Custom::onMakeFromFile(const char path[], int ttcIndex) const {
  222. std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path);
  223. return stream ? this->makeFromStream(std::move(stream), ttcIndex) : nullptr;
  224. }
  225. sk_sp<SkTypeface> SkFontMgr_Custom::onLegacyMakeTypeface(const char familyName[],
  226. SkFontStyle style) const {
  227. sk_sp<SkTypeface> tf;
  228. if (familyName) {
  229. tf.reset(this->onMatchFamilyStyle(familyName, style));
  230. }
  231. if (nullptr == tf) {
  232. tf.reset(fDefaultFamily->matchStyle(style));
  233. }
  234. return tf;
  235. }