Skip to content

Commit

Permalink
Fix line width anti-aliasing issues
Browse files Browse the repository at this point in the history
* See #650
* Adjust figure bounds according to the line width
* AbstractDiagramModelObjectFigure#setLineWidth() only accepts integers
* Use ExtendedSWTGraphics class so that we can determine the scale when exporting to image
  • Loading branch information
Phillipus committed Jul 12, 2020
1 parent 5b04eb9 commit 1e1ecb4
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ protected void paintFigure(Graphics graphics) {
bounds.height--;

// Set line width here so that the whole figure is constrained, otherwise SVG graphics will have overspill
float lineWidth = 1.0f;
float offSet = lineWidth / 2;
int lineWidth = 1;
setLineWidth(graphics, lineWidth, bounds);

graphics.setForegroundColor(getFillColor());
Expand All @@ -150,19 +149,21 @@ protected void paintFigure(Graphics graphics) {
if(getBorderColor() != null) {
graphics.setAlpha(getLineAlpha());

float lineOffset = (float)lineWidth / 2;

graphics.setForegroundColor(ColorFactory.getLighterColor(getBorderColor(), 0.82f));
Path path = new Path(null);
path.moveTo(bounds.x - offSet, bounds.y);
path.moveTo(bounds.x - lineOffset, bounds.y);
path.lineTo(bounds.x + bounds.width, bounds.y);
path.lineTo(bounds.x + bounds.width, bounds.y + bounds.height);
graphics.drawPath(path);
path.dispose();

graphics.setForegroundColor(getBorderColor());
path = new Path(null);
path.moveTo(bounds.x, bounds.y - offSet);
path.moveTo(bounds.x, bounds.y - lineOffset);
path.lineTo(bounds.x, bounds.y + bounds.height);
path.lineTo(bounds.x + bounds.width + offSet, bounds.y + bounds.height);
path.lineTo(bounds.x + bounds.width + lineOffset, bounds.y + bounds.height);
graphics.drawPath(path);
path.dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;

import com.archimatetool.editor.diagram.util.ExtendedSWTGraphics;
import com.archimatetool.editor.preferences.IPreferenceConstants;
import com.archimatetool.editor.preferences.Preferences;
import com.archimatetool.editor.ui.ArchiLabelProvider;
import com.archimatetool.editor.ui.ColorFactory;
import com.archimatetool.editor.ui.FontFactory;
import com.archimatetool.editor.ui.ImageFactory;
import com.archimatetool.editor.ui.factory.IGraphicalObjectUIProvider;
import com.archimatetool.editor.ui.factory.ObjectUIFactory;
import com.archimatetool.editor.utils.PlatformUtils;
Expand Down Expand Up @@ -94,11 +96,31 @@ protected void drawFigure(Graphics graphics) {
* @param lineWidth The line width
* @param bounds The bounds of the object
*/
protected void setLineWidth(Graphics graphics, float lineWidth, Rectangle bounds) {
protected void setLineWidth(Graphics graphics, int lineWidth, Rectangle bounds) {
graphics.setLineWidth(lineWidth);

// If we are exporting to image or printing this will be ExtendedSWTGraphics
// Otherwise it will be SWTGraphics
final double scale = graphics instanceof ExtendedSWTGraphics ? ((ExtendedSWTGraphics)graphics).getScale() : FigureUtils.getFigureScale(this);

// If line width is 1 and scale is 100% then do nothing
if(lineWidth == 1 && scale == 1.0) {
return;
}

// Width and height reduced by line width to compensate for x,y offset
bounds.width -= lineWidth;
bounds.height -= lineWidth;
graphics.setLineWidthFloat(lineWidth);
graphics.translate(lineWidth / 2, lineWidth / 2);

// x,y offset is half of line width
float offset = (float)lineWidth / 2;

// If this is a non hi-res device and scale == 100% round up to integer to stop anti-aliasing
if(ImageFactory.getDeviceZoom() == 100 && scale == 1.0) {
offset = (float)Math.ceil(offset);
}

graphics.translate(offset, offset);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected void setDisabledState(Graphics graphics) {
* @param lineWidth The line width
* @param bounds The bounds of the object
*/
protected void setLineWidth(Graphics graphics, float lineWidth, Rectangle bounds) {
protected void setLineWidth(Graphics graphics, int lineWidth, Rectangle bounds) {
if(getOwner() instanceof AbstractDiagramModelObjectFigure) {
((AbstractDiagramModelObjectFigure)getOwner()).setLineWidth(graphics, lineWidth, bounds);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public void drawFigure(Graphics graphics) {
bounds.height--;

// Set line width here so that the whole figure is constrained, otherwise SVG graphics will have overspill
float lineWidth = 1.0f;
float offSet = lineWidth / 2;
int lineWidth = 1;
setLineWidth(graphics, lineWidth, bounds);

graphics.setAlpha(getAlpha());
Expand All @@ -61,8 +60,10 @@ public void drawFigure(Graphics graphics) {
graphics.setBackgroundPattern(gradient);
}

float lineOffset = (float)lineWidth / 2;

Path path1 = new Path(null);
path1.moveTo(bounds.x - offSet, bounds.y);
path1.moveTo(bounds.x - lineOffset, bounds.y);
path1.lineTo(bounds.x + bounds.width - FOLD_HEIGHT, bounds.y);
path1.lineTo(bounds.x + bounds.width, bounds.y + FOLD_HEIGHT);
path1.lineTo(bounds.x + bounds.width, bounds.y + bounds.height);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ protected void drawFigure(Graphics graphics) {
bounds.height--;

// Set line width here so that the whole figure is constrained, otherwise SVG graphics will have overspill
float lineWidth = 1.0f;
float lineOffset = lineWidth / 2;
int lineWidth = 1;
setLineWidth(graphics, lineWidth, bounds);

int offset = 11;
Expand Down Expand Up @@ -82,6 +81,7 @@ protected void drawFigure(Graphics graphics) {
// Outline
graphics.setAlpha(getLineAlpha());
graphics.setForegroundColor(getLineColor());
float lineOffset = (float)lineWidth / 2;
path.lineTo(bounds.x - lineOffset, bounds.y);
graphics.drawPath(path);
path.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public void drawFigure(Graphics graphics) {
bounds.height--;

// Set line width here so that the whole figure is constrained, otherwise SVG graphics will have overspill
float lineWidth = 1.0f;
float lineOffset = lineWidth / 2;
int lineWidth = 1;
setLineWidth(graphics, lineWidth, bounds);

Pattern gradient = null;
Expand All @@ -65,6 +64,8 @@ public void drawFigure(Graphics graphics) {
float y1 = bounds.y + (bounds.height / 5);
float y2 = bounds.y + bounds.height - (bounds.height / 5);

float lineOffset = (float)lineWidth / 2;

path.moveTo(bounds.x, y1);
path.lineTo(x1, y1);
path.lineTo(x1, bounds.y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public void drawFigure(Graphics graphics) {
bounds.height--;

// Set line width here so that the whole figure is constrained, otherwise SVG graphics will have overspill
float lineWidth = 1.0f;
float lineOffset = lineWidth / 2;
int lineWidth = 1;
setLineWidth(graphics, lineWidth, bounds);

int offset = 6;
Expand Down Expand Up @@ -85,6 +84,7 @@ public void drawFigure(Graphics graphics) {
// Outline
graphics.setAlpha(getLineAlpha());
graphics.setForegroundColor(getLineColor());
float lineOffset = (float)lineWidth / 2;
path.lineTo(bounds.x - lineOffset, bounds.y);
graphics.drawPath(path);
path.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private static ModelReferencedImage createModelReferencedImage(IFigure figure, d

Image image = new Image(Display.getDefault(), (int)(bounds.width * scale), (int)(bounds.height * scale) );
GC gc = new GC(image);
SWTGraphics graphics = new SWTGraphics(gc);
SWTGraphics graphics = new ExtendedSWTGraphics(gc);

// If scaled, then scale now
// Issue #621: SWTGraphics supports scale() so no need to use ScaledGraphics
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* This program and the accompanying materials
* are made available under the terms of the License
* which accompanies this distribution in the file LICENSE.txt
*/
package com.archimatetool.editor.diagram.util;

import org.eclipse.draw2d.SWTGraphics;
import org.eclipse.swt.graphics.GC;

/**
* Extended SWTGraphics so we can query the scale
*
* @author Phillip Beauvoir
*/
public class ExtendedSWTGraphics extends SWTGraphics {

private double scale = 1.0;

public ExtendedSWTGraphics(GC gc) {
super(gc);
}

@Override
public void scale(double factor) {
super.scale(factor);
scale = factor;
}

/**
* Don't over-ride getAbsoluteScale() as we want that to remain the same
*
* @return The current scale
*/
public double getScale() {
return scale;
}
}

0 comments on commit 1e1ecb4

Please sign in to comment.