You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2009/12/01 21:17:01 UTC
svn commit: r885904 - in /pdfbox/trunk:
src/main/java/org/apache/pdfbox/util/PDFStreamEngine.java
test/input/cweb.pdf-sorted.txt test/input/cweb.pdf.txt
Author: lehmi
Date: Tue Dec 1 20:17:01 2009
New Revision: 885904
URL: http://svn.apache.org/viewvc?rev=885904&view=rev
Log:
PDFBOX-520: improved char and word spacing. Patch by Villu Ruusmann (villu dot ruusmann at gmail dot com) and Antony Scerri (tony dot scerri at gmail dot com)
Modified:
pdfbox/trunk/src/main/java/org/apache/pdfbox/util/PDFStreamEngine.java
pdfbox/trunk/test/input/cweb.pdf-sorted.txt
pdfbox/trunk/test/input/cweb.pdf.txt
Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/util/PDFStreamEngine.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/util/PDFStreamEngine.java?rev=885904&r1=885903&r2=885904&view=diff
==============================================================================
--- pdfbox/trunk/src/main/java/org/apache/pdfbox/util/PDFStreamEngine.java (original)
+++ pdfbox/trunk/src/main/java/org/apache/pdfbox/util/PDFStreamEngine.java Tue Dec 1 20:17:01 2009
@@ -311,7 +311,7 @@
/* Convert textMatrix to display units */
- Matrix initialMatrix = new Matrix();
+ final Matrix initialMatrix = new Matrix();
initialMatrix.setValue(0,0,1);
initialMatrix.setValue(0,1,0);
initialMatrix.setValue(0,2,0);
@@ -321,10 +321,13 @@
initialMatrix.setValue(2,0,0);
initialMatrix.setValue(2,1,riseText);
initialMatrix.setValue(2,2,1);
-
+
final Matrix ctm = graphicsState.getCurrentTransformationMatrix();
- final Matrix textMatrixStDisp = initialMatrix.multiply( textMatrix ).multiply( ctm );
-
+ final Matrix dispMatrix = initialMatrix.multiply( ctm );
+
+ Matrix textMatrixStDisp = textMatrix.multiply( dispMatrix );
+ Matrix textMatrixEndDisp = null;
+
final float xScaleDisp = textMatrixStDisp.getXScale();
final float yScaleDisp = textMatrixStDisp.getYScale();
@@ -332,9 +335,10 @@
final float wordSpacingDisp = wordSpacingText * xScaleDisp * fontSizeText;
float maxVerticalDisplacementText = 0;
- float[] individualWidthsText = new float[2048];
- StringBuffer stringResult = new StringBuffer(string.length);
-
+
+ float[] individualWidthsBuffer = new float[string.length];
+ StringBuffer characterBuffer = new StringBuffer(string.length);
+
int codeLength = 1;
for( int i=0; i<string.length; i+=codeLength )
{
@@ -381,33 +385,49 @@
spacingText += wordSpacingText;
}
- // get the X location before we update the text matrix
- float xPosBeforeText = initialMatrix.multiply( textMatrix ).multiply( ctm ).getXPosition();
-
/* The text matrix gets updated after each glyph is placed. The updated
* version will have the X and Y coordinates for the next glyph.
*/
-
+ Matrix glyphMatrixStDisp = textMatrix.multiply( dispMatrix );
+
//The adjustment will always be zero. The adjustment as shown in the
//TJ operator will be handled separately.
float adjustment=0;
// TODO : tx should be set for horizontal text and ty for vertical text
// which seems to be specified in the font (not the direction in the matrix).
- float tx = ((characterHorizontalDisplacementText-adjustment/glyphSpaceToTextSpaceFactor)*fontSizeText
- + spacingText) * horizontalScalingText;
- float ty = 0;
-
+ float tx = ((characterHorizontalDisplacementText-adjustment/glyphSpaceToTextSpaceFactor)*fontSizeText)
+ * horizontalScalingText;
+ float ty = 0;
+
Matrix td = new Matrix();
td.setValue( 2, 0, tx );
- td.setValue( 2, 1, ty );
-
+ td.setValue( 2, 1, ty );
+
textMatrix = td.multiply( textMatrix );
+ Matrix glyphMatrixEndDisp = textMatrix.multiply( dispMatrix );
+
+ float sx = spacingText * horizontalScalingText;
+ float sy = 0;
+
+ Matrix sd = new Matrix();
+ sd.setValue( 2, 0, sx );
+ sd.setValue( 2, 1, sy );
+
+ textMatrix = sd.multiply( textMatrix );
+
// determine the width of this character
// XXX: Note that if we handled vertical text, we should be using Y here
-
- float widthText = initialMatrix.multiply( textMatrix ).multiply( ctm ).getXPosition() - xPosBeforeText;
-
+
+ float widthText = glyphMatrixStDisp.getXPosition() - glyphMatrixEndDisp.getXPosition();
+
+ while( characterBuffer.length() + ( c != null ? c.length() : 1 ) > individualWidthsBuffer.length )
+ {
+ float[] tmp = new float[individualWidthsBuffer.length * 2];
+ System.arraycopy( individualWidthsBuffer, 0, tmp, 0, individualWidthsBuffer.length );
+ individualWidthsBuffer = tmp;
+ }
+
//there are several cases where one character code will
//output multiple characters. For example "fi" or a
//glyphname that has no mapping like "visiblespace"
@@ -415,18 +435,12 @@
{
// assume each character is the same size
float widthOfEachCharacterForCode = widthText/c.length();
-
+
for( int j=0; j<c.length(); j++)
{
- if( stringResult.length()+j <individualWidthsText.length )
- {
- if( c.equals("-"))
- {
- //System.out.println( "stringResult.length()+j=" + (widthOfEachCharacterForCode));
- }
- individualWidthsText[stringResult.length()+j] = widthOfEachCharacterForCode;
- }
+ individualWidthsBuffer[characterBuffer.length() + j] = widthOfEachCharacterForCode;
}
+
validCharCnt += c.length();
}
else
@@ -434,45 +448,44 @@
// PDFBOX-373: Replace a null entry with "?" so it is
// not printed as "(null)"
c = "?";
+
+ individualWidthsBuffer[characterBuffer.length()] = widthText;
}
+ characterBuffer.append(c);
+
totalCharCnt += c.length();
-
- stringResult.append( c );
- }
-
-
- String resultingString = stringResult.toString();
-
- if( individualWidthsText.length != resultingString.length() )
- {
- float[] tmp = new float[resultingString.length()];
- System.arraycopy( individualWidthsText, 0, tmp, 0,
- Math.min( individualWidthsText.length, resultingString.length() ));
- individualWidthsText = tmp;
- if( resultingString.equals( "- " ))
+
+ if( spacingText == 0 && (i + codeLength) < (string.length - 1) )
{
- //System.out.println( "EQUALS " + individualWidths[0] );
+ continue;
}
+
+ textMatrixEndDisp = glyphMatrixEndDisp;
+
+ float totalVerticalDisplacementDisp = maxVerticalDisplacementText * fontSizeText * yScaleDisp;
+
+ float[] individualWidths = new float[characterBuffer.length()];
+ System.arraycopy( individualWidthsBuffer, 0, individualWidths, 0, individualWidths.length );
+
+ // process the decoded text
+ processTextPosition(
+ new TextPosition(
+ page,
+ textMatrixStDisp,
+ textMatrixEndDisp,
+ totalVerticalDisplacementDisp,
+ individualWidths,
+ spaceWidthDisp,
+ characterBuffer.toString(),
+ font,
+ fontSizeText,
+ (int)(fontSizeText * textMatrix.getXScale()),
+ wordSpacingDisp ));
+
+ textMatrixStDisp = textMatrix.multiply( dispMatrix );
+
+ characterBuffer.setLength(0);
}
-
- float totalVerticalDisplacementDisp = maxVerticalDisplacementText * fontSizeText * yScaleDisp;
- // convert textMatrix at the end of the string to display units
- Matrix textMatrixEndDisp = initialMatrix.multiply( textMatrix ).multiply( ctm );
-
- // process the decoded text
- processTextPosition(
- new TextPosition(
- page,
- textMatrixStDisp,
- textMatrixEndDisp,
- totalVerticalDisplacementDisp,
- individualWidthsText,
- spaceWidthDisp,
- stringResult.toString(),
- font,
- fontSizeText,
- (int)(fontSizeText * textMatrix.getXScale()),
- wordSpacingDisp ));
}
/**
Modified: pdfbox/trunk/test/input/cweb.pdf-sorted.txt
URL: http://svn.apache.org/viewvc/pdfbox/trunk/test/input/cweb.pdf-sorted.txt?rev=885904&r1=885903&r2=885904&view=diff
==============================================================================
Binary files - no diff available.
Modified: pdfbox/trunk/test/input/cweb.pdf.txt
URL: http://svn.apache.org/viewvc/pdfbox/trunk/test/input/cweb.pdf.txt?rev=885904&r1=885903&r2=885904&view=diff
==============================================================================
Binary files - no diff available.