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 8 #ifndef SkFontDescriptor_DEFINED 9 #define SkFontDescriptor_DEFINED 10 11 #include "include/core/SkStream.h" 12 #include "include/core/SkString.h" 13 #include "include/core/SkTypeface.h" 14 #include "include/private/SkFixed.h" 15 #include "include/private/SkNoncopyable.h" 16 #include "include/private/SkTemplates.h" 17 18 class SkFontData { 19 public: 20 /** Makes a copy of the data in 'axis'. */ SkFontData(std::unique_ptr<SkStreamAsset> stream,int index,const SkFixed * axis,int axisCount)21 SkFontData(std::unique_ptr<SkStreamAsset> stream, int index, const SkFixed* axis, int axisCount) 22 : fStream(std::move(stream)), fIndex(index), fAxisCount(axisCount), fAxis(axisCount) 23 { 24 for (int i = 0; i < axisCount; ++i) { 25 fAxis[i] = axis[i]; 26 } 27 } SkFontData(std::unique_ptr<SkStreamAsset> stream,SkFontArguments args)28 SkFontData(std::unique_ptr<SkStreamAsset> stream, SkFontArguments args) 29 : fStream(std::move(stream)), fIndex(args.getCollectionIndex()) 30 , fAxisCount(args.getVariationDesignPosition().coordinateCount) 31 , fAxis(args.getVariationDesignPosition().coordinateCount) 32 { 33 for (int i = 0; i < fAxisCount; ++i) { 34 fAxis[i] = SkFloatToFixed(args.getVariationDesignPosition().coordinates[i].value); 35 } 36 } SkFontData(const SkFontData & that)37 SkFontData(const SkFontData& that) 38 : fStream(that.fStream->duplicate()) 39 , fIndex(that.fIndex) 40 , fAxisCount(that.fAxisCount) 41 , fAxis(fAxisCount) 42 { 43 for (int i = 0; i < fAxisCount; ++i) { 44 fAxis[i] = that.fAxis[i]; 45 } 46 } hasStream()47 bool hasStream() const { return fStream != nullptr; } detachStream()48 std::unique_ptr<SkStreamAsset> detachStream() { return std::move(fStream); } getStream()49 SkStreamAsset* getStream() { return fStream.get(); } getStream()50 SkStreamAsset const* getStream() const { return fStream.get(); } getIndex()51 int getIndex() const { return fIndex; } getAxisCount()52 int getAxisCount() const { return fAxisCount; } getAxis()53 const SkFixed* getAxis() const { return fAxis.get(); } 54 55 private: 56 std::unique_ptr<SkStreamAsset> fStream; 57 int fIndex; 58 int fAxisCount; 59 SkAutoSTMalloc<4, SkFixed> fAxis; 60 }; 61 62 class SkFontDescriptor : SkNoncopyable { 63 public: 64 SkFontDescriptor(); 65 // Does not affect ownership of SkStream. 66 static bool Deserialize(SkStream*, SkFontDescriptor* result); 67 68 void serialize(SkWStream*) const; 69 getStyle()70 SkFontStyle getStyle() const { return fStyle; } setStyle(SkFontStyle style)71 void setStyle(SkFontStyle style) { fStyle = style; } 72 getFamilyName()73 const char* getFamilyName() const { return fFamilyName.c_str(); } getFullName()74 const char* getFullName() const { return fFullName.c_str(); } getPostscriptName()75 const char* getPostscriptName() const { return fPostscriptName.c_str(); } 76 setFamilyName(const char * name)77 void setFamilyName(const char* name) { fFamilyName.set(name); } setFullName(const char * name)78 void setFullName(const char* name) { fFullName.set(name); } setPostscriptName(const char * name)79 void setPostscriptName(const char* name) { fPostscriptName.set(name); } 80 hasStream()81 bool hasStream() const { return bool(fStream); } dupStream()82 std::unique_ptr<SkStreamAsset> dupStream() const { return fStream->duplicate(); } getCollectionIndex()83 int getCollectionIndex() const { return fCollectionIndex; } getVariationCoordinateCount()84 int getVariationCoordinateCount() const { return fCoordinateCount; } getVariation()85 const SkFontArguments::VariationPosition::Coordinate* getVariation() const { 86 return fVariation.get(); 87 } 88 detachStream()89 std::unique_ptr<SkStreamAsset> detachStream() { return std::move(fStream); } setStream(std::unique_ptr<SkStreamAsset> stream)90 void setStream(std::unique_ptr<SkStreamAsset> stream) { fStream = std::move(stream); } setCollectionIndex(int collectionIndex)91 void setCollectionIndex(int collectionIndex) { fCollectionIndex = collectionIndex; } setVariationCoordinates(int coordinateCount)92 SkFontArguments::VariationPosition::Coordinate* setVariationCoordinates(int coordinateCount) { 93 fCoordinateCount = coordinateCount; 94 return fVariation.reset(coordinateCount); 95 } 96 97 std::unique_ptr<SkFontData> maybeAsSkFontData(); 98 99 private: 100 SkString fFamilyName; 101 SkString fFullName; 102 SkString fPostscriptName; 103 SkFontStyle fStyle; 104 105 std::unique_ptr<SkStreamAsset> fStream; 106 int fCollectionIndex = 0; 107 using Coordinates = SkAutoSTMalloc<4, SkFontArguments::VariationPosition::Coordinate>; 108 bool fVariationDataIsOldAndBad = false; 109 int fCoordinateCount = 0; 110 Coordinates fVariation; 111 }; 112 113 #endif // SkFontDescriptor_DEFINED 114