diff options
| author | Pierre-Charles David | 2015-09-21 08:34:11 +0000 |
|---|---|---|
| committer | Pierre-Charles David | 2016-02-23 10:29:39 +0000 |
| commit | 69ab78cbbeee444e3592898637d983272f15cf63 (patch) | |
| tree | 23abdc27eee352673afd08d66723f7417fa3b512 | |
| parent | 08dcd2af0d5d69d65a505278b14439656dc19a55 (diff) | |
| download | org.eclipse.sirius-69ab78cbbeee444e3592898637d983272f15cf63.tar.gz org.eclipse.sirius-69ab78cbbeee444e3592898637d983272f15cf63.tar.xz org.eclipse.sirius-69ab78cbbeee444e3592898637d983272f15cf63.zip | |
[442268] Move render() directly in the transcoder
Also reduce the visibility of some methods in transcoder which are no
longer called externally.
Bug: 442268
Change-Id: I6a90af727893865136094a0a1c1a8448764ba9b9
Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
2 files changed, 91 insertions, 87 deletions
diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/SVGFigure.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/SVGFigure.java index 2cf6703d47..60eb2448ff 100644 --- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/SVGFigure.java +++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/SVGFigure.java @@ -12,7 +12,6 @@ */ package org.eclipse.sirius.diagram.ui.tools.api.figure; -import java.awt.image.BufferedImage; import java.io.IOException; import java.text.MessageFormat; import java.util.Collection; @@ -29,11 +28,9 @@ import org.eclipse.sirius.diagram.DiagramPlugin; import org.eclipse.sirius.diagram.ui.provider.DiagramUIPlugin; import org.eclipse.sirius.diagram.ui.provider.Messages; import org.eclipse.sirius.diagram.ui.tools.internal.figure.TransparentFigureGraphicsModifier; -import org.eclipse.sirius.diagram.ui.tools.internal.figure.svg.SVGUtils; import org.eclipse.sirius.diagram.ui.tools.internal.figure.svg.SimpleImageTranscoder; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; -import org.eclipse.swt.widgets.Display; import org.w3c.dom.Document; import com.google.common.cache.Cache; @@ -73,7 +70,9 @@ public class SVGFigure extends Figure implements StyledFigure, ITransparentFigur String key = fig.getKey(graphics); Image result = images.getIfPresent(key); if (result == null) { - result = render(fig, clientArea, graphics); + if (fig.transcoder != null) { + result = fig.transcoder.render(fig, clientArea, graphics, CACHE_SCALED_IMAGES); + } if (result != null) { images.put(key, result); } @@ -345,29 +344,13 @@ public class SVGFigure extends Figure implements StyledFigure, ITransparentFigur protected Image getImage(Rectangle clientArea, Graphics graphics) { if (CACHE_ENABLED) { return CACHE.getImage(this, clientArea, graphics); + } else if (transcoder != null) { + return transcoder.render(this, clientArea, graphics, CACHE_SCALED_IMAGES); } else { - return render(this, clientArea, graphics); + return null; } } - protected static Image render(SVGFigure fig, Rectangle clientArea, Graphics graphics) { - Image result = null; - if (fig.getDocument() != null) { - if (CACHE_SCALED_IMAGES && graphics != null) { - Rectangle scaledArea = new Rectangle(clientArea); - scaledArea.performScale(graphics.getAbsoluteScale()); - fig.getTranscoder().setCanvasSize(scaledArea.width, scaledArea.height); - } else { - fig.getTranscoder().setCanvasSize(clientArea.width, clientArea.height); - } - fig.getTranscoder().updateRenderingHints(graphics); - BufferedImage awtImage = fig.getTranscoder().getBufferedImage(); - if (awtImage != null) { - result = SVGUtils.toSWT(Display.getCurrent(), awtImage); - } - } - return result; - } /** * Remove all entries whose key begins with the given key. Remove from the diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/figure/svg/SimpleImageTranscoder.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/figure/svg/SimpleImageTranscoder.java index 8a8337c964..6cf8150ed8 100644 --- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/figure/svg/SimpleImageTranscoder.java +++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/figure/svg/SimpleImageTranscoder.java @@ -25,8 +25,12 @@ import org.apache.batik.transcoder.TranscoderInput; import org.apache.batik.transcoder.TranscoderOutput; import org.apache.batik.transcoder.image.ImageTranscoder; import org.eclipse.draw2d.Graphics; +import org.eclipse.draw2d.geometry.Rectangle; import org.eclipse.sirius.diagram.DiagramPlugin; import org.eclipse.sirius.diagram.ui.provider.Messages; +import org.eclipse.sirius.diagram.ui.tools.api.figure.SVGFigure; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.widgets.Display; import org.w3c.dom.Document; //CHECKSTYLE:OFF @@ -49,41 +53,49 @@ public class SimpleImageTranscoder extends SVGAbstractTranscoder { return document; } - public void setCanvasSize(int width, int height) { - if (this.canvasWidth == width && this.canvasHeight == height) { - return; + public int getImageHeight() { + int height = 0; + if (canvasHeight == -1) { + height = getBufferedImage().getHeight(); + } else { + height = canvasHeight; } - this.canvasWidth = width; - this.canvasHeight = height; - contentChanged(); + return height; } - public void contentChanged() { - bufferedImage = null; + public int getImageWidth() { + int width = 0; + if (canvasWidth == -1) { + width = getBufferedImage().getWidth(); + } else { + width = canvasWidth; + } + return width; } - private void updateImage() { - if (document == null) { - return; + public int getImageAlphaValue(int x, int y) { + BufferedImage bufferedImage = getBufferedImage(); + if (bufferedImage != null && bufferedImage.getWidth() >= x && bufferedImage.getHeight() >= y) { + int[] result = bufferedImage.getAlphaRaster().getPixel(x, y, new int[1]); + return result[0]; } - try { - if (canvasWidth > 0) { - addTranscodingHint(ImageTranscoder.KEY_WIDTH, new Float(canvasWidth)); - } else { - removeTranscodingHint(ImageTranscoder.KEY_WIDTH); - } - if (canvasHeight > 0) { - addTranscodingHint(ImageTranscoder.KEY_HEIGHT, new Float(canvasHeight)); - } else { - removeTranscodingHint(ImageTranscoder.KEY_HEIGHT); - } - removeTranscodingHint(ImageTranscoder.KEY_AOI); - transcode(new TranscoderInput(document), new TranscoderOutput()); - } catch (TranscoderException e) { - DiagramPlugin.getDefault().logError(Messages.SimpleImageTranscoder_svgImageTranscodingError, e); + return 255; + } + + public double getAspectRatio() { + if (canvasHeight == -1 || canvasWidth == -1) { + int width = getBufferedImage().getWidth(); + int height = getBufferedImage().getHeight(); + return (double) width / (double) height; + } else { + return (double) canvasWidth / (double) canvasHeight; } } + public void contentChanged() { + bufferedImage = null; + } + @Override protected void transcode(Document document, String uri, TranscoderOutput output) throws TranscoderException { super.transcode(document, uri, output); @@ -91,13 +103,11 @@ public class SimpleImageTranscoder extends SVGAbstractTranscoder { int h = (int) (height + 0.5); ImageRenderer renderer = createImageRenderer(); renderer.updateOffScreen(w, h); - // curTxf.translate(0.5, 0.5); renderer.setTransform(curTxf); renderer.setTree(this.root); - this.root = null; // We're done with it... + this.root = null; try { Shape raoi = new Rectangle2D.Float(0, 0, width, height); - // Warning: the renderer's AOI must be in user space renderer.repaint(curTxf.createInverse().createTransformedShape(raoi)); bufferedImage = renderer.getOffScreen(); } catch (Exception ex) { @@ -105,59 +115,70 @@ public class SimpleImageTranscoder extends SVGAbstractTranscoder { } } - protected ImageRenderer createImageRenderer() { + private ImageRenderer createImageRenderer() { StaticRenderer renderer = new StaticRenderer(); renderer.getRenderingHints().add(renderingHints); return renderer; } - public final BufferedImage getBufferedImage() { - if (bufferedImage == null) { - updateImage(); + public Image render(SVGFigure fig, Rectangle clientArea, Graphics graphics, boolean scaleImage) { + Image result = null; + if (document != null) { + if (scaleImage && graphics != null) { + Rectangle scaledArea = new Rectangle(clientArea); + scaledArea.performScale(graphics.getAbsoluteScale()); + setCanvasSize(scaledArea.width, scaledArea.height); + } else { + setCanvasSize(clientArea.width, clientArea.height); + } + updateRenderingHints(graphics); + BufferedImage awtImage = getBufferedImage(); + if (awtImage != null) { + result = SVGUtils.toSWT(Display.getCurrent(), awtImage); + } } - return bufferedImage; + return result; } - public int getImageHeight() { - int height = 0; - if (canvasHeight == -1) { - height = getBufferedImage().getHeight(); - } else { - height = canvasHeight; + private void updateImage() { + if (document == null) { + return; } - return height; - } - - public int getImageWidth() { - int width = 0; - if (canvasWidth == -1) { - width = getBufferedImage().getWidth(); - } else { - width = canvasWidth; + try { + if (canvasWidth > 0) { + addTranscodingHint(ImageTranscoder.KEY_WIDTH, new Float(canvasWidth)); + } else { + removeTranscodingHint(ImageTranscoder.KEY_WIDTH); + } + if (canvasHeight > 0) { + addTranscodingHint(ImageTranscoder.KEY_HEIGHT, new Float(canvasHeight)); + } else { + removeTranscodingHint(ImageTranscoder.KEY_HEIGHT); + } + removeTranscodingHint(ImageTranscoder.KEY_AOI); + transcode(new TranscoderInput(document), new TranscoderOutput()); + } catch (TranscoderException e) { + DiagramPlugin.getDefault().logError(Messages.SimpleImageTranscoder_svgImageTranscodingError, e); } - return width; } - public int getImageAlphaValue(int x, int y) { - BufferedImage bufferedImage = getBufferedImage(); - if (bufferedImage != null && bufferedImage.getWidth() >= x && bufferedImage.getHeight() >= y) { - int[] result = bufferedImage.getAlphaRaster().getPixel(x, y, new int[1]); - return result[0]; + private void setCanvasSize(int width, int height) { + if (this.canvasWidth == width && this.canvasHeight == height) { + return; } - return 255; + this.canvasWidth = width; + this.canvasHeight = height; + contentChanged(); } - public double getAspectRatio() { - if (canvasHeight == -1 || canvasWidth == -1) { - int width = getBufferedImage().getWidth(); - int height = getBufferedImage().getHeight(); - return (double) width / (double) height; - } else { - return (double) canvasWidth / (double) canvasHeight; + private BufferedImage getBufferedImage() { + if (bufferedImage == null) { + updateImage(); } + return bufferedImage; } - public void updateRenderingHints(Graphics graphics) { + private void updateRenderingHints(Graphics graphics) { Object antiAliasHint = SVGUtils.getAntialiasHint(graphics); if (renderingHints.get(RenderingHints.KEY_ANTIALIASING) != antiAliasHint) { renderingHints.put(RenderingHints.KEY_ANTIALIASING, antiAliasHint); |
