Ticket #9995: hidpi_patch_1_images.patch
| File hidpi_patch_1_images.patch, 36.1 KB (added by , 10 years ago) |
|---|
-
gui/dialogs/NotesDialog.java
61 61 public static final ImageIcon ICON_OPEN = ImageProvider.get("dialogs/notes", "note_open"); 62 62 /** 16x16 icon for unresolved notes */ 63 63 public static final ImageIcon ICON_OPEN_SMALL = 64 new Image Icon(ICON_OPEN.getImage().getScaledInstance(ICON_SMALL_SIZE, ICON_SMALL_SIZE, Image.SCALE_SMOOTH));64 new ImageProvider("dialogs/notes", "note_open").setSize(ICON_SMALL_SIZE, ICON_SMALL_SIZE).get(); 65 65 /** 24x24 icon for resolved notes */ 66 66 public static final ImageIcon ICON_CLOSED = ImageProvider.get("dialogs/notes", "note_closed"); 67 67 /** 16x16 icon for resolved notes */ 68 68 public static final ImageIcon ICON_CLOSED_SMALL = 69 new Image Icon(ICON_CLOSED.getImage().getScaledInstance(ICON_SMALL_SIZE, ICON_SMALL_SIZE, Image.SCALE_SMOOTH));69 new ImageProvider("dialogs/notes", "note_closed").setSize(ICON_SMALL_SIZE, ICON_SMALL_SIZE).get(); 70 70 /** 24x24 icon for new notes */ 71 71 public static final ImageIcon ICON_NEW = ImageProvider.get("dialogs/notes", "note_new"); 72 72 /** 16x16 icon for new notes */ 73 73 public static final ImageIcon ICON_NEW_SMALL = 74 new Image Icon(ICON_NEW.getImage().getScaledInstance(ICON_SMALL_SIZE, ICON_SMALL_SIZE, Image.SCALE_SMOOTH));74 new ImageProvider("dialogs/notes", "note_new").setSize(ICON_SMALL_SIZE, ICON_SMALL_SIZE).get(); 75 75 /** Icon for note comments */ 76 76 public static final ImageIcon ICON_COMMENT = ImageProvider.get("dialogs/notes", "note_comment"); 77 77 -
gui/dialogs/ToggleDialog.java
33 33 import javax.swing.AbstractAction; 34 34 import javax.swing.BorderFactory; 35 35 import javax.swing.ButtonGroup; 36 import javax.swing.ImageIcon; 36 37 import javax.swing.JButton; 37 38 import javax.swing.JCheckBoxMenuItem; 38 39 import javax.swing.JComponent; … … 503 504 add(lblMinimized); 504 505 505 506 // scale down the dialog icon 506 lblTitle = new JLabel("", new ImageProvider("dialogs", iconName).setWidth(16).get(), JLabel.TRAILING); 507 ImageIcon icon = new ImageProvider("dialogs", iconName).setSize(ImageProvider.ImageSizes.SMALLICON).get(); 508 lblTitle = new JLabel("", icon, JLabel.TRAILING); 507 509 lblTitle.setIconTextGap(8); 508 510 509 511 JPanel conceal = new JPanel(); … … 540 542 541 543 // show the pref button if applicable 542 544 if (preferenceClass != null) { 543 JButton pref = new JButton(new ImageProvider("preference").set Width(16).get());545 JButton pref = new JButton(new ImageProvider("preference").setSize(ImageProvider.ImageSizes.SMALLICON).get()); 544 546 pref.setToolTipText(tr("Open preferences for this panel")); 545 547 pref.setBorder(BorderFactory.createEmptyBorder()); 546 548 pref.addActionListener( -
gui/io/SaveLayersDialog.java
381 381 } 382 382 383 383 final class SaveAndProceedAction extends AbstractAction implements PropertyChangeListener { 384 private static final int ICON_SIZE = 24;385 384 private static final String BASE_ICON = "BASE_ICON"; 386 385 private final transient Image save = ImageProvider.get("save").getImage(); 387 386 private final transient Image upld = ImageProvider.get("upload").getImage(); 387 private final transient int ICON_SIZE = save.getWidth(null); 388 388 private final transient Image saveDis = new BufferedImage(ICON_SIZE, ICON_SIZE, BufferedImage.TYPE_4BYTE_ABGR); 389 389 private final transient Image upldDis = new BufferedImage(ICON_SIZE, ICON_SIZE, BufferedImage.TYPE_4BYTE_ABGR); 390 390 -
gui/mappaint/MapPaintStyles.java
146 146 */ 147 147 public static ImageIcon getIcon(IconReference ref, int width, int height) { 148 148 final String namespace = ref.source.getPrefName(); 149 ImageIcon i = getIconProvider(ref, false).set Width(width).setHeight(height).get();149 ImageIcon i = getIconProvider(ref, false).setSize(width, height).get(); 150 150 if (i == null) { 151 151 Main.warn("Mappaint style \""+namespace+"\" ("+ref.source.getDisplayString()+") icon \"" + ref.iconName + "\" not found."); 152 152 return null; -
gui/preferences/PreferenceTabbedPane.java
509 509 if (expert || !tps.isExpert()) { 510 510 // Get icon 511 511 String iconName = tps.getIconName(); 512 ImageIcon icon = iconName != null && !iconName.isEmpty() ? ImageProvider.get("preferences", iconName) :null;513 // See #6985 - Force icons to be 48x48 pixels 514 if (icon != null && (icon.getIconHeight() != 48 || icon.getIconWidth() != 48)) {515 icon = new Image Icon(icon.getImage().getScaledInstance(48, 48, Image.SCALE_DEFAULT));512 ImageIcon icon = null; 513 514 if(iconName != null && !iconName.isEmpty()) { 515 icon = new ImageProvider("preferences", iconName).setSize(ImageProvider.ImageSizes.SETTINGS_TAB).get(); 516 516 } 517 517 if (settingsInitialized.contains(tps)) { 518 518 // If it has been initialized, add corresponding tab(s) -
gui/SideButton.java
25 25 * @since 744 26 26 */ 27 27 public class SideButton extends JButton implements Destroyable { 28 private static final int iconHeight = ImageProvider.ImageSizes.SIDEBUTTON.get ImageSize();28 private static final int iconHeight = ImageProvider.ImageSizes.SIDEBUTTON.getVirtualHeight(); 29 29 30 30 private transient PropertyChangeListener propertyChangeListener; 31 31 … … 60 60 */ 61 61 public SideButton(Action action, String imagename) { 62 62 super(action); 63 setIcon(getScaledImage(ImageProvider.get("dialogs", imagename).getImage())); 63 Icon icon = new ImageProvider("dialogs", imagename).setHeight(iconHeight).get(); 64 setIcon(icon); 64 65 doStyle(); 65 66 } 66 67 … … 91 92 */ 92 93 private static ImageIcon getScaledImage(Image im) { 93 94 int newWidth = im.getWidth(null) * iconHeight / im.getHeight(null); 94 return new ImageIcon(im.getScaledInstance(newWidth, iconHeight, Image.SCALE_SMOOTH));95 return ImageProvider.getScaledIcon(im, newWidth, iconHeight); 95 96 } 96 97 97 98 private void doStyle() { -
gui/SplashScreen.java
8 8 import java.awt.Dimension; 9 9 import java.awt.GridBagConstraints; 10 10 import java.awt.GridBagLayout; 11 import java.awt.Image;12 11 import java.awt.Insets; 13 12 import java.awt.event.MouseAdapter; 14 13 import java.awt.event.MouseEvent; … … 17 16 import java.util.concurrent.CopyOnWriteArrayList; 18 17 19 18 import javax.swing.BorderFactory; 20 import javax.swing.ImageIcon;21 19 import javax.swing.JFrame; 22 20 import javax.swing.JLabel; 23 21 import javax.swing.JPanel; … … 69 67 contentPane.add(innerContentPane); 70 68 71 69 // Add the logo 72 JLabel logo = new JLabel(new Image Icon(ImageProvider.get("logo.svg").getImage().getScaledInstance(128, 129, Image.SCALE_SMOOTH)));70 JLabel logo = new JLabel(new ImageProvider("logo.svg").setSize(ImageProvider.ImageSizes.SPLASH_LOGO).get()); 73 71 GridBagConstraints gbc = new GridBagConstraints(); 74 72 gbc.gridheight = 2; 75 73 gbc.insets = new Insets(0, 0, 0, 70); -
gui/widgets/TextContextualPopupMenu.java
179 179 JMenuItem mi = new JMenuItem(action); 180 180 mi.setText(label); 181 181 if (iconName != null && Main.pref.getBoolean("text.popupmenu.useicons", true)) { 182 ImageIcon icon = new ImageProvider(iconName).set Width(16).get();182 ImageIcon icon = new ImageProvider(iconName).setSize(ImageProvider.ImageSizes.SMALLICON).get(); 183 183 if (icon != null) { 184 184 mi.setIcon(icon); 185 185 } -
io/session/GenericSessionExporter.java
73 73 * Constructs a new {@code LayerSaveAction}. 74 74 */ 75 75 LayerSaveAction() { 76 putValue(SMALL_ICON, new ImageProvider("save").set Width(16).get());76 putValue(SMALL_ICON, new ImageProvider("save").setSize(ImageProvider.ImageSizes.SMALLICON).get()); 77 77 putValue(SHORT_DESCRIPTION, ((AbstractModifiableLayer) layer).requiresSaveToFile() ? 78 78 tr("Layer contains unsaved data - save to file.") : 79 79 tr("Layer does not contain unsaved data.")); -
plugins/PluginInformation.java
3 3 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt. Image;6 import java.awt.Dimension; 7 7 import java.awt.image.BufferedImage; 8 8 import java.io.File; 9 9 import java.io.FileInputStream; … … 28 28 29 29 import org.openstreetmap.josm.Main; 30 30 import org.openstreetmap.josm.data.Version; 31 import org.openstreetmap.josm.gui.util.GuiSizesHelper; 31 32 import org.openstreetmap.josm.tools.ImageProvider; 32 33 import org.openstreetmap.josm.tools.LanguageInfo; 33 34 import org.openstreetmap.josm.tools.Utils; … … 84 85 /** All manifest attributes. */ 85 86 public final Map<String, String> attr = new TreeMap<>(); 86 87 87 private static final ImageIcon emptyIcon = new ImageIcon(new BufferedImage(24, 24, BufferedImage.TYPE_INT_ARGB)); 88 private static final Dimension iconRealSize = 89 GuiSizesHelper.getDimensionDpiAdjusted(ImageProvider.ImageSizes.LARGEICON.getImageDimension()); 90 private static final ImageIcon emptyIcon = 91 new ImageIcon(new BufferedImage(iconRealSize.width, iconRealSize.height, BufferedImage.TYPE_INT_ARGB)); 88 92 89 93 /** 90 94 * Creates a plugin information object by reading the plugin information from … … 243 247 if (iconPath != null) { 244 248 if (file != null) { 245 249 // extract icon from the plugin jar file 246 icon = new ImageProvider(iconPath).setArchive(file).setMax Width(24).setMaxHeight(24).setOptional(true).get();250 icon = new ImageProvider(iconPath).setArchive(file).setMaxSize(ImageProvider.ImageSizes.LARGEICON).setOptional(true).get(); 247 251 } else if (iconPath.startsWith("data:")) { 248 icon = new ImageProvider(iconPath).setMax Width(24).setMaxHeight(24).setOptional(true).get();252 icon = new ImageProvider(iconPath).setMaxSize(ImageProvider.ImageSizes.LARGEICON).setOptional(true).get(); 249 253 } 250 254 } 251 255 canloadatruntime = Boolean.parseBoolean(attr.getValue("Plugin-Canloadatruntime")); … … 499 503 public ImageIcon getScaledIcon() { 500 504 if (icon == null) 501 505 return emptyIcon; 502 return new ImageIcon(icon.getImage().getScaledInstance(24, 24, Image.SCALE_SMOOTH));506 return ImageProvider.getScaledIcon(icon, ImageProvider.ImageSizes.LARGEICON); 503 507 } 504 508 505 509 @Override -
tools/ImageProvider.java
68 68 import org.openstreetmap.josm.gui.mappaint.styleelement.StyleElement; 69 69 import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset; 70 70 import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets; 71 import org.openstreetmap.josm.gui.util.GuiSizesHelper; 71 72 import org.openstreetmap.josm.io.CachedFile; 72 73 import org.openstreetmap.josm.plugins.PluginHandler; 73 74 import org.w3c.dom.Element; … … 144 145 /** cursor icon size */ 145 146 CURSOR(Main.pref.getInteger("iconsize.cursor", 32)), 146 147 /** cursor overlay icon size */ 147 CURSOROVERLAY(CURSOR .imageSize),148 CURSOROVERLAY(CURSOR), 148 149 /** menu icon size */ 149 MENU(SMALLICON .imageSize),150 MENU(SMALLICON), 150 151 /** menu icon size in popup menus 151 152 * @since 8323 152 153 */ 153 POPUPMENU(LARGEICON .imageSize),154 POPUPMENU(LARGEICON), 154 155 /** Layer list icon size 155 156 * @since 8323 156 157 */ … … 158 159 /** Toolbar button icon size 159 160 * @since 9253 160 161 */ 161 TOOLBAR(LARGEICON .imageSize),162 TOOLBAR(LARGEICON), 162 163 /** Side button maximum height 163 164 * @since 9253 164 165 */ 165 166 SIDEBUTTON(Main.pref.getInteger("iconsize.sidebutton", 20)), 167 /** Side button maximum height 168 * @since 9253 169 */ 170 SETTINGS_TAB(Main.pref.getInteger("iconsize.settingstab", 48)), 166 171 /** 167 172 * The default image size 168 173 * @since 9705 169 174 */ 170 DEFAULT(Main.pref.getInteger("iconsize.default", 24)) ;175 DEFAULT(Main.pref.getInteger("iconsize.default", 24)), 171 176 172 private final int imageSize; 177 /** 178 * Splash dialog logo size 179 * @since XXXX 180 */ 181 SPLASH_LOGO(128, 129), 173 182 183 /** 184 * About dialog logo size 185 * @since XXXX 186 */ 187 ABOUT_LOGO(256, 258); 188 189 private final int virtualWidth; 190 private final int virtualHeight; 191 174 192 ImageSizes(int imageSize) { 175 this.imageSize = imageSize; 193 this.virtualWidth = imageSize; 194 this.virtualHeight = imageSize; 176 195 } 177 196 197 ImageSizes(int width, int height) { 198 this.virtualWidth = width; 199 this.virtualHeight = height; 200 } 201 202 ImageSizes(ImageSizes that) { 203 this.virtualWidth = that.virtualWidth; 204 this.virtualHeight = that.virtualHeight; 205 } 206 178 207 /** 179 * Returns the image size in pixels 180 * @return the image size in pixels 208 * Returns the image width in virtual pixels 209 * @return the image width in virtual pixels 210 */ 211 public int getVirtualWidth() { 212 return virtualWidth; 213 } 214 215 /** 216 * Returns the image height in virtual pixels 217 * @return the image height in virtual pixels 181 218 * @since 9705 182 219 */ 183 public int get ImageSize() {184 return imageSize;220 public int getVirtualHeight() { 221 return virtualHeight; 185 222 } 186 223 187 224 /** … … 190 227 * @since 9705 191 228 */ 192 229 public Dimension getImageDimension() { 193 return new Dimension( imageSize, imageSize);230 return new Dimension(virtualWidth, virtualHeight); 194 231 } 195 232 } 196 233 … … 218 255 protected File archive; 219 256 /** directory inside the archive */ 220 257 protected String inArchiveDir; 221 /** width of the resulting image, -1 when original image data should be used */222 protected int width = -1;223 /** height of the resulting image, -1 when original image data should be used */224 protected int height = -1;225 /** maximum width of the resulting image, -1 for no restriction */226 protected int maxWidth = -1;227 /** maximum height of the resulting image, -1 for no restriction */228 protected int maxHeight = -1;258 /** virtual width of the resulting image, -1 when original image data should be used */ 259 protected int virtualWidth = -1; 260 /** virtual height of the resulting image, -1 when original image data should be used */ 261 protected int virtualHeight = -1; 262 /** virtual maximum width of the resulting image, -1 for no restriction */ 263 protected int virtualMaxWidth = -1; 264 /** virtual maximum height of the resulting image, -1 for no restriction */ 265 protected int virtualMaxHeight = -1; 229 266 /** In case of errors do not throw exception but return <code>null</code> for missing image */ 230 267 protected boolean optional; 231 268 /** <code>true</code> if warnings should be suppressed */ … … 305 342 this.name = image.name; 306 343 this.archive = image.archive; 307 344 this.inArchiveDir = image.inArchiveDir; 308 this. width = image.width;309 this. height = image.height;310 this. maxWidth = image.maxWidth;311 this. maxHeight = image.maxHeight;345 this.virtualWidth = image.virtualWidth; 346 this.virtualHeight = image.virtualHeight; 347 this.virtualMaxWidth = image.virtualMaxWidth; 348 this.virtualMaxHeight = image.virtualMaxHeight; 312 349 this.optional = image.optional; 313 350 this.suppressWarnings = image.suppressWarnings; 314 351 this.additionalClassLoaders = image.additionalClassLoaders; … … 388 425 * @return the current object, for convenience 389 426 */ 390 427 public ImageProvider setSize(Dimension size) { 391 this. width = size.width;392 this. height = size.height;428 this.virtualWidth = size.width; 429 this.virtualHeight = size.height; 393 430 return this; 394 431 } 395 432 … … 405 442 return setSize(size.getImageDimension()); 406 443 } 407 444 445 public ImageProvider setSize(int width, int height) { 446 this.virtualWidth = width; 447 this.virtualHeight = height; 448 return this; 449 } 450 408 451 /** 409 452 * Set image width 410 453 * @param width final width of the image … … 412 455 * @see #setSize 413 456 */ 414 457 public ImageProvider setWidth(int width) { 415 this. width = width;458 this.virtualWidth = width; 416 459 return this; 417 460 } 418 461 … … 423 466 * @see #setSize 424 467 */ 425 468 public ImageProvider setHeight(int height) { 426 this. height = height;469 this.virtualHeight = height; 427 470 return this; 428 471 } 429 472 … … 438 481 * @return the current object, for convenience 439 482 */ 440 483 public ImageProvider setMaxSize(Dimension maxSize) { 441 this. maxWidth = maxSize.width;442 this. maxHeight = maxSize.height;484 this.virtualMaxWidth = maxSize.width; 485 this.virtualMaxHeight = maxSize.height; 443 486 return this; 444 487 } 445 488 … … 457 500 * @see #setMaxSize(Dimension) 458 501 */ 459 502 public ImageProvider resetMaxSize(Dimension maxSize) { 460 if (this. maxWidth == -1 || maxSize.width < this.maxWidth) {461 this. maxWidth = maxSize.width;503 if (this.virtualMaxWidth == -1 || maxSize.width < this.virtualMaxWidth) { 504 this.virtualMaxWidth = maxSize.width; 462 505 } 463 if (this. maxHeight == -1 || maxSize.height < this.maxHeight) {464 this. maxHeight = maxSize.height;506 if (this.virtualMaxHeight == -1 || maxSize.height < this.virtualMaxHeight) { 507 this.virtualMaxHeight = maxSize.height; 465 508 } 466 509 return this; 467 510 } … … 497 540 * @see #setMaxSize 498 541 */ 499 542 public ImageProvider setMaxWidth(int maxWidth) { 500 this. maxWidth = maxWidth;543 this.virtualMaxWidth = maxWidth; 501 544 return this; 502 545 } 503 546 … … 508 551 * @see #setMaxSize 509 552 */ 510 553 public ImageProvider setMaxHeight(int maxHeight) { 511 this. maxHeight = maxHeight;554 this.virtualMaxHeight = maxHeight; 512 555 return this; 513 556 } 514 557 … … 556 599 ImageResource ir = getResource(); 557 600 if (ir == null) 558 601 return null; 559 if ( maxWidth != -1 || maxHeight != -1)560 return ir.getImageIconBounded(new Dimension( maxWidth, maxHeight));602 if (virtualMaxWidth != -1 || virtualMaxHeight != -1) 603 return ir.getImageIconBounded(new Dimension(virtualMaxWidth, virtualMaxHeight)); 561 604 else 562 return ir.getImageIcon(new Dimension( width, height));605 return ir.getImageIcon(new Dimension(virtualWidth, virtualHeight)); 563 606 } 564 607 565 608 /** … … 691 734 } 692 735 693 736 /** 737 * Scale image to imgSize dimensions. This method resizes image with respect to actual screen DPI. 738 * 739 * @param icon image to be resized 740 * @param imgSize target size of image 741 * @return new scaled image in real dimensions 742 */ 743 public static ImageIcon getScaledIcon(ImageIcon icon, ImageSizes imgSize) { 744 Dimension size = imgSize.getImageDimension(); 745 size = GuiSizesHelper.getDimensionDpiAdjusted(size); 746 return new ImageIcon(icon.getImage().getScaledInstance(size.width, size.height, Image.SCALE_SMOOTH)); 747 } 748 749 /** 750 * Scale image to virtual dimensions. This util method is used to hide real sizes calculations. 751 * All other classes should use this method to resize images. 752 * 753 * @param im image to be resized 754 * @param virtualWidth target width of image in virtual pixels 755 * @param virtualHeight target height of image in virtual pixels 756 * @return new scaled image in real dimensions 757 */ 758 public static ImageIcon getScaledIcon(Image im, int virtualWidth, int virtualHeight) { 759 int realWidth = GuiSizesHelper.getSizeDpiAdjusted(virtualWidth); 760 int realHeight = GuiSizesHelper.getSizeDpiAdjusted(virtualHeight); 761 762 return new ImageIcon(im.getScaledInstance(realWidth, realHeight, Image.SCALE_SMOOTH)); 763 } 764 765 /** 694 766 * {@code data:[<mediatype>][;base64],<data>} 695 767 * @see <a href="http://tools.ietf.org/html/rfc2397">RFC2397</a> 696 768 */ … … 1332 1404 NodeElement nodeStyle = (NodeElement) style; 1333 1405 MapImage icon = nodeStyle.mapImage; 1334 1406 if (icon != null) { 1335 int background Width = iconSize.width;1336 int background Height = iconSize.height;1337 int icon Width = icon.getWidth();1338 int icon Height = icon.getHeight();1339 BufferedImage image = new BufferedImage(background Width, backgroundHeight,1407 int backgroundRealWidth = GuiSizesHelper.getSizeDpiAdjusted(iconSize.width); 1408 int backgroundRealHeight = GuiSizesHelper.getSizeDpiAdjusted(iconSize.height); 1409 int iconRealWidth = icon.getWidth(); 1410 int iconRealHeight = icon.getHeight(); 1411 BufferedImage image = new BufferedImage(backgroundRealWidth, backgroundRealHeight, 1340 1412 BufferedImage.TYPE_INT_ARGB); 1341 double scaleFactor = Math.min(background Width / (double) iconWidth, backgroundHeight1342 / (double) icon Height);1413 double scaleFactor = Math.min(backgroundRealWidth / (double) iconRealWidth, backgroundRealHeight 1414 / (double) iconRealHeight); 1343 1415 BufferedImage iconImage = icon.getImage(false); 1344 1416 Image scaledIcon; 1345 1417 final int scaledWidth; … … 1346 1418 final int scaledHeight; 1347 1419 if (scaleFactor < 1) { 1348 1420 // Scale icon such that it fits on background. 1349 scaledWidth = (int) (icon Width * scaleFactor);1350 scaledHeight = (int) (icon Height * scaleFactor);1421 scaledWidth = (int) (iconRealWidth * scaleFactor); 1422 scaledHeight = (int) (iconRealHeight * scaleFactor); 1351 1423 scaledIcon = iconImage.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH); 1352 1424 } else { 1353 1425 // Use original size, don't upscale. 1354 scaledWidth = icon Width;1355 scaledHeight = icon Height;1426 scaledWidth = iconRealWidth; 1427 scaledHeight = iconRealHeight; 1356 1428 scaledIcon = iconImage; 1357 1429 } 1358 image.getGraphics().drawImage(scaledIcon, (background Width - scaledWidth) / 2,1359 (background Height - scaledHeight) / 2, null);1430 image.getGraphics().drawImage(scaledIcon, (backgroundRealWidth - scaledWidth) / 2, 1431 (backgroundRealHeight - scaledHeight) / 2, null); 1360 1432 1361 1433 return new ImageIcon(image); 1362 1434 } … … 1393 1465 * @return an image from the given SVG data at the desired dimension. 1394 1466 */ 1395 1467 public static BufferedImage createImageFromSvg(SVGDiagram svg, Dimension dim) { 1396 float realWidth = svg.getWidth();1397 float realHeight = svg.getHeight();1398 int width = Math.round(realWidth);1399 int height = Math.round(realHeight);1400 Double scaleX = null, scaleY = null;1468 float sourceWidth = svg.getWidth(); 1469 float sourceHeight = svg.getHeight(); 1470 int realWidth = Math.round(GuiSizesHelper.getSizeDpiAdjusted(sourceWidth)); 1471 int realHeight = Math.round(GuiSizesHelper.getSizeDpiAdjusted(sourceHeight)); 1472 Double scaleX, scaleY; 1401 1473 if (dim.width != -1) { 1402 width = dim.width;1403 scaleX = (double) width / realWidth;1474 realWidth = dim.width; 1475 scaleX = (double) realWidth / sourceWidth; 1404 1476 if (dim.height == -1) { 1405 1477 scaleY = scaleX; 1406 height = (int) Math.round(realHeight * scaleY);1478 realHeight = (int) Math.round(sourceHeight * scaleY); 1407 1479 } else { 1408 height = dim.height;1409 scaleY = (double) height / realHeight;1480 realHeight = dim.height; 1481 scaleY = (double) realHeight / sourceHeight; 1410 1482 } 1411 1483 } else if (dim.height != -1) { 1412 height = dim.height;1413 scaleX = scaleY = (double) height / realHeight;1414 width = (int) Math.round(realWidth * scaleX);1484 realHeight = dim.height; 1485 scaleX = scaleY = (double) realHeight / sourceHeight; 1486 realWidth = (int) Math.round(sourceWidth * scaleX); 1415 1487 } 1416 if (width == 0 || height == 0) { 1488 else { 1489 scaleX = scaleY = (double) realHeight / sourceHeight; 1490 } 1491 1492 if (realWidth == 0 || realHeight == 0) { 1417 1493 return null; 1418 1494 } 1419 BufferedImage img = new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB);1495 BufferedImage img = new BufferedImage(realWidth, realHeight, BufferedImage.TYPE_INT_ARGB); 1420 1496 Graphics2D g = img.createGraphics(); 1421 g.setClip(0, 0, width, height); 1422 if (scaleX != null && scaleY != null) { 1423 g.scale(scaleX, scaleY); 1424 } 1497 g.setClip(0, 0, realWidth, realHeight); 1498 g.scale(scaleX, scaleY); 1425 1499 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 1426 1500 try { 1427 1501 synchronized (getSvgUniverse()) { -
tools/ImageResource.java
13 13 import javax.swing.ImageIcon; 14 14 15 15 import com.kitfox.svg.SVGDiagram; 16 import org.openstreetmap.josm.gui.util.GuiSizesHelper; 16 17 17 18 /** 18 19 * Holds data for one particular image. … … 49 50 public ImageResource(Image img) { 50 51 CheckParameterUtil.ensureParameterNotNull(img); 51 52 this.baseImage = img; 53 54 img = scaleBaseImageIfNeeded(img); 55 52 56 imgCache.put(DEFAULT_DIMENSION, img); 53 57 } 54 58 59 /** Scale image according to screen DPI if needed. 60 * 61 * @param img an image loaded from file (it's width and height are virtual pixels) 62 * @return original img if virtual size is the same as real size or new image resized to real pixels 63 */ 64 private static Image scaleBaseImageIfNeeded(Image img) { 65 int imgWidth = img.getWidth(null); 66 int imgHeight = img.getHeight(null); 67 int realWidth = GuiSizesHelper.getSizeDpiAdjusted(imgWidth); 68 int realHeight = GuiSizesHelper.getSizeDpiAdjusted(imgHeight); 69 if (realWidth != -1 && realHeight != -1 && imgWidth != realWidth && imgHeight != realHeight) { 70 Image realImage = img.getScaledInstance(realWidth, realHeight, Image.SCALE_SMOOTH); 71 BufferedImage bimg = new BufferedImage(realWidth, realHeight, BufferedImage.TYPE_INT_ARGB); 72 bimg.getGraphics().drawImage(realImage, 0, 0, null); 73 img = bimg; 74 } 75 return img; 76 } 77 55 78 /** 56 79 * Constructs a new {@code ImageResource} from SVG data. 57 80 * @param svg SVG data … … 87 110 * @since 7693 88 111 */ 89 112 public void getImageIcon(AbstractAction a) { 90 ImageIcon icon = getImageIconBounded(ImageProvider.ImageSizes.SMALLICON.getImageDimension()); 113 Dimension iconDimension = ImageProvider.ImageSizes.SMALLICON.getImageDimension(); 114 ImageIcon icon = getImageIconBounded(iconDimension); 91 115 a.putValue(Action.SMALL_ICON, icon); 92 icon = getImageIconBounded(ImageProvider.ImageSizes.LARGEICON.getImageDimension()); 116 117 iconDimension = ImageProvider.ImageSizes.LARGEICON.getImageDimension(); 118 icon = getImageIconBounded(iconDimension); 93 119 a.putValue(Action.LARGE_ICON_KEY, icon); 94 120 } 95 121 … … 108 134 return new ImageIcon(img); 109 135 } 110 136 if (svg != null) { 111 BufferedImage bimg = ImageProvider.createImageFromSvg(svg, dim); 137 Dimension realDim = GuiSizesHelper.getDimensionDpiAdjusted(dim); 138 BufferedImage bimg = ImageProvider.createImageFromSvg(svg, realDim); 112 139 if (bimg == null) { 113 140 return null; 114 141 } … … 122 149 } else { 123 150 if (baseImage == null) throw new AssertionError(); 124 151 125 int width = dim.width;126 int height = dim.height;152 int realWidth = GuiSizesHelper.getSizeDpiAdjusted(dim.width); 153 int realHeight = GuiSizesHelper.getSizeDpiAdjusted(dim.height); 127 154 ImageIcon icon = new ImageIcon(baseImage); 128 if ( width == -1 && height == -1) {129 width = icon.getIconWidth();130 height = icon.getIconHeight();131 } else if ( width == -1) {132 width = Math.max(1, icon.getIconWidth() * height / icon.getIconHeight());133 } else if ( height == -1) {134 height = Math.max(1, icon.getIconHeight() * width / icon.getIconWidth());155 if (realWidth == -1 && realHeight == -1) { 156 realWidth = GuiSizesHelper.getSizeDpiAdjusted(icon.getIconWidth()); 157 realHeight = GuiSizesHelper.getSizeDpiAdjusted(icon.getIconHeight()); 158 } else if (realWidth == -1) { 159 realWidth = Math.max(1, icon.getIconWidth() * realHeight / icon.getIconHeight()); 160 } else if (realHeight == -1) { 161 realHeight = Math.max(1, icon.getIconHeight() * realWidth / icon.getIconWidth()); 135 162 } 136 Image i = icon.getImage().getScaledInstance( width, height, Image.SCALE_SMOOTH);137 BufferedImage bimg = new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB);163 Image i = icon.getImage().getScaledInstance(realWidth, realHeight, Image.SCALE_SMOOTH); 164 BufferedImage bimg = new BufferedImage(realWidth, realHeight, BufferedImage.TYPE_INT_ARGB); 138 165 bimg.getGraphics().drawImage(i, 0, 0, null); 139 166 if (overlayInfo != null) { 140 167 for (ImageOverlay o : overlayInfo) { … … 157 184 public ImageIcon getImageIconBounded(Dimension maxSize) { 158 185 if (maxSize.width < -1 || maxSize.width == 0 || maxSize.height < -1 || maxSize.height == 0) 159 186 throw new IllegalArgumentException(maxSize+" is invalid"); 160 float realWidth;161 float realHeight;187 float sourceWidth; 188 float sourceHeight; 162 189 int maxWidth = maxSize.width; 163 190 int maxHeight = maxSize.height; 164 191 if (svg != null) { 165 realWidth = svg.getWidth();166 realHeight = svg.getHeight();192 sourceWidth = svg.getWidth(); 193 sourceHeight = svg.getHeight(); 167 194 } else { 168 195 if (baseImage == null) throw new AssertionError(); 169 196 ImageIcon icon = new ImageIcon(baseImage); 170 realWidth = icon.getIconWidth();171 realHeight = icon.getIconHeight();172 if ( realWidth <= maxWidth) {197 sourceWidth = icon.getIconWidth(); 198 sourceHeight = icon.getIconHeight(); 199 if (sourceWidth <= maxWidth) { 173 200 maxWidth = -1; 174 201 } 175 if ( realHeight <= maxHeight) {202 if (sourceHeight <= maxHeight) { 176 203 maxHeight = -1; 177 204 } 178 205 } … … 183 210 return getImageIcon(new Dimension(-1, maxHeight)); 184 211 else if (maxHeight == -1) 185 212 return getImageIcon(new Dimension(maxWidth, -1)); 186 else if ( realWidth / maxWidth > realHeight / maxHeight)213 else if (sourceWidth / maxWidth > sourceHeight / maxHeight) 187 214 return getImageIcon(new Dimension(maxWidth, -1)); 188 215 else 189 216 return getImageIcon(new Dimension(-1, maxHeight)); -
gui/util/GuiSizesHelper.java
1 package org.openstreetmap.josm.gui.util; 2 3 import java.awt.*; 4 5 public class GuiSizesHelper { 6 private static int screenDPI = -1; 7 8 private static int getScreenDPI() { 9 if (screenDPI == -1) { 10 synchronized (GuiHelper.class) { 11 if (screenDPI == -1) { 12 screenDPI = Toolkit.getDefaultToolkit().getScreenResolution(); 13 } 14 } 15 } 16 return screenDPI; 17 } 18 19 /** 20 * Returns coefficient of monitor pixel density. All hardcoded sizes must be multiplied by this value. 21 * 22 * @return float value. 1 - means standard monitor, 2 and high - "retina" display. 23 * @since XXX 24 */ 25 public static float getPixelDensity() { 26 int pixelPerInch = getScreenDPI(); 27 return (float) (pixelPerInch / 96.); 28 } 29 30 public static boolean isHiDPI() { 31 return getPixelDensity() >= 2f; 32 } 33 34 public static int getSizeDpiAdjusted(int size) { 35 if (size <= 0) return size; 36 int pixelPerInch = getScreenDPI(); 37 return size * pixelPerInch / 96; 38 } 39 40 public static float getSizeDpiAdjusted(float size) { 41 if (size <= 0f) return size; 42 int pixelPerInch = getScreenDPI(); 43 return size * pixelPerInch / 96; 44 } 45 46 public static double getSizeDpiAdjusted(double size) { 47 if (size <= 0d) return size; 48 int pixelPerInch = getScreenDPI(); 49 return size * pixelPerInch / 96; 50 } 51 52 public static Dimension getDimensionDpiAdjusted(Dimension dim) { 53 int pixelPerInch = getScreenDPI(); 54 int width = dim.width, height = dim.height; 55 if (dim.width > 0) { 56 width = dim.width * pixelPerInch / 96; 57 } 58 59 if (dim.height > 0) { 60 height = dim.height * pixelPerInch / 96; 61 } 62 63 return new Dimension(width, height); 64 } 65 }
