use of com.tom_roush.pdfbox.util.Vector in project PdfBox-Android by TomRoush.
the class PDCIDFont method getPositionVector.
@Override
public Vector getPositionVector(int code) {
int cid = codeToCID(code);
Vector v = positionVectors.get(cid);
if (v == null) {
v = getDefaultPositionVector(cid);
}
return v;
}
use of com.tom_roush.pdfbox.util.Vector in project PdfBox-Android by TomRoush.
the class PDFStreamEngine method showText.
/**
* Process text from the PDF Stream. You should override this method if you want to
* perform an action when encoded text is being processed.
*
* @param string the encoded text
* @throws IOException if there is an error processing the string
*/
protected void showText(byte[] string) throws IOException {
PDGraphicsState state = getGraphicsState();
PDTextState textState = state.getTextState();
// get the current font
PDFont font = textState.getFont();
if (font == null) {
Log.w("PdfBox-Android", "No current font, will use default");
font = PDType1Font.HELVETICA;
}
float fontSize = textState.getFontSize();
float horizontalScaling = textState.getHorizontalScaling() / 100f;
float charSpacing = textState.getCharacterSpacing();
// put the text state parameters into matrix form
Matrix parameters = new Matrix(// 0
fontSize * horizontalScaling, // 0
0, // 0
0, // 0
fontSize, 0, // 1
textState.getRise());
// read the stream until it is empty
InputStream in = new ByteArrayInputStream(string);
while (in.available() > 0) {
// decode a character
int before = in.available();
int code = font.readCode(in);
int codeLength = before - in.available();
String unicode = font.toUnicode(code);
// Word spacing shall be applied to every occurrence of the single-byte character code
// 32 in a string when using a simple font or a composite font that defines code 32 as
// a single-byte code.
float wordSpacing = 0;
if (codeLength == 1 && code == 32) {
wordSpacing += textState.getWordSpacing();
}
// text rendering matrix (text space -> device space)
Matrix ctm = state.getCurrentTransformationMatrix();
Matrix textRenderingMatrix = parameters.multiply(textMatrix).multiply(ctm);
// changes to vertical text should be tested with PDFBOX-2294 and PDFBOX-1422
if (font.isVertical()) {
// position vector, in text space
Vector v = font.getPositionVector(code);
// apply the position vector to the horizontal origin to get the vertical origin
textRenderingMatrix.translate(v);
}
// get glyph's horizontal and vertical displacements, in text space
Vector w = font.getDisplacement(code);
// process the decoded glyph
saveGraphicsState();
Matrix textMatrixOld = textMatrix;
Matrix textLineMatrixOld = textLineMatrix;
showGlyph(textRenderingMatrix, font, code, unicode, w);
textMatrix = textMatrixOld;
textLineMatrix = textLineMatrixOld;
restoreGraphicsState();
// calculate the combined displacements
float tx;
float ty;
if (font.isVertical()) {
tx = 0;
ty = w.getY() * fontSize + charSpacing + wordSpacing;
} else {
tx = (w.getX() * fontSize + charSpacing + wordSpacing) * horizontalScaling;
ty = 0;
}
// update the text matrix
textMatrix.concatenate(Matrix.getTranslateInstance(tx, ty));
}
}
use of com.tom_roush.pdfbox.util.Vector in project PdfBox-Android by TomRoush.
the class PDCIDFont method readVerticalDisplacements.
private void readVerticalDisplacements() {
// default position vector and vertical displacement vector
COSBase dw2Base = dict.getDictionaryObject(COSName.DW2);
if (dw2Base instanceof COSArray) {
COSArray dw2Array = (COSArray) dw2Base;
COSBase base0 = dw2Array.getObject(0);
COSBase base1 = dw2Array.getObject(1);
if (base0 instanceof COSNumber && base1 instanceof COSNumber) {
dw2[0] = ((COSNumber) base0).floatValue();
dw2[1] = ((COSNumber) base1).floatValue();
}
}
// vertical metrics for individual CIDs.
COSBase w2Base = dict.getDictionaryObject(COSName.W2);
if (w2Base instanceof COSArray) {
COSArray w2Array = (COSArray) w2Base;
for (int i = 0; i < w2Array.size(); i++) {
COSNumber c = (COSNumber) w2Array.getObject(i);
COSBase next = w2Array.getObject(++i);
if (next instanceof COSArray) {
COSArray array = (COSArray) next;
for (int j = 0; j < array.size(); j++) {
int cid = c.intValue() + j / 3;
COSNumber w1y = (COSNumber) array.getObject(j);
COSNumber v1x = (COSNumber) array.getObject(++j);
COSNumber v1y = (COSNumber) array.getObject(++j);
verticalDisplacementY.put(cid, w1y.floatValue());
positionVectors.put(cid, new Vector(v1x.floatValue(), v1y.floatValue()));
}
} else {
int first = c.intValue();
int last = ((COSNumber) next).intValue();
COSNumber w1y = (COSNumber) w2Array.getObject(++i);
COSNumber v1x = (COSNumber) w2Array.getObject(++i);
COSNumber v1y = (COSNumber) w2Array.getObject(++i);
for (int cid = first; cid <= last; cid++) {
verticalDisplacementY.put(cid, w1y.floatValue());
positionVectors.put(cid, new Vector(v1x.floatValue(), v1y.floatValue()));
}
}
}
}
}
Aggregations