Skip to content

Commit bf0abd6

Browse files
committed
Merge pull request mozilla#2505 from yurydelendik/refactor-cff-float
Refactors encodeFloat, font matrix and flex args
2 parents c05f073 + 10bb6c9 commit bf0abd6

File tree

3 files changed

+96
-115
lines changed

3 files changed

+96
-115
lines changed

src/canvas.js

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ var CanvasExtraState = (function CanvasExtraStateClosure() {
159159
this.fontSize = 0;
160160
this.fontSizeScale = 1;
161161
this.textMatrix = IDENTITY_MATRIX;
162-
this.fontMatrix = IDENTITY_MATRIX;
162+
this.fontMatrix = FONT_IDENTITY_MATRIX;
163163
this.leading = 0;
164164
// Current point (in user coordinates)
165165
this.x = 0;
@@ -768,11 +768,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
768768
if (!fontObj)
769769
error('Can\'t find font for ' + fontRefName);
770770

771-
// Slice-clone matrix so we can manipulate it without affecting original
772-
if (fontObj.fontMatrix)
773-
current.fontMatrix = fontObj.fontMatrix.slice(0);
774-
else
775-
current.fontMatrix = IDENTITY_MATRIX.slice(0);
771+
current.fontMatrix = fontObj.fontMatrix ? fontObj.fontMatrix :
772+
FONT_IDENTITY_MATRIX;
776773

777774
// A valid matrix needs all main diagonal elements to be non-zero
778775
// This also ensures we bypass FF bugzilla bug #719844.
@@ -783,11 +780,11 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
783780

784781
// The spec for Tf (setFont) says that 'size' specifies the font 'scale',
785782
// and in some docs this can be negative (inverted x-y axes).
786-
// We implement this condition with fontMatrix.
787783
if (size < 0) {
788784
size = -size;
789-
current.fontMatrix[0] *= -1;
790-
current.fontMatrix[3] *= -1;
785+
current.fontDirection = -1;
786+
} else {
787+
current.fontDirection = 1;
791788
}
792789

793790
this.current.font = fontObj;
@@ -840,14 +837,13 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
840837
applyTextTransforms: function CanvasGraphics_applyTextTransforms() {
841838
var ctx = this.ctx;
842839
var current = this.current;
843-
var textHScale = current.textHScale;
844-
var fontMatrix = current.fontMatrix || IDENTITY_MATRIX;
845-
846840
ctx.transform.apply(ctx, current.textMatrix);
847-
ctx.scale(1, -1);
848-
ctx.translate(current.x, -current.y - current.textRise);
849-
ctx.transform.apply(ctx, fontMatrix);
850-
ctx.scale(textHScale, 1);
841+
ctx.translate(current.x, current.y + current.textRise);
842+
if (current.fontDirection > 0) {
843+
ctx.scale(current.textHScale, -1);
844+
} else {
845+
ctx.scale(-current.textHScale, 1);
846+
}
851847
},
852848
createTextGeometry: function CanvasGraphics_createTextGeometry() {
853849
var geometry = {};
@@ -878,9 +874,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
878874
var fontSizeScale = current.fontSizeScale;
879875
var charSpacing = current.charSpacing;
880876
var wordSpacing = current.wordSpacing;
881-
var textHScale = current.textHScale;
882-
var fontMatrix = current.fontMatrix || IDENTITY_MATRIX;
883-
var textHScale2 = textHScale * fontMatrix[0];
877+
var textHScale = current.textHScale * current.fontDirection;
878+
var fontMatrix = current.fontMatrix || FONT_IDENTITY_MATRIX;
884879
var glyphsLength = glyphs.length;
885880
var textLayer = this.textLayer;
886881
var geom;
@@ -919,8 +914,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
919914
this.restore();
920915

921916
var transformed = Util.applyTransform([glyph.width, 0], fontMatrix);
922-
var width = transformed[0] * fontSize +
923-
Util.sign(current.fontMatrix[0]) * charSpacing;
917+
var width = (transformed[0] * fontSize + charSpacing) *
918+
current.fontDirection;
924919

925920
ctx.translate(width, 0);
926921
current.x += width * textHScale;
@@ -934,8 +929,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
934929

935930
var lineWidth = current.lineWidth;
936931
var a1 = current.textMatrix[0], b1 = current.textMatrix[1];
937-
var a2 = fontMatrix[0], b2 = fontMatrix[1];
938-
var scale = Math.sqrt((a1 * a1 + b1 * b1) * (a2 * a2 + b2 * b2));
932+
var scale = Math.sqrt(a1 * a1 + b1 * b1);
939933
if (scale == 0 || lineWidth == 0)
940934
lineWidth = this.getSinglePixelWidth();
941935
else
@@ -956,13 +950,13 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
956950
var glyph = glyphs[i];
957951
if (glyph === null) {
958952
// word break
959-
x += Util.sign(current.fontMatrix[0]) * wordSpacing;
953+
x += current.fontDirection * wordSpacing;
960954
continue;
961955
}
962956

963957
var character = glyph.fontChar;
964-
var charWidth = glyph.width * fontSize * 0.001 +
965-
Util.sign(current.fontMatrix[0]) * charSpacing;
958+
var charWidth = glyph.width * fontSize * current.fontMatrix[0] +
959+
charSpacing * current.fontDirection;
966960

967961
if (!glyph.disabled) {
968962
var scaledX = x / fontSizeScale;
@@ -995,7 +989,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
995989

996990
canvasWidth += charWidth;
997991
}
998-
current.x += x * textHScale2;
992+
current.x += x * textHScale;
999993
ctx.restore();
1000994
}
1001995

@@ -1011,9 +1005,9 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
10111005
var current = this.current;
10121006
var font = current.font;
10131007
var fontSize = current.fontSize;
1014-
var textHScale = current.textHScale;
1015-
if (!font.coded)
1016-
textHScale *= (current.fontMatrix || IDENTITY_MATRIX)[0];
1008+
var textHScale = current.textHScale * (current.fontMatrix && !font.coded ?
1009+
current.fontMatrix[0] : FONT_IDENTITY_MATRIX[0]) *
1010+
current.fontDirection;
10171011
var arrLength = arr.length;
10181012
var textLayer = this.textLayer;
10191013
var geom;
@@ -1022,22 +1016,15 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
10221016

10231017
if (textSelection) {
10241018
ctx.save();
1025-
// Type3 fonts - each glyph is a "mini-PDF" (see also showText)
1026-
if (font.coded) {
1027-
ctx.transform.apply(ctx, current.textMatrix);
1028-
ctx.scale(1, -1);
1029-
ctx.translate(current.x, -1 * current.y);
1030-
ctx.scale(textHScale, 1);
1031-
} else
1032-
this.applyTextTransforms();
1019+
this.applyTextTransforms();
10331020
geom = this.createTextGeometry();
10341021
ctx.restore();
10351022
}
10361023

10371024
for (var i = 0; i < arrLength; ++i) {
10381025
var e = arr[i];
10391026
if (isNum(e)) {
1040-
var spacingLength = -e * 0.001 * fontSize * textHScale;
1027+
var spacingLength = -e * fontSize * textHScale;
10411028
current.x += spacingLength;
10421029

10431030
if (textSelection)

src/evaluator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
11711171
composite: composite,
11721172
wideChars: composite,
11731173
fixedPitch: false,
1174-
fontMatrix: dict.get('FontMatrix') || IDENTITY_MATRIX,
1174+
fontMatrix: dict.get('FontMatrix') || FONT_IDENTITY_MATRIX,
11751175
firstChar: firstChar || 0,
11761176
lastChar: lastChar || maxCharIndex,
11771177
bbox: descriptor.get('FontBBox'),

0 commit comments

Comments
 (0)