SkCharToGlyphCache.h 1.7 KB

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