Refactoring image conversion + some math bugfixes

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@168 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2014-09-03 07:04:31 +00:00
parent 9babea1b6c
commit 74d7599b11
19 changed files with 533 additions and 369 deletions

View file

@ -2,6 +2,14 @@ Changelog for Writer2LaTeX version 1.2 -> 1.4
---------- version 1.3.2 alpha ---------- ---------- version 1.3.2 alpha ----------
[w2l] Bugfix (StarMath conversion): Do not create display equations in table cells
[w2l] Bugfix (StarMath conversion): Use array instead of matrix if there is more than 10 columns
[w2l] Bugfix (StarMath conversion): Add braces if the argument to a command is a space, e.g. \text{ }
[all] Refactored and optimized memory usage of image conversion
[all] Refactored and rearranged some code; in particular the last remaining bits of the old xmerge framework has been removed [all] Refactored and rearranged some code; in particular the last remaining bits of the old xmerge framework has been removed
[all] Optimized reading of package format: The settings.xml files are not parsed and the unused parts of the ZIP file are disposed [all] Optimized reading of package format: The settings.xml files are not parsed and the unused parts of the ZIP file are disposed

View file

@ -20,7 +20,7 @@
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.4 (2014-08-27) * Version 1.4 (2014-08-28)
* *
*/ */
@ -237,7 +237,7 @@ XTypeProvider {
converter.setGraphicConverter(new GraphicConverterImpl(xComponentContext)); converter.setGraphicConverter(new GraphicConverterImpl(xComponentContext));
// Do conversion. The base name is take from the URL provided by the office // Do conversion. The base name is take from the URL provided by the office
Iterator<OutputFile> docEnum = converter.convert(dom,Misc.makeFileName(getFileName(sURL))).iterator(); Iterator<OutputFile> docEnum = converter.convert(dom,Misc.makeFileName(getFileName(sURL)),true).iterator();
if (docEnum.hasNext()) { if (docEnum.hasNext()) {
// The master document is written to the XOutStream supplied by the XMLFilterAdaptor // The master document is written to the XOutStream supplied by the XMLFilterAdaptor

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA * MA 02111-1307 USA
* *
* Copyright: 2002-2012 by Henrik Just * Copyright: 2002-2014 by Henrik Just
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.4 (2012-03-21) * Version 1.4 (2014-08-28)
* *
*/ */
@ -144,10 +144,11 @@ public interface Converter {
* @param sTargetFileName the file name to use for the converted document * @param sTargetFileName the file name to use for the converted document
* (if the converted document is a compound document consisting consisting * (if the converted document is a compound document consisting consisting
* of several files, this name will be used for the master document) * of several files, this name will be used for the master document)
* @param bDestructive set to true if the converter is allowed to remove contents from the DOM tree (to save memory)
* @return a <code>ConverterResult</code> containing the converted document * @return a <code>ConverterResult</code> containing the converted document
* @throws IOException if some exception occurs while reading the document * @throws IOException if some exception occurs while reading the document
*/ */
public ConverterResult convert(org.w3c.dom.Document dom, String sTargetFileName) public ConverterResult convert(org.w3c.dom.Document dom, String sTargetFileName, boolean bDestructive)
throws IOException; throws IOException;
} }

View file

@ -20,7 +20,7 @@
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.4 (2012-08-27) * Version 1.4 (2012-08-28)
* *
*/ */
@ -33,7 +33,7 @@ public class ConverterFactory {
// Version information // Version information
private static final String VERSION = "1.3.2"; private static final String VERSION = "1.3.2";
private static final String DATE = "2014-08-27"; private static final String DATE = "2014-08-28";
/** Return the Writer2LaTeX version in the form /** Return the Writer2LaTeX version in the form
* (major version).(minor version).(patch level)<br/> * (major version).(minor version).(patch level)<br/>

View file

@ -20,160 +20,163 @@
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.4 (2014-08-25) * Version 1.4 (2014-09-03)
* *
*/ */
package writer2latex.base; package writer2latex.base;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.InputStream;
//import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import writer2latex.api.OutputFile; import writer2latex.api.OutputFile;
import writer2latex.util.Misc; import writer2latex.util.Misc;
/** /** This class is used to represent a binary graphics document to be included in the converter result.
* <p>Class representing a binary graphics document. * I may also represent a linked image, which should <em>not</em> be included (and will produce an empty file
* This class is used for representing graphics documents that are <i>not</i> * if it is).
* interpreted in any way, but simply copied verbatim from the source format
* to the target format.</p>
*
* <p><code>GraphicsDocument</code> is used to create new graphics documents.</p>
*
*/ */
public class BinaryGraphicsDocument implements OutputFile { public class BinaryGraphicsDocument implements OutputFile {
//private final static int BUFFERSIZE = 1024; private String sFileName;
private String docName;
private byte[] data;
private int nOff;
private int nLen;
private String sFileExtension; private String sFileExtension;
private String sMimeType; private String sMimeType;
private boolean bIsLinked = false;
private boolean bIsAcceptedFormat = false;
// Data for an embedded image
private byte[] blob = null;
private int nOff;
private int nLen;
// Data for a linked image
private String sURL = null;
/** /**Constructs a new graphics document.
* <p>Constructs a new graphics document.</p> * This new document does not contain any data. Document data must
* be added using the appropriate methods.
* *
* <p>This new document does not contain any information. Document data must * @param sName The name of the <code>GraphicsDocument</code>.
* either be added using appropriate methods, or an existing file can be * @param sFileExtension the file extension
* {@link #read(InputStream) read} from an <code>InputStream</code>.</p> * @param sMimeType the MIME type of the document
*
* @param name The name of the <code>GraphicsDocument</code>.
*/ */
public BinaryGraphicsDocument(String name, String sFileExtension, String sMimeType) { public BinaryGraphicsDocument(String name, String sFileExtension, String sMimeType) {
this.sFileExtension = sFileExtension; this.sFileExtension = sFileExtension;
this.sMimeType = sMimeType; this.sMimeType = sMimeType;
docName = trimDocumentName(name); sFileName = Misc.trimDocumentName(name, sFileExtension);
} }
/** Set image contents to a byte array
/**
* <p>This method reads <code>byte</code> data from the InputStream.</p>
* *
* @param is InputStream containing a binary data file. * @param data the image data
*
* @throws IOException In case of any I/O errors.
*/ */
public void read(InputStream is) throws IOException { public void setData(byte[] data, boolean bIsAcceptedFormat) {
data = Misc.inputStreamToByteArray(is); setData(data,0,data.length,bIsAcceptedFormat);
}
public void read(byte[] data) {
read(data,0,data.length);
} }
public void read(byte[] data, int nOff, int nLen) { /** Set image contents to part of a byte array
this.data = data; *
* @param data the image data
* @param nOff the offset into the byte array
* @param nLen the number of bytes to use
* @param bIsAcceptedFormat flag to indicate that the format of the image is acceptable for the converter
*/
public void setData(byte[] data, int nOff, int nLen, boolean bIsAcceptedFormat) {
this.blob = data;
this.nOff = nOff; this.nOff = nOff;
this.nLen = nLen; this.nLen = nLen;
this.bIsAcceptedFormat = bIsAcceptedFormat;
this.bIsLinked = false;
this.sURL = null;
} }
/* /** Set the URL of a linked image
* Utility method to make sure the document name is stripped of any file *
* extensions before use. * @param sURL the URL
*/ */
private String trimDocumentName(String name) { public void setURL(String sURL) {
String temp = name.toLowerCase(); this.blob = null;
this.nOff = 0;
if (temp.endsWith(getFileExtension())) { this.nLen = 0;
// strip the extension this.bIsAcceptedFormat = false; // or rather don't know
int nlen = name.length(); this.bIsLinked = true;
int endIndex = nlen - getFileExtension().length(); this.sURL = sURL;
name = name.substring(0,endIndex);
}
return name;
}
/**
* <p>Returns the <code>Document</code> name with no file extension.</p>
*
* @return The <code>Document</code> name with no file extension.
*/
public String getName() {
return docName;
} }
/** /** Get the URL of a linked image
* <p>Returns the <code>Document</code> name with file extension.</p> *
* * @return the URL or null if this is an embedded image
* @return The <code>Document</code> name with file extension.
*/ */
public String getFileName() { public String getURL() {
return new String(docName + getFileExtension()); return sURL;
}
/** Does this <code>BinaryGraphicsDocument</code> represent a linked image?
*
* @return true if so
*/
public boolean isLinked() {
return bIsLinked;
}
/** Is this image in an acceptable format for the converter?
*
* @return true if so (always returns false for linked images)
*/
public boolean isAcceptedFormat() {
return bIsAcceptedFormat;
} }
public byte[] getData() { public byte[] getData() {
return data; return blob;
} }
// Implement OutputFile
/** /** Writes out the content to the specified <code>OutputStream</code>.
* <p>Writes out the <code>Document</code> content to the specified * Linked images will not write any data.
* <code>OutputStream</code>.</p>
* *
* <p>This method may not be thread-safe. * @param os <code>OutputStream</code> to write out the content.
* Implementations may or may not synchronize this
* method. User code (i.e. caller) must make sure that
* calls to this method are thread-safe.</p>
*
* @param os <code>OutputStream</code> to write out the
* <code>Document</code> content.
* *
* @throws IOException If any I/O error occurs. * @throws IOException If any I/O error occurs.
*/ */
public void write(OutputStream os) throws IOException { public void write(OutputStream os) throws IOException {
os.write(data, nOff, nLen); if (blob!=null) {
os.write(blob, nOff, nLen);
}
}
/** Get the file extension
*
* @return the file extension
*/
public String getFileExtension() {
return sFileExtension;
} }
/** /** Get the document with file extension.</p>
* Returns the file extension for this type of *
* <code>Document</code>. * @return The document with file extension.
*/
public String getFileName() {
return sFileName + sFileExtension;
}
/** Get the MIME type of the document.
* *
* @return The file extension of <code>Document</code>. * @return The MIME type or null if this is unknown
*/ */
public String getFileExtension(){ return sFileExtension; }
/**
* Method to return the MIME type of the document.
*
* @return String The document's MIME type.
*/
public String getDocumentMIMEType(){ return sMimeType; }
public String getMIMEType() { public String getMIMEType() {
return sMimeType; return sMimeType;
} }
public boolean isMasterDocument() { /** Is this document a master document?
*
* @return false - a graphics file is never a master document
*/
public boolean isMasterDocument() {
return false; return false;
} }
} }

View file

@ -20,7 +20,7 @@
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.4 (2014-08-27) * Version 1.4 (2014-09-03)
* *
*/ */
@ -100,20 +100,20 @@ public abstract class ConverterBase implements Converter {
// Read document // Read document
odDoc = new OfficeDocument(); odDoc = new OfficeDocument();
odDoc.read(is); odDoc.read(is);
return convert(sTargetFileName); return convert(sTargetFileName,true);
} }
public ConverterResult convert(org.w3c.dom.Document dom, String sTargetFileName) throws IOException { public ConverterResult convert(org.w3c.dom.Document dom, String sTargetFileName, boolean bDestructive) throws IOException {
// Read document // Read document
odDoc = new OfficeDocument(); odDoc = new OfficeDocument();
odDoc.read(dom); odDoc.read(dom);
return convert(sTargetFileName); return convert(sTargetFileName,bDestructive);
} }
private ConverterResult convert(String sTargetFileName) throws IOException { private ConverterResult convert(String sTargetFileName, boolean bDestructive) throws IOException {
ofr = new OfficeReader(odDoc,false); ofr = new OfficeReader(odDoc,false);
metaData = new MetaData(odDoc); metaData = new MetaData(odDoc);
imageConverter = new ImageConverter(odDoc,true); imageConverter = new ImageConverter(ofr,bDestructive,true);
imageConverter.setGraphicConverter(graphicConverter); imageConverter.setGraphicConverter(graphicConverter);
// Prepare output // Prepare output

View file

@ -1,6 +1,6 @@
/************************************************************************ /************************************************************************
* *
* ImageLoader.java * ImageConverter.java
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA * MA 02111-1307 USA
* *
* Copyright: 2002-2012 by Henrik Just * Copyright: 2002-2014 by Henrik Just
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.4 (2012-04-03) * Version 1.4 (2014-09-03)
* *
*/ */
@ -30,6 +30,7 @@ import java.text.DecimalFormat;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.HashSet; import java.util.HashSet;
import org.w3c.dom.Element;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
@ -37,20 +38,22 @@ import writer2latex.api.GraphicConverter;
import writer2latex.office.EmbeddedBinaryObject; import writer2latex.office.EmbeddedBinaryObject;
import writer2latex.office.EmbeddedObject; import writer2latex.office.EmbeddedObject;
import writer2latex.office.MIMETypes; import writer2latex.office.MIMETypes;
import writer2latex.office.OfficeDocument; import writer2latex.office.OfficeReader;
import writer2latex.office.SVMReader; import writer2latex.office.SVMReader;
import writer2latex.office.XMLString; import writer2latex.office.XMLString;
import writer2latex.util.Base64; import writer2latex.util.Base64;
import writer2latex.util.Misc; import writer2latex.util.Misc;
/** /** This class extracts and converts images from an office document.
* <p>This class extracts images from an OOo file. * The images are returned as <code>BinaryGraphicsDocument</code>.
* The images are returned as BinaryGraphicsDocument.</p> * The image converter can be configured as destructive. In this case, the returned
* graphics documents will contain the only reference to the image (the original data
* will be removed).
*/ */
public final class ImageConverter { public final class ImageConverter {
// The Office document to load images from private OfficeReader ofr;
private OfficeDocument oooDoc; private boolean bDestructive;
// Data for file name generation // Data for file name generation
private String sBaseFileName = ""; private String sBaseFileName = "";
private String sSubDirName = ""; private String sSubDirName = "";
@ -67,135 +70,210 @@ public final class ImageConverter {
private String sDefaultVectorFormat = null; private String sDefaultVectorFormat = null;
private HashSet<String> acceptedFormats = new HashSet<String>(); private HashSet<String> acceptedFormats = new HashSet<String>();
public ImageConverter(OfficeDocument oooDoc, boolean bExtractEPS) { /** Construct a new <code>ImageConverter</code> referring to a specific document
this.oooDoc = oooDoc; *
* @param doc the office document used
* @param bExtractEPS set true if EPS content should be extracted from SVM files
*/
public ImageConverter(OfficeReader ofr, boolean bDestructive, boolean bExtractEPS) {
this.ofr = ofr;
this.bDestructive = bDestructive;
this.bExtractEPS = bExtractEPS; this.bExtractEPS = bExtractEPS;
this.formatter = new DecimalFormat("000"); this.formatter = new DecimalFormat("000");
} }
public void setBaseFileName(String sBaseFileName) { this.sBaseFileName = sBaseFileName; } /** Define the base file name to use for generating file names
*
* @param sBaseFileName the base file name
*/
public void setBaseFileName(String sBaseFileName) {
this.sBaseFileName = sBaseFileName;
}
public void setUseSubdir(String sSubDirName) { this.sSubDirName = sSubDirName+"/"; } /** Define the name of a sub directory to prepend to file names
*
* @param sSubDirName the sub directory
*/
public void setUseSubdir(String sSubDirName) {
this.sSubDirName = sSubDirName+"/";
}
public void setAcceptOtherFormats(boolean b) { bAcceptOtherFormats = b; } /** Specify that the <code>ImageConverter</code> should return an image even if it was not possible
* to convert it to an acceptable format.
*
* @param b true if other formats should be accepted
*/
public void setAcceptOtherFormats(boolean b) {
bAcceptOtherFormats = b;
}
/** Define the default format for raster graphics
*
* @param sMime the MIME type of the default raster format
*/
public void setDefaultFormat(String sMime) { public void setDefaultFormat(String sMime) {
addAcceptedFormat(sMime); addAcceptedFormat(sMime);
sDefaultFormat = sMime; sDefaultFormat = sMime;
} }
/** Define the default format for vector graphics
*
* @param sMime the MIME type for the default vector format
*/
public void setDefaultVectorFormat(String sMime) { public void setDefaultVectorFormat(String sMime) {
addAcceptedFormat(sMime); addAcceptedFormat(sMime);
sDefaultVectorFormat = sMime; sDefaultVectorFormat = sMime;
} }
public void addAcceptedFormat(String sMime) { acceptedFormats.add(sMime); } /** Define an accepted graphics format
*
* @param sMime the MIME type of the format
*/
public void addAcceptedFormat(String sMime) {
acceptedFormats.add(sMime);
}
private boolean isAcceptedFormat(String sMime) { return acceptedFormats.contains(sMime); } /** Is a given format accepted?
*
* @param sMime the MIME type to query
* @return true if this is an accepted format
*/
private boolean isAcceptedFormat(String sMime) {
return acceptedFormats.contains(sMime);
}
public void setGraphicConverter(GraphicConverter gcv) { this.gcv = gcv; } /** Define the <code>GraphicConverter</code> to use for image conversion
*
* @param gcv the graphics converter
*/
public void setGraphicConverter(GraphicConverter gcv) {
this.gcv = gcv;
}
public BinaryGraphicsDocument getImage(Node node) { /** Get an image from a <code>draw:image</code> element. If the converter is destructive, the returned
// node must be a draw:image element. * <code>BinaryGraphicsDocument</code> will hold the only reference to the image data (the original
// variables to hold data about the image: * data will be removed).
String sMIME = null; *
String sExt = null; * @param node the image element
byte[] blob = null; * @return a document containing the (converted) image, or null if it was not possible to read the image
* or convert it to an accepted format
*/
public BinaryGraphicsDocument getImage(Element node) {
assert(XMLString.DRAW_IMAGE.equals(node.getTagName()));
String sHref = Misc.getAttribute(node,XMLString.XLINK_HREF); // Image data
if (sHref==null || sHref.length()==0) { String sMIME = null;
// Image must be contained in an office:binary-element as base64: String sExt = null;
Node obd = Misc.getChildByTagName(node,XMLString.OFFICE_BINARY_DATA); byte[] blob = null;
if (obd!=null) {
StringBuffer buf = new StringBuffer(); // First try to extract the image using the xlink:href attribute
NodeList nl = obd.getChildNodes(); if (node.hasAttribute(XMLString.XLINK_HREF)) {
int nLen = nl.getLength(); String sHref = node.getAttribute(XMLString.XLINK_HREF);
for (int i=0; i<nLen; i++) { if (sHref.length()>0) {
if (nl.item(i).getNodeType()==Node.TEXT_NODE) { // Image may be embedded in package:
buf.append(nl.item(i).getNodeValue()); String sPath = sHref;
} if (sPath.startsWith("#")) { sPath = sPath.substring(1); }
} if (sPath.startsWith("./")) { sPath = sPath.substring(2); }
blob = Base64.decode(buf.toString()); EmbeddedObject obj = ofr.getEmbeddedObject(sPath);
sMIME = MIMETypes.getMagicMIMEType(blob); if (obj!=null && obj instanceof EmbeddedBinaryObject) {
sExt = MIMETypes.getFileExtension(sMIME); EmbeddedBinaryObject object = (EmbeddedBinaryObject) obj;
} blob = object.getBinaryData();
} sMIME = object.getType();
else { if (sMIME.length()==0) {
// Image may be embedded in package: // If the manifest provides a media type, trust that
if (sHref.startsWith("#")) { sHref = sHref.substring(1); } // Otherwise determine it by byte inspection
if (sHref.startsWith("./")) { sHref = sHref.substring(2); } sMIME = MIMETypes.getMagicMIMEType(blob);
EmbeddedObject obj = oooDoc.getEmbeddedObject(sHref); }
if (obj!=null && obj instanceof EmbeddedBinaryObject) { sExt = MIMETypes.getFileExtension(sMIME);
EmbeddedBinaryObject object = (EmbeddedBinaryObject) obj; if (bDestructive) {
blob = object.getBinaryData(); object.dispose();
sMIME = object.getType(); }
if (sMIME.length()>0) { }
// If the manifest provides a media type, trust that else {
sExt = MIMETypes.getFileExtension(sMIME); // This is a linked image
} // TODO: Add option to download image from the URL?
else { BinaryGraphicsDocument bgd
// Otherwise determine it by byte inspection = new BinaryGraphicsDocument(Misc.getFileName(sHref),Misc.getFileExtension(sHref),null);
sMIME = MIMETypes.getMagicMIMEType(blob); bgd.setURL(ofr.fixRelativeLink(sHref));
sExt = MIMETypes.getFileExtension(sMIME); return bgd;
} }
} }
else { }
// This is a linked image
// TODO: Perhaps we should download the image from the url in sHref? // If there is no suitable xlink:href attribute, the image must be contained in an office:binary-element as base64
// Alternatively BinaryGraphicsDocument should be extended to if (blob==null) {
// handle external graphics. Node obd = Misc.getChildByTagName(node,XMLString.OFFICE_BINARY_DATA);
} if (obd!=null) {
} StringBuffer buf = new StringBuffer();
NodeList nl = obd.getChildNodes();
if (blob==null) { return null; } int nLen = nl.getLength();
for (int i=0; i<nLen; i++) {
// Assign a name (without extension) if (nl.item(i).getNodeType()==Node.TEXT_NODE) {
buf.append(nl.item(i).getNodeValue());
}
}
blob = Base64.decode(buf.toString());
sMIME = MIMETypes.getMagicMIMEType(blob);
sExt = MIMETypes.getFileExtension(sMIME);
if (bDestructive) {
node.removeChild(obd);
}
}
else {
// There is no image data
return null;
}
}
// We have an embedded image. Assign a name (without extension)
String sName = sSubDirName+sBaseFileName+formatter.format(++nImageCount); String sName = sSubDirName+sBaseFileName+formatter.format(++nImageCount);
BinaryGraphicsDocument bgd = null; // Is this an EPS file embedded in an SVM file?
if (bExtractEPS && MIMETypes.SVM.equals(sMIME)) {
if (bExtractEPS && MIMETypes.SVM.equals(MIMETypes.getMagicMIMEType(blob))) {
// Look for postscript: // Look for postscript:
int[] offlen = new int[2]; int[] offlen = new int[2];
if (SVMReader.readSVM(blob,offlen)) { if (SVMReader.readSVM(blob,offlen)) {
bgd = new BinaryGraphicsDocument(sName, BinaryGraphicsDocument bgd
MIMETypes.EPS_EXT,MIMETypes.EPS); = new BinaryGraphicsDocument(sName,MIMETypes.EPS_EXT,MIMETypes.EPS);
bgd.read(blob,offlen[0],offlen[1]); bgd.setData(blob,offlen[0],offlen[1],true);
return bgd;
} }
} }
// 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...
if (gcv!=null && !isAcceptedFormat(sMIME) && sDefaultFormat!=null) {
byte[] newBlob = null;
String sTargetMIME = null;
if (bgd==null) { if (MIMETypes.isVectorFormat(sMIME) && sDefaultVectorFormat!=null &&
// If we have a converter AND a default format AND this image gcv.supportsConversion(sMIME,sDefaultVectorFormat,false,false)) {
// is not in an accepted format AND the converter knows how to // Try vector format first
// convert it - try to convert... newBlob = gcv.convert(blob, sMIME, sTargetMIME=sDefaultVectorFormat);
if (gcv!=null && !isAcceptedFormat(sMIME) && sDefaultFormat!=null) { }
byte[] newBlob = null; if (newBlob==null && gcv.supportsConversion(sMIME,sDefaultFormat,false,false)) {
String sTargetMIME = null; // Then try bitmap format
newBlob = gcv.convert(blob,sMIME,sTargetMIME=sDefaultFormat);
if (MIMETypes.isVectorFormat(sMIME) && sDefaultVectorFormat!=null &&
gcv.supportsConversion(sMIME,sDefaultVectorFormat,false,false)) {
// Try vector format first
newBlob = gcv.convert(blob, sMIME, sTargetMIME=sDefaultVectorFormat);
}
if (newBlob==null && gcv.supportsConversion(sMIME,sDefaultFormat,false,false)) {
// Then try bitmap format
newBlob = gcv.convert(blob,sMIME,sTargetMIME=sDefaultFormat);
}
if (newBlob!=null) {
// Conversion successful - create new data
blob = newBlob;
sMIME = sTargetMIME;
sExt = MIMETypes.getFileExtension(sMIME);
}
} }
if (isAcceptedFormat(sMIME) || bAcceptOtherFormats) { if (newBlob!=null) {
bgd = new BinaryGraphicsDocument(sName,sExt,sMIME); // Conversion successful - create new data
bgd.read(blob); blob = newBlob;
sMIME = sTargetMIME;
sExt = MIMETypes.getFileExtension(sMIME);
} }
} }
return bgd; // Create the result
if (isAcceptedFormat(sMIME) || bAcceptOtherFormats) {
BinaryGraphicsDocument bgd = new BinaryGraphicsDocument(sName,sExt,sMIME);
bgd.setData(blob,isAcceptedFormat(sMIME));
return bgd;
}
else {
return null;
}
} }
} }

View file

@ -20,7 +20,7 @@
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.4 (2014-08-26) * Version 1.4 (2014-09-03)
* *
*/ */
@ -166,6 +166,7 @@ public class DrawConverter extends ConverterHelper {
} }
} }
else { // unsupported object else { // unsupported object
System.out.println("Unsupported "+sHref);
boolean bIgnore = true; boolean bIgnore = true;
if (ofr.isOpenDocument()) { // look for replacement image if (ofr.isOpenDocument()) { // look for replacement image
Element replacementImage = Misc.getChildByTagName(getFrame(node),XMLString.DRAW_IMAGE); Element replacementImage = Misc.getChildByTagName(getFrame(node),XMLString.DRAW_IMAGE);
@ -292,31 +293,13 @@ public class DrawConverter extends ConverterHelper {
private void includeGraphics(Element node, LaTeXDocumentPortion ldp, Context oc) { private void includeGraphics(Element node, LaTeXDocumentPortion ldp, Context oc) {
String sFileName = null; String sFileName = null;
boolean bCommentOut = true; boolean bCommentOut = true;
String sHref = node.getAttribute(XMLString.XLINK_HREF);
BinaryGraphicsDocument bgd = palette.getImageCv().getImage(node);
if (sHref.length()>0 && !ofr.isInPackage(sHref)) { if (bgd!=null) {
// Linked image is not yet handled by ImageLoader. This is a temp. if (!bgd.isLinked()) { // embedded image
// solution (will go away when ImageLoader is finished)
sFileName = ofr.fixRelativeLink(sHref);
int nExtStart = sHref.lastIndexOf(".");
String sExt = nExtStart>=0 ? sHref.substring(nExtStart).toLowerCase() : "";
// Accept only relative filenames and supported filetypes:
bCommentOut = sFileName.indexOf(":")>-1 || !(
config.getBackend()==LaTeXConfig.UNSPECIFIED ||
(config.getBackend()==LaTeXConfig.PDFTEX && MIMETypes.JPEG_EXT.equals(sExt)) ||
(config.getBackend()==LaTeXConfig.PDFTEX && MIMETypes.PNG_EXT.equals(sExt)) ||
(config.getBackend()==LaTeXConfig.PDFTEX && MIMETypes.PDF_EXT.equals(sExt)) ||
(config.getBackend()==LaTeXConfig.XETEX && MIMETypes.JPEG_EXT.equals(sExt)) ||
(config.getBackend()==LaTeXConfig.XETEX && MIMETypes.PNG_EXT.equals(sExt)) ||
(config.getBackend()==LaTeXConfig.XETEX && MIMETypes.PDF_EXT.equals(sExt)) ||
(config.getBackend()==LaTeXConfig.DVIPS && MIMETypes.EPS_EXT.equals(sExt)));
}
else { // embedded or base64 encoded image
BinaryGraphicsDocument bgd = palette.getImageCv().getImage(node);
if (bgd!=null) {
palette.addDocument(bgd); palette.addDocument(bgd);
sFileName = bgd.getFileName(); sFileName = bgd.getFileName();
String sMIME = bgd.getDocumentMIMEType(); String sMIME = bgd.getMIMEType();
bCommentOut = !( bCommentOut = !(
config.getBackend()==LaTeXConfig.UNSPECIFIED || config.getBackend()==LaTeXConfig.UNSPECIFIED ||
(config.getBackend()==LaTeXConfig.PDFTEX && MIMETypes.JPEG.equals(sMIME)) || (config.getBackend()==LaTeXConfig.PDFTEX && MIMETypes.JPEG.equals(sMIME)) ||
@ -326,10 +309,23 @@ public class DrawConverter extends ConverterHelper {
(config.getBackend()==LaTeXConfig.XETEX && MIMETypes.PNG.equals(sMIME)) || (config.getBackend()==LaTeXConfig.XETEX && MIMETypes.PNG.equals(sMIME)) ||
(config.getBackend()==LaTeXConfig.XETEX && MIMETypes.PDF.equals(sMIME)) || (config.getBackend()==LaTeXConfig.XETEX && MIMETypes.PDF.equals(sMIME)) ||
(config.getBackend()==LaTeXConfig.DVIPS && MIMETypes.EPS.equals(sMIME))); (config.getBackend()==LaTeXConfig.DVIPS && MIMETypes.EPS.equals(sMIME)));
} }
else { // linked image
sFileName = bgd.getURL();
String sExt = bgd.getFileExtension().toLowerCase();
// Accept only relative filenames and supported filetypes:
bCommentOut = sFileName.indexOf(":")>-1 || !(
config.getBackend()==LaTeXConfig.UNSPECIFIED ||
(config.getBackend()==LaTeXConfig.PDFTEX && MIMETypes.JPEG_EXT.equals(sExt)) ||
(config.getBackend()==LaTeXConfig.PDFTEX && MIMETypes.PNG_EXT.equals(sExt)) ||
(config.getBackend()==LaTeXConfig.PDFTEX && MIMETypes.PDF_EXT.equals(sExt)) ||
(config.getBackend()==LaTeXConfig.XETEX && MIMETypes.JPEG_EXT.equals(sExt)) ||
(config.getBackend()==LaTeXConfig.XETEX && MIMETypes.PNG_EXT.equals(sExt)) ||
(config.getBackend()==LaTeXConfig.XETEX && MIMETypes.PDF_EXT.equals(sExt)) ||
(config.getBackend()==LaTeXConfig.DVIPS && MIMETypes.EPS_EXT.equals(sExt)));
}
} }
else {
if (sFileName==null) {
ldp.append("[Warning: Image not found]"); ldp.append("[Warning: Image not found]");
return; return;
} }

View file

@ -20,7 +20,7 @@
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.4 (2014-08-25) * Version 1.4 (2014-09-03)
* *
*/ */
@ -73,12 +73,7 @@ public final class MathConverter extends ConverterHelper {
public void appendDeclarations(LaTeXDocumentPortion pack, LaTeXDocumentPortion decl) { public void appendDeclarations(LaTeXDocumentPortion pack, LaTeXDocumentPortion decl) {
if (bContainsFormulas) { if (bContainsFormulas) {
if (config.useOoomath()) { smc.appendDeclarations(pack,decl);
pack.append("\\usepackage{ooomath}").nl();
}
else {
smc.appendDeclarations(pack,decl);
}
} }
if (bNeedTexMathsPreamble) { if (bNeedTexMathsPreamble) {
// The preamble may be stored as a user defined property (newline is represented as paragraph sign) // The preamble may be stored as a user defined property (newline is represented as paragraph sign)

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA * MA 02111-1307 USA
* *
* Copyright: 2002-2011 by Henrik Just * Copyright: 2002-2014 by Henrik Just
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.2 (2011-03-30) * Version 1.4 (2014-09-02)
* *
*/ */
@ -98,7 +98,8 @@ public class ParConverter extends StyleConverter {
* \end{enumerate}). * \end{enumerate}).
*/ */
public void handleParagraph(Element node, LaTeXDocumentPortion ldp, Context oc, boolean bLastInBlock) { public void handleParagraph(Element node, LaTeXDocumentPortion ldp, Context oc, boolean bLastInBlock) {
if (palette.getMathCv().handleDisplayEquation(node,ldp)) { return; } // Check for display equation (except in table cells)
if ((!oc.isInTable()) && palette.getMathCv().handleDisplayEquation(node,ldp)) { return; }
// Get the style name for this paragraph // Get the style name for this paragraph
String sStyleName = node.getAttribute(XMLString.TEXT_STYLE_NAME); String sStyleName = node.getAttribute(XMLString.TEXT_STYLE_NAME);

View file

@ -18,7 +18,7 @@
* *
* Copyright: 2002-2014 by Henrik Just * Copyright: 2002-2014 by Henrik Just
* *
* Version 1.4 (2014-08-05) * Version 1.4 (2014-09-03)
* *
* All Rights Reserved. * All Rights Reserved.
*/ */
@ -723,6 +723,7 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert
private LaTeXConfig config; private LaTeXConfig config;
private Map<String, String> configSymbols; private Map<String, String> configSymbols;
private boolean bUseColor; private boolean bUseColor;
private int nMaxMatrixCols = 10; // trace the largest number of columns in a matrix
private SmToken curToken=new SmToken(); // contains the data of the current token private SmToken curToken=new SmToken(); // contains the data of the current token
private SimpleInputBuffer buffer; // contains the starmath formula private SimpleInputBuffer buffer; // contains the starmath formula
//private Float fBaseSize; // base size for the formula (usually 12pt) //private Float fBaseSize; // base size for the formula (usually 12pt)
@ -771,67 +772,75 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert
} }
public void appendDeclarations(LaTeXDocumentPortion pack, LaTeXDocumentPortion decl) { public void appendDeclarations(LaTeXDocumentPortion pack, LaTeXDocumentPortion decl) {
if (bDefeq) { if (config.useOoomath()) {
decl.append("\\newcommand\\defeq{\\stackrel{\\mathrm{def}}{=}}").nl(); pack.append("\\usepackage{ooomath}").nl();
} }
if (bLambdabar) { else {
decl.append("\\newcommand\\lambdabar{\\mathchar'26\\mkern-10mu\\lambda}").nl(); if (bDefeq) {
decl.append("\\newcommand\\defeq{\\stackrel{\\mathrm{def}}{=}}").nl();
}
if (bLambdabar) {
decl.append("\\newcommand\\lambdabar{\\mathchar'26\\mkern-10mu\\lambda}").nl();
}
if (bDdotsup) {
decl.append("\\newcommand\\ddotsup{\\mathinner{\\mkern1mu\\raise1pt\\vbox{\\kern7pt\\hbox{.}}\\mkern2mu\\raise4pt\\hbox{.}\\mkern2mu\\raise7pt\\hbox{.}\\mkern1mu}}").nl();
}
if (bMultimapdotbothA) {
decl.append("\\providecommand\\multimapdotbothA{\\bullet\\kern-0.4em-\\kern-0.4em\\circ}").nl();
}
if (bMultimapdotbothB) {
decl.append("\\providecommand\\multimapdotbothB{\\circ\\kern-0.4em-\\kern-0.4em\\bullet}").nl();
}
if (bLlbracket) {
decl.append("\\providecommand\\llbracket{[}").nl();
}
if (bRrbracket) {
decl.append("\\providecommand\\rrbracket{]}").nl();
}
if (bOiint) {
decl.append("\\providecommand\\oiint{\\oint}").nl();
}
if (bOiiint) {
decl.append("\\providecommand\\oiiint{\\oint}").nl();
}
if (bWideslash) {
decl.append("\\newcommand\\wideslash[2]{{}^{#1}/_{#2}}").nl();
}
if (bWidebslash) {
decl.append("\\newcommand\\widebslash[2]{{}_{#1}\\backslash^{#2}}").nl();
}
if (bBoldsubformula) {
decl.append("\\newcommand\\boldsubformula[1]{\\text{\\mathversion{bold}$#1$}}").nl();
}
if (bNormalsubformula) {
decl.append("\\newcommand\\normalsubformula[1]{\\text{\\mathversion{normal}$#1$}}").nl();
}
if (bMultiscripts || bMathoverstrike) {
decl.append("\\newlength{\\idxmathdepth}\\newlength{\\idxmathtotal}\\newlength{\\idxmathwidth}\\newlength{\\idxraiseme}").nl();
decl.append("\\newcommand{\\idxdheight}[1]{\\protect\\settoheight{\\idxmathtotal}{\\(\\displaystyle#1\\)}\\protect\\settodepth{\\idxmathdepth}{\\(\\displaystyle#1\\)}\\protect\\settowidth{\\idxmathwidth}{\\(\\displaystyle#1\\)}\\protect\\addtolength{\\idxmathtotal}{\\idxmathdepth}\\protect\\setlength{\\idxraiseme}{\\idxmathtotal/2-\\idxmathdepth}}").nl();
decl.append("\\newcommand{\\idxtheight}[1]{\\protect\\settoheight{\\idxmathtotal}{\\(\\textstyle #1\\)}\\protect\\settodepth{\\idxmathdepth}{\\(\\textstyle #1\\)}\\protect\\settowidth{\\idxmathwidth}{\\(\\textstyle#1\\)}\\protect\\addtolength{\\idxmathtotal}{\\idxmathdepth}\\protect\\setlength{\\idxraiseme}{\\idxmathtotal/2-\\idxmathdepth}}").nl();
decl.append("\\newcommand{\\idxsheight}[1]{\\protect\\settoheight{\\idxmathtotal}{\\(\\scriptstyle #1\\)}\\protect\\settodepth{\\idxmathdepth}{\\(\\scriptstyle #1\\)}\\protect\\settowidth{\\idxmathwidth}{\\(\\scriptstyle#1\\)}\\protect\\addtolength{\\idxmathtotal}{\\idxmathdepth}\\protect\\setlength{\\idxraiseme}{\\idxmathtotal/2-\\idxmathdepth}}").nl();
decl.append("\\newcommand{\\idxssheight}[1]{\\protect\\settoheight{\\idxmathtotal}{\\(\\scriptscriptstyle #1\\)}\\protect\\settodepth{\\idxmathdepth}{\\(\\scriptscriptstyle #1\\)}\\protect\\settowidth{\\idxmathwidth}{\\(\\scriptscriptstyle#1\\)}\\protect\\addtolength{\\idxmathtotal}{\\idxmathdepth}\\protect\\setlength{\\idxraiseme}{\\idxmathtotal/2-\\idxmathdepth}}").nl();
}
if (bMultiscripts) {
decl.append("\\newcommand\\multiscripts[5]{\\mathchoice")
.append("{\\idxdheight{#4}\\rule[-\\idxmathdepth]{0mm}{\\idxmathtotal}#1\\underset{#2}{\\overset{#3}{#4}}\\rule[-\\idxmathdepth]{0mm}{\\idxmathtotal}#5}")
.append("{\\idxtheight{#4}\\rule[-\\idxmathdepth]{0mm}{\\idxmathtotal}#1\\underset{#2}{\\overset{#3}{#4}}\\rule[-\\idxmathdepth]{0mm}{\\idxmathtotal}#5}")
.append("{\\idxsheight{#4}\\rule[-\\idxmathdepth]{0mm}{\\idxmathtotal}#1\\underset{#2}{\\overset{#3}{#4}}\\rule[-\\idxmathdepth]{0mm}{\\idxmathtotal}#5}")
.append("{\\idxssheight{#4}\\rule[-\\idxmathdepth]{0mm}{\\idxmathtotal}#1\\underset{#2}{\\overset{#3}{#4}}\\rule[-\\idxmathdepth]{0mm}{\\idxmathtotal}#5}}")
.nl();
}
if (bMathoverstrike) {
decl.append("\\newcommand\\mathoverstrike[1]{\\mathchoice")
.append("{\\idxdheight{#1}\\rlap{\\rule[\\idxraiseme]{\\idxmathwidth}{0.4pt}}{#1}}")
.append("{\\idxtheight{#1}\\rlap{\\rule[\\idxraiseme]{\\idxmathwidth}{0.4pt}}{#1}}")
.append("{\\idxsheight{#1}\\rlap{\\rule[\\idxraiseme]{\\idxmathwidth}{0.4pt}}{#1}}")
.append("{\\idxssheight{#1}\\rlap{\\rule[\\idxraiseme]{\\idxmathwidth}{0.4pt}}{#1}}}")
.nl();
}
} }
if (bDdotsup) { if (nMaxMatrixCols>10) { // The default for the matrix environment is at most 10 columns
decl.append("\\newcommand\\ddotsup{\\mathinner{\\mkern1mu\\raise1pt\\vbox{\\kern7pt\\hbox{.}}\\mkern2mu\\raise4pt\\hbox{.}\\mkern2mu\\raise7pt\\hbox{.}\\mkern1mu}}").nl(); decl.append("\\setcounter{MaxMatrixCols}{").append(Integer.toString(nMaxMatrixCols)).append("}").nl();
}
if (bMultimapdotbothA) {
decl.append("\\providecommand\\multimapdotbothA{\\bullet\\kern-0.4em-\\kern-0.4em\\circ}").nl();
}
if (bMultimapdotbothB) {
decl.append("\\providecommand\\multimapdotbothB{\\circ\\kern-0.4em-\\kern-0.4em\\bullet}").nl();
}
if (bLlbracket) {
decl.append("\\providecommand\\llbracket{[}").nl();
}
if (bRrbracket) {
decl.append("\\providecommand\\rrbracket{]}").nl();
}
if (bOiint) {
decl.append("\\providecommand\\oiint{\\oint}").nl();
}
if (bOiiint) {
decl.append("\\providecommand\\oiiint{\\oint}").nl();
}
if (bWideslash) {
decl.append("\\newcommand\\wideslash[2]{{}^{#1}/_{#2}}").nl();
}
if (bWidebslash) {
decl.append("\\newcommand\\widebslash[2]{{}_{#1}\\backslash^{#2}}").nl();
}
if (bBoldsubformula) {
decl.append("\\newcommand\\boldsubformula[1]{\\text{\\mathversion{bold}$#1$}}").nl();
}
if (bNormalsubformula) {
decl.append("\\newcommand\\normalsubformula[1]{\\text{\\mathversion{normal}$#1$}}").nl();
}
if (bMultiscripts || bMathoverstrike) {
decl.append("\\newlength{\\idxmathdepth}\\newlength{\\idxmathtotal}\\newlength{\\idxmathwidth}\\newlength{\\idxraiseme}").nl();
decl.append("\\newcommand{\\idxdheight}[1]{\\protect\\settoheight{\\idxmathtotal}{\\(\\displaystyle#1\\)}\\protect\\settodepth{\\idxmathdepth}{\\(\\displaystyle#1\\)}\\protect\\settowidth{\\idxmathwidth}{\\(\\displaystyle#1\\)}\\protect\\addtolength{\\idxmathtotal}{\\idxmathdepth}\\protect\\setlength{\\idxraiseme}{\\idxmathtotal/2-\\idxmathdepth}}").nl();
decl.append("\\newcommand{\\idxtheight}[1]{\\protect\\settoheight{\\idxmathtotal}{\\(\\textstyle #1\\)}\\protect\\settodepth{\\idxmathdepth}{\\(\\textstyle #1\\)}\\protect\\settowidth{\\idxmathwidth}{\\(\\textstyle#1\\)}\\protect\\addtolength{\\idxmathtotal}{\\idxmathdepth}\\protect\\setlength{\\idxraiseme}{\\idxmathtotal/2-\\idxmathdepth}}").nl();
decl.append("\\newcommand{\\idxsheight}[1]{\\protect\\settoheight{\\idxmathtotal}{\\(\\scriptstyle #1\\)}\\protect\\settodepth{\\idxmathdepth}{\\(\\scriptstyle #1\\)}\\protect\\settowidth{\\idxmathwidth}{\\(\\scriptstyle#1\\)}\\protect\\addtolength{\\idxmathtotal}{\\idxmathdepth}\\protect\\setlength{\\idxraiseme}{\\idxmathtotal/2-\\idxmathdepth}}").nl();
decl.append("\\newcommand{\\idxssheight}[1]{\\protect\\settoheight{\\idxmathtotal}{\\(\\scriptscriptstyle #1\\)}\\protect\\settodepth{\\idxmathdepth}{\\(\\scriptscriptstyle #1\\)}\\protect\\settowidth{\\idxmathwidth}{\\(\\scriptscriptstyle#1\\)}\\protect\\addtolength{\\idxmathtotal}{\\idxmathdepth}\\protect\\setlength{\\idxraiseme}{\\idxmathtotal/2-\\idxmathdepth}}").nl();
}
if (bMultiscripts) {
decl.append("\\newcommand\\multiscripts[5]{\\mathchoice")
.append("{\\idxdheight{#4}\\rule[-\\idxmathdepth]{0mm}{\\idxmathtotal}#1\\underset{#2}{\\overset{#3}{#4}}\\rule[-\\idxmathdepth]{0mm}{\\idxmathtotal}#5}")
.append("{\\idxtheight{#4}\\rule[-\\idxmathdepth]{0mm}{\\idxmathtotal}#1\\underset{#2}{\\overset{#3}{#4}}\\rule[-\\idxmathdepth]{0mm}{\\idxmathtotal}#5}")
.append("{\\idxsheight{#4}\\rule[-\\idxmathdepth]{0mm}{\\idxmathtotal}#1\\underset{#2}{\\overset{#3}{#4}}\\rule[-\\idxmathdepth]{0mm}{\\idxmathtotal}#5}")
.append("{\\idxssheight{#4}\\rule[-\\idxmathdepth]{0mm}{\\idxmathtotal}#1\\underset{#2}{\\overset{#3}{#4}}\\rule[-\\idxmathdepth]{0mm}{\\idxmathtotal}#5}}")
.nl();
}
if (bMathoverstrike) {
decl.append("\\newcommand\\mathoverstrike[1]{\\mathchoice")
.append("{\\idxdheight{#1}\\rlap{\\rule[\\idxraiseme]{\\idxmathwidth}{0.4pt}}{#1}}")
.append("{\\idxtheight{#1}\\rlap{\\rule[\\idxraiseme]{\\idxmathwidth}{0.4pt}}{#1}}")
.append("{\\idxsheight{#1}\\rlap{\\rule[\\idxraiseme]{\\idxmathwidth}{0.4pt}}{#1}}")
.append("{\\idxssheight{#1}\\rlap{\\rule[\\idxraiseme]{\\idxmathwidth}{0.4pt}}{#1}}}")
.nl();
} }
} }
@ -1606,14 +1615,23 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert
private String matrix(float fSize, Token eAlign){ private String matrix(float fSize, Token eAlign){
nextToken(); nextToken();
if (curToken.eType==Token.LGROUP){ if (curToken.eType==Token.LGROUP){
StringBuffer bufMatrix=new StringBuffer().append("\\begin{matrix}"); StringBuffer bufMatrix = new StringBuffer().append("\\begin{matrix}");
int nCols = 1;
do { do {
nextToken(); nextToken();
bufMatrix.append(align(fSize,eAlign,true,true)); bufMatrix.append(align(fSize,eAlign,true,true));
if (curToken.eType==Token.POUND) bufMatrix.append("&"); if (curToken.eType==Token.POUND) {
else if (curToken.eType==Token.DPOUND) bufMatrix.append("\\\\"); bufMatrix.append("&");
nCols++;
}
else if (curToken.eType==Token.DPOUND) {
bufMatrix.append("\\\\");
nMaxMatrixCols = Math.max(nCols, nMaxMatrixCols);
nCols = 1;
}
} while (curToken.eType==Token.POUND || curToken.eType==Token.DPOUND); } while (curToken.eType==Token.POUND || curToken.eType==Token.DPOUND);
if (curToken.eType==Token.RGROUP) nextToken(); // otherwise error in formula- ignore if (curToken.eType==Token.RGROUP) nextToken(); // otherwise error in formula- ignore
nMaxMatrixCols = Math.max(nCols, nMaxMatrixCols);
return bufMatrix.append("\\end{matrix}").toString(); return bufMatrix.append("\\end{matrix}").toString();
} }
else { // error in formula else { // error in formula
@ -1637,10 +1655,10 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert
} }
} }
// Group a LaTeX string unless it consists of exactly one character // Group a LaTeX string unless it consists of exactly one character which is not a space
// In the latter case, prepend a space character (because this string follows a command sequence) // In the latter case, prepend a space character (because this string follows a command sequence)
private String groupsp(String sLaTeX) { private String groupsp(String sLaTeX) {
if (sLaTeX.length()!=1) { if (sLaTeX.length()!=1 || sLaTeX.charAt(0)==' ') {
return "{"+sLaTeX+"}"; return "{"+sLaTeX+"}";
} }
else { else {

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA * MA 02111-1307 USA
* *
* Copyright: 2002-2012 by Henrik Just * Copyright: 2002-2014 by Henrik Just
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.4 (2012-03-26) * Version 1.4 (2012-03-28)
* *
*/ */
@ -34,7 +34,7 @@ import writer2latex.util.SimpleZipReader;
public class EmbeddedBinaryObject extends EmbeddedObject { public class EmbeddedBinaryObject extends EmbeddedObject {
/** The object's binary representation. */ /** The object's binary representation. */
private byte[] objData = null; private byte[] blob = null;
/** /**
* Package private constructor for use when reading an object from a * Package private constructor for use when reading an object from a
@ -42,11 +42,12 @@ public class EmbeddedBinaryObject extends EmbeddedObject {
* *
* @param sName The name of the object. * @param sName The name of the object.
* @param sType The MIME-type of the object. * @param sType The MIME-type of the object.
* @param doc The document containing the object.
* @param source A <code>SimpleZipReader</code> containing the object * @param source A <code>SimpleZipReader</code> containing the object
*/ */
protected EmbeddedBinaryObject(String sName, String sType, SimpleZipReader source) { protected EmbeddedBinaryObject(String sName, String sType, OfficeDocument doc, SimpleZipReader source) {
super(sName,sType); super(sName,sType,doc);
objData = source.getEntry(sName); blob = source.getEntry(sName);
} }
/** Get the binary data for this object /** Get the binary data for this object
@ -54,7 +55,12 @@ public class EmbeddedBinaryObject extends EmbeddedObject {
* @return A <code>byte</code> array containing the object's data. * @return A <code>byte</code> array containing the object's data.
*/ */
public byte[] getBinaryData() { public byte[] getBinaryData() {
return objData; return blob;
} }
public void dispose() {
super.dispose();
blob = null;
}
} }

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA * MA 02111-1307 USA
* *
* Copyright: 2002-2012 by Henrik Just * Copyright: 2002-2014 by Henrik Just
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.4 (2012-03-27) * Version 1.4 (2014-08-27)
* *
*/ */
@ -29,6 +29,7 @@ package writer2latex.office;
/** This class represents and embedded object within an ODF package document /** This class represents and embedded object within an ODF package document
*/ */
public abstract class EmbeddedObject { public abstract class EmbeddedObject {
private OfficeDocument doc;
private String sName; private String sName;
private String sType; private String sType;
@ -36,10 +37,12 @@ public abstract class EmbeddedObject {
* *
* @param sName The name of the object. * @param sName The name of the object.
* @param sType The MIME-type of the object. * @param sType The MIME-type of the object.
* @param doc The document to which the object belongs.
*/ */
protected EmbeddedObject(String sName, String sType) { protected EmbeddedObject(String sName, String sType, OfficeDocument doc) {
this.sName = sName; this.sName = sName;
this.sType = sType; this.sType = sType;
this.doc = doc;
} }
/** Get the name of the embedded object represented by this instance. /** Get the name of the embedded object represented by this instance.
@ -58,4 +61,12 @@ public abstract class EmbeddedObject {
return sType; return sType;
} }
/** Dispose this <code>EmbeddedObject</code>. This implies that the content is nullified and the object
* is removed from the collection in the <code>OfficeDocument</code>.
*
*/
public void dispose() {
doc.removeEmbeddedObject(sName);
}
} }

View file

@ -20,7 +20,7 @@
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.4 (2014-08-25) * Version 1.4 (2014-08-28)
* *
*/ */
@ -55,8 +55,8 @@ public class EmbeddedXMLObject extends EmbeddedObject {
* @param sType The MIME-type of the object. * @param sType The MIME-type of the object.
* @param source A ZIP reader providing the contents of the package * @param source A ZIP reader providing the contents of the package
*/ */
protected EmbeddedXMLObject(String sName, String sType, SimpleZipReader source) { protected EmbeddedXMLObject(String sName, String sType, OfficeDocument doc, SimpleZipReader source) {
super(sName, sType); super(sName, sType, doc);
// Read the bytes, but defer parsing until required (at that point, the bytes are nullified) // Read the bytes, but defer parsing until required (at that point, the bytes are nullified)
contentBytes = source.getEntry(sName+"/"+OfficeDocument.CONTENTXML); contentBytes = source.getEntry(sName+"/"+OfficeDocument.CONTENTXML);
stylesBytes = source.getEntry(sName+"/"+OfficeDocument.STYLESXML); stylesBytes = source.getEntry(sName+"/"+OfficeDocument.STYLESXML);
@ -100,5 +100,13 @@ public class EmbeddedXMLObject extends EmbeddedObject {
} }
return null; return null;
} }
public void dispose() {
super.dispose();
contentBytes = null;
stylesBytes = null;
contentDOM = null;
stylesDOM = null;
}
} }

View file

@ -20,7 +20,7 @@
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.4 (2014-08-25) * Version 1.4 (2014-08-28)
* *
*/ */
@ -77,7 +77,7 @@ public class OfficeDocument {
/** Collection to keep track of the embedded objects in the document. */ /** Collection to keep track of the embedded objects in the document. */
private Map<String, EmbeddedObject> embeddedObjects = null; private Map<String, EmbeddedObject> embeddedObjects = null;
/** Package or flat format? /** Package or flat format?
* @return true if the document is in package format, false if it's flat XML * @return true if the document is in package format, false if it's flat XML
*/ */
@ -145,13 +145,13 @@ public class OfficeDocument {
if (sPath.endsWith("/")) { // Remove trailing slash if (sPath.endsWith("/")) { // Remove trailing slash
sPath=sPath.substring(0, sPath.length()-1); sPath=sPath.substring(0, sPath.length()-1);
} }
embeddedObjects.put(sPath, new EmbeddedXMLObject(sPath, sType, zip)); embeddedObjects.put(sPath, new EmbeddedXMLObject(sPath, sType, this, zip));
} }
} }
else if (!sType.equals("text/xml")) { else if (!sType.equals("text/xml")) {
// XML entries are either embedded ODF doc entries or main document entries, all other // XML entries are either embedded ODF doc entries or main document entries, all other
// entries are included as binary objects // entries are included as binary objects
embeddedObjects.put(sPath, new EmbeddedBinaryObject(sPath, sType, zip)); embeddedObjects.put(sPath, new EmbeddedBinaryObject(sPath, sType, this, zip));
} }
} }
} }
@ -173,6 +173,12 @@ public class OfficeDocument {
} }
return null; return null;
} }
protected void removeEmbeddedObject(String sName) {
if (sName!=null && embeddedObjects!=null && embeddedObjects.containsKey(sName)) {
embeddedObjects.remove(sName);
}
}
/** /**
* Read the document from a DOM tree (flat XML format) * Read the document from a DOM tree (flat XML format)

View file

@ -20,7 +20,7 @@
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.4 (2014-08-27) * Version 1.4 (2014-09-03)
* *
*/ */
@ -220,7 +220,7 @@ public class OfficeReader {
return nCount; return nCount;
} }
public String getTextContent(Node node) { public static String getTextContent(Node node) {
String s = ""; String s = "";
Node child = node.getFirstChild(); Node child = node.getFirstChild();
while (child!=null) { while (child!=null) {
@ -360,10 +360,10 @@ public class OfficeReader {
private boolean bText = false; private boolean bText = false;
private boolean bSpreadsheet = false; private boolean bSpreadsheet = false;
private boolean bPresentation = false; private boolean bPresentation = false;
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// Various methods // Various methods
/** Checks whether or not this document is in package format /** Checks whether or not this document is in package format
* @return true if it's in package format * @return true if it's in package format
*/ */
@ -394,7 +394,14 @@ public class OfficeReader {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// Accessor methods // Accessor methods
/** Get an embedded object in this office document
*
*/
public EmbeddedObject getEmbeddedObject(String sName) {
return oooDoc.getEmbeddedObject(sName);
}
/** <p>Get the collection of all font declarations.</p> /** <p>Get the collection of all font declarations.</p>
* @return the <code>OfficeStyleFamily</code> of font declarations * @return the <code>OfficeStyleFamily</code> of font declarations
*/ */

View file

@ -32,6 +32,8 @@ import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.lang.Math; import java.lang.Math;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.text.Collator; import java.text.Collator;
@ -294,6 +296,38 @@ public class Misc{
return name; return name;
} }
/** Get the file name part of an URL
*
* @param sURL the URL from which the filename should be extracted
* @return the file name
*/
public static final String getFileName(String sURL) {
try {
URI uri = new URI(sURL);
String sPath = uri.getPath();
return sPath.substring(sPath.lastIndexOf('/')+1);
} catch (URISyntaxException e) {
e.printStackTrace();
return "";
}
}
/** Get the file extension from an URL
*
* @param sURL
* @return the file extension (including dot) or the empty string if there is no file extension
*/
public static final String getFileExtension(String sURL) {
String sFileName = getFileName(sURL);
int nDot = sFileName.lastIndexOf('.');
if (nDot>=0) {
return sFileName.substring(nDot);
}
else {
return "";
}
}
public static final String removeExtension(String sName) { public static final String removeExtension(String sName) {
int n = sName.lastIndexOf("."); int n = sName.lastIndexOf(".");
@ -417,7 +451,7 @@ public class Misc{
*/ */
public static String makeHref(String s) { public static String makeHref(String s) {
try { try {
java.net.URI uri = new java.net.URI(null, null, s, null); URI uri = new URI(null, null, s, null);
return uri.toString(); return uri.toString();
} }
catch (Exception e) { catch (Exception e) {

View file

@ -20,7 +20,7 @@
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.4 (2014-08-26) * Version 1.4 (2014-09-03)
* *
*/ */
@ -466,30 +466,22 @@ public class DrawConverter extends ConverterHelper {
// Get the image from the ImageLoader // Get the image from the ImageLoader
BinaryGraphicsDocument bgd = null; BinaryGraphicsDocument bgd = null;
String sFileName = null; String sFileName = null;
String sHref = Misc.getAttribute(onode,XMLString.XLINK_HREF); bgd = converter.getImageCv().getImage(onode);
if (sHref!=null && sHref.length()>0 && !ofr.isInPackage(sHref)) { if (bgd!=null) {
// Linked image is not yet handled by ImageLoader. This is a temp. if (!bgd.isLinked()) { // embedded image
// solution (will go away when ImageLoader is finished)
if (!converter.isOPS()) { // Cannot have linked images in EPUB, ignore the image
sFileName = sHref;
// In OpenDocument *package* format ../ means "leave the package"
if (ofr.isOpenDocument() && ofr.isPackageFormat() && sFileName.startsWith("../")) {
sFileName=sFileName.substring(3);
}
//String sExt = sHref.substring(sHref.lastIndexOf(".")).toLowerCase();
}
}
else { // embedded or base64 encoded image
bgd = converter.getImageCv().getImage(onode);
if (bgd!=null) {
sFileName = bgd.getFileName(); sFileName = bgd.getFileName();
// If this is the cover image, add it to the converter result // If this is the cover image, add it to the converter result
if (bCoverImage && onode==ofr.getFirstImage()) { if (bCoverImage && onode==ofr.getFirstImage()) {
converter.setCoverImageFile(bgd,null); converter.setCoverImageFile(bgd,null);
} }
} }
} else { // linked image
if (!converter.isOPS()) { // Cannot have linked images in EPUB, ignore the image
sFileName = bgd.getURL();
}
}
}
if (sFileName==null) { return; } // TODO: Add warning? if (sFileName==null) { return; } // TODO: Add warning?
// Create the image (sFileName contains the file name) // Create the image (sFileName contains the file name)

View file

@ -20,7 +20,7 @@
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Version 1.4 (2014-08-26) * Version 1.4 (2014-09-01)
* *
*/ */
@ -70,7 +70,7 @@ public class MathConverter extends ConverterHelper {
* @param onode the math node * @param onode the math node
* @param hnode the xhtml node to which content should be added * @param hnode the xhtml node to which content should be added
*/ */
public void convert(Node image, Element onode, Node hnode, boolean bAllowDisplayStyle) { public void convert(Element image, Element onode, Node hnode, boolean bAllowDisplayStyle) {
if (bSupportMathML) { if (bSupportMathML) {
convertAsMathML(onode,hnode,bAllowDisplayStyle); convertAsMathML(onode,hnode,bAllowDisplayStyle);
} }
@ -131,7 +131,7 @@ public class MathConverter extends ConverterHelper {
// For plain xhtml: Convert the formula as an image or as plain text // For plain xhtml: Convert the formula as an image or as plain text
private void convertAsImageOrText(Node image, Node onode, Node hnode) { private void convertAsImageOrText(Element image, Node onode, Node hnode) {
NodeList annotationList = ((Element) onode).getElementsByTagName(XMLString.ANNOTATION); // Since OOo 3.2 NodeList annotationList = ((Element) onode).getElementsByTagName(XMLString.ANNOTATION); // Since OOo 3.2
if (annotationList.getLength()==0) { if (annotationList.getLength()==0) {
annotationList = ((Element) onode).getElementsByTagName(XMLString.MATH_ANNOTATION); annotationList = ((Element) onode).getElementsByTagName(XMLString.MATH_ANNOTATION);
@ -153,7 +153,7 @@ public class MathConverter extends ConverterHelper {
if (sHref==null || sHref.length()==0 || ofr.isInPackage(sHref)) { if (sHref==null || sHref.length()==0 || ofr.isInPackage(sHref)) {
BinaryGraphicsDocument bgd = converter.getImageCv().getImage(image); BinaryGraphicsDocument bgd = converter.getImageCv().getImage(image);
if (bgd!=null) { if (bgd!=null) {
String sMIME = bgd.getDocumentMIMEType(); String sMIME = bgd.getMIMEType();
if (MIMETypes.PNG.equals(sMIME) || MIMETypes.JPEG.equals(sMIME) || MIMETypes.GIF.equals(sMIME)) { if (MIMETypes.PNG.equals(sMIME) || MIMETypes.JPEG.equals(sMIME) || MIMETypes.GIF.equals(sMIME)) {
converter.addDocument(bgd); converter.addDocument(bgd);
// Create the image and add the StarMath/LaTeX formula as alternative text // Create the image and add the StarMath/LaTeX formula as alternative text