| 1 |
/* |
| 2 |
* Copyright 2006-2012 The Android Open Source Project |
| 3 |
* Copyright 2012 Mozilla Foundation |
| 4 |
* |
| 5 |
* Use of this source code is governed by a BSD-style license that can be |
| 6 |
* found in the LICENSE file. |
| 7 |
*/ |
| 8 |
|
| 9 |
#ifndef SkTypeface_Freetype_DEFINED |
| 10 |
#define SkTypeface_Freetype_DEFINED |
| 11 |
|
| 12 |
#include "include/core/SkFontScanner.h" |
| 13 |
#include "include/core/SkSpan.h" |
| 14 |
#include "include/core/SkTypeface.h" |
| 15 |
#include "include/core/SkTypes.h" |
| 16 |
#include "include/private/base/SkFixed.h" |
| 17 |
#include "include/private/base/SkMutex.h" |
| 18 |
#include "include/private/base/SkNoncopyable.h" |
| 19 |
#include "include/private/base/SkTArray.h" |
| 20 |
#include "src/base/SkSharedMutex.h" |
| 21 |
#include "src/utils/SkCharToGlyphCache.h" |
| 22 |
|
| 23 |
class SkFontData; |
| 24 |
|
| 25 |
// These are forward declared to avoid pimpl but also hide the FreeType implementation. |
| 26 |
typedef struct FT_LibraryRec_* FT_Library; |
| 27 |
typedef struct FT_FaceRec_* FT_Face; |
| 28 |
typedef struct FT_StreamRec_* FT_Stream; |
| 29 |
typedef signed long FT_Pos; |
| 30 |
typedef struct FT_BBox_ FT_BBox; |
| 31 |
|
| 32 |
class SkTypeface_FreeType : public SkTypeface { |
| 33 |
public: |
| 34 |
/** Fetch units/EM from "head" table if needed (ie for bitmap fonts) */ |
| 35 |
static int GetUnitsPerEm(FT_Face face); |
| 36 |
|
| 37 |
/** Return the font data, or nullptr on failure. */ |
| 38 |
std::unique_ptr<SkFontData> makeFontData() const; |
| 39 |
class FaceRec; |
| 40 |
FaceRec* getFaceRec() const; |
| 41 |
|
| 42 |
static constexpr SkTypeface::FactoryId FactoryId = SkSetFourByteTag('f','r','e','e'); |
| 43 |
static sk_sp<SkTypeface> MakeFromStream(std::unique_ptr<SkStreamAsset>, const SkFontArguments&); |
| 44 |
|
| 45 |
protected: |
| 46 |
SkTypeface_FreeType(const SkFontStyle& style, bool isFixedPitch); |
| 47 |
~SkTypeface_FreeType() override; |
| 48 |
|
| 49 |
std::unique_ptr<SkFontData> cloneFontData(const SkFontArguments&, SkFontStyle* style) const; |
| 50 |
std::unique_ptr<SkScalerContext> onCreateScalerContext( |
| 51 |
const SkScalerContextEffects&, |
| 52 |
const SkDescriptor*) const override; |
| 53 |
std::unique_ptr<SkScalerContext> onCreateScalerContextAsProxyTypeface( |
| 54 |
const SkScalerContextEffects&, |
| 55 |
const SkDescriptor*, |
| 56 |
SkTypeface* proxyTypeface) const override; |
| 57 |
void onFilterRec(SkScalerContextRec*) const override; |
| 58 |
void getGlyphToUnicodeMap(SkSpan<SkUnichar>) const override; |
| 59 |
std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override; |
| 60 |
void getPostScriptGlyphNames(SkString* dstArray) const override; |
| 61 |
bool onGetPostScriptName(SkString*) const override; |
| 62 |
int onGetUPEM() const override; |
| 63 |
bool onGetKerningPairAdjustments(SkSpan<const SkGlyphID>, |
| 64 |
SkSpan<int32_t> adjustments) const override; |
| 65 |
void onCharsToGlyphs(SkSpan<const SkUnichar>, SkSpan<SkGlyphID>) const override; |
| 66 |
int onCountGlyphs() const override; |
| 67 |
|
| 68 |
LocalizedStrings* onCreateFamilyNameIterator() const override; |
| 69 |
|
| 70 |
bool onGlyphMaskNeedsCurrentColor() const override; |
| 71 |
int onGetVariationDesignPosition( |
| 72 |
SkSpan<SkFontArguments::VariationPosition::Coordinate>) const override; |
| 73 |
int onGetVariationDesignParameters(SkSpan<SkFontParameters::Variation::Axis>) const override; |
| 74 |
int onGetTableTags(SkSpan<SkFontTableTag>) const override; |
| 75 |
size_t onGetTableData(SkFontTableTag, size_t offset, |
| 76 |
size_t length, void* data) const override; |
| 77 |
sk_sp<SkData> onCopyTableData(SkFontTableTag) const override; |
| 78 |
|
| 79 |
virtual std::unique_ptr<SkFontData> onMakeFontData() const = 0; |
| 80 |
/** Utility to fill out the SkFontDescriptor palette information from the SkFontData. */ |
| 81 |
static void FontDataPaletteToDescriptorPalette(const SkFontData&, SkFontDescriptor*); |
| 82 |
|
| 83 |
private: |
| 84 |
mutable SkOnce fFTFaceOnce; |
| 85 |
mutable std::unique_ptr<FaceRec> fFaceRec; |
| 86 |
|
| 87 |
mutable SkSharedMutex fC2GCacheMutex; |
| 88 |
mutable SkCharToGlyphCache fC2GCache; |
| 89 |
|
| 90 |
mutable SkOnce fGlyphMasksMayNeedCurrentColorOnce; |
| 91 |
mutable bool fGlyphMasksMayNeedCurrentColor; |
| 92 |
|
| 93 |
using INHERITED = SkTypeface; |
| 94 |
}; |
| 95 |
|
| 96 |
class SkTypeface_FreeTypeStream : public SkTypeface_FreeType { |
| 97 |
public: |
| 98 |
SkTypeface_FreeTypeStream(std::unique_ptr<SkFontData> fontData, const SkString familyName, |
| 99 |
const SkFontStyle& style, bool isFixedPitch); |
| 100 |
~SkTypeface_FreeTypeStream() override; |
| 101 |
|
| 102 |
protected: |
| 103 |
void onGetFamilyName(SkString* familyName) const override; |
| 104 |
void onGetFontDescriptor(SkFontDescriptor*, bool* serialize) const override; |
| 105 |
std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override; |
| 106 |
std::unique_ptr<SkFontData> onMakeFontData() const override; |
| 107 |
sk_sp<SkTypeface> onMakeClone(const SkFontArguments&) const override; |
| 108 |
|
| 109 |
private: |
| 110 |
const SkString fFamilyName; |
| 111 |
const std::unique_ptr<const SkFontData> fData; |
| 112 |
}; |
| 113 |
|
| 114 |
#endif // SkTypeface_Freetype_DEFINED |