def clear(self): ''' Clear all the elements and reset group to an empty group ''' self.isw = Area.isw self.width = Area.width self.height = Area.height self.objbox = Bbox() self.objects = []
def recalc_size(self): ''' recalculate internal container size based on objects within ''' self.objbox = Bbox() for obj in self.objects: self.objbox.union(obj.bbox()) if self.objbox.is_set(): self.isw = self.objbox.sw self.width = self.objbox.width self.height = self.objbox.height
def bbox(self): """ Return the bounding box of the Page object as a Bbox object """ area = self.area() return Bbox(sw = area.sw, width = area.width, height = area.height)
def bbox(self): """ Gather together common bounding box for group Don't use Area's bbox as transformations may mean a tighter bbox (eg a circle) @return: a Bbox() @rtype: Bbox """ # We need to do the calculation in the # external co-ordinates (that's where the # bounding box will be used) # first a null Bbox bbox = Bbox() for obj in self.objects: bbox.union(obj.bbox(), self.itoe) return bbox
def __init__(self, *objects, **options): """ Initialisation of Group object @param objects: list of objects to group together @type objects: list @param options: dictionary of options @type options: dict """ self.objects = [] self.objbox = Bbox() if len(objects) == 1 and type(objects[0]) in (TupleType, ListType): apply(self.append, objects[0]) #self.append(objects[0]) else: apply(self.append, objects) #self.append(objects) Area.__init__(self, **options)
def bbox(self, itoe=Identity): """ Return the bounding box """ p0 = itoe(self.s) p1 = itoe(self.e) x0 = min(p0[0], p1[0]) x1 = max(p0[0], p1[0]) y0 = min(p0[1], p1[1]) y1 = max(p0[1], p1[1]) return Bbox(sw=P(x0, y0), width=x1 - x0, height=y1 - y0)
def bbox(self): """ Return the bounding box of the object """ x1, y1 = self.sw x2, y2 = self.ne for p in [self.sw, self.nw, self.ne, self.se]: x1 = min(x1, p[0]) y1 = min(y1, p[1]) x2 = max(x2, p[0]) y2 = max(y2, p[1]) return Bbox(sw=P(x1, y1), width=x2 - x1, height=y2 - y1)
def bbox(self): """ Return the bounding box object of the Circle """ #grab a tight boundingbox by zipping around circumference SW = self.locus(0) NE = self.locus(0) for ii in xrange(self.start, self.end + 10, 10): p = self.locus(ii) SW[0] = min(SW[0], p[0]) SW[1] = min(SW[1], p[1]) NE[0] = max(NE[0], p[0]) NE[1] = max(NE[1], p[1]) return Bbox(sw=SW, width=NE[0] - SW[0], height=NE[1] - SW[1])
def bbox(self): """ Return the bounding box of the Path """ b = Bbox() for pl in self._pathlettes: b.union(pl.bbox(self.itoe)) # take into account extent of arrowheads for ar in self.heads: b.union(ar.bbox()) return b
def bbox(self, itoe=Identity): """ Return the bounding box of the object """ # run through the list of points to get the bounding box #if self.length is None: # self._cache() p0 = itoe(self.s) x0, y0 = p0 x1, y1 = p0 for p in self._points: p1 = itoe(p) x0 = min(x0, p1[0]) x1 = max(x1, p1[0]) y0 = min(y0, p1[1]) y1 = max(y1, p1[1]) return Bbox(sw=P(x0, y0), width=x1 - x0, height=y1 - y0)
def bbox(self): """ Return the bounding box of the Path """ # the (0,0) point: p0 = self.itoe(P(0, 0)) xmax = xmin = p0.x ymax = ymin = p0.y for bez in self.shape: c1x, c1y, c2x, c2y, p2x, p2y = bez p1 = self.itoe(P(c1x, c1y) / float(defaults.units)) p2 = self.itoe(P(c2x, c2y) / float(defaults.units)) p3 = self.itoe(P(p2x, p2y) / float(defaults.units)) xmax = max(xmax, p1.x, p2.x, p3.x) xmin = min(xmin, p1.x, p2.x, p3.x) ymax = max(ymax, p1.y, p2.y, p3.y) ymin = min(ymin, p1.y, p2.y, p3.y) return Bbox(sw=P(xmin, ymin), width=xmax - xmin, height=ymax - ymin)
class Group(Area): """ Groups together a list of objects """ def __init__(self, *objects, **options): """ Initialisation of Group object @param objects: list of objects to group together @type objects: list @param options: dictionary of options @type options: dict """ self.objects = [] self.objbox = Bbox() if len(objects) == 1 and type(objects[0]) in (TupleType, ListType): apply(self.append, objects[0]) #self.append(objects[0]) else: apply(self.append, objects) #self.append(objects) Area.__init__(self, **options) def __getitem__(self, i): """ Get an item from the list of objects @param i: the index of the item to get @type i: int """ return self.objects[i] # these will break alignment #def __setitem__(self,i,other): # self.objects[i]=other def __getslice__(self, i, j): """ Get a slice of items from the list of objects @param i: the first index of the slice of items to get @type i: int @param j: the last index of the slice of items to get @type j: int """ return self.objects[i:j] #def __setslice__(self,i,j,wert): # self.objects[i:j]=wert def __len__(self): """ Returns the length of the object list """ return len(self.objects) def validate(self, obj): ''' make sure this object can be inserted into group @param obj: object to test for insertability @type obj: object ''' if isinstance(obj, Page) or isinstance(obj, Pages): raise TypeError, "Can't add a Page to %s" % str(self.__class__) def reverse(self): """ Reverse the order of objects in the list of objects in the group """ self.objects.reverse() # for convenience return reference to group return self def insert(self, idx, obj): ''' insert object @param idx: index at which to insert object @type idx: int @param obj: the object to insert @type obj: object ''' self.validate(obj) self.objbox.union(obj.bbox()) self.objects.insert(idx, obj) # for convenience return reference to group return self def append(self, *objs, **options): ''' append object(s) to group @param objs: list of objects to append @type objs: list @param options: dictionary of options @type options: dict ''' for obj in objs: self.validate(obj) self.objbox.union(obj.bbox()) self.objects.append(obj) # update size if self.objbox.is_set(): self.isw = self.objbox.sw self.width = self.objbox.width self.height = self.objbox.height # for convenience return reference to group return self def apply(self, **options): ''' apply attributes to all objects @param options: dictionary of attributes @type options: dict @return: reference to self @rtype: self ''' # do this by attributes since they # might not all get accepted for key, value in options.items(): dict1 = {key:value} for obj in self.objects: if isinstance(obj, Group): # recurse apply(obj.apply, (), options) try: apply(obj, (), dict1) except AttributeError: # skip objects that don't have the attribute pass # we don't know if the sizes where changes so recalculate them self.recalc_size() # for convenience return reference to group return self def recalc_size(self): ''' recalculate internal container size based on objects within ''' self.objbox = Bbox() for obj in self.objects: self.objbox.union(obj.bbox()) if self.objbox.is_set(): self.isw = self.objbox.sw self.width = self.objbox.width self.height = self.objbox.height def clear(self): ''' Clear all the elements and reset group to an empty group ''' self.isw = Area.isw self.width = Area.width self.height = Area.height self.objbox = Bbox() self.objects = [] def body(self): """ Returns the body postscript of the object """ out = cStringIO.StringIO() for obj in self.objects: if obj.bbox().sw is not None: out.write(str(obj)) return out.getvalue() def bbox(self): """ Gather together common bounding box for group Don't use Area's bbox as transformations may mean a tighter bbox (eg a circle) @return: a Bbox() @rtype: Bbox """ # We need to do the calculation in the # external co-ordinates (that's where the # bounding box will be used) # first a null Bbox bbox = Bbox() for obj in self.objects: bbox.union(obj.bbox(), self.itoe) return bbox
def bbox(self): """ Return the bounding box of the Dot """ return Bbox(sw=self.sw, width=2 * self.r, height=2 * self.r)