SkTestScalerContext.h revision 97043299cb352f50f604878b7a1dc8ad127fc1c6
1/* 2 * Copyright 2014 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 SkTestScalerContext_DEFINED 9#define SkTestScalerContext_DEFINED 10 11#include "SkFixed.h" 12#include "SkPaint.h" 13#include "SkPath.h" 14#include "SkRefCnt.h" 15#include "SkTDArray.h" 16#include "SkTypeface.h" 17 18class SkTestFont; 19 20struct SkTestFontData { 21 const SkScalar* fPoints; 22 const unsigned char* fVerbs; 23 const unsigned* fCharCodes; 24 const size_t fCharCodesCount; 25 const SkFixed* fWidths; 26 const SkPaint::FontMetrics& fMetrics; 27 const char* fName; 28 SkTypeface::Style fStyle; 29 sk_sp<SkTestFont> fCachedFont; 30}; 31 32class SkTestFont : public SkRefCnt { 33public: 34 SkTestFont(const SkTestFontData& ); 35 virtual ~SkTestFont(); 36 int codeToIndex(SkUnichar charCode) const; 37 void init(const SkScalar* pts, const unsigned char* verbs); 38#ifdef SK_DEBUG // detect missing test font data 39 mutable unsigned char fDebugBits[16]; 40 mutable SkUnichar fDebugOverage[8]; 41 const char* fDebugName; 42 SkFontStyle fDebugStyle; 43 const char* debugFontName() const { return fName; } 44#endif 45private: 46 const unsigned* fCharCodes; 47 const size_t fCharCodesCount; 48 const SkFixed* fWidths; 49 const SkPaint::FontMetrics& fMetrics; 50 const char* fName; 51 SkPath** fPaths; 52 friend class SkTestTypeface; 53 typedef SkRefCnt INHERITED; 54}; 55 56 57class SkTestTypeface : public SkTypeface { 58public: 59 SkTestTypeface(sk_sp<SkTestFont>, const SkFontStyle& style); 60 void getAdvance(SkGlyph* glyph); 61 void getFontMetrics(SkPaint::FontMetrics* metrics); 62 void getMetrics(SkGlyph* glyph); 63 void getPath(SkGlyphID glyph, SkPath* path); 64protected: 65 SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&, 66 const SkDescriptor* desc) const override; 67 void onFilterRec(SkScalerContextRec* rec) const override; 68 SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics( 69 PerGlyphInfo, 70 const uint32_t* glyphIDs, 71 uint32_t glyphIDsCount) const override; 72 73 SkStreamAsset* onOpenStream(int* ttcIndex) const override { 74 return nullptr; 75 } 76 77 void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override; 78 79 int onCharsToGlyphs(const void* chars, Encoding encoding, 80 uint16_t glyphs[], int glyphCount) const override; 81 82 int onCountGlyphs() const override { 83 return (int) fTestFont->fCharCodesCount; 84 } 85 86 int onGetUPEM() const override { 87 SkASSERT(0); // don't expect to get here 88 return 1; 89 } 90 91 void onGetFamilyName(SkString* familyName) const override; 92 SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; 93 94 int onGetTableTags(SkFontTableTag tags[]) const override { 95 return 0; 96 } 97 98 size_t onGetTableData(SkFontTableTag tag, size_t offset, 99 size_t length, void* data) const override { 100 return 0; 101 } 102private: 103 sk_sp<SkTestFont> fTestFont; 104 friend class SkTestScalerContext; 105}; 106 107SkTypeface* CreateTestTypeface(const char* name, SkTypeface::Style style); 108 109#endif 110