| 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/SkFontArguments.h" |
| 12 |
#include "include/core/SkFontStyle.h" |
| 13 |
#include "include/core/SkScalar.h" |
| 14 |
#include "include/core/SkStream.h" |
| 15 |
#include "include/core/SkString.h" |
| 16 |
#include "include/core/SkTypeface.h" |
| 17 |
#include "include/private/base/SkFixed.h" |
| 18 |
#include "include/private/base/SkNoncopyable.h" |
| 19 |
#include "include/private/base/SkTemplates.h" |
| 20 |
|
| 21 |
#include <memory> |
| 22 |
#include <utility> |
| 23 |
|
| 24 |
class SkFontData { |
| 25 |
public: |
| 26 |
/** Makes a copy of the data in 'axis'. */ |
| 27 |
SkFontData(std::unique_ptr<SkStreamAsset> stream, int index, int paletteIndex, |
| 28 |
const SkFixed* axis, int axisCount, |
| 29 |
const SkFontArguments::Palette::Override* paletteOverrides, int paletteOverrideCount) |
| 30 |
: fStream(std::move(stream)) |
| 31 |
, fIndex(index) |
| 32 |
, fPaletteIndex(paletteIndex) |
| 33 |
, fAxisCount(axisCount) |
| 34 |
, fPaletteOverrideCount(paletteOverrideCount) |
| 35 |
, fAxis(fAxisCount) |
| 36 |
, fPaletteOverrides(fPaletteOverrideCount) |
| 37 |
{ |
| 38 |
for (int i = 0; i < fAxisCount; ++i) { |
| 39 |
fAxis[i] = axis[i]; |
| 40 |
} |
| 41 |
for (int i = 0; i < fPaletteOverrideCount; ++i) { |
| 42 |
fPaletteOverrides[i] = paletteOverrides[i]; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
SkFontData(const SkFontData& that) |
| 47 |
: fStream(that.fStream->duplicate()) |
| 48 |
, fIndex(that.fIndex) |
| 49 |
, fPaletteIndex(that.fPaletteIndex) |
| 50 |
, fAxisCount(that.fAxisCount) |
| 51 |
, fPaletteOverrideCount(that.fPaletteOverrideCount) |
| 52 |
, fAxis(fAxisCount) |
| 53 |
, fPaletteOverrides(fPaletteOverrideCount) |
| 54 |
{ |
| 55 |
for (int i = 0; i < fAxisCount; ++i) { |
| 56 |
fAxis[i] = that.fAxis[i]; |
| 57 |
} |
| 58 |
for (int i = 0; i < fPaletteOverrideCount; ++i) { |
| 59 |
fPaletteOverrides[i] = that.fPaletteOverrides[i]; |
| 60 |
} |
| 61 |
} |
| 62 |
bool hasStream() const { return fStream != nullptr; } |
| 63 |
std::unique_ptr<SkStreamAsset> detachStream() { return std::move(fStream); } |
| 64 |
SkStreamAsset* getStream() { return fStream.get(); } |
| 65 |
SkStreamAsset const* getStream() const { return fStream.get(); } |
| 66 |
int getIndex() const { return fIndex; } |
| 67 |
int getAxisCount() const { return fAxisCount; } |
| 68 |
const SkFixed* getAxis() const { return fAxis.get(); } |
| 69 |
int getPaletteIndex() const { return fPaletteIndex; } |
| 70 |
int getPaletteOverrideCount() const { return fPaletteOverrideCount; } |
| 71 |
const SkFontArguments::Palette::Override* getPaletteOverrides() const { |
| 72 |
return fPaletteOverrides.get(); |
| 73 |
} |
| 74 |
|
| 75 |
private: |
| 76 |
std::unique_ptr<SkStreamAsset> fStream; |
| 77 |
int fIndex; |
| 78 |
int fPaletteIndex; |
| 79 |
int fAxisCount; |
| 80 |
int fPaletteOverrideCount; |
| 81 |
skia_private::AutoSTMalloc<4, SkFixed> fAxis; |
| 82 |
skia_private::AutoSTMalloc<4, SkFontArguments::Palette::Override> fPaletteOverrides; |
| 83 |
}; |
| 84 |
|
| 85 |
class SkFontDescriptor : SkNoncopyable { |
| 86 |
public: |
| 87 |
SkFontDescriptor(); |
| 88 |
// Does not affect ownership of SkStream. |
| 89 |
static bool Deserialize(SkStream*, SkFontDescriptor* result); |
| 90 |
|
| 91 |
bool serialize(SkWStream*) const; |
| 92 |
|
| 93 |
SkFontStyle getStyle() const { return fStyle; } |
| 94 |
void setStyle(SkFontStyle style) { fStyle = style; } |
| 95 |
|
| 96 |
const char* getFamilyName() const { return fFamilyName.c_str(); } |
| 97 |
const char* getFullName() const { return fFullName.c_str(); } |
| 98 |
const char* getPostscriptName() const { return fPostscriptName.c_str(); } |
| 99 |
|
| 100 |
void setFamilyName(const char* name) { fFamilyName.set(name); } |
| 101 |
void setFullName(const char* name) { fFullName.set(name); } |
| 102 |
void setPostscriptName(const char* name) { fPostscriptName.set(name); } |
| 103 |
|
| 104 |
bool hasStream() const { return bool(fStream); } |
| 105 |
std::unique_ptr<SkStreamAsset> dupStream() const { return fStream->duplicate(); } |
| 106 |
int getCollectionIndex() const { return fCollectionIndex; } |
| 107 |
int getPaletteIndex() const { return fPaletteIndex; } |
| 108 |
int getVariationCoordinateCount() const { return fCoordinateCount; } |
| 109 |
const SkFontArguments::VariationPosition::Coordinate* getVariation() const { |
| 110 |
return fVariation.get(); |
| 111 |
} |
| 112 |
int getPaletteEntryOverrideCount() const { return fPaletteEntryOverrideCount; } |
| 113 |
const SkFontArguments::Palette::Override* getPaletteEntryOverrides() const { |
| 114 |
return fPaletteEntryOverrides.get(); |
| 115 |
} |
| 116 |
bool getSyntheticBold() const { return fSyntheticBold; } |
| 117 |
bool getSyntheticOblique() const { return fSyntheticOblique; } |
| 118 |
SkTypeface::FactoryId getFactoryId() { |
| 119 |
return fFactoryId; |
| 120 |
} |
| 121 |
|
| 122 |
std::unique_ptr<SkStreamAsset> detachStream() { return std::move(fStream); } |
| 123 |
void setStream(std::unique_ptr<SkStreamAsset> stream) { fStream = std::move(stream); } |
| 124 |
void setCollectionIndex(int collectionIndex) { fCollectionIndex = collectionIndex; } |
| 125 |
void setPaletteIndex(int paletteIndex) { fPaletteIndex = paletteIndex; } |
| 126 |
SkFontArguments::VariationPosition::Coordinate* setVariationCoordinates(int coordinateCount) { |
| 127 |
fCoordinateCount = coordinateCount; |
| 128 |
return fVariation.reset(coordinateCount); |
| 129 |
} |
| 130 |
SkFontArguments::Palette::Override* setPaletteEntryOverrides(int paletteEntryOverrideCount) { |
| 131 |
fPaletteEntryOverrideCount = paletteEntryOverrideCount; |
| 132 |
return fPaletteEntryOverrides.reset(paletteEntryOverrideCount); |
| 133 |
} |
| 134 |
void setSyntheticBold(bool bold) { fSyntheticBold = bold; } |
| 135 |
void setSyntheticOblique(bool oblique) { fSyntheticOblique = oblique; } |
| 136 |
void setFactoryId(SkTypeface::FactoryId factoryId) { |
| 137 |
fFactoryId = factoryId; |
| 138 |
} |
| 139 |
|
| 140 |
SkFontArguments getFontArguments() const { |
| 141 |
return SkFontArguments() |
| 142 |
.setCollectionIndex(this->getCollectionIndex()) |
| 143 |
.setVariationDesignPosition({this->getVariation(),this->getVariationCoordinateCount()}) |
| 144 |
.setPalette({this->getPaletteIndex(), |
| 145 |
this->getPaletteEntryOverrides(), |
| 146 |
this->getPaletteEntryOverrideCount()}) |
| 147 |
.setSyntheticBold(fSyntheticBold) |
| 148 |
.setSyntheticOblique(fSyntheticOblique); |
| 149 |
} |
| 150 |
static SkFontStyle::Width SkFontStyleWidthForWidthAxisValue(SkScalar width); |
| 151 |
static SkScalar SkFontWidthAxisValueForStyleWidth(int width); |
| 152 |
|
| 153 |
private: |
| 154 |
SkString fFamilyName; |
| 155 |
SkString fFullName; |
| 156 |
SkString fPostscriptName; |
| 157 |
SkFontStyle fStyle; |
| 158 |
|
| 159 |
std::unique_ptr<SkStreamAsset> fStream; |
| 160 |
int fCollectionIndex = 0; |
| 161 |
using Coordinates = |
| 162 |
skia_private::AutoSTMalloc<4, SkFontArguments::VariationPosition::Coordinate>; |
| 163 |
int fCoordinateCount = 0; |
| 164 |
Coordinates fVariation; |
| 165 |
int fPaletteIndex = 0; |
| 166 |
int fPaletteEntryOverrideCount = 0; |
| 167 |
skia_private::AutoTMalloc<SkFontArguments::Palette::Override> fPaletteEntryOverrides; |
| 168 |
bool fSyntheticBold = false; |
| 169 |
bool fSyntheticOblique = false; |
| 170 |
SkTypeface::FactoryId fFactoryId = 0; |
| 171 |
}; |
| 172 |
|
| 173 |
#endif // SkFontDescriptor_DEFINED |