Example #1
0
  /**
   * 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);
  }
  /**
   * Renders a label for the given rectangle. The label is fitted into the rectangle if possible.
   *
   * @param event the paint event to work with on
   * @param text the text to render
   * @param color the color to use
   * @param bounds the rectangle bounds
   */
  protected void render(
      final PaintEvent event, final String text, final RGB color, final IRectangle<N> bounds) {
    if (text != null) {
      // quite strange to create a new font object all the time?
      // it did not work when I tried to reuse a long-living font object!
      final Font font = new Font(event.display, fontName, 16, SWT.BOLD);
      try {
        event.gc.setFont(font);
        final Color c = new Color(event.display, color);
        try {
          event.gc.setForeground(c);
        } finally {
          c.dispose();
        }

        final Point p = event.gc.textExtent(text);
        p.x = p.x * 12 / 10; // make some space
        final float scale =
            (float) Math.min(bounds.getWidth() / (double) p.x, bounds.getHeight() / (double) p.y);
        final Transform transform = new Transform(event.display);
        try {
          transform.translate(
              (int) (bounds.getX() + bounds.getWidth() / 12d),
              bounds.getY() + (bounds.getHeight() - scale * p.y) / 2);
          transform.scale(scale, scale);
          event.gc.setTransform(transform);
          event.gc.drawString(text, 0, 0, true);
        } finally {
          transform.dispose();
        }
      } finally {
        font.dispose();
      }
    }
  }
Example #3
0
 /**
  * Applies a scale transform.
  *
  * @param scaleX the scale factor along the x-axis.
  * @param scaleY the scale factor along the y-axis.
  */
 @Override
 public void scale(double scaleX, double scaleY) {
   Transform swtTransform = new Transform(this.gc.getDevice());
   this.gc.getTransform(swtTransform);
   swtTransform.scale((float) scaleX, (float) scaleY);
   this.gc.setTransform(swtTransform);
   swtTransform.dispose();
 }
Example #4
0
 /**
  * Applies a translation.
  *
  * @param x the translation along the x-axis.
  * @param y the translation along the y-axis.
  */
 @Override
 public void translate(int x, int y) {
   Transform swtTransform = new Transform(this.gc.getDevice());
   this.gc.getTransform(swtTransform);
   swtTransform.translate(x, y);
   this.gc.setTransform(swtTransform);
   swtTransform.dispose();
 }
Example #5
0
 /**
  * Concatenates the specified transform to the existing transform.
  *
  * @param t the transform.
  */
 @Override
 public void transform(AffineTransform t) {
   Transform swtTransform = new Transform(this.gc.getDevice());
   this.gc.getTransform(swtTransform);
   swtTransform.multiply(getSwtTransformFromPool(t));
   this.gc.setTransform(swtTransform);
   swtTransform.dispose();
 }
Example #6
0
 /**
  * Returns the current transform.
  *
  * @return The current transform.
  */
 @Override
 public AffineTransform getTransform() {
   Transform swtTransform = new Transform(this.gc.getDevice());
   this.gc.getTransform(swtTransform);
   AffineTransform awtTransform = toAwtTransform(swtTransform);
   swtTransform.dispose();
   return awtTransform;
 }
Example #7
0
    @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);
    }
Example #8
0
 /** Clean used resources. */
 public void clean() {
   if (_clippingPath != null) {
     _gc.setClipping((Rectangle) null);
     _clippingPath.dispose();
     _clippingPath = null;
     _clippingArea = null;
   }
   if (_color != null) {
     _color.dispose();
     _color = null;
   }
   if (_transform != null) {
     _gc.setTransform(null);
     _transform.dispose();
   }
 }
Example #9
0
  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;
  }