Skip to content

Commit

Permalink
Merge pull request #99 from adobe-photoshop/dk/pixmap-params
Browse files Browse the repository at this point in the history
Provide a utility function to ease the task of scaling assets exactly
  • Loading branch information
joelrbrandt committed Aug 23, 2013
2 parents 029fc6c + cd092b4 commit 371a9a3
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 2 deletions.
193 changes: 193 additions & 0 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,199 @@

};

Generator.prototype.getDeepBounds = function (layer) {
var bounds;

if (! layer.layers) {
bounds = layer.bounds;
}
else {
layer.layers.forEach(function (sub) {
var childBounds = this.getDeepBounds(sub);

if (!bounds) {
bounds = childBounds;
} else {
bounds = { // Compute containing rect of union of bounds and childBounds
left: Math.min(bounds.left, childBounds.left),
top: Math.min(bounds.top, childBounds.top),
right: Math.max(bounds.right, childBounds.right),
bottom: Math.max(bounds.bottom, childBounds.bottom)
};
}
});
}

if (layer.mask && layer.mask.bounds) {
var maskBounds = layer.mask.bounds;

bounds = { // compute containing rect of intersection of bounds and maskBounds
left: Math.max(bounds.left, maskBounds.left),
top: Math.max(bounds.top, maskBounds.top),
right: Math.min(bounds.right, maskBounds.right),
bottom: Math.min(bounds.bottom, maskBounds.bottom)
};
}

return bounds;
};

function diffBounds(bounds, shiftedBounds) {
shiftedBounds = shiftedBounds || bounds;
return {
left: shiftedBounds.left - bounds.left,
right: shiftedBounds.right - bounds.right,
top: shiftedBounds.top - bounds.top,
bottom: shiftedBounds.bottom - bounds.bottom
};
}

/**
* Computes expected bounds based on scaling specified in the layer name
* TODO: Document further
*/
Generator.prototype.getPixmapParams = function (settings, innerBounds, outerBounds) {
var params = { boundsOnly: settings.boundsOnly },
targetScale = settings.scale,
targetWidth = settings.width,
targetHeight = settings.height,
outerWidth = outerBounds.right - outerBounds.left,
outerHeight = outerBounds.bottom - outerBounds.top,
boundsShift = diffBounds(innerBounds, outerBounds),
shiftX = boundsShift.right - boundsShift.left,
shiftY = boundsShift.bottom - boundsShift.top;

var expectedWidth,
expectedHeight;

var scaleX,
scaleY;

if (settings.scaleMode === "relative") {
if (targetScale) {
scaleX = scaleY = targetScale;
expectedWidth = outerWidth * scaleX;
expectedHeight = outerHeight * scaleY;

params.scaleX = scaleX;
params.scaleY = scaleY;
}
else if (targetWidth || targetHeight) {
if (targetWidth) {
scaleX = targetWidth / outerWidth;
if (!targetHeight) {
scaleY = scaleX;
}
}
if (targetHeight) {
scaleY = targetHeight / outerHeight;
if (!targetWidth) {
scaleX = scaleY;
}
}

// In case of non-uniform transforms, the effects are not scaled
if (scaleX !== scaleY) {
scaleX = (targetWidth - shiftX) / (outerWidth - shiftX);
scaleY = (targetHeight - shiftY) / (outerHeight - shiftY);
expectedWidth = ((outerWidth - shiftX) * scaleX) + shiftX;
expectedHeight = ((outerHeight - shiftY) * scaleY) + shiftY;
} else {
expectedWidth = outerWidth * scaleX;
expectedHeight = outerHeight * scaleY;
}

params.scaleX = scaleX;
params.scaleY = scaleY;
}
else {
expectedWidth = outerWidth;
expectedHeight = outerHeight;
}
}
else if (settings.scaleMode === "absolute" || !settings.scaleMode) {
var inputRect,
outputRect,
left,
top;

if (settings.scale) {
targetWidth = outerWidth * settings.scale;
targetHeight = outerHeight * settings.scale;
}

if (targetWidth || targetHeight) {
if (targetWidth) {
scaleX = targetWidth / outerWidth;
if (!targetHeight) {
scaleY = scaleX;
targetHeight = outerHeight * scaleY;
}
}
if (targetHeight) {
scaleY = targetHeight / outerHeight;
if (!targetWidth) {
scaleX = scaleY;
targetWidth = outerWidth * scaleX;
}
}

// In case of non-uniform transforms, the effects are not scaled
if (scaleX !== scaleY) {
left = outerBounds.left - boundsShift.left;
top = outerBounds.top - boundsShift.top;

inputRect = {
left: left,
top: top,
right: left + outerWidth - shiftX,
bottom: top + outerHeight - shiftY
};
outputRect = {
left: left,
top: top,
right: left + targetWidth - shiftX,
bottom: top + targetHeight - shiftY
};

expectedWidth = (outputRect.right - outputRect.left) + shiftX;
expectedHeight = (outputRect.bottom - outputRect.top) + shiftY;
} else {
left = outerBounds.left - boundsShift.left;
top = outerBounds.top - boundsShift.top;

inputRect = {
left: left,
top: top,
right: left + outerWidth - shiftX,
bottom: top + outerHeight - shiftY
};
outputRect = {
left: 0,
top: 0,
right: targetWidth - shiftX * scaleX,
bottom: targetHeight - shiftY * scaleY
};

expectedWidth = (outputRect.right - outputRect.left) + shiftX * scaleX;
expectedHeight = (outputRect.bottom - outputRect.top) + shiftY * scaleY;
}
}
else {
expectedWidth = outerWidth;
expectedHeight = outerHeight;
}

if (inputRect) { params.inputRect = inputRect; }
if (outputRect) { params.outputRect = outputRect; }
}

params.expectedWidth = expectedWidth;
params.expectedHeight = expectedHeight;

return params;
};

/**
* @param pixmap An object representing the layer's image
* @param {!integer} pixmap.width The width of the image
Expand Down
3 changes: 1 addition & 2 deletions lib/jsx/getLayerPixmap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ actionDescriptor.putEnumerated(

if (params.boundsOnly) {
actionDescriptor.putBoolean(stringIDToTypeID("boundsOnly"), params.boundsOnly);
} else if (params.bounds) {
actionDescriptor.putBoolean(stringIDToTypeID("bounds"), params.bounds);
}
actionDescriptor.putBoolean(stringIDToTypeID("bounds"), params.bounds);

executeAction(stringIDToTypeID("sendLayerThumbnailToNetworkClient"), actionDescriptor, DialogModes.NO);

0 comments on commit 371a9a3

Please sign in to comment.