@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 newTextGraphics(TerminalPosition topLeftCorner, TerminalSize size) throws IllegalArgumentException { TerminalSize writableArea = getSize(); if (topLeftCorner.getColumn() + size.getColumns() <= 0 || topLeftCorner.getColumn() >= writableArea.getColumns() || topLeftCorner.getRow() + size.getRows() <= 0 || topLeftCorner.getRow() >= writableArea.getRows()) { // The area selected is completely outside of this TextGraphics, so we can return a "null" // object that doesn't // do anything because it is impossible to change anything anyway return new NullTextGraphics(size); } return new SubTextGraphics(this, topLeftCorner, size); }
@Override public TerminalSize getPreferredSize(final ComboBox<V> comboBox) { TerminalSize size = TerminalSize.ONE.withColumns( (comboBox.getItemCount() == 0 ? CJKUtils.getColumnWidth(comboBox.getText()) : 0) + 2); synchronized (comboBox) { for (int i = 0; i < comboBox.getItemCount(); i++) { V item = comboBox.getItem(i); size = size.max( new TerminalSize( CJKUtils.getColumnWidth(item.toString()) + 2 + 1, 1)); // +1 to add a single column of space } } return size; }