Support EPUB style resources (embed images and fonts used in custom CSS file)

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@108 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2011-06-07 09:01:15 +00:00
parent 2ad8f3b7a2
commit ef05312e21
14 changed files with 342 additions and 97 deletions

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2010 by Henrik Just
* Copyright: 2002-2011 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.2 (2010-04-12)
* Version 1.2 (2011-06-05)
*
*/
@ -33,7 +33,9 @@ import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;
import java.util.Vector;
import writer2latex.api.BatchConverter;
@ -59,6 +61,7 @@ import writer2latex.util.Misc;
* <li><code>-config[=]filename</code>
* <li><code>-template[=]filename</code>
* <li><code>-stylesheet[=]filename</code>
* <li><code>-resource[=]filename[::media type]</code>
* <li><code>-option[=]value</code>
* </ul>
* <p>where <code>option</code> can be any simple option known to Writer2LaTeX
@ -72,6 +75,7 @@ public final class Application {
private Vector<String> configFileNames = new Vector<String>();
private String sTemplateFileName = null;
private String sStyleSheetFileName = null;
private Set<String> resources = new HashSet<String>();
private Hashtable<String,String> options = new Hashtable<String,String>();
private String sSource = null;
private String sTarget = null;
@ -194,6 +198,30 @@ public final class Application {
System.out.println(" "+e.getMessage());
}
}
// Step 5c: Read style resources
for (String sResource : resources) {
String sMediaType;
String sFileName;
int nSeparator = sResource.indexOf("::");
if (nSeparator>-1) {
sFileName = sResource.substring(0,nSeparator);
sMediaType = sResource.substring(nSeparator+2);
}
else {
sFileName = sResource;
sMediaType = null;
}
System.out.println("Reading resource file "+sFileName);
try {
byte [] resourceBytes = Misc.inputStreamToByteArray(new FileInputStream(sFileName));
converter.readResource(new ByteArrayInputStream(resourceBytes),sFileName,sMediaType);
} catch (IOException e) {
System.out.println("--> Failed to read the resource file!");
System.out.println(" "+e.getMessage());
}
}
// Step 6: Read config
for (int i=0; i<configFileNames.size(); i++) {
@ -302,6 +330,7 @@ public final class Application {
System.out.println(" -recurse");
System.out.println(" -template[=]<template file>");
System.out.println(" -stylesheet[=]<style sheet file>");
System.out.println(" -resource[=]<resource file>[::<media type>]");
System.out.println(" -ultraclean");
System.out.println(" -clean");
System.out.println(" -pdfprint");
@ -353,6 +382,7 @@ public final class Application {
if ("-config".equals(sArg)) { configFileNames.add(sArg2); }
else if ("-template".equals(sArg)) { sTemplateFileName = sArg2; }
else if ("-stylesheet".equals(sArg)) { sStyleSheetFileName = sArg2; }
else if ("-resource".equals(sArg)) { resources.add(sArg2); }
else { // configuration option
options.put(sArg.substring(1),sArg2);
}

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2010 by Henrik Just
* Copyright: 2002-2011 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.2 (2010-12-21)
* Version 1.2 (2011-06-05)
*
*/
@ -96,7 +96,7 @@ public interface Converter {
*
* @param is an <code>InputStream</code> from which to read the resource
* @param sFileName the file name to use for the resource
* @param sMediaType the media type of the resource
* @param sMediaType the media type of the resource, if null the media type will be guessed from the file name
* @throws IOException if some exception occurs while reading the resource
*/
public void readResource(InputStream is, String sFileName, String sMediaType) throws IOException;
@ -107,7 +107,7 @@ public interface Converter {
*
* @param file a file from which to read the style sheet
* @param sFileName the file name to use for the resource
* @param sMediaType the media type of the resource
* @param sMediaType the media type of the resource, if null the media type will be guessed from the file name
* @throws IOException if the file does not exist or some exception occurs
* while reading the resource
*/

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.2 (2011-06-05)
* Version 1.2 (2011-06-07)
*
*/
@ -33,7 +33,7 @@ public class ConverterFactory {
// Version information
private static final String VERSION = "1.1.8";
private static final String DATE = "2011-06-05";
private static final String DATE = "2011-06-07";
/** Return the Writer2LaTeX version in the form
* (major version).(minor version).(patch level)<br/>

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.2 (2011-05-09)
* Version 1.2 (2011-06-07)
*
*/
@ -65,6 +65,7 @@ import writer2latex.util.Misc;
*
*/
public class Converter extends ConverterBase {
private static final String EPUB_STYLES_FOLDER = "styles/";
private static final String EPUB_STYLESHEET = "styles/styles1.css";
private static final String EPUB_CUSTOM_STYLESHEET = "styles/styles.css";
@ -142,7 +143,21 @@ public class Converter extends ConverterBase {
}
@Override public void readResource(InputStream is, String sFileName, String sMediaType) throws IOException {
ResourceDocument doc = new ResourceDocument(sFileName, sMediaType);
if (sMediaType==null) {
// Guess the media type from the file extension
sMediaType="";
String sfilename = sFileName.toLowerCase();
// PNG, JPEG and GIF are the basic raster image formats that must be supported by EPUB readers
if (sfilename.endsWith(MIMETypes.PNG_EXT)) { sMediaType = MIMETypes.PNG; }
else if (sfilename.endsWith(MIMETypes.JPEG_EXT)) { sMediaType = MIMETypes.JPEG; }
else if (sfilename.endsWith(".jpeg")) { sMediaType = MIMETypes.JPEG; }
else if (sfilename.endsWith(MIMETypes.GIF_EXT)) { sMediaType = MIMETypes.GIF; }
// The OPS 2.0.1 specification recommends (and only mentions) OpenType with this media type
else if (sfilename.endsWith(".otf")) { sMediaType = "application/vnd.ms-opentype"; }
// For convenience we also include a media type for true type fonts (the most common, it seems)
else if (sfilename.endsWith(".ttf")) { sMediaType = "application/x-font-ttf"; }
}
ResourceDocument doc = new ResourceDocument(EPUB_STYLES_FOLDER+sFileName, sMediaType);
doc.read(is);
resources.add(doc);
}