GRAM - A Simple Diagram Creator

Mike Parr. 5/Sep/2013 .... mikeparr@live.com

History

I got frustrated with drawing simple diagrams using e.g. Word. I could not find a very simple text-based tool either. (Yes, I have read about the pic little language, which was possible). I looked at svg and decided to use it, so wrote a program to create svg from my diagram instructions.

I know that shape drawing is a classic OO task, but decided to implement a non-OO version at first. This was a mistake, as the code is a complete mess. But it does what I want, and I cannot be bothered to re-write it.

It is open-source, written in Delphi - around 1000 lines - would probably run with lazarus or Free Pascal - it does not use a gui. It runs from the Windows command line, but I imagine it would be simple to run under Gnu/Linux.

Running It

Eg:

gram infile.txt  outfile.xml

The infile has gram instructions, the outfile can be viewed by a web browser - you can copy/paste the image from the browser (and shrink/enlarge as well).

Sample files and pictures are provided with this doc - see elsewhere.

Instructions in Gram

The drawing area has 0,0 at the top left. A typical small diagram might be e.g 300 by 300 units.

For shapes, you provide the coordinates of their centres, This makes it easy to line them up. They expand to show your required text, but the centre does not move. So your first task is to sketch the diagram (I use paper) and choose numbers for the centres of each shape.

Here are the shapes. Their first letter denotes the kind of shape.

box

It surrounds your given text with a rectangle. Note that each line of your text is centred, not left-justified.

Start the box name with a 'b', then any letters, digits, e.g:

b12  box1  bHeading

Provide 2 absolute coords for centre.

Provide 1 line of text, or several lines, ending them by 'endtext'. 2 examples:

b23 300 400 text The black cat

b21 333 450 lines
The cat
sat on the mat.
endtext

Use 'text' for 1 line (after the word 'text') or use 'lines'. The numbers can be negative (only useful if you use 'set' to shift the origin).

ibox

Invisible box. Start with 'i', e.g iHeader, i234

Use like a box, but the rectangle is not drawn:

ititle 234 456 text My Picture

circle

Start with a 'c', e,g circle, c66. Use like box.

c66 444 555 text some words

point

Start with a 'p'. Like a circle, with no text given. You can use this to join lines.

p88 500 800

line

Joins 2 shapes. You do NOT provide numbers. Instead, you refer to shape names. Here is how we join 2 circles:

line c66 cBig

The word 'line' is compulsory. For circles, just use their name. (They must have been created before the line instruction is obeyed.)

For boxes, use a '.' to precede a point on the side of the box, as in:

t0      t1      t2      t3      t4/r0              (top)
l0

l1                              r1
(left)                          (right)

l2                              r2


l3                              r3


l4/                              r4/
b0      b1      b2      b3      b4                (bottom )     

So:

line c66  b44.b0

draws from a circle to the bottom left corner of a box. There are 5 points on an edge.

The edges are named t b l r - top bottom left right. If we omit the number, as in b44.t the line is drawn to the midpoint of the edge (like t2).

A circle or point cannot have a dotted pedge position.

arrow / midarrow

Along with line, we have arrow and midarrow.

Arrow has an arrow at the end of the line (the 'to' end.)

midarrow has an arrow in the middle.

fixedline

For special shapes (e.g. triangles) there is a fixedline, where you specify the 4 absolute cooridinates x1 y1 x2 y2:

fixedline 22 33  444 555

Set commands

There are a few set commands:

set xorigin 100
set yorigin 200

These change the top left coords to your chosen value.

set fontsize 14

The initial font size is 16. You can change this. - it is untested.

set scale 90

shrinks the distance between shapes by a percentage (set initially to 100.