Skip to content

Commit b5af18c

Browse files
committed
[DiagramUtils] Use ImagePrintFigureOperation to generate images
- Needs to use an ImageGraphics for SWTGraphics or get the actual scale some other way. But ImagePrintFigureOperation sets scale on SWTGraphics so not sure if that will work - This is much slower and uses more memory. Nebula Gallery can run out of memory. - Needs methods that take a boolean of useImageOperation so that there is a per-method way to get an image with ImagePrintFigureOperation or not. - Have to guard against small zooms when using the Images in a Nebula Gallery
1 parent 5cec750 commit b5af18c

1 file changed

Lines changed: 60 additions & 2 deletions

File tree

com.archimatetool.editor/src/com/archimatetool/editor/diagram/util/DiagramUtils.java

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
package com.archimatetool.editor.diagram.util;
77

88
import org.eclipse.draw2d.FreeformFigure;
9+
import org.eclipse.draw2d.Graphics;
910
import org.eclipse.draw2d.IFigure;
11+
import org.eclipse.draw2d.ImagePrintFigureOperation;
1012
import org.eclipse.draw2d.SWTGraphics;
13+
import org.eclipse.draw2d.geometry.Dimension;
1114
import org.eclipse.draw2d.geometry.Rectangle;
1215
import org.eclipse.gef.EditPartFactory;
1316
import org.eclipse.gef.GraphicalViewer;
@@ -16,17 +19,19 @@
1619
import org.eclipse.gef.editparts.FreeformGraphicalRootEditPart;
1720
import org.eclipse.gef.editparts.LayerManager;
1821
import org.eclipse.gef.ui.parts.GraphicalViewerImpl;
19-
import org.eclipse.swt.graphics.AutoscalingMode;
2022
import org.eclipse.swt.graphics.GC;
2123
import org.eclipse.swt.graphics.Image;
24+
import org.eclipse.swt.graphics.ImageData;
2225
import org.eclipse.swt.layout.FillLayout;
2326
import org.eclipse.swt.widgets.Composite;
27+
import org.eclipse.swt.widgets.Control;
2428
import org.eclipse.swt.widgets.Display;
2529
import org.eclipse.swt.widgets.Shell;
2630

2731
import com.archimatetool.editor.diagram.DiagramEditorFactoryExtensionHandler;
2832
import com.archimatetool.editor.diagram.IDiagramEditorFactory;
2933
import com.archimatetool.editor.diagram.editparts.ArchimateDiagramEditPartFactory;
34+
import com.archimatetool.editor.diagram.figures.FigureUtils;
3035
import com.archimatetool.editor.diagram.sketch.editparts.SketchEditPartFactory;
3136
import com.archimatetool.model.IArchimateDiagramModel;
3237
import com.archimatetool.model.IDiagramModel;
@@ -70,7 +75,7 @@ else if(model instanceof ISketchModel) {
7075
}
7176

7277
GraphicalViewerImpl viewer = new GraphicalViewerImpl();
73-
viewer.createControl(parent).setAutoscalingMode(AutoscalingMode.ENABLED); // Stops text clipping on Windows
78+
viewer.createControl(parent);
7479

7580
viewer.setEditPartFactory(editPartFactory);
7681

@@ -155,6 +160,21 @@ private static ModelReferencedImage createModelReferencedImage(IFigure figure, d
155160
bounds.expand(margin / scale, margin / scale);
156161
}
157162

163+
// Use Image Operation
164+
// TODO: Use ImageGraphics to set scale
165+
if(FigureUtils.isAutoScaleEnabled()) {
166+
Shell shell = new Shell();
167+
try {
168+
ImagePrintDiagramOperation op = new ImagePrintDiagramOperation(shell, figure, scale, bounds);
169+
Image image = op.run();
170+
return new ModelReferencedImage(image, bounds);
171+
}
172+
finally {
173+
shell.dispose();
174+
}
175+
}
176+
177+
// Use manual method
158178
Image image = new Image(Display.getDefault(), (int)(bounds.width * scale), (int)(bounds.height * scale));
159179
GC gc = new GC(image);
160180
SWTGraphics graphics = new ImageGraphics(gc); // Use ImageGraphics so we can get actual scale
@@ -220,4 +240,42 @@ public static Rectangle getMinimumBounds(IFigure figure) {
220240

221241
return minimumBounds;
222242
}
243+
244+
/**
245+
* ImagePrintDiagramOperation
246+
* TODO: Use ImageGraphics to set scale or get scale some other way
247+
*/
248+
private static class ImagePrintDiagramOperation extends ImagePrintFigureOperation {
249+
private final Rectangle bounds;
250+
private final double scale;
251+
252+
public ImagePrintDiagramOperation(Control control, IFigure printSource, double scale, Rectangle bounds) {
253+
super(control, printSource);
254+
this.scale = scale;
255+
this.bounds = bounds.getCopy(); // Important to use a copy in case bounds is changed elsewhere
256+
}
257+
258+
@Override
259+
protected ImageData getImageDataAtZoom(int zoom) {
260+
// Zoom can be < 100 see https://github.com/eclipse-gef/gef-classic/issues/1146
261+
if(zoom < 100) {
262+
return null;
263+
}
264+
return super.getImageDataAtZoom(zoom);
265+
}
266+
267+
@Override
268+
protected Dimension getImageSize() {
269+
// Scale the size (width and height) of the bounds
270+
return bounds.getSize().scale(scale);
271+
}
272+
273+
@Override
274+
protected void preparePrintSource(Graphics graphics) {
275+
// Scale
276+
graphics.scale(scale);
277+
// Compensate for negative co-ordinates
278+
graphics.translate(bounds.x * -1, bounds.y * -1);
279+
}
280+
}
223281
}

0 commit comments

Comments
 (0)