Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/genpieces.py
blob: f4d84853e8fbffcae1906857f699a4e6ff8db10a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#Copyright (c) 2009-11 Walter Bender

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import os


class SVG:
    ''' SVG generators '''

    def __init__(self):
        self._scale = 1
        self._stroke_width = 1
        self._fill = '#FFFFFF'
        self._stroke = '#000000'

    def _svg_style(self, stroke=False, extras=""):
        if stroke:
            return "%s%s%s%s%s%f%s%s%s" % ("style=\"fill:", self._fill,
                                           ";stroke:", "#000000",
                                           ";stroke-width:",
                                           self._stroke_width, ";", extras,
                                           "\" />\n")
        else:
            return "%s%s%s%s%s%f%s%s%s" % ("style=\"fill:", self._fill,
                                           ";stroke:", self._stroke,
                                           ";stroke-width:",
                                           self._stroke_width, ";", extras,
                                           "\" />\n")

    def _svg_text(self, x, y, font_size, text_string, stroke=False,
                  center=False):
        if center:
            align = 'text-align:center;text-anchor:middle'
        else:
            align = ''
        svg_string = "<text x=\"%d\" y=\"%d\" " % (x, y)
        if stroke:
            svg_string += "style=\"font-size:%dpx;font-weight:bold;font-family:Sans;fill:%s;\
stroke:#000000;%s\">" % (font_size, self._stroke, align)
        else:
            svg_string += "style=\"font-size:%dpx;font-weight:bold;font-family:Sans;fill:%s;\
%s\">" % (font_size, self._stroke, align)
        svg_string += "<tspan x=\"%d\" y=\"%d\">%s</tspan></text>" % \
            (x, y, text_string)
        return svg_string

    def _svg_line(self, x1, y1, x2, y2):
        svg_string = "<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\"\n" % \
                      (x1, y1, x2, y2)
        svg_string += self._svg_style("stroke-linecap:square;")
        return svg_string

    def _svg_rect(self, w, h, rx, ry, x, y, stroke=False):
        svg_string = "       <rect\n"
        svg_string += "          width=\"%f\"\n" % (w)
        svg_string += "          height=\"%f\"\n" % (h)
        svg_string += "          rx=\"%f\"\n" % (rx)
        svg_string += "          ry=\"%f\"\n" % (ry)
        svg_string += "          x=\"%f\"\n" % (x)
        svg_string += "          y=\"%f\"\n" % (y)
        self.set_stroke_width(1.0)
        svg_string += self._svg_style(stroke=stroke)
        return svg_string

    def _background(self, scale, stroke=False):
        return self._svg_rect(79.5 * scale, 59.5 * scale, 1, 1, 0.25, 0.25,
                              stroke=stroke)

    def header(self, scale=1, background=True, stroke=False):
        svg_string = "<?xml version=\"1.0\" encoding=\"UTF-8\""
        svg_string += " standalone=\"no\"?>\n"
        svg_string += "<!-- Created with Emacs -->\n"
        svg_string += "<svg\n"
        svg_string += "   xmlns:svg=\"http://www.w3.org/2000/svg\"\n"
        svg_string += "   xmlns=\"http://www.w3.org/2000/svg\"\n"
        svg_string += "   version=\"1.0\"\n"
        svg_string += "%s%f%s" % ("   width=\"", scale * 80 * self._scale,
                                  "\"\n")
        svg_string += "%s%f%s" % ("   height=\"", scale * 60 * self._scale,
                                  "\">\n")
        svg_string += "%s%f%s%f%s" % ("<g\n       transform=\"matrix(",
                                      self._scale, ",0,0,", self._scale,
                                      ",0,0)\">\n")
        if background:
            svg_string += self._background(scale, stroke=stroke)
        return svg_string

    def footer(self):
        svg_string = "</g>\n"
        svg_string += "</svg>\n"
        return svg_string

    def set_scale(self, scale=1.0):
        self._scale = scale

    def set_colors(self, colors):
        self._stroke = colors[0]
        self._fill = colors[1]

    def set_stroke_width(self, stroke_width=1.0):
        self._stroke_width = stroke_width


def generate_card(string='a', colors=['#FF0000', '#FFFFFF'],
                  background=True, scale=1, stroke=False, center=False,
                  font_size=40):
    svg = SVG()
    svg.set_scale(scale)
    svg.set_colors(colors)
    svg_string = svg.header(background=background, stroke=stroke)
    if center:
        x = 40
    else:
        x = 5
    svg_string += svg._svg_text(x, 45, font_size, string, stroke=stroke,
                                    center=center)
    svg_string += svg.footer()
    return svg_string


def open_file(datapath, filename):
    return file(os.path.join(datapath, filename), "w")


def close_file(f):
    f.close()


def generator(datapath):
    i = 1
    filename = "tile-%d.svg" % (i)
    f = open_file(datapath, filename)
    f.write(generate_card(string='b', background=False))
    close_file(f)


def main():
    return 0

if __name__ == "__main__":
    if not os.path.exists(os.path.join(os.path.abspath('.'), 'images')):
        os.mkdir(os.path.join(os.path.abspath('.'), 'images'))
    generator(os.path.join(os.path.abspath('.'), 'images'))
    main()