Hi Marc,
I tried the modified version of of the script and it gave the same results as we discussed before. for ease, I have already attached a sample document and hope that it will fulfill the purpose for testing. Hope to see this through.
Regards,
Aman
Hi Aman,
1. The sample document contains aboveLine anchors (not only pure inline.) I found that in such case rescaling to the height of the text frame is not sufficient to wake up the ghost object. An advanced product could surely work around this using smart calculations and so. But here I will consider the inline mode as a requirement, so the version below just converts any aboveLine anchor into inline. (Recall that custom anchored objects are ignored.)
2. Smart text reflow is not properly configured in the sample document. AFAIK, the master spread (applied to page 1) should have primary text frames, which should be chained due to facing pages, and the frame on page 1 should instantiate the master frame. The other pages can be removed by the script as a first step.
Then the code below worked for me.
Again, we may provide an automated check-and-repair mechanism regarding the issues above, but it's not technically interesting (and quite boring to implement), so better is to let the user prepare the input document as specified. (A simple document preset would solve this once for all.)
function fixAndDownscaleInlines( q,doc,t,wso,n,items,chars,z,c,o,aos,k,wh,xy,x,y)
//----------------------------------
// [180621] Force inline anchoring when aboveLine is found.
// [180619] Added `document.recompose()` to force smart reflow to update.
// This function proportionnaly downscale every *inline* or
// *aboveLine* anchored item found in the document, so that:
// 1. Target width does not exceed parent frame width (PFW.)
// 2. Targets whose width is *under* PFW are not touched.
// 3. Objects whose height exceeds the parent frame height (then causing
// overset issues that SmartTextReflow can't fix) are temporarily
// downscaled so that the algorithm can eat them. When available back,
// those objects are adjusted to the frame height unless width adjustment
// is required instead (to prevent them from violating rule 1.)
// Note: Smart Text Reflow must be turned on and properly set.
{
// Cache and constants.
// ---
q = (callee.Q||callee.Q={});
const EPSILON = 1e-3,
DOWN = 100, // Downscaling factor
ANCH = +AnchorPosition.ANCHORED,
ABOV = +AnchorPosition.ABOVE_LINE,
INLN = +AnchorPosition.INLINE_POSITION,
// ---
CPAR = +CoordinateSpaces.parentCoordinates,
CINN = +CoordinateSpaces.innerCoordinates,
BVSB = +BoundingBoxLimits.outerStrokeBounds,
// ---
PAR0 = [+AnchorPoint.topLeftAnchor, BVSB],
PAR1 = [+AnchorPoint.bottomRightAnchor, BVSB],
INN0 = PAR0.concat(CPAR),
INN1 = PAR1.concat(CPAR),
MATX = [1,0,0,1, 0,0];
// Checkpoint.
// ---
if( !(doc=app.properties.activeDocument) )
{ alert("No document!"); return; }
if( !(t=doc.textPreferences).properties.smartTextReflow )
{ alert("SmartTextReflow must be turned on!"); return; }
if( !(t=doc.pages[0].appliedMaster) )
{
if( !confirm("No applied master on first page. Fix this?") ) return;
t = doc.pages[0].appliedMaster = doc.masterSpreads[0];
}
if( !t.primaryTextFrame )
{ alert("No primary frame on spread "+t.name+"."); return; }
// [ADD180621] Prepare text reflow.
// ---
doc.pages.itemByRange(1,-1).remove();
t.properties = {
addPages: +AddPageOptions.END_OF_STORY,
deleteEmptyPages: true,
limitToMasterTextFrames: true,
preserveFacingPageSpreads: true,
};
// Get anchored items from all stories, and the respective parent char.
// ---
t = doc.stories.everyItem().texts.everyItem().pageItems;
if( !t.length ){ alert("No anchored item."); return; }
items = (t=t.everyItem()).getElements(); // array of anchored items
chars = t.parent; // array of anchor characters
// Safe scaling options are definitely needed.
// ---
t = +WhenScalingOptions.ADJUST_SCALING_PERCENTAGE;
wso = +app.transformPreferences.whenScaling;
wso==t ? (wso=0) : (app.transformPreferences.whenScaling=t);
// [180618] Patch. Looping here from top to bottom (in
// each story stream) we try to restore missing bounding boxes
// due to 'formal heights' being higher than the parent frame.
// A flag is then set in q[_id] to save those special cases.
// ---
for( n=items.length, z=-1 ; ++z < n ; )
{
// `o` is a PageItem which typically contains
// a Graphic which typically controls a Link.
// ---
o = items
; if( o.properties.visibleBounds ) continue;
// `o` has no parent text frame because it's too high!
// Let's temporarily apply a hard downscaling to it.
// ---
if( !(x=o.properties.horizontalScale) ) continue;
if( !(y=o.properties.verticalScale) ) continue;
o.horizontalScale = x/DOWN; // 1%
o.verticalScale = y/DOWN; // 1%
q['_'+o.id] = 1; // flag
}
// [180619] Force recomposition now.
// ---
doc.recompose();
// Scan each anchor character and its respective parent frame (if any!)
// [REM] Original reason for looping upwards: "Get as many parent frames
// as possible while rescaling." Sounds pointless now, but keeping the
// logic won't hurt.
// ---
for( z=0 ; (c=chars.pop()) && (o=items.pop()) ; )
{
// c in overset text -> no parent available.
// ---
if( !(t=(c.parentTextFrames||0)[0]) ) continue;
// Filtering conditions on `o` (you may add your own.)
// Here we check that the anchor is of inline/aboveLine kind.
// ---
aos = o.properties.anchoredObjectSettings||0;
if( (!aos) || ANCH==+aos.anchoredPosition ) continue;
// [FIX180621] Force inline.
// ---
aos.anchoredPosition = INLN;
// Visible width and height of the text frame (if not in cache yet.)
// ---
if( !(wh=q[k=t.id]) )
{
wh = q
= t.resolve(PAR1,CINN)[0]; xy = t.resolve(PAR0,CINN)[0];
wh[0] -= xy[0];
wh[1] -= xy[1];
}
// Temporarily store the (w,h) of `o` (in parent space.)
// ---
t = o.resolve(INN1,CPAR)[0].concat(o.resolve(INN0,CPAR)[0]);
t[0] -= t[2];
t[1] -= t[3];
t.length = 2;
// The x factor is required in either case.
// ---
x = wh[0]/t[0];
// If `o` has been previously downscaled to make it show up,
// we need to find the *smallest fit factor*.
// ---
y = q['_'+o.id] ? wh[1]/t[1] : false;
// If `o` hasn't been downscaled, prevent it from being upscaled.
// ("Width shorter than the frame width -> do nothing.")
// ---
if( (!y) && 1 <= x+EPSILON ) continue;
// Rescale.
// ---
y && y < x && (x=y);
MATX[0] = MATX[3] = x;
o.transform(CPAR,INN0,MATX);
++z;
}
wso && (app.transformPreferences.whenScaling=wso);
alert( "Processed items: " + z);
};
app.doScript(fixAndDownscaleInlines, void 0, void 0, UndoModes.entireScript, "FixAndDownscaleInlines");
@+
Marc
Sign up
Already have an account? Login
To post, reply, or follow discussions, please sign in with your Adobe ID.
Sign inSign in to Adobe Community
To post, reply, or follow discussions, please sign in with your Adobe ID.
Sign inEnter your E-mail address. We'll send you an e-mail with instructions to reset your password.
