Skip to content

Commit

Permalink
1068: Handling null source image document creation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Windchild292 committed Mar 20, 2022
1 parent e9d2cda commit d5d594a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 29 deletions.
41 changes: 25 additions & 16 deletions megameklab/src/megameklab/printing/PrintCompositeTankSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,31 +94,40 @@ protected void processImage(int startPage, PageFormat pageFormat) {
double ratio = includeReferenceCharts() ? TABLE_RATIO : 1.0;
RecordSheetOptions subOptions = new RecordSheetOptions(options);
subOptions.setReferenceCharts(false);
Element g;

// First Sheet
PrintRecordSheet sheet = new PrintTank(tank1, getFirstPage(), subOptions);
sheet.createDocument(startPage, pageFormat, false);
Element g = getSVGDocument().createElementNS(svgNS, SVGConstants.SVG_G_TAG);
g.setAttributeNS(null, SVGConstants.SVG_TRANSFORM_ATTRIBUTE,
String.format("%s(%f 0 0 %f %f %f)", SVGConstants.SVG_MATRIX_VALUE,
ratio, ratio, pageFormat.getImageableX(), pageFormat.getImageableY()));
sheet.hideElement(FOOTER);
g.appendChild(getSVGDocument().importNode(sheet.getSVGDocument().getDocumentElement(), true));
getSVGDocument().getDocumentElement().appendChild(g);
if (sheet.createDocument(startPage, pageFormat, false)) {
g = getSVGDocument().createElementNS(svgNS, SVGConstants.SVG_G_TAG);
g.setAttributeNS(null, SVGConstants.SVG_TRANSFORM_ATTRIBUTE,
String.format("%s(%f 0 0 %f %f %f)", SVGConstants.SVG_MATRIX_VALUE, ratio,
ratio, pageFormat.getImageableX(), pageFormat.getImageableY()));
sheet.hideElement(FOOTER);
g.appendChild(getSVGDocument().importNode(sheet.getSVGDocument().getDocumentElement(), true));
getSVGDocument().getDocumentElement().appendChild(g);
}

// Second Sheet
if (tank2 != null) {
sheet = new PrintTank(tank2, getFirstPage(), subOptions);
} else if (tank1 instanceof VTOL) {
sheet = new VTOLTables(options);
} else {
sheet = new TankTables(options);
}
sheet.createDocument(startPage, pageFormat, false);
g = getSVGDocument().createElementNS(svgNS, SVGConstants.SVG_G_TAG);
g.setAttributeNS(null, SVGConstants.SVG_TRANSFORM_ATTRIBUTE,
String.format("%s(%f 0 0 %f %f %f)", SVGConstants.SVG_MATRIX_VALUE,
ratio, ratio, pageFormat.getImageableX(),
pageFormat.getImageableY() + pageFormat.getImageableHeight() * 0.5 * ratio));
g.appendChild(getSVGDocument().importNode(sheet.getSVGDocument().getDocumentElement(), true));
getSVGDocument().getDocumentElement().appendChild(g);

if (sheet.createDocument(startPage, pageFormat, false)) {
g = getSVGDocument().createElementNS(svgNS, SVGConstants.SVG_G_TAG);
g.setAttributeNS(null, SVGConstants.SVG_TRANSFORM_ATTRIBUTE,
String.format("%s(%f 0 0 %f %f %f)", SVGConstants.SVG_MATRIX_VALUE, ratio,
ratio, pageFormat.getImageableX(),
pageFormat.getImageableY() + pageFormat.getImageableHeight() * 0.5 * ratio));
g.appendChild(getSVGDocument().importNode(sheet.getSVGDocument().getDocumentElement(), true));
getSVGDocument().getDocumentElement().appendChild(g);
}

// Reference Charts
if (includeReferenceCharts()) {
addReferenceCharts(pageFormat);
}
Expand Down
25 changes: 16 additions & 9 deletions megameklab/src/megameklab/printing/PrintRecordSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.svggen.SVGGeneratorContext;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.util.SVGConstants;
import org.apache.batik.util.XMLResourceDescriptor;
import org.apache.fop.configuration.Configuration;
import org.apache.fop.configuration.ConfigurationException;
import org.apache.fop.configuration.DefaultConfigurationBuilder;
import org.apache.fop.svg.PDFTranscoder;
import org.apache.logging.log4j.LogManager;
Expand All @@ -45,7 +43,6 @@
import org.w3c.dom.svg.SVGRectElement;
import org.w3c.dom.xpath.XPathEvaluator;
import org.w3c.dom.xpath.XPathResult;
import org.xml.sax.SAXException;

import javax.imageio.ImageIO;
import java.awt.*;
Expand Down Expand Up @@ -283,9 +280,14 @@ private void subColorElements() {
return loadSVG(getSVGDirectoryName(), getSVGFileName(pageIndex - firstPage));
}

protected void createDocument(int pageIndex, PageFormat pageFormat, boolean addMargin) {
/**
* @return true if the document was created successfully, otherwise false
*/
protected boolean createDocument(int pageIndex, PageFormat pageFormat, boolean addMargin) {
setSVGDocument(loadTemplate(pageIndex, pageFormat));

if (getSVGDocument() == null) {
return false;
}
subFonts((SVGDocument) getSVGDocument());
subColorElements();
SVGGeneratorContext context = SVGGeneratorContext.createDefault(getSVGDocument());
Expand All @@ -311,13 +313,16 @@ protected void createDocument(int pageIndex, PageFormat pageFormat, boolean addM
}
}
processImage(pageIndex - firstPage, pageFormat);
return true;
}

@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
Graphics2D g2d = (Graphics2D) graphics;
if (null != g2d) {
createDocument(pageIndex, pageFormat, true);
if (!createDocument(pageIndex, pageFormat, true)) {
return NO_SUCH_PAGE;
}
GraphicsNode node = build();
node.paint(g2d);
/* Testing code that outputs the generated svg
Expand All @@ -337,10 +342,12 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
return PAGE_EXISTS;
}

public InputStream exportPDF(int pageNumber, PageFormat pageFormat) throws TranscoderException, SAXException, IOException, ConfigurationException {
createDocument(pageNumber + firstPage, pageFormat, true);
public @Nullable InputStream exportPDF(int pageNumber, PageFormat pageFormat) throws Exception {
if (!createDocument(pageNumber + firstPage, pageFormat, true)) {
return null;
}
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
Configuration cfg = cfgBuilder.build(getClass().getResourceAsStream("fop-config.xml"));
Configuration cfg = cfgBuilder.build(getClass().getResourceAsStream("fop-config.xml")); // TODO : remove inline filename
PDFTranscoder transcoder = new PDFTranscoder();
transcoder.configure(cfg);
transcoder.addTranscodingHint(PDFTranscoder.KEY_AUTO_FONTS, false);
Expand Down
5 changes: 3 additions & 2 deletions megameklab/src/megameklab/printing/PrintSmallUnitSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ protected void processImage(int startPage, PageFormat pageFormat) {
Element g = getSVGDocument().getElementById("unit_" + count);
if (g != null) {
PrintEntity sheet = getBlockFor(entity, count);
sheet.createDocument(startPage, pageFormat, false);
g.appendChild(getSVGDocument().importNode(sheet.getSVGDocument().getDocumentElement(), true));
if (sheet.createDocument(startPage, pageFormat, false)) {
g.appendChild(getSVGDocument().importNode(sheet.getSVGDocument().getDocumentElement(), true));
}
}
count++;
}
Expand Down
9 changes: 7 additions & 2 deletions megameklab/src/megameklab/printing/RecordSheetTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.InputStream;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;

/**
Expand Down Expand Up @@ -189,7 +191,10 @@ public Void doInBackground() throws Exception {
final PrintRecordSheet rs = iter.next();
bookmarkNames.put(rs.getFirstPage(), rs.getBookmarkNames());
for (int i = 0; i < rs.getPageCount(); i++) {
merger.addSource(rs.exportPDF(i, pageFormat));
final InputStream is = rs.exportPDF(i, pageFormat);
if (is != null) {
merger.addSource(is);
}
}
iter.remove();
}
Expand All @@ -200,7 +205,7 @@ public Void doInBackground() throws Exception {
PDDocument doc = PDDocument.load(file);
PDDocumentOutline outline = new PDDocumentOutline();
doc.getDocumentCatalog().setDocumentOutline(outline);
for (Map.Entry<Integer, List<String>> entry : bookmarkNames.entrySet()) {
for (Entry<Integer, List<String>> entry : bookmarkNames.entrySet()) {
for (String name : entry.getValue()) {
PDOutlineItem bookmark = new PDOutlineItem();
bookmark.setDestination(doc.getPage(entry.getKey()));
Expand Down

0 comments on commit d5d594a

Please sign in to comment.