1 /* 2 * Copyright 2019 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 SkCharToGlyphCache_DEFINED 9 #define SkCharToGlyphCache_DEFINED 10 11 #include "include/core/SkTypes.h" 12 #include "include/private/base/SkTDArray.h" 13 #include "include/private/base/SkTo.h" 14 15 #include <cstdint> 16 17 class SkCharToGlyphCache { 18 public: 19 SkCharToGlyphCache(); 20 ~SkCharToGlyphCache(); 21 22 // return number of unichars cached count()23 int count() const { 24 return fK32.size(); 25 } 26 27 void reset(); // forget all cache entries (to save memory) 28 29 /** 30 * Given a unichar, return its glyphID (if the return value is positive), else return 31 * ~index of where to insert the computed glyphID. 32 * 33 * int result = cache.charToGlyph(unichar); 34 * if (result >= 0) { 35 * glyphID = result; 36 * } else { 37 * glyphID = compute_glyph_using_typeface(unichar); 38 * cache.insertCharAndGlyph(~result, unichar, glyphID); 39 * } 40 */ 41 int findGlyphIndex(SkUnichar c) const; 42 43 /** 44 * Insert a new char/glyph pair into the cache at the specified index. 45 * See charToGlyph() for how to compute the bit-not of the index. 46 */ 47 void insertCharAndGlyph(int index, SkUnichar, SkGlyphID); 48 49 // helper to pre-seed an entry in the cache addCharAndGlyph(SkUnichar unichar,SkGlyphID glyph)50 void addCharAndGlyph(SkUnichar unichar, SkGlyphID glyph) { 51 int index = this->findGlyphIndex(unichar); 52 if (index >= 0) { 53 SkASSERT(SkToU16(index) == glyph); 54 } else { 55 this->insertCharAndGlyph(~index, unichar, glyph); 56 } 57 } 58 59 private: 60 SkTDArray<int32_t> fK32; 61 SkTDArray<uint16_t> fV16; 62 double fDenom; 63 }; 64 65 #endif 66