SkTypefaceCache.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright 2011 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 SkTypefaceCache_DEFINED
  8. #define SkTypefaceCache_DEFINED
  9. #include "include/core/SkRefCnt.h"
  10. #include "include/core/SkTypeface.h"
  11. #include "include/private/SkTArray.h"
  12. class SkTypefaceCache {
  13. public:
  14. SkTypefaceCache();
  15. /**
  16. * Callback for FindByProc. Returns true if the given typeface is a match
  17. * for the given context. The passed typeface is owned by the cache and is
  18. * not additionally ref()ed. The typeface may be in the disposed state.
  19. */
  20. typedef bool(*FindProc)(SkTypeface*, void* context);
  21. /**
  22. * Add a typeface to the cache. Later, if we need to purge the cache,
  23. * typefaces uniquely owned by the cache will be unref()ed.
  24. */
  25. void add(sk_sp<SkTypeface>);
  26. /**
  27. * Iterate through the cache, calling proc(typeface, ctx) for each typeface.
  28. * If proc returns true, then return that typeface.
  29. * If it never returns true, return nullptr.
  30. */
  31. sk_sp<SkTypeface> findByProcAndRef(FindProc proc, void* ctx) const;
  32. /**
  33. * This will unref all of the typefaces in the cache for which the cache
  34. * is the only owner. Normally this is handled automatically as needed.
  35. * This function is exposed for clients that explicitly want to purge the
  36. * cache (e.g. to look for leaks).
  37. */
  38. void purgeAll();
  39. /**
  40. * Helper: returns a unique fontID to pass to the constructor of
  41. * your subclass of SkTypeface
  42. */
  43. static SkFontID NewFontID();
  44. // These are static wrappers around a global instance of a cache.
  45. static void Add(sk_sp<SkTypeface>);
  46. static sk_sp<SkTypeface> FindByProcAndRef(FindProc proc, void* ctx);
  47. static void PurgeAll();
  48. /**
  49. * Debugging only: dumps the status of the typefaces in the cache
  50. */
  51. static void Dump();
  52. private:
  53. static SkTypefaceCache& Get();
  54. void purge(int count);
  55. SkTArray<sk_sp<SkTypeface>> fTypefaces;
  56. };
  57. #endif