Changeset 11525 in josm for trunk/src/com/kitfox/svg/app/beans/SVGIcon.java
- Timestamp:
- 2017-02-02T00:08:08+01:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/kitfox/svg/app/beans/SVGIcon.java
r10787 r11525 4 4 * All rights reserved. 5 5 * 6 * Redistribution and use in source and binary forms, with or 6 * Redistribution and use in source and binary forms, with or 7 7 * without modification, are permitted provided that the following 8 8 * conditions are met: 9 9 * 10 * - Redistributions of source code must retain the above 10 * - Redistributions of source code must retain the above 11 11 * copyright notice, this list of conditions and the following 12 12 * disclaimer. 13 13 * - Redistributions in binary form must reproduce the above 14 14 * copyright notice, this list of conditions and the following 15 * disclaimer in the documentation and/or other materials 15 * disclaimer in the documentation and/or other materials 16 16 * provided with the distribution. 17 17 * … … 27 27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 * OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 29 * OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 31 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other 32 32 * projects can be found at http://www.kitfox.com … … 37 37 package com.kitfox.svg.app.beans; 38 38 39 import com.kitfox.svg.SVGCache;40 import com.kitfox.svg.SVGDiagram;41 import com.kitfox.svg.SVGException;42 import com.kitfox.svg.SVGUniverse;43 39 import java.awt.Component; 44 40 import java.awt.Dimension; … … 54 50 import java.beans.PropertyChangeSupport; 55 51 import java.net.URI; 52 56 53 import javax.swing.ImageIcon; 57 54 55 import com.kitfox.svg.SVGCache; 56 import com.kitfox.svg.SVGDiagram; 57 import com.kitfox.svg.SVGException; 58 import com.kitfox.svg.SVGUniverse; 58 59 59 60 /** … … 66 67 67 68 public static final String PROP_AUTOSIZE = "PROP_AUTOSIZE"; 68 69 69 70 private final PropertyChangeSupport changes = new PropertyChangeSupport(this); 70 71 71 72 SVGUniverse svgUniverse = SVGCache.getSVGUniverse(); 72 73 public static final int INTERP_NEAREST_NEIGHBOR = 0; 73 74 public static final int INTERP_BILINEAR = 1; 74 75 public static final int INTERP_BICUBIC = 2; 75 76 76 77 private boolean antiAlias; 77 78 private int interpolation = INTERP_NEAREST_NEIGHBOR; 78 79 private boolean clipToViewbox; 79 80 80 81 URI svgURI; 81 82 82 83 // private boolean scaleToFit; 83 84 AffineTransform scaleXform = new AffineTransform(); … … 89 90 public static final int AUTOSIZE_STRETCH = 4; 90 91 private int autosize = AUTOSIZE_NONE; 91 92 92 93 Dimension preferredSize; 93 94 94 95 /** Creates a new instance of SVGIcon */ 95 96 public SVGIcon() 96 97 { 97 98 } 98 99 99 100 public void addPropertyChangeListener(PropertyChangeListener p) 100 101 { 101 102 changes.addPropertyChangeListener(p); 102 103 } 103 104 104 105 public void removePropertyChangeListener(PropertyChangeListener p) 105 106 { 106 107 changes.removePropertyChangeListener(p); 107 108 } 108 109 110 @Override 109 111 public Image getImage() 110 112 { … … 117 119 * @return height of this icon 118 120 */ 119 public int getIconHeight ()121 public int getIconHeightIgnoreAutosize() 120 122 { 121 123 if (preferredSize != null && 122 (autosize == AUTOSIZE_VERT || autosize == AUTOSIZE_STRETCH 124 (autosize == AUTOSIZE_VERT || autosize == AUTOSIZE_STRETCH 123 125 || autosize == AUTOSIZE_BESTFIT)) 124 126 { 125 127 return preferredSize.height; 126 128 } 127 129 128 130 SVGDiagram diagram = svgUniverse.getDiagram(svgURI); 129 131 if (diagram == null) … … 133 135 return (int)diagram.getHeight(); 134 136 } 135 137 136 138 /** 137 139 * @return width of this icon 138 140 */ 139 public int getIconWidth ()141 public int getIconWidthIgnoreAutosize() 140 142 { 141 143 if (preferredSize != null && 142 (autosize == AUTOSIZE_HORIZ || autosize == AUTOSIZE_STRETCH 144 (autosize == AUTOSIZE_HORIZ || autosize == AUTOSIZE_STRETCH 143 145 || autosize == AUTOSIZE_BESTFIT)) 144 146 { 145 147 return preferredSize.width; 146 148 } 147 149 148 150 SVGDiagram diagram = svgUniverse.getDiagram(svgURI); 149 151 if (diagram == null) … … 153 155 return (int)diagram.getWidth(); 154 156 } 155 157 158 private boolean isAutoSizeBestFitUseFixedHeight(final int iconWidthIgnoreAutosize, final int iconHeightIgnoreAutosize, 159 final SVGDiagram diagram) 160 { 161 return iconHeightIgnoreAutosize/diagram.getHeight() < iconWidthIgnoreAutosize/diagram.getWidth(); 162 } 163 164 @Override 165 public int getIconWidth() 166 { 167 final int iconWidthIgnoreAutosize = getIconWidthIgnoreAutosize(); 168 final int iconHeightIgnoreAutosize = getIconHeightIgnoreAutosize(); 169 final SVGDiagram diagram = svgUniverse.getDiagram(svgURI); 170 if (preferredSize != null && (autosize == AUTOSIZE_VERT || 171 (autosize == AUTOSIZE_BESTFIT && isAutoSizeBestFitUseFixedHeight(iconWidthIgnoreAutosize, iconHeightIgnoreAutosize, diagram)))) 172 { 173 final double aspectRatio = diagram.getHeight()/diagram.getWidth(); 174 return (int)(iconHeightIgnoreAutosize / aspectRatio); 175 } 176 else 177 { 178 return iconWidthIgnoreAutosize; 179 } 180 } 181 182 @Override 183 public int getIconHeight() 184 { 185 final int iconWidthIgnoreAutosize = getIconWidthIgnoreAutosize(); 186 final int iconHeightIgnoreAutosize = getIconHeightIgnoreAutosize(); 187 final SVGDiagram diagram = svgUniverse.getDiagram(svgURI); 188 if (preferredSize != null && (autosize == AUTOSIZE_HORIZ || 189 (autosize == AUTOSIZE_BESTFIT && !isAutoSizeBestFitUseFixedHeight(iconWidthIgnoreAutosize, iconHeightIgnoreAutosize, diagram)))) 190 { 191 final double aspectRatio = diagram.getHeight()/diagram.getWidth(); 192 return (int)(iconWidthIgnoreAutosize * aspectRatio); 193 } 194 else 195 { 196 return iconHeightIgnoreAutosize; 197 } 198 } 199 156 200 /** 157 201 * Draws the icon to the specified component. … … 161 205 * @param y - Y coordinate to draw icon 162 206 */ 207 @Override 163 208 public void paintIcon(Component comp, Graphics gg, int x, int y) 164 209 { 165 //Copy graphics object so that 210 //Copy graphics object so that 166 211 Graphics2D g = (Graphics2D)gg.create(); 167 212 paintIcon(comp, g, x, y); 168 213 g.dispose(); 169 214 } 170 215 171 216 private void paintIcon(Component comp, Graphics2D g, int x, int y) 172 217 { 173 218 Object oldAliasHint = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING); 174 219 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antiAlias ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF); 175 220 176 221 Object oldInterpolationHint = g.getRenderingHint(RenderingHints.KEY_INTERPOLATION); 177 222 switch (interpolation) … … 187 232 break; 188 233 } 189 190 234 235 191 236 SVGDiagram diagram = svgUniverse.getDiagram(svgURI); 192 237 if (diagram == null) … … 194 239 return; 195 240 } 196 241 197 242 g.translate(x, y); 198 243 diagram.setIgnoringClipHeuristic(!clipToViewbox); … … 201 246 g.setClip(new Rectangle2D.Float(0, 0, diagram.getWidth(), diagram.getHeight())); 202 247 } 203 204 248 249 205 250 if (autosize == AUTOSIZE_NONE) 206 251 { … … 217 262 return; 218 263 } 219 220 final int width = getIconWidth ();221 final int height = getIconHeight ();264 265 final int width = getIconWidthIgnoreAutosize(); 266 final int height = getIconHeightIgnoreAutosize(); 222 267 // int width = getWidth(); 223 268 // int height = getHeight(); 224 269 225 270 if (width == 0 || height == 0) 226 271 { 227 272 return; 228 273 } 229 274 230 275 // if (width == 0 || height == 0) 231 276 // { … … 236 281 // return; 237 282 // } 238 283 239 284 // g.setClip(0, 0, width, height); 240 241 285 286 242 287 // final Rectangle2D.Double rect = new Rectangle2D.Double(); 243 288 // diagram.getViewRect(rect); 244 // 289 // 245 290 // scaleXform.setToScale(width / rect.width, height / rect.height); 246 291 double diaWidth = diagram.getWidth(); 247 292 double diaHeight = diagram.getHeight(); 248 293 249 294 double scaleW = 1; 250 295 double scaleH = 1; 251 296 if (autosize == AUTOSIZE_BESTFIT) 252 297 { 253 scaleW = scaleH = (height / diaHeight < width / diaWidth) 298 scaleW = scaleH = (height / diaHeight < width / diaWidth) 254 299 ? height / diaHeight : width / diaWidth; 255 300 } … … 268 313 } 269 314 scaleXform.setToScale(scaleW, scaleH); 270 315 271 316 AffineTransform oldXform = g.getTransform(); 272 317 g.transform(scaleXform); 273 318 274 319 try 275 320 { … … 280 325 throw new RuntimeException(e); 281 326 } 282 327 283 328 g.setTransform(oldXform); 284 285 329 330 286 331 g.translate(-x, -y); 287 332 288 333 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAliasHint); 289 334 if (oldInterpolationHint != null) … … 292 337 } 293 338 } 294 339 295 340 /** 296 341 * @return the universe this icon draws it's SVGDiagrams from … … 300 345 return svgUniverse; 301 346 } 302 347 303 348 public void setSvgUniverse(SVGUniverse svgUniverse) 304 349 { … … 307 352 changes.firePropertyChange("svgUniverse", old, svgUniverse); 308 353 } 309 354 310 355 /** 311 356 * @return the uni of the document being displayed by this icon … … 315 360 return svgURI; 316 361 } 317 362 318 363 /** 319 364 * Loads an SVG document from a URI. … … 324 369 URI old = this.svgURI; 325 370 this.svgURI = svgURI; 326 371 327 372 SVGDiagram diagram = svgUniverse.getDiagram(svgURI); 328 373 if (diagram != null) … … 335 380 diagram.setDeviceViewport(new Rectangle(0, 0, size.width, size.height)); 336 381 } 337 382 338 383 changes.firePropertyChange("svgURI", old, svgURI); 339 384 } 340 385 341 386 /** 342 387 * Loads an SVG document from the classpath. This function is equivilant to … … 347 392 { 348 393 URI old = this.svgURI; 349 394 350 395 try 351 396 { 352 397 svgURI = new URI(getClass().getResource(resourcePath).toString()); 353 398 changes.firePropertyChange("svgURI", old, svgURI); 354 399 355 400 SVGDiagram diagram = svgUniverse.getDiagram(svgURI); 356 401 if (diagram != null) … … 358 403 diagram.setDeviceViewport(new Rectangle(0, 0, preferredSize.width, preferredSize.height)); 359 404 } 360 405 361 406 } 362 407 catch (Exception e) … … 365 410 } 366 411 } 367 368 /** 369 * If this SVG document has a viewbox, if scaleToFit is set, will scale the viewbox to match the 370 * preferred size of this icon 371 * @deprecated 372 * @return 373 */ 374 public boolean isScaleToFit() 375 { 376 return autosize == AUTOSIZE_STRETCH; 377 } 378 379 /** 380 * @deprecated 381 * @return 382 */ 383 public void setScaleToFit(boolean scaleToFit) 384 { 385 setAutosize(AUTOSIZE_STRETCH); 386 // boolean old = this.scaleToFit; 387 // this.scaleToFit = scaleToFit; 388 // firePropertyChange("scaleToFit", old, scaleToFit); 389 } 390 412 391 413 public Dimension getPreferredSize() 392 414 { … … 400 422 } 401 423 } 402 424 403 425 return new Dimension(preferredSize); 404 426 } 405 427 406 428 public void setPreferredSize(Dimension preferredSize) 407 429 { 408 430 Dimension old = this.preferredSize; 409 431 this.preferredSize = preferredSize; 410 432 411 433 SVGDiagram diagram = svgUniverse.getDiagram(svgURI); 412 434 if (diagram != null) … … 414 436 diagram.setDeviceViewport(new Rectangle(0, 0, preferredSize.width, preferredSize.height)); 415 437 } 416 438 417 439 changes.firePropertyChange("preferredSize", old, preferredSize); 418 440 } 419 420 441 421 442 /** 422 443 * @return true if antiAliasing is turned on. 423 * @deprecated424 */425 public boolean getUseAntiAlias()426 {427 return getAntiAlias();428 }429 430 /**431 * @param antiAlias true to use antiAliasing.432 * @deprecated433 */434 public void setUseAntiAlias(boolean antiAlias)435 {436 setAntiAlias(antiAlias);437 }438 439 /**440 * @return true if antiAliasing is turned on.441 444 */ 442 445 public boolean getAntiAlias() … … 444 447 return antiAlias; 445 448 } 446 449 447 450 /** 448 451 * @param antiAlias true to use antiAliasing. … … 454 457 changes.firePropertyChange("antiAlias", old, antiAlias); 455 458 } 456 459 457 460 /** 458 461 * @return interpolation used in rescaling images … … 462 465 return interpolation; 463 466 } 464 467 465 468 /** 466 469 * @param interpolation Interpolation value used in rescaling images. … … 476 479 changes.firePropertyChange("interpolation", old, interpolation); 477 480 } 478 481 479 482 /** 480 483 * clipToViewbox will set a clip box equivilant to the SVG's viewbox before … … 485 488 return clipToViewbox; 486 489 } 487 490 488 491 public void setClipToViewbox(boolean clipToViewbox) 489 492 { … … 508 511 changes.firePropertyChange(PROP_AUTOSIZE, oldAutosize, autosize); 509 512 } 510 513 511 514 }
Note:
See TracChangeset
for help on using the changeset viewer.