Experimental support for embedded SVG in HTML5

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@146 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2012-04-03 07:50:53 +00:00
parent d1ef4d8f8c
commit 71bff7e653
13 changed files with 187 additions and 115 deletions

View file

@ -16,19 +16,16 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2011 by Henrik Just
* Copyright: 2002-2012 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.2 (2011-07-22)
* Version 1.4 (2012-04-03)
*
*/
package writer2latex.office;
//import java.io.ByteArrayInputStream;
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashSet;
@ -41,8 +38,6 @@ import writer2latex.util.Base64;
import writer2latex.util.Misc;
import writer2latex.xmerge.BinaryGraphicsDocument;
//import writer2latex.util.*;
/**
* <p>This class extracts images from an OOo file.
* The images are returned as BinaryGraphicsDocument.</p>
@ -169,24 +164,24 @@ public final class ImageLoader {
// 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 (MIMETypes.isVectorFormat(sMIME) && sDefaultVectorFormat!=null &&
gcv.supportsConversion(sMIME,sDefaultVectorFormat,false,false)) {
sTargetMIME = sDefaultVectorFormat;
// Try vector format first
newBlob = gcv.convert(blob, sMIME, sTargetMIME=sDefaultVectorFormat);
}
else if (gcv.supportsConversion(sMIME,sDefaultFormat,false,false)) {
sTargetMIME = sDefaultFormat;
// Then try bitmap format
newBlob = gcv.convert(blob,sMIME,sTargetMIME=sDefaultFormat);
}
if (sTargetMIME!=null) {
byte[] newBlob = gcv.convert(blob,sMIME,sTargetMIME);
if (newBlob!=null) {
// Conversion succesful - create new data
blob = newBlob;
sMIME = sTargetMIME;
sExt = MIMETypes.getFileExtension(sMIME);
}
if (newBlob!=null) {
// Conversion successful - create new data
blob = newBlob;
sMIME = sTargetMIME;
sExt = MIMETypes.getFileExtension(sMIME);
}
}

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2011 by Henrik Just
* Copyright: 2002-2012 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.2 (2011-08-05)
* Version 1.4 (2012-04-01)
*
*/
@ -61,6 +61,7 @@ public final class MIMETypes extends writer2latex.api.MIMETypes {
public static final byte[] EPS_SIG = { 0x25, 0x21 }; // %!
public static final byte[] SVM_SIG = { 0x56, 0x43, 0x4c, 0x4d, 0x54, 0x46 }; // VCLMTF
public static final byte[] ZIP_SIG = { 0x50, 0x4b, 0x03, 0x04 }; // PK..
public static final byte[] SVG_SIG = { 0x3c, 0x73, 0x76, 0x67 }; // Not so magic: <svg
// Preferred file extensions for some files
@ -77,6 +78,7 @@ public final class MIMETypes extends writer2latex.api.MIMETypes {
public static final String EMF_EXT = ".emf";
public static final String WMF_EXT = ".wmf";
public static final String EPS_EXT = ".eps";
public static final String SVG_EXT = ".svg";
public static final String SVM_EXT = ".svm";
public static final String PDF_EXT = ".pdf";
@ -87,6 +89,24 @@ public final class MIMETypes extends writer2latex.api.MIMETypes {
}
return true;
}
private static final boolean isSVG(byte[] blob) {
// Look for <svg within the first 250 bytes
int m = Math.max(blob.length, 250);
int n = SVG_SIG.length;
for (int j=0; j<m; j++) {
boolean bFound = true;
for (int i=0; i<n; i++) {
if (blob[j+i]!=SVG_SIG[i]) {
bFound = false;
break;
}
}
if (bFound) return true;
}
return false;
}
public static final String getMagicMIMEType(byte[] blob) {
if (isType(blob,PNG_SIG)) { return PNG; }
@ -102,6 +122,7 @@ public final class MIMETypes extends writer2latex.api.MIMETypes {
if (isType(blob,EPS_SIG)) { return EPS; }
if (isType(blob,SVM_SIG)) { return SVM; }
if (isType(blob,ZIP_SIG)) { return ZIP; }
if (isSVG(blob)) { return SVG; }
return "";
}
@ -114,6 +135,7 @@ public final class MIMETypes extends writer2latex.api.MIMETypes {
if (EMF.equals(sMIME)) { return EMF_EXT; }
if (WMF.equals(sMIME)) { return WMF_EXT; }
if (EPS.equals(sMIME)) { return EPS_EXT; }
if (SVG.equals(sMIME)) { return SVG_EXT; }
if (SVM.equals(sMIME)) { return SVM_EXT; }
if (PDF.equals(sMIME)) { return PDF_EXT; }
if (LATEX.equals(sMIME)) { return LATEX_EXT; }
@ -125,7 +147,7 @@ public final class MIMETypes extends writer2latex.api.MIMETypes {
}
public static boolean isVectorFormat(String sMIME) {
return EMF.equals(sMIME) || WMF.equals(sMIME) || EPS.equals(sMIME) || SVM.equals(sMIME) || PDF.equals(sMIME);
return EMF.equals(sMIME) || WMF.equals(sMIME) || EPS.equals(sMIME) || SVG.equals(sMIME) || SVM.equals(sMIME) || PDF.equals(sMIME);
}

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.4 (2012-03-27)
* Version 1.4 (2012-04-01)
*
*/
@ -43,7 +43,7 @@ import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import writer2latex.util.SimpleSAXHandler;
import writer2latex.util.SimpleXMLParser;
import writer2latex.util.SimpleZipReader;
/**
@ -225,6 +225,7 @@ public class OfficeDocument {
* @param is Office document <code>InputStream</code>.
*
* @throws IOException If any I/O error occurs.
* @throws SAXException
*/
public void read(InputStream is) throws IOException {
// We need to read 4 bytes ahead to detect flat or zip format
@ -295,21 +296,11 @@ public class OfficeDocument {
private void readFlat(InputStream is) throws IOException {
SAXParserFactory factory=SAXParserFactory.newInstance();
SimpleSAXHandler handler = new SimpleSAXHandler();
try {
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(is,handler);
contentDoc = SimpleXMLParser.parse(is);
} catch (SAXException e) {
throw new IOException(e);
}
catch (SAXException e){
System.err.println("Oops - Error parsing document");
e.printStackTrace();
}
catch (ParserConfigurationException e) {
System.err.println("Oops - failed to get XML parser!?");
e.printStackTrace();
}
contentDoc = handler.getDOM();
styleDoc = null;
settingsDoc = null;
metaDoc = null;
@ -332,7 +323,7 @@ public class OfficeDocument {
*/
static Document parse(byte bytes[]) throws SAXException, IOException {
SAXParserFactory factory=SAXParserFactory.newInstance();
SimpleSAXHandler handler = new SimpleSAXHandler();
SimpleXMLParser handler = new SimpleXMLParser();
try {
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new ByteArrayInputStream(bytes),handler);