SkTypeface.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * Copyright 2011 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/SkFontMetrics.h"
  8. #include "include/core/SkFontMgr.h"
  9. #include "include/core/SkStream.h"
  10. #include "include/core/SkTypeface.h"
  11. #include "include/private/SkMutex.h"
  12. #include "include/private/SkOnce.h"
  13. #include "src/core/SkAdvancedTypefaceMetrics.h"
  14. #include "src/core/SkEndian.h"
  15. #include "src/core/SkFontDescriptor.h"
  16. #include "src/core/SkMakeUnique.h"
  17. #include "src/core/SkSurfacePriv.h"
  18. #include "src/core/SkTypefaceCache.h"
  19. #include "src/sfnt/SkOTTable_OS_2.h"
  20. SkTypeface::SkTypeface(const SkFontStyle& style, bool isFixedPitch)
  21. : fUniqueID(SkTypefaceCache::NewFontID()), fStyle(style), fIsFixedPitch(isFixedPitch) { }
  22. SkTypeface::~SkTypeface() { }
  23. #ifdef SK_WHITELIST_SERIALIZED_TYPEFACES
  24. extern void WhitelistSerializeTypeface(const SkTypeface*, SkWStream* );
  25. #define SK_TYPEFACE_DELEGATE WhitelistSerializeTypeface
  26. #else
  27. #define SK_TYPEFACE_DELEGATE nullptr
  28. #endif
  29. void (*gSerializeTypefaceDelegate)(const SkTypeface*, SkWStream* ) = SK_TYPEFACE_DELEGATE;
  30. sk_sp<SkTypeface> (*gDeserializeTypefaceDelegate)(SkStream* ) = nullptr;
  31. ///////////////////////////////////////////////////////////////////////////////
  32. namespace {
  33. class SkEmptyTypeface : public SkTypeface {
  34. public:
  35. static sk_sp<SkTypeface> Make() { return sk_sp<SkTypeface>(new SkEmptyTypeface); }
  36. protected:
  37. SkEmptyTypeface() : SkTypeface(SkFontStyle(), true) { }
  38. std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override { return nullptr; }
  39. sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override {
  40. return sk_ref_sp(this);
  41. }
  42. SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&,
  43. const SkDescriptor*) const override {
  44. return nullptr;
  45. }
  46. void onFilterRec(SkScalerContextRec*) const override { }
  47. std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override {
  48. return nullptr;
  49. }
  50. void onGetFontDescriptor(SkFontDescriptor*, bool*) const override { }
  51. void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const override {
  52. sk_bzero(glyphs, count * sizeof(glyphs[0]));
  53. }
  54. int onCountGlyphs() const override { return 0; }
  55. void getPostScriptGlyphNames(SkString*) const override {}
  56. void getGlyphToUnicodeMap(SkUnichar*) const override {}
  57. int onGetUPEM() const override { return 0; }
  58. class EmptyLocalizedStrings : public SkTypeface::LocalizedStrings {
  59. public:
  60. bool next(SkTypeface::LocalizedString*) override { return false; }
  61. };
  62. void onGetFamilyName(SkString* familyName) const override {
  63. familyName->reset();
  64. }
  65. SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override {
  66. return new EmptyLocalizedStrings;
  67. }
  68. int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
  69. int coordinateCount) const override
  70. {
  71. return 0;
  72. }
  73. int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],
  74. int parameterCount) const override
  75. {
  76. return 0;
  77. }
  78. int onGetTableTags(SkFontTableTag tags[]) const override { return 0; }
  79. size_t onGetTableData(SkFontTableTag, size_t, size_t, void*) const override {
  80. return 0;
  81. }
  82. };
  83. } // namespace
  84. SkFontStyle SkTypeface::FromOldStyle(Style oldStyle) {
  85. return SkFontStyle((oldStyle & SkTypeface::kBold) ? SkFontStyle::kBold_Weight
  86. : SkFontStyle::kNormal_Weight,
  87. SkFontStyle::kNormal_Width,
  88. (oldStyle & SkTypeface::kItalic) ? SkFontStyle::kItalic_Slant
  89. : SkFontStyle::kUpright_Slant);
  90. }
  91. SkTypeface* SkTypeface::GetDefaultTypeface(Style style) {
  92. static SkOnce once[4];
  93. static sk_sp<SkTypeface> defaults[4];
  94. SkASSERT((int)style < 4);
  95. once[style]([style] {
  96. sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
  97. auto t = fm->legacyMakeTypeface(nullptr, FromOldStyle(style));
  98. defaults[style] = t ? t : SkEmptyTypeface::Make();
  99. });
  100. return defaults[style].get();
  101. }
  102. sk_sp<SkTypeface> SkTypeface::MakeDefault() {
  103. return sk_ref_sp(GetDefaultTypeface());
  104. }
  105. uint32_t SkTypeface::UniqueID(const SkTypeface* face) {
  106. if (nullptr == face) {
  107. face = GetDefaultTypeface();
  108. }
  109. return face->uniqueID();
  110. }
  111. bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) {
  112. return facea == faceb || SkTypeface::UniqueID(facea) == SkTypeface::UniqueID(faceb);
  113. }
  114. ///////////////////////////////////////////////////////////////////////////////
  115. sk_sp<SkTypeface> SkTypeface::MakeFromName(const char name[],
  116. SkFontStyle fontStyle) {
  117. if (nullptr == name && (fontStyle.slant() == SkFontStyle::kItalic_Slant ||
  118. fontStyle.slant() == SkFontStyle::kUpright_Slant) &&
  119. (fontStyle.weight() == SkFontStyle::kBold_Weight ||
  120. fontStyle.weight() == SkFontStyle::kNormal_Weight)) {
  121. return sk_ref_sp(GetDefaultTypeface(static_cast<SkTypeface::Style>(
  122. (fontStyle.slant() == SkFontStyle::kItalic_Slant ? SkTypeface::kItalic :
  123. SkTypeface::kNormal) |
  124. (fontStyle.weight() == SkFontStyle::kBold_Weight ? SkTypeface::kBold :
  125. SkTypeface::kNormal))));
  126. }
  127. return SkFontMgr::RefDefault()->legacyMakeTypeface(name, fontStyle);
  128. }
  129. sk_sp<SkTypeface> SkTypeface::MakeFromStream(std::unique_ptr<SkStreamAsset> stream, int index) {
  130. if (!stream) {
  131. return nullptr;
  132. }
  133. return SkFontMgr::RefDefault()->makeFromStream(std::move(stream), index);
  134. }
  135. sk_sp<SkTypeface> SkTypeface::MakeFromData(sk_sp<SkData> data, int index) {
  136. if (!data) {
  137. return nullptr;
  138. }
  139. return SkFontMgr::RefDefault()->makeFromData(std::move(data), index);
  140. }
  141. sk_sp<SkTypeface> SkTypeface::MakeFromFontData(std::unique_ptr<SkFontData> data) {
  142. return SkFontMgr::RefDefault()->makeFromFontData(std::move(data));
  143. }
  144. sk_sp<SkTypeface> SkTypeface::MakeFromFile(const char path[], int index) {
  145. return SkFontMgr::RefDefault()->makeFromFile(path, index);
  146. }
  147. sk_sp<SkTypeface> SkTypeface::makeClone(const SkFontArguments& args) const {
  148. return this->onMakeClone(args);
  149. }
  150. ///////////////////////////////////////////////////////////////////////////////
  151. void SkTypeface::serialize(SkWStream* wstream, SerializeBehavior behavior) const {
  152. if (gSerializeTypefaceDelegate) {
  153. (*gSerializeTypefaceDelegate)(this, wstream);
  154. return;
  155. }
  156. bool isLocalData = false;
  157. SkFontDescriptor desc;
  158. this->onGetFontDescriptor(&desc, &isLocalData);
  159. bool shouldSerializeData = false;
  160. switch (behavior) {
  161. case SerializeBehavior::kDoIncludeData: shouldSerializeData = true; break;
  162. case SerializeBehavior::kDontIncludeData: shouldSerializeData = false; break;
  163. case SerializeBehavior::kIncludeDataIfLocal: shouldSerializeData = isLocalData; break;
  164. }
  165. // TODO: why do we check hasFontData() and allow the data to pass through even if the caller
  166. // has said they don't want the fontdata? Does this actually happen (getDescriptor returns
  167. // fontdata as well?)
  168. if (shouldSerializeData && !desc.hasFontData()) {
  169. desc.setFontData(this->onMakeFontData());
  170. }
  171. desc.serialize(wstream);
  172. }
  173. sk_sp<SkData> SkTypeface::serialize(SerializeBehavior behavior) const {
  174. SkDynamicMemoryWStream stream;
  175. this->serialize(&stream, behavior);
  176. return stream.detachAsData();
  177. }
  178. sk_sp<SkTypeface> SkTypeface::MakeDeserialize(SkStream* stream) {
  179. if (gDeserializeTypefaceDelegate) {
  180. return (*gDeserializeTypefaceDelegate)(stream);
  181. }
  182. SkFontDescriptor desc;
  183. if (!SkFontDescriptor::Deserialize(stream, &desc)) {
  184. return nullptr;
  185. }
  186. std::unique_ptr<SkFontData> data = desc.detachFontData();
  187. if (data) {
  188. sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData(std::move(data)));
  189. if (typeface) {
  190. return typeface;
  191. }
  192. }
  193. return SkTypeface::MakeFromName(desc.getFamilyName(), desc.getStyle());
  194. }
  195. ///////////////////////////////////////////////////////////////////////////////
  196. int SkTypeface::getVariationDesignPosition(
  197. SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const
  198. {
  199. return this->onGetVariationDesignPosition(coordinates, coordinateCount);
  200. }
  201. int SkTypeface::getVariationDesignParameters(
  202. SkFontParameters::Variation::Axis parameters[], int parameterCount) const
  203. {
  204. return this->onGetVariationDesignParameters(parameters, parameterCount);
  205. }
  206. int SkTypeface::countTables() const {
  207. return this->onGetTableTags(nullptr);
  208. }
  209. int SkTypeface::getTableTags(SkFontTableTag tags[]) const {
  210. return this->onGetTableTags(tags);
  211. }
  212. size_t SkTypeface::getTableSize(SkFontTableTag tag) const {
  213. return this->onGetTableData(tag, 0, ~0U, nullptr);
  214. }
  215. size_t SkTypeface::getTableData(SkFontTableTag tag, size_t offset, size_t length,
  216. void* data) const {
  217. return this->onGetTableData(tag, offset, length, data);
  218. }
  219. sk_sp<SkData> SkTypeface::copyTableData(SkFontTableTag tag) const {
  220. return this->onCopyTableData(tag);
  221. }
  222. sk_sp<SkData> SkTypeface::onCopyTableData(SkFontTableTag tag) const {
  223. size_t size = this->getTableSize(tag);
  224. if (size) {
  225. sk_sp<SkData> data = SkData::MakeUninitialized(size);
  226. (void)this->getTableData(tag, 0, size, data->writable_data());
  227. return data;
  228. }
  229. return nullptr;
  230. }
  231. std::unique_ptr<SkStreamAsset> SkTypeface::openStream(int* ttcIndex) const {
  232. int ttcIndexStorage;
  233. if (nullptr == ttcIndex) {
  234. // So our subclasses don't need to check for null param
  235. ttcIndex = &ttcIndexStorage;
  236. }
  237. return this->onOpenStream(ttcIndex);
  238. }
  239. std::unique_ptr<SkFontData> SkTypeface::makeFontData() const {
  240. return this->onMakeFontData();
  241. }
  242. // This implementation is temporary until this method can be made pure virtual.
  243. std::unique_ptr<SkFontData> SkTypeface::onMakeFontData() const {
  244. int index;
  245. std::unique_ptr<SkStreamAsset> stream(this->onOpenStream(&index));
  246. if (!stream) {
  247. return nullptr;
  248. }
  249. return skstd::make_unique<SkFontData>(std::move(stream), index, nullptr, 0);
  250. };
  251. void SkTypeface::unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const {
  252. if (count > 0 && glyphs && uni) {
  253. this->onCharsToGlyphs(uni, count, glyphs);
  254. }
  255. }
  256. SkGlyphID SkTypeface::unicharToGlyph(SkUnichar uni) const {
  257. SkGlyphID glyphs[1] = { 0 };
  258. this->onCharsToGlyphs(&uni, 1, glyphs);
  259. return glyphs[0];
  260. }
  261. int SkTypeface::countGlyphs() const {
  262. return this->onCountGlyphs();
  263. }
  264. int SkTypeface::getUnitsPerEm() const {
  265. // should we try to cache this in the base-class?
  266. return this->onGetUPEM();
  267. }
  268. bool SkTypeface::getKerningPairAdjustments(const uint16_t glyphs[], int count,
  269. int32_t adjustments[]) const {
  270. SkASSERT(count >= 0);
  271. // check for the only legal way to pass a nullptr.. everything is 0
  272. // in which case they just want to know if this face can possibly support
  273. // kerning (true) or never (false).
  274. if (nullptr == glyphs || nullptr == adjustments) {
  275. SkASSERT(nullptr == glyphs);
  276. SkASSERT(0 == count);
  277. SkASSERT(nullptr == adjustments);
  278. }
  279. return this->onGetKerningPairAdjustments(glyphs, count, adjustments);
  280. }
  281. SkTypeface::LocalizedStrings* SkTypeface::createFamilyNameIterator() const {
  282. return this->onCreateFamilyNameIterator();
  283. }
  284. void SkTypeface::getFamilyName(SkString* name) const {
  285. SkASSERT(name);
  286. this->onGetFamilyName(name);
  287. }
  288. void SkTypeface::getGlyphToUnicodeMap(SkUnichar* dst) const {
  289. sk_bzero(dst, sizeof(SkUnichar) * this->countGlyphs());
  290. }
  291. std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface::getAdvancedMetrics() const {
  292. std::unique_ptr<SkAdvancedTypefaceMetrics> result = this->onGetAdvancedMetrics();
  293. if (result && result->fPostScriptName.isEmpty()) {
  294. result->fPostScriptName = result->fFontName;
  295. }
  296. if (result && result->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
  297. SkOTTableOS2::Version::V2::Type::Field fsType;
  298. constexpr SkFontTableTag os2Tag = SkTEndian_SwapBE32(SkOTTableOS2::TAG);
  299. constexpr size_t fsTypeOffset = offsetof(SkOTTableOS2::Version::V2, fsType);
  300. if (this->getTableData(os2Tag, fsTypeOffset, sizeof(fsType), &fsType) == sizeof(fsType)) {
  301. if (fsType.Bitmap || (fsType.Restricted && !(fsType.PreviewPrint || fsType.Editable))) {
  302. result->fFlags |= SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag;
  303. }
  304. if (fsType.NoSubsetting) {
  305. result->fFlags |= SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag;
  306. }
  307. }
  308. }
  309. return result;
  310. }
  311. bool SkTypeface::onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
  312. int32_t adjustments[]) const {
  313. return false;
  314. }
  315. ///////////////////////////////////////////////////////////////////////////////
  316. #include "include/core/SkPaint.h"
  317. #include "src/core/SkDescriptor.h"
  318. SkRect SkTypeface::getBounds() const {
  319. fBoundsOnce([this] {
  320. if (!this->onComputeBounds(&fBounds)) {
  321. fBounds.setEmpty();
  322. }
  323. });
  324. return fBounds;
  325. }
  326. bool SkTypeface::onComputeBounds(SkRect* bounds) const {
  327. // we use a big size to ensure lots of significant bits from the scalercontext.
  328. // then we scale back down to return our final answer (at 1-pt)
  329. const SkScalar textSize = 2048;
  330. const SkScalar invTextSize = 1 / textSize;
  331. SkFont font;
  332. font.setTypeface(sk_ref_sp(const_cast<SkTypeface*>(this)));
  333. font.setSize(textSize);
  334. font.setLinearMetrics(true);
  335. SkScalerContextRec rec;
  336. SkScalerContextEffects effects;
  337. SkScalerContext::MakeRecAndEffectsFromFont(font, &rec, &effects);
  338. SkAutoDescriptor ad;
  339. SkScalerContextEffects noeffects;
  340. SkScalerContext::AutoDescriptorGivenRecAndEffects(rec, noeffects, &ad);
  341. std::unique_ptr<SkScalerContext> ctx = this->createScalerContext(noeffects, ad.getDesc(), true);
  342. if (!ctx) {
  343. return false;
  344. }
  345. SkFontMetrics fm;
  346. ctx->getFontMetrics(&fm);
  347. bounds->set(fm.fXMin * invTextSize, fm.fTop * invTextSize,
  348. fm.fXMax * invTextSize, fm.fBottom * invTextSize);
  349. return true;
  350. }
  351. std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface::onGetAdvancedMetrics() const {
  352. SkDEBUGFAIL("Typefaces that need to work with PDF backend must override this.");
  353. return nullptr;
  354. }