Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 50 additions & 48 deletions src/core/bounding_boxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {isDict} from "./primitives";

var BoundingBoxesCalculator = (function PartialEvaluatorClosure() {
function BoundingBoxesCalculator(ignoreCalculations) {
this.textState = new TextState();
this.textStateManager = new StateManager(new TextState());
this.graphicsStateManager = new StateManager(new GraphicsState());
this.clipping = false;
this.boundingBoxesStack = new BoundingBoxStack();
Expand Down Expand Up @@ -44,60 +44,60 @@ var BoundingBoxesCalculator = (function PartialEvaluatorClosure() {

let ctm = this.graphicsStateManager.state.ctm;

let descent = (this.textState.font.descent || 0) * this.textState.fontSize;
let ascent = (this.textState.font.ascent || 1) * this.textState.fontSize;
let rise = this.textState.textRise * this.textState.fontSize;
let descent = (this.textStateManager.state.font.descent || 0) * this.textStateManager.state.fontSize;
let ascent = (this.textStateManager.state.font.ascent || 1) * this.textStateManager.state.fontSize;
let rise = this.textStateManager.state.textRise * this.textStateManager.state.fontSize;

//Calculate transformed height and shift to place whole glyph inside of bbox
let shift = Util.applyTransform([0, descent + rise], this.textState.textMatrix);
shift[0] -= this.textState.textMatrix[4];
shift[1] -= this.textState.textMatrix[5];
let shift = Util.applyTransform([0, descent + rise], this.textStateManager.state.textMatrix);
shift[0] -= this.textStateManager.state.textMatrix[4];
shift[1] -= this.textStateManager.state.textMatrix[5];

let height = Util.applyTransform([0, ascent + rise], this.textState.textMatrix);
height[0] -= this.textState.textMatrix[4] + shift[0];
height[1] -= this.textState.textMatrix[5] + shift[1];
let height = Util.applyTransform([0, ascent + rise], this.textStateManager.state.textMatrix);
height[0] -= this.textStateManager.state.textMatrix[4] + shift[0];
height[1] -= this.textStateManager.state.textMatrix[5] + shift[1];
height = Math.sqrt(height[0] * height[0] + height[1] * height[1]);

//Left Bottom point of text bbox
//Save before text matrix will be changed with going through glyphs
let [tx0, ty0] = [this.textState.textMatrix[4] + shift[0], this.textState.textMatrix[5] + shift[1]];
let [tx0, ty0] = [this.textStateManager.state.textMatrix[4] + shift[0], this.textStateManager.state.textMatrix[5] + shift[1]];

for (let i = 0; i < glyphs.length; i++) {
let glyph = glyphs[i];
if (isNum(glyph)) {
if (this.textState.font.vertical) {
ty = -glyph / 1000 * this.textState.fontSize * this.textState.textHScale;
if (this.textStateManager.state.font.vertical) {
ty = -glyph / 1000 * this.textStateManager.state.fontSize * this.textStateManager.state.textHScale;
} else {
tx = -glyph / 1000 * this.textState.fontSize * this.textState.textHScale;
tx = -glyph / 1000 * this.textStateManager.state.fontSize * this.textStateManager.state.textHScale;
}
} else {
let glyphWidth = null;
if (this.textState.font.vertical && glyph.vmetric) {
if (this.textStateManager.state.font.vertical && glyph.vmetric) {
glyphWidth = glyph.vmetric[0];
} else {
glyphWidth = glyph.width;
}
if (!this.textState.font.vertical) {
let w0 = glyphWidth * (this.textState.fontMatrix ? this.textState.fontMatrix[0] : 1 / 1000);
tx = (w0 * this.textState.fontSize + this.textState.charSpacing + (glyph.isSpace ? this.textState.wordSpacing : 0)) *
this.textState.textHScale;
if (!this.textStateManager.state.font.vertical) {
let w0 = glyphWidth * (this.textStateManager.state.fontMatrix ? this.textStateManager.state.fontMatrix[0] : 1 / 1000);
tx = (w0 * this.textStateManager.state.fontSize + this.textStateManager.state.charSpacing + (glyph.isSpace ? this.textStateManager.state.wordSpacing : 0)) *
this.textStateManager.state.textHScale;
} else {
let w1 = glyphWidth * (this.textState.fontMatrix ? this.textState.fontMatrix[0] : 1 / 1000);
ty = w1 * this.textState.fontSize + this.textState.charSpacing + (glyph.isSpace ? this.textState.wordSpacing : 0);
let w1 = glyphWidth * (this.textStateManager.state.fontMatrix ? this.textStateManager.state.fontMatrix[0] : 1 / 1000);
ty = w1 * this.textStateManager.state.fontSize + this.textStateManager.state.charSpacing + (glyph.isSpace ? this.textStateManager.state.wordSpacing : 0);
}
}
this.textState.translateTextMatrix(tx, ty);
this.textStateManager.state.translateTextMatrix(tx, ty);
}

//Right Bottom point is in text matrix after going through glyphs
let [tx1, ty1] = [this.textState.textMatrix[4] + shift[0], this.textState.textMatrix[5] + shift[1]];
let [tx1, ty1] = [this.textStateManager.state.textMatrix[4] + shift[0], this.textStateManager.state.textMatrix[5] + shift[1]];
//Top point can be calculated from base and height
let [tx2, ty2, tx3, ty3] = this.getTopPoints(tx0, ty0, tx1, ty1, height);
if (this.textState.textMatrix[3] < 0) {
ty0 += height * this.textState.textMatrix[3];
ty1 += height * this.textState.textMatrix[3];
ty2 += height * this.textState.textMatrix[3];
ty3 += height * this.textState.textMatrix[3];
if (this.textStateManager.state.textMatrix[3] < 0) {
ty0 += height * this.textStateManager.state.textMatrix[3];
ty1 += height * this.textStateManager.state.textMatrix[3];
ty2 += height * this.textStateManager.state.textMatrix[3];
ty3 += height * this.textStateManager.state.textMatrix[3];
}

//Apply transform matrix to bbox
Expand Down Expand Up @@ -374,9 +374,11 @@ var BoundingBoxesCalculator = (function PartialEvaluatorClosure() {
switch (fn | 0) {
case OPS.restore:
this.graphicsStateManager.restore();
this.textStateManager.restore();
break;
case OPS.save:
this.graphicsStateManager.save();
this.textStateManager.save();
break;
case OPS.fill:
case OPS.eoFill:
Expand All @@ -401,46 +403,46 @@ var BoundingBoxesCalculator = (function PartialEvaluatorClosure() {
this.clipping = true;
break;
case OPS.setFont:
this.textState.fontSize = args[0];
this.textState.fontMatrix = args[1].font.fontMatrix;
this.textState.font = args[1].font;
this.textStateManager.state.fontSize = args[0];
this.textStateManager.state.fontMatrix = args[1].font.fontMatrix;
this.textStateManager.state.font = args[1].font;
break;
case OPS.setTextMatrix:
this.textState.setTextMatrix(args[0], args[1], args[2], args[3],
this.textStateManager.state.setTextMatrix(args[0], args[1], args[2], args[3],
args[4], args[5]);
this.textState.setTextLineMatrix(args[0], args[1], args[2], args[3],
this.textStateManager.state.setTextLineMatrix(args[0], args[1], args[2], args[3],
args[4], args[5]);
break;
case OPS.nextLine:
this.textState.carriageReturn();
this.textStateManager.state.carriageReturn();
break;
case OPS.setCharSpacing:
this.textState.charSpacing = args[0];
this.textStateManager.state.charSpacing = args[0];
break;
case OPS.setWordSpacing:
this.textState.wordSpacing = args[0];
this.textStateManager.state.wordSpacing = args[0];
break;
case OPS.setHScale:
this.textState.textHScale = args[0] / 100;
this.textStateManager.state.textHScale = args[0] / 100;
break;
case OPS.setLeading:
this.textState.leading = args[0];
this.textStateManager.state.leading = args[0];
break;
case OPS.setTextRise:
this.textState.textRise = args[0];
this.textStateManager.state.textRise = args[0];
break;
case OPS.setLeadingMoveText:
this.textState.leading = -args[1];
this.textState.translateTextLineMatrix(...args);
this.textState.textMatrix = this.textState.textLineMatrix.slice();
this.textStateManager.state.leading = -args[1];
this.textStateManager.state.translateTextLineMatrix(...args);
this.textStateManager.state.textMatrix = this.textStateManager.state.textLineMatrix.slice();
break;
case OPS.moveText:
this.textState.translateTextLineMatrix(args[0], args[1]);
this.textState.textMatrix = this.textState.textLineMatrix.slice();
this.textStateManager.state.translateTextLineMatrix(args[0], args[1]);
this.textStateManager.state.textMatrix = this.textStateManager.state.textLineMatrix.slice();
break;
case OPS.beginText:
this.textState.textMatrix = IDENTITY_MATRIX.slice();
this.textState.textLineMatrix = IDENTITY_MATRIX.slice();
this.textStateManager.state.textMatrix = IDENTITY_MATRIX.slice();
this.textStateManager.state.textLineMatrix = IDENTITY_MATRIX.slice();
break;
case OPS.moveTo:
let ctm = this.graphicsStateManager.state.ctm.slice();
Expand Down Expand Up @@ -538,8 +540,8 @@ var BoundingBoxesCalculator = (function PartialEvaluatorClosure() {
},

setFont: function BoundingBoxesCalculator_setFont(translated) {
this.textState.fontMatrix = translated.font.fontMatrix;
this.textState.font = translated.font;
this.textStateManager.state.fontMatrix = translated.font.fontMatrix;
this.textStateManager.state.font = translated.font;
},
};

Expand Down