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;
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 {
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;