Fix NPE
[nikiroo-utils.git] / src / jexer / io / SwingScreen.java
index b7c8410993ce8647e23eacc8c5f8aad3545ece8f..a70c8824b3cc82c5198a42d51342e0a160da67d9 100644 (file)
@@ -3,7 +3,7 @@
  *
  * The MIT License (MIT)
  *
- * Copyright (C) 2016 Kevin Lamonte
+ * Copyright (C) 2017 Kevin Lamonte
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -59,7 +59,7 @@ public final class SwingScreen extends Screen {
     /**
      * If true, use triple buffering thread.
      */
-    private static final boolean tripleBuffer = true;
+    private static boolean tripleBuffer = true;
 
     /**
      * Cursor style to draw.
@@ -165,6 +165,11 @@ public final class SwingScreen extends Screen {
          */
         SwingScreen screen;
 
+        /**
+         * If true, we were successful getting Terminus.
+         */
+        private boolean gotTerminus = false;
+
         /**
          * Width of a character cell.
          */
@@ -320,6 +325,14 @@ public final class SwingScreen extends Screen {
                 cursorStyle = CursorStyle.BLOCK;
             }
 
+            if (System.getProperty("jexer.Swing.tripleBuffer") != null) {
+                if (System.getProperty("jexer.Swing.tripleBuffer").
+                    equals("false")) {
+
+                    SwingScreen.tripleBuffer = false;
+                }
+            }
+
             setTitle("Jexer Application");
             setBackground(Color.black);
 
@@ -329,8 +342,9 @@ public final class SwingScreen extends Screen {
                         getContextClassLoader();
                 InputStream in = loader.getResourceAsStream(FONTFILE);
                 Font terminusRoot = Font.createFont(Font.TRUETYPE_FONT, in);
-                Font terminus = terminusRoot.deriveFont(Font.PLAIN, 22);
+                Font terminus = terminusRoot.deriveFont(Font.PLAIN, 20);
                 setFont(terminus);
+                gotTerminus = true;
             } catch (Exception e) {
                 e.printStackTrace();
                 // setFont(new Font("Liberation Mono", Font.PLAIN, 24));
@@ -375,15 +389,24 @@ public final class SwingScreen extends Screen {
             Rectangle2D bounds = fm.getMaxCharBounds(gr);
             int leading = fm.getLeading();
             textWidth = (int)Math.round(bounds.getWidth());
-            textHeight = (int)Math.round(bounds.getHeight()) - maxDescent;
-            // This also produces the same number, but works better for ugly
+            // textHeight = (int)Math.round(bounds.getHeight()) - maxDescent;
+
+            // This produces the same number, but works better for ugly
             // monospace.
             textHeight = fm.getMaxAscent() + maxDescent - leading;
 
+            if (gotTerminus == true) {
+                textHeight++;
+            }
+
             if (System.getProperty("os.name").startsWith("Windows")) {
                 textAdjustY = -1;
                 textAdjustX = 0;
             }
+            if (System.getProperty("os.name").startsWith("Mac")) {
+                textAdjustY = -1;
+                textAdjustX = 0;
+            }
         }
 
         /**
@@ -437,11 +460,19 @@ public final class SwingScreen extends Screen {
             }
 
             // Generate glyph and draw it.
-
-            image = new BufferedImage(textWidth, textHeight,
-                BufferedImage.TYPE_INT_ARGB);
-            Graphics2D gr2 = image.createGraphics();
-            gr2.setFont(getFont());
+            Graphics2D gr2 = null;
+            int gr2x = xPixel;
+            int gr2y = yPixel;
+            if (tripleBuffer) {
+                image = new BufferedImage(textWidth, textHeight,
+                    BufferedImage.TYPE_INT_ARGB);
+                gr2 = image.createGraphics();
+                gr2.setFont(getFont());
+                gr2x = 0;
+                gr2y = 0;
+            } else {
+                gr2 = (Graphics2D) gr;
+            }
 
             Cell cellColor = new Cell();
             cellColor.setTo(cell);
@@ -454,7 +485,7 @@ public final class SwingScreen extends Screen {
 
             // Draw the background rectangle, then the foreground character.
             gr2.setColor(attrToBackgroundColor(cellColor));
-            gr2.fillRect(0, 0, textWidth, textHeight);
+            gr2.fillRect(gr2x, gr2y, textWidth, textHeight);
 
             // Handle blink and underline
             if (!cell.isBlink()
@@ -463,25 +494,30 @@ public final class SwingScreen extends Screen {
                 gr2.setColor(attrToForegroundColor(cellColor));
                 char [] chars = new char[1];
                 chars[0] = cell.getChar();
-                gr2.drawChars(chars, 0, 1, 0 + textAdjustX,
-                    0 + textHeight - maxDescent + textAdjustY);
+                gr2.drawChars(chars, 0, 1, gr2x + textAdjustX,
+                    gr2y + textHeight - maxDescent + textAdjustY);
 
                 if (cell.isUnderline()) {
-                    gr2.fillRect(0, 0 + textHeight - 2, textWidth, 2);
+                    gr2.fillRect(gr2x, gr2y + textHeight - 2, textWidth, 2);
                 }
             }
-            gr2.dispose();
 
-            // We need a new key that will not be mutated by invertCell().
-            Cell key = new Cell();
-            key.setTo(cell);
-            if (cell.isBlink() && !cursorBlinkVisible) {
-                glyphCacheBlink.put(key, image);
-            } else {
-                glyphCache.put(key, image);
+            if (tripleBuffer) {
+                gr2.dispose();
+
+                // We need a new key that will not be mutated by
+                // invertCell().
+                Cell key = new Cell();
+                key.setTo(cell);
+                if (cell.isBlink() && !cursorBlinkVisible) {
+                    glyphCacheBlink.put(key, image);
+                } else {
+                    glyphCache.put(key, image);
+                }
+
+                gr.drawImage(image, xPixel, yPixel, this);
             }
 
-            gr.drawImage(image, xPixel, yPixel, this);
         }
 
         /**
@@ -857,4 +893,13 @@ public final class SwingScreen extends Screen {
         return ((y - frame.top) / frame.textHeight);
     }
 
+    /**
+     * Set the window title.
+     *
+     * @param title the new title
+     */
+    public void setTitle(final String title) {
+        frame.setTitle(title);
+    }
+
 }