Microsoft Hackathon - PhoTex
Idea
A programming language that allows tech savvy graphic designers develop scalable vector graphics using plain text code. The designer can create a canvas and declare various elements that are to be rendered on that canvas. They can also define and name custom fonts, colors, and complex objects that can be reused in their project.
A video we made about the project containing more information and a coding tutorial: https://www.youtube.com/watch?v=-G7Mk0YNnjw
The designer’s code (stored in a .ptex
file) is interpretted by PhoTeX and an SVG representation of their design is generated. The resulting image can be outputted in many common image file formats.
Inspiration
There does not exist a good universal text-based tool for creating general purpose vector and raster graphics. Almost all existing tools are GUI-based visual editors and thus have several limitations that could be overcome by using a WYSIWYM approach. For example, in GUI-based tools like Photoshop and Illustrator, it can be a lot of work to bring in assets you have created for other projects that you wish to name, reuse, and customize.
We were inspired by visualization and formatting tools such as LaTeX, Graphviz, Markdown and ORG. Our goal is to build a similar style tool, but with less steep of a learning curve. It will be purpose built to create highly customizable graphics that export to any format. Moreover, we want to build a system that automates some aspects of graphic design that Photoshop and other visual tools cannot.
How It Works
We decided to break the problem into two parts, the parser and the exporter. The parser will read in the plain text .ptex
file into an abstract syntax tree and check the semantics of the user’s program. This approach will allow us to deliver the user meaninful error messages in the cases of synactic and semantic errors. If the parser doesn’t find any semantic or syntax errors it will pass a compiled hierarchy of graphical elements (shapes, images, text, etc.) to the exporter. The exporter will convert the parse tree into a set of hierarchically grouped primitive objects which can then be easily be converted to SVG format. Lastly, the exporter will convert the resulting svg into the final file output type (.png
, .jpg
, .svg
, etc).
We are currently using the Lark parser (https://github.com/lark-parser/lark). Lark is an open-source python library that enables us to build our abstract syntax trees based on an EBNF-like context-free grammar. To assist in building and exporting SVG files, we are using SVGLib (https://github.com/deeplook/svglib).
Getting Started
- If you haven’t already, install Python 3.x.x (https://www.python.org/)
- Open a terminal window and navigate to the project root
- [Optional] Create a new virtual environment
- Run
pip -r requirements.txt
in the terminal to acquire all pip dependencies - Navigate to the
src
folder - Create a
.ptex
file and write your code! - Build your code by running
python photex.py [YOUR_FILE_NAME].ptex
- If there were no errors, your output files should be located in the same folder as your code