How to automatically place a label on visual center of nested shapes?

Community Beginner ,
Jan 02, 2021 Jan 02, 2021

Copy link to clipboard

Copied

Hi, I’ve written a JavaScript for Adobe Illustrator to automatically add text labels to shapes. The problem is that the shapes have sometimes odd forms (like U f.e.) it’s therefore hard to find the visual center of the shapes lying inside the shape to place the text label. I found the following algorithm: A new algorithm for finding a visual center of a polygon | by Mapbox | maps for developers which does the job to some extend (N.B.: some “porting” of the code has been required to use it in Illustrator, see  Solved: Re: ‘require’ keyword to import modules in Illustr... - Adobe Support Community - 11708866 )  

The result of using this algo is still not satisfying especially in case of “nested” shapes like for example:

nested.jpg

This is NOT because the mentioned algo is not good enough but rather because my script does a ‘for loop’ over each shape but there is currently no detection mechanism to check if there is another (the outermost) shape lying inside the current shape. Such a inner shape would determine the ‘hole’ inside the current shape which shall be added to the overall coordinated of the current shapes (in GeoJSON-like format ) to get better results with the mentioned algorithm. I wonder if somebody has overcome this problem? I would be grateful if you could share the (JavaScript) code you've been using to solve this problem! 

TOPICS
Scripting

Views

130

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Community Beginner , Jan 10, 2021 Jan 10, 2021
Hi femkeblanco, thank you for your reply! I tried to automatically convert my (nested) paths to compound paths using the "Pathfinder Tool" but I couldn’t find a convenient method (some of the paths disappear when converting, which is expected behavior). And to answer your question: No, my existing script doesn’t work on compound paths (I didn’t really investigate why…). Anyway, your reply inspired me to adapt my existing script, unfortunately I encountered a new problem: “How to check if a shape...

Likes

Translate

Translate
Enthusiast ,
Jan 02, 2021 Jan 02, 2021

Copy link to clipboard

Copied

Will the algorithm work for a given polygon with a "hole" (a compound path in Illustrator)?  I suspect it won't.  But if it did, it's simply a matter of making each path a compound path with a copy of the path in front. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 10, 2021 Jan 10, 2021

Copy link to clipboard

Copied

LATEST

Hi femkeblanco, thank you for your reply! I tried to automatically convert my (nested) paths to compound paths using the "Pathfinder Tool" but I couldn’t find a convenient method (some of the paths disappear when converting, which is expected behavior). And to answer your question: No, my existing script doesn’t work on compound paths (I didn’t really investigate why…). Anyway, your reply inspired me to adapt my existing script, unfortunately I encountered a new problem: “How to check if a shape is inside another curved shape?” I’ll post a new ticket. Thanks again, kind regards!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines