3

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.

Design of letter type with expansion and path offset Acrylic laser cut letter outside the CNC-cut mortise Acrylic laser cut letter inside the CNC-cut 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
  • 1
    As a workaround, in Python script you can use fonts preliminary converted to SVG format, there are online converters, for example everythingfonts.com/ttf-to-svg – emax Apr 13 '18 at 11:46
  • 1
    I'm not sure you want a library for Ilustrator specificaly but, give a look at rsvg, svgpathtools, both libraries to manipulate SVG images, maybe you can even take the process out of illustrator with time. – RomuloPBenedetti Apr 14 '18 at 8:39
  • @emax: regarding ttf-to-svg, rendering a font as SVG is not my bottleneck, as I can use svgwriteto render fonts in SVG like this: import svgwrite dwg = svgwrite.Drawing('test.svg', size = ("300px", "400px")) letter = "P" dwg.add(dwg.text(letter, insert = (60, 220), fill = "black", style = ("font-size:192pt;font-family:Arial Rounded MT Bold;"))) dwg.save() – miguelmorin Apr 21 '18 at 15:01
  • @emax: and ttf-to-svg failed to convert my font with a message "Failed to convert the file from ttf to svg." What I need is to expand the <text> tag into paths and offset those paths by 3mm. – miguelmorin Apr 21 '18 at 15:08
  • @RomuloPBenedetti: I don't think RSVG can do it because I searched in the manual for (rsvg expand)[developer.gnome.org/search?q=rsvg+expand] and for (object to path)[developer.gnome.org/search?q=object+to+path] and both return "no results." SvgPathtools does work to offset paths, called "parallel curve" (in this PIP page)[pypi.org/project/svgpathtools/]. I was unable to find how to do "object to path" or "expand". I could add another program to convert letters into SVG files, but I doubt that people would use it if they need to install several libraries. – miguelmorin Apr 22 '18 at 18:35
3

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:

Vector graph of expanded and offset letter

Inkscape also allows writing extensions in Python, so maybe that could be a solution in Python.

Inkscape can offset paths (called "offset path", "outset path", or "dynamic offset object"), but it has issues mentioned in this thread, so I prefer doing it with stroke width.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.