I use Adobe Illustrator extensively and automate it with AppleScript. I recently made a children’s toy where I need to expand letter font objects (so I can cut them with the laser cutter) and offset the expanded objects by 0.6mm (so I can cut a mortise with the CNC router for the letters to fit into). The design for the letter P looks like this with the orange part to become the laser-cutter letter and the black stroke to become the mortise. I also add pictures of the result with the letter outside and inside the mortise.
I automated this with GUI scripting in Illustrator (see appendix below) and want to code this with an open source library so it is more accessible to anyone who wants to make such a toy with custom letters.
I have tried SVGwrite as suggested here, but I was unable to expand type. I found no answers whether Cairo would allow type expansion and path offset. One solution is to use Inkscape, but I would rather keep the code self-contained by importing a library rather than requiring the installation of an external program.
Do you know of a Python library that allows type expansion and path offset?
Appendix: Here is the only solution I found to automate this. I use AppleScript and Illustrator was with GUI interaction:
-- Code to interact with Illustrator via GUI property ai_process : "Adobe Illustrator" on clickMenuBarItemAndItem(menuBarItem, menuItem) tell application "System Events" tell application process ai_process tell menu bar 1 tell menu bar item menuBarItem tell menu 1 tell menu item menuItem click end tell end tell end tell end tell end tell end tell end clickMenuBarItemAndItem -- Code to expand object on expand() -- no expand command in API, so expand with GUI (this becomes a group) my clickMenuBarItemAndItem("Object", "Expand...") tell application "System Events" tell application process ai_process delay 1 key code 36 -- enter end tell end tell end expand -- Code to offset path my clickMenuBarItemAndTwoItems("Object", "Path", "Offset path...") tell application "System Events" tell application "Adobe Illustrator" to activate keystroke "0.6mm" delay 0.1 key code 36 -- enter delay 0.1 end tell
I think this is difficult in Python and I used Inkscape, which can expand font objects (called “object to path”), add stroke to paths, and allows running commands from the command-line. For example, this Python script expands and offsets the letter P:
import os import subprocess # Set directory filepath = os.path.expanduser('~/letter.svg') character = "P" # Initial plain SVG with the letter letter_text = ('<text ' + 'fill="black" ' + ('style="' + ( ("font-size:67px;" + "font-family:Arial Rounded MT Bold;" + "stroke:black;" + "stroke-width:2px;" # round linejoin avoids protruding shards in lower case cursive o and z + "stroke-linejoin:round;" ) + '"') + (' x="10" y="100" ') + ">" + character + "</text>") ) svg_text = ('<?xml version="1.0" encoding="utf-8" ?><svg height="200px" width="200px"><defs />' + letter_text + "</svg>") # Write to disk with open(filepath, "w+") as f: f.write(svg_text) # Object to path (Inkscape) or expand (Illustrator) # https://stackoverflow.com/questions/15203650/programmatically-convert-svg-shapes-to-paths-lineto-moveto # You'll need the path to inkscape, e.g. # /Applications/Inkscape.app/Contents/Resources/bin/inkscape # or /usr/local/bin/inkscape inkscape_command = ("/usr/local/bin/inkscape -f '%s'" % filepath + " --verb EditSelectAll " + " --verb ObjectToPath " + " --verb FileSave " + " --verb FileQuit") #shell = True is important in this call, otherwise it fails subprocess.check_output(inkscape_command, shell = True)
The result is this vector graph:
Inkscape also allows writing extensions in Python, so maybe that could be a solution in Python.