Skip to content

Commit

Permalink
Merge pull request LibrePDF#58 from albfernandez/tibco_changes
Browse files Browse the repository at this point in the history
Modifications made by TIBCO Software for JasperReports >= 6.2
  • Loading branch information
tlxtellef authored Dec 5, 2017
2 parents 32aef70 + 2d34473 commit 2fe8cfe
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 9 deletions.
37 changes: 37 additions & 0 deletions openpdf/src/main/java/com/lowagie/text/pdf/FontDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

package com.lowagie.text.pdf;

import java.awt.font.GlyphVector;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

Expand Down Expand Up @@ -244,6 +245,42 @@ byte[] convertToBytes(String text) {
return b;
}

byte[] convertToBytes(GlyphVector glyphVector) {
if (fontType != BaseFont.FONT_TYPE_TTUNI || symbolic) {
throw new UnsupportedOperationException("Only supported for True Type Unicode fonts");
}

char[] glyphs = new char[glyphVector.getNumGlyphs()];
int glyphCount = 0;
for (int i = 0; i < glyphs.length; i++) {
int code = glyphVector.getGlyphCode(i);
if (code == 0xFFFE || code == 0xFFFF) {// considered non-glyphs by
// AWT
continue;
}

glyphs[glyphCount++] = (char) code;// FIXME supplementary plane?

Integer codeKey = Integer.valueOf(code);
if (!longTag.containsKey(codeKey)) {
int glyphWidth = ttu.getGlyphWidth(code);
Integer charCode = ttu.getCharacterCode(code);
int[] metrics = charCode != null ? new int[] { code, glyphWidth, charCode.intValue() } : new int[] {
code, glyphWidth };
longTag.put(codeKey, metrics);
}
}

String s = new String(glyphs, 0, glyphCount);
try {
byte[] b = s.getBytes(CJKFont.CJK_ENCODING);
return b;
} catch (UnsupportedEncodingException e) {
throw new ExceptionConverter(e);
}
}


/**
* Writes the font definition to the document.
* @param writer the <CODE>PdfWriter</CODE> of this document
Expand Down
25 changes: 23 additions & 2 deletions openpdf/src/main/java/com/lowagie/text/pdf/PdfContentByte.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@
package com.lowagie.text.pdf;

import java.awt.Color;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.print.PrinterJob;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -1435,7 +1437,13 @@ public void showText(String text) {
showText2(text);
content.append("Tj").append_i(separator);
}


public void showText(GlyphVector glyphVector) {
byte[] b = state.fontDetails.convertToBytes(glyphVector);
escapeString(b, content);
content.append("Tj").append_i(separator);
}

/**
* Constructs a kern array for a text in a certain font
* @param text the text
Expand Down Expand Up @@ -3015,6 +3023,11 @@ public void setDefaultColorspace(PdfName name, PdfObject obj) {
* @param struc the tagging structure
*/
public void beginMarkedContentSequence(PdfStructureElement struc) {
PdfDictionary dict = new PdfDictionary();
beginMarkedContentSequence(struc, dict);
}

public void beginMarkedContentSequence(PdfStructureElement struc, PdfDictionary dict) {
PdfObject obj = struc.get(PdfName.K);
int mark = pdf.getMarkPoint();
if (obj != null) {
Expand Down Expand Up @@ -3043,7 +3056,15 @@ else if (obj.isArray()) {
}
pdf.incMarkPoint();
mcDepth++;
content.append(struc.get(PdfName.S).getBytes()).append(" <</MCID ").append(mark).append(">> BDC").append_i(separator);
dict.put(PdfName.MCID, new PdfNumber(mark));
content.append(struc.get(PdfName.S).getBytes()).append(" ");
try {
dict.toPdf(writer, content);
}
catch (IOException e) {
throw new ExceptionConverter(e);
}
content.append(" BDC").append_i(separator);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion openpdf/src/main/java/com/lowagie/text/pdf/PdfDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,10 @@ else if (isJustified) {
hangingCorrection = width - oldWidth;
}
}
float baseFactor = width / (ratio * numberOfSpaces + lineLen - 1);
// if there's a single word on the line and we are using NO_SPACE_CHAR_RATIO,
// we don't want any character spacing
float baseFactor = (numberOfSpaces == 0 && ratio == PdfWriter.NO_SPACE_CHAR_RATIO)
? 0f : width / (ratio * numberOfSpaces + lineLen - 1);
baseWordSpacing = ratio * baseFactor;
baseCharacterSpacing = baseFactor;
lastBaseFactor = baseFactor;
Expand Down
22 changes: 18 additions & 4 deletions openpdf/src/main/java/com/lowagie/text/pdf/PdfGraphics2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,7 @@ public Graphics create() {
g2.paint = this.paint;
g2.fillGState = this.fillGState;
g2.currentFillGState = this.currentFillGState;
g2.currentStrokeGState = this.currentStrokeGState;
g2.strokeGState = this.strokeGState;
g2.background = this.background;
g2.mediaTracker = this.mediaTracker;
Expand Down Expand Up @@ -1086,7 +1087,7 @@ public void setClip(Shape s) {
followPath(s, CLIP);
}
paintFill = paintStroke = null;
currentFillGState = currentStrokeGState = 255;
currentFillGState = currentStrokeGState = -1;
oldStroke = strokeOne;
}

Expand Down Expand Up @@ -1487,7 +1488,7 @@ private boolean drawImage(Image img, Image mask, AffineTransform xform, Color bg
} catch (Exception ex) {
throw new IllegalArgumentException();
}
if (currentFillGState != 255) {
if (currentFillGState != 255 && currentFillGState != -1) {
PdfGState gs = fillGState[currentFillGState];
cb.setGState(gs);
}
Expand Down Expand Up @@ -1613,10 +1614,23 @@ else if (paint instanceof TexturePaint) {
PdfPatternPainter pattern = cb.createPattern(width, height);
image.setAbsolutePosition(0,0);
pattern.addImage(image);
if (fill)

if (fill) {
if (currentFillGState != 255){
currentFillGState = 255;
PdfGState gs = fillGState[255];
if (gs == null) {
gs = new PdfGState();
gs.setFillOpacity(1);
fillGState[255] = gs;
}
cb.setGState(gs);
}
cb.setPatternFill(pattern);
else
}
else {
cb.setPatternStroke(pattern);
}
} catch (Exception ex) {
if (fill)
cb.setColorFill(Color.gray);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,6 @@ void process(byte ttfAfm[], boolean preload) throws DocumentException, IOExcepti
readCMaps();
readKerning();
readBbox();
GlyphWidths = null;
}
}
finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@
package com.lowagie.text.pdf;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import com.lowagie.text.error_messages.MessageLocalization;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.lowagie.text.DocumentException;
import com.lowagie.text.Utilities;
import com.lowagie.text.error_messages.MessageLocalization;

/** Represents a True Type font with Unicode encoding. All the character
* in the font can be used directly by using the encoding Identity-H or
Expand All @@ -71,6 +75,8 @@ class TrueTypeFontUnicode extends TrueTypeFont implements Comparator{
*/
boolean vertical = false;

Map<Integer, Integer> inverseCmap;

/**
* Creates a new TrueType font addressed by Unicode characters. The font
* will always be embedded.
Expand Down Expand Up @@ -117,6 +123,32 @@ class TrueTypeFontUnicode extends TrueTypeFont implements Comparator{
vertical = enc.endsWith("V");
}

void readCMaps() throws DocumentException, IOException {
super.readCMaps();

Map cmap = null;
if (cmapExt != null) {
cmap = cmapExt;
} else if (cmap31 != null) {
cmap = cmap31;
}

if (cmap != null) {
inverseCmap = new HashMap<Integer, Integer>();
for (Iterator iterator = cmap.entrySet().iterator(); iterator.hasNext();) {
Map.Entry entry = (Map.Entry) iterator.next();
Integer code = (Integer) entry.getKey();
int[] metrics = (int[]) entry.getValue();
inverseCmap.put(metrics[0], code);
}
}
}

protected Integer getCharacterCode(int code) {
return inverseCmap == null ? null : inverseCmap.get(code);
}


/**
* Gets the width of a <CODE>char</CODE> in normalized 1000 units.
* @param char1 the unicode <CODE>char</CODE> to get the width of
Expand Down Expand Up @@ -174,6 +206,7 @@ public int getWidth(String text) {
* @return the stream representing this CMap or <CODE>null</CODE>
*/
private PdfStream getToUnicode(Object metrics[]) {
metrics = filterCmapMetrics(metrics);
if (metrics.length == 0)
return null;
StringBuffer buf = new StringBuffer(
Expand Down Expand Up @@ -215,6 +248,30 @@ private PdfStream getToUnicode(Object metrics[]) {
return stream;
}

private Object[] filterCmapMetrics(Object[] metrics) {
if (metrics.length == 0) {
return metrics;
}

List<int[]> cmapMetrics = new ArrayList<int[]>(metrics.length);
for (int i = 0; i < metrics.length; i++) {
int metric[] = (int[]) metrics[i];
// PdfContentByte.showText(GlyphVector) uses glyphs that might not
// map to a character.
// the glyphs are included in the metrics array, but we need to
// exclude them from the cmap.
if (metric.length >= 3) {
cmapMetrics.add(metric);
}
}

if (cmapMetrics.size() == metrics.length) {
return metrics;
}

return cmapMetrics.toArray();
}

private static String toHex4(int n) {
String s = "0000" + Integer.toHexString(n);
return s.substring(s.length() - 4);
Expand Down

0 comments on commit 2fe8cfe

Please sign in to comment.