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