提交 963e25ee 编写于 作者: M Mathieu Bastian

Merge pull request #1224 from jersub/png-margin-regression

Fix the PNG margin regression
......@@ -149,7 +149,7 @@ public class G2DRenderTargetBuilder implements RenderTargetBuilder {
}
@Override
public void refresh() {
public synchronized void refresh() {
if (graphics != null) {
graphics.refresh(previewModel, this);
}
......@@ -183,32 +183,39 @@ public class G2DRenderTargetBuilder implements RenderTargetBuilder {
}
public void refresh(PreviewModel m, RenderTarget target) {
if (m != null) {
background = m.getProperties()
.getColorValue(PreviewProperty.BACKGROUND_COLOR);
initAppletLayout(m);
g2.clearRect(0, 0, width, height);
g2.setTransform(new AffineTransform());
if (background != null) {
g2.setColor(background);
g2.fillRect(0, 0, width, height);
}
// user zoom
Vector center = new Vector(width / 2f, height / 2f);
Vector scaledCenter = Vector.mult(center, scaling);
Vector scaledTrans = Vector.sub(center, scaledCenter);
g2.translate(scaledTrans.x, scaledTrans.y);
g2.scale(scaling, scaling);
// user move
g2.translate(trans.x, trans.y);
//Draw target
previewController.render(target);
if (m == null) {
return;
}
if (!inited) {
CanvasSize cs = getSheetCanvasSize(m);
scaling = computeDefaultScaling(cs);
fit(cs);
inited = true;
}
g2.clearRect(0, 0, width, height);
g2.setTransform(new AffineTransform());
background = m.getProperties()
.getColorValue(PreviewProperty.BACKGROUND_COLOR);
if (background != null) {
g2.setColor(background);
g2.fillRect(0, 0, width, height);
}
// user zoom
Vector center = new Vector(width / 2F, height / 2F);
Vector scaledCenter = Vector.mult(center, scaling);
Vector scaledTrans = Vector.sub(center, scaledCenter);
g2.translate(scaledTrans.x, scaledTrans.y);
g2.scale(scaling, scaling);
// user move
g2.translate(trans.x, trans.y);
//Draw target
previewController.render(target);
}
public Vector getTranslate() {
......@@ -243,32 +250,33 @@ public class G2DRenderTargetBuilder implements RenderTargetBuilder {
inited = false;
}
/**
* Initializes the preview applet layout according to the graph's
* dimension.
*/
private void initAppletLayout(PreviewModel m) {
// graphSheet.setMargin(MARGIN);
if (!inited) {
private CanvasSize getSheetCanvasSize(PreviewModel m) {
CanvasSize cs = m.getGraphicsCanvasSize();
float marginPercentage = m.getProperties()
.getFloatValue(PreviewProperty.MARGIN);
float marginWidth = cs.getWidth() * marginPercentage / 100F;
float marginHeight = cs.getHeight() * marginPercentage / 100F;
return new CanvasSize(
cs.getX() - marginWidth,
cs.getY() - marginHeight,
cs.getWidth() + 2F * marginWidth,
cs.getHeight() + 2F * marginHeight);
}
// initializes zoom
CanvasSize cs = m.getGraphicsCanvasSize();
Vector box = new Vector(cs.getWidth(), cs.getHeight());
float ratioWidth = width / box.x;
float ratioHeight = height / box.y;
scaling = ratioWidth < ratioHeight ? ratioWidth : ratioHeight;
// initializes move
Vector semiBox = Vector.div(box, 2);
Vector topLeft = new Vector(cs.getX(), cs.getY());
Vector center = new Vector(width / 2F, height / 2F);
Vector scaledCenter = Vector.add(topLeft, semiBox);
trans.set(center);
trans.sub(scaledCenter);
// lastMove.set(trans);
private float computeDefaultScaling(CanvasSize cs) {
float ratioWidth = width / cs.getWidth();
float ratioHeight = height / cs.getHeight();
return ratioWidth < ratioHeight ? ratioWidth : ratioHeight;
}
inited = true;
}
private void fit(CanvasSize cs) {
Vector box = new Vector(cs.getWidth(), cs.getHeight());
Vector semiBox = Vector.div(box, 2F);
Vector topLeft = new Vector(cs.getX(), cs.getY());
Vector center = new Vector(width / 2F, height / 2F);
Vector scaledCenter = Vector.add(topLeft, semiBox);
trans.set(center);
trans.sub(scaledCenter);
}
}
}
......@@ -50,6 +50,7 @@ import org.gephi.io.exporter.spi.ByteExporter;
import org.gephi.io.exporter.spi.VectorExporter;
import org.gephi.preview.api.G2DTarget;
import org.gephi.preview.api.PreviewController;
import org.gephi.preview.api.PreviewModel;
import org.gephi.preview.api.PreviewProperties;
import org.gephi.preview.api.PreviewProperty;
import org.gephi.preview.api.RenderTarget;
......@@ -72,34 +73,27 @@ public class PNGExporter implements VectorExporter, ByteExporter, LongTask {
private int width = 1024;
private int height = 1024;
private boolean transparentBackground = false;
private int margin = 4;
private int margin = 4; //FIXME Use a float instead to avoid extra cast
private G2DTarget target;
private Color oldColor;
@Override
public boolean execute() {
Progress.start(progress);
PreviewController controller = Lookup.getDefault().lookup(PreviewController.class);
controller.getModel(workspace).getProperties().putValue(PreviewProperty.VISIBILITY_RATIO, 1.0);
PreviewController ctrl
= Lookup.getDefault().lookup(PreviewController.class);
PreviewModel m = ctrl.getModel(workspace);
PreviewProperties props = controller.getModel(workspace).getProperties();
props.putValue("width", width);
props.putValue("height", height);
Color oldColor = props.getColorValue(PreviewProperty.BACKGROUND_COLOR);
if (transparentBackground) {
props.putValue(PreviewProperty.BACKGROUND_COLOR, new Color(255, 255, 255, 0));//White transparent
}
props.putValue(PreviewProperty.MARGIN, new Float((float) margin));
controller.refreshPreview(workspace);
target = (G2DTarget) controller.getRenderTarget(RenderTarget.G2D_TARGET, workspace);
setExportProperties(m);
ctrl.refreshPreview(workspace);
target = (G2DTarget) ctrl.getRenderTarget(
RenderTarget.G2D_TARGET,
workspace);
if (target instanceof LongTask) {
((LongTask) target).setProgressTicket(progress);
}
//Fix bug caused by keeping width and height in the workspace preview properties.
//When a .gephi file is loaded later with these properties PGraphics will be created instead of a PApplet
props.removeSimpleValue("width");
props.removeSimpleValue("height");
props.removeSimpleValue(PreviewProperty.MARGIN);
try {
target.refresh();
......@@ -111,12 +105,12 @@ public class PNGExporter implements VectorExporter, ByteExporter, LongTask {
img.getGraphics().drawImage(sourceImg, 0, 0, null);
ImageIO.write(img, "png", stream);
stream.close();
props.putValue(PreviewProperty.BACKGROUND_COLOR, oldColor);
} catch (Exception e) {
throw new RuntimeException(e);
}
discardExportProperties(m);
Progress.finish(progress);
return !cancel;
......@@ -169,6 +163,7 @@ public class PNGExporter implements VectorExporter, ByteExporter, LongTask {
this.stream = stream;
}
@Override
public boolean cancel() {
cancel = true;
if (target instanceof LongTask) {
......@@ -177,7 +172,30 @@ public class PNGExporter implements VectorExporter, ByteExporter, LongTask {
return true;
}
@Override
public void setProgressTicket(ProgressTicket progressTicket) {
this.progress = progressTicket;
}
private synchronized void setExportProperties(PreviewModel m) {
PreviewProperties props = m.getProperties();
props.putValue(PreviewProperty.VISIBILITY_RATIO, 1.0F);
props.putValue("width", width);
props.putValue("height", height);
oldColor = props.getColorValue(PreviewProperty.BACKGROUND_COLOR);
if (transparentBackground) {
props.putValue(
PreviewProperty.BACKGROUND_COLOR,
new Color(255, 255, 255, 0)); //White transparent
}
props.putValue(PreviewProperty.MARGIN, new Float(margin));
}
private synchronized void discardExportProperties(PreviewModel m) {
PreviewProperties props = m.getProperties();
props.removeSimpleValue("width");
props.removeSimpleValue("height");
props.removeSimpleValue(PreviewProperty.MARGIN);
props.putValue(PreviewProperty.BACKGROUND_COLOR, oldColor);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册