cache.dart 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import 'package:flutter/painting.dart';
  2. import 'package:quiver/collection.dart';
  3. class TextLayoutCache {
  4. TextLayoutCache(this.textDirection, int maximumSize)
  5. : _cache = LruMap<int, TextPainter>(maximumSize: maximumSize);
  6. final LruMap<int, TextPainter> _cache;
  7. final TextDirection textDirection;
  8. TextPainter? getLayoutFromCache(int key) {
  9. return _cache[key];
  10. }
  11. // TextPainter getOrPerformLayout(TextSpan text, int key) {
  12. // final cachedPainter = _cache[key];
  13. // if (cachedPainter != null) {
  14. // return cachedPainter;
  15. // } else {
  16. // return performAndCacheLayout(text, key);
  17. // }
  18. // }
  19. TextPainter performAndCacheLayout(TextSpan text, int key) {
  20. final textPainter = TextPainter(text: text, textDirection: textDirection);
  21. textPainter.layout();
  22. _cache[key] = textPainter;
  23. return textPainter;
  24. }
  25. int get length {
  26. return _cache.length;
  27. }
  28. }
  29. final textLayoutCache = TextLayoutCache(TextDirection.ltr, 10240);
  30. // class CodePointCache {
  31. // CodePointCache(int maximumSize)
  32. // : _cache = LruMap<int, String>(maximumSize: maximumSize);
  33. // final LruMap<int, String> _cache;
  34. // String getOrConstruct(int codePoint) {
  35. // final cachedString = _cache[codePoint];
  36. // if (cachedString != null) {
  37. // return cachedString;
  38. // } else {
  39. // return _constructAndCacheString(codePoint);
  40. // }
  41. // }
  42. // String _constructAndCacheString(int codePoint) {
  43. // final string = String.fromCharCode(codePoint);
  44. // _cache[codePoint] = string;
  45. // return string;
  46. // }
  47. // }
  48. // final codePointCache = CodePointCache(1024);