Index of /sandbox/cliechti/aafigure

[ICO]NameLast modifiedSizeDescription

[DIR]Parent Directory  -
[TXT]Makefile.docutils-update30-Dec-2009 11:46 293
[TXT]aa.py30-Dec-2009 11:46 2.9K
[   ]aa.pyc05-Sep-2008 20:27 3.4K
[IMG]aafigure-0.svg30-Dec-2009 11:46 588
[IMG]aafigure-1.svg30-Dec-2009 11:46 903
[IMG]aafigure-2.svg30-Dec-2009 11:46 6.5K
[IMG]aafigure-3.svg30-Dec-2009 11:46 814
[IMG]aafigure-4.svg30-Dec-2009 11:46 3.1K
[IMG]aafigure-5.svg30-Dec-2009 11:46 3.7K
[IMG]aafigure-6.svg30-Dec-2009 11:46 77K
[IMG]aafigure-7.svg30-Dec-2009 11:46 18K
[IMG]aafigure-8.svg30-Dec-2009 11:46 2.2K
[IMG]aafigure-9.svg30-Dec-2009 11:46 1.8K
[IMG]aafigure-10.svg30-Dec-2009 11:46 1.2K
[IMG]aafigure-11.svg30-Dec-2009 11:46 720
[IMG]aafigure-12.svg30-Dec-2009 11:46 3.5K
[IMG]aafigure-13.svg30-Dec-2009 11:46 766
[IMG]aafigure-14.svg30-Dec-2009 11:46 4.9K
[IMG]aafigure-15.svg30-Dec-2009 11:46 5.6K
[IMG]aafigure-16.svg30-Dec-2009 11:46 12K
[IMG]aafigure-17.svg30-Dec-2009 11:46 5.4K
[IMG]aafigure-18.svg30-Dec-2009 11:46 7.2K
[IMG]aafigure-19.svg30-Dec-2009 11:46 4.6K
[IMG]aafigure-20.svg30-Dec-2009 11:46 6.9K
[IMG]aafigure-21.svg30-Dec-2009 11:46 17K
[IMG]aafigure-22.svg30-Dec-2009 11:46 4.5K
[IMG]aafigure-23.svg30-Dec-2009 11:46 6.4K
[IMG]aafigure-24.svg30-Dec-2009 11:46 15K
[IMG]aafigure-25.svg30-Dec-2009 11:46 24K
[TXT]aafigure.py30-Dec-2009 11:46 36K
[   ]aafigure.pyc30-Dec-2009 11:46 41K
[   ]aafigure_directive.py30-Dec-2009 11:46 4.5K
[   ]aafigure_directive.pyc30-Dec-2009 11:46 3.9K
[IMG]benford.svg30-Dec-2009 11:46 7.9K
[TXT]makefile30-Dec-2009 11:46 293
[TXT]pil.py30-Dec-2009 11:46 4.1K
[   ]pil.pyc30-Dec-2009 11:46 5.5K
[TXT]rst2html.py30-Dec-2009 11:46 777
[   ]svg.py30-Dec-2009 11:46 6.0K
[   ]svg.pyc30-Dec-2009 11:46 7.8K

aafigure README

aafigure README

Overview

The idea is to parse ASCII art images, embedded in reST documents and output an image. This would mean that simple illustrations could be embedded as ASCII art in the reST source and still look nice when converted to e.g. HTML

Implementation

Files:

aafigure.py
ASCII art parser. This is the main module.
aafigure_directive.py
Implmements the aafigure Docutils directive that takes these ASCII art figures and generates a drawing.
aa.py
ASCII art output backend. Intended for tests, not for the end user.
pil.py
Bitmap output backend. Using PIL, it can write PNG, JPEG and more formats.
rst2html.py
Patched version that adds the aafigure Docutils directive.
svg.py
SVG output backend.

The aafigure module contains code to parse ASCII art figures and create a list of of shapes. The different output modules can walk trough a list of shapes and write image files.

Usage

./rst2html.py README.txt >README.html

This results in the README.html file and a .svg file for each aafigure.

Display the resulting README.html file in a SVG capable browser. It has been tested with Firefox 1.5.

Short introduction

This code in a reST document that is processed with the enhanced rst2html.py looks like this:

.. aafigure::

    -->

Which results in an image like this:

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 68)

Unknown directive type "aafigure".

.. aafigure::

    -->

The aafigure directive has the following options:

Lines

The - and | are normaly used for lines. _ can also be used. It is a slightly longer line than the - and it's is drawn a bit lower. = gives a thicker line. The later two line types can only be drawn horizontaly.

---- |         ___
     | --  ___|     ===

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 116)

Unknown directive type "aafigure".

.. aafigure::

  ---- |         ___
       | --  ___|     ===


It is also possible to draw diagonal lines. Their use is somewhat restricted tough. Not all cases work as expected.

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 125)

Unknown directive type "aafigure".

.. aafigure::

                                     +
      |  -  +   |  -  +   |  -  +   /               -
     /  /  /   /  /  /   /  /  /   /     --     |/| /    +
    |  |  |   +  +  +   -  -  -   /     /  \        -   \|/  |\
                                 +     +    +          +-+-+ | +
    |  |  |   +  +  +   -  -  -   \     \  /        -   /|\  |/
     \  \  \   \  \  \   \  \  \   \     --     |\| \    +
      |  -  +   |  -  +   |  -  +   \               -
                                     +

And drawing longer diagonal lines with different angles looks ugly...

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 139)

Unknown directive type "aafigure".

.. aafigure::

    +      |
     \    /
      \  /
       --


Arrows

Arrow styles are:

--->   | | | | | |
---<   | | | | | |
---o   ^ V v o O #
---O
---#

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 157)

Unknown directive type "aafigure".

.. aafigure::

    --->   | | | | | |
    ---<   | | | | | |
    ---o   ^ V v o O #
    ---O
    ---#

Boxes

Boxes are automaticaly draw when the edges are made with +, filled boxes are made with X (must be at least two units high or wide). It is also possibe to make rounded edges in two ways:

+-----+   XXX  /--\     --   |
|     |   XXX  |  |    /    /
+-----+   XXX  \--/   |   --

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 175)

Unknown directive type "aafigure".

.. aafigure::

    +-----+   XXX  /--\     --   |
    |     |   XXX  |  |    /    /
    +-----+   XXX  \--/   |   --

Fills

Upper case characters generate shapes with borders, lower case without border. Fills must be at least two characters wide or high. (This reduces the chance that it is detected as Fill instead of a string)

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 188)

Unknown directive type "aafigure".

.. aafigure::

    A   B   C   D   E   F   G   H   I   J   K   L   M
     AA  BB  CC  DD  EE  FF  GG  HH  II  JJ  KK  LL  MM
     AA  BB  CC  DD  EE  FF  GG  HH  II  JJ  KK  LL  MM

     aa  bb  cc  dd  ee  ff  gg  hh  ii  jj  kk  ll  mm
     aa  bb  cc  dd  ee  ff  gg  hh  ii  jj  kk  ll  mm

    N   O   P   Q   R   S   T   U   V   W   X   Y   Z
     NN  OO  PP  QQ  RR  SS  TT  UU  VV  WW  XX  YY  ZZ
     NN  OO  PP  QQ  RR  SS  TT  UU  VV  WW  XX  YY  ZZ

     nn  oo  pp  qq  rr  ss  tt  uu  vv  ww  xx  yy  zz
     nn  oo  pp  qq  rr  ss  tt  uu  vv  ww  xx  yy  zz

Complex shapes can be filled:

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 206)

Unknown directive type "aafigure".

.. aafigure::

    CCCCC     C         dededede
     C  CCCC  CC        dededede
     CC    CCCCC        dededede

Text

The images may contain text too. There are different styles to enter text:

direct

Bby default are repeated characters detected as fill:

Hello World  dd d
                d

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 223)

Unknown directive type "aafigure".

.. aafigure::

    Hello World  dd d
                    d

quoted

Text between quotes has piority over any graphical meaning:

"Hello World"  dd d
                  d

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 235)

Unknown directive type "aafigure".

.. aafigure::

    "Hello World"  dd d
                      d

", ' and \` are all valid quotation marks. The quotes are not visible in the resulting image. This not only disables fills (see below), it also treats -, | etc as text.

textual option

The :textual: option disables horizontal fill detection. Fills are only detected when they are verticaly at least 2 charcacters high:

Hello World  dd d
                d

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 252)

Unknown directive type "aafigure".

.. aafigure::
    :textual:

    Hello World  dd d
                    d


Other

* { }

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 266)

Unknown directive type "aafigure".

.. aafigure::

    * { }



TODO

License

BSD

Tests

To compare input and output, look at the sources of this file.

Simple tests

Different arrow types:

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 330)

Unknown directive type "aafigure".

.. aafigure::

    <-->  >->   --> <--
    >--<  o-->  -->+<--
    o--o          o=>

Boxes and shapes:

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 338)

Unknown directive type "aafigure".

.. aafigure::

    +---------------+
    |A box with text|
    +---------------+

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 344)

Unknown directive type "aafigure".

.. aafigure::

        ---> | ^|   |   +++
        <--- | || --+-- +++
        <--> | |V   |   +++<-
     __             __    ^
    |  |__  +---+  |__|   |
            |box|   ..
            +---+  Xenophon


Flow chart

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 357)

Unknown directive type "aafigure".

.. aafigure::
    :textual:

        /---------\
        |  Start  |
        \----+----/
             |
             V
        +----+----+
        |  Init   |
        +----+----+
             |
             +<-----------+
             |            |
             V            |
        +----+----+       |
        | Process |       |
        +----+----+       |
             |            |
             V            |
        +----+----+  yes  |
        |  more?  +-------+
        +----+----+
             | no
             V
        /----+----\
        |   End   |
        \---------/


UML

No not realy, yet. But you get the idea.

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 391)

Unknown directive type "aafigure".

.. aafigure::
    :scale: 0.8

    +---------+  +---------+  +---------+
    |Object 1 |  |Object 2 |  |Object 3 |
    +----+----+  +----+----+  +----+----+
         |            |            |
         |            |            |
         X            |            |
         X----------->X            |
         X            X            |
         X<-----------X            |
         X            |            |
         X            |            |
         X------------------------>X
         |            |            X
         X----------->X            X---+
         X            X            X   |
         |            |            X<--+
         X<------------------------X
         X            |            |
         |            |            |
         |            |            |

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 415)

Unknown directive type "aafigure".

.. aafigure::
    :scale: 0.8

    +---------+         +---------+     +---------+
    |  Shape  |         |  Line   |     |  Point  |
    +---------+         +---------+   2 +---------+
    | draw    +<--------+ start   +----O+ x       |
    | move    +<-+      | end     |     | y       |
    +---------+   \     +---------+     +---------+
                   \
                    \   +---------+
                     +--+ Circle  |
                        +---------+
                        | center  |
                        | radius  |
                        +---------+

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 432)

Unknown directive type "aafigure".

.. aafigure::

                             /-----------\     yes /----------\
                          -->| then this |--->*--->| and this |
                      +  /   \-----------/    |no  \----------/
     /------------\   +--                     |
     | First this |-->+                       |
     \------------/   +--                     |
                      +  \   /---------\      V        /------\
                          -->| or that |----->*------->| Done |
                             \---------/               \------/

Electronics

It would be cool if it could display simple schematics.

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 448)

Unknown directive type "aafigure".

.. aafigure::
    :fill: #fff

          Iin +-----+      Iout
        O->---+ R1  +---o-->-----O
       |      +-----+   |         |
    Vin|       100k   ----- C1    | Vout
       |              ----- 100n  |
       v                |         v
        O---------------o--------O

  • Capacitor not good, would prefer --||-- -> symbol detection

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 464)

Unknown directive type "aafigure".

.. aafigure::

       |/|       |\|       | |     +---+       e|
    ---+ +---  --+ +--   --+ +--  -+   +-    b|/
       |\|       |/|       | |     +---+    --+
                                              |\
       |        |           |        |         c|
      -+-      -+-         -+-      +++
      / \      \ /                  | |    -   -
      -+-      -+-         -+-      | |    c\ /e
       |        |           |       +++     -+-
                                     |       |b

  • Diodes OK
  • Caps not optimal. Too far appart in image, not very good recognisable in ASCII. Space cannot be removed as the two + signs would be connected otherwise. The schematic below uses an other style.
  • Arrows in transistor symbols can not be drawn

Here is a complete circuit with different parts:

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 487)

Unknown directive type "aafigure".

.. aafigure::
    :fill: #fff
    :scale: 0.8
    :textual:

                         Q1  _  8MHz
                           || ||
                      +----+| |+----+
                      |    ||_||    |
                      |             |
                +-----+-------------+-----+
                |    XIN           XOUT   |
                |                         |
                |                    P3.3 +--------------+
    SDA/I2C O---+ P2.0                    |              |
                |                         |             e|
                |        MSP430F123       |   +----+  b|/  V1
    SCL/I2C O---+ P2.1               P3.4 +---+ R1 +---+   PNP
                |                         |   +----+   |\
                |           IC1           |      1k     c|    +----+
                |                         |              o----+ R3 +---O TXD/RS232
                |    VCC             GND  |              |    +----+
                +-----+---------------+---+              |      1k
                      |               |                  |    +----+
                      |               |                  +----+ R2 +---O RXD/RS232
                      |               |                       +----+
                      |               |                         10k
    GND/I2C O---o-----+----o----------o-----------o--------------------O GND/RS232
                |     |    |   C1     |           |   C2
               =+=    |  ----- 1u     |         ----- 10u
                      |  ----- 5V +---+---+     ----- 16V
                      |    |      |  GND  |       |            D1|/|
                      +----o------+out  in+-------o----------o---+ +---O RTS/RS232
                                  |  3V   |                  |   |\|
                                  +-------+                  |
                                   IC2                       | D2|/|
                                                             +---+ +---O DTR/RS232
                                                                 |\|


Timing diagrams

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 529)

Unknown directive type "aafigure".

.. aafigure::
    :aspect: 0.5

      ^    ___     ___           ____
    A |___|   |___|   |_________|    |______
      |      ___        ___           __
    B |_____|   |______|   |________XX  XX__
      |
      +-------------------------------------> t

Here is one with descriptions:

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 541)

Unknown directive type "aafigure".

.. aafigure::

                        SDA edge
         start                              stop
           |    |          |                 |
           v    v          v                 v
        ___      __________                   ___
    SDA    |    |          |                 |
           |____|          |_____..._________|
        ______      _____       _..._       _____
    SCL       |    |     |     |     |     |
              |____|     |_____|     |_____|

              ^    ^     ^     ^     ^     ^
              |    |     |     |     |     |
              | 'sh_in'  |  'sh_in'  |  'sh_in
           'sh_out'   'sh_out'    'sh_out'

                        SCL edge

Statistical diagrams

Benfords distribution of the sizes of files on my harddrive:

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 568)

Unknown directive type "aafigure".

.. aafigure::
    :name: benford
    :foreground: #ff1050
    :aspect: 0.7

      |
    1 +------------------------------------------------------------> 31.59%
    2 +-------------------------------> 16.80%
    3 +-----------------------> 12.40%
    4 +-----------------> 9.31%
    5 +--------------> 7.89%
    6 +-----------> 6.10%
    7 +---------> 5.20%
    8 +---------> 4.90%
    9 +--------> 4.53%
      |         +         |         +         |         +         |
      +---------+---------+---------+---------+---------+---------+--->
      |         +         |         +         |         +         |
      0         5        10        15        20        25        30

Just some bars:

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 590)

Unknown directive type "aafigure".

.. aafigure::
    :fill: #00b

    ^     2
    |    EE
    | 1  EE       4
    |DD  EE   3  HH
    |DD  EE  GG  HH
    |DD  EE  GG  HH
    +------------------>


Schedules

System Message: ERROR/3 (./sandbox/cliechti/aafigure/README.txt, line 605)

Unknown directive type "aafigure".

.. aafigure::

    "Week"      |  1    |  2    |  3    |  4    |  5    |
    ------------+----------------------------------------
    "Task 1"    |HHHH
    "Task 2"    |    EEEEEEEEEEEE
    "Task 3"    |                GGGGGGGGZZZZZZZZZZ
    "Task 4"    |DD      DD          DD          DD