From f85e3a7947879a03273842258081fe4588bfdc5a Mon Sep 17 00:00:00 2001 From: DD Date: Wed, 24 Oct 2018 14:20:40 -0400 Subject: [PATCH] Use different bounds for different situations --- src/item/Item.js | 3 ++- src/svg/SvgExport.js | 2 +- src/text/PointText.js | 34 +++++++++++++++++++++++----------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/item/Item.js b/src/item/Item.js index 6acb0951..6f52dcc4 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -835,7 +835,8 @@ new function() { // Injection scope for various item event handlers }, Base.each({ // Produce getters for bounds properties: getStrokeBounds: { stroke: true }, getHandleBounds: { handle: true }, - getInternalBounds: { internal: true } + getInternalBounds: { internal: true }, + getVisibleBounds: { stroke: true, forSvgExport: true }, }, function(options, key) { this[key] = function(matrix) { diff --git a/src/svg/SvgExport.js b/src/svg/SvgExport.js index 954e589f..2af1e4aa 100644 --- a/src/svg/SvgExport.js +++ b/src/svg/SvgExport.js @@ -438,7 +438,7 @@ new function() { rect = bounds === 'view' ? new Rectangle([0, 0], view.getViewSize()) : bounds === 'content' - ? Item._getBounds(children, matrix, { stroke: true }) + ? Item._getBounds(children, matrix, { stroke: true, forSvgExport: true }) .rect : Rectangle.read([bounds], 0, { readNull: true }), attrs = { diff --git a/src/text/PointText.js b/src/text/PointText.js index a6b7601b..586b8bd5 100644 --- a/src/text/PointText.js +++ b/src/text/PointText.js @@ -101,18 +101,31 @@ var PointText = TextItem.extend(/** @lends PointText# */{ ctx.translate(0, leading); } }, - _getBounds: function(matrix, options) { - var rect = this._getTextSize(); + var rect = options.forSvgExport ? this._getVisibleTextSize() : this._getMeasuredTextSize(); + console.log(rect); return matrix ? matrix._transformBounds(rect, rect) : rect; }, - _getTextSize () { - // Return cached measurement if any - if (!this._project._textSizeCache) { - this._project._textSizeCache = {}; - } - if (this._project._textSizeCache[this.content]) return this._project._textSizeCache[this.content]; - + _getMeasuredTextSize () { + console.log("get measured text size"); + var style = this._style, + lines = this._lines, + numLines = lines.length, + justification = style.getJustification(), + leading = style.getLeading(), + width = this.getView().getTextWidth(style.getFontStyle(), lines), + x = 0; + // Adjust for different justifications. + if (justification !== 'left') + x -= width / (justification === 'center' ? 2: 1); + // Until we don't have baseline measuring, assume 1 / 4 leading as a + // rough guess: + return new Rectangle(x, + numLines ? - 0.75 * leading : 0, + width, numLines * leading); + }, + _getVisibleTextSize () { + console.log("get visible text size"); var numLines = this._lines.length; var leading = this._style.getLeading(); @@ -164,8 +177,7 @@ var PointText = TextItem.extend(/** @lends PointText# */{ var y = bbox.y - halfStrokeWidth; // Add 1 to give space for text cursor - this._project._textSizeCache[this.content] = new Rectangle(x, y, width + 1, Math.max(height, numLines * leading)); - return this._project._textSizeCache[this.content]; + return new Rectangle(x, y, width + 1, Math.max(height, numLines * leading)); }, _hitTestSelf: function(point, options) { if (options.fill && (this.hasFill() || options.hitUnfilledPaths) && this._contains(point))