def generate(self): """Generate the actual svg from the coding""" string = self.encode(self.text) if string == 'ERROR': return name = self.get_id('barcode') # use an svg group element to contain the barcode barcode = Group() barcode.set('id', name) barcode.set('style', 'fill: black;') barcode.transform.add_translate(self.pos_x, self.pos_y) if self.scale: barcode.transform.add_scale(self.scale) bar_id = 1 bar_offset = 0 tops = set() for datum in self.graphical_array(string): # Datum 0 tells us what style of bar is to come next style = self.get_style(int(datum[0])) # Datum 1 tells us what width in units, # style tells us how wide a unit is width = int(datum[1]) * int(style['width']) if style['write']: tops.add(style['top']) rect = Rectangle() rect.set('x', str(bar_offset)) rect.set('y', str(style['top'])) if self.pos_text == TEXT_POS_TOP: rect.set('y', str(style['top'] + self.font_size)) rect.set('id', "{}_bar{:d}".format(name, bar_id)) rect.set('width', str(width)) rect.set('height', str(style['height'])) barcode.append(rect) bar_offset += width bar_id += 1 for extra in self._extra: if extra is not None: barcode.append(extra) bar_width = bar_offset # Add text at the bottom of the barcode text = TextElement() text.set('x', str(int(bar_width / 2))) text.set('y', str(min(tops) + self.font_size - 1)) if self.pos_text == TEXT_POS_BOTTOM: text.set('y', str(self.height + max(tops) + self.font_size)) text.set('style', TEXT_TEMPLATE % self.font_size) text.set('xml:space', 'preserve') text.set('id', '{}_text'.format(name)) text.text = str(self.text) barcode.append(text) return barcode
def textAt( x, y, text, style, rotate=0 ): "Places a test-element at global-coordinates." elem = TextElement(x=str(x), y=str(y)) elem.text = text elem.style = style if rotate != 0: elem.set('transform','rotate({},{},{})'.format(rotate,x,y)) return elem
def add_text(self, x, y, text): """Add a text label at the given location""" elem = TextElement(x=str(x), y=str(y)) elem.text = str(text) elem.style = { 'font-size': self.svg.unittouu(self.options.fontsize), 'fill-opacity': '1.0', 'stroke': 'none', 'font-weight': 'normal', 'font-style': 'normal', 'fill': '#999' } return elem
def draw_text(self, text, add_size=0, anchor='start', **kwargs): """Draw a textual label""" vtext = TextElement(**kwargs) vtext.style = { 'fill': self.options.font_color, 'font-family': self.options.font, 'font-size': str(self.options.font_size + add_size) + 'px', 'font-style': 'normal', 'font-variant': 'normal', 'font-weight': 'normal', 'font-stretch': 'normal', '-inkscape-font-specification': 'Bitstream Charter', 'text-align': anchor, 'text-anchor': anchor, } vtext.text = str(text) return vtext
def effect(self): if len(self.svg.selection) != 1: raise AbortExtension(_("Debe seleccionar un objeto")) scale = self.svg.unittouu('1mm') # convert to document units patternSize = self.options.patternSize fromSize = self.options.fromSize toSize = self.options.toSize if not (fromSize <= patternSize <= toSize): raise AbortExtension( _("La talla del patrón debe estar dentro de desde y hasta")) downerSizesCount = patternSize - fromSize upperSizesCount = toSize - patternSize pattern = self.svg.selection.first() parent = pattern.getparent() bbox = pattern.shape_box() scaleX = 10 * scale # scale width 10mm scaleY = 23.21 * scale # scale height 23.21mm width = bbox.width * scale height = bbox.height * scale for i, size in enumerate( range(patternSize + upperSizesCount, patternSize, -1)): copy = pattern.duplicate() size_text = TextElement() proportionX = 1 + (1 - ((width - (scaleX * (upperSizesCount - i))) / width)) proportionY = 1 + (1 - ((height - (scaleY * (upperSizesCount - i))) / height)) transform = Transform() transform.add_scale(proportionX, proportionY) copy.transform = transform size_text.text = str(size) size_text.set( 'style', "font-size:8px;shape-inside:url(#{});".format(copy.get('id'))) group = Group() group.append(copy) group.append(size_text) parent.append(group) group.set( 'transform', "translate(-{},-{})".format(copy.shape_box().left, copy.shape_box().top)) for i, size in enumerate( range(patternSize - 1, patternSize - downerSizesCount - 1, -1), 1): copy = pattern.duplicate() size_text = TextElement() proportionX = (width - (scaleX * i)) / width proportionY = (height - (scaleY * i)) / height transform = Transform() transform.add_scale(proportionX, proportionY) copy.transform = transform size_text.text = str(size) size_text.set( 'style', "font-size:8px;shape-inside:url(#{});".format(copy.get('id'))) group = Group() group.append(copy) group.append(size_text) parent.append(group) group.set( 'transform', "translate(-{},-{})".format(copy.shape_box().left, copy.shape_box().top)) patternGroup = Group() pattern_size_text = TextElement() pattern_size_text.text = str(patternSize) pattern_size_text.set( 'style', "font-size:8px;shape-inside:url(#{});".format(pattern.get('id'))) patternGroup.append(pattern) patternGroup.append(pattern_size_text) parent.append(patternGroup)