|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* (c) Copyright 2015 l33t labs LLC and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* l33t labs LLC and others - initial contribution |
| 10 |
*******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.images.renderer; |
| 13 |
|
| 14 |
import java.awt.RenderingHints; |
| 15 |
import java.awt.image.BufferedImage; |
| 16 |
import java.io.ByteArrayInputStream; |
| 17 |
import java.io.ByteArrayOutputStream; |
| 18 |
import java.io.File; |
| 19 |
import java.io.FileInputStream; |
| 20 |
import java.io.FileWriter; |
| 21 |
import java.io.IOException; |
| 22 |
import java.io.OutputStream; |
| 23 |
import java.util.ArrayList; |
| 24 |
import java.util.Collections; |
| 25 |
import java.util.List; |
| 26 |
import java.util.concurrent.Callable; |
| 27 |
import java.util.concurrent.ExecutorService; |
| 28 |
import java.util.concurrent.Executors; |
| 29 |
import java.util.concurrent.atomic.AtomicInteger; |
| 30 |
|
| 31 |
import javax.imageio.ImageIO; |
| 32 |
|
| 33 |
import org.apache.batik.dom.svg.SAXSVGDocumentFactory; |
| 34 |
import org.apache.batik.gvt.renderer.ImageRenderer; |
| 35 |
import org.apache.batik.transcoder.ErrorHandler; |
| 36 |
import org.apache.batik.transcoder.TranscoderException; |
| 37 |
import org.apache.batik.transcoder.TranscoderInput; |
| 38 |
import org.apache.batik.transcoder.TranscoderOutput; |
| 39 |
import org.apache.batik.transcoder.image.PNGTranscoder; |
| 40 |
import org.apache.batik.util.XMLResourceDescriptor; |
| 41 |
import org.apache.maven.plugin.AbstractMojo; |
| 42 |
import org.apache.maven.plugin.MojoExecutionException; |
| 43 |
import org.apache.maven.plugin.MojoFailureException; |
| 44 |
import org.apache.maven.plugin.logging.Log; |
| 45 |
import org.w3c.dom.Element; |
| 46 |
import org.w3c.dom.svg.SVGDocument; |
| 47 |
|
| 48 |
import com.jhlabs.image.ContrastFilter; |
| 49 |
import com.jhlabs.image.GrayscaleFilter; |
| 50 |
import com.jhlabs.image.HSBAdjustFilter; |
| 51 |
|
| 52 |
/** |
| 53 |
* <p>Mojo which renders SVG icons into PNG format.</p> |
| 54 |
* |
| 55 |
* @goal render-icons |
| 56 |
* @phase generate-resources |
| 57 |
*/ |
| 58 |
public class RenderMojo extends AbstractMojo { |
| 59 |
|
| 60 |
/** Maven logger */ |
| 61 |
Log log; |
| 62 |
|
| 63 |
/** Used for high res rendering support. */ |
| 64 |
public static final String ECLIPSE_SVG_SCALE = "eclipse.svg.scale"; |
| 65 |
|
| 66 |
/** Used to specify the number of render threads when rasterizing icons. */ |
| 67 |
public static final String RENDERTHREADS = "eclipse.svg.renderthreads"; |
| 68 |
|
| 69 |
/** Used to specify the directory name where the SVGs are taken from. */ |
| 70 |
public static final String SOURCE_DIR = "eclipse.svg.sourcedirectory"; |
| 71 |
|
| 72 |
/** Used to specify the directory name where the PNGs are saved to. */ |
| 73 |
public static final String TARGET_DIR = "eclipse.svg.targetdirectory"; |
| 74 |
|
| 75 |
/** A list of directories with svg sources to rasterize. */ |
| 76 |
private List<IconEntry> icons; |
| 77 |
|
| 78 |
/** The pool used to render multiple icons concurrently. */ |
| 79 |
private ExecutorService execPool; |
| 80 |
|
| 81 |
/** The number of threads to use when rendering icons. */ |
| 82 |
private int threads; |
| 83 |
|
| 84 |
/** |
| 85 |
* A counter used to keep track of the number of rendered icons. Atomic is |
| 86 |
* used to make it easy to access between threads concurrently. |
| 87 |
*/ |
| 88 |
private AtomicInteger counter; |
| 89 |
|
| 90 |
/** List of icons that failed to render, made safe for parallel access */ |
| 91 |
List<IconEntry> failedIcons = Collections |
| 92 |
.synchronizedList(new ArrayList<IconEntry>(5)); |
| 93 |
|
| 94 |
/** The amount of scaling to apply to rasterized images. */ |
| 95 |
private double outputScale; |
| 96 |
|
| 97 |
/** |
| 98 |
* @return the number of icons rendered at the time of the call |
| 99 |
*/ |
| 100 |
public int getIconsRendered() { |
| 101 |
return counter.get(); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* @return the number of icons that failed during the rendering process |
| 106 |
*/ |
| 107 |
public int getFailedIcons() { |
| 108 |
return failedIcons.size(); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* <p>Generates raster images from the input SVG vector image.</p> |
| 113 |
* |
| 114 |
* @param icon |
| 115 |
* the icon to render |
| 116 |
*/ |
| 117 |
public void rasterize(IconEntry icon, GrayscaleFilter grayFilter, HSBAdjustFilter desaturator, ContrastFilter decontrast) { |
| 118 |
if (icon == null) { |
| 119 |
log.error("Null icon definition, skipping."); |
| 120 |
failedIcons.add(icon); |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
if (icon.inputPath == null) { |
| 125 |
log.error("Null icon input path, skipping: " |
| 126 |
+ icon.nameBase); |
| 127 |
failedIcons.add(icon); |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
if (!icon.inputPath.exists()) { |
| 132 |
log.error("Input path specified does not exist, skipping: " |
| 133 |
+ icon.nameBase); |
| 134 |
failedIcons.add(icon); |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
if (icon.outputPath != null && !icon.outputPath.exists()) { |
| 139 |
icon.outputPath.mkdirs(); |
| 140 |
} |
| 141 |
|
| 142 |
if (icon.disabledPath != null && !icon.disabledPath.exists()) { |
| 143 |
icon.disabledPath.mkdirs(); |
| 144 |
} |
| 145 |
|
| 146 |
// Create the document to rasterize |
| 147 |
SVGDocument svgDocument = generateSVGDocument(icon); |
| 148 |
|
| 149 |
if(svgDocument == null) { |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
// Determine the output sizes (native, double, quad) |
| 154 |
// We render at quad size and resample down for output |
| 155 |
Element svgDocumentNode = svgDocument.getDocumentElement(); |
| 156 |
String nativeWidthStr = svgDocumentNode.getAttribute("width"); |
| 157 |
String nativeHeightStr = svgDocumentNode.getAttribute("height"); |
| 158 |
int nativeWidth = -1; |
| 159 |
int nativeHeight = -1; |
| 160 |
|
| 161 |
try{ |
| 162 |
if (nativeWidthStr != "" && nativeHeightStr != ""){ |
| 163 |
nativeWidth = Integer.parseInt(nativeWidthStr); |
| 164 |
nativeHeight = Integer.parseInt(nativeHeightStr); |
| 165 |
} else { |
| 166 |
// Vector graphics editing programs don't always output height and width attributes on SVG. |
| 167 |
// As fall back: parse the viewBox attribute (which is almost always set). |
| 168 |
String viewBoxStr = svgDocumentNode.getAttribute("viewBox"); |
| 169 |
if (viewBoxStr == ""){ |
| 170 |
log.error("Icon defines neither width/height nor a viewBox, skipping: " + icon.nameBase); |
| 171 |
failedIcons.add(icon); |
| 172 |
return; |
| 173 |
} |
| 174 |
String[] splitted = viewBoxStr.split(" "); |
| 175 |
String xStr = splitted[0]; |
| 176 |
String yStr = splitted[1]; |
| 177 |
String widthStr = splitted[2]; |
| 178 |
String heightStr = splitted[3]; |
| 179 |
nativeWidth = Integer.parseInt(widthStr) - Integer.parseInt(xStr); |
| 180 |
nativeHeight = Integer.parseInt(heightStr) - Integer.parseInt(yStr); |
| 181 |
} |
| 182 |
}catch (NumberFormatException e){ |
| 183 |
log.error("Dimension could not be parsed ( "+e.getMessage()+ "), skipping: " + icon.nameBase); |
| 184 |
failedIcons.add(icon); |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
int outputWidth = (int) (nativeWidth * outputScale); |
| 189 |
int outputHeight = (int) (nativeHeight * outputScale); |
| 190 |
|
| 191 |
// Guesstimate the PNG size in memory, BAOS will enlarge if necessary. |
| 192 |
int outputInitSize = nativeWidth * nativeHeight * 4 + 1024; |
| 193 |
ByteArrayOutputStream iconOutput = new ByteArrayOutputStream( |
| 194 |
outputInitSize); |
| 195 |
|
| 196 |
// Render to SVG |
| 197 |
try { |
| 198 |
log.info(Thread.currentThread().getName() + " " |
| 199 |
+ " Rasterizing: " + icon.nameBase + ".png at " + outputWidth |
| 200 |
+ "x" + outputHeight); |
| 201 |
|
| 202 |
TranscoderInput svgInput = new TranscoderInput(svgDocument); |
| 203 |
|
| 204 |
boolean success = renderIcon(icon.nameBase, outputWidth, outputHeight, svgInput, iconOutput); |
| 205 |
|
| 206 |
if (!success) { |
| 207 |
log.error("Failed to render icon: " + icon.nameBase + ".png, skipping."); |
| 208 |
failedIcons.add(icon); |
| 209 |
return; |
| 210 |
} |
| 211 |
} catch (Exception e) { |
| 212 |
log.error("Failed to render icon: " + e.getMessage()); |
| 213 |
failedIcons.add(icon); |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
// Generate a buffered image from Batik's png output |
| 218 |
byte[] imageBytes = iconOutput.toByteArray(); |
| 219 |
ByteArrayInputStream imageInputStream = new ByteArrayInputStream(imageBytes); |
| 220 |
|
| 221 |
BufferedImage inputImage = null; |
| 222 |
try { |
| 223 |
inputImage = ImageIO.read(imageInputStream); |
| 224 |
|
| 225 |
if(inputImage == null) { |
| 226 |
log.error("Failed to generate BufferedImage from rendered icon, ImageIO returned null: " + icon.nameBase); |
| 227 |
failedIcons.add(icon); |
| 228 |
return; |
| 229 |
} |
| 230 |
} catch (IOException e2) { |
| 231 |
log.error("Failed to generate BufferedImage from rendered icon: " + icon.nameBase + " - " + e2.getMessage()); |
| 232 |
failedIcons.add(icon); |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
writeIcon(icon, outputWidth, outputHeight, inputImage); |
| 237 |
|
| 238 |
try { |
| 239 |
if (icon.disabledPath != null) { |
| 240 |
BufferedImage desaturated16 = desaturator.filter( |
| 241 |
grayFilter.filter(inputImage, null), null); |
| 242 |
|
| 243 |
BufferedImage deconstrast = decontrast.filter(desaturated16, null); |
| 244 |
|
| 245 |
ImageIO.write(deconstrast, "PNG", new File(icon.disabledPath, icon.nameBase + ".png")); |
| 246 |
} |
| 247 |
} catch (Exception e1) { |
| 248 |
log.error("Failed to render disabled icon: " + |
| 249 |
icon.nameBase, e1); |
| 250 |
failedIcons.add(icon); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* <p>Generates a Batik SVGDocument for the supplied IconEntry's input |
| 256 |
* file.</p> |
| 257 |
* |
| 258 |
* @param icon the icon entry to generate an SVG document for |
| 259 |
* |
| 260 |
* @return a batik SVGDocument instance or null if one could not be generated |
| 261 |
*/ |
| 262 |
private SVGDocument generateSVGDocument(IconEntry icon) { |
| 263 |
// Load the document and find out the native height/width |
| 264 |
// We reuse the document later for rasterization |
| 265 |
SVGDocument svgDocument = null; |
| 266 |
try { |
| 267 |
FileInputStream iconDocumentStream = new FileInputStream(icon.inputPath); |
| 268 |
|
| 269 |
String parser = XMLResourceDescriptor.getXMLParserClassName(); |
| 270 |
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser); |
| 271 |
|
| 272 |
// What kind of URI is batik expecting here??? the docs don't say |
| 273 |
svgDocument = f.createSVGDocument("file://" + icon.nameBase + ".svg", iconDocumentStream); |
| 274 |
} catch (Exception e3) { |
| 275 |
log.error("Error parsing SVG icon document: " + e3.getMessage()); |
| 276 |
failedIcons.add(icon); |
| 277 |
return null; |
| 278 |
} |
| 279 |
return svgDocument; |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* <p>Resizes the supplied inputImage to the specified width and height, using |
| 284 |
* lanczos resampling techniques.</p> |
| 285 |
* |
| 286 |
* @param icon the icon that's being resized |
| 287 |
* @param width the desired output width after rescaling operations |
| 288 |
* @param height the desired output height after rescaling operations |
| 289 |
* @param sourceImage the source image to resource |
| 290 |
*/ |
| 291 |
private void writeIcon(IconEntry icon, int width, int height, BufferedImage sourceImage) { |
| 292 |
try { |
| 293 |
String outputName = icon.nameBase; |
| 294 |
if (outputScale != 1) { |
| 295 |
String scaleId = outputScale == (double) (int) outputScale ? Integer.toString((int) outputScale): Double.toString(outputScale); |
| 296 |
outputName += "@" + scaleId + "x"; |
| 297 |
} |
| 298 |
outputName += ".png"; |
| 299 |
ImageIO.write(sourceImage, "PNG", new File(icon.outputPath, outputName)); |
| 300 |
} catch (Exception e1) { |
| 301 |
log.error("Failed to resize rendered icon to output size: " + |
| 302 |
icon.nameBase, e1); |
| 303 |
failedIcons.add(icon); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* <p>Handles concurrently rasterizing the icons to |
| 309 |
* reduce the duration on multicore systems.</p> |
| 310 |
*/ |
| 311 |
public void rasterizeAll() { |
| 312 |
// The number of icons that haven't been distributed to |
| 313 |
// callables |
| 314 |
int remainingIcons = icons.size(); |
| 315 |
|
| 316 |
// The number of icons to distribute to a rendering callable |
| 317 |
final int threadExecSize = Math.max(1, icons.size() / this.threads); |
| 318 |
|
| 319 |
// The current offset to start a batch, as they're distributed |
| 320 |
// between rendering callables |
| 321 |
int batchOffset = 0; |
| 322 |
|
| 323 |
// A list of callables used to render icons on multiple threads |
| 324 |
// Each callable gets a set of icons to render |
| 325 |
List<Callable<Object>> tasks = new ArrayList<>( |
| 326 |
this.threads); |
| 327 |
|
| 328 |
// Distribute the rasterization operations between multiple threads |
| 329 |
while (remainingIcons > 0) { |
| 330 |
// The current start index for the current batch |
| 331 |
final int batchStart = batchOffset; |
| 332 |
|
| 333 |
// Increment the offset to reflect this batch (used for the next batch) |
| 334 |
batchOffset += threadExecSize; |
| 335 |
|
| 336 |
// Determine this batch size, used for batches that have less than |
| 337 |
// threadExecSize at the end of the distribution operation |
| 338 |
int batchSize = 0; |
| 339 |
|
| 340 |
// Determine if we can fit a full batch in this callable |
| 341 |
// or if we are at the end of gathered icons |
| 342 |
if (remainingIcons > threadExecSize) { |
| 343 |
batchSize = threadExecSize; |
| 344 |
} else { |
| 345 |
// We have less than a full batch worth of remaining icons |
| 346 |
// just add them all |
| 347 |
batchSize = remainingIcons; |
| 348 |
} |
| 349 |
|
| 350 |
// Deincrement the remaining Icons |
| 351 |
remainingIcons -= threadExecSize; |
| 352 |
|
| 353 |
// Used for access in the callable's scope |
| 354 |
final int execCount = batchSize; |
| 355 |
|
| 356 |
// Create the callable and add it to the task pool |
| 357 |
Callable<Object> runnable = new Callable<Object>() { |
| 358 |
@Override |
| 359 |
public Object call() throws Exception { |
| 360 |
// The jhlabs filters are not thread safe, so provide one set per thread |
| 361 |
GrayscaleFilter grayFilter = new GrayscaleFilter(); |
| 362 |
|
| 363 |
HSBAdjustFilter desaturator = new HSBAdjustFilter(); |
| 364 |
desaturator.setSFactor(0.0f); |
| 365 |
|
| 366 |
ContrastFilter decontrast = new ContrastFilter(); |
| 367 |
decontrast.setBrightness(2.9f); |
| 368 |
decontrast.setContrast(0.2f); |
| 369 |
|
| 370 |
// Rasterize this batch |
| 371 |
for (int count = 0; count < execCount; count++) { |
| 372 |
rasterize(icons.get(batchStart + count), grayFilter, desaturator, decontrast); |
| 373 |
} |
| 374 |
|
| 375 |
// Update the render counter |
| 376 |
counter.getAndAdd(execCount); |
| 377 |
log.info("Finished rendering batch, index: " + batchStart); |
| 378 |
|
| 379 |
return null; |
| 380 |
} |
| 381 |
}; |
| 382 |
|
| 383 |
tasks.add(runnable); |
| 384 |
} |
| 385 |
|
| 386 |
// Execute the rasterization operations that |
| 387 |
// have been added to the pool |
| 388 |
try { |
| 389 |
execPool.invokeAll(tasks); |
| 390 |
} catch (InterruptedException e) { |
| 391 |
// TODO Auto-generated catch block |
| 392 |
e.printStackTrace(); |
| 393 |
} |
| 394 |
|
| 395 |
// Print info about failed render operations, so they can be fixed |
| 396 |
log.info("Failed Icon Count: " + failedIcons.size()); |
| 397 |
for (IconEntry icon : failedIcons) { |
| 398 |
log.info("Failed Icon: " + icon.nameBase); |
| 399 |
} |
| 400 |
|
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Use batik to rasterize the input SVG into a raster image at the specified |
| 405 |
* image dimensions. |
| 406 |
* |
| 407 |
* @param width the width to render the icons at |
| 408 |
* @param height the height to render the icon at |
| 409 |
* @param input the SVG transcoder input |
| 410 |
* @param stream the stream to write the PNG data to |
| 411 |
*/ |
| 412 |
public boolean renderIcon(final String iconName, int width, int height, |
| 413 |
TranscoderInput tinput, OutputStream stream) { |
| 414 |
PNGTranscoder transcoder = new PNGTranscoder() { |
| 415 |
protected ImageRenderer createRenderer() { |
| 416 |
ImageRenderer renderer = super.createRenderer(); |
| 417 |
|
| 418 |
RenderingHints renderHints = renderer.getRenderingHints(); |
| 419 |
|
| 420 |
renderHints.add(new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, |
| 421 |
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF)); |
| 422 |
|
| 423 |
renderHints.add(new RenderingHints(RenderingHints.KEY_RENDERING, |
| 424 |
RenderingHints.VALUE_RENDER_QUALITY)); |
| 425 |
|
| 426 |
renderHints.add(new RenderingHints(RenderingHints.KEY_DITHERING, |
| 427 |
RenderingHints.VALUE_DITHER_DISABLE)); |
| 428 |
|
| 429 |
renderHints.add(new RenderingHints(RenderingHints.KEY_INTERPOLATION, |
| 430 |
RenderingHints.VALUE_INTERPOLATION_BICUBIC)); |
| 431 |
|
| 432 |
renderHints.add(new RenderingHints(RenderingHints.KEY_ALPHA_INTERPOLATION, |
| 433 |
RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY)); |
| 434 |
|
| 435 |
renderHints.add(new RenderingHints(RenderingHints.KEY_ANTIALIASING, |
| 436 |
RenderingHints.VALUE_ANTIALIAS_ON)); |
| 437 |
|
| 438 |
renderHints.add(new RenderingHints(RenderingHints.KEY_COLOR_RENDERING, |
| 439 |
RenderingHints.VALUE_COLOR_RENDER_QUALITY)); |
| 440 |
|
| 441 |
renderHints.add(new RenderingHints(RenderingHints.KEY_STROKE_CONTROL, |
| 442 |
RenderingHints.VALUE_STROKE_PURE)); |
| 443 |
|
| 444 |
renderHints.add(new RenderingHints(RenderingHints.KEY_FRACTIONALMETRICS, |
| 445 |
RenderingHints.VALUE_FRACTIONALMETRICS_ON)); |
| 446 |
|
| 447 |
renderer.setRenderingHints(renderHints); |
| 448 |
|
| 449 |
return renderer; |
| 450 |
} |
| 451 |
}; |
| 452 |
|
| 453 |
transcoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, new Float(width)); |
| 454 |
transcoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, new Float(height)); |
| 455 |
|
| 456 |
transcoder.setErrorHandler(new ErrorHandler() { |
| 457 |
public void warning(TranscoderException arg0) |
| 458 |
throws TranscoderException { |
| 459 |
log.error("Icon: " + iconName + " - WARN: " + arg0.getMessage()); |
| 460 |
} |
| 461 |
|
| 462 |
public void fatalError(TranscoderException arg0) |
| 463 |
throws TranscoderException { |
| 464 |
log.error("Icon: " + iconName + " - FATAL: " + arg0.getMessage()); |
| 465 |
} |
| 466 |
|
| 467 |
public void error(TranscoderException arg0) |
| 468 |
throws TranscoderException { |
| 469 |
log.error("Icon: " + iconName + " - ERROR: " + arg0.getMessage()); |
| 470 |
} |
| 471 |
}); |
| 472 |
|
| 473 |
// Transcode the SVG document input to a PNG via the output stream |
| 474 |
TranscoderOutput output = new TranscoderOutput(stream); |
| 475 |
|
| 476 |
try { |
| 477 |
transcoder.transcode(tinput, output); |
| 478 |
return true; |
| 479 |
} catch (Exception e) { |
| 480 |
e.printStackTrace(); |
| 481 |
return false; |
| 482 |
} finally { |
| 483 |
try { |
| 484 |
stream.close(); |
| 485 |
} catch (IOException e) { |
| 486 |
// TODO Auto-generated catch block |
| 487 |
e.printStackTrace(); |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
/** |
| 493 |
* <p>Initializes rasterizer defaults</p> |
| 494 |
* |
| 495 |
* @param threads the number of threads to render with |
| 496 |
* @param scale multiplier to use with icon output dimensions |
| 497 |
*/ |
| 498 |
private void init(int threads, double scale) { |
| 499 |
this.threads = threads; |
| 500 |
this.outputScale = Math.max(1, scale); |
| 501 |
icons = new ArrayList<>(); |
| 502 |
execPool = Executors.newFixedThreadPool(threads); |
| 503 |
counter = new AtomicInteger(); |
| 504 |
} |
| 505 |
|
| 506 |
/** |
| 507 |
* @see AbstractMojo#execute() |
| 508 |
*/ |
| 509 |
public void execute() throws MojoExecutionException, MojoFailureException { |
| 510 |
log = getLog(); |
| 511 |
|
| 512 |
// Default to 2x the number of processor cores but allow override via jvm arg |
| 513 |
int threads = Math.max(1, Runtime.getRuntime().availableProcessors() * 2); |
| 514 |
String threadStr = System.getProperty(RENDERTHREADS); |
| 515 |
if (threadStr != null) { |
| 516 |
try { |
| 517 |
threads = Integer.parseInt(threadStr); |
| 518 |
} catch (Exception e) { |
| 519 |
e.printStackTrace(); |
| 520 |
System.out |
| 521 |
.println("Could not parse thread count, using default thread count"); |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
// if high res is enabled, the icons output size will be scaled by iconScale |
| 526 |
// Defaults to 1, meaning native size |
| 527 |
double iconScale = 1; |
| 528 |
String iconScaleStr = System.getProperty(ECLIPSE_SVG_SCALE); |
| 529 |
if (iconScaleStr != null) { |
| 530 |
iconScale = Double.parseDouble(iconScaleStr); |
| 531 |
if (iconScale != 1 && iconScale != 1.5 && iconScale != 2) { |
| 532 |
log.warn("Unusual scale factor: " + iconScaleStr + " (@" + iconScale + "x)"); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
// Defaults to "eclipse-svg" |
| 537 |
String sourceDir = "eclipse-svg"; |
| 538 |
String sourceDirProp = System.getProperty(SOURCE_DIR); |
| 539 |
if (sourceDirProp != null) { |
| 540 |
sourceDir = sourceDirProp; |
| 541 |
} |
| 542 |
|
| 543 |
// Defaults to "eclipse-png" |
| 544 |
String targetDir = "eclipse-png"; |
| 545 |
String targetDirProp = System.getProperty(TARGET_DIR); |
| 546 |
if (targetDirProp != null) { |
| 547 |
targetDir = targetDirProp; |
| 548 |
} |
| 549 |
|
| 550 |
// Track the time it takes to render the entire set |
| 551 |
long totalStartTime = System.currentTimeMillis(); |
| 552 |
|
| 553 |
// initialize defaults (the old renderer was instantiated via constructor) |
| 554 |
init(threads, iconScale); |
| 555 |
|
| 556 |
String workingDirectory = System.getProperty("user.dir"); |
| 557 |
|
| 558 |
File outputDir = new File(workingDirectory + (iconScale == 1 ? "/" + targetDir + "/" : "/" + targetDir + "-highdpi/")); |
| 559 |
File iconDirectoryRoot = new File(sourceDir + "/"); |
| 560 |
|
| 561 |
if (!iconDirectoryRoot.exists()){ |
| 562 |
log.error("Source directory' "+sourceDir+"' does not exist."); |
| 563 |
return; |
| 564 |
} |
| 565 |
|
| 566 |
// Search each subdir in the root dir for svg icons |
| 567 |
for (File file : iconDirectoryRoot.listFiles()) { |
| 568 |
if(!file.isDirectory()) { |
| 569 |
continue; |
| 570 |
} |
| 571 |
|
| 572 |
String dirName = file.getName(); |
| 573 |
|
| 574 |
// Where to place the rendered icon |
| 575 |
File outputBase = new File(outputDir, (iconScale == 1 ? dirName : dirName + ".highdpi")); |
| 576 |
if (iconScale != 1) { |
| 577 |
createFragmentFiles(outputBase, dirName); |
| 578 |
} |
| 579 |
|
| 580 |
IconGatherer.gatherIcons(icons, "svg", file, file, outputBase, true); |
| 581 |
} |
| 582 |
|
| 583 |
log.info("Working directory: " + outputDir.getAbsolutePath()); |
| 584 |
log.info("SVG Icon Directory: " + iconDirectoryRoot.getAbsolutePath()); |
| 585 |
log.info("Rendering icons with " + threads + " threads, scaling output to " + iconScale + "x"); |
| 586 |
long startTime = System.currentTimeMillis(); |
| 587 |
|
| 588 |
// Render the icons |
| 589 |
rasterizeAll(); |
| 590 |
|
| 591 |
// Print summary of operations |
| 592 |
int iconRendered = getIconsRendered(); |
| 593 |
int failedIcons = getFailedIcons(); |
| 594 |
int fullIconCount = iconRendered - failedIcons; |
| 595 |
|
| 596 |
log.info(fullIconCount + " Icons Rendered"); |
| 597 |
log.info(failedIcons + " Icons Failed"); |
| 598 |
log.info("Took: " + (System.currentTimeMillis() - startTime) + " ms."); |
| 599 |
|
| 600 |
log.info("Rasterization operations completed, Took: " |
| 601 |
+ (System.currentTimeMillis() - totalStartTime) + " ms."); |
| 602 |
} |
| 603 |
|
| 604 |
private void createFragmentFiles(File outputBase, String dirName) { |
| 605 |
createFile(new File(outputBase, "build.properties"), "bin.includes = META-INF/,icons/,.\n"); |
| 606 |
createFile(new File(outputBase, ".project"), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + |
| 607 |
"<projectDescription>\n" + |
| 608 |
" <name>" + dirName + ".highdpi</name>\n" + |
| 609 |
" <comment></comment>\n" + |
| 610 |
" <projects>\n" + |
| 611 |
" </projects>\n" + |
| 612 |
" <buildSpec>\n" + |
| 613 |
" <buildCommand>\n" + |
| 614 |
" <name>org.eclipse.pde.ManifestBuilder</name>\n" + |
| 615 |
" <arguments>\n" + |
| 616 |
" </arguments>\n" + |
| 617 |
" </buildCommand>\n" + |
| 618 |
" <buildCommand>\n" + |
| 619 |
" <name>org.eclipse.pde.SchemaBuilder</name>\n" + |
| 620 |
" <arguments>\n" + |
| 621 |
" </arguments>\n" + |
| 622 |
" </buildCommand>\n" + |
| 623 |
" </buildSpec>\n" + |
| 624 |
" <natures>\n" + |
| 625 |
" <nature>org.eclipse.pde.PluginNature</nature>\n" + |
| 626 |
" </natures>\n" + |
| 627 |
"</projectDescription>\n"); |
| 628 |
createFile(new File(outputBase, "META-INF/MANIFEST.MF"), "Manifest-Version: 1.0\n" + |
| 629 |
"Bundle-ManifestVersion: 2\n" + |
| 630 |
"Bundle-Name: " + dirName + ".highdpi\n" + |
| 631 |
"Bundle-SymbolicName: " + dirName + ".highdpi\n" + |
| 632 |
"Bundle-Version: 0.1.0.qualifier\n" + |
| 633 |
"Fragment-Host: " + dirName + "\n"); |
| 634 |
} |
| 635 |
|
| 636 |
private void createFile(File file, String contents) { |
| 637 |
try { |
| 638 |
file.getParentFile().mkdirs(); |
| 639 |
FileWriter writer = new FileWriter(file); |
| 640 |
writer.write(contents); |
| 641 |
writer.close(); |
| 642 |
} catch (IOException e) { |
| 643 |
log.error(e); |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
} |