#5 auto-detect textAdjustX/textAdjustY, fix RGB VGA colors
authorKevin Lamonte <kevin.lamonte@gmail.com>
Tue, 4 Apr 2017 18:28:29 +0000 (14:28 -0400)
committerKevin Lamonte <kevin.lamonte@gmail.com>
Tue, 4 Apr 2017 18:28:29 +0000 (14:28 -0400)
src/jexer/io/ECMA48Terminal.java
src/jexer/io/SwingScreen.java

index 0cf0452a52f438ecc9865e66c672660cc6534c4c..6bc2fc87dcb48f77ba7a1da79f54e05c97bf73d1 100644 (file)
@@ -1294,19 +1294,19 @@ public final class ECMA48Terminal implements Runnable {
             // Bold implies foreground only
             sb.append("38;2;");
             if (color.equals(Color.BLACK)) {
-                sb.append("116;116;116");
+                sb.append("84;84;84");
             } else if (color.equals(Color.RED)) {
-                sb.append("252;116;116");
+                sb.append("252;84;84");
             } else if (color.equals(Color.GREEN)) {
-                sb.append("116;252;116");
+                sb.append("84;252;84");
             } else if (color.equals(Color.YELLOW)) {
-                sb.append("252;252;116");
+                sb.append("252;252;84");
             } else if (color.equals(Color.BLUE)) {
-                sb.append("116;116;252");
+                sb.append("84;84;252");
             } else if (color.equals(Color.MAGENTA)) {
-                sb.append("252;116;252");
+                sb.append("252;84;252");
             } else if (color.equals(Color.CYAN)) {
-                sb.append("116;252;252");
+                sb.append("84;252;252");
             } else if (color.equals(Color.WHITE)) {
                 sb.append("252;252;252");
             }
@@ -1323,7 +1323,7 @@ public final class ECMA48Terminal implements Runnable {
             } else if (color.equals(Color.GREEN)) {
                 sb.append("0;168;0");
             } else if (color.equals(Color.YELLOW)) {
-                sb.append("168;116;0");
+                sb.append("168;84;0");
             } else if (color.equals(Color.BLUE)) {
                 sb.append("0;0;168");
             } else if (color.equals(Color.MAGENTA)) {
index a70c8824b3cc82c5198a42d51342e0a160da67d9..da5a42a24bd5b11c55c98b224f24a57778eb9110 100644 (file)
@@ -379,6 +379,87 @@ public final class SwingScreen extends Screen {
             }
         }
 
+        /**
+         * Figure out what textAdjustX and textAdjustY should be, based on
+         * the location of a vertical bar (to find textAdjustY) and a
+         * horizontal bar (to find textAdjustX).
+         *
+         * @return true if textAdjustX and textAdjustY were guessed at
+         * correctly
+         */
+        private boolean getFontAdjustments() {
+            BufferedImage image = null;
+
+            // What SHOULD happen is that the topmost/leftmost white pixel is
+            // at position (gr2x, gr2y).  But it might also be off by a pixel
+            // in either direction.
+
+            Graphics2D gr2 = null;
+            int gr2x = 3;
+            int gr2y = 3;
+            image = new BufferedImage(textWidth * 2, textHeight * 2,
+                BufferedImage.TYPE_INT_ARGB);
+
+            gr2 = image.createGraphics();
+            gr2.setFont(getFont());
+            gr2.setColor(java.awt.Color.BLACK);
+            gr2.fillRect(0, 0, textWidth * 2, textHeight * 2);
+            gr2.setColor(java.awt.Color.WHITE);
+            char [] chars = new char[1];
+            chars[0] = jexer.bits.GraphicsChars.VERTICAL_BAR;
+            gr2.drawChars(chars, 0, 1, gr2x, gr2y + textHeight - maxDescent);
+            gr2.dispose();
+
+            for (int x = 0; x < textWidth; x++) {
+                for (int y = 0; y < textHeight; y++) {
+
+                    /*
+                    System.err.println("X: " + x + " Y: " + y + " " +
+                        image.getRGB(x, y));
+                     */
+
+                    if ((image.getRGB(x, y) & 0xFFFFFF) != 0) {
+                        textAdjustY = (gr2y - y);
+
+                        // System.err.println("textAdjustY: " + textAdjustY);
+                        x = textWidth;
+                        break;
+                    }
+                }
+            }
+
+            gr2 = image.createGraphics();
+            gr2.setFont(getFont());
+            gr2.setColor(java.awt.Color.BLACK);
+            gr2.fillRect(0, 0, textWidth * 2, textHeight * 2);
+            gr2.setColor(java.awt.Color.WHITE);
+            chars[0] = jexer.bits.GraphicsChars.SINGLE_BAR;
+            gr2.drawChars(chars, 0, 1, gr2x, gr2y + textHeight - maxDescent);
+            gr2.dispose();
+
+            for (int x = 0; x < textWidth; x++) {
+                for (int y = 0; y < textHeight; y++) {
+
+                    /*
+                    System.err.println("X: " + x + " Y: " + y + " " +
+                        image.getRGB(x, y));
+                     */
+
+                    if ((image.getRGB(x, y) & 0xFFFFFF) != 0) {
+                        textAdjustX = (gr2x - x);
+
+                        // System.err.println("textAdjustX: " + textAdjustX);
+                        return true;
+                    }
+                }
+            }
+
+            // Something weird happened, don't rely on this function.
+            // System.err.println("getFontAdjustments: false");
+            return false;
+        }
+
+
         /**
          * Figure out my font dimensions.
          */
@@ -399,13 +480,18 @@ public final class SwingScreen extends Screen {
                 textHeight++;
             }
 
-            if (System.getProperty("os.name").startsWith("Windows")) {
-                textAdjustY = -1;
-                textAdjustX = 0;
-            }
-            if (System.getProperty("os.name").startsWith("Mac")) {
-                textAdjustY = -1;
-                textAdjustX = 0;
+            if (getFontAdjustments() == false) {
+                // We were unable to programmatically determine textAdjustX
+                // and textAdjustY, so try some guesses based on operating
+                // system.
+                if (System.getProperty("os.name").startsWith("Windows")) {
+                    textAdjustY = -1;
+                    textAdjustX = 0;
+                }
+                if (System.getProperty("os.name").startsWith("Mac")) {
+                    textAdjustY = -1;
+                    textAdjustX = 0;
+                }
             }
         }