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 effect(self): scale = self.svg.unittouu('1px') # convert to document units self.options.xoffset *= scale self.options.yoffset *= scale if not self.svg.selected: raise inkex.AbortExtension("Please select an object") if self.options.type == "geometric": bbox = self.svg.selection.bounding_box() else: bbox = self.svg.selection.first().bounding_box() layer = self.svg.get_current_layer() self.add_marker('Arrow1Lstart', False) self.add_marker('Arrow1Lend', True) group = Group() layer.append(group) group.set('fill', 'none') group.set('stroke', 'black') line = self.horz_line(bbox.top, [0, 1], bbox) line.set('marker-start', 'url(#Arrow1Lstart)') line.set('marker-end', 'url(#Arrow1Lend)') line.set('stroke-width', str(scale)) group.append(line) line = self.vert_line(bbox.left, [0, 2], bbox) line.set('stroke-width', str(0.5 * scale)) group.append(line) line = self.vert_line(bbox.right, [0, 2], bbox) line.set('stroke-width', str(0.5 * scale)) group.append(line) line = self.vert_line(bbox.left, [1, 0], bbox) line.set('marker-start', 'url(#Arrow1Lstart)') line.set('marker-end', 'url(#Arrow1Lend)') line.set('stroke-width', str(scale)) group.append(line) line = self.horz_line(bbox.top, [2, 0], bbox) line.set('stroke-width', str(0.5 * scale)) group.append(line) line = self.horz_line(bbox.bottom, [2, 0], bbox) line.set('stroke-width', str(0.5 * scale)) group.append(line) for node in self.svg.selected.values(): group.append(node) layer.append(group) return None
def get_slicer_layer(self, force_creation=False): # Test if webslicer-layer layer existis layer = self.svg.getElement( '//*[@id="webslicer-layer" and @inkscape:groupmode="layer"]') if layer is None: if force_creation: # Create a new layer layer = Group(id='webslicer-layer') layer.set('inkscape:label', 'Web Slicer') layer.set('inkscape:groupmode', 'layer') self.document.getroot().append(layer) else: layer = None return layer
def generate(self): scale = self.svg.unittouu('1px') # convert to document units opt = self.options if not opt.text: raise inkex.AbortExtension('Please enter an input text') elif opt.drawtype == "symbol" and opt.symbolid == "": raise inkex.AbortExtension('Please enter symbol id') # for Python 3 ugly hack to represent bytes as str for Python2 compatibility text_bytes = bytes(opt.text, opt.encoding).decode("latin_1") text_str = str(opt.text) grp = Group() grp.set('inkscape:label', 'QR Code: ' + text_str) if opt.groupid: grp.set('id', opt.groupid) pos_x, pos_y = self.svg.namedview.center grp.transform.add_translate(pos_x, pos_y) if scale: grp.transform.add_scale(scale) # GENERATE THE QRCODE if opt.typenumber == 0: # Automatic QR code size` code = QRCode.getMinimumQRCode(text_bytes, opt.correctionlevel) else: # Manual QR code size code = QRCode(correction=opt.correctionlevel) code.setTypeNumber(int(opt.typenumber)) code.addData(text_bytes) code.make() self.boxsize = opt.modulesize self.invert_code = opt.invert self.margin = 4 self.draw = GridDrawer(opt.invert, opt.smoothval) self.draw.set_grid(code.modules) self.render_svg(grp, opt.drawtype) return grp
def split_fill_and_stroke(path_node): """Split a path into two paths, one filled and one stroked Returns a the list [fill, stroke], where each is the XML element of the fill or stroke, or None. """ style = dict(inkex.Style.parse_str(path_node.get("style", ""))) # If there is only stroke or only fill, don't split anything if "fill" in style and style["fill"] == "none": if "stroke" not in style or style["stroke"] == "none": return [None, None] # Path has neither stroke nor fill else: return [None, path_node] if "stroke" not in style.keys() or style["stroke"] == "none": return [path_node, None] group = Group() fill = group.add(PathElement()) stroke = group.add(PathElement()) d = path_node.pop('d') if d is None: raise AssertionError("Cannot split stroke and fill of non-path element") nodetypes = path_node.pop('sodipodi:nodetypes', None) path_id = path_node.pop('id', str(id(path_node))) transform = path_node.pop('transform', None) path_node.pop('style') # Pass along all remaining attributes to the group for attrib_name, attrib_value in path_node.attrib.items(): group.set(attrib_name, attrib_value) group.set("id", path_id) # Next split apart the style attribute style_group = {} style_fill = {"stroke": "none", "fill": "#000000"} style_stroke = {"fill": "none", "stroke": "none"} for key in style.keys(): if key.startswith("fill"): style_fill[key] = style[key] elif key.startswith("stroke"): style_stroke[key] = style[key] elif key.startswith("marker"): style_stroke[key] = style[key] elif key.startswith("filter"): style_group[key] = style[key] else: style_fill[key] = style[key] style_stroke[key] = style[key] if len(style_group) != 0: group.set("style", str(inkex.Style(style_group))) fill.set("style", str(inkex.Style(style_fill))) stroke.set("style", str(inkex.Style(style_stroke))) # Finalize the two paths fill.set("d", d) stroke.set("d", d) if nodetypes is not None: fill.set('sodipodi:nodetypes', nodetypes) stroke.set('sodipodi:nodetypes', nodetypes) fill.set("id", path_id + "-fill") stroke.set("id", path_id + "-stroke") if transform is not None: fill.set("transform", transform) stroke.set("transform", transform) # Replace the original node with the group path_node.getparent().replace(path_node, group) return [fill, stroke]
def effect(self): # Create some styles first linewidth = self.svg.unittouu(str(self.options.stroke_width)+self.options.stroke_width_unit) cssstyle = { 'stroke': '#000000', 'stroke-width': linewidth, 'fill': 'none', 'stroke-linecap': 'butt', 'stroke-linejoin': 'round' } pathstyle = Style(cssstyle) cssstyle['stroke-linecap'] = 'square' borderstyle = Style(cssstyle) cssstyle['stroke-width'] = linewidth*0.6 tickstyle = Style(cssstyle) cssstyle['stroke-width'] = linewidth*.3 subtickstyle = Style(cssstyle) cssstyle['stroke'] = '#999' cssstyle['stroke-width'] = linewidth*0.5 gridstyle = Style(cssstyle) cssstyle['stroke-width'] = linewidth*0.2 subgridstyle = Style(cssstyle) self.ticksize = self.svg.unittouu("8px") self.subticksize = self.svg.unittouu("4px") self.fontsize = self.svg.unittouu("10pt") textstyle = { 'font-size': self.fontsize, 'font-family':self.options.font_family, 'fill-opacity': '1.0', 'stroke': 'none', 'text-align': 'end', 'text-anchor': 'end', 'font-weight': 'normal', 'font-style': 'normal', 'fill': '#000' } textrechtsbdg = Style(textstyle) textstyle['text-align']='center' textstyle['text-anchor']='middle' textzentriert = Style(textstyle) textstyle['font-size']=2*self.fontsize texttitle = Style(textstyle) # Check on selected object if len(self.svg.selected) != 1: raise AbortExtension(_("Please select exact one object, a rectangle is preferred.")) try: yidxarray = [ int(x) for x in re.split(r'[^0-9]',self.options.yidxs) if x ] except ValueError as e: raise AbortExtension(_("Split produces a string, that is not convertable to int.")) # get CSV data self.data = getCSVData(self.options.csv_file, self.options.csv_encoding, { 'delimiter': self.options.csv_delimiter.replace('\\t', '\t') }, self.options.ignorefirst, self.options.xidx, yidxarray) if self.data.len() < 2: raise AbortExtension(_("Less than 2 pairs of values. Nothing to plot.")) # sort by x-values for a proper line self.data.sort() self.data.calculateMinMax() # Get minima and maxima for x- and y-values self.xmin = self.data.getXMin() if self.options.xmin_autodetect else self.options.xmin self.xmax = self.data.getXMax() if self.options.xmax_autodetect else self.options.xmax if self.xmin >= self.xmax: raise AbortExtension(_('xmin > xmax')) self.ymin = self.data.getYMin() if self.options.ymin_autodetect else self.options.ymin self.ymax = self.data.getYMax() if self.options.ymax_autodetect else self.options.ymax if self.ymin >= self.ymax: raise AbortExtension(_('ymin > ymax')) # Get the parent of the node (nodeid,node) = self.svg.selected.popitem() self.bb = node.bounding_box() parent = node.getparent() group = Group() parent.add(group) # get axis and path xAchse = Axis(Vector2d(self.bb.left,self.bb.bottom),Vector2d(self.bb.right,self.bb.bottom),self.xmin,self.xmax) yAchse = Axis(Vector2d(self.bb.left,self.bb.bottom),Vector2d(self.bb.left ,self.bb.top) ,self.ymin,self.ymax) plot = [] for i in range(len(yidxarray)): plot.append( self.plotPath(pathstyle,i) ) # evaluate options and add all together if self.options.xgrid: group.add(xAchse.getTicks( self.options.xtickn, 0, -self.bb.height, gridstyle)) group.add(xAchse.getSubTicks(self.options.xtickn, self.options.xsubtickn, 0, -self.bb.height, subgridstyle)) if self.options.ygrid: group.add(yAchse.getTicks( self.options.ytickn, 0, self.bb.width, gridstyle)) group.add(yAchse.getSubTicks(self.options.ytickn, self.options.ysubtickn, 0, self.bb.width, subgridstyle)) if self.options.xtickn > 0: tlvon = -self.ticksize if self.options.xticksin else 0 tlbis = self.ticksize if self.options.xticksout else 0 group.add(xAchse.getTicks( self.options.xtickn, tlvon, tlbis, tickstyle)) group.add(xAchse.getSubTicks(self.options.xtickn, self.options.xsubtickn, .5*tlvon, .5*tlbis, subtickstyle)) group.add(xAchse.getNumbers( self.options.xtickn, self.options.xformat, 3*self.ticksize, 0, textzentriert)) if self.options.ytickn > 0: tlvon = -self.ticksize if self.options.yticksout else 0 tlbis = self.ticksize if self.options.yticksin else 0 group.add(yAchse.getTicks( self.options.ytickn, tlvon, tlbis, tickstyle)) group.add(yAchse.getSubTicks(self.options.ytickn, self.options.ysubtickn, .5*tlvon, .5*tlbis, subtickstyle)) group.add(yAchse.getNumbers( self.options.ytickn, self.options.yformat, -2*self.ticksize, -.3*self.fontsize, textrechtsbdg)) for i in range(len(yidxarray)): group.add(plot[i]) if self.options.border_left: group.add( self.createBorderLine('left', borderstyle) ) if self.options.border_bottom: group.add( self.createBorderLine('bottom', borderstyle) ) if self.options.border_right: group.add( self.createBorderLine('right', borderstyle) ) if self.options.border_top: group.add( self.createBorderLine('top', borderstyle) ) if self.options.label_title: group.add( textAt(self.bb.center.x,self.bb.minimum.y-self.fontsize,self.options.label_title, texttitle) ) if self.options.label_yaxis: group.add( textAt(self.bb.minimum.x-4*self.fontsize,self.bb.center.y,self.options.label_yaxis, textzentriert,-90) ) if self.options.label_xaxis: group.add( textAt(self.bb.center.x,self.bb.maximum.y+3.5*self.fontsize,self.options.label_xaxis, textzentriert) ) if self.options.storedata: group.set('data-values',str(self.data)) if self.options.remove: parent.remove(node)
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)