From 74d7599b115830e4bd74769d5f6dab12738303ab Mon Sep 17 00:00:00 2001 From: henrikjust Date: Wed, 3 Sep 2014 07:04:31 +0000 Subject: [PATCH] Refactoring image conversion + some math bugfixes git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@168 f0f2a975-2e09-46c8-9428-3b39399b9f3c --- source/distro/changelog.txt | 8 + .../w2lcommon/filter/ExportFilterBase.java | 4 +- source/java/writer2latex/api/Converter.java | 7 +- .../writer2latex/api/ConverterFactory.java | 4 +- .../base/BinaryGraphicsDocument.java | 201 ++++++------ .../java/writer2latex/base/ConverterBase.java | 12 +- .../writer2latex/base/ImageConverter.java | 292 +++++++++++------- .../writer2latex/latex/DrawConverter.java | 50 ++- .../writer2latex/latex/MathConverter.java | 9 +- .../java/writer2latex/latex/ParConverter.java | 7 +- .../writer2latex/latex/StarMathConverter.java | 148 +++++---- .../office/EmbeddedBinaryObject.java | 20 +- .../writer2latex/office/EmbeddedObject.java | 17 +- .../office/EmbeddedXMLObject.java | 16 +- .../writer2latex/office/OfficeDocument.java | 14 +- .../writer2latex/office/OfficeReader.java | 17 +- source/java/writer2latex/util/Misc.java | 36 ++- .../writer2latex/xhtml/DrawConverter.java | 32 +- .../writer2latex/xhtml/MathConverter.java | 8 +- 19 files changed, 533 insertions(+), 369 deletions(-) diff --git a/source/distro/changelog.txt b/source/distro/changelog.txt index b8b8078..c6f7095 100644 --- a/source/distro/changelog.txt +++ b/source/distro/changelog.txt @@ -2,6 +2,14 @@ Changelog for Writer2LaTeX version 1.2 -> 1.4 ---------- 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] Optimized reading of package format: The settings.xml files are not parsed and the unused parts of the ZIP file are disposed diff --git a/source/java/org/openoffice/da/comp/w2lcommon/filter/ExportFilterBase.java b/source/java/org/openoffice/da/comp/w2lcommon/filter/ExportFilterBase.java index ca6d439..a67726f 100644 --- a/source/java/org/openoffice/da/comp/w2lcommon/filter/ExportFilterBase.java +++ b/source/java/org/openoffice/da/comp/w2lcommon/filter/ExportFilterBase.java @@ -20,7 +20,7 @@ * * 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)); // Do conversion. The base name is take from the URL provided by the office - Iterator docEnum = converter.convert(dom,Misc.makeFileName(getFileName(sURL))).iterator(); + Iterator docEnum = converter.convert(dom,Misc.makeFileName(getFileName(sURL)),true).iterator(); if (docEnum.hasNext()) { // The master document is written to the XOutStream supplied by the XMLFilterAdaptor diff --git a/source/java/writer2latex/api/Converter.java b/source/java/writer2latex/api/Converter.java index ec6b453..3bf22ae 100644 --- a/source/java/writer2latex/api/Converter.java +++ b/source/java/writer2latex/api/Converter.java @@ -16,11 +16,11 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * - * Copyright: 2002-2012 by Henrik Just + * Copyright: 2002-2014 by Henrik Just * * 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 * (if the converted document is a compound document consisting consisting * 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 ConverterResult containing the converted 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; } diff --git a/source/java/writer2latex/api/ConverterFactory.java b/source/java/writer2latex/api/ConverterFactory.java index a1b7448..af29092 100644 --- a/source/java/writer2latex/api/ConverterFactory.java +++ b/source/java/writer2latex/api/ConverterFactory.java @@ -20,7 +20,7 @@ * * All Rights Reserved. * - * Version 1.4 (2012-08-27) + * Version 1.4 (2012-08-28) * */ @@ -33,7 +33,7 @@ public class ConverterFactory { // Version information 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 * (major version).(minor version).(patch level)
diff --git a/source/java/writer2latex/base/BinaryGraphicsDocument.java b/source/java/writer2latex/base/BinaryGraphicsDocument.java index c39c292..aafebc4 100644 --- a/source/java/writer2latex/base/BinaryGraphicsDocument.java +++ b/source/java/writer2latex/base/BinaryGraphicsDocument.java @@ -20,160 +20,163 @@ * * All Rights Reserved. * - * Version 1.4 (2014-08-25) + * Version 1.4 (2014-09-03) * */ package writer2latex.base; import java.io.OutputStream; -import java.io.InputStream; -//import java.io.ByteArrayOutputStream; import java.io.IOException; import writer2latex.api.OutputFile; import writer2latex.util.Misc; -/** - *

Class representing a binary graphics document. - * This class is used for representing graphics documents that are not - * interpreted in any way, but simply copied verbatim from the source format - * to the target format.

- * - *

GraphicsDocument is used to create new graphics documents.

- * +/** This class is used to represent a binary graphics document to be included in the converter result. + * I may also represent a linked image, which should not be included (and will produce an empty file + * if it is). */ public class BinaryGraphicsDocument implements OutputFile { - //private final static int BUFFERSIZE = 1024; - - private String docName; - - private byte[] data; - private int nOff; - private int nLen; - + private String sFileName; private String sFileExtension; 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.

+ /**Constructs a new graphics document. + * This new document does not contain any data. Document data must + * be added using the appropriate methods. * - *

This new document does not contain any information. Document data must - * either be added using appropriate methods, or an existing file can be - * {@link #read(InputStream) read} from an InputStream.

- * - * @param name The name of the GraphicsDocument. + * @param sName The name of the GraphicsDocument. + * @param sFileExtension the file extension + * @param sMimeType the MIME type of the document */ public BinaryGraphicsDocument(String name, String sFileExtension, String sMimeType) { this.sFileExtension = sFileExtension; this.sMimeType = sMimeType; - docName = trimDocumentName(name); + sFileName = Misc.trimDocumentName(name, sFileExtension); } - - - /** - *

This method reads byte data from the InputStream.

+ + /** Set image contents to a byte array * - * @param is InputStream containing a binary data file. - * - * @throws IOException In case of any I/O errors. + * @param data the image data */ - public void read(InputStream is) throws IOException { - data = Misc.inputStreamToByteArray(is); - } - - public void read(byte[] data) { - read(data,0,data.length); + public void setData(byte[] data, boolean bIsAcceptedFormat) { + setData(data,0,data.length,bIsAcceptedFormat); } - public void read(byte[] data, int nOff, int nLen) { - this.data = data; + /** Set image contents to part of a byte array + * + * @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.nLen = nLen; + this.bIsAcceptedFormat = bIsAcceptedFormat; + this.bIsLinked = false; + this.sURL = null; } - /* - * Utility method to make sure the document name is stripped of any file - * extensions before use. + /** Set the URL of a linked image + * + * @param sURL the URL */ - private String trimDocumentName(String name) { - String temp = name.toLowerCase(); - - if (temp.endsWith(getFileExtension())) { - // strip the extension - int nlen = name.length(); - int endIndex = nlen - getFileExtension().length(); - name = name.substring(0,endIndex); - } - - return name; - } - - /** - *

Returns the Document name with no file extension.

- * - * @return The Document name with no file extension. - */ - public String getName() { - return docName; + 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; } - /** - *

Returns the Document name with file extension.

- * - * @return The Document name with file extension. + /** Get the URL of a linked image + * + * @return the URL or null if this is an embedded image */ - public String getFileName() { - return new String(docName + getFileExtension()); + public String getURL() { + return sURL; + } + + /** Does this BinaryGraphicsDocument 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() { - return data; + return blob; } + // Implement OutputFile - /** - *

Writes out the Document content to the specified - * OutputStream.

+ /** Writes out the content to the specified OutputStream. + * Linked images will not write any data. * - *

This method may not be thread-safe. - * Implementations may or may not synchronize this - * method. User code (i.e. caller) must make sure that - * calls to this method are thread-safe.

- * - * @param os OutputStream to write out the - * Document content. + * @param os OutputStream to write out the content. * * @throws IOException If any I/O error occurs. */ 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; } - /** - * Returns the file extension for this type of - * Document. + /** Get the document with file extension.

+ * + * @return The document with file extension. + */ + public String getFileName() { + return sFileName + sFileExtension; + } + + /** Get the MIME type of the document. * - * @return The file extension of Document. + * @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() { 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; } - } \ No newline at end of file diff --git a/source/java/writer2latex/base/ConverterBase.java b/source/java/writer2latex/base/ConverterBase.java index 3c64fbf..0a0ad70 100644 --- a/source/java/writer2latex/base/ConverterBase.java +++ b/source/java/writer2latex/base/ConverterBase.java @@ -20,7 +20,7 @@ * * 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 odDoc = new OfficeDocument(); 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 odDoc = new OfficeDocument(); 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); metaData = new MetaData(odDoc); - imageConverter = new ImageConverter(odDoc,true); + imageConverter = new ImageConverter(ofr,bDestructive,true); imageConverter.setGraphicConverter(graphicConverter); // Prepare output diff --git a/source/java/writer2latex/base/ImageConverter.java b/source/java/writer2latex/base/ImageConverter.java index fb2a137..0759f0f 100644 --- a/source/java/writer2latex/base/ImageConverter.java +++ b/source/java/writer2latex/base/ImageConverter.java @@ -1,6 +1,6 @@ /************************************************************************ * - * ImageLoader.java + * ImageConverter.java * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -16,11 +16,11 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * - * Copyright: 2002-2012 by Henrik Just + * Copyright: 2002-2014 by Henrik Just * * 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.util.HashSet; +import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @@ -37,20 +38,22 @@ import writer2latex.api.GraphicConverter; import writer2latex.office.EmbeddedBinaryObject; import writer2latex.office.EmbeddedObject; import writer2latex.office.MIMETypes; -import writer2latex.office.OfficeDocument; +import writer2latex.office.OfficeReader; import writer2latex.office.SVMReader; import writer2latex.office.XMLString; import writer2latex.util.Base64; import writer2latex.util.Misc; -/** - *

This class extracts images from an OOo file. - * The images are returned as BinaryGraphicsDocument.

+/** This class extracts and converts images from an office document. + * The images are returned as BinaryGraphicsDocument. + * 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 { - // The Office document to load images from - private OfficeDocument oooDoc; - + private OfficeReader ofr; + private boolean bDestructive; + // Data for file name generation private String sBaseFileName = ""; private String sSubDirName = ""; @@ -67,135 +70,210 @@ public final class ImageConverter { private String sDefaultVectorFormat = null; private HashSet acceptedFormats = new HashSet(); - public ImageConverter(OfficeDocument oooDoc, boolean bExtractEPS) { - this.oooDoc = oooDoc; + /** Construct a new ImageConverter referring to a specific document + * + * @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.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 ImageConverter 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) { addAcceptedFormat(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) { addAcceptedFormat(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 GraphicConverter to use for image conversion + * + * @param gcv the graphics converter + */ + public void setGraphicConverter(GraphicConverter gcv) { + this.gcv = gcv; + } - public BinaryGraphicsDocument getImage(Node node) { - // node must be a draw:image element. - // variables to hold data about the image: - String sMIME = null; - String sExt = null; - byte[] blob = null; + /** Get an image from a draw:image element. If the converter is destructive, the returned + * BinaryGraphicsDocument will hold the only reference to the image data (the original + * data will be removed). + * + * @param node the image element + * @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); - if (sHref==null || sHref.length()==0) { - // Image must be contained in an office:binary-element as base64: - Node obd = Misc.getChildByTagName(node,XMLString.OFFICE_BINARY_DATA); - if (obd!=null) { - StringBuffer buf = new StringBuffer(); - NodeList nl = obd.getChildNodes(); - int nLen = nl.getLength(); - for (int i=0; i0) { - // If the manifest provides a media type, trust that - sExt = MIMETypes.getFileExtension(sMIME); - } - else { - // Otherwise determine it by byte inspection - sMIME = MIMETypes.getMagicMIMEType(blob); - sExt = MIMETypes.getFileExtension(sMIME); - } - } - else { - // This is a linked image - // TODO: Perhaps we should download the image from the url in sHref? - // Alternatively BinaryGraphicsDocument should be extended to - // handle external graphics. - } - } - - if (blob==null) { return null; } - - // Assign a name (without extension) + // Image data + 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) { + // Image may be embedded in package: + String sPath = sHref; + if (sPath.startsWith("#")) { sPath = sPath.substring(1); } + if (sPath.startsWith("./")) { sPath = sPath.substring(2); } + EmbeddedObject obj = ofr.getEmbeddedObject(sPath); + if (obj!=null && obj instanceof EmbeddedBinaryObject) { + EmbeddedBinaryObject object = (EmbeddedBinaryObject) obj; + blob = object.getBinaryData(); + sMIME = object.getType(); + if (sMIME.length()==0) { + // If the manifest provides a media type, trust that + // Otherwise determine it by byte inspection + sMIME = MIMETypes.getMagicMIMEType(blob); + } + sExt = MIMETypes.getFileExtension(sMIME); + if (bDestructive) { + object.dispose(); + } + } + else { + // This is a linked image + // TODO: Add option to download image from the URL? + BinaryGraphicsDocument bgd + = new BinaryGraphicsDocument(Misc.getFileName(sHref),Misc.getFileExtension(sHref),null); + bgd.setURL(ofr.fixRelativeLink(sHref)); + return bgd; + } + } + } + + // If there is no suitable xlink:href attribute, the image must be contained in an office:binary-element as base64 + if (blob==null) { + Node obd = Misc.getChildByTagName(node,XMLString.OFFICE_BINARY_DATA); + if (obd!=null) { + StringBuffer buf = new StringBuffer(); + NodeList nl = obd.getChildNodes(); + int nLen = nl.getLength(); + for (int i=0; i0 && !ofr.isInPackage(sHref)) { - // Linked image is not yet handled by ImageLoader. This is a temp. - // 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) { + + BinaryGraphicsDocument bgd = palette.getImageCv().getImage(node); + if (bgd!=null) { + if (!bgd.isLinked()) { // embedded image palette.addDocument(bgd); sFileName = bgd.getFileName(); - String sMIME = bgd.getDocumentMIMEType(); + String sMIME = bgd.getMIMEType(); bCommentOut = !( config.getBackend()==LaTeXConfig.UNSPECIFIED || (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.PDF.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))); + } } - - if (sFileName==null) { + else { ldp.append("[Warning: Image not found]"); return; } diff --git a/source/java/writer2latex/latex/MathConverter.java b/source/java/writer2latex/latex/MathConverter.java index 6c02f7c..79a28d3 100644 --- a/source/java/writer2latex/latex/MathConverter.java +++ b/source/java/writer2latex/latex/MathConverter.java @@ -20,7 +20,7 @@ * * 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) { if (bContainsFormulas) { - if (config.useOoomath()) { - pack.append("\\usepackage{ooomath}").nl(); - } - else { - smc.appendDeclarations(pack,decl); - } + smc.appendDeclarations(pack,decl); } if (bNeedTexMathsPreamble) { // The preamble may be stored as a user defined property (newline is represented as paragraph sign) diff --git a/source/java/writer2latex/latex/ParConverter.java b/source/java/writer2latex/latex/ParConverter.java index 34c9542..e2f9018 100644 --- a/source/java/writer2latex/latex/ParConverter.java +++ b/source/java/writer2latex/latex/ParConverter.java @@ -16,11 +16,11 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * - * Copyright: 2002-2011 by Henrik Just + * Copyright: 2002-2014 by Henrik Just * * 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}). */ 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 String sStyleName = node.getAttribute(XMLString.TEXT_STYLE_NAME); diff --git a/source/java/writer2latex/latex/StarMathConverter.java b/source/java/writer2latex/latex/StarMathConverter.java index bb1224d..3685427 100644 --- a/source/java/writer2latex/latex/StarMathConverter.java +++ b/source/java/writer2latex/latex/StarMathConverter.java @@ -18,7 +18,7 @@ * * Copyright: 2002-2014 by Henrik Just * - * Version 1.4 (2014-08-05) + * Version 1.4 (2014-09-03) * * All Rights Reserved. */ @@ -723,6 +723,7 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert private LaTeXConfig config; private Map configSymbols; 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 SimpleInputBuffer buffer; // contains the starmath formula //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) { - if (bDefeq) { - decl.append("\\newcommand\\defeq{\\stackrel{\\mathrm{def}}{=}}").nl(); + if (config.useOoomath()) { + pack.append("\\usepackage{ooomath}").nl(); } - if (bLambdabar) { - decl.append("\\newcommand\\lambdabar{\\mathchar'26\\mkern-10mu\\lambda}").nl(); + else { + 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) { - 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 (nMaxMatrixCols>10) { // The default for the matrix environment is at most 10 columns + decl.append("\\setcounter{MaxMatrixCols}{").append(Integer.toString(nMaxMatrixCols)).append("}").nl(); } } @@ -1606,14 +1615,23 @@ public final class StarMathConverter implements writer2latex.api.StarMathConvert private String matrix(float fSize, Token eAlign){ nextToken(); if (curToken.eType==Token.LGROUP){ - StringBuffer bufMatrix=new StringBuffer().append("\\begin{matrix}"); + StringBuffer bufMatrix = new StringBuffer().append("\\begin{matrix}"); + int nCols = 1; do { nextToken(); bufMatrix.append(align(fSize,eAlign,true,true)); - if (curToken.eType==Token.POUND) bufMatrix.append("&"); - else if (curToken.eType==Token.DPOUND) bufMatrix.append("\\\\"); + if (curToken.eType==Token.POUND) { + 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); if (curToken.eType==Token.RGROUP) nextToken(); // otherwise error in formula- ignore + nMaxMatrixCols = Math.max(nCols, nMaxMatrixCols); return bufMatrix.append("\\end{matrix}").toString(); } 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) private String groupsp(String sLaTeX) { - if (sLaTeX.length()!=1) { + if (sLaTeX.length()!=1 || sLaTeX.charAt(0)==' ') { return "{"+sLaTeX+"}"; } else { diff --git a/source/java/writer2latex/office/EmbeddedBinaryObject.java b/source/java/writer2latex/office/EmbeddedBinaryObject.java index 4b9c061..ae54000 100644 --- a/source/java/writer2latex/office/EmbeddedBinaryObject.java +++ b/source/java/writer2latex/office/EmbeddedBinaryObject.java @@ -16,11 +16,11 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * - * Copyright: 2002-2012 by Henrik Just + * Copyright: 2002-2014 by Henrik Just * * 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 { /** The object's binary representation. */ - private byte[] objData = null; + private byte[] blob = null; /** * 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 sType The MIME-type of the object. + * @param doc The document containing the object. * @param source A SimpleZipReader containing the object */ - protected EmbeddedBinaryObject(String sName, String sType, SimpleZipReader source) { - super(sName,sType); - objData = source.getEntry(sName); + protected EmbeddedBinaryObject(String sName, String sType, OfficeDocument doc, SimpleZipReader source) { + super(sName,sType,doc); + blob = source.getEntry(sName); } /** Get the binary data for this object @@ -54,7 +55,12 @@ public class EmbeddedBinaryObject extends EmbeddedObject { * @return A byte array containing the object's data. */ public byte[] getBinaryData() { - return objData; + return blob; } + public void dispose() { + super.dispose(); + blob = null; + } + } diff --git a/source/java/writer2latex/office/EmbeddedObject.java b/source/java/writer2latex/office/EmbeddedObject.java index 4dd7dc1..62875de 100644 --- a/source/java/writer2latex/office/EmbeddedObject.java +++ b/source/java/writer2latex/office/EmbeddedObject.java @@ -16,11 +16,11 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * - * Copyright: 2002-2012 by Henrik Just + * Copyright: 2002-2014 by Henrik Just * * 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 */ public abstract class EmbeddedObject { + private OfficeDocument doc; private String sName; private String sType; @@ -36,10 +37,12 @@ public abstract class EmbeddedObject { * * @param sName The name 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.sType = sType; + this.doc = doc; } /** Get the name of the embedded object represented by this instance. @@ -58,4 +61,12 @@ public abstract class EmbeddedObject { return sType; } + /** Dispose this EmbeddedObject. This implies that the content is nullified and the object + * is removed from the collection in the OfficeDocument. + * + */ + public void dispose() { + doc.removeEmbeddedObject(sName); + } + } \ No newline at end of file diff --git a/source/java/writer2latex/office/EmbeddedXMLObject.java b/source/java/writer2latex/office/EmbeddedXMLObject.java index 5967676..105946c 100644 --- a/source/java/writer2latex/office/EmbeddedXMLObject.java +++ b/source/java/writer2latex/office/EmbeddedXMLObject.java @@ -20,7 +20,7 @@ * * 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 source A ZIP reader providing the contents of the package */ - protected EmbeddedXMLObject(String sName, String sType, SimpleZipReader source) { - super(sName, sType); + protected EmbeddedXMLObject(String sName, String sType, OfficeDocument doc, SimpleZipReader source) { + super(sName, sType, doc); // Read the bytes, but defer parsing until required (at that point, the bytes are nullified) contentBytes = source.getEntry(sName+"/"+OfficeDocument.CONTENTXML); stylesBytes = source.getEntry(sName+"/"+OfficeDocument.STYLESXML); @@ -100,5 +100,13 @@ public class EmbeddedXMLObject extends EmbeddedObject { } return null; } - + + public void dispose() { + super.dispose(); + contentBytes = null; + stylesBytes = null; + contentDOM = null; + stylesDOM = null; + } + } \ No newline at end of file diff --git a/source/java/writer2latex/office/OfficeDocument.java b/source/java/writer2latex/office/OfficeDocument.java index 70ce0b6..fc89667 100644 --- a/source/java/writer2latex/office/OfficeDocument.java +++ b/source/java/writer2latex/office/OfficeDocument.java @@ -20,7 +20,7 @@ * * 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. */ private Map embeddedObjects = null; - + /** Package or flat format? * @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 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")) { // XML entries are either embedded ODF doc entries or main document entries, all other // 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; } + + 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) diff --git a/source/java/writer2latex/office/OfficeReader.java b/source/java/writer2latex/office/OfficeReader.java index fb9891e..4be2e3d 100644 --- a/source/java/writer2latex/office/OfficeReader.java +++ b/source/java/writer2latex/office/OfficeReader.java @@ -20,7 +20,7 @@ * * All Rights Reserved. * - * Version 1.4 (2014-08-27) + * Version 1.4 (2014-09-03) * */ @@ -220,7 +220,7 @@ public class OfficeReader { return nCount; } - public String getTextContent(Node node) { + public static String getTextContent(Node node) { String s = ""; Node child = node.getFirstChild(); while (child!=null) { @@ -360,10 +360,10 @@ public class OfficeReader { private boolean bText = false; private boolean bSpreadsheet = false; private boolean bPresentation = false; - + /////////////////////////////////////////////////////////////////////////// // Various methods - + /** Checks whether or not this document is in package format * @return true if it's in package format */ @@ -394,7 +394,14 @@ public class OfficeReader { /////////////////////////////////////////////////////////////////////////// // Accessor methods - + + /** Get an embedded object in this office document + * + */ + public EmbeddedObject getEmbeddedObject(String sName) { + return oooDoc.getEmbeddedObject(sName); + } + /**

Get the collection of all font declarations.

* @return the OfficeStyleFamily of font declarations */ diff --git a/source/java/writer2latex/util/Misc.java b/source/java/writer2latex/util/Misc.java index 68c76b0..609927c 100644 --- a/source/java/writer2latex/util/Misc.java +++ b/source/java/writer2latex/util/Misc.java @@ -32,6 +32,8 @@ import java.io.InputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.lang.Math; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URLEncoder; import java.net.URLDecoder; import java.text.Collator; @@ -294,6 +296,38 @@ public class Misc{ 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) { int n = sName.lastIndexOf("."); @@ -417,7 +451,7 @@ public class Misc{ */ public static String makeHref(String s) { try { - java.net.URI uri = new java.net.URI(null, null, s, null); + URI uri = new URI(null, null, s, null); return uri.toString(); } catch (Exception e) { diff --git a/source/java/writer2latex/xhtml/DrawConverter.java b/source/java/writer2latex/xhtml/DrawConverter.java index d192fe7..f9a26c4 100644 --- a/source/java/writer2latex/xhtml/DrawConverter.java +++ b/source/java/writer2latex/xhtml/DrawConverter.java @@ -20,7 +20,7 @@ * * 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 BinaryGraphicsDocument bgd = null; String sFileName = null; - String sHref = Misc.getAttribute(onode,XMLString.XLINK_HREF); - if (sHref!=null && sHref.length()>0 && !ofr.isInPackage(sHref)) { - // Linked image is not yet handled by ImageLoader. This is a temp. - // 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) { + bgd = converter.getImageCv().getImage(onode); + if (bgd!=null) { + if (!bgd.isLinked()) { // embedded image sFileName = bgd.getFileName(); // If this is the cover image, add it to the converter result if (bCoverImage && onode==ofr.getFirstImage()) { 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? // Create the image (sFileName contains the file name) diff --git a/source/java/writer2latex/xhtml/MathConverter.java b/source/java/writer2latex/xhtml/MathConverter.java index 622b22e..2d48289 100644 --- a/source/java/writer2latex/xhtml/MathConverter.java +++ b/source/java/writer2latex/xhtml/MathConverter.java @@ -20,7 +20,7 @@ * * 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 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) { 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 - 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 if (annotationList.getLength()==0) { 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)) { BinaryGraphicsDocument bgd = converter.getImageCv().getImage(image); if (bgd!=null) { - String sMIME = bgd.getDocumentMIMEType(); + String sMIME = bgd.getMIMEType(); if (MIMETypes.PNG.equals(sMIME) || MIMETypes.JPEG.equals(sMIME) || MIMETypes.GIF.equals(sMIME)) { converter.addDocument(bgd); // Create the image and add the StarMath/LaTeX formula as alternative text