mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 02:38:43 -05:00
Use different bounds for different situations
This commit is contained in:
parent
edfa5aedfa
commit
f85e3a7947
3 changed files with 26 additions and 13 deletions
|
@ -835,7 +835,8 @@ new function() { // Injection scope for various item event handlers
|
||||||
}, Base.each({ // Produce getters for bounds properties:
|
}, Base.each({ // Produce getters for bounds properties:
|
||||||
getStrokeBounds: { stroke: true },
|
getStrokeBounds: { stroke: true },
|
||||||
getHandleBounds: { handle: true },
|
getHandleBounds: { handle: true },
|
||||||
getInternalBounds: { internal: true }
|
getInternalBounds: { internal: true },
|
||||||
|
getVisibleBounds: { stroke: true, forSvgExport: true },
|
||||||
},
|
},
|
||||||
function(options, key) {
|
function(options, key) {
|
||||||
this[key] = function(matrix) {
|
this[key] = function(matrix) {
|
||||||
|
|
|
@ -438,7 +438,7 @@ new function() {
|
||||||
rect = bounds === 'view'
|
rect = bounds === 'view'
|
||||||
? new Rectangle([0, 0], view.getViewSize())
|
? new Rectangle([0, 0], view.getViewSize())
|
||||||
: bounds === 'content'
|
: bounds === 'content'
|
||||||
? Item._getBounds(children, matrix, { stroke: true })
|
? Item._getBounds(children, matrix, { stroke: true, forSvgExport: true })
|
||||||
.rect
|
.rect
|
||||||
: Rectangle.read([bounds], 0, { readNull: true }),
|
: Rectangle.read([bounds], 0, { readNull: true }),
|
||||||
attrs = {
|
attrs = {
|
||||||
|
|
|
@ -101,18 +101,31 @@ var PointText = TextItem.extend(/** @lends PointText# */{
|
||||||
ctx.translate(0, leading);
|
ctx.translate(0, leading);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
_getBounds: function(matrix, options) {
|
_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;
|
return matrix ? matrix._transformBounds(rect, rect) : rect;
|
||||||
},
|
},
|
||||||
_getTextSize () {
|
_getMeasuredTextSize () {
|
||||||
// Return cached measurement if any
|
console.log("get measured text size");
|
||||||
if (!this._project._textSizeCache) {
|
var style = this._style,
|
||||||
this._project._textSizeCache = {};
|
lines = this._lines,
|
||||||
}
|
numLines = lines.length,
|
||||||
if (this._project._textSizeCache[this.content]) return this._project._textSizeCache[this.content];
|
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 numLines = this._lines.length;
|
||||||
var leading = this._style.getLeading();
|
var leading = this._style.getLeading();
|
||||||
|
|
||||||
|
@ -164,8 +177,7 @@ var PointText = TextItem.extend(/** @lends PointText# */{
|
||||||
var y = bbox.y - halfStrokeWidth;
|
var y = bbox.y - halfStrokeWidth;
|
||||||
|
|
||||||
// Add 1 to give space for text cursor
|
// 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 new Rectangle(x, y, width + 1, Math.max(height, numLines * leading));
|
||||||
return this._project._textSizeCache[this.content];
|
|
||||||
},
|
},
|
||||||
_hitTestSelf: function(point, options) {
|
_hitTestSelf: function(point, options) {
|
||||||
if (options.fill && (this.hasFill() || options.hitUnfilledPaths) && this._contains(point))
|
if (options.fill && (this.hasFill() || options.hitUnfilledPaths) && this._contains(point))
|
||||||
|
|
Loading…
Reference in a new issue