Skip to content

Commit

Permalink
checking Width and Height from the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
sterlp committed Dec 14, 2023
1 parent 716a592 commit c4cd245
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 73 deletions.
2 changes: 0 additions & 2 deletions src/main/java/org/sterl/svg2png/CliOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ private static String getValue(CommandLine cmd, CliOptions option) {
public static OutputConfig parse(CommandLine cmd) {
OutputConfig result = selectPredefinedJsonConfig(cmd);


if (cmd.hasOption(ALLOW_EXTERNAL.longName)) {
result.setAllowExternalResource(true);
}
Expand Down Expand Up @@ -93,7 +92,6 @@ public static OutputConfig parse(CommandLine cmd) {
if (cmd.hasOption(FORCE_TRANSPARENT_WHITE.longName)) {
result.enableForceTransparentWhite();
}

return result;
}

Expand Down
54 changes: 32 additions & 22 deletions src/main/java/org/sterl/svg2png/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.sterl.svg2png.config.OutputConfig;
Expand Down Expand Up @@ -47,30 +48,12 @@ static List<File> run(String[] args) throws Svg2PngException {
if (null == args || args.length == 0) {
printHelp();
System.exit(0);
} else if (args.length == 1) {
cfg = OutputConfig.fromPath(args[0]);
} else {
CommandLine cmd = new DefaultParser().parse(options, args);
cfg = CliOptions.parse(cmd);
}

// validation
if (!cfg.hasDirectoryOrFile()) {
throw new IllegalArgumentException("Please specify either a directory or a file to convert!");
} else if (cfg.getInputFile() != null) {
File f = FileUtil.newFile(cfg.getInputFile());
if (!f.exists()) throw new IllegalArgumentException("File '" + cfg.getInputFile() + "' not found!");
if (!f.isFile()) throw new IllegalArgumentException(cfg.getInputFile() + " is not a file!");
} else if (cfg.getInputDirectory() != null) {
File d = FileUtil.newFile(cfg.getInputDirectory());
if (!d.exists()) throw new IllegalArgumentException("Directory " + cfg.getInputDirectory() + " not found!");
if (!d.isDirectory()) throw new IllegalArgumentException(cfg.getInputDirectory() + " is not a directory!");
}
if (cfg.getOutputDirectory() == null && cfg.getOutputName() != null) {
cfg.setOutputDirectory("./");
}


cfg = buildOutputConfig(args);

return new Svg2Png(cfg).convert();

} catch (TranscoderException e) {
final Exception ex = e.getException();
if (ex.getMessage().contains("do not allow any external resources")) {
Expand All @@ -82,6 +65,33 @@ static List<File> run(String[] args) throws Svg2PngException {
}
}

static OutputConfig buildOutputConfig(String[] args) throws ParseException {
OutputConfig cfg;
if (args.length == 1) {
cfg = OutputConfig.fromPath(args[0]);
} else {
CommandLine cmd = new DefaultParser().parse(options, args);
cfg = CliOptions.parse(cmd);
}

// validation
if (!cfg.hasDirectoryOrFile()) {
throw new IllegalArgumentException("Please specify either a directory or a file to convert!");
} else if (cfg.getInputFile() != null) {
File f = FileUtil.newFile(cfg.getInputFile());
if (!f.exists()) throw new IllegalArgumentException("File '" + cfg.getInputFile() + "' not found!");
if (!f.isFile()) throw new IllegalArgumentException(cfg.getInputFile() + " is not a file!");
} else if (cfg.getInputDirectory() != null) {
File d = FileUtil.newFile(cfg.getInputDirectory());
if (!d.exists()) throw new IllegalArgumentException("Directory " + cfg.getInputDirectory() + " not found!");
if (!d.isDirectory()) throw new IllegalArgumentException(cfg.getInputDirectory() + " is not a directory!");
}
if (cfg.getOutputDirectory() == null && cfg.getOutputName() != null) {
cfg.setOutputDirectory("./");
}
return cfg;
}

private static void printHelp() throws IOException {
System.out.println(StringUtils.repeat("=", 80));
System.out.println(StringUtils.center("SVG to PNG", 80));
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/org/sterl/svg2png/Svg2Png.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@ private static List<File> convertFile(File input, OutputConfig cfg) throws IOExc

// https://github.com/sterlp/svg2png/issues/11
t.addTranscodingHint(ImageTranscoder.KEY_FORCE_TRANSPARENT_WHITE, out.isForceTransparentWhite());
setSizeHint(SVGAbstractTranscoder.KEY_WIDTH, out.getWidth(), t, info);
setSizeHint(SVGAbstractTranscoder.KEY_HEIGHT, out.getHeight(), t, info);
if (out.hasBackgroundColor()) {
if (out.getWidth() > 0) {
setSizeHint(SVGAbstractTranscoder.KEY_WIDTH, out.getWidth(), t, info);
info.append(out.getWidth() +"w ");
}
if (out.getHeight() > 0) {
setSizeHint(SVGAbstractTranscoder.KEY_HEIGHT, out.getHeight(), t, info);
info.append(out.getHeight() +"h ");
}
if (out.hasBackgroundColor()) {
t.addTranscodingHint(PNGTranscoder.KEY_BACKGROUND_COLOR, Color.decode("#" + out.getBackgroundColor()));
}

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/sterl/svg2png/config/OutputConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ public boolean hasDirectoryOrFile() {
return inputFile != null || inputDirectory != null;
}

/**
* @param width -1 will be ignored
* @param height -1 will be ignored
*/
public void applyOutputSize(int width, int height) {
for (FileOutput fileOutput : files) {
fileOutput.setWidth(width);
fileOutput.setHeight(height);
if (width > -1) fileOutput.setWidth(width);
if (height > -1) fileOutput.setHeight(height);
}
}

Expand Down
43 changes: 43 additions & 0 deletions src/test/java/org/sterl/svg2png/AbstractFileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.sterl.svg2png;

import java.io.File;
import java.io.IOException;
import java.util.Collection;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;

public class AbstractFileTest {
protected static final File tmpDir = new File("./tmp1");

@BeforeEach
public void before() throws IOException {
tmpDir.mkdirs();
tmpDir.deleteOnExit();
Collection<File> files = FileUtils.listFiles(tmpDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
for (File file : files) {
file.delete();
}
}

@AfterAll
public static void after() throws Exception {
clean();
}

protected static void clean() {
try {
FileUtils.deleteDirectory(tmpDir);
} catch (Exception e) {
System.err.println("TEST: Failed to clean " + tmpDir + " -> " + e.getCause().getMessage());
Collection<File> files = FileUtils.listFiles(tmpDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
for (File file : files) {
file.delete();
file.deleteOnExit();
}
tmpDir.deleteOnExit();
}
}
}
38 changes: 38 additions & 0 deletions src/test/java/org/sterl/svg2png/MainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.sterl.svg2png;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.sterl.svg2png.TestUtil.svgPath;

import org.junit.jupiter.api.Test;
import org.sterl.svg2png.config.FileOutput;
import org.sterl.svg2png.config.OutputConfig;

class MainTest extends AbstractFileTest {

/**
* https://github.com/sterlp/svg2png/issues/21
*/
@Test
public void testBug21() throws Exception {
// WHEN
final OutputConfig config = Main.buildOutputConfig(new String[] {
"-f",
svgPath("sample.svg"),
"-c",
getClass().getResource("/Bug21-config.json").toURI().toString(),
"-o", "./"
});

// THEN
for (FileOutput f : config.getFiles()) {
assertNotEquals(f.getWidth(), -1);
assertNotEquals(f.getHeight(), -1);
}
assertEquals(config.getFiles().get(0).getWidth(), 16);
assertEquals(config.getFiles().get(0).getHeight(), 16);

assertEquals(config.getFiles().get(1).getWidth(), 38);
assertEquals(config.getFiles().get(1).getHeight(), 32);
}
}
46 changes: 4 additions & 42 deletions src/test/java/org/sterl/svg2png/Svg2PngTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,17 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.sterl.svg2png.AssertUtil.assertEndsWith;
import static org.sterl.svg2png.TestUtil.assertEndsWith;
import static org.sterl.svg2png.TestUtil.svgPath;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.sterl.svg2png.config.OutputConfig;

Expand All @@ -29,40 +24,11 @@
import com.github.romankh3.image.comparison.model.ImageComparisonResult;
import com.github.romankh3.image.comparison.model.ImageComparisonState;

public class Svg2PngTest {

static final File tmpDir = new File("./tmp1");

@BeforeEach
public void before() throws IOException {
tmpDir.mkdirs();
tmpDir.deleteOnExit();
Collection<File> files = FileUtils.listFiles(tmpDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
for (File file : files) {
file.delete();
}
}
@AfterAll
public static void after() throws Exception {
clean();
}
private static void clean() {
try {
FileUtils.deleteDirectory(tmpDir);
} catch (Exception e) {
System.err.println("TEST: Failed to clean " + tmpDir + " -> " + e.getCause().getMessage());
Collection<File> files = FileUtils.listFiles(tmpDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
for (File file : files) {
file.delete();
file.deleteOnExit();
}
tmpDir.deleteOnExit();
}
}
public class Svg2PngTest extends AbstractFileTest {

@Test
public void testConversionOfOneFile() throws Exception {
// GIVEN
// GIVEN
OutputConfig cfg = OutputConfig.fromPath(svgPath("sample.svg"));
cfg.applyOutputSize(128, 128);

Expand Down Expand Up @@ -353,10 +319,6 @@ public void testCustomBackground() throws Exception {
assertEqualImages("background-128x128-0077FF.png", subject.getAbsolutePath());
}

private String svgPath(String file) throws URISyntaxException {
return getClass().getResource("/svgfolder/" + file).toString();
}

private static void assertEqualImages(String source, String actual) {
BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources(source);
BufferedImage actualImage = ImageComparisonUtil.readImageFromResources(actual);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public final class AssertUtil {
import java.net.URISyntaxException;

public final class TestUtil {
public final static void assertEndsWith(String absolutePath, String value) {
assertNotNull("Expected end with: " + value, absolutePath);
assertTrue(absolutePath.equals(value) || absolutePath.endsWith(value),
absolutePath + " doesn't end with: " + value);
}

public final static String svgPath(String file) throws URISyntaxException {
return TestUtil.class.getResource("/svgfolder/" + file).toString();
}
}
2 changes: 1 addition & 1 deletion src/test/java/org/sterl/svg2png/config/TestFileOutput.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.sterl.svg2png.config;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.sterl.svg2png.AssertUtil.assertEndsWith;
import static org.sterl.svg2png.TestUtil.assertEndsWith;

import java.io.File;
import java.nio.file.Files;
Expand Down
64 changes: 64 additions & 0 deletions src/test/resources/Bug21-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"files": [
{
"directory": "icon.iconset",
"nameSuffix": "_16x16",
"height": 16,
"width": 16
},
{
"directory": "icon.iconset",
"nameSuffix": "_16x16@2x",
"height": 32,
"width": 38
},
{
"directory": "icon.iconset",
"nameSuffix": "_32x32",
"height": 32,
"width": 32
},
{
"directory": "icon.iconset",
"nameSuffix": "_32x32@2x",
"height": 64,
"width": 64
},
{
"directory": "icon.iconset",
"nameSuffix": "_128x128",
"height": 128,
"width": 128
},
{
"directory": "icon.iconset",
"nameSuffix": "_128x128@2x",
"height": 256,
"width": 256
},
{
"directory": "icon.iconset",
"nameSuffix": "_256x256",
"height": 256,
"width": 256
},
{
"directory": "icon.iconset",
"nameSuffix": "_256x256@2x",
"height": 512,
"width": 512
},
{
"directory": "icon.iconset",
"nameSuffix": "_512x512",
"height": 512,
"width": 512
},
{
"directory": "icon.iconset",
"nameSuffix": "_512x512@2x",
"height": 1024,
"width": 1024
}
]
}

0 comments on commit c4cd245

Please sign in to comment.