MakeTiles: impl support for SVG files
SVG files are converted to PNG files at the appropriate resolution by calling out to Inkscape.exe (must be installed somewhere) git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@747 d9718cc8-9f43-0410-858b-315f434eb58c
This commit is contained in:
parent
68717e3ea6
commit
e3dcff2804
2 changed files with 87 additions and 9 deletions
|
@ -46,12 +46,12 @@
|
||||||
|
|
||||||
<target name="compose-tiles" depends="init-builddir,compile">
|
<target name="compose-tiles" depends="init-builddir,compile">
|
||||||
<java classname="micropolisj.build_tool.MakeTiles" classpath="${builddir}"
|
<java classname="micropolisj.build_tool.MakeTiles" classpath="${builddir}"
|
||||||
fork="true" dir="graphics">
|
fork="true" failonerror="true" dir="graphics">
|
||||||
<arg file="graphics/tiles.rc" />
|
<arg file="graphics/tiles.rc" />
|
||||||
<arg file="${builddir}/tiles.png" />
|
<arg file="${builddir}/tiles.png" />
|
||||||
</java>
|
</java>
|
||||||
<java classname="micropolisj.build_tool.MakeTiles" classpath="${builddir}"
|
<java classname="micropolisj.build_tool.MakeTiles" classpath="${builddir}"
|
||||||
fork="true" dir="graphics">
|
fork="true" failonerror="true" dir="graphics">
|
||||||
<sysproperty key="tile_size" value="32" />
|
<sysproperty key="tile_size" value="32" />
|
||||||
<arg file="graphics/tiles.rc" />
|
<arg file="graphics/tiles.rc" />
|
||||||
<arg file="${builddir}/tiles_32x32.png" />
|
<arg file="${builddir}/tiles_32x32.png" />
|
||||||
|
|
|
@ -52,6 +52,7 @@ public class MakeTiles
|
||||||
drawTo(ref, gr, 0, TILE_SIZE*i);
|
drawTo(ref, gr, 0, TILE_SIZE*i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
System.out.println("Generating tiles array: "+outputFile);
|
||||||
ImageIO.write(buf, "png", outputFile);
|
ImageIO.write(buf, "png", outputFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,25 +137,102 @@ public class MakeTiles
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static File findInkscape()
|
||||||
|
{
|
||||||
|
String exeName = "inkscape";
|
||||||
|
if (System.getProperty("os.name").startsWith("Windows")) {
|
||||||
|
exeName += ".exe";
|
||||||
|
}
|
||||||
|
|
||||||
|
File [] pathsToTry = {
|
||||||
|
new File("/usr/bin"),
|
||||||
|
new File("c:\\Program Files\\Inkscape"),
|
||||||
|
new File("c:\\Program Files (x86)\\Inkscape")
|
||||||
|
};
|
||||||
|
for (File p : pathsToTry) {
|
||||||
|
File f = new File(p, exeName);
|
||||||
|
if (f.exists()) {
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new Error("INKSCAPE not installed (or not found)");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void renderSvg(File svgFile, File pngFile)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
if (pngFile.exists() &&
|
||||||
|
pngFile.lastModified() > svgFile.lastModified())
|
||||||
|
{
|
||||||
|
// looks like the PNG file is already up-to-date
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
File inkscapeBin = findInkscape();
|
||||||
|
|
||||||
|
System.out.println("Generating raster image: "+pngFile);
|
||||||
|
if (pngFile.exists()) {
|
||||||
|
pngFile.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
String [] cmdline = {
|
||||||
|
inkscapeBin.toString(),
|
||||||
|
"--export-width="+TILE_SIZE,
|
||||||
|
"--export-height="+TILE_SIZE,
|
||||||
|
"--export-png="+pngFile.toString(),
|
||||||
|
svgFile.toString()
|
||||||
|
};
|
||||||
|
Process p = Runtime.getRuntime().exec(cmdline);
|
||||||
|
int exit_value;
|
||||||
|
try {
|
||||||
|
exit_value = p.waitFor();
|
||||||
|
}
|
||||||
|
catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (exit_value != 0) {
|
||||||
|
throw new RuntimeException("Helper exit status: "+exit_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pngFile.exists()) {
|
||||||
|
throw new RuntimeException("File not found: "+pngFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static SourceImage loadImage(String fileName)
|
static SourceImage loadImage(String fileName)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
File f = new File(fileName+"_"+TILE_SIZE+"x"+TILE_SIZE+".png");
|
File svgFile, pngFile;
|
||||||
if (f.exists()) {
|
|
||||||
ImageIcon ii = new ImageIcon(f.toString());
|
svgFile = new File(fileName+"_"+TILE_SIZE+"x"+TILE_SIZE+".svg");
|
||||||
|
pngFile = new File(fileName+"_"+TILE_SIZE+"x"+TILE_SIZE+".png");
|
||||||
|
|
||||||
|
if (svgFile.exists()) {
|
||||||
|
renderSvg(svgFile, pngFile);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
svgFile = new File(fileName+".svg");
|
||||||
|
if (svgFile.exists()) {
|
||||||
|
renderSvg(svgFile, pngFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pngFile.exists()) {
|
||||||
|
ImageIcon ii = new ImageIcon(pngFile.toString());
|
||||||
return new SourceImage(
|
return new SourceImage(
|
||||||
ii.getImage(),
|
ii.getImage(),
|
||||||
TILE_SIZE);
|
TILE_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
f = new File(fileName+".png");
|
pngFile = new File(fileName+".png");
|
||||||
if (f.exists()) {
|
if (pngFile.exists()) {
|
||||||
ImageIcon ii = new ImageIcon(f.toString());
|
ImageIcon ii = new ImageIcon(pngFile.toString());
|
||||||
return new SourceImage(
|
return new SourceImage(
|
||||||
ii.getImage(),
|
ii.getImage(),
|
||||||
16);
|
16);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new IOException("File not found: "+fileName+".png");
|
throw new IOException("File not found: "+fileName+".{svg,png}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue