Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding advanced typography capability #297 #329

Merged
merged 19 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion openpdf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,16 @@
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>

<dependency>
codecracker2014 marked this conversation as resolved.
Show resolved Hide resolved
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>${fop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>xmlgraphics-commons</artifactId>
<version>${fop.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
56 changes: 36 additions & 20 deletions openpdf/src/main/java/com/lowagie/text/pdf/FontDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,29 +211,18 @@ byte[] convertToBytes(String text) {
longTag.put(metrics[0], new int[]{metrics[0], metrics[1], ttu.getUnicodeDifferences(b[k] & 0xff)});
glyph[i++] = (char)metrics[0];
}
String s = new String(glyph, 0, i);
b = s.getBytes(CJKFont.CJK_ENCODING);

}
else {
for (int k = 0; k < len; ++k) {
int val;
if (Utilities.isSurrogatePair(text, k)) {
val = Utilities.convertToUtf32(text, k);
k++;
}
else {
val = text.charAt(k);
}
metrics = ttu.getMetricsTT(val);
if (metrics == null)
continue;
int m0 = metrics[0];
Integer gl = m0;
if (!longTag.containsKey(gl))
longTag.put(gl, new int[]{m0, metrics[1], val});
glyph[i++] = (char)m0;
String fileName = ((TrueTypeFontUnicode)getBaseFont()).fileName;
if (fileName!=null && fileName.length()>0 &&( fileName.contains(".ttf") || fileName.contains(".TTF"))){
return FopGlyphProcessor.convertToBytesWithGlyphs(ttu,text,fileName,longTag);
}else {
return convertToBytesWithGlyphs(text);
}
}
String s = new String(glyph, 0, i);
b = s.getBytes(CJKFont.CJK_ENCODING);
}
catch (UnsupportedEncodingException e) {
throw new ExceptionConverter(e);
Expand All @@ -243,7 +232,34 @@ byte[] convertToBytes(String text) {
}
return b;
}


private byte[] convertToBytesWithGlyphs(String text) throws UnsupportedEncodingException {
int len = text.length();
int[] metrics = null;
char[] glyph = new char[len];
int i = 0;
for (int k = 0; k < len; ++k) {
int val;
if (Utilities.isSurrogatePair(text, k)) {
val = Utilities.convertToUtf32(text, k);
k++;
}
else {
val = text.charAt(k);
}
metrics = ttu.getMetricsTT(val);
if (metrics == null)
continue;
int m0 = metrics[0];
Integer gl = m0;
if (!longTag.containsKey(gl))
longTag.put(gl, new int[]{m0, metrics[1], val});
glyph[i++] = (char)m0;
}
String s = new String(glyph, 0, i);
return s.getBytes(CJKFont.CJK_ENCODING);
}

byte[] convertToBytes(GlyphVector glyphVector) {
if (fontType != BaseFont.FONT_TYPE_TTUNI || symbolic) {
throw new UnsupportedOperationException("Only supported for True Type Unicode fonts");
Expand Down
53 changes: 53 additions & 0 deletions openpdf/src/main/java/com/lowagie/text/pdf/FopGlyphProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.lowagie.text.pdf;

import java.io.UnsupportedEncodingException;
import java.nio.IntBuffer;
import java.util.Map;

import org.apache.fop.complexscripts.fonts.GlyphSubstitutionTable;
import org.apache.fop.complexscripts.util.CharScript;
import org.apache.fop.complexscripts.util.GlyphSequence;
import org.apache.fop.fonts.truetype.TTFFile;

/**
* Utilizing Fop advanced typography capabilities for TrueType fonts.
* @author Gajendra kumar ([email protected])
*/
public class FopGlyphProcessor {

public static byte[] convertToBytesWithGlyphs(TrueTypeFontUnicode ttu, String text, String fileName,
Map<Integer, int[]> longTag) throws UnsupportedEncodingException {
IntBuffer charBuffer = IntBuffer.allocate(text.length());
IntBuffer ghyphBuffer = IntBuffer.allocate(text.length());
for (char c : text.toCharArray()) {
charBuffer.put(c);
int[] metrics = ttu.getMetricsTT(c);
ghyphBuffer.put(metrics[0]);
}

GlyphSequence glyphSequence = new GlyphSequence(charBuffer, ghyphBuffer, null);
TTFFile ttf = TTFCache.getTTFFile(fileName);
GlyphSubstitutionTable gsubTable = ttf.getGSUB();
if (gsubTable != null) {
String script = CharScript.scriptTagFromCode(CharScript.dominantScript(text));
String language = "dflt";
if ("zyyy".equals(script) || "auto".equals(script)) {
script = "*";
}
glyphSequence = gsubTable.substitute(glyphSequence, script, language);
}
int[] processedChars = glyphSequence.getGlyphs().array();
char[] charEncodedGlyphCodes = new char[processedChars.length];

for (int i = 0; i < processedChars.length; i++) {
charEncodedGlyphCodes[i] = (char) processedChars[i];
Integer glyphCode = processedChars[i];
if (!longTag.containsKey(glyphCode)) {
longTag.put(glyphCode,
new int[] { processedChars[i], ttu.getGlyphWidth(processedChars[i]), processedChars[i] });
}
}
return new String(charEncodedGlyphCodes).getBytes(CJKFont.CJK_ENCODING);
}

}
65 changes: 65 additions & 0 deletions openpdf/src/main/java/com/lowagie/text/pdf/TTFCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.lowagie.text.pdf;

import org.apache.fop.fonts.apps.TTFReader;
import org.apache.fop.fonts.truetype.FontFileReader;
import org.apache.fop.fonts.truetype.OFFontLoader;
import org.apache.fop.fonts.truetype.TTFFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.lowagie.text.ExceptionConverter;

/**
*
*
* @author Gajendra kumar ([email protected])
*/
public class TTFCache {

private static Map<String,TTFFile> ttfFileMap =new ConcurrentHashMap<>();

public static TTFFile getTTFFile(String fileName) {

if (ttfFileMap.containsKey(fileName)){
return ttfFileMap.get(fileName);
}
TTFReader app = new TTFReader();
TTFFile ttf =null;
try {
ttf = loadTTF(app,fileName);
ttfFileMap.put(fileName,ttf);
return ttf;
} catch (IOException e) {
throw new ExceptionConverter(e);
}
}

private static TTFFile loadTTF(TTFReader app, String fileName) throws IOException{

try {
return app.loadTTF(fileName, null, true, true);
} catch (IOException e) {
TTFFile ttfFile = new TTFFile(true, true);
InputStream stream = BaseFont.getResourceStream(fileName, null);
try {
FontFileReader reader = new FontFileReader(stream);
String header = OFFontLoader.readHeader(reader);
String fontName = null;
boolean supported = ttfFile.readFont(reader, header, fontName);
if (!supported) {
return null;
}
} finally {
stream.close();
}
if (ttfFile.isCFF()) {
throw new UnsupportedOperationException(
"OpenType fonts with CFF data are not supported, yet");
}
return ttfFile;
}
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<maven.surefire.plugin.version>2.22.1</maven.surefire.plugin.version>
<maven.test.failure.ignore>false</maven.test.failure.ignore>
<mockito.version>2.25.1</mockito.version>
<fop.version>2.3</fop.version>
</properties>


Expand Down