float rise = graphicsState.getTextState().getRise();
final float wordSpacing = graphicsState.getTextState().getWordSpacing();
final float characterSpacing = graphicsState.getTextState().getCharacterSpacing();
float wordSpacingDisplacement = 0;
PDFont font = graphicsState.getTextState().getFont();
//This will typically be 1000 but in the case of a type3 font
//this might be a different number
float glyphSpaceToTextSpaceFactor = 1f/font.getFontMatrix().getValue( 0, 0 );
float averageWidth = font.getAverageFontWidth();
Matrix initialMatrix = new Matrix();
initialMatrix.setValue(0,0,1);
initialMatrix.setValue(0,1,0);
initialMatrix.setValue(0,2,0);
initialMatrix.setValue(1,0,0);
initialMatrix.setValue(1,1,1);
initialMatrix.setValue(1,2,0);
initialMatrix.setValue(2,0,0);
initialMatrix.setValue(2,1,rise);
initialMatrix.setValue(2,2,1);
//this
int codeLength = 1;
Matrix ctm = graphicsState.getCurrentTransformationMatrix();
//lets see what the space displacement should be
spaceDisplacement = (font.getFontWidth( SPACE_BYTES, 0, 1 )/glyphSpaceToTextSpaceFactor);
if( spaceDisplacement == 0 )
{
spaceDisplacement = (averageWidth/glyphSpaceToTextSpaceFactor);
//The average space width appears to be higher than necessary
//so lets make it a little bit smaller.
spaceDisplacement *= .80f;
}
int pageRotation = page.findRotation();
Matrix trm = initialMatrix.multiply( textMatrix ).multiply( ctm );
float x = trm.getValue(2,0);
float y = trm.getValue(2,1);
if( pageRotation == 0 )
{
trm.setValue( 2,1, -y + page.findMediaBox().getHeight() );
}
else if( pageRotation == 90 )
{
trm.setValue( 2,0, y );
trm.setValue( 2,1, x );
}
else if( pageRotation == 270 )
{
trm.setValue( 2,0, -y + page.findMediaBox().getHeight() );
trm.setValue( 2,1, x );
}
for( int i=0; i<string.length; i+=codeLength )
{
codeLength = 1;
String c = font.encode( string, i, codeLength );
if( c == null && i+1<string.length)
{
//maybe a multibyte encoding
codeLength++;
c = font.encode( string, i, codeLength );
}
stringResult.append( c );
//todo, handle horizontal displacement
characterHorizontalDisplacement += (font.getFontWidth( string, i, codeLength )/glyphSpaceToTextSpaceFactor);
characterVerticalDisplacement =
Math.max(
characterVerticalDisplacement,
font.getFontHeight( string, i, codeLength)/glyphSpaceToTextSpaceFactor);
// PDF Spec - 5.5.2 Word Spacing
//
// Word spacing works the same was as character spacing, but applies