diff --git a/pdf.js b/pdf.js index 0af728baf..ba4ef61c1 100644 --- a/pdf.js +++ b/pdf.js @@ -3448,7 +3448,7 @@ var EvalState = (function() { // Character and word spacing this.charSpace = 0; this.wordSpace = 0; - this.textHScale = 100; + this.textHScale = 1; // Color spaces this.fillColorSpace = null; this.strokeColorSpace = null; @@ -3866,7 +3866,7 @@ var CanvasExtraState = (function() { // Character and word spacing this.charSpace = 0; this.wordSpace = 0; - this.textHScale = 100; + this.textHScale = 1; // Color spaces this.fillColorSpaceObj = null; this.strokeColorSpaceObj = null; @@ -4114,13 +4114,13 @@ var CanvasGraphics = (function() { endText: function() { }, setCharSpacing: function(spacing) { - this.ctx.charSpacing = spacing; + this.current.charSpacing = spacing; }, setWordSpacing: function(spacing) { - this.ctx.wordSpacing = spacing; + this.current.wordSpacing = spacing; }, setHScale: function(scale) { - this.ctx.textHScale = (scale % 100) * 0.01; + this.current.textHScale = scale / 100; }, setLeading: function(leading) { this.current.leading = leading; @@ -4195,18 +4195,40 @@ var CanvasGraphics = (function() { ctx.save(); ctx.transform.apply(ctx, current.textMatrix); ctx.scale(1, -1); + + ctx.translate(current.x, -1 * current.y); - if (this.ctx.$showText) { - ctx.$showText(current.y, text); - } else { - ctx.translate(current.x, -1 * current.y); - var font = this.current.font; - if (font) { - if (this.current.fontSize < kRasterizerMin) - ctx.transform(1 / kScalePrecision, 0, 0, 1 / kScalePrecision, 0, 0); - ctx.transform.apply(ctx, font.textMatrix); - text = font.charsToUnicode(text); + var scaleFactorX = 1, scaleFactorY = 1; + var font = this.current.font; + if (font) { + if (this.current.fontSize < kRasterizerMin) { + scaleFactorX = scaleFactorY = kScalePrecision; + ctx.scale(1 / scaleFactorX, 1 / scaleFactorY); } + ctx.transform.apply(ctx, font.textMatrix); + text = font.charsToUnicode(text); + } + + var charSpacing = current.charSpacing; + var wordSpacing = current.wordSpacing; + var textHScale = current.textHScale; + + if (charSpacing != 0 || wordSpacing != 0 || textHScale != 1) { + scaleFactorX *= textHScale; + ctx.scale(1 / textHScale, 1); + var width = 0; + + for (var i = 0, ii = text.length; i < ii; ++i) { + var c = text.charAt(i); + ctx.fillText(c, 0, 0); + var charWidth = FontMeasure.measureText(c) + charSpacing; + if (c.charCodeAt(0) == 32) + charWidth += wordSpacing; + ctx.translate(charWidth * scaleFactorX, 0); + width += charWidth; + } + current.x += width; + } else { ctx.fillText(text, 0, 0); current.x += FontMeasure.measureText(text); } @@ -4220,7 +4242,7 @@ var CanvasGraphics = (function() { if (this.ctx.$addCurrentX) { this.ctx.$addCurrentX(-e * 0.001 * this.current.fontSize); } else { - this.current.x -= e * 0.001 * this.current.fontSize; + this.current.x -= e * 0.001 * this.current.fontSize * this.current.textHScale; } } else if (IsString(e)) { this.showText(e);