refactor, sixel performance
authorKevin Lamonte <kevin.lamonte@gmail.com>
Mon, 5 Aug 2019 00:54:20 +0000 (19:54 -0500)
committerKevin Lamonte <kevin.lamonte@gmail.com>
Mon, 5 Aug 2019 00:54:20 +0000 (19:54 -0500)
src/jexer/TImage.java
src/jexer/TTerminalWindow.java
src/jexer/backend/LogicalScreen.java
src/jexer/backend/MultiScreen.java
src/jexer/backend/Screen.java
src/jexer/backend/TWindowBackend.java
src/jexer/tterminal/Sixel.java

index c3e75bed0fd3f39ed9aa265a64fdc50e2e69a6bf..3aaca2f832cf41b74a8a9c60600c7cdad4f96a45 100644 (file)
@@ -366,25 +366,8 @@ public class TImage extends TWidget {
      * @param always if true, always resize the cells
      */
     private void sizeToImage(final boolean always) {
-        int textWidth = 16;
-        int textHeight = 20;
-
-        if (getScreen() instanceof SwingTerminal) {
-            SwingTerminal terminal = (SwingTerminal) getScreen();
-
-            textWidth = terminal.getTextWidth();
-            textHeight = terminal.getTextHeight();
-        } if (getScreen() instanceof MultiScreen) {
-            MultiScreen terminal = (MultiScreen) getScreen();
-
-            textWidth = terminal.getTextWidth();
-            textHeight = terminal.getTextHeight();
-        } else if (getScreen() instanceof ECMA48Terminal) {
-            ECMA48Terminal terminal = (ECMA48Terminal) getScreen();
-
-            textWidth = terminal.getTextWidth();
-            textHeight = terminal.getTextHeight();
-        }
+        int textWidth = getScreen().getTextWidth();
+        int textHeight = getScreen().getTextHeight();
 
         if (image == null) {
             image = rotateImage(originalImage, clockwise);
index 9780e29d7d6643ad0c032801c6605d3320b91052..d703b676007be0d256bc889bd1fa885addfc539c 100644 (file)
@@ -767,20 +767,8 @@ public class TTerminalWindow extends TScrollableWindow
         newStatusBar(i18n.getString("statusBarRunning"));
 
         // Pass the correct text cell width/height to the emulator
-        int textWidth = 16;
-        int textHeight = 20;
-        if (getScreen() instanceof SwingTerminal) {
-            SwingTerminal terminal = (SwingTerminal) getScreen();
-
-            textWidth = terminal.getTextWidth();
-            textHeight = terminal.getTextHeight();
-        } else if (getScreen() instanceof ECMA48Terminal) {
-            ECMA48Terminal terminal = (ECMA48Terminal) getScreen();
-            textWidth = terminal.getTextWidth();
-            textHeight = terminal.getTextHeight();
-        }
-        emulator.setTextWidth(textWidth);
-        emulator.setTextHeight(textHeight);
+        emulator.setTextWidth(getScreen().getTextWidth());
+        emulator.setTextHeight(getScreen().getTextHeight());
     }
 
     /**
@@ -961,15 +949,12 @@ public class TTerminalWindow extends TScrollableWindow
     private void putDoubleWidthCharXY(final DisplayLine line, final int x,
         final int y, final Cell cell) {
 
-        int textWidth = 16;
-        int textHeight = 20;
+        int textWidth = getScreen().getTextWidth();
+        int textHeight = getScreen().getTextHeight();
         boolean cursorBlinkVisible = true;
 
         if (getScreen() instanceof SwingTerminal) {
             SwingTerminal terminal = (SwingTerminal) getScreen();
-
-            textWidth = terminal.getTextWidth();
-            textHeight = terminal.getTextHeight();
             cursorBlinkVisible = terminal.getCursorBlinkVisible();
         } else if (getScreen() instanceof ECMA48Terminal) {
             ECMA48Terminal terminal = (ECMA48Terminal) getScreen();
@@ -981,9 +966,6 @@ public class TTerminalWindow extends TScrollableWindow
                 putCharXY(x + 1, y, ' ', cell);
                 return;
             }
-
-            textWidth = terminal.getTextWidth();
-            textHeight = terminal.getTextHeight();
             cursorBlinkVisible = blinkState;
         } else {
             // We don't know how to dray glyphs to this screen, draw them as
index 24ba4aff9f12afeec1b8b0609baf4e733d3a0524..513c599145440e8a9682ef5c764b8e5d650e9c29 100644 (file)
@@ -134,6 +134,26 @@ public class LogicalScreen implements Screen {
     // Screen -----------------------------------------------------------------
     // ------------------------------------------------------------------------
 
+    /**
+     * Get the width of a character cell in pixels.
+     *
+     * @return the width in pixels of a character cell
+     */
+    public int getTextWidth() {
+        // Default width is 16 pixels.
+        return 16;
+    }
+
+    /**
+     * Get the height of a character cell in pixels.
+     *
+     * @return the height in pixels of a character cell
+     */
+    public int getTextHeight() {
+        // Default height is 20 pixels.
+        return 20;
+    }
+
     /**
      * Set drawing offset for x.
      *
index b77872bba5f6eb617c70dace855efe9c9501bb2c..0c3a2894bbe29c9bc0af7067cd9b79f8d9b060bc 100644 (file)
@@ -646,14 +646,7 @@ public class MultiScreen implements Screen {
     public int getTextWidth() {
         int textWidth = 16;
         for (Screen screen: screens) {
-            int newTextWidth = textWidth;
-            if (screen instanceof MultiScreen) {
-                newTextWidth = ((MultiScreen) screen).getTextWidth();
-            } else if (screen instanceof ECMA48Terminal) {
-                newTextWidth = ((ECMA48Terminal) screen).getTextWidth();
-            } else if (screen instanceof SwingTerminal) {
-                newTextWidth = ((SwingTerminal) screen).getTextWidth();
-            }
+            int newTextWidth = screen.getTextWidth();
             if (newTextWidth < textWidth) {
                 textWidth = newTextWidth;
             }
@@ -669,14 +662,7 @@ public class MultiScreen implements Screen {
     public int getTextHeight() {
         int textHeight = 20;
         for (Screen screen: screens) {
-            int newTextHeight = textHeight;
-            if (screen instanceof MultiScreen) {
-                newTextHeight = ((MultiScreen) screen).getTextHeight();
-            } else if (screen instanceof ECMA48Terminal) {
-                newTextHeight = ((ECMA48Terminal) screen).getTextHeight();
-            } else if (screen instanceof SwingTerminal) {
-                newTextHeight = ((SwingTerminal) screen).getTextHeight();
-            }
+            int newTextHeight = screen.getTextHeight();
             if (newTextHeight < textHeight) {
                 textHeight = newTextHeight;
             }
index 41c07562ed2c67089adb7591472899cc499de8de..2d9cd65459d6ed5f9740a5abad4a1825e1ba06c2 100644 (file)
@@ -395,4 +395,18 @@ public interface Screen {
      */
     public void setTitle(final String title);
 
+    /**
+     * Get the width of a character cell in pixels.
+     *
+     * @return the width in pixels of a character cell
+     */
+    public int getTextWidth();
+
+    /**
+     * Get the height of a character cell in pixels.
+     *
+     * @return the height in pixels of a character cell
+     */
+    public int getTextHeight();
+
 }
index 3c1f832cfefdb8ba6cc2a0c10204abc2fec92306..f644b76ba4bf3d9e1642502ae330fc97146d1a5d 100644 (file)
@@ -112,6 +112,26 @@ public class TWindowBackend extends TWindow implements Backend {
             window.setHeight(getHeight() + 2);
         }
 
+        /**
+         * Get the width of a character cell in pixels.
+         *
+         * @return the width in pixels of a character cell
+         */
+        @Override
+        public int getTextWidth() {
+            return window.getScreen().getTextWidth();
+        }
+
+        /**
+         * Get the height of a character cell in pixels.
+         *
+         * @return the height in pixels of a character cell
+         */
+        @Override
+        public int getTextHeight() {
+            return window.getScreen().getTextHeight();
+        }
+
     }
 
 
index 1921f44e5c83716ba0c75e2634e30617e3582c20..63e3c0fadcfa670d308c0f327f89f8e21313b810 100644 (file)
@@ -172,6 +172,11 @@ public class Sixel {
         BufferedImage newImage = new BufferedImage(newWidth, newHeight,
             BufferedImage.TYPE_INT_ARGB);
 
+        if (DEBUG) {
+            System.err.println("resizeImage(); old " + image.getWidth() + "x" +
+                image.getHeight() + " new " + newWidth + "x" + newHeight);
+        }
+
         Graphics2D gr = newImage.createGraphics();
         gr.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
         gr.dispose();
@@ -407,14 +412,15 @@ public class Sixel {
                 toGround();
             }
 
-            if (height + 6 < image.getHeight()) {
+            height += 6;
+            x = 0;
+
+            if (height + 6 > image.getHeight()) {
                 // Resize the image, give us another HEIGHT_INCREASE
                 // pixels of vertical length.
                 resizeImage(image.getWidth(),
                     image.getHeight() + HEIGHT_INCREASE);
             }
-            height += 6;
-            x = 0;
             return;
         }