SkFontDescriptor.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright 2012 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/SkData.h"
  8. #include "include/core/SkStream.h"
  9. #include "src/core/SkFontDescriptor.h"
  10. #include "src/core/SkMakeUnique.h"
  11. enum {
  12. kInvalid = 0x00,
  13. // these must match the sfnt 'name' enums
  14. kFontFamilyName = 0x01,
  15. kFullName = 0x04,
  16. kPostscriptName = 0x06,
  17. // These count backwards from 0xFF, so as not to collide with the SFNT
  18. // defines for names in its 'name' table.
  19. kFontAxes = 0xFB,
  20. kFontAxes_bad = 0xFC, // Broken negative axes, remove when MIN_PICTURE_VERSION > 62.
  21. kFontIndex = 0xFD,
  22. kSentinel = 0xFF,
  23. };
  24. SkFontDescriptor::SkFontDescriptor() { }
  25. static bool SK_WARN_UNUSED_RESULT read_string(SkStream* stream, SkString* string) {
  26. size_t length;
  27. if (!stream->readPackedUInt(&length)) { return false; }
  28. if (length > 0) {
  29. string->resize(length);
  30. if (stream->read(string->writable_str(), length) != length) { return false; }
  31. }
  32. return true;
  33. }
  34. static bool write_string(SkWStream* stream, const SkString& string, uint32_t id) {
  35. if (string.isEmpty()) { return true; }
  36. return stream->writePackedUInt(id) &&
  37. stream->writePackedUInt(string.size()) &&
  38. stream->write(string.c_str(), string.size());
  39. }
  40. static bool write_uint(SkWStream* stream, size_t n, uint32_t id) {
  41. return stream->writePackedUInt(id) &&
  42. stream->writePackedUInt(n);
  43. }
  44. static size_t SK_WARN_UNUSED_RESULT read_id(SkStream* stream) {
  45. size_t i;
  46. if (!stream->readPackedUInt(&i)) { return kInvalid; }
  47. return i;
  48. }
  49. bool SkFontDescriptor::Deserialize(SkStream* stream, SkFontDescriptor* result) {
  50. size_t styleBits;
  51. if (!stream->readPackedUInt(&styleBits)) { return false; }
  52. result->fStyle = SkFontStyle((styleBits >> 16) & 0xFFFF,
  53. (styleBits >> 8 ) & 0xFF,
  54. static_cast<SkFontStyle::Slant>(styleBits & 0xFF));
  55. SkAutoSTMalloc<4, SkFixed> axis;
  56. size_t axisCount = 0;
  57. size_t index = 0;
  58. for (size_t id; (id = read_id(stream)) != kSentinel;) {
  59. switch (id) {
  60. case kFontFamilyName:
  61. if (!read_string(stream, &result->fFamilyName)) { return false; }
  62. break;
  63. case kFullName:
  64. if (!read_string(stream, &result->fFullName)) { return false; }
  65. break;
  66. case kPostscriptName:
  67. if (!read_string(stream, &result->fPostscriptName)) { return false; }
  68. break;
  69. case kFontAxes:
  70. if (!stream->readPackedUInt(&axisCount)) { return false; }
  71. axis.reset(axisCount);
  72. for (size_t i = 0; i < axisCount; ++i) {
  73. if (!stream->readS32(&axis[i])) { return false; }
  74. }
  75. break;
  76. case kFontAxes_bad:
  77. if (!stream->readPackedUInt(&axisCount)) { return false; }
  78. axis.reset(axisCount);
  79. for (size_t i = 0; i < axisCount; ++i) {
  80. size_t packedAxis;
  81. if (!stream->readPackedUInt(&packedAxis)) { return false; }
  82. axis[i] = packedAxis;
  83. }
  84. break;
  85. case kFontIndex:
  86. if (!stream->readPackedUInt(&index)) { return false; }
  87. break;
  88. default:
  89. SkDEBUGFAIL("Unknown id used by a font descriptor");
  90. return false;
  91. }
  92. }
  93. size_t length;
  94. if (!stream->readPackedUInt(&length)) { return false; }
  95. if (length > 0) {
  96. sk_sp<SkData> data(SkData::MakeUninitialized(length));
  97. if (stream->read(data->writable_data(), length) != length) {
  98. SkDEBUGFAIL("Could not read font data");
  99. return false;
  100. }
  101. result->fFontData = skstd::make_unique<SkFontData>(
  102. SkMemoryStream::Make(std::move(data)), index, axis, axisCount);
  103. }
  104. return true;
  105. }
  106. void SkFontDescriptor::serialize(SkWStream* stream) const {
  107. uint32_t styleBits = (fStyle.weight() << 16) | (fStyle.width() << 8) | (fStyle.slant());
  108. stream->writePackedUInt(styleBits);
  109. write_string(stream, fFamilyName, kFontFamilyName);
  110. write_string(stream, fFullName, kFullName);
  111. write_string(stream, fPostscriptName, kPostscriptName);
  112. if (fFontData.get()) {
  113. if (fFontData->getIndex()) {
  114. write_uint(stream, fFontData->getIndex(), kFontIndex);
  115. }
  116. if (fFontData->getAxisCount()) {
  117. write_uint(stream, fFontData->getAxisCount(), kFontAxes);
  118. for (int i = 0; i < fFontData->getAxisCount(); ++i) {
  119. stream->write32(fFontData->getAxis()[i]);
  120. }
  121. }
  122. }
  123. stream->writePackedUInt(kSentinel);
  124. if (fFontData.get() && fFontData->hasStream()) {
  125. std::unique_ptr<SkStreamAsset> fontStream = fFontData->detachStream();
  126. size_t length = fontStream->getLength();
  127. stream->writePackedUInt(length);
  128. stream->writeStream(fontStream.get(), length);
  129. } else {
  130. stream->writePackedUInt(0);
  131. }
  132. }