diff --git a/openpdf/src/main/java/com/lowagie/text/pdf/FontDetails.java b/openpdf/src/main/java/com/lowagie/text/pdf/FontDetails.java index 773d31839..b6e48f1ed 100755 --- a/openpdf/src/main/java/com/lowagie/text/pdf/FontDetails.java +++ b/openpdf/src/main/java/com/lowagie/text/pdf/FontDetails.java @@ -49,6 +49,7 @@ package com.lowagie.text.pdf; +import java.awt.font.GlyphVector; import java.io.UnsupportedEncodingException; import java.util.HashMap; @@ -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 PdfWriter of this document diff --git a/openpdf/src/main/java/com/lowagie/text/pdf/PdfContentByte.java b/openpdf/src/main/java/com/lowagie/text/pdf/PdfContentByte.java index eae055197..2a81da2ab 100644 --- a/openpdf/src/main/java/com/lowagie/text/pdf/PdfContentByte.java +++ b/openpdf/src/main/java/com/lowagie/text/pdf/PdfContentByte.java @@ -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; @@ -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 @@ -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) { @@ -3043,7 +3056,15 @@ else if (obj.isArray()) { } pdf.incMarkPoint(); mcDepth++; - content.append(struc.get(PdfName.S).getBytes()).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); } /** diff --git a/openpdf/src/main/java/com/lowagie/text/pdf/PdfDocument.java b/openpdf/src/main/java/com/lowagie/text/pdf/PdfDocument.java index 0f5c26d56..aea1aaf88 100644 --- a/openpdf/src/main/java/com/lowagie/text/pdf/PdfDocument.java +++ b/openpdf/src/main/java/com/lowagie/text/pdf/PdfDocument.java @@ -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; diff --git a/openpdf/src/main/java/com/lowagie/text/pdf/PdfGraphics2D.java b/openpdf/src/main/java/com/lowagie/text/pdf/PdfGraphics2D.java index 0e9d3b365..95eb0afa6 100755 --- a/openpdf/src/main/java/com/lowagie/text/pdf/PdfGraphics2D.java +++ b/openpdf/src/main/java/com/lowagie/text/pdf/PdfGraphics2D.java @@ -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; @@ -1086,7 +1087,7 @@ public void setClip(Shape s) { followPath(s, CLIP); } paintFill = paintStroke = null; - currentFillGState = currentStrokeGState = 255; + currentFillGState = currentStrokeGState = -1; oldStroke = strokeOne; } @@ -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); } @@ -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); diff --git a/openpdf/src/main/java/com/lowagie/text/pdf/TrueTypeFont.java b/openpdf/src/main/java/com/lowagie/text/pdf/TrueTypeFont.java index 1befc3d4d..ca8ab25e0 100644 --- a/openpdf/src/main/java/com/lowagie/text/pdf/TrueTypeFont.java +++ b/openpdf/src/main/java/com/lowagie/text/pdf/TrueTypeFont.java @@ -672,7 +672,6 @@ void process(byte ttfAfm[], boolean preload) throws DocumentException, IOExcepti readCMaps(); readKerning(); readBbox(); - GlyphWidths = null; } } finally { diff --git a/openpdf/src/main/java/com/lowagie/text/pdf/TrueTypeFontUnicode.java b/openpdf/src/main/java/com/lowagie/text/pdf/TrueTypeFontUnicode.java index c5743bfec..aacd82f86 100755 --- a/openpdf/src/main/java/com/lowagie/text/pdf/TrueTypeFontUnicode.java +++ b/openpdf/src/main/java/com/lowagie/text/pdf/TrueTypeFontUnicode.java @@ -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 @@ -71,6 +75,8 @@ class TrueTypeFontUnicode extends TrueTypeFont implements Comparator{ */ boolean vertical = false; + Map inverseCmap; + /** * Creates a new TrueType font addressed by Unicode characters. The font * will always be embedded. @@ -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(); + 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 char in normalized 1000 units. * @param char1 the unicode char to get the width of @@ -174,6 +206,7 @@ public int getWidth(String text) { * @return the stream representing this CMap or null */ private PdfStream getToUnicode(Object metrics[]) { + metrics = filterCmapMetrics(metrics); if (metrics.length == 0) return null; StringBuffer buf = new StringBuffer( @@ -215,6 +248,30 @@ private PdfStream getToUnicode(Object metrics[]) { return stream; } + private Object[] filterCmapMetrics(Object[] metrics) { + if (metrics.length == 0) { + return metrics; + } + + List cmapMetrics = new ArrayList(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);