diff --git a/src/main/java/w2phtml/base/BinaryGraphicsDocument.java b/src/main/java/w2phtml/base/BinaryGraphicsDocument.java index af538d9..ba57496 100644 --- a/src/main/java/w2phtml/base/BinaryGraphicsDocument.java +++ b/src/main/java/w2phtml/base/BinaryGraphicsDocument.java @@ -55,11 +55,12 @@ import w2phtml.xhtml.XhtmlConfig; */ public class BinaryGraphicsDocument implements OutputFile { - private String sFileName; - private String sMimeType; - - private boolean bAcceptedFormat; - + private String sFileName; + private String sMimeType; + private String imageName = ""; + + private boolean bAcceptedFormat; + private boolean bRecycled = false; // Data for an embedded image @@ -96,6 +97,7 @@ public class BinaryGraphicsDocument implements OutputFile { this.sMimeType = bgd.getMIMEType(); this.blob = bgd.getData(); this.bAcceptedFormat = bgd.isAcceptedFormat(); + this.imageName = bgd.getImageName(); this.bRecycled = true; } @@ -112,7 +114,7 @@ public class BinaryGraphicsDocument implements OutputFile { this.horizontalPPI = widthDPI; } } catch (ImageReadException | IOException e) { - logger.error("Image " + sFileName + "error reading image info."); + logger.error("Image " + getImageName() + " error reading image info."); e.printStackTrace(); } @@ -235,7 +237,7 @@ public class BinaryGraphicsDocument implements OutputFile { try { BufferedImage image = ImageIO.read(bis); if (image == null) { - logger.error("Image " + sFileName + " couldn't be processed as it is damaged or has unknown format."); + logger.error("Image " + getImageName() + " couldn't be processed as it is damaged or has unknown format."); return; } int height = image.getHeight(); @@ -265,10 +267,10 @@ public class BinaryGraphicsDocument implements OutputFile { this.cropped = true; updateFileName(offsets); } catch (IOException e) { - logger.error("Error. Image " + sFileName + " " + e.getLocalizedMessage()); + logger.error("Error. Image " + getImageName() + " " + e.getLocalizedMessage()); e.printStackTrace(); } catch (RasterFormatException e ) { - logger.error("Error. Image " + sFileName + " " + e.getLocalizedMessage()); + logger.error("Error. Image " + getImageName() + " " + e.getLocalizedMessage()); e.printStackTrace(); } } @@ -290,7 +292,7 @@ public class BinaryGraphicsDocument implements OutputFile { try { BufferedImage image = ImageIO.read(bis); if (image == null) { - logger.error("Image " + sFileName + " couldn't be processed as it is damaged or has unknown format."); + logger.error("Image " + getImageName() + " couldn't be processed as it is damaged or has unknown format."); return; } int width = image.getWidth(); @@ -303,10 +305,10 @@ public class BinaryGraphicsDocument implements OutputFile { this.blob = baos.toByteArray(); extractPPI(); } else { - logger.error("Error. Image " + sFileName + " width = 0"); + logger.error("Error. Image " + getImageName() + " width = 0"); } } catch (IOException e) { - logger.error("Error. Image " + sFileName + " " + e.getLocalizedMessage()); + logger.error("Error. Image " + getImageName() + " " + e.getLocalizedMessage()); e.printStackTrace(); } } @@ -368,6 +370,16 @@ public class BinaryGraphicsDocument implements OutputFile { return offsets; } + public void setImageName(String imageName) { + this.imageName = imageName; + } + + public String getImageName() { + if (imageName == null || imageName.isBlank()) { + return sFileName; + } + return imageName; + } /** Does this document contain formulas? * * @return false - a graphics file does not contain formulas diff --git a/src/main/java/w2phtml/base/ImageConverter.java b/src/main/java/w2phtml/base/ImageConverter.java index 8fb8cfa..530be3f 100644 --- a/src/main/java/w2phtml/base/ImageConverter.java +++ b/src/main/java/w2phtml/base/ImageConverter.java @@ -195,6 +195,7 @@ public final class ImageConverter { private BinaryGraphicsDocument getImage(Element node, String sName) { assert(XMLString.DRAW_IMAGE.equals(node.getTagName())); + String imageName = getImageName(node); // Image data String sExt = null; String sMIME = null; @@ -234,8 +235,8 @@ public final class ImageConverter { // This is a linked image // TODO: Add option to download image from the URL? String sFileName = ofr.fixRelativeLink(sHref); - BinaryGraphicsDocument bgd - = new BinaryGraphicsDocument(sFileName,null); + BinaryGraphicsDocument bgd = new BinaryGraphicsDocument(sFileName,null); + bgd.setImageName(imageName); return bgd; } } @@ -281,8 +282,8 @@ public final class ImageConverter { int[] offlen = new int[2]; if (SVMReader.readSVM(blob,offlen)) { String sFileName = sName+MIMETypes.EPS_EXT; - BinaryGraphicsDocument bgd - = new BinaryGraphicsDocument(sFileName, MIMETypes.EPS); + BinaryGraphicsDocument bgd = new BinaryGraphicsDocument(sFileName, MIMETypes.EPS); + bgd.setImageName(imageName); bgd.setData(blob,offlen[0],offlen[1],true); return bgd; } @@ -319,6 +320,7 @@ public final class ImageConverter { if (isAcceptedFormat(sMIME) || bAcceptOtherFormats) { String sFileName = sName+sExt; BinaryGraphicsDocument bgd = new BinaryGraphicsDocument(sFileName,sMIME); + bgd.setImageName(imageName); bgd.setData(blob,isAcceptedFormat(sMIME)); if (sId!=null) { recycledImages.put(sId, new BinaryGraphicsDocument(bgd)); @@ -330,13 +332,23 @@ public final class ImageConverter { } } - private Element getAlternativeImage(Element node) { - Node sibling = node.getNextSibling(); - if (sibling!=null && Misc.isElement(sibling, XMLString.DRAW_IMAGE)) { - return (Element) sibling; + private String getImageName(Element node) { + Node parent = node.getParentNode(); + if (parent == null || parent.getNodeType() != Node.ELEMENT_NODE || parent.getNodeName() != XMLString.DRAW_FRAME) { + return null; } - return null; - } + Element frame = (Element) parent; + String name = frame.getAttribute(XMLString.DRAW_NAME); + return name; + } + + private Element getAlternativeImage(Element node) { + Node sibling = node.getNextSibling(); + if (sibling != null && Misc.isElement(sibling, XMLString.DRAW_IMAGE)) { + return (Element) sibling; + } + return null; + } // Create a fingerprint of a blob. The fingerprint concatenates the MD5 hash with the first 10 bytes of the blob. private String createId(byte[] blob) {