def test_path_TepidQuadratic(self): path = Path("M 10 5 Q 15 30 25 15 T 50 40") pe = PathElement() pe.path = path ibb = (10, 50), (5, 40) self.assert_bounding_box_is_equal(pe, *ibb)
def add_frame(self, name, box, style, radius=0): """ name -- The name of the new frame object. box -- The boundary box of the node. style -- The style used to draw the path. radius -- The corner radius of the frame. returns a new frame node. """ r = min( [radius, (abs(box[1] - box[0]) / 2), (abs(box[3] - box[2]) / 2)]) if radius > 0: d = ' '.join( str(x) for x in [ 'M', box[0], (box[2] + r), 'A', r, r, '0 0 1', ( box[0] + r), box[2], 'L', (box[1] - r), box[2], 'A', r, r, '0 0 1', box[1], (box[2] + r), 'L', box[1], ( box[3] - r), 'A', r, r, '0 0 1', (box[1] - r), box[3], 'L', (box[0] + r), box[3], 'A', r, r, '0 0 1', box[0], (box[3] - r), 'Z' ]) else: d = ' '.join( str(x) for x in [ 'M', box[0], box[2], 'L', box[1], box[2], 'L', box[1], box[3], 'L', box[0], box[3], 'Z' ]) elem = PathElement() elem.style = style elem.label = name elem.path = d return elem
def test_path_with_move_commands_only(self): path = PathElement() path.set_path("M 0 0 " "m 100 100 " "M 200 200") self.assert_bounding_box_is_equal(path, (0, 200), (0, 200))
def test_path_two_straight_lines_relative(self): path = PathElement() path.set_path("M 0 0 " "l 10 10 " "m -11 -9 " "l 12 12") self.assert_bounding_box_is_equal(path, (-1, 11), (0, 13))
def test_path_combined_1(self): path = Path("M 0 0 C 11 14 33 3 85 98 H 84 V 91 L 13 78 C 26 83 65 24 94 77") # path = Path("M 0 0 C 11 14 33 3 85 98") pe = PathElement() pe.path = path ibb = self.get_inkscape_bounding_box(pe) self.assert_bounding_box_is_equal(pe, *ibb, disable_inkscape_check=True)
def vert_line(self, x, xlat, bbox): """Create a vertical line""" line = PathElement() x = x - xlat[0] * self.options.xoffset y1 = bbox.top - xlat[1] * self.options.yoffset y2 = bbox.bottom line.set('d', 'M %f %f V %f' % (x, y1, y2)) return line
def horz_line(self, y, xlat, bbox): """Create a horzontal line""" line = PathElement() x1 = bbox.left - xlat[0] * self.options.xoffset x2 = bbox.right y1 = y - xlat[1] * self.options.yoffset line.set('d', 'M %f %f H %f' % (x1, y1, x2)) return line
def test_path_two_straight_lines_abosolute(self): path = PathElement() path.set_path("M 0 0 " "L 10 10 " "M -1 1 " "L 10 10") self.assert_bounding_box_is_equal(path, (-1, 10), (0, 10))
def generateLine(self, x1, y1, x2, y2, strokeWidth, stroke, name): line = PathElement() line.path = 'M {} {} L {} {}'.format(x1, y1, x2, y2) line.style = { 'stroke': stroke, 'stroke-width': strokeWidth, 'fill': 'none' } line.label = name return line
def test_path_straight_line_scaled(self): path = PathElement() scale_x = 2 scale_y = 3 path.set_path("M 10 10 " "L 20 20") path.transform = Transform(scale=(scale_x, scale_y)) self.assert_bounding_box_is_equal(path, (scale_x * 10, 20 * scale_x), (scale_y * 10, 20 * scale_y))
def render_path(self, pointStr): singlePath = self.get_icon_path_str(pointStr) pathStr = "" for row in range(self.draw.row_count()): for col in range(self.draw.col_count()): if self.draw.isDark(col, row): x, y = self.get_svg_pos(col, row) pathStr += "M %f,%f " % (x, y) + singlePath + " z " path = PathElement() path.set('d', pathStr) return path
def generateSectorColoredPart(): nSectorPlusOne = nSector + 1 pathPattern = 'm{} {} v {} h {} v {} h {} v {} h {} v {} Z' coloredPart = PathElement() coloredPart.style = { 'stroke': stroke, 'stroke-width': strokeWidth, 'fill': self.sectorColors[nSector] } coloredPart.label = 'colored-part-{}'.format(nSectorPlusOne) coloredPart.path = pathPattern.format( (nColumns / 2 - 2) * xBoxSize, initialY - yBoxSize, -initialY + yBoxSize, xBoxSize, yBoxSize * (nBoxesPerColumn - 5), xBoxSize, yBoxSize, -xBoxSize, yBoxSize * 3) return coloredPart
def generateOutline(): pathPattern = 'm{} {} v {} l {} {} h {} l {} {} v {} Z' outline = PathElement() outline.style = { 'stroke': stroke, 'stroke-width': strokeWidth, 'fill': fillSecure } outline.label = 'outline-{}'.format(nSector + 1) outline.path = pathPattern.format(initialX, initialY, -initialY + yBoxSize, obliqueDistance, -yBoxSize, topDistance, obliqueDistance, yBoxSize, yBoxSize * (nBoxesPerColumn - 1)) return outline
def drawline(self, dstr, name, parent, sstr=None): line_style = {'stroke': '#000000', 'stroke-width': '1', 'fill': 'none'} if sstr == None: stylestr = str(Style(line_style)) else: stylestr = sstr el = parent.add(PathElement()) el.path = dstr el.style = stylestr el.label = name
def generateSectorCenterPart(): nSectorPlusOne = nSector + 1 pathPattern = 'm{} {} L {} {} L {} {}' centerColoredPart = PathElement() fill = ('none', self.sectorColors[nSector])[not self.useLaser and self.generateCenter] centerColoredPart.style = { 'stroke': stroke, 'stroke-width': strokeWidth, 'fill': fill } centerColoredPart.label = 'colored-part-{}'.format(nSectorPlusOne) cornerSize = initialX + obliqueDistance centerColoredPart.path = pathPattern.format( cornerSize, 0, 0, -distanceFromBoardCenter, -cornerSize, 0) return centerColoredPart
def draw_line(x1, y1, x2, y2, width, name, parent): """Draw an SVG line""" line = parent.add(PathElement()) line.style = { 'stroke': '#000000', 'stroke-width': str(width), 'fill': 'none' } line.path = 'M {},{} L {},{}'.format(x1, y1, x2, y2) line.label = name
def draw_SVG_line(a, b, style, name, parent): (x1, y1) = a (x2, y2) = b line = parent.add(PathElement()) line.style = { 'stroke': style.l_col, 'stroke-width': str(style.l_th), 'fill': style.l_fill } line.path = 'M ' + str(x1) + ',' + str(y1) + ' L ' + str(x2) + ',' + str( y2) line.lavel = name
def add_clip(self, node, clip_path): """ Adds a new clip path node to the defs and sets the clip-path on the node. node -- The node that will be clipped. clip_path -- The clip path object. """ clip = ClipPath() clip.append(PathElement(d=str(clip_path.path))) clip_id = self.svg.get_unique_id('clipPath') clip.set('id', clip_id) self.svg.defs.append(clip) node.set('clip-path', 'url(#{})'.format(str(clip_id)))
def test_random_path_1(self): import random from inkex.paths import Line, Vert, Horz, Curve, Move, Arc, Quadratic, TepidQuadratic, Smooth, ZoneClose klasses = (Line, Vert, Horz, Curve, Move, Quadratic) # , ZoneClose, Arc def random_segment(klass): args = [random.randint(1, 100) for _ in range(klass.nargs)] if klass is Arc: args[2] = 0 # random.randint(0, 1) args[3] = 0 # random.randint(0, 1) args[4] = 0 # random.randint(0, 1) return klass(*args) random.seed(2128506) # random.seed(datetime.now()) n_trials = 10 n_elements = 15 for i in range(n_trials): path = Path() path.append(Move(0, 0)) for j in range(n_elements): k = random.choice(klasses) path.append(random_segment(k)) if k is Curve: while random.randint(0, 1) == 1: path.append(random_segment(Smooth)) if k is Quadratic: while random.randint(0, 1) == 1: path.append(random_segment(TepidQuadratic)) pe = PathElement() pe.path = path ibb = self.get_inkscape_bounding_box(pe) self.assert_bounding_box_is_equal(pe, *ibb, disable_inkscape_check=True)
def plotPath( self, style, yidx=0 ): "Generates path-command-string and returns a path-element." pathstr = "" dataiter = iter(self.data) vals = next(dataiter) values = {'x':vals['x'],'y':vals['y'][yidx]} nextmove = 'M' if values['x'] > self.xmin and values['x'] < self.xmax and values['y'] > self.ymin and values['y'] < self.ymax: pathstr += '{}{},{}'.format( nextmove, self.transformx(values['x']), self.transformy(values['y']) ) nextmove = 'L' prevalues=values while True: try: vals = next(dataiter) values = {'x':vals['x'],'y':vals['y'][yidx]} except StopIteration: break if values['x'] > self.xmin and values['x'] < self.xmax and values['y'] > self.ymin and values['y'] < self.ymax: if nextmove == 'M': interpoint = intersectionPoint(values,prevalues,self.xmin,self.xmax,self.ymin,self.ymax) pathstr += ' {}{},{}'.format( nextmove, self.transformx(interpoint['x']), self.transformy(interpoint['y']) ) nextmove = 'L' pathstr += ' {}{},{}'.format( nextmove, self.transformx(values['x']), self.transformy(values['y']) ) nextmove = 'L' else: if nextmove == 'L': interpoint = intersectionPoint(values,prevalues,self.xmin,self.xmax,self.ymin,self.ymax) pathstr += ' {}{},{}'.format( nextmove, self.transformx(interpoint['x']), self.transformy(interpoint['y']) ) nextmove = 'M' prevalues = values newpath = PathElement(d=pathstr) newpath.style = style return newpath
def test_path_horizontal_line_stroke_square_cap(self): path = PathElement() path.set_path("M 0 0 " "L 1 0") stroke_half_width = 1.0 path.style = Style("stroke-width:{};stroke:red".format(stroke_half_width * 2)) path.set("stroke-linecap", "square") self.assert_bounding_box_is_equal(path, (-stroke_half_width, 1 + stroke_half_width), (-stroke_half_width, stroke_half_width))
def draw_SVG_tri(vert_mat, params, style, name, parent): p1, p2, p3 = get_cartesian_tri( vert_mat, params) # get the vertex matrix in cartesian points elem = parent.add(PathElement()) elem.path = 'M ' + str(p1[0]) + ',' + str(p1[1]) +\ ' L ' + str(p2[0]) + ',' + str(p2[1]) +\ ' L ' + str(p3[0]) + ',' + str(p3[1]) +\ ' L ' + str(p1[0]) + ',' + str(p1[1]) + ' z' elem.style = { 'stroke': style.l_col, 'stroke-width': str(style.l_th), 'fill': style.l_fill } elem.label = name
def add_marker(self, name, rotate): """Create a marker in the defs of the svg""" marker = Marker() marker.set('id', name) marker.set('orient', 'auto') marker.set('refX', '0.0') marker.set('refY', '0.0') marker.set('style', 'overflow:visible') marker.set('inkscape:stockid', name) self.svg.defs.append(marker) arrow = PathElement( d='M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z ') if rotate: arrow.set('transform', 'scale(0.8) rotate(180) translate(12.5,0)') else: arrow.set('transform', 'scale(0.8) translate(12.5,0)') arrow.set( 'style', 'fill-rule:evenodd;stroke:#000000;stroke-width:1.0pt;marker-start:none' ) marker.append(arrow)
def test_path_TepidQuadratic_2(self): path = Path("M 10 5 Q 15 30 25 15 T 50 40 T 15 20") pe = PathElement() pe.path = path ibb = (10, 10 + 43.462), (5, 56) self.assert_bounding_box_is_equal(pe, *ibb)
def test_path_Curve(self): path = Path("M10 10 C 20 20, 40 20, 50 10") pe = PathElement() pe.path = path self.assert_bounding_box_is_equal(pe, (10, 50), (10, 17.5))
def test_path_vert(self): path = Path("M 15 30 v 20") pe = PathElement() pe.path = path self.assert_bounding_box_is_equal(pe, (15, 15), (30, 50))
def test_path_horz(self): path = Path("M 15 30 h 20") pe = PathElement() pe.path = path self.assert_bounding_box_is_equal(pe, (15, 35), (30, 30))
def test_path_Zone(self): path = Path("M 15 30 Z") pe = PathElement() pe.path = path self.assert_bounding_box_is_equal(pe, (15, 15), (30, 30))
def test_path_line(self): path = Path("M 15 30 l 10 20") pe = PathElement() pe.path = path self.assert_bounding_box_is_equal(pe, (15, 25), (30, 50))
def test_path_Line(self): path = Path("M 15 30 L 10 20") pe = PathElement() pe.path = path self.assert_bounding_box_is_equal(pe, (10, 15),( 20, 30))