Converts stroke points generated by https://www.calligrapher.ai/ into a single-stroke SVG format, enabling easy conversion to GCODE for plotting.
Requires svgpathtools
and numpy
.
The fitCurves folder is a submodule, so make sure to include the --recurse-submodules
flag when git clone
'ing this repo.
https://www.calligrapher.ai/ provides an easy way to generate handwriting for arbitrary text. I wanted to be able to send that information to my 3D printer and use it as a plotter to write in a way that mimics human handwriting. However, while there are lots of programs to turn an SVG file (which the website will generate) into GCODE, there were several issues:
- The website's SVG output outlines and then fills each stroke, so converted GCODE will trace the outline of the strokes, which does not mimic human handwriting.
- The original code repo it's ported from does output stroke lines (based on the provided example outputs), but it's so old that I couldn't figure out how to get it to compile/run after several hours of trying.
- I tried a couple updated forks of that repo and couldn't get them to run either (my complete lack of ML coding experience probably contributed).
- One of the forks ported to Rust has an easy-to-use Windows executable pre-built, but has the same issue as the website and generates filled outline shapes. And I don't know Rust, so I couldn't modify that.
- The Javascript on calligrapher.ai is obfuscated, so you can't inspect or modify its behavior unless you reverse-engineer the whole thing. I even tried looking on Wayback Machine for older un-obfuscated copies, but there were none.
So finally I figured out a hacky way to extract the point arrays generated by the Javascript on the site. The points can be saved as JSON and imported into this program which will convert them into to a plotter/engraving-friendly SVG. Then other standard workflows can be used to convert that into GCODE that you can send to your printer.
Currently the text generation and JSON point array extraction must be done manually; at some point in the future I may attempt to automate that using Selenium or something.
- Navigate your web browser to https://www.calligrapher.ai/ and generate some text.
- Open up the browser's Developer Tools (Firefox/Chrome: right click->Inspect) and go to the Console tab.
- Type
JSON.stringify(tr)
into the Console and hit Enter.tr
is the variable in the Javascript that stores the AI-generated point array
- Copy the JSON output to a file.
- If The output begins and ends with double quotes (
"
, default in Firefox) you will need to remove them and also replace every instance of\"
with"
to make it valid JSON. - If the output begins and ends with single quotes (
'
, default in Chrome) you just need to remove them to make it valid JSON.
- If The output begins and ends with double quotes (
- Feed that file into this program with
callig2svg.py <json_file_name>
. This will placeoutput.svg
in the output folder.- See
callig2svg.py --help
for more options.
- See
- Follow this tutorial to convert the generated SVG into GCODE using Inkscape.
If you don't mind the uniform look of a font that mimicks handwriting, you can skip this repo entirely and just generate GCODE from an engraving font in Inkscape.