cache.dart 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. void clear() {
  9. _cache.clear();
  10. }
  11. TextPainter? getLayoutFromCache(int key) {
  12. return _cache[key];
  13. }
  14. // TextPainter getOrPerformLayout(TextSpan text, int key) {
  15. // final cachedPainter = _cache[key];
  16. // if (cachedPainter != null) {
  17. // return cachedPainter;
  18. // } else {
  19. // return performAndCacheLayout(text, key);
  20. // }
  21. // }
  22. TextPainter performAndCacheLayout(TextSpan text, int key) {
  23. final textPainter = TextPainter(text: text, textDirection: textDirection);
  24. textPainter.layout();
  25. _cache[key] = textPainter;
  26. return textPainter;
  27. }
  28. int get length {
  29. return _cache.length;
  30. }
  31. }
  32. final textLayoutCache = TextLayoutCache(TextDirection.ltr, 10240);
  33. double textLayoutCacheFontSize = 0;
  34. // class CodePointCache {
  35. // CodePointCache(int maximumSize)
  36. // : _cache = LruMap<int, String>(maximumSize: maximumSize);
  37. // final LruMap<int, String> _cache;
  38. // String getOrConstruct(int codePoint) {
  39. // final cachedString = _cache[codePoint];
  40. // if (cachedString != null) {
  41. // return cachedString;
  42. // } else {
  43. // return _constructAndCacheString(codePoint);
  44. // }
  45. // }
  46. // String _constructAndCacheString(int codePoint) {
  47. // final string = String.fromCharCode(codePoint);
  48. // _cache[codePoint] = string;
  49. // return string;
  50. // }
  51. // }
  52. // final codePointCache = CodePointCache(1024);