TestTypeface.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/SkBitmap.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkFontMetrics.h"
  10. #include "include/core/SkImageInfo.h"
  11. #include "include/core/SkMatrix.h"
  12. #include "include/core/SkPath.h"
  13. #include "include/core/SkPoint.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkString.h"
  16. #include "include/private/SkTDArray.h"
  17. #include "include/private/SkTo.h"
  18. #include "src/core/SkAdvancedTypefaceMetrics.h"
  19. #include "src/core/SkFontDescriptor.h"
  20. #include "src/core/SkFontPriv.h"
  21. #include "src/core/SkGlyph.h"
  22. #include "src/core/SkPaintPriv.h"
  23. #include "src/core/SkScalerContext.h"
  24. #include "src/core/SkUtils.h"
  25. #include "src/sfnt/SkOTUtils.h"
  26. #include "tools/fonts/TestTypeface.h"
  27. #include <utility>
  28. class SkDescriptor;
  29. SkTestFont::SkTestFont(const SkTestFontData& fontData)
  30. : INHERITED()
  31. , fCharCodes(fontData.fCharCodes)
  32. , fCharCodesCount(fontData.fCharCodes ? fontData.fCharCodesCount : 0)
  33. , fWidths(fontData.fWidths)
  34. , fMetrics(fontData.fMetrics)
  35. , fName(fontData.fName)
  36. , fPaths(nullptr) {
  37. init(fontData.fPoints, fontData.fVerbs);
  38. }
  39. SkTestFont::~SkTestFont() {
  40. for (unsigned index = 0; index < fCharCodesCount; ++index) {
  41. delete fPaths[index];
  42. }
  43. delete[] fPaths;
  44. }
  45. SkGlyphID SkTestFont::glyphForUnichar(SkUnichar charCode) const {
  46. for (size_t index = 0; index < fCharCodesCount; ++index) {
  47. if (fCharCodes[index] == charCode) {
  48. return SkTo<SkGlyphID>(index);
  49. }
  50. }
  51. return 0;
  52. }
  53. void SkTestFont::init(const SkScalar* pts, const unsigned char* verbs) {
  54. fPaths = new SkPath*[fCharCodesCount];
  55. for (unsigned index = 0; index < fCharCodesCount; ++index) {
  56. SkPath* path = new SkPath;
  57. SkPath::Verb verb;
  58. while ((verb = (SkPath::Verb)*verbs++) != SkPath::kDone_Verb) {
  59. switch (verb) {
  60. case SkPath::kMove_Verb:
  61. path->moveTo(pts[0], pts[1]);
  62. pts += 2;
  63. break;
  64. case SkPath::kLine_Verb:
  65. path->lineTo(pts[0], pts[1]);
  66. pts += 2;
  67. break;
  68. case SkPath::kQuad_Verb:
  69. path->quadTo(pts[0], pts[1], pts[2], pts[3]);
  70. pts += 4;
  71. break;
  72. case SkPath::kCubic_Verb:
  73. path->cubicTo(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]);
  74. pts += 6;
  75. break;
  76. case SkPath::kClose_Verb: path->close(); break;
  77. default: SkDEBUGFAIL("bad verb"); return;
  78. }
  79. }
  80. // This should make SkPath::getBounds() queries threadsafe.
  81. path->updateBoundsCache();
  82. fPaths[index] = path;
  83. }
  84. }
  85. TestTypeface::TestTypeface(sk_sp<SkTestFont> testFont, const SkFontStyle& style)
  86. : SkTypeface(style, false), fTestFont(std::move(testFont)) {}
  87. void TestTypeface::getAdvance(SkGlyph* glyph) {
  88. SkGlyphID glyphID = glyph->getGlyphID();
  89. glyphID = glyphID < fTestFont->fCharCodesCount ? glyphID : 0;
  90. // TODO(benjaminwagner): Update users to use floats.
  91. glyph->fAdvanceX = SkFixedToFloat(fTestFont->fWidths[glyphID]);
  92. glyph->fAdvanceY = 0;
  93. }
  94. void TestTypeface::getFontMetrics(SkFontMetrics* metrics) { *metrics = fTestFont->fMetrics; }
  95. void TestTypeface::getPath(SkGlyphID glyphID, SkPath* path) {
  96. glyphID = glyphID < fTestFont->fCharCodesCount ? glyphID : 0;
  97. *path = *fTestFont->fPaths[glyphID];
  98. }
  99. void TestTypeface::onFilterRec(SkScalerContextRec* rec) const {
  100. rec->setHinting(SkFontHinting::kNone);
  101. }
  102. void TestTypeface::getGlyphToUnicodeMap(SkUnichar* glyphToUnicode) const {
  103. unsigned glyphCount = fTestFont->fCharCodesCount;
  104. for (unsigned gid = 0; gid < glyphCount; ++gid) {
  105. glyphToUnicode[gid] = SkTo<SkUnichar>(fTestFont->fCharCodes[gid]);
  106. }
  107. }
  108. std::unique_ptr<SkAdvancedTypefaceMetrics> TestTypeface::onGetAdvancedMetrics() const { // pdf only
  109. std::unique_ptr<SkAdvancedTypefaceMetrics>info(new SkAdvancedTypefaceMetrics);
  110. info->fFontName.set(fTestFont->fName);
  111. return info;
  112. }
  113. void TestTypeface::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
  114. desc->setFamilyName(fTestFont->fName);
  115. desc->setStyle(this->fontStyle());
  116. *isLocal = false;
  117. }
  118. void TestTypeface::onCharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const {
  119. for (int i = 0; i < count; ++i) {
  120. glyphs[i] = fTestFont->glyphForUnichar(uni[i]);
  121. }
  122. }
  123. void TestTypeface::onGetFamilyName(SkString* familyName) const { *familyName = fTestFont->fName; }
  124. SkTypeface::LocalizedStrings* TestTypeface::onCreateFamilyNameIterator() const {
  125. SkString familyName(fTestFont->fName);
  126. SkString language("und"); // undetermined
  127. return new SkOTUtils::LocalizedStrings_SingleName(familyName, language);
  128. }
  129. class SkTestScalerContext : public SkScalerContext {
  130. public:
  131. SkTestScalerContext(sk_sp<TestTypeface> face,
  132. const SkScalerContextEffects& effects,
  133. const SkDescriptor* desc)
  134. : SkScalerContext(std::move(face), effects, desc) {
  135. fRec.getSingleMatrix(&fMatrix);
  136. this->forceGenerateImageFromPath();
  137. }
  138. protected:
  139. TestTypeface* getTestTypeface() const {
  140. return static_cast<TestTypeface*>(this->getTypeface());
  141. }
  142. unsigned generateGlyphCount() override { return this->getTestTypeface()->onCountGlyphs(); }
  143. bool generateAdvance(SkGlyph* glyph) override {
  144. this->getTestTypeface()->getAdvance(glyph);
  145. const SkVector advance =
  146. fMatrix.mapXY(SkFloatToScalar(glyph->fAdvanceX), SkFloatToScalar(glyph->fAdvanceY));
  147. glyph->fAdvanceX = SkScalarToFloat(advance.fX);
  148. glyph->fAdvanceY = SkScalarToFloat(advance.fY);
  149. return true;
  150. }
  151. void generateMetrics(SkGlyph* glyph) override {
  152. glyph->zeroMetrics();
  153. this->generateAdvance(glyph);
  154. // Always generates from paths, so SkScalerContext::getMetrics will figure the bounds.
  155. }
  156. void generateImage(const SkGlyph&) override { SK_ABORT("Should have generated from path."); }
  157. bool generatePath(SkGlyphID glyph, SkPath* path) override {
  158. this->getTestTypeface()->getPath(glyph, path);
  159. path->transform(fMatrix);
  160. return true;
  161. }
  162. void generateFontMetrics(SkFontMetrics* metrics) override {
  163. this->getTestTypeface()->getFontMetrics(metrics);
  164. SkFontPriv::ScaleFontMetrics(metrics, fMatrix.getScaleY());
  165. }
  166. private:
  167. SkMatrix fMatrix;
  168. };
  169. SkScalerContext* TestTypeface::onCreateScalerContext(const SkScalerContextEffects& effects,
  170. const SkDescriptor* desc) const {
  171. return new SkTestScalerContext(sk_ref_sp(const_cast<TestTypeface*>(this)), effects, desc);
  172. }