1 import math
, cmath
, copy
, new
, sys
2 import defaults
, svg
, glyphs
, trans
, curve
4 ############################### class Fig
6 class Fig(trans
.Delay
):
21 _varlist
= ["xmin", "xmax", "ymin", "ymax", "xlogbase", "ylogbase", "x", "y", "width", "height", "flipx", "flipy", "clip"]
23 def __init__(self
, *args
, **kwds
):
24 trans
.Delay
.__init
__(self
, *args
, **kwds
)
26 for var
in self
._varlist
:
28 self
.__dict
__[var
] = kwds
[var
]
32 raise TypeError, "Unrecognized keywords " + ", ".join(map(lambda word
: "\"%s\"" % word
, kwds
.keys()))
38 if len(self
.children
) == 1: ren
= ""
40 if self
.clip
: clip
= " clip"
41 return "<Fig (%d child%s) xmin=%s xmax=%s ymin=%s ymax=%s%s>" % (len(self
.children
), ren
, self
.xmin
, self
.xmax
, self
.ymin
, self
.ymax
, clip
)
43 def transform(self
, t
):
44 t
= svg
.cannonical_transformation(t
)
45 x1
, y1
= t(self
.x
, self
.y
)
46 x2
, y2
= t(self
.x
+ self
.width
, self
.y
+ self
.height
)
47 self
.x
, self
.y
= x1
, y1
48 self
.width
, self
.height
= x2
- x1
, y2
- y1
50 def bbox(self
): return defaults
.BBox(self
.x
, self
.x
+ self
.width
, self
.y
, self
.y
+ self
.height
)
53 if self
.xmin
is not None and self
.xmax
is not None and \
54 self
.ymin
is not None and self
.ymax
is not None:
55 self
.trans
= trans
.window(self
.xmin
, self
.xmax
, self
.ymin
, self
.ymax
,
56 x
=self
.x
, y
=self
.y
, width
=self
.width
, height
=self
.height
,
57 xlogbase
=self
.xlogbase
, ylogbase
=self
.ylogbase
,
58 minusInfinityX
=(self
.x
- 10.*self
.width
), minusInfinityY
=(self
.y
- 10.*self
.height
),
59 flipx
=self
.flipx
, flipy
=self
.flipy
)
63 self
._svg
= new
.instance(svg
.SVG
)
64 self
._svg
.__dict
__["tag"] = "g"
65 self
._svg
.__dict
__["attrib"] = self
.attrib
66 self
._svg
.__dict
__["_svg"] = self
._svg
68 self
._svg
.__dict
__["children"] = []
69 for child
in self
.children
:
70 self
._svg
.__dict
__["children"].append(trans
.transform(self
.trans
, child
))
73 clipPath
= svg
.SVG("clipPath", id=svg
.randomid("clip-"))(svg
.SVG("rect", self
.x
, self
.y
, self
.width
, self
.height
))
74 self
._svg
["clip-path"] = "url(#%s)" % clipPath
["id"]
75 self
._svg
= svg
.SVG("g", clipPath
, self
._svg
)
77 def __getstate__(self
):
78 mostdict
= copy
.copy(self
.__dict
__)
79 if self
.trans
is not None:
81 transcode
= self
.trans
.func_code
, self
.trans
.func_name
84 return (sys
.version_info
, defaults
.version_info
, mostdict
, transcode
)
86 def __setstate__(self
, state
):
87 self
.__dict
__ = state
[2]
88 self
.__dict
__["trans"] = []
89 if state
[3] is not None:
92 if "z" in code
.co_names
:
93 context
.update(cmath
.__dict
__)
95 context
.update(math
.__dict
__)
96 self
.__dict
__["trans"] = new
.function(code
, context
)
97 self
.__dict
__["trans"].func_name
= name
99 self
.__dict
__["trans"] = None
101 def __deepcopy__(self
, memo
={}):
102 mostdict
= copy
.copy(self
.__dict
__)
103 del mostdict
["trans"]
104 if "repr" in mostdict
: del mostdict
["repr"]
105 output
= new
.instance(self
.__class
__)
106 output
.__dict
__ = copy
.deepcopy(mostdict
, memo
)
107 output
.__dict
__["trans"] = self
.trans
109 memo
[id(self
)] = output
113 bbox
= defaults
.BBox(None, None, None, None)
114 for child
in self
.children
:
117 if self
.xmin
is not None: bbox
.xmin
= self
.xmin
118 if self
.xmax
is not None: bbox
.xmax
= self
.xmax
119 if self
.ymin
is not None: bbox
.ymin
= self
.ymin
120 if self
.ymax
is not None: bbox
.ymax
= self
.ymax
122 self
.trans
= trans
.window(bbox
.xmin
, bbox
.xmax
, bbox
.ymin
, bbox
.ymax
,
123 x
=self
.x
, y
=self
.y
, width
=self
.width
, height
=self
.height
,
124 xlogbase
=self
.xlogbase
, ylogbase
=self
.ylogbase
,
125 minusInfinityX
=(self
.x
- 10.*self
.width
), minusInfinityY
=(self
.y
- 10.*self
.height
),
126 flipx
=self
.flipx
, flipy
=self
.flipy
)
128 ############################### class Canvas
134 def __init__(self
, width
, height
, *args
, **kwds
):
135 Fig
.__init
__(self
, *args
, **kwds
)
142 if len(self
.children
) == 1: ren
= ""
143 return "<Canvas (%d child%s) width=%g height=%g xmin=%s xmax=%s ymin=%s ymax=%s>" % (len(self
.children
), ren
, self
.width
, self
.height
, self
.xmin
, self
.xmax
, self
.ymin
, self
.ymax
)
148 self
._svg
= svg
.SVG("svg", self
.width
, self
.height
, [0, 0, self
.width
, self
.height
])
149 self
._svg
.__dict
__["children"] = output
.children
150 self
._svg
.__dict
__["attrib"].update(output
.attrib
)