Commits

shemnon  committed 0e3d57d

A better DSL for images, less verbose and more to the point

Switch from AWT toolkit to ImageIO

  • Participants
  • Parent commits 51a5bb4

Comments (0)

Files changed (2)

File gradle-javafx-plugin/src/main/groovy/org/bitbucket/shemnon/javafxplugin/tasks/JavaFXDeployTask.groovy

 import org.gradle.api.tasks.TaskAction
 import org.gradle.util.ConfigureUtil
 
-import java.awt.*
+import javax.imageio.ImageIO
 import java.awt.geom.AffineTransform
 import java.awt.image.BufferedImage
 import java.util.List
     Map<String, String> systemProperties = [:]
     List<String> arguments = []
 
-    // deplpy/info attributes
+    // deploy/info attributes
     String category
     String copyright
     String description
     String licenseType
     String vendor
-    List<IconInfo> icons = []
+    List<IconInfo> iconInfos = []
 
     // deploy/preferences attributes
     Boolean installSystemWide
         //java.lang.String preloader;
         //java.util.List<com.sun.javafx.tools.packager.Param> params;
         //java.util.List<com.sun.javafx.tools.packager.HtmlParam> htmlParams;
-        //java.util.List<java.lang.String> arguments;
         //boolean embedCertificates;
         //boolean isExtension;
         //boolean isSwingApp;
         //java.util.List<com.sun.javafx.tools.packager.DeployParams.Template> templates;
         //java.lang.String jrePlatform;
         //java.lang.String fxPlatform;
-        //java.util.List<java.lang.String> jvmargs;
-        //java.util.Map<java.lang.String,java.lang.String> properties;
         //java.lang.String fallbackApp;
-        //java.util.List<com.sun.javafx.tools.packager.DeployParams.Icon> icons;
 
 
         deployParams.width = getWidth()
 
         deployParams.updateMode = getUpdateMode()
         deployParams.offlineAllowed = getOfflineAllowed()
-        for (IconInfo ii : icons) {
+        for (IconInfo ii : iconInfos) {
             deployParams.addIcon(ii.href, ii.kind, ii.width, ii.height, ii.depth, ii.mode);
         }
         if (getCodebase() != null) {
     }
 
     def icon(Closure closure) {
-        icons.add(new IconInfo(closure))
+        iconInfos.add(new IconInfo(closure))
     }
 
+    def icons(Closure closure) {
+        Map m = [:]
+        ConfigureUtil.configure(closure, m)
+        m.each {k, v ->
+            if (v instanceof List) {
+                v.each {
+                    addIcon(k, it)
+                }
+            } else {
+                addIcon(k, v)
+            }
+        }
+    }
+
+    protected void addIcon(String kind, String href) {
+        File file = project.file(href)
+        if (!file.file) {
+            // try to resolve relative to output
+            file = new File(getResourcesDir(), href)
+        }
+        if (!file.file) return
+        BufferedImage image = ImageIO.read(file)
+
+        IconInfo ii = new IconInfo()
+        ii.image = image
+        ii.kind = kind
+        ii.href = href
+        if (href.contains('@2x')) {
+            ii.width = image.width / 2
+            ii.height = image.height / 2
+            ii.scale = 2
+        } else {
+            ii.width = image.width
+            ii.height = image.height
+            ii.scale = 1
+        }
+        iconInfos.add(ii)
+
+    }
 
     protected void processIcons(File destination) {
         if (Os.isFamily(Os.FAMILY_MAC)) {
         def dest = "$project.buildDir/icons/${kind}.iconset"
         project.mkdir(dest)
         boolean createIcon = false
-        for (IconInfo ii : icons) {
+        for (IconInfo ii : iconInfos) {
             if (kind == ii.kind) {
                 if (ii.width != ii.height) {
                     logger.info("Icon $ii.href for $ii.kind rejected from MacOSX bundling because it is not square: $ii.width x $ii.height")
     protected void processWindowsIcons(File destination) {
         processWindowsIco('shortcut',
                 new File(destination, "windows/${project.javafx.appName}.ico"))
-        processWidnowsBMP('setup-icon',
+        processWidnowsBMP('setup',
                 new File(destination, "windows/${project.javafx.appName}-setup-icon.bmp"))
     }
 
     protected void processWidnowsBMP(String kind, File destination) {
         boolean processed = false
-        for (IconInfo ii : icons) {
+        for (IconInfo ii : iconInfos) {
             if (kind == ii.kind) {
 
                 File file = project.file(ii.href)
                     continue;
                 }
 
-                Image icon = Toolkit.defaultToolkit.getImage(file.toURI().toURL())
+                BufferedImage icon = ii.image ?: ImageIO.read(file)
 
 
                 double scale = Math.min(Math.min(55.0 / icon.width, 58.0 / icon.height), 1.0)
 
     protected void processWindowsIco(String kind, File destination) {
         Map<Integer, BufferedImage> images = new TreeMap<Integer, BufferedImage>()
-        for (IconInfo ii : icons) {
+        for (IconInfo ii : iconInfos) {
             if (kind == ii.kind) {
                 File file = project.file(ii.href)
                 if (!file.exists()) {
                     continue;
                 }
 
-                Image icon = Toolkit.defaultToolkit.getImage(file.toURI().toURL())
+                BufferedImage icon = ii.image ?: ImageIO.read(file)
 
                 if (icon.width != icon.height) {
                     logger.info("Icon $ii.href for $ii.kind rejected from Windows bundling because it is not square: $icon.width x $icon.height")
 
     protected void processLinuxIcons(File destination) {
         File icon16, icon32
-        for (IconInfo ii : icons) {
+        for (IconInfo ii : iconInfos) {
             if ('shortcut' == ii.kind) {
 
                 File file = project.file(ii.href)
                     continue;
                 }
 
-                Image icon = Toolkit.defaultToolkit.getImage(file.toURI().toURL())
+                BufferedImage icon = ii.image ?: ImageIO.read(file)
 
                 if (icon.width == 32 && icon.height == 32) {
                     icon32 = file
     int depth = -1
     double scale = 1 // for retina
     RunMode mode = RunMode.ALL
+    protected BufferedImage image
 
 
     public IconInfo(Closure configure) {

File samples/FullyExpressed/build.gradle

     shortcut = true
 
     codebase = 'http://example.com/bogus/JNLP/Codebase'
+
+    // app icons
+    icons {
+        shortcut = ['shortcut-16.png', 'shortcut-32.png', 'shortcut-128.png', 'shortcut-256.png', 'shortcut-16@2x.png', 'shortcut-32@2x.png', 'shortcut-128@2x.png']
+        volume = 'javafx-icon.png'
+        setup = 'shortcut-32.png'
+    }
+
+    // jnlp icons, legacy
     icon {
         href = 'src/main/resources/javafx-icon.png'
-        kind = 'volume'
+        kind = 'splash'
         width = 128
         height = 128
     }
-
-    icon {
-        href = 'shortcut-16.png'
-        kind = 'shortcut'
-        width = 16
-        height = 16
-        scale = 1
-    }
     icon {
-        href = "$buildDir/resources/package/shortcut-16@2x.png"
-        kind = 'shortcut'
+        href = 'shortcut-32@2x.png'
+        kind = 'selected'
         width = 16
         height = 16
-        scale = 2
-    }
-    icon {
-        href = "$buildDir/resources/package/shortcut-32.png"
-        kind = 'shortcut'
-        width = 32
-        height = 32
-        scale = 1
-    }
-    icon {
-        href = "shortcut-32@2x.png"
-        kind = 'shortcut'
-        width = 32
-        height = 32
-        scale = 2
-    }
-    icon {
-        href = 'shortcut-128.png'
-        kind = 'shortcut'
-        width = 128
-        height = 128
-        scale = 1
-    }
-    icon {
-        href = "$buildDir/resources/package/shortcut-128@2x.png"
-        kind = 'shortcut'
-        width = 128
-        height = 128
-        scale = 2
-    }
-    icon {
-        href = "$buildDir/resources/package/shortcut-256.png"
-        kind = 'shortcut'
-        width = 256
-        height = 256
-        scale = 1
-    }
-    icon {
-        href = "$buildDir/resources/package/shortcut-32.png"
-        kind = 'setup-icon'
-        width = 32
-        height = 32
         scale = 1
     }
 }