@Override public void copyTo( TextImage destination, int startRowIndex, int rows, int startColumnIndex, int columns, int destinationRowOffset, int destinationColumnOffset) { // If the source image position is negative, offset the whole image if (startColumnIndex < 0) { destinationColumnOffset += -startColumnIndex; columns += startColumnIndex; startColumnIndex = 0; } if (startRowIndex < 0) { startRowIndex += -startRowIndex; rows = startRowIndex; startRowIndex = 0; } // Make sure we can't copy more than is available columns = Math.min(buffer[0].length - startColumnIndex, columns); rows = Math.min(buffer.length - startRowIndex, rows); // Adjust target lengths as well columns = Math.min(destination.getSize().getColumns() - destinationColumnOffset, columns); rows = Math.min(destination.getSize().getRows() - destinationRowOffset, rows); if (columns <= 0 || rows <= 0) { return; } TerminalSize destinationSize = destination.getSize(); if (destination instanceof BasicTextImage) { int targetRow = destinationRowOffset; for (int y = startRowIndex; y < startRowIndex + rows && targetRow < destinationSize.getRows(); y++) { System.arraycopy( buffer[y], startColumnIndex, ((BasicTextImage) destination).buffer[targetRow++], destinationColumnOffset, columns); } } else { // Manually copy character by character for (int y = startRowIndex; y < startRowIndex + rows; y++) { for (int x = startColumnIndex; x < startColumnIndex + columns; x++) { destination.setCharacterAt( x - startColumnIndex + destinationColumnOffset, y - startRowIndex + destinationRowOffset, buffer[y][x]); } } } }
@Override public TextGraphics drawImage( TerminalPosition topLeft, TextImage image, TerminalPosition sourceImageTopLeft, TerminalSize sourceImageSize) { // If the source image position is negative, offset the whole image if (sourceImageTopLeft.getColumn() < 0) { topLeft = topLeft.withRelativeColumn(-sourceImageTopLeft.getColumn()); sourceImageSize = sourceImageSize.withRelativeColumns(sourceImageTopLeft.getColumn()); sourceImageTopLeft = sourceImageTopLeft.withColumn(0); } if (sourceImageTopLeft.getRow() < 0) { topLeft = topLeft.withRelativeRow(-sourceImageTopLeft.getRow()); sourceImageSize = sourceImageSize.withRelativeRows(sourceImageTopLeft.getRow()); sourceImageTopLeft = sourceImageTopLeft.withRow(0); } // cropping specified image-subrectangle to the image itself: int fromRow = Math.max(sourceImageTopLeft.getRow(), 0); int untilRow = Math.min( sourceImageTopLeft.getRow() + sourceImageSize.getRows(), image.getSize().getRows()); int fromColumn = Math.max(sourceImageTopLeft.getColumn(), 0); int untilColumn = Math.min( sourceImageTopLeft.getColumn() + sourceImageSize.getColumns(), image.getSize().getColumns()); // difference between position in image and position on target: int diffRow = topLeft.getRow() - sourceImageTopLeft.getRow(); int diffColumn = topLeft.getColumn() - sourceImageTopLeft.getColumn(); // top/left-crop at target(TextGraphics) rectangle: (only matters, if topLeft has a negative // coordinate) fromRow = Math.max(fromRow, -diffRow); fromColumn = Math.max(fromColumn, -diffColumn); // bot/right-crop at target(TextGraphics) rectangle: (only matters, if topLeft has a negative // coordinate) untilRow = Math.min(untilRow, getSize().getRows() - diffRow); untilColumn = Math.min(untilColumn, getSize().getColumns() - diffColumn); if (fromRow >= untilRow || fromColumn >= untilColumn) { return this; } for (int row = fromRow; row < untilRow; row++) { for (int column = fromColumn; column < untilColumn; column++) { setCharacter(column + diffColumn, row + diffRow, image.getCharacterAt(column, row)); } } return this; }
@Override public TextGraphics drawImage(TerminalPosition topLeft, TextImage image) { return drawImage(topLeft, image, TerminalPosition.TOP_LEFT_CORNER, image.getSize()); }