Skip to content

Commit

Permalink
Merge pull request #39 from metanorma/beta
Browse files Browse the repository at this point in the history
Version 1.17
  • Loading branch information
Intelligent2013 authored Jun 1, 2020
2 parents c81fe55 + a75531a commit e4fcf9a
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 48 deletions.
14 changes: 6 additions & 8 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ jobs:
# Install cabextract
brew install cabextract
- name: Install Microsoft Vista fonts
run: |
export MN_PDF_FONT_PATH=${GITHUB_WORKSPACE}/fonts
export MS_FONT_PATH=${MN_PDF_FONT_PATH}/truetype/vista
export RENAME_FONTS=false
curl -Ls https://raw.githubusercontent.com/metanorma/vista-fonts-installer/master/vista-fonts-installer.sh | bash
# - name: Install Microsoft Vista fonts
# run: |
# export MN_PDF_FONT_PATH=${GITHUB_WORKSPACE}/fonts
# export MS_FONT_PATH=${MN_PDF_FONT_PATH}/truetype/vista
# export RENAME_FONTS=false
# curl -Ls https://raw.githubusercontent.com/metanorma/vista-fonts-installer/master/vista-fonts-installer.sh | bash

- name: Build
env:
Expand All @@ -45,6 +45,4 @@ jobs:
GH_TOKEN: ${{ github.token }}
GH_USERNAME: ${{ github.actor }}
run: |
export MN_PDF_FONT_PATH=${GITHUB_WORKSPACE}/fonts
ls -alR ${MN_PDF_FONT_PATH}
make test
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ jobs:
echo ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true | sudo debconf-set-selections
sudo apt-get install ttf-mscorefonts-installer
- name: Setup Cambria fonts
run: |
curl -Ls https://raw.githubusercontent.com/metanorma/vista-fonts-installer/master/vista-fonts-installer.sh | sudo bash
# - name: Setup Cambria fonts
# run: |
# curl -Ls https://raw.githubusercontent.com/metanorma/vista-fonts-installer/master/vista-fonts-installer.sh | sudo bash

- uses: actions/checkout@master

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ jobs:
echo ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true | sudo debconf-set-selections
sudo apt-get install ttf-mscorefonts-installer
- name: Setup Cambria fonts
run: |
curl -Ls https://raw.githubusercontent.com/metanorma/vista-fonts-installer/master/vista-fonts-installer.sh | sudo bash
# - name: Setup Cambria fonts
# run: |
# curl -Ls https://raw.githubusercontent.com/metanorma/vista-fonts-installer/master/vista-fonts-installer.sh | sudo bash

- name: Build
env:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
SHELL ?= /bin/bash

#JAR_VERSION := $(shell mvn -q -Dexec.executable="echo" -Dexec.args='$${project.version}' --non-recursive exec:exec -DforceStdout)
JAR_VERSION := 1.16
JAR_VERSION := 1.17
JAR_FILE := mn2pdf-$(JAR_VERSION).jar

all: target/$(JAR_FILE)
Expand Down
10 changes: 5 additions & 5 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ You will need the `maven` build tool and `make`.

[source,sh]
----
java -Xss5m -Xmx1024m -jar target/mn2pdf-1.16.jar --xml-file <XML-FileName> --xsl-file <XSLT-FileName> --pdf-file <Output-PDF-FileName>
java -Xss5m -Xmx1024m -jar target/mn2pdf-1.17.jar --xml-file <XML-FileName> --xsl-file <XSLT-FileName> --pdf-file <Output-PDF-FileName>
----

e.g.

[source,sh]
----
java -Xss5m -Xmx1024m -jar target/mn2pdf-1.16.jar --xml-file tests/G.191.xml --xsl-file tests/itu.recommendation.xsl --pdf-file tests/G.191.pdf
java -Xss5m -Xmx1024m -jar target/mn2pdf-1.17.jar --xml-file tests/G.191.xml --xsl-file tests/itu.recommendation.xsl --pdf-file tests/G.191.pdf
----


Expand All @@ -42,7 +42,7 @@ Update version in `pom.xml`, e.g.:
----
<groupId>com.metanorma.fop</groupId>
<artifactId>mn2pdf</artifactId>
<version>1.16</version>
<version>1.17</version>
<name>Metanorma XML to PDF converter</name>
----

Expand All @@ -53,8 +53,8 @@ Tag the same version in Git:

[source,xml]
----
git tag v1.12
git push origin v1.12
git tag v1.17
git push origin v1.17
----

Then the corresponding GitHub release will be automatically created at:
Expand Down
135 changes: 107 additions & 28 deletions src/main/java/com/metanorma/fop/mn2pdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -25,6 +26,8 @@
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.ErrorListener;
//import javax.xml.transform.ErrorListener;
import javax.xml.transform.OutputKeys;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
Expand All @@ -43,6 +46,9 @@
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import org.apache.fop.events.Event;
import org.apache.fop.events.model.EventSeverity;
import org.apache.fop.events.EventFormatter;

import net.sourceforge.jeuclid.fop.plugin.JEuclidFopFactoryConfigurator;

Expand All @@ -53,6 +59,7 @@
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
Expand Down Expand Up @@ -151,17 +158,17 @@ public class mn2pdf {
* @throws FOPException, SAXException In case of a FOP problem
*/
public void convertmn2pdf(String fontPath, File xml, File xsl, File pdf) throws IOException, FOPException, SAXException, TransformerException, TransformerConfigurationException, TransformerConfigurationException, ParserConfigurationException {



String imagesxml = getImageFilePath(xml);

try {
//Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xsl));

transformer.setParameter("svg_images", imagesxml);
TransformerFactory factoryFO = TransformerFactory.newInstance();
Transformer transformerFO = factoryFO.newTransformer(new StreamSource(xsl));

transformerFO.setOutputProperty(OutputKeys.ENCODING, "UTF-16"); // to fix issue with UTF-16 surrogate pairs

transformerFO.setParameter("svg_images", imagesxml);

//Setup input for XSLT transformation
Source src = new StreamSource(xml);
Expand All @@ -172,38 +179,40 @@ public void convertmn2pdf(String fontPath, File xml, File xsl, File pdf) throws
StringWriter resultWriter = new StringWriter();
StreamResult sr = new StreamResult(resultWriter);
//Start XSLT transformation and FO generating
transformer.transform(src, sr);
transformerFO.transform(src, sr);
String xmlFO = resultWriter.toString();
if (DEBUG) {
//DEBUG: write intermediate FO to file
//BufferedWriter writer = new BufferedWriter(new FileWriter(pdf.getAbsolutePath() + ".fo.xml"));
BufferedWriter writer = Files.newBufferedWriter(Paths.get(pdf.getAbsolutePath() + ".fo.xml"));

writer.write(xmlFO);
writer.close();
//DEBUG: write intermediate FO to file
try (
BufferedWriter writer = Files.newBufferedWriter(Paths.get(pdf.getAbsolutePath() + ".fo.xml"))) {
writer.write(xmlFO);
}
//Setup output
//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);
//Start XSLT transformation and FO generating
//transformer.transform(src, res);

// using resultWriter
//StringWriter resultWriter = new StringWriter();
//StreamResult sr = new StreamResult(resultWriter);
//transformer.transform(src, sr);
}

fontConfig fontcfg = new fontConfig(xmlFO, fontPath);

// FO processing by FOP
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(); // identity transformer
src = new StreamSource(new StringReader(xmlFO));

runFOP(fontcfg, src, pdf, transformer);


if(PDFUA_error) {
System.out.println("WARNING: Trying to generate PDF in non PDF/UA-1 mode.");
fontcfg.setPDFUAmode("DISABLED");
src = new StreamSource(new StringReader(xmlFO));
runFOP(fontcfg, src, pdf, transformer);
System.out.println("WARNING: PDF generated in non PDF/UA-1 mode.");
}

for(String msg: fontcfg.getMessages()) {
System.out.println(msg);
}
Expand All @@ -224,7 +233,6 @@ 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 {
Expand All @@ -235,6 +243,11 @@ private void runFOP (fontConfig fontcfg, Source src, File pdf, Transformer trans
JEuclidFopFactoryConfigurator.configure(fopFactory);
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
// configure foUserAgent

//Adding a simple logging listener that writes to stdout and stderr
//foUserAgent.getEventBroadcaster().addEventListener(new SysOutEventListener());
// Add your own event listener
//foUserAgent.getEventBroadcaster().addEventListener(new MyEventListener());

// Setup output stream. Note: Using BufferedOutputStream
// for performance reasons (helpful with FileOutputStreams).
Expand All @@ -248,27 +261,34 @@ private void runFOP (fontConfig fontcfg, Source src, File pdf, Transformer trans
//factory = TransformerFactory.newInstance();
//transformer = factory.newTransformer(); // identity transformer

// Setup input stream
//Source srcFO = new StreamSource(new StringReader(xmlFO));


// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());

transformer.setErrorListener(new DefaultErrorListener());

// Start XSLT transformation and FOP processing
transformer.transform(src, res);
// Setup input stream
transformer.transform(src, res);

} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(ERROR_EXIT_CODE);
String excstr=e.toString();
if (excstr.contains("PDFConformanceException") && excstr.contains("PDF/UA-1") && !PDFUA_error) { // excstr.contains("all fonts, even the base 14 fonts, have to be embedded")
System.err.println(e.toString());
PDFUA_error = true;
} else {
e.printStackTrace(System.err);
System.exit(ERROR_EXIT_CODE);
}

} finally {
if (out != null) {
out.close();
}
}
}

private class DefaultErrorListener implements ErrorListener {
private class DefaultErrorListener implements javax.xml.transform.ErrorListener {

public void warning(TransformerException exc) {
System.err.println(exc.toString());
Expand All @@ -291,6 +311,65 @@ public void fatalError(TransformerException exc)
}
}

private static class MyEventListener implements org.apache.fop.events.EventListener {

public void processEvent(Event event) {
if ("org.apache.fop.layoutmgr.BlockLevelEventProducer.overconstrainedAdjustEndIndent".
equals(event.getEventID())) {
//skip
} else
if("org.apache.fop.render.RendererEventProducer.endPage".
equals(event.getEventID())) {
//skip
}else
if ("org.apache.fop.pdf.PDFConformanceException".
equals(event.getEventID())) {
System.err.println(new RuntimeException(EventFormatter.format(event)).toString());
PDFUA_error = true;
}
else
if ("org.apache.fop.ResourceEventProducer.imageNotFound"
.equals(event.getEventID())) {

//Get the FileNotFoundException that's part of the event's parameters
//FileNotFoundException fnfe = (FileNotFoundException)event.getParam("fnfe");

System.out.println("---=== imageNotFound Event for " + event.getParam("uri")
+ "!!! ===---");
//Stop processing when an image could not be found. Otherwise, FOP would just
//continue without the image!

System.out.println("Throwing a RuntimeException...");
//throw new RuntimeException(EventFormatter.format(event), fnfe);
} else {
//ignore all other events
}
}

}

/** A simple event listener that writes the events to stdout and sterr. */
private static class SysOutEventListener implements org.apache.fop.events.EventListener {

/** {@inheritDoc} */
public void processEvent(Event event) {
String msg = EventFormatter.format(event);
EventSeverity severity = event.getSeverity();
if (severity == EventSeverity.INFO) {
System.out.println("[INFO ] " + msg);
} else if (severity == EventSeverity.WARN) {
System.out.println("[WARN ] " + msg);
} else if (severity == EventSeverity.ERROR) {
System.err.println("[ERROR] " + msg);
} else if (severity == EventSeverity.FATAL) {
System.err.println("[FATAL] " + msg);
} else {
assert false;
}
}
}



/**
* Main method.
Expand Down
47 changes: 47 additions & 0 deletions src/main/resources/pdf_fonts_config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,53 @@ Relative config URLs are resolved relative to location of this file.
<font-triplet name="SimHei" style="italic" weight="bold"/>
</font>

<!-- Helvetica Neue -->
<font kerning="yes" embed-url="HelveticaNeue-Roman.ttf">
<alternate embed-url="arial.ttf"/>
<font-triplet name="Helvetica Neue" style="normal" weight="normal"/>
</font>
<font kerning="yes" embed-url="HelveticaNeue-Bold.ttf">
<alternate embed-url="arialbd.ttf"/>
<font-triplet name="Helvetica Neue" style="normal" weight="bold"/>
</font>
<font kerning="yes" embed-url="HelveticaNeue-Italic.ttf">
<alternate embed-url="ariali.ttf"/>
<font-triplet name="Helvetica Neue" style="italic" weight="normal"/>
</font>
<font kerning="yes" embed-url="HelveticaNeue-BoldItalic.ttf">
<alternate embed-url="arialbi.ttf"/>
<font-triplet name="Helvetica Neue" style="italic" weight="bold"/>
</font>

<!-- SFMono-Regular -->
<font kerning="yes" embed-url="SFMono.ttf">
<alternate embed-url="consola.ttf"/>
<alternate embed-url="cour.ttf"/>
<alternate embed-url="Courier New.ttf"/>
<alternate embed-url="SourceCodePro-Regular.ttf"/>
<font-triplet name="SFMono-Regular" style="normal" weight="normal"/>
</font>
<font kerning="yes" embed-url="SFMono.ttf" simulate-style="true">
<alternate embed-url="consolab.ttf"/>
<alternate embed-url="courbd.ttf"/>
<alternate embed-url="Courier New Bold.ttf"/>
<alternate embed-url="SourceCodePro-Bold.ttf"/>
<font-triplet name="SFMono-Regular" style="normal" weight="bold"/>
</font>
<font kerning="yes" embed-url="SFMono.ttf" simulate-style="true">
<alternate embed-url="consolai.ttf"/>
<alternate embed-url="couri.ttf"/>
<alternate embed-url="Courier New Italic.ttf"/>
<alternate embed-url="SourceCodePro-It.ttf"/>
<font-triplet name="SFMono-Regular" style="italic" weight="normal"/>
</font>
<font kerning="yes" embed-url="SFMono.ttf" simulate-style="true">
<alternate embed-url="consolaz.ttf"/>
<alternate embed-url="courbi.ttf"/>
<alternate embed-url="Courier New Bold Italic.ttf"/>
<alternate embed-url="SourceCodePro-BoldIt.ttf"/>
<font-triplet name="SFMono-Regular" style="italic" weight="bold"/>
</font>

</fonts>

Expand Down

0 comments on commit e4fcf9a

Please sign in to comment.