#!/usr/bin/python

"""Screensaver that demonstrates simple raw-xlib 70's-style
   vector text."""

import xss_base
import optparse
import os
import string
import random
import sys

# 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# 
# target points:
# 
# x:  0 w/2 w  Y:
#     1  2  3  0
#
#           a  h/4
#
#     8  9  4  h/2
#
#           b  3h/4
#
#     7  6  5  h

def target_offset(code, w, h):
    """turn code into offset"""
    if code in [1, 2, 3]:
        y = 0
    elif code in [4, 8, 9]:
        y = h/2
    elif code in [5, 6, 7]:
        y = h
    elif code in [0xa]:
        y = h/4
    elif code in [0xb]:
        y = 3*h/4
    else:
        sys.exit("invalid code %s" % code)
    if code in [1, 8, 7]:
        x = 0
    elif code in [2, 6, 9]:
        x = w/2
    elif code in [3, 4, 5, 0xa, 0xb]:
        x = w
    else:
        sys.exit("invalid code %s" % code)
    return (x, y)

turtles = {
    "A": "7824845",
    "B": "712a989b67",
    "C": "32865",
    "D": "12ab671",
    "E": "3189875",
    "F": "318987",
    "G": "31876b49",
    "H": "178435",
    "I": "26",
    "J": "3568",
    "K": "178385",
    "L": "175",
    "M": "71935",
    "N": "7153",
    "O": "24682", # weak, need curves
    "P": "712a98",
    "Q": "682a4b5b6",
    "R": "712a9895",
    "S": "3189b67",
    "T": "1326",
    "U": "18643",
    "V": "163",
    "W": "17953",
    "X": "15 73",
    "Y": "19396",
    "Z": "1375",

    "0":"24682", # weak, need curves (same as O)
    "1":"126",
    "2":"12a975",
    "3":"12a989b67",
    "4":"184a5",
    "5":"3189b67", # actually S
    "6":"32876b98",
    "7":"13a96",
    "8":"12a9b678981",
    "9":"812a9798",
    }

def draw_letter(window, gc, x, y, letter):
    """draw a letter, the hard way"""
    
    w = 30
    h = 50
    if letter in turtles:
        trail = turtles[letter]
        for stepfrom, stepto in zip(trail, trail[1:]):
            if stepfrom == " ":
                continue
            if stepto == " ":
                continue
            xfrom, yfrom = target_offset(int(stepfrom, 16), w, h)
            xto,   yto   = target_offset(int(stepto,   16), w, h)
            window.line(gc, x+xfrom, y+yfrom, x+xto, y+yto)

# note: if
# /usr/share/python-support/python-xlib/Xlib/xobject/drawable.py
# line 117 (Drawable.line) says PolSegment... fix it to say PolySegment
# (sourceforge is already correct... in 2003:
#   revision 1.13
#   date: 2003/07/11 20:49:33;  author: petli;  state: Exp;  lines: +7 -7
#   Fix typo
# but 0.12-5.1 from xandros (or debian) is wrong...)


class draw_words(xss_base.xss_base):
    """draw words in random places, with vectors"""

    delay = 2.5

    def __init__(self, wordlist):
        xss_base.xss_base.__init__(self)
        self.sentences = map(string.strip, file(wordlist))

    def render(self):
        phrase = random.choice(self.sentences)
        # TODO: loop
        self.window.clear_area(0, 0, 0, 0)
        for idx, letter in enumerate(phrase):
            draw_letter(self.window, self.gc, idx*33, 5, letter)

if __name__ == "__main__":
    parser = optparse.OptionParser(usage=__doc__)
    parser.add_option("--noisy", action="store_true")
    options, args = xss_base.parse_args(parser)
    assert os.getenv("XSCREENSAVER_WINDOW"), os.environ
    if options.window_id:
        assert os.getenv("XSCREENSAVER_WINDOW") == options.window_id, options.window_id

    noisy = options.noisy
    wordfile, = args
    saver = draw_words(wordfile)
    saver.run()
