Skip to content

Instantly share code, notes, and snippets.

@hilukasz
Created June 8, 2012 01:20
Show Gist options
  • Save hilukasz/2892838 to your computer and use it in GitHub Desktop.
Save hilukasz/2892838 to your computer and use it in GitHub Desktop.
#target illustrator
function init(){
// @include lukaszLibrary.jsx
// objects and methods
function Container(theChild){
this.value = theChild; // reference to single child container
this.symbolItems = []; //array of symbols
this.parent = [];
}
// set up vars
var smallestChild = "this is the first smallest child found",
parentSmallest = [],
smallestChildrenCollection = [],
tempChildToParentRelationship = [],
smallestParentArea = 10000000,
subLayers = app.activeDocument.layers.getByName("parent").pageItems;
// CHILD LOOP
for(var i = 0; i < subLayers.length; i++){
var smallestChild,
usedContainerIndexes = [],
childContainer = subLayers[i],
childContainerArea = childContainer.width * childContainer.height,
collectionOfParents = [],
myContainer;
print("#################"+childContainer.name+" : CHILD compare ###################");
// PARENT LOOP CHECK
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 -----");
// SET PARENT AND CHILD RELATIONSHIP VARIABLES
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 its parent is: "+parentContainer);
myContainer = new Container(childContainer);
smallestChild = childContainer;
smallestChildrenCollection.push(childContainer);
smallestParentArea = parentArea;
usedContainerIndexes.push(j);
//usedChildContainerIndexes.push(i);
parentSmallest.push(parentContainer);
// push multiple instances of parent container to smallest container class
collectionOfParents.push(parentContainer);
}
}
} // END : PARENT
myContainer.parent.push(collectionOfParents);
myContainer.value = smallestChild;
print("$$$$$$$$$$$$ SMALLEST CHILD FOR OBJECT: "+smallestChild);
} //END : CHILD
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;
}
print("\n ---- \n !!! "+smallestChild.name+" is the smallest conatiner. it's position is: "+smallestChild.visibleBounds+"\n \n"+" items with children: "+parentSmallest);
print("\n ---- \nsmallest parent: "+theSmallestParent(parentSmallest));
print("\n ---- \nParent: "+parentSmallest+" Child: "+smallestChild);
print("\n ---- \n children collection"+smallestChildrenCollection);
printObj(myContainer);
var childContainer = null,
childContainerArea = null,
parentArea = null,
parentContainer = null,
isSame = null;
} init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment