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