Copy link to clipboard
Copied
I'm developing an Illustrator script (JSX) to automate text placement on various artboards. My goal is to position text precisely based on its actual visual size (glyph bounds), not the text frame's bounding box, which includes inconsistent padding.
My script is constantly misaligned because:
TextFrame.position and geometricBounds always reference the outer bounding box of the text frame.
If I change the text content (TextFrame.contents), the bounding box height changes, but the text is still misaligned relative to the bounding box's top edge.
I've tried calculating the required scale factor using a temporary TextFrame.createOutline() object to find the glyph height, and that works for scaling the font size. However, I still can't find a reliable way to calculate the precise (x,y) coordinates for the final TextFrame.position (which is the bottom-left corner) so that the visible top of the text aligns with a specific coordinate.
Is there a reliable way in Illustrator scripting to:
Get the Y offset from the top of the Text Frame Bounding Box to the top of the first line's visible glyph?
Or, is there a property that allows positioning based on the visual text content itself, similar to how the Area Type bounding box can be adjusted relative to the visible text?
Any guidance on accurately positioning the live text frame based on the scaled glyph size would be greatly appreciated!"
Copy link to clipboard
Copied
Hans,
I believe you can use this combination in the process,
1) Effect > Path > Outline Object,
2) General Preferences > Tick Use Preview Bounds.
including a reversal of both, to avoid their causing unforeseen issues with unrelated things.
This is a way to bypass the fundamental alignment of live Type.
You can say that it is the reverse of Snap to Glyph,
https://helpx.adobe.com/illustrator/using/snap-to-glyph.html
so you can also consider whether there is a way to use that in reverse.
Or consider a feature request to that effect.
Copy link to clipboard
Copied
Hi @hans_4621, I've written a function that does this. Pass it a TextRange and it will give you an object with `fontBounds` and `bounds`. See this post for the script listing.
- Mark
Edit: in case it wasn't obvious, to get the distance between the top left of the text frame and the top left of the first glyph, you can get the bounds of the first character using my function and subtract the text frame's top left which will give you the distance as [dx, dy]. Something like this:
(function () {
var doc = app.activeDocument;
var tf = doc.textFrames[0];
var delta = getGlyphDistanceFromTextFrameTopLeft(tf);
// for demonstration only, draw the delta
drawRectangle(doc, [tf.position[0], tf.position[1], tf.position[0] + delta[0], tf.position[1] + delta[1]], { fillColor: makeColor([255, 0, 0]), opacity: 30 });
})();
/**
* Returns the distance between the first glyph of `textFrame`, and the top,left of `textFrame`.
* @param {TextFrame} textFrame - an Illustrator TextFrame.
* @returns {Array<Number>} - [dx, dy].
*/
function getGlyphDistanceFromTextFrameTopLeft(textFrame) {
var dimensions = getTextBounds(textFrame.textRange.characters[0]);
if (!dimensions)
throw new Error('getGlyphDistanceFromTextFrameTopLeft: bad `textFrame` supplied.');
return [
dimensions.bounds[0] - textFrame.geometricBounds[0],
dimensions.bounds[1] - textFrame.geometricBounds[1],
];
};
(Note: above code must be used with the functions in the original script.)
Copy link to clipboard
Copied
I think it would help if Illustrator had alignment functions that allowed a selected text object to be aligned to other objects via its baseline. Currently this has to be done manually with the aid of Smart Guides or other snapping functions.
Aligning text objects via the letter's physical shape (aka glyph bounds) work's only so well. The approach is fine with clean, sans serif typefaces like Helvetica. A live dummy letter like "E" can be scaled to desired size (using font height options) and aligned to another object accurately since the bottom and top edges of the letter are perfectly horizontal. Actual type can replace the dummy "E" once it's in the right position. Most other typefaces that are not geometric or "grotesque" in nature routinely have parts of glyphs overshoot below the baseline and rise above the cap height line (or even fall short of the cap height line). Script typefaces are especially challenging in that regard.
I thought it should be simple for graphics software to be able to size and position lettering in font files according to capital letter size or lowercase letter size since font files have built in dimensions for baseline, cap height line, x-height (lowercase), ascender and descender. Just take in the defined distance in UPM numbers from the baseline to cap height line and use that. I assumed sign making applications like Flexi used this approach. After opening some existing font files in font editing software I learned many fonts don't always have their glyphs aligning with those boundaries. Some fonts have the tops of their capital letters undershoot the cap height line by considerable distances. The type designer probably had good reasons to set those dimension lines like that. Unfortunately it complicates the situation for sizing and aligning letters according to physical glyph size.
Copy link to clipboard
Copied
I agree @Bobby Henderson it would be great to have access to all the font metrics, converted into document or artboard coordinates, including sidebearings. It is a pity that we can't even get the x,y coordinates for the anchor point of a point text item!
Copy link to clipboard
Copied
Illustrator’s scripting API doesn’t expose glyph bounds directly for live text — only frame or geometric bounds. The most accurate workaround is your current method: duplicate the text, run `createOutline()`, measure the path bounds, and use that to calculate the vertical offset. Then delete the outline and adjust your live text’s `position` based on that offset. Unfortunately, there’s no native property to align by visual glyph height.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now