@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]); } } } }