From efd22f8bf1ba90c17d6c991b21afac9fff340502 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Sat, 29 Jan 2022 13:08:11 -0500 Subject: [PATCH] cleaning up TextAreaPainter to be less cute; adding more clarifications --- .../app/syntax/TextAreaPainter.java | 36 +++++++++---------- todo.txt | 2 ++ 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/app/src/processing/app/syntax/TextAreaPainter.java b/app/src/processing/app/syntax/TextAreaPainter.java index e68144cf1..5824ef6b2 100644 --- a/app/src/processing/app/syntax/TextAreaPainter.java +++ b/app/src/processing/app/syntax/TextAreaPainter.java @@ -376,9 +376,11 @@ public class TextAreaPainter extends JComponent implements TabExpander { y += fontMetrics.getHeight(); for (int i = 0; i < currentLine.count; i++) { gfx.drawChars(currentLine.array, currentLine.offset + i, 1, x, y); - x = currentLine.array[currentLine.offset + i] == '\t' ? - x0 + (int) nextTabStop(x - x0, i) : - x + fontMetrics.charWidth(currentLine.array[currentLine.offset + i]); // TODO why this char? + if (currentLine.array[currentLine.offset + i] == '\t') { + x = x0 + (int) nextTabStop(x - x0, i); + } else { + x += fontMetrics.charWidth(currentLine.array[currentLine.offset + i]); // TODO why this char? + } //textArea.offsetToX(line, currentLine.offset + i); } @@ -468,9 +470,11 @@ public class TextAreaPainter extends JComponent implements TabExpander { line.count = length; // huh? suspicious for (int i = 0; i < line.count; i++) { gfx.drawChars(line.array, line.offset + i, 1, x, y); - x = line.array[line.offset + i] == '\t' ? - x0 + (int) nextTabStop(x - x0, i) : - x + fontMetrics.charWidth(line.array[line.offset + i]); + if (line.array[line.offset + i] == '\t') { + x = x0 + (int) nextTabStop(x - x0, i); + } else { + x += fontMetrics.charWidth(line.array[line.offset + i]); + } } line.offset += length; tokens = tokens.next; @@ -564,36 +568,32 @@ public class TextAreaPainter extends JComponent implements TabExpander { protected void paintCaret(Graphics gfx, int line, int y) { - //System.out.println("painting caret " + line + " " + y); if (textArea.isCaretVisible()) { - //System.out.println("caret is visible"); int offset = textArea.getCaretPosition() - textArea.getLineStartOffset(line); int caretX = textArea._offsetToX(line, offset); - int caretWidth = ((defaults.blockCaret || - textArea.isOverwriteEnabled()) ? - fontMetrics.charWidth('w') : 1); + int caretWidth = 1; + if (defaults.blockCaret || textArea.isOverwriteEnabled()) { + caretWidth = fontMetrics.charWidth('w'); + } y += getLineDisplacement(); int height = fontMetrics.getHeight(); - //System.out.println("caretX, width = " + caretX + " " + caretWidth); - gfx.setColor(defaults.caretColor); if (textArea.isOverwriteEnabled()) { gfx.fillRect(caretX, y + height - 1, caretWidth,1); } else { - // some machines don't like the drawRect for the single - // pixel caret.. this caused a lot of hell because on that + // Some machines don't like the drawRect when the caret is a + // single pixel wide. This caused a lot of hell because on that // minority of machines, the caret wouldn't show up past - // the first column. the fix is to use drawLine() in - // those cases, as a workaround. + // the first column. The fix is to use drawLine() instead. if (caretWidth == 1) { + //gfx.drawLine(caretX, y, caretX, y + height - 1); // workaround for single pixel dots showing up when caret // is rendered a single pixel too tall [fry 220129] ((Graphics2D) gfx).draw(new Line2D.Float(caretX, y + 0.5f, caretX, y + height - 0.5f)); - //gfx.drawLine(caretX, y, caretX, y + height - 1); } else { gfx.drawRect(caretX, y, caretWidth - 1, height - 1); } diff --git a/todo.txt b/todo.txt index ae18277d9..11de16d6e 100755 --- a/todo.txt +++ b/todo.txt @@ -19,6 +19,8 @@ X also updated the two older bugs X https://github.com/processing/processing4/issues/226 X https://github.com/processing/processing4/issues/342 X caret is sometimes one pixel too tall +X cleaning up TextAreaPainter to be less cute; adding more clarifications + . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .