Skip to content

Commit

Permalink
honor autosize in SVGIcon.getIcon{Width,Height}
Browse files Browse the repository at this point in the history
  • Loading branch information
fnatter committed Nov 5, 2016
1 parent e2fbdf9 commit 132e760
Showing 1 changed file with 48 additions and 6 deletions.
54 changes: 48 additions & 6 deletions svg-core/src/main/java/com/kitfox/svg/app/beans/SVGIcon.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ public Image getImage()
/**
* @return height of this icon
*/
@Override
public int getIconHeight()
public int getIconHeightIgnoreAutosize()
{
if (preferredSize != null &&
(autosize == AUTOSIZE_VERT || autosize == AUTOSIZE_STRETCH
Expand All @@ -138,8 +137,8 @@ public int getIconHeight()
/**
* @return width of this icon
*/
@Override
public int getIconWidth()

public int getIconWidthIgnoreAutosize()
{
if (preferredSize != null &&
(autosize == AUTOSIZE_HORIZ || autosize == AUTOSIZE_STRETCH
Expand All @@ -156,6 +155,49 @@ public int getIconWidth()
return (int)diagram.getWidth();
}

private boolean isAutoSizeBestFitUseFixedHeight(final int iconWidthIgnoreAutosize, final int iconHeightIgnoreAutosize,
final SVGDiagram diagram)
{
return iconHeightIgnoreAutosize/diagram.getHeight() < iconWidthIgnoreAutosize/diagram.getWidth();
}

@Override
public int getIconWidth()
{
final int iconWidthIgnoreAutosize = getIconWidthIgnoreAutosize();
final int iconHeightIgnoreAutosize = getIconHeightIgnoreAutosize();
final SVGDiagram diagram = svgUniverse.getDiagram(svgURI);
if (preferredSize != null && (autosize == AUTOSIZE_VERT ||
(autosize == AUTOSIZE_BESTFIT && isAutoSizeBestFitUseFixedHeight(iconWidthIgnoreAutosize, iconHeightIgnoreAutosize, diagram))))
{
final double aspectRatio = diagram.getHeight()/diagram.getWidth();
return (int)(iconHeightIgnoreAutosize / aspectRatio);
}
else
{
return iconWidthIgnoreAutosize;
}
}

@Override
public int getIconHeight()
{
final int iconWidthIgnoreAutosize = getIconWidthIgnoreAutosize();
final int iconHeightIgnoreAutosize = getIconHeightIgnoreAutosize();
final SVGDiagram diagram = svgUniverse.getDiagram(svgURI);
if (preferredSize != null && (autosize == AUTOSIZE_HORIZ ||
(autosize == AUTOSIZE_BESTFIT && !isAutoSizeBestFitUseFixedHeight(iconWidthIgnoreAutosize, iconHeightIgnoreAutosize, diagram))))
{
final double aspectRatio = diagram.getHeight()/diagram.getWidth();
return (int)(iconWidthIgnoreAutosize * aspectRatio);
}
else
{
return iconHeightIgnoreAutosize;
}
}


/**
* Draws the icon to the specified component.
* @param comp - Component to draw icon to. This is ignored by SVGIcon, and can be set to null; only gg is used for drawing the icon
Expand Down Expand Up @@ -221,8 +263,8 @@ private void paintIcon(Component comp, Graphics2D g, int x, int y)
return;
}

final int width = getIconWidth();
final int height = getIconHeight();
final int width = getIconWidthIgnoreAutosize();
final int height = getIconHeightIgnoreAutosize();
// int width = getWidth();
// int height = getHeight();

Expand Down

0 comments on commit 132e760

Please sign in to comment.