//We won't know the actual number of characters until
//we process the byte data(could be two bytes each) but
//it won't ever be more than string.length*2(there are some cases
//were a single byte will result in two output characters "fi"
final PDFont font = graphicsState.getTextState().getFont();
//This will typically be 1000 but in the case of a type3 font
//this might be a different number
final float glyphSpaceToTextSpaceFactor = 1f/font.getFontMatrix().getValue( 0, 0 );
// lets see what the space displacement should be
float spaceWidthText = (font.getFontWidth( SPACE_BYTES, 0, 1 )/glyphSpaceToTextSpaceFactor);
if( spaceWidthText == 0 )
{
spaceWidthText = (font.getAverageFontWidth()/glyphSpaceToTextSpaceFactor);
//The average space width appears to be higher than necessary
//so lets make it a little bit smaller.
spaceWidthText *= .80f;
}
/* Convert textMatrix to display units */
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,riseText);
initialMatrix.setValue(2,2,1);
final Matrix ctm = graphicsState.getCurrentTransformationMatrix();
final Matrix textMatrixStDisp = initialMatrix.multiply( textMatrix ).multiply( ctm );
final float xScaleDisp = textMatrixStDisp.getXScale();
final float yScaleDisp = textMatrixStDisp.getYScale();
final float spaceWidthDisp = spaceWidthText * xScaleDisp * fontSizeText;
final float wordSpacingDisp = wordSpacingText * xScaleDisp * fontSizeText;
float maxVerticalDisplacementText = 0;
float[] individualWidthsText = new float[2048];
StringBuffer stringResult = new StringBuffer(string.length);
int codeLength = 1;
for( int i=0; i<string.length; i+=codeLength )
{
// Decode the value to a Unicode character
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 );
}
//todo, handle horizontal displacement
// get the width and height of this character in text units
float characterHorizontalDisplacementText =
(font.getFontWidth( string, i, codeLength )/glyphSpaceToTextSpaceFactor);
maxVerticalDisplacementText =
Math.max(
maxVerticalDisplacementText,
font.getFontHeight( string, i, codeLength)/glyphSpaceToTextSpaceFactor);
// PDF Spec - 5.5.2 Word Spacing
//
// Word spacing works the same was as character spacing, but applies
// only to the space character, code 32.