Skip to content

Commit

Permalink
Issues when resizing some shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
arnobl committed Nov 3, 2015
1 parent 22d1fdc commit 9f9f766
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 80 deletions.
2 changes: 2 additions & 0 deletions latexdraw-core/net.sf.latexdraw/release_note.txt
Expand Up @@ -2,6 +2,8 @@
Version 3.3.2

Bugs fixed:
- Issues when resizing some shapes
https://bugs.launchpad.net/latexdraw/+bug/1504095
- NPE crash when exporting shapes as a template
https://bugs.launchpad.net/latexdraw/+bug/1504098
- Crash while loading an SVG document
Expand Down
Expand Up @@ -52,10 +52,17 @@ protected LAbstractCtrlPointShape() {


@Override
public void scale(final double x, final double y, final Position pos, final Rectangle2D bound) {
super.scale(x, y, pos, bound);
scaleSetPoints(firstCtrlPts, x, y, pos, bound);
scaleSetPoints(secondCtrlPts, x, y, pos, bound);
public void scale(final double prevWidth, final double prevHeight, final Position pos, final Rectangle2D bound) {
super.scale(prevWidth, prevHeight, pos, bound);
scaleSetPoints(firstCtrlPts, prevWidth, prevHeight, pos, bound);
scaleSetPoints(secondCtrlPts, prevWidth, prevHeight, pos, bound);
}

@Override
public void scaleWithRatio(final double prevWidth, final double prevHeight, final Position pos, final Rectangle2D bound) {
super.scaleWithRatio(prevWidth, prevHeight, pos, bound);
scaleSetPointsWithRatio(firstCtrlPts, prevWidth, prevHeight, pos, bound);
scaleSetPointsWithRatio(secondCtrlPts, prevWidth, prevHeight, pos, bound);
}

/**
Expand Down
Expand Up @@ -34,7 +34,7 @@ class LDot extends LPositionShape implements IDot {
protected DotStyle style;

/** The radius of the dot. */
protected double radius;
protected double diametre;


/**
Expand All @@ -44,7 +44,7 @@ protected LDot(final IPoint pt) {
super(pt);

style = DotStyle.DOT;
radius = 20.;
diametre = 20.;
}


Expand All @@ -62,7 +62,7 @@ public DotStyle getDotStyle() {

@Override
public double getDiametre() {
return radius;
return diametre;
}


Expand All @@ -74,9 +74,9 @@ public void setDotStyle(final DotStyle style) {


@Override
public void setDiametre(final double radius) {
if(radius > 0. && GLibUtilities.isValidCoordinate(radius))
this.radius = radius;
public void setDiametre(final double diam) {
if(diam > 0. && GLibUtilities.isValidCoordinate(diam))
this.diametre = diam;
}


Expand Down Expand Up @@ -135,10 +135,14 @@ public double getY() {
}


@Override
public void scale(final double prevWidth, final double prevHeight, final Position pos, final Rectangle2D bound) {
scaleWithRatio(prevWidth, prevHeight, pos, bound);
}

@Override
public void scale(final double x, final double y, final Position pos, final Rectangle2D bound) {
setDiametre(radius * Math.max(x/bound.getWidth(), y/bound.getHeight()));
public void scaleWithRatio(final double prevWidth, final double prevHeight, final Position pos, final Rectangle2D bound) {
setDiametre(diametre * Math.max(prevWidth/bound.getWidth(), prevHeight/bound.getHeight()));
}


Expand Down Expand Up @@ -215,31 +219,31 @@ protected void getTopLeftBottomRightPoints(final IPoint tl, final IPoint br) {
final IPoint centre = getPosition();
final double x = centre.getX();
final double y = centre.getY();
final double tlx = x - radius;
final double tly = y - radius;
final double brx = x + radius;
final double bry = y + radius;
final double dec = 2. * radius / THICKNESS_O_STYLE_FACTOR;
final double tlx = x - diametre;
final double tly = y - diametre;
final double brx = x + diametre;
final double bry = y + diametre;
final double dec = 2. * diametre / THICKNESS_O_STYLE_FACTOR;

// Each dot shape has a special shape computed from the parameters
// defined below.
switch(style){
case ASTERISK:// TODO: to check, I do not think it works.
final double radiusAst = tly + radius / 5. - (bry - radius / 5.) / 2. + dec;
final double radiusAst = tly + diametre / 5. - (bry - diametre / 5.) / 2. + dec;
tl.setX(Math.cos(7 * Math.PI / 6.) * radiusAst + x);
tl.setY(tly + radius / 5. - dec);
tl.setY(tly + diametre / 5. - dec);
br.setX(Math.cos(Math.PI / 6.) * radiusAst + x);
br.setY(bry - radius / 5. + dec);
br.setY(bry - diametre / 5. + dec);
break;
case BAR:
// The thickness of the bar.
final double barThickness = radius / 8.;
final double barThickness = diametre / 8.;
tl.setX(x - barThickness);
tl.setY(tly);
br.setX(x + barThickness);
// TODO: check if it is not radius*(1/1.875+1/8.): the bar
// thickness may be used into radius/1.875
br.setY(bry + radius / 1.875);
br.setY(bry + diametre / 1.875);
break;
case DIAMOND:
case FDIAMOND:
Expand All @@ -253,7 +257,7 @@ protected void getTopLeftBottomRightPoints(final IPoint tl, final IPoint br) {
break;
case FPENTAGON:
case PENTAGON:
final double dist = radius + dec;
final double dist = diametre + dec;
final double xValue = Math.sin(2. * Math.PI / 5.) * dist;
tl.setX(-xValue + x);
tl.setY(tly - dec);
Expand Down Expand Up @@ -284,14 +288,14 @@ protected void getTopLeftBottomRightPoints(final IPoint tl, final IPoint br) {
br.setY(bry);
break;
case PLUS:// TODO may be wrong, to compare with 2.0.
final double plusGap = radius / 80.;
final double plusGap = diametre / 80.;
tl.setX(tlx - plusGap);
tl.setY(tly - plusGap);
br.setX(brx + plusGap);
br.setY(bry + plusGap);
break;
case X:// TODO may be wrong, to compare with 2.0.
final double crossGap = radius / 5.;
final double crossGap = diametre / 5.;
tl.setX(tlx - crossGap);
tl.setY(tly - crossGap);
br.setX(brx + crossGap);
Expand Down Expand Up @@ -319,58 +323,58 @@ public boolean isFilled() {
@Override
public IPoint getLazyTopLeftPoint() {
final IPoint centre = getPosition();
return ShapeFactory.createPoint(centre.getX() - radius / 2., centre.getY() - radius / 2.);
return ShapeFactory.createPoint(centre.getX() - diametre / 2., centre.getY() - diametre / 2.);
}



@Override
public IPoint getLazyBottomRightPoint() {
final IPoint centre = getPosition();
return ShapeFactory.createPoint(centre.getX() + radius / 2., centre.getY() + radius / 2.);
return ShapeFactory.createPoint(centre.getX() + diametre / 2., centre.getY() + diametre / 2.);
}



@Override
public double getPlusGap() {
return radius / 160.;
return diametre / 160.;
}



@Override
public double getCrossGap() {
return radius / 10.;
return diametre / 10.;
}



@Override
public double getBarGap() {
return radius / 3.75;
return diametre / 3.75;
}



@Override
public double getBarThickness() {
return radius / 8.;
return diametre / 8.;
}



@Override
public double getGeneralGap() {
return radius / IDot.THICKNESS_O_STYLE_FACTOR;
return diametre / IDot.THICKNESS_O_STYLE_FACTOR;
}



@Override
public double getOGap() {
final double dec = style==DotStyle.O ? 3.6 : 2.6;
return radius * (0.1 / dec) * 2;
return diametre * (0.1 / dec) * 2;
}


Expand Down
Expand Up @@ -137,9 +137,13 @@ public IPoint getTopLeftPoint() {
return ShapeFactory.createPoint(pos.getX()+getGridMinX()*PPC, pos.getY()-getGridMaxY()*PPC*unit);
}


@Override
public void scale(final double x, final double y, final Position pos, final Rectangle2D bound) {
scaleWithRatio(x, y, pos, bound);
}

@Override
public void scaleWithRatio(final double x, final double y, final Position pos, final Rectangle2D bound) {
if(pos==null || bound==null) return;

final double sx = x/bound.getWidth();
Expand Down
@@ -1,12 +1,18 @@
package net.sf.latexdraw.glib.models.impl

import java.awt.geom.Rectangle2D
import java.awt.Color
import java.awt.geom.Rectangle2D
import scala.collection.JavaConversions.asScalaBuffer
import scala.collection.mutable.Buffer
import net.sf.latexdraw.glib.models.ShapeFactory
import net.sf.latexdraw.glib.models.interfaces.shape.IGroup
import net.sf.latexdraw.glib.models.interfaces.shape.IPoint
import net.sf.latexdraw.glib.models.interfaces.shape.IShape
import net.sf.latexdraw.glib.models.ShapeFactory
import net.sf.latexdraw.glib.models.interfaces.shape.ISquaredShape
import net.sf.latexdraw.glib.models.interfaces.shape.IShape.Position
import net.sf.latexdraw.glib.models.interfaces.prop.IStdGridProp
import net.sf.latexdraw.glib.models.interfaces.shape.IDot
import net.sf.latexdraw.glib.models.interfaces.shape.IStandardGrid

/**
* This trait encapsulates the code of the group related to the support of the general shape's properties.<br>
Expand Down Expand Up @@ -38,11 +44,15 @@ private[impl] trait LGroupShape extends IGroup {
override def setThickness(thickness : Double) = getShapes.foreach{_.setThickness(thickness)}


override def scale(sx : Double, sy : Double, pos : IShape.Position, bound : Rectangle2D) {
getShapes.foreach{_.scale(sx, sy, pos, bound)}
override def scale(prevWidth : Double, prevHeight : Double, pos : IShape.Position, bound : Rectangle2D) {
val shs:Buffer[IShape] = getShapes

if(shs.exists{sh => sh.isInstanceOf[ISquaredShape] || sh.isInstanceOf[IStandardGrid] || sh.isInstanceOf[IDot]})
shs.foreach{_.scaleWithRatio(prevWidth, prevHeight, pos, bound)}
else
shs.foreach{_.scale(prevWidth, prevHeight, pos, bound)}
}



override def getThickness: Double = {
getShapes.find{_.isThicknessable} match {
case Some(sh) => sh.getThickness
Expand Down
Expand Up @@ -8,8 +8,10 @@ import net.sf.latexdraw.glib.models.interfaces.prop.IPlotProp
import net.sf.latexdraw.parsers.ps.PSFunctionParser
import net.sf.latexdraw.glib.models.interfaces.shape.IShape
import net.sf.latexdraw.glib.models.interfaces.prop.IDotProp
import net.sf.latexdraw.glib.models.interfaces.shape.IShape.Position
import net.sf.latexdraw.glib.views.pst.PSTricksConstants
import java.awt.Color
import java.awt.geom.Rectangle2D

/**
* Implementation of the plotted function.
Expand Down Expand Up @@ -92,7 +94,7 @@ private[impl] class LPlot(pt:IPoint, var minX:Double, var maxX:Double, var equat
override def getBottomRightPoint = {
val step = getPlottingStep
val pos = getPosition
ShapeFactory.createPoint(pos.getX+maxX*IShape.PPC*xscale, pos.getY-getY(minX)*IShape.PPC*xscale)
ShapeFactory.createPoint(pos.getX+maxX*IShape.PPC*xscale, pos.getY-(0 until nbPoints).map{x=>getY(minX+x*step)}.min*IShape.PPC*yscale)
}

override def getTopRightPoint = {
Expand All @@ -104,7 +106,24 @@ private[impl] class LPlot(pt:IPoint, var minX:Double, var maxX:Double, var equat
override def getBottomLeftPoint = {
val step = getPlottingStep
val pos = getPosition
ShapeFactory.createPoint(pos.getX+minX*IShape.PPC*xscale, pos.getY-getY(minX)*IShape.PPC*xscale)
ShapeFactory.createPoint(pos.getX+minX*IShape.PPC*xscale, pos.getY-(0 until nbPoints).map{x=>getY(minX+x*step)}.min*IShape.PPC*yscale)
}

protected override def scaleSetPointsWithRatio(pts:java.util.List[IPoint], prevWidth:Double, prevHeight:Double, pos:Position, bound:Rectangle2D) {
scaleSetPoints(pts, prevWidth, prevHeight, pos, bound)
}

protected override def scaleSetPoints(pts:java.util.List[IPoint], prevWidth:Double, prevHeight:Double, pos:Position, bound:Rectangle2D) {
pos match {
case Position.EAST => getPtAt(0).translate(bound.getWidth-prevWidth, 0.0)
case Position.WEST => getPtAt(0).translate(prevWidth-bound.getWidth, 0.0)
case Position.SOUTH => getPtAt(0).translate(0.0, bound.getHeight-prevHeight)
case Position.NORTH => getPtAt(0).translate(0.0, prevHeight-bound.getHeight)
case Position.NE => getPtAt(0).translate(bound.getWidth-prevWidth, prevHeight-bound.getHeight)
case Position.NW => getPtAt(0).translate(prevWidth-bound.getWidth, prevHeight-bound.getHeight)
case Position.SE => getPtAt(0).translate(bound.getWidth-prevWidth, bound.getHeight-prevHeight)
case Position.SW => getPtAt(0).translate(prevWidth-bound.getWidth, bound.getHeight-prevHeight)
}
}

override def getPosition = getPtAt(0)
Expand Down
Expand Up @@ -5,15 +5,15 @@
import java.util.ArrayList;
import java.util.List;

import org.malai.mapping.MappingRegistry;

import net.sf.latexdraw.glib.models.GLibUtilities;
import net.sf.latexdraw.glib.models.ShapeFactory;
import net.sf.latexdraw.glib.models.interfaces.shape.IPoint;
import net.sf.latexdraw.glib.models.interfaces.shape.IShape;
import net.sf.latexdraw.glib.views.pst.PSTricksConstants;
import net.sf.latexdraw.util.LNumber;

import org.malai.mapping.MappingRegistry;

/**
* Defines a model of a shape.<br>
* <br>
Expand Down Expand Up @@ -725,19 +725,40 @@ public void setLineStyle(final LineStyle lineStyle) {


@Override
public void scale(final double x, final double y, final Position pos, final Rectangle2D bound) {
public void scale(final double prevWidth, final double prevHeight, final Position pos, final Rectangle2D bound) {
if(bound==null || pos==null) return ;
scaleSetPoints(points, prevWidth, prevHeight, pos, bound);
}

@Override
public void scaleWithRatio(final double prevWidth, final double prevHeight, final Position pos, final Rectangle2D bound) {
if(bound==null || pos==null) return ;
scaleSetPoints(points, x, y, pos, bound);
scaleSetPointsWithRatio(points, prevWidth, prevHeight, pos, bound);
}


protected void scaleSetPointsWithRatio(final List<IPoint> pts, final double prevWidth, final double prevHeight, final Position pos, final Rectangle2D bound) {
final double s = Math.max(prevWidth/bound.getWidth(), prevHeight/bound.getHeight());
final IPoint refPt = pos.getReferencePoint(bound);
final double refX = refPt.getX();
final double refY = refPt.getY();

protected void scaleSetPoints(final List<IPoint> pts, final double x, final double y, final Position pos, final Rectangle2D bound) {
final double sx = x/bound.getWidth();
final double sy = y/bound.getHeight();
for(final IPoint pt : pts) {
if(!LNumber.equalsDouble(pt.getX(), refX))
pt.setX(refX+(pt.getX()-refX)*s);
if(!LNumber.equalsDouble(pt.getY(), refY))
pt.setY(refY+(pt.getY()-refY)*s);
}
}

protected void scaleSetPoints(final List<IPoint> pts, final double prevWidth, final double prevHeight, final Position pos, final Rectangle2D bound) {
final double sx = prevWidth/bound.getWidth();
final double sy = prevHeight/bound.getHeight();
final boolean xScale = pos.isEast() || pos.isWest();
final boolean yScale = pos.isNorth() || pos.isSouth();
final double refX = pos.isWest() ? bound.getX() : bound.getMaxX();
final double refY = pos.isNorth() ? bound.getY() : bound.getMaxY();
final IPoint refPt = pos.getReferencePoint(bound);
final double refX = refPt.getX();
final double refY = refPt.getY();

for(final IPoint pt : pts) {
if(xScale && !LNumber.equalsDouble(pt.getX(), refX))
Expand Down

0 comments on commit 9f9f766

Please sign in to comment.