You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ja...@apache.org on 2014/08/06 21:43:14 UTC
svn commit: r1616324 - in
/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox:
pdmodel/graphics/state/ rendering/ util/operator/text/
Author: jahewson
Date: Wed Aug 6 19:43:13 2014
New Revision: 1616324
URL: http://svn.apache.org/r1616324
Log:
PDFBOX-678: support all text rendering modes for Glyph2D
Added:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/RenderingMode.java
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDGraphicsState.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDTextState.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/text/SetTextRenderingMode.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDGraphicsState.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDGraphicsState.java?rev=1616324&r1=1616323&r2=1616324&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDGraphicsState.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDGraphicsState.java Wed Aug 6 19:43:13 2014
@@ -38,6 +38,7 @@ import org.apache.pdfbox.util.Matrix;
*/
public class PDGraphicsState implements Cloneable
{
+ private boolean isClippingPathDirty;
private Area clippingPath;
private Matrix currentTransformationMatrix = new Matrix();
private PDColor strokingColor = PDColor.DEVICE_GRAY_BLACK;
@@ -460,6 +461,7 @@ public class PDGraphicsState implements
clone.nonStrokingColor = nonStrokingColor; // immutable
clone.lineDashPattern = lineDashPattern; // immutable
clone.clippingPath = clippingPath; // not cloned, see intersectClippingPath
+ clone.isClippingPathDirty = false;
return clone;
}
catch (CloneNotSupportedException e)
@@ -555,11 +557,24 @@ public class PDGraphicsState implements
*/
public void intersectClippingPath(GeneralPath path)
{
+ intersectClippingPath(new Area(path));
+ }
+
+ /**
+ * Modify the current clipping path by intersecting it with the given path.
+ * @param area area to intersect with the clipping path
+ */
+ public void intersectClippingPath(Area area)
+ {
// lazy cloning of clipping path for performance
- clippingPath = (Area) clippingPath.clone();
+ if (isClippingPathDirty)
+ {
+ clippingPath = (Area) clippingPath.clone();
+ isClippingPathDirty = true;
+ }
// intersection as usual
- clippingPath.intersect(new Area(path));
+ clippingPath.intersect(area);
}
/**
@@ -572,12 +587,12 @@ public class PDGraphicsState implements
return clippingPath;
}
- public Composite getStrokeJavaComposite()
+ public Composite getStrokingJavaComposite()
{
return BlendComposite.getInstance(blendMode, (float) alphaConstants);
}
- public Composite getNonStrokeJavaComposite()
+ public Composite getNonStrokingJavaComposite()
{
return BlendComposite.getInstance(blendMode, (float) nonStrokingAlphaConstants);
}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDTextState.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDTextState.java?rev=1616324&r1=1616323&r2=1616324&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDTextState.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDTextState.java Wed Aug 6 19:43:13 2014
@@ -22,53 +22,17 @@ import org.apache.pdfbox.pdmodel.font.PD
* This class will hold the current state of the text parameters when executing a
* content stream.
*
- * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
- * @version $Revision: 1.4 $
+ * @author Ben Litchfield
*/
public class PDTextState implements Cloneable
{
- /**
- * See PDF Reference 1.5 Table 5.3.
- */
- public static final int RENDERING_MODE_FILL_TEXT = 0;
- /**
- * See PDF Reference 1.5 Table 5.3.
- */
- public static final int RENDERING_MODE_STROKE_TEXT = 1;
- /**
- * See PDF Reference 1.5 Table 5.3.
- */
- public static final int RENDERING_MODE_FILL_THEN_STROKE_TEXT = 2;
- /**
- * See PDF Reference 1.5 Table 5.3.
- */
- public static final int RENDERING_MODE_NEITHER_FILL_NOR_STROKE_TEXT = 3;
- /**
- * See PDF Reference 1.5 Table 5.3.
- */
- public static final int RENDERING_MODE_FILL_TEXT_AND_ADD_TO_PATH_FOR_CLIPPING = 4;
- /**
- * See PDF Reference 1.5 Table 5.3.
- */
- public static final int RENDERING_MODE_STROKE_TEXT_AND_ADD_TO_PATH_FOR_CLIPPING = 5;
- /**
- * See PDF Reference 1.5 Table 5.3.
- */
- public static final int RENDERING_MODE_FILL_THEN_STROKE_TEXT_AND_ADD_TO_PATH_FOR_CLIPPING = 6;
- /**
- * See PDF Reference 1.5 Table 5.3.
- */
- public static final int RENDERING_MODE_ADD_TEXT_TO_PATH_FOR_CLIPPING = 7;
-
-
- //these are set default according to PDF Reference 1.5 section 5.2
private float characterSpacing = 0;
private float wordSpacing = 0;
private float horizontalScaling = 100;
private float leading = 0;
private PDFont font;
private float fontSize;
- private int renderingMode = 0;
+ private RenderingMode renderingMode = RenderingMode.FILL;
private float rise = 0;
private boolean knockout = true;
@@ -199,7 +163,7 @@ public class PDTextState implements Clon
*
* @return The renderingMode.
*/
- public int getRenderingMode()
+ public RenderingMode getRenderingMode()
{
return renderingMode;
}
@@ -207,11 +171,11 @@ public class PDTextState implements Clon
/**
* Set the value of the renderingMode.
*
- * @param value The renderingMode.
+ * @param renderingMode The renderingMode.
*/
- public void setRenderingMode(int value)
+ public void setRenderingMode(RenderingMode renderingMode)
{
- renderingMode = value;
+ this.renderingMode = renderingMode;
}
/**
Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/RenderingMode.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/RenderingMode.java?rev=1616324&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/RenderingMode.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/RenderingMode.java Wed Aug 6 19:43:13 2014
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.pdmodel.graphics.state;
+
+/**
+ * Text Rendering Mode.
+ *
+ * @author John Hewson
+ */
+public enum RenderingMode
+{
+ /**
+ * Fill text.
+ */
+ FILL(0),
+
+ /**
+ * Stroke text.
+ */
+ STROKE(1),
+
+ /**
+ * Fill, then stroke text.
+ */
+ FILL_STROKE(2),
+
+ /**
+ * Neither fill nor stroke text (invisible)
+ */
+ NEITHER(3),
+
+ /**
+ * Fill text and add to path for clipping.
+ */
+ FILL_CLIP(4),
+
+ /**
+ * Stroke text and add to path for clipping.
+ */
+ STROKE_CLIP(5),
+
+ /**
+ * Fill, then stroke text and add to path for clipping.
+ */
+ FILL_STROKE_CLIP(6),
+
+ /**
+ * Add text to path for clipping.
+ */
+ NEITHER_CLIP(7);
+
+ private static final RenderingMode[] VALUES = RenderingMode.values();
+
+ public static RenderingMode fromInt(int value)
+ {
+ return VALUES[value];
+ }
+
+ private final int value;
+
+ RenderingMode(int value)
+ {
+ this.value = value;
+ }
+
+ /**
+ * Returns the integer value of this mode, as used in a PDF file.
+ */
+ public int intValue()
+ {
+ return value;
+ }
+
+ /**
+ * Returns true is this mode fills text.
+ */
+ public boolean isFill()
+ {
+ return this == FILL ||
+ this == FILL_STROKE ||
+ this == FILL_CLIP ||
+ this == FILL_STROKE_CLIP;
+ }
+
+ /**
+ * Returns true is this mode strokes text.
+ */
+ public boolean isStroke()
+ {
+ return this == STROKE ||
+ this == FILL_STROKE ||
+ this == STROKE ||
+ this == FILL_STROKE_CLIP;
+ }
+
+ /**
+ * Returns true is this mode clips text.
+ */
+ public boolean isClip()
+ {
+ return this == FILL_CLIP ||
+ this == STROKE_CLIP ||
+ this == FILL_STROKE_CLIP ||
+ this == NEITHER_CLIP;
+ }
+}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java?rev=1616324&r1=1616323&r2=1616324&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java Wed Aug 6 19:43:13 2014
@@ -18,7 +18,6 @@ package org.apache.pdfbox.rendering;
import java.awt.BasicStroke;
import java.awt.Color;
-import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
@@ -49,6 +48,7 @@ import org.apache.fontbox.type1.Type1Fon
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSStream;
import org.apache.pdfbox.pdmodel.graphics.image.PDImage;
+import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode;
import org.apache.pdfbox.rendering.font.Glyph2D;
import org.apache.pdfbox.rendering.font.TTFGlyph2D;
import org.apache.pdfbox.rendering.font.Type1Glyph2D;
@@ -77,7 +77,6 @@ import org.apache.pdfbox.pdmodel.graphic
import org.apache.pdfbox.pdmodel.graphics.pattern.PDTilingPattern;
import org.apache.pdfbox.pdmodel.graphics.shading.PDShading;
import org.apache.pdfbox.pdmodel.graphics.state.PDGraphicsState;
-import org.apache.pdfbox.pdmodel.graphics.state.PDTextState;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;
@@ -92,7 +91,6 @@ import org.apache.pdfbox.util.PDFGraphic
public class PageDrawer extends PDFGraphicsStreamEngine
{
private static final Log LOG = LogFactory.getLog(PageDrawer.class);
- private static final Color COLOR_TRANSPARENT = new Color(0, 0, 0, 0);
// parent document renderer
private final PDFRenderer renderer;
@@ -156,7 +154,7 @@ public class PageDrawer extends PDFGraph
this.pageSize = pageSize;
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
- graphics.translate(0, (int)pageSize.getHeight());
+ graphics.translate(0, (int) pageSize.getHeight());
graphics.scale(1, -1);
// TODO use getStroke() to set the initial stroke
graphics.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
@@ -260,36 +258,34 @@ public class PageDrawer extends PDFGraph
@Override
protected void processText(byte[] string) throws IOException
{
+ //
+ // DEPRECATED: Used for AWT text only. Will be removed soon! Don't edit me.
+ //
+
PDGraphicsState state = getGraphicsState();
- Composite composite;
- Paint paint;
- switch (state.getTextState().getRenderingMode())
- {
- case PDTextState.RENDERING_MODE_FILL_TEXT:
- composite = state.getNonStrokeJavaComposite();
- paint = getNonStrokingPaint();
- break;
- case PDTextState.RENDERING_MODE_STROKE_TEXT:
- composite = state.getStrokeJavaComposite();
- paint = getStrokingPaint();
- break;
- case PDTextState.RENDERING_MODE_NEITHER_FILL_NOR_STROKE_TEXT:
- // basic support for text rendering mode "invisible"
- // TODO why are we drawing anything at all?
- paint = COLOR_TRANSPARENT;
- composite = state.getStrokeJavaComposite();
- break;
- default:
- // TODO : need to implement....
- LOG.debug("Unsupported RenderingMode "
- + this.getGraphicsState().getTextState().getRenderingMode()
- + " in PageDrawer.processTextPosition()." + " Using RenderingMode "
- + PDTextState.RENDERING_MODE_FILL_TEXT + " instead");
- composite = state.getNonStrokeJavaComposite();
- paint = getNonStrokingPaint();
+ RenderingMode renderingMode = state.getTextState().getRenderingMode();
+
+ if (renderingMode == RenderingMode.FILL)
+ {
+ graphics.setComposite(state.getNonStrokingJavaComposite());
+ graphics.setPaint(getNonStrokingPaint());
}
- graphics.setComposite(composite);
- graphics.setPaint(paint);
+ else if (renderingMode == RenderingMode.STROKE)
+ {
+ graphics.setComposite(state.getStrokingJavaComposite());
+ graphics.setPaint(getStrokingPaint());
+ graphics.setStroke(getStroke());
+ }
+ else
+ {
+ LOG.debug("Unsupported RenderingMode " +
+ this.getGraphicsState().getTextState().getRenderingMode() +
+ " in PageDrawer.processTextPosition()." + " Using RenderingMode " +
+ RenderingMode.FILL + " instead");
+ graphics.setComposite(state.getNonStrokingJavaComposite());
+ graphics.setPaint(getNonStrokingPaint());
+ }
+
super.processText(string);
}
@@ -322,7 +318,7 @@ public class PageDrawer extends PDFGraph
fontMatrix.getValue(2, 0), fontMatrix.getValue(2, 1));
at.concatenate(fontMatrixAT);
// Let PDFBox render the font if supported
- drawGlyph2D(glyph2D, charCodes, at);
+ drawGlyphs2D(glyph2D, charCodes, at);
}
else
{
@@ -339,22 +335,59 @@ public class PageDrawer extends PDFGraph
}
/**
- * Render the font using the Glyph2d interface.
+ * Render the font using the Glyph2D interface.
*
* @param glyph2D the Glyph2D implementation provided a GeneralPath for each glyph
* @param codePoints the string to be rendered
* @param at the transformation
* @throws IOException if something went wrong
*/
- private void drawGlyph2D(Glyph2D glyph2D, int[] codePoints, AffineTransform at) throws IOException
+ private void drawGlyphs2D(Glyph2D glyph2D, int[] codePoints, AffineTransform at) throws IOException
{
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- for (int i = 0; i < codePoints.length; i++)
+
+ PDGraphicsState state = getGraphicsState();
+ RenderingMode renderingMode = state.getTextState().getRenderingMode();
+
+ boolean needsFill = true;
+ boolean needsStroke = true;
+
+ for (int codePoint : codePoints)
{
- GeneralPath path = glyph2D.getPathForCharacterCode(codePoints[i]);
+ GeneralPath path = glyph2D.getPathForCharacterCode(codePoint);
if (path != null)
{
- graphics.fill(at.createTransformedShape(path));
+ Shape glyph = at.createTransformedShape(path);
+
+ if (renderingMode.isFill())
+ {
+ if (needsFill)
+ {
+ graphics.setComposite(state.getNonStrokingJavaComposite());
+ graphics.setPaint(getNonStrokingPaint());
+ needsFill = false;
+ needsStroke = true;
+ }
+ graphics.fill(glyph);
+ }
+
+ if (renderingMode.isStroke())
+ {
+ if (needsStroke)
+ {
+ graphics.setComposite(state.getStrokingJavaComposite());
+ graphics.setPaint(getStrokingPaint());
+ graphics.setStroke(getStroke());
+ needsFill = true;
+ needsStroke = false;
+ }
+ graphics.draw(glyph);
+ }
+
+ if (renderingMode.isClip())
+ {
+ state.intersectClippingPath(new Area(glyph));
+ }
}
}
}
@@ -427,13 +460,17 @@ public class PageDrawer extends PDFGraph
private void writeFont(final AffineTransform at, final GlyphVector glyphs)
{
+ //
+ // DEPRECATED: Will be removed soon! Don't edit me.
+ //
+
// Convert from PDF, where glyphs are upright when direction is from
// bottom to top, to AWT, where this is the other way around
-
+
// PDFBOX-2141: do not use graphics.transform(), because this prevents
// the correct rendering of shading patterns
// don't apply the translation to each glyph, only scale and shear
- AffineTransform atRS = new AffineTransform(at.getScaleX(), at.getShearY(),
+ AffineTransform atRS = new AffineTransform(at.getScaleX(), at.getShearY(),
-at.getShearX(), -at.getScaleY(), 0, 0);
for (int i = 0; i < glyphs.getNumGlyphs(); i++)
@@ -452,6 +489,10 @@ public class PageDrawer extends PDFGraph
*/
private Font createAWTFont(PDFont font) throws IOException
{
+ //
+ // DEPRECATED: Will be removed soon! Don't edit me.
+ //
+
Font awtFont = null;
// Is there already a AWTFont for the given font?
if (awtFonts.containsKey(font))
@@ -460,6 +501,8 @@ public class PageDrawer extends PDFGraph
}
else
{
+ LOG.info("Using AWT font for " + font.getBaseFont());
+
if (font instanceof PDType1Font)
{
PDType1Font type1Font = (PDType1Font) font;
@@ -703,7 +746,7 @@ public class PageDrawer extends PDFGraph
@Override
public void strokePath() throws IOException
{
- graphics.setComposite(getGraphicsState().getStrokeJavaComposite());
+ graphics.setComposite(getGraphicsState().getStrokingJavaComposite());
Paint strokingPaint = getStrokingPaint();
if (strokingPaint == null)
{
@@ -722,7 +765,7 @@ public class PageDrawer extends PDFGraph
@Override
public void fillPath(int windingRule) throws IOException
{
- graphics.setComposite(getGraphicsState().getNonStrokeJavaComposite());
+ graphics.setComposite(getGraphicsState().getNonStrokingJavaComposite());
Paint nonStrokingPaint = getNonStrokingPaint();
if (nonStrokingPaint == null)
{
@@ -803,7 +846,7 @@ public class PageDrawer extends PDFGraph
}
linePath.reset();
}
-
+
public void drawImage(PDImage pdImage) throws IOException
{
Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();
@@ -829,7 +872,7 @@ public class PageDrawer extends PDFGraph
public void drawBufferedImage(BufferedImage image, AffineTransform at) throws IOException
{
- graphics.setComposite(getGraphicsState().getNonStrokeJavaComposite());
+ graphics.setComposite(getGraphicsState().getNonStrokingJavaComposite());
setClip();
PDSoftMask softMask = getGraphicsState().getSoftMask();
if( softMask != null )
@@ -865,7 +908,7 @@ public class PageDrawer extends PDFGraph
Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();
Paint paint = shading.toPaint(ctm, (int)pageSize.getHeight());
- graphics.setComposite(getGraphicsState().getNonStrokeJavaComposite());
+ graphics.setComposite(getGraphicsState().getNonStrokingJavaComposite());
graphics.setPaint(paint);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
graphics.setClip(null);
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/text/SetTextRenderingMode.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/text/SetTextRenderingMode.java?rev=1616324&r1=1616323&r2=1616324&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/text/SetTextRenderingMode.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/operator/text/SetTextRenderingMode.java Wed Aug 6 19:43:13 2014
@@ -20,6 +20,7 @@ import java.util.List;
import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSNumber;
+import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode;
import org.apache.pdfbox.util.operator.Operator;
import org.apache.pdfbox.util.operator.OperatorProcessor;
@@ -35,7 +36,8 @@ public class SetTextRenderingMode extend
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
- COSNumber mode = (COSNumber)arguments.get( 0 );
- context.getGraphicsState().getTextState().setRenderingMode( mode.intValue() );
+ COSNumber mode = (COSNumber)arguments.get(0);
+ RenderingMode renderingMode = RenderingMode.fromInt(mode.intValue());
+ context.getGraphicsState().getTextState().setRenderingMode(renderingMode);
}
}