SkFontMgr.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright 2015 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/SkStream.h"
  9. #include "include/core/SkTypes.h"
  10. #include "include/private/SkOnce.h"
  11. #include "src/core/SkFontDescriptor.h"
  12. class SkFontStyle;
  13. class SkTypeface;
  14. class SkEmptyFontStyleSet : public SkFontStyleSet {
  15. public:
  16. int count() override { return 0; }
  17. void getStyle(int, SkFontStyle*, SkString*) override {
  18. SkDEBUGFAIL("SkFontStyleSet::getStyle called on empty set");
  19. }
  20. SkTypeface* createTypeface(int index) override {
  21. SkDEBUGFAIL("SkFontStyleSet::createTypeface called on empty set");
  22. return nullptr;
  23. }
  24. SkTypeface* matchStyle(const SkFontStyle&) override {
  25. return nullptr;
  26. }
  27. };
  28. SkFontStyleSet* SkFontStyleSet::CreateEmpty() { return new SkEmptyFontStyleSet; }
  29. ///////////////////////////////////////////////////////////////////////////////
  30. class SkEmptyFontMgr : public SkFontMgr {
  31. protected:
  32. int onCountFamilies() const override {
  33. return 0;
  34. }
  35. void onGetFamilyName(int index, SkString* familyName) const override {
  36. SkDEBUGFAIL("onGetFamilyName called with bad index");
  37. }
  38. SkFontStyleSet* onCreateStyleSet(int index) const override {
  39. SkDEBUGFAIL("onCreateStyleSet called with bad index");
  40. return nullptr;
  41. }
  42. SkFontStyleSet* onMatchFamily(const char[]) const override {
  43. return SkFontStyleSet::CreateEmpty();
  44. }
  45. SkTypeface* onMatchFamilyStyle(const char[], const SkFontStyle&) const override {
  46. return nullptr;
  47. }
  48. SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
  49. const SkFontStyle& style,
  50. const char* bcp47[],
  51. int bcp47Count,
  52. SkUnichar character) const override {
  53. return nullptr;
  54. }
  55. SkTypeface* onMatchFaceStyle(const SkTypeface*, const SkFontStyle&) const override {
  56. return nullptr;
  57. }
  58. sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int) const override {
  59. return nullptr;
  60. }
  61. sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, int) const override {
  62. return nullptr;
  63. }
  64. sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
  65. const SkFontArguments&) const override {
  66. return nullptr;
  67. }
  68. sk_sp<SkTypeface> onMakeFromFontData(std::unique_ptr<SkFontData>) const override {
  69. return nullptr;
  70. }
  71. sk_sp<SkTypeface> onMakeFromFile(const char[], int) const override {
  72. return nullptr;
  73. }
  74. sk_sp<SkTypeface> onLegacyMakeTypeface(const char [], SkFontStyle) const override {
  75. return nullptr;
  76. }
  77. };
  78. static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) {
  79. if (nullptr == fsset) {
  80. fsset = SkFontStyleSet::CreateEmpty();
  81. }
  82. return fsset;
  83. }
  84. int SkFontMgr::countFamilies() const {
  85. return this->onCountFamilies();
  86. }
  87. void SkFontMgr::getFamilyName(int index, SkString* familyName) const {
  88. this->onGetFamilyName(index, familyName);
  89. }
  90. SkFontStyleSet* SkFontMgr::createStyleSet(int index) const {
  91. return emptyOnNull(this->onCreateStyleSet(index));
  92. }
  93. SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) const {
  94. return emptyOnNull(this->onMatchFamily(familyName));
  95. }
  96. SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[],
  97. const SkFontStyle& fs) const {
  98. return this->onMatchFamilyStyle(familyName, fs);
  99. }
  100. SkTypeface* SkFontMgr::matchFamilyStyleCharacter(const char familyName[], const SkFontStyle& style,
  101. const char* bcp47[], int bcp47Count,
  102. SkUnichar character) const {
  103. return this->onMatchFamilyStyleCharacter(familyName, style, bcp47, bcp47Count, character);
  104. }
  105. SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face,
  106. const SkFontStyle& fs) const {
  107. return this->onMatchFaceStyle(face, fs);
  108. }
  109. sk_sp<SkTypeface> SkFontMgr::makeFromData(sk_sp<SkData> data, int ttcIndex) const {
  110. if (nullptr == data) {
  111. return nullptr;
  112. }
  113. return this->onMakeFromData(std::move(data), ttcIndex);
  114. }
  115. sk_sp<SkTypeface> SkFontMgr::makeFromStream(std::unique_ptr<SkStreamAsset> stream,
  116. int ttcIndex) const {
  117. if (nullptr == stream) {
  118. return nullptr;
  119. }
  120. return this->onMakeFromStreamIndex(std::move(stream), ttcIndex);
  121. }
  122. sk_sp<SkTypeface> SkFontMgr::makeFromStream(std::unique_ptr<SkStreamAsset> stream,
  123. const SkFontArguments& args) const {
  124. if (nullptr == stream) {
  125. return nullptr;
  126. }
  127. return this->onMakeFromStreamArgs(std::move(stream), args);
  128. }
  129. sk_sp<SkTypeface> SkFontMgr::makeFromFontData(std::unique_ptr<SkFontData> data) const {
  130. if (nullptr == data) {
  131. return nullptr;
  132. }
  133. return this->onMakeFromFontData(std::move(data));
  134. }
  135. sk_sp<SkTypeface> SkFontMgr::makeFromFile(const char path[], int ttcIndex) const {
  136. if (nullptr == path) {
  137. return nullptr;
  138. }
  139. return this->onMakeFromFile(path, ttcIndex);
  140. }
  141. sk_sp<SkTypeface> SkFontMgr::legacyMakeTypeface(const char familyName[], SkFontStyle style) const {
  142. return this->onLegacyMakeTypeface(familyName, style);
  143. }
  144. sk_sp<SkTypeface> SkFontMgr::onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream,
  145. const SkFontArguments& args) const {
  146. return this->makeFromStream(std::move(stream), args.getCollectionIndex());
  147. }
  148. sk_sp<SkTypeface> SkFontMgr::onMakeFromFontData(std::unique_ptr<SkFontData> data) const {
  149. return this->makeFromStream(data->detachStream(), data->getIndex());
  150. }
  151. // A global function pointer that's not declared, but can be overriden at startup by test tools.
  152. sk_sp<SkFontMgr> (*gSkFontMgr_DefaultFactory)() = nullptr;
  153. sk_sp<SkFontMgr> SkFontMgr::RefDefault() {
  154. static SkOnce once;
  155. static sk_sp<SkFontMgr> singleton;
  156. once([]{
  157. sk_sp<SkFontMgr> fm = gSkFontMgr_DefaultFactory ? gSkFontMgr_DefaultFactory()
  158. : SkFontMgr::Factory();
  159. singleton = fm ? std::move(fm) : sk_make_sp<SkEmptyFontMgr>();
  160. });
  161. return singleton;
  162. }
  163. /**
  164. * Width has the greatest priority.
  165. * If the value of pattern.width is 5 (normal) or less,
  166. * narrower width values are checked first, then wider values.
  167. * If the value of pattern.width is greater than 5 (normal),
  168. * wider values are checked first, followed by narrower values.
  169. *
  170. * Italic/Oblique has the next highest priority.
  171. * If italic requested and there is some italic font, use it.
  172. * If oblique requested and there is some oblique font, use it.
  173. * If italic requested and there is some oblique font, use it.
  174. * If oblique requested and there is some italic font, use it.
  175. *
  176. * Exact match.
  177. * If pattern.weight < 400, weights below pattern.weight are checked
  178. * in descending order followed by weights above pattern.weight
  179. * in ascending order until a match is found.
  180. * If pattern.weight > 500, weights above pattern.weight are checked
  181. * in ascending order followed by weights below pattern.weight
  182. * in descending order until a match is found.
  183. * If pattern.weight is 400, 500 is checked first
  184. * and then the rule for pattern.weight < 400 is used.
  185. * If pattern.weight is 500, 400 is checked first
  186. * and then the rule for pattern.weight < 400 is used.
  187. */
  188. SkTypeface* SkFontStyleSet::matchStyleCSS3(const SkFontStyle& pattern) {
  189. int count = this->count();
  190. if (0 == count) {
  191. return nullptr;
  192. }
  193. struct Score {
  194. int score;
  195. int index;
  196. Score& operator +=(int rhs) { this->score += rhs; return *this; }
  197. Score& operator <<=(int rhs) { this->score <<= rhs; return *this; }
  198. bool operator <(const Score& that) { return this->score < that.score; }
  199. };
  200. Score maxScore = { 0, 0 };
  201. for (int i = 0; i < count; ++i) {
  202. SkFontStyle current;
  203. this->getStyle(i, &current, nullptr);
  204. Score currentScore = { 0, i };
  205. // CSS stretch / SkFontStyle::Width
  206. // Takes priority over everything else.
  207. if (pattern.width() <= SkFontStyle::kNormal_Width) {
  208. if (current.width() <= pattern.width()) {
  209. currentScore += 10 - pattern.width() + current.width();
  210. } else {
  211. currentScore += 10 - current.width();
  212. }
  213. } else {
  214. if (current.width() > pattern.width()) {
  215. currentScore += 10 + pattern.width() - current.width();
  216. } else {
  217. currentScore += current.width();
  218. }
  219. }
  220. currentScore <<= 8;
  221. // CSS style (normal, italic, oblique) / SkFontStyle::Slant (upright, italic, oblique)
  222. // Takes priority over all valid weights.
  223. static_assert(SkFontStyle::kUpright_Slant == 0 &&
  224. SkFontStyle::kItalic_Slant == 1 &&
  225. SkFontStyle::kOblique_Slant == 2,
  226. "SkFontStyle::Slant values not as required.");
  227. SkASSERT(0 <= pattern.slant() && pattern.slant() <= 2 &&
  228. 0 <= current.slant() && current.slant() <= 2);
  229. static const int score[3][3] = {
  230. /* Upright Italic Oblique [current]*/
  231. /* Upright */ { 3 , 1 , 2 },
  232. /* Italic */ { 1 , 3 , 2 },
  233. /* Oblique */ { 1 , 2 , 3 },
  234. /* [pattern] */
  235. };
  236. currentScore += score[pattern.slant()][current.slant()];
  237. currentScore <<= 8;
  238. // Synthetics (weight, style) [no stretch synthetic?]
  239. // CSS weight / SkFontStyle::Weight
  240. // The 'closer' to the target weight, the higher the score.
  241. // 1000 is the 'heaviest' recognized weight
  242. if (pattern.weight() == current.weight()) {
  243. currentScore += 1000;
  244. } else if (pattern.weight() <= 500) {
  245. if (400 <= pattern.weight() && pattern.weight() < 450) {
  246. if (450 <= current.weight() && current.weight() <= 500) {
  247. // Artificially boost the 500 weight.
  248. // TODO: determine correct number to use.
  249. currentScore += 500;
  250. }
  251. }
  252. if (current.weight() <= pattern.weight()) {
  253. currentScore += 1000 - pattern.weight() + current.weight();
  254. } else {
  255. currentScore += 1000 - current.weight();
  256. }
  257. } else if (pattern.weight() > 500) {
  258. if (current.weight() > pattern.weight()) {
  259. currentScore += 1000 + pattern.weight() - current.weight();
  260. } else {
  261. currentScore += current.weight();
  262. }
  263. }
  264. if (maxScore < currentScore) {
  265. maxScore = currentScore;
  266. }
  267. }
  268. return this->createTypeface(maxScore.index);
  269. }