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/SkTDArray.h"
13
14class SkCharToGlyphCache {
15public:
16    SkCharToGlyphCache();
17    ~SkCharToGlyphCache();
18
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
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
56private:
57    SkTDArray<int32_t>   fK32;
58    SkTDArray<uint16_t>  fV16;
59    double               fDenom;
60};
61
62#endif
63