Source

evolve-keyboard-layout / svg_layouts.py

#!/usr/bin/env python3

u"""Write svg images using pySVG."""

# all imports used in any test
try: 
    from pysvg.builders import *
    from pysvg.core import *
    from pysvg.filter import  *
    from pysvg.filter import *
    from pysvg.gradient import *
    from pysvg.linking import *
    from pysvg.script import *
    from pysvg.shape import *
    from pysvg.structure import *
    from pysvg.style import *
    from pysvg.text import *
except ImportError:
    raise ImportError(u"""You need pySVG for python3 for this script to run. Either get it from http://code.google.com/p/pysvg/ and run `2to3 -w *; python3 setup.py install` or get a copy of the converted Mercurial repository from Arne Babenhauserheide (http://draketo.de).""")

def HelloWorld1():
    s = svg()
    t = text(u"Hello World", 0, 100)
    s.addElement(t)
    print s.getXML()
#    s.save('./testoutput/1_HelloWorld1.svg')

def colorwheel(idx=0, palette=u"red_to_blue"):
    u"""get a color by index, going from red to blue.
    @param idx: color index between 0 and 1020.
    255,0,0
    255,255,0
    0,255,0
    0,255,255
    0,0,255
    """
    if idx < 0:
        raise ValueError(u"idx outside the valid range between 0 and 1020" + unicode(idx))
    if palette == u"red_to_blue": 
        if idx<=255: return 255, idx, 0
        if idx<=510: return 510-idx, 255, 0
        if idx<=765: return 0,255,idx-510
        if idx<=1020: return 0,1020-idx,255
        raise ValueError(u"idx outside the valid range between 0 and 1020" + unicode(idx))
    if palette == u"grey":
        if idx<=255: return 255-idx, 255-idx, 255-idx
        raise ValueError(u"idx outside the valid range between 0 and 255" + unicode(idx))

def add_line(d, color=(255,0,0), xy0=(0,0), xy1=(200,400), width=3, upstroke=True, opacity=1.0):
    u"""Draw a single curved line.

    @param S: S = svd(name)
    @param d: d = defs()
    """

    dx = xy1[0] - xy0[0]
    to_right = dx > 0
    dy = xy1[1] - xy0[1]
    upwards = dy < 0

    if to_right: 
        color_id = u"r" + u"_".join([unicode(c) for c in color]) + u"_" + unicode(opacity)
    elif not dx and upwards:
        color_id = u"u" + u"_".join([unicode(c) for c in color]) + u"_" + unicode(opacity)
    elif not dx and not upwards:
        color_id = u"d" + u"_".join([unicode(c) for c in color]) + u"_" + unicode(opacity)
    else: 
        color_id = u"l" + u"_".join([unicode(c) for c in color]) + u"_" + unicode(opacity)
    color_string = u"rgb(" + u",".join([unicode(c) for c in color]) + u")"

    lg = linearGradient()
    lg.set_id(color_id)
    s = stop(offset=u"0%")
    s.set_stop_color(color_string)
    if to_right or not dx and not upwards: 
        s.set_stop_opacity(0.1)
    else: s.set_stop_opacity(opacity)
    lg.addElement(s)
    s = stop(offset=u"100%")
    s.set_stop_color(color_string)
    if to_right or not dx and not upwards: 
        s.set_stop_opacity(opacity)
    else: s.set_stop_opacity(0.1)
    lg.addElement(s)
    if not dx:
        tb = TransformBuilder()
        tb.setRotation(90)
        lg.set_gradientTransform(tb.getTransform())
    d.addElement(lg)

    sh=StyleBuilder()
    sh.setFilling(u'none')
    sh.setStroke(u'url(#' + color_id + u')')
    sh.setStrokeWidth(unicode(width)+u'px')

    start = u"M " + u",".join([unicode(p) for p in xy0])
    
    path3=path(start, style=sh.getStyle())

    if not upstroke and upwards:
        control_y = 0.2*(dy)
    elif not upstroke and not upwards:
        control_y = 1.4*(dy)
    elif upstroke and upwards:
        control_y = 1.4*(dy)
    else: 
        control_y = 0.2*(dy)
    
    # make sure we always have movement up or down.
    if not control_y and upstroke:
        control_y = -0.2 * abs(dx)
    elif not control_y and not upstroke:
        control_y = 0.2 * abs(dx)
    
    path3.appendQuadraticCurveToPath(
        0.5*(dx), control_y, # control point, x, y
        dx, dy # target
        )
    
    return path3


### Self-Test ###

if __name__ == u"__main__": 

    S = svg(u"Belegung")
    d = defs()
    S.addElement(d)
    
    S.addElement(add_line(d))
    S.addElement(add_line(d, upstroke=False))
    S.addElement(add_line(d, color=(255,0,0), xy0=(200,400), xy1=(100,300), width=6, upstroke=True))
    for i in xrange(100):
        color=colorwheel(10*i)
        S.addElement(add_line(d, color=color, xy0=(30*i,3*i), xy1=(30*(i+0.5),3*(i+1)), width=i, upstroke=i%2 == 0))
    print S.getXML()