Saturday, June 17, 2006

 

Sometimes, You Need a Blunt Instrument

I have a very complex script that does a lot of automatic page composition. One of the elements it has to work with is an inline group of figures with a caption. During the process of importing the images into this group, the group changes size. Because of the way the group is constructed, it is impossible to keep the inline sitting properly on the baseline as this happens.

So, the solution is to move it back inline. Well, I already had a function at hand that does this:
function moveInlineToBaseline(theGraph) {
var myBase = theGraph.parent.baseline;
var myBounds = theGraph.geometricBounds;
theGraph.move(undefined, [0, myBase - myBounds[2]]);
theGraph.parent.parentStory.recompose();
}
I wrote this for any inline graphic; hence the argument name theGraph. But for some reason, this logic failed on one instance in a very long document. The move command threw up an error to the effect that the object couldn't be moved by the requested amount because it would fall outside the bounding box of the containing frame. This was not true based on my measurements and examining the data, and I cannot see anything wrong with the logic, but attempting some debugging in ESTK, I discovered that the group in question couldn't be moved at all. Clearly, something odd was going on, but what?

Well, sometimes you just have to bite the bullet and try some other approach. In this case, I went for the Cut/Paste blunt instrument approach:
function moveInlineToBaseline(theGraph) {
var myBase = theGraph.parent.baseline;
var myBounds = theGraph.geometricBounds;
try {
theGraph.move(undefined, [0, myBase - myBounds[2]]);
} catch (e) {
// I don't know why this should ever fail, but I have an example, so let's go at it a different way in this case
var myIPindex = theGraph.parent.index;
var myStory = theGraph.parent.parentStory;
app.select(theGraph);
app.cut();
app.select(myStory.insertionPoints[myIPindex]);
app.paste();
myStory.recompose();
return
}
theGraph.parent.parentStory.recompose();
}
And that worked. With any luck, that Cut/Paste path through the script will never again be used, but if it is, I can be confident it will work because it solved my problem.

Note that when you paste a frame or group inline, it sits on the baseline properly, even if, as in this case, it was not on the baseline when it was cut. That's because I selected the object and not the character containing the object. Had I done the latter, the position relative to the baseline would have been part of the cut information.

Comments:
Since I'm always loathe to destroy the contents of my clipboard unless absolutely necessary, it appears that you can accomplish the same blunt instrument appoach without the cut/paste by toggling the object's anchoredPosition from inlinePosition to anchored and then back to inlinePosition.
 
Caleb,

This doesn't always work, as I'm now discovering. I think what can happen is that if a group changes size because of the moving of an element within the group, the change in height can be missed by the composer.

Dave
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?