From: Kevin Lamonte Date: Thu, 8 Aug 2019 09:53:21 +0000 (-0500) Subject: #49 cell attributes to int X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=027de5ae322ef58d3bc74051d3aa20847455361a;p=nikiroo-utils.git #49 cell attributes to int --- diff --git a/src/jexer/TTerminalWindow.java b/src/jexer/TTerminalWindow.java index 5970b6c..4e0123f 100644 --- a/src/jexer/TTerminalWindow.java +++ b/src/jexer/TTerminalWindow.java @@ -326,10 +326,9 @@ public class TTerminalWindow extends TScrollableWindow */ @Override public void draw() { - int width = getDisplayWidth(); boolean syncEmulator = false; - if ((System.currentTimeMillis() - lastUpdateTime > 125) + if ((System.currentTimeMillis() - lastUpdateTime >= 25) && (dirty == true) ) { // Too much time has passed, draw it all. @@ -410,8 +409,7 @@ public class TTerminalWindow extends TScrollableWindow continue; } - Cell newCell = new Cell(); - newCell.setTo(ch); + Cell newCell = new Cell(ch); boolean reverse = line.isReverseColor() ^ ch.isReverse(); newCell.setReverse(false); if (reverse) { @@ -985,8 +983,7 @@ public class TTerminalWindow extends TScrollableWindow BufferedImage image; if (line.getDoubleHeight() == 1) { // Double-height top half: don't draw the underline. - Cell newCell = new Cell(); - newCell.setTo(cell); + Cell newCell = new Cell(cell); newCell.setUnderline(false); image = doubleFont.getImage(newCell, textWidth * 2, textHeight * 2, cursorBlinkVisible); @@ -997,10 +994,8 @@ public class TTerminalWindow extends TScrollableWindow // Now that we have the double-wide glyph drawn, copy the right // pieces of it to the cells. - Cell left = new Cell(); - Cell right = new Cell(); - left.setTo(cell); - right.setTo(cell); + Cell left = new Cell(cell); + Cell right = new Cell(cell); right.setChar(' '); BufferedImage leftImage = null; BufferedImage rightImage = null; diff --git a/src/jexer/backend/GlyphMaker.java b/src/jexer/backend/GlyphMaker.java index 0c798b1..08cbee8 100644 --- a/src/jexer/backend/GlyphMaker.java +++ b/src/jexer/backend/GlyphMaker.java @@ -201,8 +201,7 @@ class GlyphMakerFont { Graphics2D gr2 = image.createGraphics(); gr2.setFont(font); - Cell cellColor = new Cell(); - cellColor.setTo(cell); + Cell cellColor = new Cell(cell); // Check for reverse if (cell.isReverse()) { @@ -231,8 +230,7 @@ class GlyphMakerFont { gr2.dispose(); // We need a new key that will not be mutated by invertCell(). - Cell key = new Cell(); - key.setTo(cell); + Cell key = new Cell(cell); if (cell.isBlink() && !blinkVisible) { glyphCacheBlink.put(key, image); } else { diff --git a/src/jexer/backend/LogicalScreen.java b/src/jexer/backend/LogicalScreen.java index 9af633d..e8d2662 100644 --- a/src/jexer/backend/LogicalScreen.java +++ b/src/jexer/backend/LogicalScreen.java @@ -978,16 +978,14 @@ public class LogicalScreen implements Screen { BufferedImage rightImage = image.getSubimage(getTextWidth(), 0, getTextWidth(), getTextHeight()); - Cell left = new Cell(); - left.setTo(cell); + Cell left = new Cell(cell); left.setImage(leftImage); left.setWidth(Cell.Width.LEFT); // Blank out the char itself, so that shadows do not leave artifacts. left.setChar(' '); putCharXY(x, y, left); - Cell right = new Cell(); - right.setTo(cell); + Cell right = new Cell(cell); right.setImage(rightImage); right.setWidth(Cell.Width.RIGHT); // Blank out the char itself, so that shadows do not leave artifacts. @@ -1006,8 +1004,7 @@ public class LogicalScreen implements Screen { public final void putFullwidthCharXY(final int x, final int y, final char ch, final CellAttributes attr) { - Cell cell = new Cell(ch); - cell.setAttr(attr); + Cell cell = new Cell(ch, attr); putFullwidthCharXY(x, y, cell); } diff --git a/src/jexer/backend/SwingTerminal.java b/src/jexer/backend/SwingTerminal.java index 4cd6d07..58b8f79 100644 --- a/src/jexer/backend/SwingTerminal.java +++ b/src/jexer/backend/SwingTerminal.java @@ -572,7 +572,6 @@ public class SwingTerminal extends LogicalScreen && (swing.getBufferStrategy() != null) ) { do { - clearPhysical(); do { drawToSwing(); } while (swing.getBufferStrategy().contentsRestored()); @@ -1201,8 +1200,7 @@ public class SwingTerminal extends LogicalScreen gr2 = (Graphics2D) gr; } - Cell cellColor = new Cell(); - cellColor.setTo(cell); + Cell cellColor = new Cell(cell); // Check for reverse if (cell.isReverse()) { @@ -1234,8 +1232,7 @@ public class SwingTerminal extends LogicalScreen // We need a new key that will not be mutated by // invertCell(). - Cell key = new Cell(); - key.setTo(cell); + Cell key = new Cell(cell); if (cell.isBlink() && !cursorBlinkVisible) { glyphCacheBlink.put(key, image); } else { diff --git a/src/jexer/bits/Cell.java b/src/jexer/bits/Cell.java index 4be433c..c949c3e 100644 --- a/src/jexer/bits/Cell.java +++ b/src/jexer/bits/Cell.java @@ -73,7 +73,7 @@ public final class Cell extends CellAttributes { /** * The character at this cell. */ - private char ch; + private char ch = ' '; /** * The display width of this cell. @@ -94,7 +94,7 @@ public final class Cell extends CellAttributes { * The background color used for the area the image portion might not * cover. */ - private Color background = null; + private Color background = Color.BLACK; /** * hashCode() needs to call image.hashCode(), which can get quite @@ -119,7 +119,7 @@ public final class Cell extends CellAttributes { * @see #reset() */ public Cell() { - reset(); + // NOP } /** @@ -130,7 +130,17 @@ public final class Cell extends CellAttributes { * @see #reset() */ public Cell(final char ch) { - reset(); + this.ch = ch; + } + + /** + * Public constructor sets the character and attributes. + * + * @param ch character to set to + * @param attr attributes to use + */ + public Cell(final char ch, final CellAttributes attr) { + super(attr); this.ch = ch; } diff --git a/src/jexer/bits/CellAttributes.java b/src/jexer/bits/CellAttributes.java index f60fd30..99366fd 100644 --- a/src/jexer/bits/CellAttributes.java +++ b/src/jexer/bits/CellAttributes.java @@ -34,43 +34,53 @@ package jexer.bits; public class CellAttributes { // ------------------------------------------------------------------------ - // Variables -------------------------------------------------------------- + // Constants -------------------------------------------------------------- // ------------------------------------------------------------------------ /** * Bold attribute. */ - private boolean bold; + private static final int BOLD = 0x01; /** * Blink attribute. */ - private boolean blink; + private static final int BLINK = 0x02; /** * Reverse attribute. */ - private boolean reverse; + private static final int REVERSE = 0x04; /** * Underline attribute. */ - private boolean underline; + private static final int UNDERLINE = 0x08; /** * Protected attribute. */ - private boolean protect; + private static final int PROTECT = 0x10; + + + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Boolean flags. + */ + private int flags = 0; /** * Foreground color. Color.WHITE, Color.RED, etc. */ - private Color foreColor; + private Color foreColor = Color.WHITE; /** * Background color. Color.WHITE, Color.RED, etc. */ - private Color backColor; + private Color backColor = Color.BLACK; /** * Foreground color as 24-bit RGB value. Negative value means not set. @@ -93,7 +103,7 @@ public class CellAttributes { * @see #reset() */ public CellAttributes() { - reset(); + // NOP } /** @@ -116,7 +126,7 @@ public class CellAttributes { * @return bold value */ public final boolean isBold() { - return bold; + return ((flags & BOLD) == 0 ? false : true); } /** @@ -125,7 +135,11 @@ public class CellAttributes { * @param bold new bold value */ public final void setBold(final boolean bold) { - this.bold = bold; + if (bold) { + flags |= BOLD; + } else { + flags &= ~BOLD; + } } /** @@ -134,7 +148,7 @@ public class CellAttributes { * @return blink value */ public final boolean isBlink() { - return blink; + return ((flags & BLINK) == 0 ? false : true); } /** @@ -143,7 +157,11 @@ public class CellAttributes { * @param blink new blink value */ public final void setBlink(final boolean blink) { - this.blink = blink; + if (blink) { + flags |= BLINK; + } else { + flags &= ~BLINK; + } } /** @@ -152,7 +170,7 @@ public class CellAttributes { * @return reverse value */ public final boolean isReverse() { - return reverse; + return ((flags & REVERSE) == 0 ? false : true); } /** @@ -161,7 +179,11 @@ public class CellAttributes { * @param reverse new reverse value */ public final void setReverse(final boolean reverse) { - this.reverse = reverse; + if (reverse) { + flags |= REVERSE; + } else { + flags &= ~REVERSE; + } } /** @@ -170,7 +192,7 @@ public class CellAttributes { * @return underline value */ public final boolean isUnderline() { - return underline; + return ((flags & UNDERLINE) == 0 ? false : true); } /** @@ -179,7 +201,11 @@ public class CellAttributes { * @param underline new underline value */ public final void setUnderline(final boolean underline) { - this.underline = underline; + if (underline) { + flags |= UNDERLINE; + } else { + flags &= ~UNDERLINE; + } } /** @@ -188,7 +214,7 @@ public class CellAttributes { * @return protect value */ public final boolean isProtect() { - return protect; + return ((flags & PROTECT) == 0 ? false : true); } /** @@ -197,7 +223,11 @@ public class CellAttributes { * @param protect new protect value */ public final void setProtect(final boolean protect) { - this.protect = protect; + if (protect) { + flags |= PROTECT; + } else { + flags &= ~PROTECT; + } } /** @@ -286,11 +316,7 @@ public class CellAttributes { * bold/underline/blink/rever/protect. */ public void reset() { - bold = false; - blink = false; - reverse = false; - underline = false; - protect = false; + flags = 0; foreColor = Color.WHITE; backColor = Color.BLACK; foreColorRGB = -1; @@ -310,15 +336,11 @@ public class CellAttributes { } CellAttributes that = (CellAttributes) rhs; - return ((foreColor == that.foreColor) + return ((flags == that.flags) + && (foreColor == that.foreColor) && (backColor == that.backColor) && (foreColorRGB == that.foreColorRGB) - && (backColorRGB == that.backColorRGB) - && (bold == that.bold) - && (reverse == that.reverse) - && (underline == that.underline) - && (blink == that.blink) - && (protect == that.protect)); + && (backColorRGB == that.backColorRGB)); } /** @@ -331,11 +353,7 @@ public class CellAttributes { int A = 13; int B = 23; int hash = A; - hash = (B * hash) + (bold ? 1 : 0); - hash = (B * hash) + (blink ? 1 : 0); - hash = (B * hash) + (underline ? 1 : 0); - hash = (B * hash) + (reverse ? 1 : 0); - hash = (B * hash) + (protect ? 1 : 0); + hash = (B * hash) + flags; hash = (B * hash) + foreColor.hashCode(); hash = (B * hash) + backColor.hashCode(); hash = (B * hash) + foreColorRGB; @@ -351,11 +369,7 @@ public class CellAttributes { public void setTo(final Object rhs) { CellAttributes that = (CellAttributes) rhs; - this.bold = that.bold; - this.blink = that.blink; - this.reverse = that.reverse; - this.underline = that.underline; - this.protect = that.protect; + this.flags = that.flags; this.foreColor = that.foreColor; this.backColor = that.backColor; this.foreColorRGB = that.foreColorRGB; @@ -374,8 +388,8 @@ public class CellAttributes { (foreColorRGB & 0xFFFFFF), (backColorRGB & 0xFFFFFF)); } - return String.format("%s%s%s on %s", (bold == true ? "bold " : ""), - (blink == true ? "blink " : ""), foreColor, backColor); + return String.format("%s%s%s on %s", (isBold() ? "bold " : ""), + (isBlink() ? "blink " : ""), foreColor, backColor); } } diff --git a/src/jexer/tterminal/DisplayLine.java b/src/jexer/tterminal/DisplayLine.java index 74e63a8..a32da9b 100644 --- a/src/jexer/tterminal/DisplayLine.java +++ b/src/jexer/tterminal/DisplayLine.java @@ -103,8 +103,7 @@ public class DisplayLine { public DisplayLine(final CellAttributes attr) { chars = new Cell[MAX_LINE_LENGTH]; for (int i = 0; i < chars.length; i++) { - chars[i] = new Cell(); - chars[i].setTo(attr); + chars[i] = new Cell(attr); } } @@ -193,8 +192,7 @@ public class DisplayLine { */ public void insert(final int idx, final Cell newCell) { System.arraycopy(chars, idx, chars, idx + 1, chars.length - idx - 1); - chars[idx] = new Cell(); - chars[idx].setTo(newCell); + chars[idx] = new Cell(newCell); } /** @@ -247,8 +245,7 @@ public class DisplayLine { */ public void delete(final int idx, final Cell newCell) { System.arraycopy(chars, idx + 1, chars, idx, chars.length - idx - 1); - chars[chars.length - 1] = new Cell(); - chars[chars.length - 1].setTo(newCell); + chars[chars.length - 1] = new Cell(newCell); } } diff --git a/src/jexer/tterminal/ECMA48.java b/src/jexer/tterminal/ECMA48.java index 80f0ffb..b33c785 100644 --- a/src/jexer/tterminal/ECMA48.java +++ b/src/jexer/tterminal/ECMA48.java @@ -690,9 +690,9 @@ public class ECMA48 implements Runnable { char [] readBufferUTF8 = null; byte [] readBuffer = null; if (utf8) { - readBufferUTF8 = new char[128]; + readBufferUTF8 = new char[2048]; } else { - readBuffer = new byte[128]; + readBuffer = new byte[2048]; } while (!done && !stopReaderThread) { @@ -3458,8 +3458,7 @@ public class ECMA48 implements Runnable { * DECALN - Screen alignment display. */ private void decaln() { - Cell newCell = new Cell(); - newCell.setChar('E'); + Cell newCell = new Cell('E'); for (DisplayLine line: display) { for (int i = 0; i < line.length(); i++) { line.replace(i, newCell); @@ -6909,8 +6908,7 @@ public class ECMA48 implements Runnable { lastTextHeight = textHeight; } - Cell cell = new Cell(ch); - cell.setAttr(currentState.attr); + Cell cell = new Cell(ch, currentState.attr); BufferedImage image = glyphMaker.getImage(cell, textWidth * 2, textHeight); BufferedImage leftImage = image.getSubimage(0, 0, textWidth, @@ -6918,14 +6916,12 @@ public class ECMA48 implements Runnable { BufferedImage rightImage = image.getSubimage(textWidth, 0, textWidth, textHeight); - Cell left = new Cell(); - left.setTo(cell); + Cell left = new Cell(cell); left.setImage(leftImage); left.setWidth(Cell.Width.LEFT); display.get(leftY).replace(leftX, left); - Cell right = new Cell(); - right.setTo(cell); + Cell right = new Cell(cell); right.setImage(rightImage); right.setWidth(Cell.Width.RIGHT); display.get(rightY).replace(rightX, right);