SkFontDescriptor.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #ifndef SkFontDescriptor_DEFINED
  8. #define SkFontDescriptor_DEFINED
  9. #include "include/core/SkStream.h"
  10. #include "include/core/SkString.h"
  11. #include "include/core/SkTypeface.h"
  12. #include "include/private/SkFixed.h"
  13. #include "include/private/SkNoncopyable.h"
  14. class SkFontData {
  15. public:
  16. /** Makes a copy of the data in 'axis'. */
  17. SkFontData(std::unique_ptr<SkStreamAsset> stream, int index, const SkFixed axis[],int axisCount)
  18. : fStream(std::move(stream)), fIndex(index), fAxisCount(axisCount), fAxis(axisCount)
  19. {
  20. for (int i = 0; i < axisCount; ++i) {
  21. fAxis[i] = axis[i];
  22. }
  23. }
  24. SkFontData(const SkFontData& that)
  25. : fStream(that.fStream->duplicate())
  26. , fIndex(that.fIndex)
  27. , fAxisCount(that.fAxisCount)
  28. , fAxis(fAxisCount)
  29. {
  30. for (int i = 0; i < fAxisCount; ++i) {
  31. fAxis[i] = that.fAxis[i];
  32. }
  33. }
  34. bool hasStream() const { return fStream.get() != nullptr; }
  35. std::unique_ptr<SkStreamAsset> detachStream() { return std::move(fStream); }
  36. SkStreamAsset* getStream() { return fStream.get(); }
  37. SkStreamAsset const* getStream() const { return fStream.get(); }
  38. int getIndex() const { return fIndex; }
  39. int getAxisCount() const { return fAxisCount; }
  40. const SkFixed* getAxis() const { return fAxis.get(); }
  41. private:
  42. std::unique_ptr<SkStreamAsset> fStream;
  43. int fIndex;
  44. int fAxisCount;
  45. SkAutoSTMalloc<4, SkFixed> fAxis;
  46. };
  47. class SkFontDescriptor : SkNoncopyable {
  48. public:
  49. SkFontDescriptor();
  50. // Does not affect ownership of SkStream.
  51. static bool Deserialize(SkStream*, SkFontDescriptor* result);
  52. void serialize(SkWStream*) const;
  53. SkFontStyle getStyle() const { return fStyle; }
  54. void setStyle(SkFontStyle style) { fStyle = style; }
  55. const char* getFamilyName() const { return fFamilyName.c_str(); }
  56. const char* getFullName() const { return fFullName.c_str(); }
  57. const char* getPostscriptName() const { return fPostscriptName.c_str(); }
  58. bool hasFontData() const { return fFontData.get() != nullptr; }
  59. std::unique_ptr<SkFontData> detachFontData() { return std::move(fFontData); }
  60. void setFamilyName(const char* name) { fFamilyName.set(name); }
  61. void setFullName(const char* name) { fFullName.set(name); }
  62. void setPostscriptName(const char* name) { fPostscriptName.set(name); }
  63. /** Set the font data only if it is necessary for serialization. */
  64. void setFontData(std::unique_ptr<SkFontData> data) { fFontData = std::move(data); }
  65. private:
  66. SkString fFamilyName;
  67. SkString fFullName;
  68. SkString fPostscriptName;
  69. std::unique_ptr<SkFontData> fFontData;
  70. SkFontStyle fStyle;
  71. };
  72. #endif // SkFontDescriptor_DEFINED