From: Artur Adib Date: Tue, 29 Nov 2011 19:17:05 +0000 (-0500) Subject: Merge branch 'master' of git://github.com/mozilla/pdf.js into text-select X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=a544bed57e9f2d79cf6280075b6630fdf73b6af4;p=pdf.js.git Merge branch 'master' of git://github.com/mozilla/pdf.js into text-select Conflicts: src/canvas.js --- a544bed57e9f2d79cf6280075b6630fdf73b6af4 diff --cc src/canvas.js index bf09e80,5873579..99644eb --- a/src/canvas.js +++ b/src/canvas.js @@@ -565,70 -528,11 +565,64 @@@ var CanvasGraphics = (function canvasGr nextLine: function canvasGraphicsNextLine() { this.moveText(0, this.current.leading); }, - showText: function canvasGraphicsShowText(text) { + applyTextTransforms: function canvasApplyTransforms() { var ctx = this.ctx; var current = this.current; + var textHScale = current.textHScale; var font = current.font; - var glyphs = font.charsToGlyphs(text); + + ctx.transform.apply(ctx, current.textMatrix); + ctx.scale(1, -1); + ctx.translate(current.x, -1 * current.y); + ctx.transform.apply(ctx, font.fontMatrix || IDENTITY_MATRIX); + ctx.scale(1 / textHScale, 1); + }, + getTextGeometry: function canvasGetTextGeometry() { + var geom = {}; + var ctx = this.ctx; + var font = this.current.font; + var ctxMatrix = ctx.mozCurrentTransform; + if (ctxMatrix) { + var bl = Util.applyTransform([0, 0], ctxMatrix); + var tr = Util.applyTransform([1, 1], ctxMatrix); + geom.x = bl[0]; + geom.y = bl[1]; + geom.hScale = tr[0] - bl[0]; + geom.vScale = tr[1] - bl[1]; + } + var spaceGlyph = font.charsToGlyphs(' '); + // Hack (sometimes space is not encoded) + if (spaceGlyph.length === 0 || spaceGlyph[0].width === 0) + spaceGlyph = font.charsToGlyphs('i'); + // Fallback + if (spaceGlyph.length === 0 || spaceGlyph[0].width === 0) + spaceGlyph = [ {width:0} ]; + geom.spaceWidth = spaceGlyph[0].width; + return geom; + }, + pushTextDivs: function canvasGraphicsPushTextDivs(text) { + var div = document.createElement('div'); + var fontSize = this.current.fontSize; + + // vScale and hScale already contain the scaling to pixel units + // as mozCurrentTransform reflects ctx.scale() changes + // (see beginDrawing()) + var fontHeight = fontSize * text.geom.vScale; + div.dataset.canvasWidth = text.canvasWidth * text.geom.hScale; + + div.style.fontSize = fontHeight + 'px'; + div.style.fontFamily = this.current.font.loadedName || 'sans-serif'; + div.style.left = text.geom.x + 'px'; + div.style.top = (text.geom.y - fontHeight) + 'px'; + div.innerHTML = text.str; + div.dataset.textLength = text.length; + this.textDivs.push(div); + }, + showText: function canvasGraphicsShowText(str, skipTextSelection) { - function unicodeToChar(unicode) { - return (unicode >= 0x10000) ? - String.fromCharCode(0xD800 | ((unicode - 0x10000) >> 10), - 0xDC00 | (unicode & 0x3FF)) : String.fromCharCode(unicode); - }; - + var ctx = this.ctx; + var current = this.current; + var font = current.font; + var glyphs = font.charsToGlyphs(str); var fontSize = current.fontSize; var charSpacing = current.charSpacing; var wordSpacing = current.wordSpacing; @@@ -669,13 -561,11 +663,13 @@@ this.restore(); var transformed = Util.applyTransform([glyph.width, 0], fontMatrix); - var width = transformed[0] * fontSize + charSpacing; - - ctx.translate(width, 0); - current.x += width; + var charWidth = transformed[0] * fontSize + charSpacing; + ctx.translate(charWidth, 0); + current.x += charWidth; - text.str += unicodeToChar(glyph.unicode); ++ text.str += glyph.fontChar; + text.length++; + text.canvasWidth += charWidth; } ctx.restore(); } else { @@@ -691,25 -586,18 +685,25 @@@ continue; } - var char = unicodeToChar(glyph.unicode); + var char = glyph.fontChar; + var charWidth = glyph.width * fontSize * 0.001 + charSpacing; ctx.fillText(char, width, 0); - width += glyph.width * fontSize * 0.001 + charSpacing; - - // TODO actual characters can be extracted from the glyph.unicode + width += charWidth; + + text.str += char === ' ' ? ' ' : char; + text.length++; + text.canvasWidth += charWidth; } - current.x += width; + current.x += width; ctx.restore(); } - }, + if (textSelection) + this.pushTextDivs(text); + + return text; + }, showSpacedText: function canvasGraphicsShowSpacedText(arr) { var ctx = this.ctx; var current = this.current;