xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jon Burgin <jbur...@onboard-software.com>
Subject Resizing the view port (cont'd)
Date Thu, 11 Apr 2002 21:51:02 GMT
I'm not sure what you are after, but If you want to scale the svg to the
size of your svg canvas, you can extend jsvgcanvas and overload the
following function:

    /**
     * Computes the initial value of the transform used for rendering
	  * this scales the image (preserving aspect ratio)
	  * so that it fits the size of the canvas.
	  * 
     */
    protected void computeRenderingTransform() {
				Dimension2D _imageSize;
				Rectangle _paneSize;
				double x, y;
		  try{
				_imageSize = getSVGDocumentSize();
				_paneSize = this.getBounds();
				x = _paneSize.getWidth() /
_imageSize.getWidth();
				y = _paneSize.getHeight() /
_imageSize.getHeight();
				//If the image is too big in either
direction, 
				//shrink so larger dimension will fit. (i.e
apply the smaller ratio)
				if(x < 1.0 || y < 1.0){
					 x = Math.min(x,y);
					 y = Math.min(x,y);
				}else{//if smaller (or same in both) apply
the larger ratio
					 x = Math.max(x,y);
					 y = Math.max(x,y);
				}
		  }catch(NullPointerException npe){
				x = y = 1;
		  }
        initialTransform = new AffineTransform();
		  initialTransform.setToScale(x,y);
        setRenderingTransform(initialTransform);
    }
Be careful though, as jsvgcomponent will set the preferred size of
jsvgcanvas.  This will cause a problem if you have you canvas inside a
jscrollpane
To fix this I added the following function and set the min max size:

	/**
	 * We have to overwrite this function so that the loading of a new
	 * document does not change the preferred size beyond the min/max
	 * JSVGComponent.buildComplete(..) calls this.
	 * 
	 * @param d 
	 * 
	 */
	 public void setPreferredSize(Dimension d){
		  Dimension dmin, dmax;

		  dmin = getMinimumSize();
		  d.width = (int)Math.max(d.getWidth(),dmin.getWidth());
		  d.height = (int)Math.max(d.getHeight(),dmin.getHeight());

		  dmax = getMaximumSize();
		  d.width = (int)Math.min(d.getWidth(),dmax.getWidth());
		  d.height = (int)Math.min(d.getHeight(),dmax.getHeight());
		  super.setPreferredSize(d);
	 };

Hope this helps,
Jon

Jonathan Burgin
OnBoard Software Inc.
12621 Silicon Dr., Ste. 113
San Antonio, TX 78249
www.onboard-software.com
Phone: 210.932.1683
Direct Line: 210.477.5390
Fax: 210.932.1280


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Mime
View raw message