Skip to content

Commit

Permalink
Logger to MMLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
rjhancock committed Sep 12, 2024
1 parent c35dbea commit c34b2f4
Show file tree
Hide file tree
Showing 49 changed files with 1,395 additions and 838 deletions.
253 changes: 161 additions & 92 deletions megameklab/src/megameklab/printing/ArmorPipLayout.java

Large diffs are not rendered by default.

66 changes: 36 additions & 30 deletions megameklab/src/megameklab/printing/PrintCapitalShip.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@
*/
package megameklab.printing;

import megamek.common.Jumpship;
import megamek.common.SpaceStation;
import megamek.common.UnitType;
import megamek.common.Warship;
import java.awt.geom.Rectangle2D;

import org.apache.batik.util.SVGConstants;
import org.apache.logging.log4j.LogManager;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGElement;
import org.w3c.dom.svg.SVGRectElement;

import java.awt.geom.Rectangle2D;
import megamek.common.Jumpship;
import megamek.common.SpaceStation;
import megamek.common.UnitType;
import megamek.common.Warship;
import megamek.logging.MMLogger;

/**
* Generates a record sheet image for JumpShips, WarShips, and space stations.
Expand All @@ -32,6 +33,7 @@
* @author Neoancient
*/
public class PrintCapitalShip extends PrintDropship {
private static final MMLogger logger = MMLogger.create(PrintCapitalShip.class);

/** Default width for armor pip */
public static final double ARMOR_PIP_WIDTH = 4.5;
Expand Down Expand Up @@ -62,11 +64,11 @@ public class PrintCapitalShip extends PrintDropship {
* Creates an SVG object for the record sheet
*
* @param ship
* The ship to print
* The ship to print
* @param startPage
* The print job page number for this sheet
* The print job page number for this sheet
* @param options
* Overrides the global options for which elements are printed
* Overrides the global options for which elements are printed
*/
public PrintCapitalShip(Jumpship ship, int startPage, RecordSheetOptions options) {
super(ship, startPage, options);
Expand All @@ -77,9 +79,9 @@ public PrintCapitalShip(Jumpship ship, int startPage, RecordSheetOptions options
* Creates an SVG object for the record sheet using the global printing options
*
* @param ship
* The ship to print
* The ship to print
* @param startPage
* The print job page number for this sheet
* The print job page number for this sheet
*/
public PrintCapitalShip(Jumpship ship, int startPage) {
this(ship, startPage, new RecordSheetOptions());
Expand Down Expand Up @@ -173,7 +175,7 @@ protected void drawArmorStructurePips() {
if (element instanceof SVGRectElement) {
printArmorRegion((SVGRectElement) element, ship.getOArmor(loc));
} else {
LogManager.getLogger().error("No SVGRectElement found with id " + id);
logger.error("No SVGRectElement found with id " + id);
}
}
}
Expand All @@ -182,12 +184,13 @@ protected void drawArmorStructurePips() {
* Print pips for some internal structure region.
*
* @param rectId
* The id of the rectangle element that describes the outline of the
* region to print pips
* The id of the rectangle element that describes the
* outline of the
* region to print pips
* @param structure
* The number of structure pips
* The number of structure pips
* @param pipsPerBlock
* The maximum number of pips to draw in a single block
* The maximum number of pips to draw in a single block
*/
private void printInternalRegion(String rectId, int structure, int pipsPerBlock) {
Element element = getSVGDocument().getElementById(rectId);
Expand All @@ -200,12 +203,13 @@ private void printInternalRegion(String rectId, int structure, int pipsPerBlock)
* Print pips for some internal structure region.
*
* @param svgRect
* The rectangle that describes the outline of the region to print
* pips
* The rectangle that describes the outline of the region to
* print
* pips
* @param structure
* The number of structure pips
* The number of structure pips
* @param pipsPerBlock
* The maximum number of pips to draw in a single block
* The maximum number of pips to draw in a single block
*/
private void printInternalRegion(SVGRectElement svgRect, int structure, int pipsPerBlock) {
Rectangle2D bbox = getRectBBox(svgRect);
Expand All @@ -231,10 +235,11 @@ private void printInternalRegion(SVGRectElement svgRect, int structure, int pips
* Method to determine rectangle grid for armor or internal pips and draw it.
*
* @param svgRect
* A rectangle that outlines the border of the space for the armor
* block.
* A rectangle that outlines the border of the space for the
* armor
* block.
* @param armor
* The amount of armor in the location
* The amount of armor in the location
*/
private void printArmorRegion(SVGRectElement svgRect, int armor) {
Rectangle2D bbox = getRectBBox(svgRect);
Expand Down Expand Up @@ -277,7 +282,8 @@ private void printArmorRegion(SVGRectElement svgRect, int armor) {
// Center on edge closest to ship outline
final double startX = bbox.getX() + ((bbox.getWidth() - (blockWidth * cols - ARMOR_PIP_WIDTH)) / 2.0);
int leftOver = armor % (MAX_PIP_ROWS * PIPS_PER_ROW);
// Partial rows are automatically centered horizontally. But if we have an incomplete block
// Partial rows are automatically centered horizontally. But if we have an
// incomplete block
// that is the only one on the row, we should adjust the starting y as well.
double actualHeight = blockHeight * rows;
if (leftOver > 0 && (cols == 1 || numBlocks % cols == 1)) {
Expand Down Expand Up @@ -310,15 +316,15 @@ private void printArmorRegion(SVGRectElement svgRect, int armor) {
* armor. Any unprinted armor pips are returned.
*
* @param startX
* The x coordinate of the top left of the block
* The x coordinate of the top left of the block
* @param startY
* The y coordinate of the top left of the block
* The y coordinate of the top left of the block
* @param parent
* The parent node of the bounding rectangle
* The parent node of the bounding rectangle
* @param numPips
* The number of pips to print
* The number of pips to print
* @param shadow
* Whether to add a drop shadow
* Whether to add a drop shadow
* @return The Y location of the end of the block
*/
private int printPipBlock(double startX, double startY, SVGElement parent, int numPips, double pipWidth,
Expand Down Expand Up @@ -365,4 +371,4 @@ private Element createPip(double pipWidth, double pipHeight, String fillColor,
box.setAttributeNS(null, SVGConstants.SVG_FILL_ATTRIBUTE, fillColor);
return box;
}
}
}
9 changes: 5 additions & 4 deletions megameklab/src/megameklab/printing/PrintMek.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.apache.batik.dom.util.SAXDocumentFactory;
import org.apache.batik.util.SVGConstants;
import org.apache.batik.util.XMLResourceDescriptor;
import org.apache.logging.log4j.LogManager;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
Expand All @@ -38,6 +37,7 @@
import megamek.common.*;
import megamek.common.annotations.Nullable;
import megamek.common.equipment.MiscMounted;
import megamek.logging.MMLogger;
import megameklab.printing.reference.*;
import megameklab.util.CConfig;
import megameklab.util.RSScale;
Expand All @@ -49,6 +49,7 @@
* @author Neoancient
*/
public class PrintMek extends PrintEntity {
private static final MMLogger logger = MMLogger.create(PrintMek.class);

/**
* The current Mek being printed.
Expand Down Expand Up @@ -157,7 +158,7 @@ public void processImage(int pageNum, PageFormat pageFormat) {
if (si instanceof SVGRectElement) {
drawSIPips((SVGRectElement) si);
} else {
LogManager.getLogger().error("Region siPips does not exist in template or is not a <rect>");
logger.error("Region siPips does not exist in template or is not a <rect>");
}
}

Expand Down Expand Up @@ -332,12 +333,12 @@ private boolean copyPipPattern(NodeList nl, String parentName) {
SAXDocumentFactory df = new SAXDocumentFactory(impl, parser);
doc = df.createDocument(f.toURI().toASCIIString(), is);
} catch (Exception e) {
LogManager.getLogger().error("Failed to open pip SVG file! Path: " + f.getName());
logger.error("Failed to open pip SVG file! Path: " + f.getName());
return null;
}

if (doc == null) {
LogManager.getLogger().error("Failed to open pip SVG file! Path: " + f.getName());
logger.error("Failed to open pip SVG file! Path: " + f.getName());
return null;
} else {
return doc.getElementsByTagName(SVGConstants.SVG_PATH_TAG);
Expand Down
25 changes: 15 additions & 10 deletions megameklab/src/megameklab/printing/PrintProtoMek.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import org.apache.batik.anim.dom.SVGLocatableSupport;
import org.apache.batik.util.SVGConstants;
import org.apache.logging.log4j.LogManager;
import org.w3c.dom.Element;

import megamek.common.Entity;
Expand All @@ -24,22 +23,24 @@
import megamek.common.Mounted;
import megamek.common.ProtoMek;
import megamek.common.TechConstants;
import megamek.logging.MMLogger;

/**
* Lays out a record sheet block for a single ProtoMek
*/
public class PrintProtoMek extends PrintEntity {
private static final MMLogger logger = MMLogger.create(PrintProtoMek.class);

private final ProtoMek proto;
private final int unitIndex;

/**
* Creates an SVG object for the record sheet
*
* @param proto The protoMek to print
* @param startPage The print job page number for this sheet
* @param unitIndex The index of this unit on the page
* @param options Overrides the global options for which elements are printed
* @param proto The protoMek to print
* @param startPage The print job page number for this sheet
* @param unitIndex The index of this unit on the page
* @param options Overrides the global options for which elements are printed
*/
public PrintProtoMek(ProtoMek proto, int startPage, int unitIndex, RecordSheetOptions options) {
super(startPage, options);
Expand Down Expand Up @@ -90,7 +91,8 @@ protected void writeTextFields() {
}

/**
* Checks whether the unit name is too large to fit into the primary field without kerning.
* Checks whether the unit name is too large to fit into the primary field
* without kerning.
* If so, looks for a break point and puts the remainder on a second line.
*/
private void splitName() {
Expand All @@ -100,25 +102,28 @@ private void splitName() {
if (null != fieldWidth) {
try {
double width = Double.parseDouble(fieldWidth);
// Clear any kerning that has already been applied so we can measure the full text length
// Clear any kerning that has already been applied so we can measure the full
// text length
element.removeAttribute(SVGConstants.SVG_TEXT_LENGTH_ATTRIBUTE);
element.removeAttribute(SVGConstants.SVG_SPACING_AND_GLYPHS_VALUE);
build();
double textWidth = SVGLocatableSupport.getBBox(element).getWidth();
if (textWidth > width) {
String name = element.getTextContent();
// The goal is a 2:1 split, but we start back a little to avoid just missing a break point
// The goal is a 2:1 split, but we start back a little to avoid just missing a
// break point
int pos = name.indexOf(" ", (int) (name.length() * 0.6));
if (pos < 0) {
// If we don't find a break point, give up and assign the text again to reset the kerning
// If we don't find a break point, give up and assign the text again to reset
// the kerning
setTextField(TYPE, name);
} else {
setTextField(TYPE, name.substring(0, pos));
setTextField(TYPE2, name.substring(pos + 1));
}
}
} catch (NumberFormatException ex) {
LogManager.getLogger().warn("Could not parse fieldWidth: " + fieldWidth);
logger.warn("Could not parse fieldWidth: " + fieldWidth);
}
}
}
Expand Down
17 changes: 8 additions & 9 deletions megameklab/src/megameklab/printing/PrintRecordSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import org.apache.fop.configuration.Configuration;
import org.apache.fop.configuration.DefaultConfigurationBuilder;
import org.apache.fop.svg.PDFTranscoder;
import org.apache.logging.log4j.LogManager;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
Expand Down Expand Up @@ -280,7 +279,7 @@ private void shadeTableRows() {
private @Nullable Document loadSVG(String directoryPath, String filename) {
final File file = new File(directoryPath, filename);
if (!file.exists()) {
LogManager.getLogger()
logger
.error(String.format("SVG file does not exist at path: %s/%s", directoryPath, filename));
return null;
}
Expand All @@ -291,11 +290,11 @@ private void shadeTableRows() {
XMLResourceDescriptor.getXMLParserClassName());
document = df.createDocument(file.toURI().toASCIIString(), is);
} catch (Exception ex) {
LogManager.getLogger().error("", ex);
logger.error("", ex);
}

if (document == null) {
LogManager.getLogger()
logger
.error(String.format("Failed to open SVG file! Path: %s/%s", directoryPath, filename));
return null;
}
Expand Down Expand Up @@ -417,7 +416,7 @@ public GraphicsNode build() {
// If an image can't be rendered we'll log it and return an empty document in
// its place rather than throwing an exception.
public SVGDocument getBrokenLinkDocument(Element e, String url, String message) {
LogManager.getLogger().warn("Cannot render image: " + message);
logger.warn("Cannot render image: " + message);
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
SVGDocument doc = (SVGDocument) impl.createDocument(svgNS, SVGConstants.SVG_SVG_TAG, null);
Element text = doc.createElementNS(svgNS, SVGConstants.SVG_TEXT_TAG);
Expand Down Expand Up @@ -524,7 +523,7 @@ protected void setTextField(String id, String text, boolean unhide) {
SVGConstants.SVG_SPACING_AND_GLYPHS_VALUE);
}
} catch (NumberFormatException ex) {
LogManager.getLogger().warn("Could not parse fieldWidth: " + fieldWidth);
logger.warn("Could not parse fieldWidth: " + fieldWidth);
}
}
}
Expand Down Expand Up @@ -905,9 +904,9 @@ public void embedImage(@Nullable File imageFile, Element canvas, Rectangle2D bbo
try {
embedImage(ImageIO.read(imageFile), canvas, bbox, center);
} catch (FileNotFoundException e) {
LogManager.getLogger().error("Fluff image file not found: " + imageFile.getPath());
logger.error("Fluff image file not found: " + imageFile.getPath());
} catch (IOException e) {
LogManager.getLogger().error("Error reading fluff image file: " + imageFile.getPath());
logger.error("Error reading fluff image file: " + imageFile.getPath());
}
}

Expand Down Expand Up @@ -951,7 +950,7 @@ public void embedImage(@Nullable Image image, Element canvas, Rectangle2D bbox,
"data:" + mimeType + ";base64," + Base64.getEncoder().encodeToString(bytes.toByteArray()));
canvas.appendChild(img);
} catch (IOException ex) {
LogManager.getLogger().error("Error embedding fluff image", ex);
logger.error("Error embedding fluff image", ex);
}
}

Expand Down
Loading

0 comments on commit c34b2f4

Please sign in to comment.