Răsfoiți Sursa

replace TextPainter with Paragraph to reduce method calls

xuty 4 ani în urmă
părinte
comite
24abb0de78
2 a modificat fișierele cu 21 adăugiri și 61 ștergeri
  1. 15 46
      lib/frontend/cache.dart
  2. 6 15
      lib/frontend/terminal_view.dart

+ 15 - 46
lib/frontend/cache.dart

@@ -1,37 +1,33 @@
+import 'dart:ui';
+
 import 'package:flutter/painting.dart';
 import 'package:quiver/collection.dart';
 
 class TextLayoutCache {
   TextLayoutCache(this.textDirection, int maximumSize)
-      : _cache = LruMap<int, TextPainter>(maximumSize: maximumSize);
+      : _cache = LruMap<int, Paragraph>(maximumSize: maximumSize);
 
-  final LruMap<int, TextPainter> _cache;
+  final LruMap<int, Paragraph> _cache;
   final TextDirection textDirection;
 
   void clear() {
     _cache.clear();
   }
-
-  TextPainter? getLayoutFromCache(int key) {
+  
+  Paragraph? getLayoutFromCache(int key) {
     return _cache[key];
   }
 
-  // TextPainter getOrPerformLayout(TextSpan text, int key) {
-  //   final cachedPainter = _cache[key];
-  //   if (cachedPainter != null) {
-  //     return cachedPainter;
-  //   } else {
-  //     return performAndCacheLayout(text, key);
-  //   }
-  // }
-
-  TextPainter performAndCacheLayout(TextSpan text, int key) {
-    final textPainter = TextPainter(text: text, textDirection: textDirection);
-    textPainter.layout();
+  Paragraph performAndCacheLayout(String text, TextStyle style, int key) {
+    final builder = ParagraphBuilder(style.getParagraphStyle());
+    builder.pushStyle(style.getTextStyle());
+    builder.addText(text);
 
-    _cache[key] = textPainter;
+    final paragraph = builder.build();
+    paragraph.layout(ParagraphConstraints(width: double.infinity));
 
-    return textPainter;
+    _cache[key] = paragraph;
+    return paragraph;
   }
 
   int get length {
@@ -39,31 +35,4 @@ class TextLayoutCache {
   }
 }
 
-final textLayoutCache = TextLayoutCache(TextDirection.ltr, 10240);
-double textLayoutCacheFontSize = 0;
-
-// class CodePointCache {
-//   CodePointCache(int maximumSize)
-//       : _cache = LruMap<int, String>(maximumSize: maximumSize);
-
-//   final LruMap<int, String> _cache;
-
-//   String getOrConstruct(int codePoint) {
-//     final cachedString = _cache[codePoint];
-//     if (cachedString != null) {
-//       return cachedString;
-//     } else {
-//       return _constructAndCacheString(codePoint);
-//     }
-//   }
-
-//   String _constructAndCacheString(int codePoint) {
-//     final string = String.fromCharCode(codePoint);
-
-//     _cache[codePoint] = string;
-
-//     return string;
-//   }
-// }
-
-// final codePointCache = CodePointCache(1024);
+final textLayoutCache = TextLayoutCache(TextDirection.ltr, 10240);

+ 6 - 15
lib/frontend/terminal_view.dart

@@ -501,15 +501,11 @@ class TerminalPainter extends CustomPainter {
 
     // final cellHash = line.cellGetHash(cell);
     final fontSize = view.style.fontSize;
-    if (textLayoutCacheFontSize != fontSize) {
-      textLayoutCache.clear();
-      textLayoutCacheFontSize = fontSize;
-    }
     final cellHash = hashValues(codePoint, fgColor, bgColor, flags);
 
-    var tp = textLayoutCache.getLayoutFromCache(cellHash);
-    if (tp != null) {
-      tp.paint(canvas, Offset(offsetX, offsetY));
+    var character = textLayoutCache.getLayoutFromCache(cellHash);
+    if (character != null) {
+      canvas.drawParagraph(character, Offset(offsetX, offsetY));
       return;
     }
 
@@ -551,16 +547,11 @@ class TerminalPainter extends CustomPainter {
             fontFamilyFallback: view.style.fontFamily,
           );
 
-    final span = TextSpan(
-      text: String.fromCharCode(codePoint),
-      // text: codePointCache.getOrConstruct(cell.codePoint),
-      style: style,
-    );
-
     // final tp = textLayoutCache.getOrPerformLayout(span);
-    tp = textLayoutCache.performAndCacheLayout(span, cellHash);
+    character = textLayoutCache.performAndCacheLayout(
+        String.fromCharCode(codePoint), style, cellHash);
 
-    tp.paint(canvas, Offset(offsetX, offsetY));
+    canvas.drawParagraph(character, Offset(offsetX, offsetY));
   }
 
   void _paintCursor(Canvas canvas) {