Skip to content

Commit

Permalink
Merge pull request #222 from metanorma/update
Browse files Browse the repository at this point in the history
Update for SVG fonts
  • Loading branch information
Intelligent2013 authored Jul 15, 2023
2 parents ecfe55a + 70c18c6 commit 28c6c4f
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SHELL ?= /bin/bash
endif

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

all: target/$(JAR_FILE)
Expand Down
6 changes: 3 additions & 3 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Update version in `pom.xml`, e.g.:
----
<groupId>org.metanorma.fop</groupId>
<artifactId>mn2pdf</artifactId>
<version>1.77</version>
<version>1.78</version>
<name>Metanorma XML to PDF converter</name>
----

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

[source,xml]
----
git tag v1.77
git push origin v1.77
git tag v1.78
git push origin v1.78
----

Then the corresponding GitHub release will be automatically created at:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.metanorma.fop</groupId>
<artifactId>mn2pdf</artifactId>
<version>1.77</version>
<version>1.78</version>
<name>Metanorma XML to PDF converter</name>
<packaging>jar</packaging>
<url>https://www.metanorma.org</url>
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/metanorma/fop/SourceXMLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,26 @@ public List<String> getDocumentFonts() {
} catch (Exception ex) {}
}

query = xPath.compile("//*/@style[contains(., 'font-family')]");
nList = (NodeList)query.evaluate(srcXML, XPathConstants.NODESET);
for (int i = 0; i < nList.getLength(); i++) {
try {
Attr attr = (Attr) nList.item(i);
String attrText = attr.getNodeValue();
attrText = attrText.substring(attrText.indexOf("font-family"))
.substring(attrText.indexOf(":") + 1);
if (attrText.indexOf(";") != -1) {
attrText = attrText.substring(0, attrText.indexOf(";"));
}
for (String fname: attrText.split(",")) {
fname = fname.trim();
if (!documentFontList.contains(fname)) {
documentFontList.add(fname);
}
}
} catch (Exception ex) {}
}

} catch (XPathExpressionException ex) {
logger.info(ex.toString());
}
Expand Down
60 changes: 52 additions & 8 deletions src/main/java/org/metanorma/fop/fontConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.apache.fop.fonts.FontTriplet;
import org.metanorma.fop.fonts.DefaultFonts;
import org.metanorma.fop.fonts.FOPFont;
import org.metanorma.fop.fonts.FOPFontAlternate;
Expand Down Expand Up @@ -33,11 +34,9 @@
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
Expand All @@ -56,6 +55,7 @@
import javax.xml.xpath.XPathFactory;
import org.apache.fop.fonts.autodetect.FontFileFinder;
import static org.metanorma.fop.PDFGenerator.logger;

import org.metanorma.utils.LoggerHelper;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Expand All @@ -72,6 +72,7 @@ class fontConfig {

static final String ENV_FONT_PATH = "MN_PDF_FONT_PATH";
static final String WARNING_FONT = "WARNING: Font file '%s' (font name '%s', font style '%s', font weight '%s') doesn't exist. Replaced by '%s'.";
static final String WARNING_FONT_NO_FILE = "WARNING: Font '%s' (font name '%s', font style '%s', font weight '%s') doesn't exist. Replaced by '%s'.";
private final String CONFIG_NAME = "pdf_fonts_config.xml";
private final String CONFIG_NAME_UPDATED = CONFIG_NAME + ".out";

Expand All @@ -92,6 +93,8 @@ class fontConfig {

private static List<String> registeredFonts = new ArrayList<>();

//private String mainFont = "";

private boolean isReady = false;

//private final List<String> defaultFontList = new DefaultFonts().getDefaultFonts();
Expand Down Expand Up @@ -438,15 +441,49 @@ public FOPFontTriplet getFontDefaultProperties(String font) {
public void setSourceDocumentFontList(List<String> sourceDocumentFontList) {

this.sourceDocumentFontList = sourceDocumentFontList;


/*if (!sourceDocumentFontList.isEmpty()) {
this.mainFont = sourceDocumentFontList.get(0);
}*/

isReady = false;

for(String sourceDocumentFont: sourceDocumentFontList) {
fopFonts.stream()
.filter(fopFont -> !fopFont.getFont_triplet().isEmpty())
.filter(fopFont -> fopFont.getName().equals(sourceDocumentFont))
.forEach(fopFont -> fopFont.setIsUsing(true));


// if font isn't exist in FOP fonts config, then add it
if (fopFonts.stream()
.filter(fopFont -> fopFont.getName().equals(sourceDocumentFont))
.count() == 0) {

String fontWeight = "normal";
String fontStyle = "normal";
if (sourceDocumentFont.contains("Bold")) {
fontWeight="bold";
}
if (sourceDocumentFont.contains("Italic")) {
fontStyle="italic";
}
List<FOPFontTriplet> fopFontTriplets = new ArrayList<>();
fopFontTriplets.add(new FOPFontTriplet(sourceDocumentFont, fontWeight, fontStyle));
if (fontWeight.equals("normal") && fontStyle.equals("normal")) {
// add 3 fonts: bold, italic and bold+italic
fopFontTriplets.add(new FOPFontTriplet(sourceDocumentFont, "bold", "normal"));
fopFontTriplets.add(new FOPFontTriplet(sourceDocumentFont, "normal", "italic"));
fopFontTriplets.add(new FOPFontTriplet(sourceDocumentFont, "bold", "italic"));
}
for (FOPFontTriplet fopFontTriplet: fopFontTriplets) {
FOPFont fopFont = new FOPFont();
fopFont.setEmbed_url("filenotfound_" + sourceDocumentFont + ".ttf");
fopFont.setIsUsing(true);
fopFont.setFont_triplet(Arrays.asList(fopFontTriplet));
fopFonts.add(fopFont);
}
}

/* for (FOPFont fopFont: fopFonts) {
if(!fopFont.getFont_triplet().isEmpty() &&
fopFont.getFont_triplet().get(0).getName().equals(sourceDocumentFont)) {
Expand Down Expand Up @@ -557,7 +594,10 @@ public List<FOPFont> getUsedFonts ()
private void setFontsPaths() throws IOException, URISyntaxException {

List<String> machineFontList = getMachineFonts();


// remove unused fonts
fopFonts.removeIf(fopFont -> !fopFont.isUsing());

fopFonts.stream()
.filter(fopFont -> !fopFont.getEmbed_url().isEmpty())
.filter(fopFont -> !fopFont.isReadyToUse())
Expand Down Expand Up @@ -633,7 +673,11 @@ private void setFontsPaths() throws IOException, URISyntaxException {

//printMessage(msg + " (font style '" + fontstyle + "', font weight '" + fontweight + "') doesn't exist. Replaced by '" + font_replacementpath + "'.");
//printMessage(String.format(WARNING_FONT, embed_url, fopFontTriplet.getStyle(), fopFontTriplet.getWeight(), font_replacementpath));
fopFont.setMessage(String.format(WARNING_FONT, embed_url, fopFontTriplet.getName(), fopFontTriplet.getStyle(), fopFontTriplet.getWeight(), font_replacementpath));
if (embed_url.contains("filenotfound_")) {
fopFont.setMessage(String.format(WARNING_FONT_NO_FILE, fopFontTriplet.getName(), fopFontTriplet.getName(), fopFontTriplet.getStyle(), fopFontTriplet.getWeight(), font_replacementpath));
} else {
fopFont.setMessage(String.format(WARNING_FONT, embed_url, fopFontTriplet.getName(), fopFontTriplet.getStyle(), fopFontTriplet.getWeight(), font_replacementpath));
}

/*try{
font_replacementpath = new File(font_replacementpath).toURI().toURL().toString();
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/org/metanorma/fop/mn2pdfTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ public void successFontReplacement() throws ParseException, IOException {
String capturedLog = getTestCapturedLog();
//assertTrue(systemOutRule.getLog().contains(
assertTrue(capturedLog.contains(
String.format(fontConfig.WARNING_FONT, embed_url, "TestFont", "normal", "normal", newPath)));
String.format(fontConfig.WARNING_FONT, embed_url, "TestFont", "normal", "normal", newPath)) ||
capturedLog.contains(
String.format(fontConfig.WARNING_FONT_NO_FILE, "TestFont", "TestFont", "normal", "normal", newPath)));
assertTrue(Files.exists(pdf));
}

Expand Down

0 comments on commit 28c6c4f

Please sign in to comment.