blob: 6e0a47a930a69c2fe5e095e73b3f339f8709b8fc [file] [log] [blame]
Mike Reed0c607082019-04-11 17:10:17 -04001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkTypes.h"
12#include "include/private/SkTDArray.h"
Mike Reed0c607082019-04-11 17:10:17 -040013
14class SkCharToGlyphCache {
15public:
16 SkCharToGlyphCache();
17 ~SkCharToGlyphCache();
18
Mike Reedb07e9a82019-04-19 10:02:10 -040019 // 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 Reed0c607082019-04-11 17:10:17 -040026 /**
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
56private:
Mike Reed194cab02019-04-15 12:07:19 -040057 SkTDArray<int32_t> fK32;
58 SkTDArray<uint16_t> fV16;
59 double fDenom;
Mike Reed0c607082019-04-11 17:10:17 -040060};
61
62#endif