Skip to content

Commit

Permalink
Updated for doc font list
Browse files Browse the repository at this point in the history
  • Loading branch information
Intelligent2013 committed May 13, 2020
1 parent bcf9446 commit d9d1913
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 24 deletions.
63 changes: 61 additions & 2 deletions src/main/java/com/metanorma/fop/fontConfig.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.metanorma.fop;

import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URISyntaxException;
import java.net.URL;
Expand All @@ -26,10 +28,18 @@
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.fop.fonts.autodetect.FontFileFinder;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;


/**
*
Expand All @@ -42,8 +52,10 @@ class fontConfig {
private final String FONT_PREFIX = "Source";
private final String FONT_SUFFIX = "Pro";
private final Document configXML;
private final Document srcXML;
private File updatedConfig;
private String fontPath;
private ArrayList<String> messages;
private final ArrayList<String> defaultFontList = new ArrayList<String>() {
{
// Example
Expand All @@ -65,13 +77,20 @@ class fontConfig {
};


public fontConfig(String fontPath) throws SAXException, ParserConfigurationException, IOException, Exception {
public fontConfig(String xmlFO, String fontPath) throws SAXException, ParserConfigurationException, IOException, Exception {
messages = new ArrayList<>();
this.fontPath = fontPath;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
InputStream config = getStreamFromResources(CONFIG_NAME);

this.configXML = dBuilder.parse(config);

InputSource is = new InputSource(new StringReader(xmlFO));
this.srcXML = dBuilder.parse(is);



//extract all .ttf files from resources into fontPath folder
prepareFonts();
// replace missing font in fonts/substitutions sections
Expand Down Expand Up @@ -145,6 +164,8 @@ private void substFonts() throws IOException, URISyntaxException {

List<String> machineFontList = getMachineFonts();

//List<String> srcDocumentFontList = getDocumentFonts();

NodeList fonts = configXML.getElementsByTagName("font");

//iterate each font
Expand Down Expand Up @@ -209,7 +230,7 @@ private void substFonts() throws IOException, URISyntaxException {

font_replacementpath = Paths.get(fontPath, fontFamilySubst + ".ttf").toString();

System.out.println(msg + " (font style '" + fontstyle + "', font weight '" + fontweight + "') doesn't exist. " + "Font '" + font_replacementpath + "' will be used.");
printMessage(msg + " (font style '" + fontstyle + "', font weight '" + fontweight + "') doesn't exist. " + "Font '" + font_replacementpath + "' will be used.");

font_replacementpath = new File(font_replacementpath).toURI().toURL().toString();
}
Expand Down Expand Up @@ -247,6 +268,34 @@ private List<String> getMachineFonts() throws IOException{
return machineFontList;
}

private List<String> getDocumentFonts() {
List<String> documentFontList = new ArrayList<>();

XPath xPath = XPathFactory.newInstance().newXPath();

XPathExpression query;
try {
query = xPath.compile("//*/@font-family");
NodeList nList = (NodeList)query.evaluate(srcXML, XPathConstants.NODESET);

for (int i = 0; i < nList.getLength(); i++) {
try {
Attr attr = (Attr) nList.item(i);
for (String fname: attr.getNodeValue().split(",")) {
fname = fname.trim();
if (!documentFontList.contains(fname)) {
documentFontList.add(fname);
}
}
} catch (Exception ex) {}
}

} catch (XPathExpressionException ex) {
System.out.println(ex.toString());
}

return documentFontList;
}

private void writeXmlDocumentToXmlFile(Document xmlDocument) throws IOException
{
Expand Down Expand Up @@ -342,4 +391,14 @@ private String getFontsURL(String property) throws Exception {
appProps.load(getStreamFromResources("app.properties"));
return appProps.getProperty(property);
}

private void printMessage(String msg) {
System.out.println(msg);
messages.add(msg);
}

public ArrayList<String> getMessages() {
return messages;
}

}
52 changes: 30 additions & 22 deletions src/main/java/com/metanorma/fop/mn2pdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -166,39 +167,44 @@ public void convertmn2pdf(String fontPath, File xml, File xsl, File pdf) throws
Source src = new StreamSource(xml);

//DEBUG: write intermediate FO to file
if (DEBUG) {

OutputJaxpImplementationInfo();

// Step 0. Convert XML to FO file with XSL


OutputJaxpImplementationInfo();

// Step 0. Convert XML to FO file with XSL
StringWriter resultWriter = new StringWriter();
StreamResult sr = new StreamResult(resultWriter);
//Start XSLT transformation and FO generating
transformer.transform(src, sr);
String xmlFO = resultWriter.toString();
if (DEBUG) {
BufferedWriter writer = new BufferedWriter(new FileWriter(pdf.getAbsolutePath() + ".fo.xml"));
writer.write(xmlFO);
writer.close();
//Setup output
OutputStream outstream = new java.io.FileOutputStream(pdf.getAbsolutePath() + ".fo.xml");
//OutputStream outstream = new java.io.FileOutputStream(pdf.getAbsolutePath() + ".fo.xml");
//Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new StreamResult(outstream);

//Result res = new StreamResult(outstream);
//Start XSLT transformation and FO generating
transformer.transform(src, res);
//transformer.transform(src, res);

// using resultWriter
//StringWriter resultWriter = new StringWriter();
//StreamResult sr = new StreamResult(resultWriter);
//transformer.transform(src, sr);
//String xmlFO = resultWriter.toString();
//BufferedWriter writer = new BufferedWriter(new FileWriter("fo.xml"));
//writer.write(xmlFO);
//writer.close();
}

fontConfig fontcfg = new fontConfig(fontPath);

fontConfig fontcfg = new fontConfig(xmlFO, fontPath);
runFOP(fontcfg, src, pdf, transformer);

if(PDFUA_error) {
System.out.println("INFO: Trying to generate PDF in non PDF/UA-1 mode.");
fontcfg.setPDFUAmode("DISABLED");
runFOP(fontcfg, src, pdf, transformer);
}
for(String msg: fontcfg.getMessages()) {
// System.out.println(msg);
}
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(ERROR_EXIT_CODE);
Expand All @@ -220,6 +226,7 @@ public void convertmn2pdf(String fontPath, File xml, File xsl, File pdf) throws
private void runFOP (fontConfig fontcfg, Source src, File pdf, Transformer transformer) throws IOException, FOPException, SAXException, TransformerException, TransformerConfigurationException, TransformerConfigurationException {
OutputStream out = null;
try {
System.out.println("Transforming...");
// Step 1: Construct a FopFactory by specifying a reference to the configuration file
FopFactory fopFactory = FopFactory.newInstance(fontcfg.getUpdatedConfig());

Expand Down Expand Up @@ -341,8 +348,7 @@ public static void main(String[] args) throws ParseException {
System.out.println(String.format(INPUT_LOG, XSL_INPUT, fXSL));
System.out.println("Output: PDF (" + fPDF + ")");
System.out.println();
System.out.println("Transforming...");


try {
mn2pdf app = new mn2pdf();
app.convertmn2pdf(argFontsPath, fXML, fXSL, fPDF);
Expand All @@ -369,10 +375,12 @@ private static String getUsage() {
}

private static void OutputJaxpImplementationInfo() {
System.out.println(getJaxpImplementationInfo("DocumentBuilderFactory", DocumentBuilderFactory.newInstance().getClass()));
System.out.println(getJaxpImplementationInfo("XPathFactory", XPathFactory.newInstance().getClass()));
System.out.println(getJaxpImplementationInfo("TransformerFactory", TransformerFactory.newInstance().getClass()));
System.out.println(getJaxpImplementationInfo("SAXParserFactory", SAXParserFactory.newInstance().getClass()));
if (DEBUG) {
System.out.println(getJaxpImplementationInfo("DocumentBuilderFactory", DocumentBuilderFactory.newInstance().getClass()));
System.out.println(getJaxpImplementationInfo("XPathFactory", XPathFactory.newInstance().getClass()));
System.out.println(getJaxpImplementationInfo("TransformerFactory", TransformerFactory.newInstance().getClass()));
System.out.println(getJaxpImplementationInfo("SAXParserFactory", SAXParserFactory.newInstance().getClass()));
}
}

private static String getJaxpImplementationInfo(String componentName, Class componentClass) {
Expand Down

0 comments on commit d9d1913

Please sign in to comment.