/** * Draws the rotated text. * * @param gc the graphics context * @param text the text * @param x the x coordinate * @param y the y coordinate * @param angle the angle */ private void drawRotatedText(GC gc, String text, float x, float y, int angle) { int textWidth = gc.textExtent(text).x; int textHeight = gc.textExtent(text).y; // create image to draw text Image image = new Image(Display.getCurrent(), textWidth, textHeight); GC tmpGc = new GC(image); tmpGc.setForeground(getForeground()); tmpGc.setBackground(gc.getBackground()); tmpGc.setFont(getFont()); tmpGc.drawText(text, 0, 0); // set transform to rotate Transform transform = new Transform(gc.getDevice()); transform.translate(x, y); transform.rotate(360 - angle); gc.setTransform(transform); // draw the image on the rotated graphics context gc.drawImage(image, 0, 0); // dispose resources tmpGc.dispose(); transform.dispose(); image.dispose(); gc.setTransform(null); }
@Override public void render( final PaintEvent event, final ITreeModel<IRectangle<TaxonomyNode>> model, final IRectangle<TaxonomyNode> rectangle, final IColorProvider<TaxonomyNode, Color> colorProvider, final ILabelProvider<TaxonomyNode> labelProvider) { TaxonomyNode item = rectangle.getNode(); if (item.getClassification() != null) return; Color oldForeground = event.gc.getForeground(); Color oldBackground = event.gc.getBackground(); Rectangle r = new Rectangle( rectangle.getX(), rectangle.getY(), rectangle.getWidth(), rectangle.getHeight()); this.colorProvider.drawRectangle(model.getRoot().getNode(), item, event.gc, r); String label = item.getName(); String info = String.format( "%s (%s%%)", Values.Amount.format(item.getActual()), // $NON-NLS-1$ Values.Percent.format( (double) item.getActual() / (double) this.model.getRootNode().getActual())); event.gc.setForeground(Colors.getTextColor(event.gc.getBackground())); Point labelExtend = event.gc.textExtent(label); Point infoExtend = event.gc.textExtent(info); int width = Math.max(labelExtend.x, infoExtend.x); if (width <= rectangle.getWidth() || rectangle.getWidth() > rectangle.getHeight()) { event.gc.drawText(label, r.x + 2, r.y + 2, true); event.gc.drawText(info, r.x + 2, r.y + 2 + labelExtend.y, true); } else { final Transform transform = new Transform(event.display); try { transform.translate(r.x, r.y); transform.rotate(-90); event.gc.setTransform(transform); event.gc.drawString(label, -labelExtend.x - 2, 2, true); event.gc.drawString(info, -infoExtend.x - 2, 2 + labelExtend.y, true); } finally { transform.dispose(); } } event.gc.setForeground(oldForeground); event.gc.setBackground(oldBackground); }
public static /* synchronized */ Image resize( final Display display, final Image srcImage, final int newWidth, final int newHeight, final int antialias, final int interpolation, final Rotation exifRotation) { if (srcImage == null) { return null; } final Rectangle originalImageBounds = srcImage.getBounds(); final int originalWidth = originalImageBounds.width; final int originalHeight = originalImageBounds.height; final int srcWidth = originalWidth; final int srcHeight = originalHeight; final int destWidth = newWidth; final int destHeight = newHeight; int imgWidth = newWidth; int imgHeight = newHeight; // OSX is rotating the image automatically boolean isNoAutoRotate = UI.IS_OSX == false; isNoAutoRotate |= _isRotateImageAutomatically == false; if (isNoAutoRotate) { if (exifRotation == Rotation.CW_90 || exifRotation == Rotation.CW_270) { // swap width/height imgWidth = newHeight; imgHeight = newWidth; } } final Image scaledImage = new Image(display, imgWidth, imgHeight); final GC gc = new GC(scaledImage); Transform transformation = null; try { gc.setAdvanced(true); gc.setAntialias(antialias); gc.setInterpolation(interpolation); // gc.setAntialias(SWT.ON); // gc.setInterpolation(SWT.LOW); int destX = 0; int destY = 0; if (exifRotation != null && isNoAutoRotate) { final int imgWidth2 = imgWidth / 2; final int imgHeight2 = imgHeight / 2; transformation = new Transform(display); transformation.translate(imgWidth2, imgHeight2); if (exifRotation == Rotation.CW_90) { transformation.rotate(90); destX = -imgHeight2; destY = -imgWidth2; } else if (exifRotation == Rotation.CW_180) { // this case is not yet tested transformation.rotate(180); destX = -imgWidth2; destY = -imgHeight2; } else if (exifRotation == Rotation.CW_270) { transformation.rotate(270); destX = -imgHeight2; destY = -imgWidth2; } gc.setTransform(transformation); } gc.drawImage( srcImage, // 0, 0, srcWidth, srcHeight, // destX, destY, destWidth, destHeight); } finally { // ensure resources are disposed when an error occures gc.dispose(); if (transformation != null) { transformation.dispose(); } } return scaledImage; }