From 16061d90eb2b69b55e6ec700addededfbb260f4a Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 20 Mar 2022 12:04:32 -0400 Subject: [PATCH 1/3] Initial code cleanup --- .../printing/PrintCompositeTankSheet.java | 2 +- .../megameklab/printing/PrintRecordSheet.java | 90 ++++++++++--------- 2 files changed, 47 insertions(+), 45 deletions(-) diff --git a/megameklab/src/megameklab/printing/PrintCompositeTankSheet.java b/megameklab/src/megameklab/printing/PrintCompositeTankSheet.java index cefe4d355..47d22827a 100644 --- a/megameklab/src/megameklab/printing/PrintCompositeTankSheet.java +++ b/megameklab/src/megameklab/printing/PrintCompositeTankSheet.java @@ -80,7 +80,7 @@ public List getBookmarkNames() { } @Override - Document loadTemplate(int pageIndex, PageFormat pageFormat) { + protected @Nullable Document loadTemplate(int pageIndex, PageFormat pageFormat) { DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation(); Document doc = domImpl.createDocument(svgNS, SVGConstants.SVG_SVG_TAG, null); Element svgRoot = doc.getDocumentElement(); diff --git a/megameklab/src/megameklab/printing/PrintRecordSheet.java b/megameklab/src/megameklab/printing/PrintRecordSheet.java index ca483293c..89f30a5c8 100644 --- a/megameklab/src/megameklab/printing/PrintRecordSheet.java +++ b/megameklab/src/megameklab/printing/PrintRecordSheet.java @@ -208,14 +208,12 @@ private void subColorElements() { Element element = svgDocument.getElementById(RS_TEMPLATE); if (element != null) { String style = element.getAttributeNS(null, SVGConstants.SVG_STYLE_ATTRIBUTE); - if (style != null) { - for (String field : style.split(";")) { - if (field.startsWith(MML_COLOR_ELEMENTS + ":")) { - String[] ids = field.substring(field.indexOf(":") + 1).split(","); - for (String id : ids) { - hideElement(id + "Color", !options.useColor()); - hideElement(id + "BW", options.useColor()); - } + for (String field : style.split(";")) { + if (field.startsWith(MML_COLOR_ELEMENTS + ":")) { + String[] ids = field.substring(field.indexOf(":") + 1).split(","); + for (String id : ids) { + hideElement(id + "Color", !options.useColor()); + hideElement(id + "BW", options.useColor()); } } } @@ -228,21 +226,23 @@ private void subColorElements() { * @param filename The name of the SVG file * @return The document object */ - static @Nullable Document loadSVG(String dirName, String filename) { - File f = new File(dirName, filename); + private static @Nullable Document loadSVG(String directoryPath, String filename) { + final File file = new File(directoryPath, filename); + Document svgDocument = null; - try (InputStream is = new FileInputStream(f)) { - DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); - final String parser = XMLResourceDescriptor.getXMLParserClassName(); - SAXDocumentFactory df = new SAXDocumentFactory(impl, parser); - svgDocument = df.createDocument(f.toURI().toASCIIString(), is); + try (InputStream is = new FileInputStream(file)) { + SAXDocumentFactory df = new SAXDocumentFactory(SVGDOMImplementation.getDOMImplementation(), + XMLResourceDescriptor.getXMLParserClassName()); + svgDocument = df.createDocument(file.toURI().toASCIIString(), is); } catch (Exception ex) { LogManager.getLogger().error("", ex); } if (svgDocument == null) { - LogManager.getLogger().error("Failed to open SVG file! Path: data/images/recordsheets/" + filename); + LogManager.getLogger().error(String.format("Failed to open SVG file! Path: %s/%s", directoryPath, filename)); + return null; } + return svgDocument; } @@ -268,43 +268,45 @@ private void subColorElements() { * which is then filled in using the individual record sheet templates. * * @param pageIndex The index of this page in the print job - * @param pageFormat The page format seleted by the user + * @param pageFormat The page format selected by the user * @return An SVG document for one page of the print job */ - @Nullable Document loadTemplate(int pageIndex, PageFormat pageFormat) { + protected @Nullable Document loadTemplate(int pageIndex, PageFormat pageFormat) { return loadSVG(getSVGDirectoryName(), getSVGFileName(pageIndex - firstPage)); } - void createDocument(int pageIndex, PageFormat pageFormat, boolean addMargin) { + protected void createDocument(int pageIndex, PageFormat pageFormat, boolean addMargin) { svgDocument = loadTemplate(pageIndex, pageFormat); - if (null != svgDocument) { - subFonts((SVGDocument) svgDocument); - subColorElements(); - SVGGeneratorContext context = SVGGeneratorContext.createDefault(svgDocument); - svgGenerator = new SVGGraphics2D(context, false); - double ratio = Math.min(pageFormat.getImageableWidth() / (options.getPaperSize().pxWidth - 36), - pageFormat.getPaper().getImageableHeight() / (options.getPaperSize().pxHeight - 36)); - if ((pageIndex == firstPage) && includeReferenceCharts()) { - ratio *= TABLE_RATIO; - } - Element svgRoot = svgDocument.getDocumentElement(); - svgRoot.setAttributeNS(null, SVGConstants.SVG_WIDTH_ATTRIBUTE, String.valueOf(pageFormat.getWidth())); - svgRoot.setAttributeNS(null, SVGConstants.SVG_HEIGHT_ATTRIBUTE, String.valueOf(pageFormat.getHeight())); - Element g = svgDocument.getElementById(RS_TEMPLATE); - if (g != null) { - if (addMargin) { - 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())); - } else { - g.setAttributeNS(null, SVGConstants.SVG_TRANSFORM_ATTRIBUTE, - String.format("%s(%f %f)", SVGConstants.SVG_SCALE_ATTRIBUTE, - ratio, ratio)); - } + if (svgDocument == null) { + return; + } + + subFonts((SVGDocument) svgDocument); + subColorElements(); + SVGGeneratorContext context = SVGGeneratorContext.createDefault(svgDocument); + svgGenerator = new SVGGraphics2D(context, false); + double ratio = Math.min(pageFormat.getImageableWidth() / (options.getPaperSize().pxWidth - 36), + pageFormat.getPaper().getImageableHeight() / (options.getPaperSize().pxHeight - 36)); + if ((pageIndex == firstPage) && includeReferenceCharts()) { + ratio *= TABLE_RATIO; + } + Element svgRoot = svgDocument.getDocumentElement(); + svgRoot.setAttributeNS(null, SVGConstants.SVG_WIDTH_ATTRIBUTE, String.valueOf(pageFormat.getWidth())); + svgRoot.setAttributeNS(null, SVGConstants.SVG_HEIGHT_ATTRIBUTE, String.valueOf(pageFormat.getHeight())); + Element g = svgDocument.getElementById(RS_TEMPLATE); + if (g != null) { + if (addMargin) { + 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())); + } else { + g.setAttributeNS(null, SVGConstants.SVG_TRANSFORM_ATTRIBUTE, + String.format("%s(%f %f)", SVGConstants.SVG_SCALE_ATTRIBUTE, + ratio, ratio)); } - processImage(pageIndex - firstPage, pageFormat); } + processImage(pageIndex - firstPage, pageFormat); } @Override From e9d2cda9f0784affd2e7e337fc4ef1b828243aaf Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 20 Mar 2022 12:59:59 -0400 Subject: [PATCH 2/3] More code cleanup and removing unused constructors --- .../src/megameklab/printing/PrintAero.java | 12 +--- .../megameklab/printing/PrintBattleArmor.java | 11 --- .../src/megameklab/printing/PrintEntity.java | 31 ++++---- .../megameklab/printing/PrintInfantry.java | 13 +--- .../src/megameklab/printing/PrintMech.java | 10 --- .../megameklab/printing/PrintProtomech.java | 11 --- .../megameklab/printing/PrintRecordSheet.java | 72 ++++++++++--------- .../printing/PrintSmallUnitSheet.java | 1 - .../src/megameklab/printing/PrintTank.java | 19 ++--- .../src/megameklab/util/UnitPrintManager.java | 29 ++++---- 10 files changed, 72 insertions(+), 137 deletions(-) diff --git a/megameklab/src/megameklab/printing/PrintAero.java b/megameklab/src/megameklab/printing/PrintAero.java index 27b169cad..d8fc87907 100644 --- a/megameklab/src/megameklab/printing/PrintAero.java +++ b/megameklab/src/megameklab/printing/PrintAero.java @@ -53,16 +53,6 @@ public PrintAero(Aero aero, int startPage, RecordSheetOptions options) { this.aero = aero; } - /** - * Creates an SVG object for the record sheet using the global printing options - * - * @param aero The aerospace unit to print - * @param startPage The print job page number for this sheet - */ - public PrintAero(Aero aero, int startPage) { - this(aero, startPage, new RecordSheetOptions()); - } - @Override protected String getSVGFileName(int pageNumber) { if (aero instanceof SmallCraft) { @@ -332,4 +322,4 @@ protected void addReferenceCharts(PageFormat pageFormat) { height * 0.5 - 3.0)); } } -} \ No newline at end of file +} diff --git a/megameklab/src/megameklab/printing/PrintBattleArmor.java b/megameklab/src/megameklab/printing/PrintBattleArmor.java index 21d120faf..d1239f562 100644 --- a/megameklab/src/megameklab/printing/PrintBattleArmor.java +++ b/megameklab/src/megameklab/printing/PrintBattleArmor.java @@ -47,17 +47,6 @@ public PrintBattleArmor(BattleArmor battleArmor, int squadIndex, int startPage, this.squadIndex = squadIndex; } - /** - * Creates an SVG object for the record sheet using the global printing options - * - * @param battleArmor The BattleArmor to print - * @param squadIndex The index of this unit on the page - * @param startPage The print job page number for this sheet - */ - public PrintBattleArmor(BattleArmor battleArmor, int squadIndex, int startPage) { - this(battleArmor, startPage, squadIndex, new RecordSheetOptions()); - } - @Override protected String getSVGFileName(int pageNumber) { return "battle_armor_squad.svg"; diff --git a/megameklab/src/megameklab/printing/PrintEntity.java b/megameklab/src/megameklab/printing/PrintEntity.java index b950577d1..6c8c7c122 100644 --- a/megameklab/src/megameklab/printing/PrintEntity.java +++ b/megameklab/src/megameklab/printing/PrintEntity.java @@ -1,5 +1,5 @@ /* - * MegaMekLab - Copyright (C) 2017 - The MegaMek Team + * MegaMekLab - Copyright (c) 2017-2022 - The MegaMek Team. All Rights Reserved. * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software @@ -13,16 +13,12 @@ */ package megameklab.printing; -import java.awt.geom.Rectangle2D; -import java.awt.print.PageFormat; -import java.io.File; -import java.text.DecimalFormat; -import java.text.NumberFormat; -import java.util.*; - import megamek.client.generator.RandomNameGenerator; import megamek.common.*; -import megameklab.printing.reference.ReferenceTable; +import megamek.common.options.IOption; +import megamek.common.options.IOptionGroup; +import megamek.common.options.PilotOptions; +import megamek.common.options.Quirks; import megameklab.util.CConfig; import org.apache.batik.anim.dom.SVGGraphicsElement; import org.apache.batik.anim.dom.SVGLocatableSupport; @@ -32,16 +28,17 @@ import org.w3c.dom.svg.SVGRectElement; import org.w3c.dom.svg.SVGTextContentElement; -import megamek.common.options.IOption; -import megamek.common.options.IOptionGroup; -import megamek.common.options.PilotOptions; -import megamek.common.options.Quirks; +import java.awt.geom.Rectangle2D; +import java.awt.print.PageFormat; +import java.io.File; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.*; /** * Base class for printing Entity record sheets * * @author Neoancient - * */ public abstract class PrintEntity extends PrintRecordSheet { @@ -252,7 +249,7 @@ protected void writeTextFields() { if (null != element) { double offset = nameOffset; String prev = element.getAttribute(SVGConstants.SVG_X_ATTRIBUTE); - if (null != prev) { + if (!prev.isBlank()) { offset += Double.parseDouble(prev); } else { offset += ((SVGTextContentElement) element).getStartPositionOfChar(0).getX(); @@ -282,7 +279,7 @@ protected void writeTextFields() { if (rect instanceof SVGRectElement) { Rectangle2D bbox = getRectBBox((SVGRectElement) rect); Element canvas = (Element) rect.getParentNode(); - String spaText = "Abilities: " + spaList.toString(); + String spaText = "Abilities: " + spaList; float fontSize = FONT_SIZE_MEDIUM; if (getTextLength(spaText, fontSize) > bbox.getWidth()) { fontSize = (float) bbox.getHeight() / 2.4f; @@ -508,7 +505,7 @@ void drawHeatSinkPips(SVGRectElement svgRect, int hsCount) { // First check whether we can shrink them less than what is required for a new column if (cols * (int) (rows * nextCol) > hsCount) { rows = (int) Math.ceil((double) hsCount / cols); - size = (double) viewHeight / rows; + size = viewHeight / rows; } else { cols++; size *= viewWidth / (cols * size); diff --git a/megameklab/src/megameklab/printing/PrintInfantry.java b/megameklab/src/megameklab/printing/PrintInfantry.java index 865b847a6..8717975f3 100644 --- a/megameklab/src/megameklab/printing/PrintInfantry.java +++ b/megameklab/src/megameklab/printing/PrintInfantry.java @@ -11,7 +11,6 @@ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. */ - package megameklab.printing; import megamek.common.*; @@ -48,16 +47,6 @@ public PrintInfantry(Infantry infantry, int startPage, RecordSheetOptions option this.infantry = infantry; } - /** - * Creates an SVG object for the record sheet using the global printing options - * - * @param infantry The infantry to print - * @param startPage The print job page number for this sheet - */ - public PrintInfantry(Infantry infantry, int startPage) { - this(infantry, startPage, new RecordSheetOptions()); - } - @Override protected String getSVGFileName(int pageNumber) { return "conventional_infantry_platoon.svg"; @@ -368,7 +357,7 @@ protected void drawArmor() { sj.add("ECM"); } if (sj.length() > 0) { - setTextField(ARMOR_KIT, "Sneak(" + sj.toString() + ")"); + setTextField(ARMOR_KIT, "Sneak(" + sj + ")"); } } setTextField(ARMOR_DIVISOR, infantry.calcDamageDivisor() diff --git a/megameklab/src/megameklab/printing/PrintMech.java b/megameklab/src/megameklab/printing/PrintMech.java index 30911f2c2..d09f8bf3d 100644 --- a/megameklab/src/megameklab/printing/PrintMech.java +++ b/megameklab/src/megameklab/printing/PrintMech.java @@ -61,16 +61,6 @@ public PrintMech(Mech mech, int startPage, RecordSheetOptions options) { this.mech = mech; } - /** - * Creates an SVG object for the record sheet using the global printing options - * - * @param mech The mech to print - * @param startPage The print job page number for this sheet - */ - public PrintMech(Mech mech, int startPage) { - this(mech, startPage, new RecordSheetOptions()); - } - @Override protected String getSVGFileName(int pageNumber) { String base; diff --git a/megameklab/src/megameklab/printing/PrintProtomech.java b/megameklab/src/megameklab/printing/PrintProtomech.java index f99781d7e..131132180 100644 --- a/megameklab/src/megameklab/printing/PrintProtomech.java +++ b/megameklab/src/megameklab/printing/PrintProtomech.java @@ -41,17 +41,6 @@ public PrintProtomech(Protomech proto, int startPage, int unitIndex, RecordSheet this.unitIndex = unitIndex; } - /** - * Creates an SVG object for the record sheet using the global printing options - * - * @param proto The protomech to print - * @param startPage The print job page number for this sheet - * @param unitIndex The index of this unit on the page - */ - public PrintProtomech(Protomech proto, int startPage, int unitIndex) { - this(proto, startPage, unitIndex, new RecordSheetOptions()); - } - @Override protected String getSVGFileName(int pageNumber) { if (proto.isQuad()) { diff --git a/megameklab/src/megameklab/printing/PrintRecordSheet.java b/megameklab/src/megameklab/printing/PrintRecordSheet.java index 89f30a5c8..ce58d1653 100644 --- a/megameklab/src/megameklab/printing/PrintRecordSheet.java +++ b/megameklab/src/megameklab/printing/PrintRecordSheet.java @@ -124,10 +124,14 @@ protected final int getFirstPage() { return firstPage; } - public final Document getSVGDocument() { + public final @Nullable Document getSVGDocument() { return svgDocument; } + public final void setSVGDocument(final @Nullable Document svgDocument) { + this.svgDocument = svgDocument; + } + /** * Provides a callback function that can be used to provide updates on printing progress. * As each page is rendered, the callback is invoked with the page number. @@ -205,7 +209,7 @@ private void subFonts(SVGDocument doc) { } private void subColorElements() { - Element element = svgDocument.getElementById(RS_TEMPLATE); + Element element = getSVGDocument().getElementById(RS_TEMPLATE); if (element != null) { String style = element.getAttributeNS(null, SVGConstants.SVG_STYLE_ATTRIBUTE); for (String field : style.split(";")) { @@ -226,8 +230,12 @@ private void subColorElements() { * @param filename The name of the SVG file * @return The document object */ - private static @Nullable Document loadSVG(String directoryPath, String filename) { + private @Nullable Document loadSVG(String directoryPath, String filename) { final File file = new File(directoryPath, filename); + if (!file.exists()) { + LogManager.getLogger().error(String.format("SVG file does not exist at path: %s/%s", directoryPath, filename)); + return null; + } Document svgDocument = null; try (InputStream is = new FileInputStream(file)) { @@ -272,29 +280,25 @@ private void subColorElements() { * @return An SVG document for one page of the print job */ protected @Nullable Document loadTemplate(int pageIndex, PageFormat pageFormat) { - return loadSVG(getSVGDirectoryName(), - getSVGFileName(pageIndex - firstPage)); + return loadSVG(getSVGDirectoryName(), getSVGFileName(pageIndex - firstPage)); } protected void createDocument(int pageIndex, PageFormat pageFormat, boolean addMargin) { - svgDocument = loadTemplate(pageIndex, pageFormat); - if (svgDocument == null) { - return; - } + setSVGDocument(loadTemplate(pageIndex, pageFormat)); - subFonts((SVGDocument) svgDocument); + subFonts((SVGDocument) getSVGDocument()); subColorElements(); - SVGGeneratorContext context = SVGGeneratorContext.createDefault(svgDocument); + SVGGeneratorContext context = SVGGeneratorContext.createDefault(getSVGDocument()); svgGenerator = new SVGGraphics2D(context, false); double ratio = Math.min(pageFormat.getImageableWidth() / (options.getPaperSize().pxWidth - 36), pageFormat.getPaper().getImageableHeight() / (options.getPaperSize().pxHeight - 36)); if ((pageIndex == firstPage) && includeReferenceCharts()) { ratio *= TABLE_RATIO; } - Element svgRoot = svgDocument.getDocumentElement(); + Element svgRoot = getSVGDocument().getDocumentElement(); svgRoot.setAttributeNS(null, SVGConstants.SVG_WIDTH_ATTRIBUTE, String.valueOf(pageFormat.getWidth())); svgRoot.setAttributeNS(null, SVGConstants.SVG_HEIGHT_ATTRIBUTE, String.valueOf(pageFormat.getHeight())); - Element g = svgDocument.getElementById(RS_TEMPLATE); + Element g = getSVGDocument().getElementById(RS_TEMPLATE); if (g != null) { if (addMargin) { g.setAttributeNS(null, SVGConstants.SVG_TRANSFORM_ATTRIBUTE, @@ -330,7 +334,7 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) { if (callback != null) { callback.accept(pageIndex); } - return Printable.PAGE_EXISTS; + return PAGE_EXISTS; } public InputStream exportPDF(int pageNumber, PageFormat pageFormat) throws TranscoderException, SAXException, IOException, ConfigurationException { @@ -370,7 +374,7 @@ public SVGDocument getBrokenLinkDocument(Element e, String url, String message) } }); ctx.setDynamic(true); - return builder.build(ctx, svgDocument); + return builder.build(ctx, getSVGDocument()); } /** @@ -393,15 +397,15 @@ protected void processImage(int pageNum, PageFormat pageFormat) { } String getSVGDirectoryName() { - return "data/images/recordsheets/" + options.getPaperSize().dirName; + return "data/images/recordsheets/" + options.getPaperSize().dirName; // TODO : Remove inline file path } /** - * @param pageNumber The page number in the current record sheet, where the first page is numberd zero. + * @param pageNumber The page number in the current record sheet, where the first page is numbered zero. * @return The file name for the current page in the record sheet image directory */ protected abstract String getSVGFileName(int pageNumber); - + /** * @return The title to use for the record sheet */ @@ -413,15 +417,15 @@ String getSVGDirectoryName() { * @return Names of outline entries */ public abstract List getBookmarkNames(); - + protected void setTextField(String id, int i) { setTextField(id, String.valueOf(i)); } - + protected void setTextField(String id, String text) { setTextField(id, text, false); } - + /** * Sets the text content of the text element in the SVG diagram corresponding with the given id. * If the element does not exist, does nothing. If the text is null, hides the element instead. @@ -432,7 +436,7 @@ protected void setTextField(String id, String text) { * @param unhide Sets the element visible if the text is non-null */ protected void setTextField(String id, String text, boolean unhide) { - Element element = svgDocument.getElementById(id); + Element element = getSVGDocument().getElementById(id); if (null != element) { if (null == text) { hideElement(element, true); @@ -467,8 +471,8 @@ protected void setTextField(String id, String text, boolean unhide) { } /** - * Convenience method for creating a new SVG Text element and adding it to the parent. The width of the text is - * returned, to aid in layout. + * Convenience method for creating a new SVG Text element and adding it to the parent. The width + * of the text is returned, to aid in layout. * * @param parent The SVG element to add the text element to. * @param x The X position of the new element. @@ -486,8 +490,8 @@ protected double addTextElement(Element parent, double x, double y, String text, } /** - * Convenience method for creating a new SVG Text element and adding it to the parent. The height of the text is - * returned, to aid in layout. + * Convenience method for creating a new SVG Text element and adding it to the parent. The + * height of the text is returned, to aid in layout. * * @param parent The SVG element to add the text element to. * @param x The X position of the new element. @@ -502,7 +506,7 @@ protected double addTextElement(Element parent, double x, double y, String text, */ protected double addTextElement(Element parent, double x, double y, String text, float fontSize, String anchor, String weight, String fill) { - Element newText = svgDocument.createElementNS(svgNS, SVGConstants.SVG_TEXT_TAG); + Element newText = getSVGDocument().createElementNS(svgNS, SVGConstants.SVG_TEXT_TAG); newText.setTextContent(text); newText.setAttributeNS(null, SVGConstants.SVG_X_ATTRIBUTE, String.valueOf(x)); newText.setAttributeNS(null, SVGConstants.SVG_Y_ATTRIBUTE, String.valueOf(y)); @@ -552,7 +556,7 @@ protected void addTextElementToFit(Element parent, double x, double y, double wi */ protected void addTextElementToFit(Element parent, double x, double y, double width, String text, float fontSize, String anchor, String weight, String fill) { - Element newText = svgDocument.createElementNS(svgNS, SVGConstants.SVG_TEXT_TAG); + Element newText = getSVGDocument().createElementNS(svgNS, SVGConstants.SVG_TEXT_TAG); newText.setTextContent(text); newText.setAttributeNS(null, SVGConstants.SVG_X_ATTRIBUTE, String.valueOf(x)); newText.setAttributeNS(null, SVGConstants.SVG_Y_ATTRIBUTE, String.valueOf(y)); @@ -617,7 +621,7 @@ protected int addMultilineTextElement(Element canvas, double x, double y, double // The index of the character after the most recent delimiter found. Everything in text // up to pos will fit in the available space. int pos = 0; - while (text.length() > 0) { + while (!text.isBlank()) { // If the remaining text fits, add a line and exit. if (getTextLength(text, fontSize) <= width) { addTextElement(canvas, x, y, text, fontSize, anchor, weight, fill); @@ -672,7 +676,7 @@ protected Element createPip(double x, double y, double radius, double strokeWidt */ protected Element createPip(double x, double y, double radius, double strokeWidth, PipType type, String fill) { - Element path = svgDocument.createElementNS(svgNS, SVGConstants.SVG_PATH_TAG); + Element path = getSVGDocument().createElementNS(svgNS, SVGConstants.SVG_PATH_TAG); path.setAttributeNS(null, SVGConstants.SVG_FILL_ATTRIBUTE, fill); path.setAttributeNS(null, SVGConstants.SVG_STROKE_ATTRIBUTE, FILL_BLACK); path.setAttributeNS(null, SVGConstants.SVG_STROKE_WIDTH_ATTRIBUTE, Double.toString(strokeWidth)); @@ -714,7 +718,7 @@ protected Element createPip(double x, double y, double radius, double strokeWidt protected Element createRoundedRectangle(double x, double y, double width, double height, double radius, double control, double strokeWidth, String stroke) { - Element path = svgDocument.createElementNS(svgNS, SVGConstants.SVG_PATH_ATTRIBUTE); + Element path = getSVGDocument().createElementNS(svgNS, SVGConstants.SVG_PATH_ATTRIBUTE); path.setAttributeNS(null, SVGConstants.CSS_FILL_PROPERTY, SVGConstants.SVG_NONE_VALUE); path.setAttributeNS(null, SVGConstants.CSS_STROKE_PROPERTY, stroke); path.setAttributeNS(null, SVGConstants.CSS_STROKE_WIDTH_PROPERTY, String.valueOf(strokeWidth)); @@ -734,14 +738,14 @@ protected Element createRoundedRectangle(double x, double y, double width, doubl } protected void hideElement(String id) { - Element element = svgDocument.getElementById(id); + Element element = getSVGDocument().getElementById(id); if (null != element) { hideElement(element, true); } } protected void hideElement(String id, boolean hide) { - Element element = svgDocument.getElementById(id); + Element element = getSVGDocument().getElementById(id); if (null != element) { hideElement(element, hide); } @@ -831,7 +835,7 @@ public void embedImage(@Nullable File imageFile, Element canvas, Rectangle2D bbo x += (bbox.getWidth() - width) / 2; y += (bbox.getHeight() - height) / 2; } - Element img = svgDocument.createElementNS(svgNS, SVGConstants.SVG_IMAGE_TAG); + Element img = getSVGDocument().createElementNS(svgNS, SVGConstants.SVG_IMAGE_TAG); img.setAttributeNS(null, SVGConstants.SVG_X_ATTRIBUTE, Double.toString(x)); img.setAttributeNS(null, SVGConstants.SVG_Y_ATTRIBUTE, Double.toString(y)); img.setAttributeNS(null, SVGConstants.SVG_WIDTH_ATTRIBUTE, Double.toString(width)); diff --git a/megameklab/src/megameklab/printing/PrintSmallUnitSheet.java b/megameklab/src/megameklab/printing/PrintSmallUnitSheet.java index dc666a3de..4cd658a3a 100644 --- a/megameklab/src/megameklab/printing/PrintSmallUnitSheet.java +++ b/megameklab/src/megameklab/printing/PrintSmallUnitSheet.java @@ -11,7 +11,6 @@ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. */ - package megameklab.printing; import megamek.common.*; diff --git a/megameklab/src/megameklab/printing/PrintTank.java b/megameklab/src/megameklab/printing/PrintTank.java index a719a057f..327d43978 100644 --- a/megameklab/src/megameklab/printing/PrintTank.java +++ b/megameklab/src/megameklab/printing/PrintTank.java @@ -48,16 +48,6 @@ public PrintTank(Tank tank, int startPage, RecordSheetOptions options) { this.tank = tank; } - /** - * Creates an SVG object for the record sheet using the global printing options - * - * @param tank The tank to print - * @param startPage The print job page number for this sheet - */ - public PrintTank(Tank tank, int startPage) { - this(tank, startPage, new RecordSheetOptions()); - } - @Override public Entity getEntity() { return tank; @@ -82,7 +72,7 @@ protected String getSVGFileName(int pageNumber) { subtype = "wige"; break; } - //fall through + // fall through default: subtype = "vehicle"; break; @@ -165,7 +155,8 @@ protected String formatRun() { @Override public String formatFeatures() { StringJoiner sj = new StringJoiner(", "); - List chassisMods = tank.getMisc().stream().filter(m -> m.getType().hasFlag(MiscType.F_CHASSIS_MODIFICATION)) + List chassisMods = tank.getMisc().stream() + .filter(m -> m.getType().hasFlag(MiscType.F_CHASSIS_MODIFICATION)) .map(m -> m.getType().getShortName()) .collect(Collectors.toList()); if (!chassisMods.isEmpty()) { @@ -201,9 +192,7 @@ public String formatFeatures() { @Override protected void drawFluffImage() { File f; - if (tank.getMovementMode().equals(EntityMovementMode.NAVAL) - || tank.getMovementMode().equals(EntityMovementMode.HYDROFOIL) - || tank.getMovementMode().equals(EntityMovementMode.SUBMARINE)) { + if (tank.getMovementMode().isMarine()) { f = ImageHelper.getFluffFile(tank, ImageHelper.imageNaval); } else if (tank instanceof LargeSupportTank) { f = ImageHelper.getFluffFile(tank, ImageHelper.imageLargeSupportVehicle); diff --git a/megameklab/src/megameklab/util/UnitPrintManager.java b/megameklab/src/megameklab/util/UnitPrintManager.java index 24a6d7ab2..d2c828cea 100644 --- a/megameklab/src/megameklab/util/UnitPrintManager.java +++ b/megameklab/src/megameklab/util/UnitPrintManager.java @@ -120,22 +120,19 @@ public static File getExportFile(Frame parent) { } private static File getExportFile(Frame parent, String suggestedFileName) { - JFileChooser f; - FileNameExtensionFilter filter; - int returnVal; - f = new JFileChooser(System.getProperty("user.dir")); + JFileChooser f = new JFileChooser(System.getProperty("user.dir")); f.setLocation(parent.getLocation().x + 150, parent.getLocation().y + 100); f.setDialogTitle("Choose export file name"); f.setMultiSelectionEnabled(false); if (!suggestedFileName.isEmpty()) { f.setSelectedFile(new File(suggestedFileName)); } - filter = new FileNameExtensionFilter("PDF files", "pdf"); + FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF files", "pdf"); // Add a filter for mul files f.setFileFilter(filter); - returnVal = f.showSaveDialog(parent); + int returnVal = f.showSaveDialog(parent); if (returnVal != JFileChooser.APPROVE_OPTION) { // I want a file, y'know! return null; @@ -158,7 +155,7 @@ private static List createSheets(List entities, boolea UnitUtil.removeOneShotAmmo(unit); UnitUtil.expandUnitMounts((Mech) unit); sheets.add(new PrintMech((Mech) unit, pageCount++, options)); - } else if ((unit instanceof Tank) && ((unit.getMovementMode() == EntityMovementMode.NAVAL) || (unit.getMovementMode() == EntityMovementMode.SUBMARINE) || (unit.getMovementMode() == EntityMovementMode.HYDROFOIL))) { + } else if ((unit instanceof Tank) && unit.getMovementMode().isMarine()) { sheets.add(new PrintTank((Tank) unit, pageCount++, options)); } else if (unit instanceof Tank) { if (singlePrint || options.showReferenceCharts()) { @@ -206,12 +203,11 @@ private static List createSheets(List entities, boolea protoList = new ArrayList<>(); } } else { - //TODO: show a message dialog that lists the unprintable units unprintable.add(unit); } } - if (unprintable.size() > 0) { + if (!unprintable.isEmpty()) { JOptionPane.showMessageDialog(null, "Exporting is not currently supported for the following units:\n" + unprintable.stream().map(en -> en.getChassis() + " " + en.getModel()) .collect(Collectors.joining("\n"))); @@ -220,13 +216,16 @@ private static List createSheets(List entities, boolea if (null != tank1) { sheets.add(new PrintCompositeTankSheet(tank1, null, pageCount++)); } - if (baList.size() > 0) { + + if (!baList.isEmpty()) { sheets.add(new PrintSmallUnitSheet(baList, pageCount++)); } - if (infList.size() > 0) { + + if (!infList.isEmpty()) { sheets.add(new PrintSmallUnitSheet(infList, pageCount++)); } - if (protoList.size() > 0) { + + if (!protoList.isEmpty()) { sheets.add(new PrintSmallUnitSheet(protoList, pageCount)); } return sheets; @@ -259,7 +258,7 @@ public static void printAllUnits(List loadedUnits, boolean singlePrint) * @param options The options to use for this print job */ public static void printAllUnits(List loadedUnits, boolean singlePrint, - RecordSheetOptions options) { + RecordSheetOptions options) { HashPrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); aset.add(options.getPaperSize().sizeName); aset.add(options.getPaperSize().printableArea); @@ -277,7 +276,7 @@ public static void printAllUnits(List loadedUnits, boolean singlePrint, if (loadedUnits.size() > 1) { masterPrintJob.setJobName(loadedUnits.get(0).getShortNameRaw() + " etc"); - } else if (loadedUnits.size() > 0) { + } else if (!loadedUnits.isEmpty()) { masterPrintJob.setJobName(loadedUnits.get(0).getShortNameRaw()); } @@ -430,4 +429,4 @@ public static void printUnitFile(JFrame parent, boolean singleUnit, boolean pdf) LogManager.getLogger().error("", ex); } } -} \ No newline at end of file +} From d5d594a2e88a4f9d0d98f88a022de767d6d9a432 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 20 Mar 2022 13:13:51 -0400 Subject: [PATCH 3/3] 1068: Handling null source image document creation issues --- .../printing/PrintCompositeTankSheet.java | 41 +++++++++++-------- .../megameklab/printing/PrintRecordSheet.java | 25 +++++++---- .../printing/PrintSmallUnitSheet.java | 5 ++- .../megameklab/printing/RecordSheetTask.java | 9 +++- 4 files changed, 51 insertions(+), 29 deletions(-) diff --git a/megameklab/src/megameklab/printing/PrintCompositeTankSheet.java b/megameklab/src/megameklab/printing/PrintCompositeTankSheet.java index 47d22827a..28441f351 100644 --- a/megameklab/src/megameklab/printing/PrintCompositeTankSheet.java +++ b/megameklab/src/megameklab/printing/PrintCompositeTankSheet.java @@ -94,16 +94,21 @@ 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) { @@ -111,14 +116,18 @@ protected void processImage(int startPage, PageFormat pageFormat) { } 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); } diff --git a/megameklab/src/megameklab/printing/PrintRecordSheet.java b/megameklab/src/megameklab/printing/PrintRecordSheet.java index ce58d1653..f807dad53 100644 --- a/megameklab/src/megameklab/printing/PrintRecordSheet.java +++ b/megameklab/src/megameklab/printing/PrintRecordSheet.java @@ -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; @@ -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.*; @@ -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()); @@ -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 @@ -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); diff --git a/megameklab/src/megameklab/printing/PrintSmallUnitSheet.java b/megameklab/src/megameklab/printing/PrintSmallUnitSheet.java index 4cd658a3a..56ac1649a 100644 --- a/megameklab/src/megameklab/printing/PrintSmallUnitSheet.java +++ b/megameklab/src/megameklab/printing/PrintSmallUnitSheet.java @@ -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++; } diff --git a/megameklab/src/megameklab/printing/RecordSheetTask.java b/megameklab/src/megameklab/printing/RecordSheetTask.java index d08183269..0622cf15c 100644 --- a/megameklab/src/megameklab/printing/RecordSheetTask.java +++ b/megameklab/src/megameklab/printing/RecordSheetTask.java @@ -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; /** @@ -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(); } @@ -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> entry : bookmarkNames.entrySet()) { + for (Entry> entry : bookmarkNames.entrySet()) { for (String name : entry.getValue()) { PDOutlineItem bookmark = new PDOutlineItem(); bookmark.setDestination(doc.getPage(entry.getKey()));