Skip to content

Commit

Permalink
rename to svg2png
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Sterl committed Feb 15, 2016
1 parent 301f2e6 commit d0192c1
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 58 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.sterl.encoding-util</groupId>
<artifactId>encoding-util</artifactId>
<groupId>org.sterl.svg2png</groupId>
<artifactId>svg2png</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<url>https://github.com/puel/SvgToPng</url>
Expand Down Expand Up @@ -100,7 +100,7 @@
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>org.sterl.encoding.Main</Main-Class>
<Main-Class>org.sterl.svg2png.Main</Main-Class>
<X-Compile-Source-JDK>${java.version}</X-Compile-Source-JDK>
<X-Compile-Target-JDK>${java.version}</X-Compile-Target-JDK>
</manifestEntries>
Expand Down Expand Up @@ -138,7 +138,7 @@

<!-- (optional) name for binary executable, if not set will just -->
<!-- make the regular jar artifact executable -->
<programFile>svgtopng</programFile>
<programFile>svg2png</programFile>

<!-- (optional) name for a file that will define what script
gets -->
Expand Down
18 changes: 9 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@ Manage your Icons in SVG and generate the needed PNG into your projects as neede

## Download & Requirements

* [Download at svgtopng at bintray](https://bintray.com/puel/Releases/SvgToPng#files)
* [Download at svg2png at bintray](https://bintray.com/puel/Releases/svg2png#files)
* You have to have Java 8 installed on your PC
* Sidenote: [Google Android Icons](https://www.google.com/design/icons/)

## CLI Samples

# just convert a file
svgtopng foo.svg
svg2png foo.svg
# generate a PNG with a name
svgtopng -f foo.svg -n bar.png
svg2png -f foo.svg -n bar.png
# convert all file in a directory
svgtopng -d /Picures/icons/svg -o /Pictures/icons/png
svg2png -d /Picures/icons/svg -o /Pictures/icons/png
# convert with a JSON configuration
svgtopng -d . -c my.json
svg2png -d . -c my.json
# convert SVG files using the default Android configuration
svgtopng -d . -o /dev/workset/android-project/app/src/main/res --android
svg2png -d . -o /dev/workset/android-project/app/src/main/res --android
# you can always start it like any other java jar file
java -jar svgtopng
java -jar svg2png
## CLI Usage

================================================================================
SVG to PNG
usage: svgtopng
usage: svg2png
--android Android default config from mdpi 48x48 -> xxxhdpi
192x192.
--android-small Android Small default config from mdpi 24x24 ->
Expand All @@ -48,7 +48,7 @@ Manage your Icons in SVG and generate the needed PNG into your projects as neede
-w <arg> Width of the output file.

## JSON Android Config Sample

{
"files": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.sterl.encoding;
package org.sterl.svg2png;

import java.io.FileInputStream;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.io.IOUtils;
import org.sterl.encoding.config.FileOutput;
import org.sterl.encoding.config.OutputConfig;
import org.sterl.encoding.util.FileUtil;
import org.sterl.svg2png.config.FileOutput;
import org.sterl.svg2png.config.OutputConfig;
import org.sterl.svg2png.util.FileUtil;

import com.fasterxml.jackson.databind.ObjectMapper;

Expand All @@ -20,7 +20,8 @@ public enum CliOptions {
HEIGHT("h", null, true, "Height of the output file."),
CONFIG("c", null, true, "JSON Config file for the file output."),

ANDROID(null, "android", false, "Android Launcher Icon config mdpi 48x48 -> xxxhdpi 192x192."),
ANDROID(null, "android", false, "Android Icon 48dp mdpi 48x48 -> xxxhdpi 192x192."),
ANDROID_LAUNCH(null, "android-launch", false, "Android Launcher Icon config mdpi 48x48 -> xxxhdpi 192x192."),
ANDROID_ICON(null, "android-icon", false, "Android Icon (Action Bar, Dialog etc.) config mdpi 36x36 -> xxxhdpi 128x128."),
ANDROID_SMALL(null, "android-small", false, "Android Small default config from mdpi 24x24 -> xxxhdpi 96x96.")
;
Expand Down Expand Up @@ -70,6 +71,12 @@ public static OutputConfig parse(CommandLine cmd) {
} catch (Exception e) {
throw new RuntimeException(e);
}
} else if (cmd.hasOption(ANDROID_LAUNCH.longName)) {
try {
result = m.readerFor(OutputConfig.class).readValue(CliOptions.class.getResourceAsStream("/android-launcher.json"));
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
result = new OutputConfig();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sterl.encoding;
package org.sterl.svg2png;

import java.io.File;
import java.io.IOException;
Expand All @@ -11,8 +11,8 @@
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.lang3.StringUtils;
import org.sterl.encoding.config.OutputConfig;
import org.sterl.encoding.util.FileUtil;
import org.sterl.svg2png.config.OutputConfig;
import org.sterl.svg2png.util.FileUtil;

//https://commons.apache.org/proper/commons-cli/usage.html
public class Main {
Expand Down Expand Up @@ -43,7 +43,7 @@ static List<File> run(String[] args) throws ParseException, IOException, Transco
} else if (args.length == 1) {
cfg = OutputConfig.fromPath(args[0]);
} else {
CommandLine cmd = new DefaultParser().parse( options, args);
CommandLine cmd = new DefaultParser().parse(options, args);
cfg = CliOptions.parse(cmd);
}

Expand All @@ -61,31 +61,31 @@ static List<File> run(String[] args) throws ParseException, IOException, Transco
if (!d.isDirectory()) throw new IllegalArgumentException(cfg.getInputDirectory() + " is not a directory!");
}

return new SvgToPng(cfg).convert();
return new Svg2Png(cfg).convert();
}

private static void printHelp() {
System.out.println(StringUtils.repeat("=", 80));
System.out.println(StringUtils.center("SVG to PNG", 80));
System.out.println();
formatter.printHelp( "svgtopng", options );
formatter.printHelp( "svg2png", options );
System.out.println();
System.out.println("Examples:");
System.out.println("---------");
System.out.println("# just convert a file");
System.out.println("svgtopng foo.svg");
System.out.println("svg2png foo.svg");
System.out.println("");
System.out.println("# generate a PNG with a name");
System.out.println("svgtopng -f foo.svg -n bar.png");
System.out.println("svg2png -f foo.svg -n bar.png");
System.out.println("");
System.out.println("# convert all file in a directory");
System.out.println("svgtopng -d /Picures/icons/svg -o /Pictures/icons/png");
System.out.println("svg2png -d /Picures/icons/svg -o /Pictures/icons/png");
System.out.println("");
System.out.println("# convert with a JSON configuration");
System.out.println("svgtopng -d . -c my.json");
System.out.println("svg2png -d . -c my.json");
System.out.println("");
System.out.println("# convert SVG files using the default Android configuration");
System.out.println("svgtopng -d . -o /dev/workset/android-project/app/src/main/res --android");
System.out.println("svg2png -d . -o /dev/workset/android-project/app/src/main/res --android");
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sterl.encoding;
package org.sterl.svg2png;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -13,15 +13,15 @@
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.commons.io.FileUtils;
import org.sterl.encoding.config.FileOutput;
import org.sterl.encoding.config.OutputConfig;
import org.sterl.encoding.util.FileUtil;
import org.sterl.svg2png.config.FileOutput;
import org.sterl.svg2png.config.OutputConfig;
import org.sterl.svg2png.util.FileUtil;

public class SvgToPng {
public class Svg2Png {

private final OutputConfig outCfg;

public SvgToPng(OutputConfig outCfg) {
public Svg2Png(OutputConfig outCfg) {
super();
this.outCfg = outCfg;
}
Expand Down Expand Up @@ -54,15 +54,14 @@ private static List<File> convertFile(File input, OutputConfig cfg) throws IOExc
info.append(input.getName());

if (out.getWidth() > 0) {
t.addTranscodingHint(PNGTranscoder.KEY_MAX_WIDTH, new Float(out.getWidth()));
t.addTranscodingHint(PNGTranscoder.KEY_WIDTH, new Float(out.getWidth()));
info.append(" w").append(out.getWidth());
}
else t.removeTranscodingHint(PNGTranscoder.KEY_MAX_WIDTH);
} else t.removeTranscodingHint(PNGTranscoder.KEY_WIDTH);

if (out.getHeight() > 0) {
t.addTranscodingHint(PNGTranscoder.KEY_MAX_HEIGHT, new Float(out.getHeight()));
t.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, new Float(out.getHeight()));
info.append(" h").append(out.getHeight());
}
else t.removeTranscodingHint(PNGTranscoder.KEY_MAX_HEIGHT);
} else t.removeTranscodingHint(PNGTranscoder.KEY_HEIGHT);

File outputFile = out.toOutputFile(input, cfg.getOutputDirectory(), cfg.getOutputName());
if (outputFile.exists()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.sterl.encoding.config;
package org.sterl.svg2png.config;

import java.io.File;

import org.apache.commons.io.FilenameUtils;
import org.sterl.encoding.util.FileUtil;
import org.sterl.svg2png.util.FileUtil;

import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -14,22 +14,28 @@
public class FileOutput {
private int height = -1;
private int width = -1;
private String name;
private String directory;
private String nameSuffix;
private String namePrefix;

public File toOutputFile(File source, String basePath, String outName) {
// either a set path or the one of the parent
String path = basePath != null ? basePath : source.getParent();

if (path == null) path = "";
// if the configured path is absolute we take it, otherwise we append it
if (directory != null && directory.startsWith("/")) {
path = directory;
} if (directory != null) {
path += "/" + directory;
}
if (directory != null) {
if (path.length() > 0) {
path += "/" + directory;
} else {
path += directory;
}
}
// setting the name
path += "/" + buildName(FilenameUtils.getBaseName(source.getName()), outName, namePrefix, nameSuffix);
path += "/" + buildName(FilenameUtils.getBaseName(source.getName()), name != null ? name : outName, namePrefix, nameSuffix);

return FileUtil.newFile(path);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.sterl.encoding.config;
package org.sterl.svg2png.config;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.sterl.encoding.util.FileUtil;
import org.sterl.svg2png.util.FileUtil;

import lombok.Getter;
import lombok.Setter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sterl.encoding.util;
package org.sterl.svg2png.util;

import java.io.File;
import java.net.URI;
Expand Down
30 changes: 30 additions & 0 deletions src/main/resources/android-launcher.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"files": [
{
"name": "ic_launcher.png",
"directory": "mipmap-xxxhdpi",
"height": 192,
"width": 192
},{
"name": "ic_launcher.png",
"directory": "mipmap-xxhdpi",
"height": 144,
"width": 144
},{
"name": "ic_launcher.png",
"directory": "mipmap-xhdpi",
"height": 96,
"width": 96
},{
"name": "ic_launcher.png",
"directory": "mipmap-hdpi",
"height": 72,
"width": 72
},{
"name": "ic_launcher.png",
"directory": "mipmap-mdpi",
"height": 48,
"width": 48
}
]
}
37 changes: 37 additions & 0 deletions src/test/java/org/sterl/svg2png/CliOptionsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.sterl.svg2png;

import static org.junit.Assert.*;

import java.io.File;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.junit.Test;
import org.sterl.svg2png.config.FileOutput;
import org.sterl.svg2png.config.OutputConfig;

public class CliOptionsTest {

@Test
public void test() throws Exception {
Options options = new Options();
CliOptions.addOptions(options);

CommandLine cmd = new DefaultParser().parse(options, new String[]{"--android", "-f", "ic_launcher.svg"});
OutputConfig cfg = CliOptions.parse(cmd);

assertNull(cfg.getInputDirectory());
assertNull(cfg.getOutputDirectory());
assertNull(cfg.getOutputName());
assertEquals(5, cfg.getFiles().size());
assertEquals("ic_launcher.svg", cfg.getInputFile());

for (FileOutput fo : cfg.getFiles()) {
final String outPath = fo.toOutputFile(new File("ic_launcher.svg"), null, null).getAbsolutePath();
System.out.println(outPath);
assertTrue(!outPath.startsWith("null/"));
assertTrue(outPath.endsWith("/ic_launcher.png"));
}
}
}
Loading

0 comments on commit d0192c1

Please sign in to comment.