SVG support + merge spans + bugfixes

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@170 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2014-09-06 19:19:17 +00:00
parent a336023983
commit a0b76b3729
18 changed files with 212 additions and 130 deletions

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.4 (2014-09-03)
* Version 1.4 (2014-09-05)
*
*/
@ -30,7 +30,6 @@ import java.io.OutputStream;
import java.io.IOException;
import writer2latex.api.OutputFile;
import writer2latex.util.Misc;
/** This class is used to represent a binary graphics document to be included in the converter result.
@ -40,37 +39,55 @@ import writer2latex.util.Misc;
public class BinaryGraphicsDocument implements OutputFile {
private String sFileName;
private String sFileExtension;
private String sMimeType;
private boolean bIsLinked = false;
private boolean bIsAcceptedFormat = false;
private boolean bAcceptedFormat;
private boolean bRecycled = false;
// Data for an embedded image
private byte[] blob = null;
private int nOff;
private int nLen;
private int nOff = 0;
private int nLen = 0;
// Data for a linked image
private String sURL = null;
/**Constructs a new graphics document.
* This new document does not contain any data. Document data must
* be added using the appropriate methods.
* Until data is added using the <code>read</code> methods, the document is considered a link to
* the image given by the file name.
*
* @param sName The name of the <code>GraphicsDocument</code>.
* @param sFileExtension the file extension
* @param sFileName The name or URL of the <code>GraphicsDocument</code>.
* @param sMimeType the MIME type of the document
*/
public BinaryGraphicsDocument(String sName, String sFileExtension, String sMimeType) {
this.sFileExtension = sFileExtension;
public BinaryGraphicsDocument(String sFileName, String sMimeType) {
this.sFileName = sFileName;
this.sMimeType = sMimeType;
sFileName = Misc.trimDocumentName(sName, sFileExtension);
bAcceptedFormat = false; // or rather "don't know"
}
/** Construct a new graphics document which is a recycled version of the supplied one.
* This implies that all information is identical, but the recycled version does not contain any data.
* This is for images that are used more than once in the document.
*
* @param bgd the source document
*/
public BinaryGraphicsDocument(BinaryGraphicsDocument bgd) {
this.sFileName = bgd.getFileName();
this.sMimeType = bgd.getMIMEType();
this.bAcceptedFormat = bgd.isAcceptedFormat();
this.bRecycled = true;
}
/** Is this graphics document recycled?
*
* @return true if this is the case
*/
public boolean isRecycled() {
return bRecycled;
}
/** Set image contents to a byte array
*
* @param data the image data
* @param bIsAcceptedFormat flag to indicate that the format of the image is acceptable for the converter
*/
public void setData(byte[] data, boolean bIsAcceptedFormat) {
setData(data,0,data.length,bIsAcceptedFormat);
@ -87,30 +104,7 @@ public class BinaryGraphicsDocument implements OutputFile {
this.blob = data;
this.nOff = nOff;
this.nLen = nLen;
this.bIsAcceptedFormat = bIsAcceptedFormat;
this.bIsLinked = false;
this.sURL = null;
}
/** Set the URL of a linked image
*
* @param sURL the URL
*/
public void setURL(String sURL) {
this.blob = null;
this.nOff = 0;
this.nLen = 0;
this.bIsAcceptedFormat = false; // or rather don't know
this.bIsLinked = true;
this.sURL = sURL;
}
/** Get the URL of a linked image
*
* @return the URL or null if this is an embedded image
*/
public String getURL() {
return sURL;
this.bAcceptedFormat = bIsAcceptedFormat;
}
/** Does this <code>BinaryGraphicsDocument</code> represent a linked image?
@ -118,7 +112,7 @@ public class BinaryGraphicsDocument implements OutputFile {
* @return true if so
*/
public boolean isLinked() {
return bIsLinked;
return blob==null && !bRecycled;
}
/** Is this image in an acceptable format for the converter?
@ -126,9 +120,13 @@ public class BinaryGraphicsDocument implements OutputFile {
* @return true if so (always returns false for linked images)
*/
public boolean isAcceptedFormat() {
return bIsAcceptedFormat;
return bAcceptedFormat;
}
/** Get the data of the image
*
* @return the image data as a byte array - or null if this is a linked image
*/
public byte[] getData() {
return blob;
}
@ -148,20 +146,12 @@ public class BinaryGraphicsDocument implements OutputFile {
}
}
/** Get the file extension
*
* @return the file extension
*/
public String getFileExtension() {
return sFileExtension;
}
/** Get the document with file extension.</p>
/** Get the document name or URL</p>
*
* @return The document with file extension.
* @return The document name or URL
*/
public String getFileName() {
return sFileName + sFileExtension;
return sFileName;
}
/** Get the MIME type of the document.

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.4 (2014-09-03)
* Version 1.4 (2014-09-05)
*
*/
@ -28,6 +28,7 @@ package writer2latex.base;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.HashSet;
import org.w3c.dom.Element;
@ -69,6 +70,10 @@ public final class ImageConverter {
private String sDefaultFormat = null;
private String sDefaultVectorFormat = null;
private HashSet<String> acceptedFormats = new HashSet<String>();
// In the package format, the same image file may be used more than once in the document
// Hence we keep information of all documents for potential
private HashMap<String,BinaryGraphicsDocument> recycledImages = new HashMap<String,BinaryGraphicsDocument>();
/** Construct a new <code>ImageConverter</code> referring to a specific document
*
@ -159,17 +164,48 @@ public final class ImageConverter {
* or convert it to an accepted format
*/
public BinaryGraphicsDocument getImage(Element node) {
String sName = sSubDirName+sBaseFileName+formatter.format(++nImageCount);
BinaryGraphicsDocument bgd = getImage(node,sName);
if (bgd!=null) {
if (!bgd.isAcceptedFormat()) { // We may have better luck with an alternative image
Element sibling = getAlternativeImage(node);
if (sibling!=null) {
BinaryGraphicsDocument altBgd = getImage(sibling,sName);
if (altBgd!=null && altBgd.isAcceptedFormat()) {
bgd = altBgd;
}
}
}
}
if (bgd==null || bgd.isLinked() || bgd.isRecycled()) {
// The file name was not used
nImageCount--;
}
else if (node.hasAttribute(XMLString.XLINK_HREF)) {
// This is an embedded image we meet for the first time.
// Recycle it on behalf of the original image node.
String sHref = node.getAttribute(XMLString.XLINK_HREF);
recycledImages.put(sHref, new BinaryGraphicsDocument(bgd));
}
return bgd;
}
private BinaryGraphicsDocument getImage(Element node, String sName) {
assert(XMLString.DRAW_IMAGE.equals(node.getTagName()));
// Image data
String sExt = null;
String sMIME = null;
String sExt = null;
byte[] blob = null;
// First try to extract the image using the xlink:href attribute
if (node.hasAttribute(XMLString.XLINK_HREF)) {
String sHref = node.getAttribute(XMLString.XLINK_HREF);
if (sHref.length()>0) {
// We may have seen this image before, return the recycled version
if (recycledImages.containsKey(sHref)) {
return recycledImages.get(sHref);
}
// Image may be embedded in package:
String sPath = sHref;
if (sPath.startsWith("#")) { sPath = sPath.substring(1); }
@ -192,9 +228,9 @@ public final class ImageConverter {
else {
// This is a linked image
// TODO: Add option to download image from the URL?
String sFileName = ofr.fixRelativeLink(sHref);
BinaryGraphicsDocument bgd
= new BinaryGraphicsDocument(Misc.getFileName(sHref),Misc.getFileExtension(sHref),null);
bgd.setURL(ofr.fixRelativeLink(sHref));
= new BinaryGraphicsDocument(sFileName,null);
return bgd;
}
}
@ -225,21 +261,22 @@ public final class ImageConverter {
}
}
// We have an embedded image. Assign a name (without extension)
String sName = sSubDirName+sBaseFileName+formatter.format(++nImageCount);
// Is this an EPS file embedded in an SVM file?
// (This case is obsolete, but kept for the sake of old documents)
if (bExtractEPS && MIMETypes.SVM.equals(sMIME)) {
// Look for postscript:
int[] offlen = new int[2];
if (SVMReader.readSVM(blob,offlen)) {
String sFileName = sName+MIMETypes.EPS_EXT;
BinaryGraphicsDocument bgd
= new BinaryGraphicsDocument(sName,MIMETypes.EPS_EXT,MIMETypes.EPS);
= new BinaryGraphicsDocument(sFileName, MIMETypes.EPS);
bgd.setData(blob,offlen[0],offlen[1],true);
return bgd;
}
}
// We have an embedded image.
// If we have a converter AND a default format AND this image
// is not in an accepted format AND the converter knows how to
// convert it - try to convert...
@ -264,10 +301,12 @@ public final class ImageConverter {
sExt = MIMETypes.getFileExtension(sMIME);
}
}
// Create the result
if (isAcceptedFormat(sMIME) || bAcceptOtherFormats) {
BinaryGraphicsDocument bgd = new BinaryGraphicsDocument(sName,sExt,sMIME);
String sFileName = sName+sExt;
BinaryGraphicsDocument bgd = new BinaryGraphicsDocument(sFileName,sMIME);
bgd.setData(blob,isAcceptedFormat(sMIME));
return bgd;
}
@ -276,4 +315,12 @@ 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;
}
return null;
}
}