Skip to content

Instantly share code, notes, and snippets.

@hilukasz
Created June 7, 2012 20:06
Show Gist options
  • Save hilukasz/2891259 to your computer and use it in GitHub Desktop.
Save hilukasz/2891259 to your computer and use it in GitHub Desktop.
// objects and methods
function Container(theChild){
this.value = theChild; // reference to single child container
this.symbolItems = []; //array of symbols
this.parent = [];
}
//$.writeln(mySymbolContainer.parent);
//start script
var subLayers = app.activeDocument.layers.getByName("parent").pageItems;
var smallestParentArea = 10000000;
var smallestChild = "this is the first smallest child found";
var parentSmallest = [];
var usedContainerIndexes = [];
smallestChildrenCollection = [];
tempChildToParentRelationship = [];
//OOP object that stores SYMBOLS/PARENT/CHILD info
var myContainer = new Container(childContainer);
// CHILD LOOP : loop through all layers to find children : i
for(var i = 0; i < subLayers.length; i++){
var smallestChild,
childContainer = subLayers[i],
childContainerArea = childContainer.width * childContainer.height;
//OOP stuffz
myContainer.value.push(childContainer);
print("#################"+childContainer.name+" : CHILD compare ###################");
// PARENT LOOP : compare call portential parents to outer children loop : j
for(var j = 0; j < subLayers.length; j++){
var parentContainer = subLayers[j],
parentArea = parentContainer.width * parentContainer.height;
if (i != j){
print("-----"+parentContainer.name+" : PARENT compair -----");
if(isIn(childContainer, parentContainer) && childContainerArea < parentArea && usedContainerIndexes.indexOf(j) === -1) {
print(">> TEST :: "+childContainer.name+": SMALLEST CHILD // area:"+childContainerArea+" // "+smallestChild+" (index: "+j+") USED TO BE SMALLEST CHILD");
smallestChild = childContainer;
smallestChildrenCollection.push(childContainer);
//smallestChildrenContainers.push();
smallestParentArea = parentArea;
usedContainerIndexes.push(j);
parentSmallest.push(parentContainer);
//come back to this, making an array of children to associated smallest parent
//print("Child: "+parentSmallest+" Parent: "+smallestChild);
//print("parent is:"+parentContainer.name+"\n");
}
}
}
}
function findSmallestObject() {
}
function isIn(child, parentItem){
var childVB = child.visibleBounds,
childLeft = childVB[0],
childTop = Math.abs(childVB[1]),
childRight = childVB[2],
childBottom = Math.abs(childVB[3]);
var parentVB = parentItem.visibleBounds,
parentLeft = parentVB[0],
parentTop = Math.abs(parentVB[1]),
parentRight = parentVB[2],
parentBottom = Math.abs(parentVB[3]);
$.write(child.name+" : [ left: "+childVB[0]+" , top: "+Math.abs(childVB[1]) +" , right: "+childVB[2] +", bottom: "+Math.abs(childVB[3])+"] \n\n" );
$.write(parentItem.name+" : [ left: "+parentVB[0]+", top: "+Math.abs(parentVB[1]) +" , right: "+parentVB[2] +" , bottom: "+Math.abs(parentVB[3])+"] \n\n" );
//return true if child is in parent
return childLeft >= parentLeft &&
childTop >= parentTop &&
childRight <= parentRight &&
childBottom <= parentBottom;
}
//check what the smallest container is:
//accepts an array of parent items
function theSmallestParent(parentToCheckForSmallest){
var theSmallestParent;
for(var i = 0; i < parentToCheckForSmallest.length; i++){
var smallestParent;
var parent = parentToCheckForSmallest[i];
var parentArea = parentToCheckForSmallest[i].height*parentToCheckForSmallest[i].width;
//$.writeln("\n\n"+parentToCheckForSmallest[i].name+" %% PARENT NAME %%, area: "+parentArea);
for(var j = 0; j < parentToCheckForSmallest.length; j++){
if (i != j){ //don't check against itself
var child = parentToCheckForSmallest[j];
var childArea = parentToCheckForSmallest[j].height*parentToCheckForSmallest[j].width;
//$.writeln(parentToCheckForSmallest[j].name+" %% CHILD NAME %%, area: "+childArea);
if(childArea < parentArea){
//$.writeln(">>>>"+parentToCheckForSmallest[j].name+" is now the smallest");
smallestParent = child;
}
}
}
theSmallestParent = smallestParent;
}
return theSmallestParent;
}
$.writeln("!!! "+smallestChild.name+" is the smallest conatiner. it's position is: "+smallestChild.visibleBounds+"\n \n"+" items with children: "+parentSmallest);
//$.write("\n\n smallestparent:"+smallestParent);
//$.writeln("\n\n"+parentSmallest)
//$.writeln("\n\n"+layersArray+" new array");
//print("\n\n"+smallestParent);
print("smallest parent: "+theSmallestParent(parentSmallest));
print("Parent: "+parentSmallest+" Child: "+smallestChild);
//print(childToParentRelationship+" smallest Child"+smallestChild);
//clear variables because ES is stupid sometimes
var childContainer = null,
childContainerArea = null,
parentArea = null,
parentContainer = null,
isSame = null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment