Skip to content

Commit

Permalink
issue #49 fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
Intelligent2013 committed Sep 27, 2020
1 parent 40d85d2 commit 88de48f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/metanorma/fop/Util.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.metanorma.fop;

import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -136,4 +137,14 @@ public static String getDecodedBase64SVGnode(String encodedString) { //throws SA
return decodedString;
}*/
}

public static void showAvailableAWTFonts() {
final String[] fam = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
System.out.println("====================");
System.out.println("Available fonts:");
for (final String element : fam) {
System.out.println(element);
}
System.out.println("====================");
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/metanorma/fop/fontConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.metanorma.fop;

import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
Expand All @@ -22,6 +25,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
Expand Down Expand Up @@ -98,6 +102,9 @@ public fontConfig(String xmlFO, String fontPath) throws SAXException, ParserConf
substFonts();
//write updated FOP config file
writeXmlDocumentToXmlFile(configXML);

//add fonts from fontPath to system available fonts
updateFonts();
}

//extract all .ttf files from resources into fontPath folder
Expand Down Expand Up @@ -434,6 +441,36 @@ private String getFontsURL(String property) throws Exception {
return appProps.getProperty(property);
}

private void updateFonts(){
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

try (Stream<Path> walk = Files.walk(Paths.get(this.fontPath))) {
List<String> fontfiles = walk.map(x -> x.toString())
.filter(f -> f.endsWith(".ttf") || f.endsWith(".TTF") || f.endsWith(".ttc") || f.endsWith(".TTC") || f.endsWith(".otf") || f.endsWith(".OTF")).collect(Collectors.toList());

for(String fontfile : fontfiles) {
try {
Font ttfFont = Font.createFont(Font.TRUETYPE_FONT, new File(fontfile));
//register the font
ge.registerFont(ttfFont);
} catch(FontFormatException e) {
try {
Font type1Font = Font.createFont(Font.TYPE1_FONT, new File(fontfile));
//register the font
ge.registerFont(type1Font);
} catch(FontFormatException e1) {
e1.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}

}

private void printMessage(String msg) {
System.out.println(msg);
messages.add(msg);
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/com/metanorma/fop/mn2pdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ public void convertmn2pdf(String fontPath, File xml, File xsl, Properties xslpar
}

fontConfig fontcfg = new fontConfig(xmlFO, fontPath);
if (DEBUG) {
Util.showAvailableAWTFonts();
}

// FO processing by FOP
TransformerFactory factory = TransformerFactory.newInstance();
Expand Down Expand Up @@ -679,11 +682,5 @@ private static int getCoverPagesCount (File fXSL) {
public int getPageCount() {
return pageCount;
}

private Optional<String> getExtensionByStringHandling(String filename) {
return Optional.ofNullable(filename)
.filter(f -> f.contains("."))
.map(f -> f.substring(filename.lastIndexOf(".") + 1));
}


}

0 comments on commit 88de48f

Please sign in to comment.