Esempio n. 1
0
def draw_line_with_colours(start_x, start_y, stop_x, stop_y, drawing: Drawing, colours):
    number_of_colours = len(colours)

    initial_offset = number_of_colours / 2
    counter = 0

    for colour in colours:
        x_diff = stop_x - start_x
        y_diff = stop_y - start_y

        offset = initial_offset - counter
        counter += 1

        if x_diff == 0:
            drawing.append(draw.Line(start_x + offset, start_y,
                                     stop_x + offset, stop_y,
                                     stroke=colour, stroke_width=1))
        else:
            y2 = 1
            y1 = -(y_diff/x_diff)

            length = np.sqrt(y1 * y1 + y2)
            y1_norm = y1 / length
            y2_norm = y2 / length

            drawing.append(draw.Line(start_x + y1_norm * offset, start_y + y2_norm * offset,
                                stop_x + y1_norm * offset, stop_y + y2_norm * offset,
                                stroke=colour, stroke_width=1))
Esempio n. 2
0
 def drawFret(self, d: dsvg.Drawing, x: float, y: float,
              strings: int) -> float:
     fs = self.fretStyle
     fretBoardHeight = (strings - 1) * fs.fretHeight
     d.append(
         dsvg.Line(x,
                   y,
                   x,
                   y - fretBoardHeight,
                   stroke_width=stringWidth,
                   stroke=stringColor))
     return fs.fretWidth
Esempio n. 3
0
 def drawString(self, d: dsvg.Drawing, lowerFret: int, upperFret: int,
                x: float, y: float) -> float:
     fs = self.frets[0].fretStyle
     fretWidth = fs.fretWidth
     stringLength = (upperFret - lowerFret + 1) * fretWidth
     d.append(
         dsvg.Line(x,
                   y,
                   x + stringLength,
                   y,
                   stroke_width=stringWidth * pow(1.1, self.stringIndex),
                   stroke=stringColor))
     return fs.fretHeight
Esempio n. 4
0
    def drawFretNumber(self, d: dsvg.Drawing, x: float, y: float) -> float:
        fs = self.fretStyle

        textY = y - fs.fontSize / 4.0
        d.append(
            dsvg.Text(str(self.fretIndex),
                      fs.fontSize,
                      x + fs.circleX,
                      textY,
                      fill=fs.circleStrokeColor,
                      center=False,
                      text_anchor='middle'))
        return fs.fretWidth
Esempio n. 5
0
 def drawHeading(self, d: dsvg.Drawing, baseNote: str, x: float,
                 y: float) -> float:
     scaleName = self.subscribers[0].comboBoxScales.currentText()
     modeName = self.subscribers[0].comboBoxModes.currentText()
     heading = f"{baseNote} {scaleName}, Mode {modeName}"
     d.append(
         dsvg.Text(heading,
                   headingFontSize,
                   x,
                   y,
                   fill=headingColor,
                   center=False,
                   text_anchor='left'))
     return headingFontSize
Esempio n. 6
0
def groove(drawing: draw.Drawing, group: np.ndarray, former_radius: float, inner_radius: float, wire_radius: float, style='normal') -> None:

    """
    drawing: a drawSVG drawing object
    group: array representing wires in a single bundle
    former_radius: radius of coil former
    inner_radius: turn radius of innermost wire
    wire_radius: radius of a single wire, 32 AWG, etc...
    style: style of which to draw the grooves
    'normal' denotes a square groove with rounding where the wires meet the coil former
    'chamfer' denotes the same groove with a 60 degree chamfer on the upper edge for ease of 3d modeling 
    Append a groove drawing to the canvas
    """

    first_wire = group[0]  # first wire in bundle
    r_ex = 0.05  # extra groove spacing
    n = len(group)

    p = draw.Path(stroke_width=0.01, stroke='black', fill='none')

    p.m(former_radius + r_ex, first_wire - wire_radius - r_ex)  # starting point
    p.h(inner_radius - former_radius - r_ex)  # horizontal line from starting point to lower-left of first wire
    
    if style == 'normal':
        if n == 1:
            p.arc(inner_radius, first_wire, wire_radius + r_ex, 270, 90, cw=True, includeM=False)  # 180 degree arc around first wire
        else:
            p.arc(inner_radius, first_wire, wire_radius + r_ex, 270, 180, cw=True, includeM=False)  # 90 degree arc around first wire
            p.v(2*(n-1)*wire_radius)  # vertical line between wires of group
            p.arc(inner_radius, first_wire+2*(n-1)*wire_radius, wire_radius + r_ex, 180, 90, cw=True, includeM=False)  # 90 degree arc around last wire
        p.h(former_radius + r_ex - inner_radius)  # horizontal line from top-left of last wire to coil former
    
    if style == 'chamfer':
        if n == 1:
            p.arc(inner_radius, first_wire, wire_radius + r_ex, 270, 120, cw=True, includeM=False)
            p.l(former_radius + r_ex - inner_radius - (wire_radius + r_ex)*np.cos(2*np.pi/3),
                (former_radius + r_ex - inner_radius - (wire_radius + r_ex)*np.cos(2*np.pi/3))*np.tan(np.pi/6))
        else:
            p.arc(inner_radius, first_wire, wire_radius + r_ex, 270, 180, cw=True, includeM=False)
            p.v(2*(n-1)*wire_radius)
            p.arc(inner_radius, first_wire+2*(n-1)*wire_radius, wire_radius + r_ex, 180, 120, cw=True, includeM=False)
            p.l(former_radius + r_ex - inner_radius - (wire_radius + r_ex)*np.cos(2*np.pi/3),
                (former_radius + r_ex - inner_radius - (wire_radius + r_ex)*np.cos(2*np.pi/3))*np.tan(np.pi/6))

    p.Z()  # connect last point to starting point, should always be a vertical line
    drawing.append(p)
Esempio n. 7
0
def start_drawing(width, height):
    expected_svg = Drawing(width, height, origin=(0, 0))
    expected_svg.append(
        Rectangle(0,
                  height - 15,
                  200,
                  10,
                  stroke='lightgrey',
                  fill='lightgrey'))
    expected_svg.append(
        Text('Header',
             10,
             width / 2,
             height - 15,
             font_family='monospace',
             text_anchor='middle'))
    f = Figure()
    f.add(Track(0, width, label='Header'))
    return f, expected_svg
Esempio n. 8
0
    def drawNote(self, d: dsvg.Drawing, x: float, y: float) -> float:
        fs = self.fretStyle
        if self.individualMarked:
            textY = y - fs.fontSize / 4.0
            d.append(
                dsvg.Circle(x + fs.circleX,
                            y,
                            fs.radius,
                            fill=fs.circleFillColor,
                            stroke_width=2,
                            stroke=fs.circleStrokeColor))

            d.append(
                dsvg.Text(self.noteName,
                          fs.fontSize,
                          x + fs.circleX,
                          textY,
                          fill=fs.circleStrokeColor,
                          center=False,
                          text_anchor='middle'))
        return fs.fretWidth
Esempio n. 9
0
def test_scaled_arrow(svg_differ):
    expected_svg = Drawing(100, 35, origin=(0, 0))
    expected_svg.append(Line(0, 10, 93, 10, stroke='black'))
    expected_svg.append(Circle(50, 20, 10, stroke='black', fill='ivory'))
    expected_svg.append(
        Text('2.3', 11, 50, 20, text_anchor='middle', dy="0.35em"))
    expected_svg.append(
        Lines(100, 10, 93, 13.5, 93, 6.5, 100, 10, fill='black'))

    f = Figure()
    f.add(Arrow(0, 200, h=20, elevation=-1, label='2.3'))
    svg = f.show(w=100)

    svg_differ.assert_equal(svg, expected_svg, 'test_arrow')
Esempio n. 10
0
def drawBagel(text: str,
              text2: str,
              fontSize: int = 13,
              textColor: str = '#FFF',
              leftColor: str = '#555',
              rightColor: str = '#08C',
              spacing: int = 10,
              height: int = 20,
              fontName: str = None) -> str:
    spacing = max(spacing, 3)
    height = max(20, height)
    fontSize = max(round(height * 0.5), max(round(height * 0.75), fontSize))
    s = settings()
    s.ReadSettings()
    if fontName is None:
        fontName = s.defaultSvgFont
        fontFamily = 'sans-serif'
    else:
        fontFamily = fontName
    t1w = textwidth(text, fontSize, fontName)
    t2w = textwidth(text2, fontSize, fontName)
    zw = 4 * spacing + t1w + t2w
    d = Drawing(zw, height)
    if fontForgeSupported and 'sans-serif' != fontFamily:
        css = generateFontCSS(fontName, text + text2, genFlags=ASCII_SUPPORT)
        if css is not None:
            d.append(Style(css))
    m = Mask(id="m")
    d.append(m)
    rx = round(height * 0.15)
    m.append(Rectangle(0, 0, zw, height, fill='#FFF', rx=rx, ry=rx))
    g1 = Group(mask="url(#m)")
    g1.append(Rectangle(0, 0, 2 * spacing + t1w, height, fill=leftColor))
    g1.append(Rectangle(2 * spacing + t1w, 0, zw, height, fill=rightColor))
    d.append(g1)
    g2 = Group(aria_hidden="true",
               fill=textColor,
               text_anchor='start',
               font_family=fontFamily)
    g2.append(Text(text, fontSize, spacing, height - fontSize, textLength=t1w))
    g2.append(
        Text(text2,
             fontSize,
             3 * spacing + t1w,
             height - fontSize,
             textLength=t2w))
    d.append(g2)
    return d.asSvg().replace('\n', '')
Esempio n. 11
0
def test_tiny_arrow_at_edge(svg_differ):
    expected_svg = Drawing(210, 35, origin=(0, 0))
    expected_svg.append(Circle(197.5, 20, 10, stroke='black', fill='ivory'))
    expected_svg.append(
        Text('2.3', 11, 197.5, 20, text_anchor='middle', dy="0.35em"))
    expected_svg.append(
        Lines(200, 10, 195, 13.5, 195, 6.5, 200, 10, fill='black'))

    f = Figure()
    f.add(ArrowGroup([Arrow(195, 200, h=20, elevation=-1, label='2.3')]))
    svg = f.show()

    svg_differ.assert_equal(svg, expected_svg, 'test_arrow')
Esempio n. 12
0
        path.Z()
        return path

    def render(self, drawing=None, thickness=1.0):
        drawing = drawing or Drawing(self.width, self.height, origin=(0, 0))
        drawing.append(self.render_path(thickness))
        return drawing


if __name__ == '__main__':
    try:
        nodes = int(sys.argv[1])
    except (ValueError, IndexError):
        nodes = 23

    flash = Flash()
    flashes = [flash]
    current = 0
    for _ in range(nodes):
        if flash.current_point().within_perimeter(flash.end, 10):
            flash = Flash()
            flashes.append(flash)
            current += 1
        flash.random_walk()

    drawing = Drawing(500, 500, origin=(0, 0))
    for flash in flashes:
        drawing.append(flash.render_path())
    drawing.saveSvg('/tmp/flash.svg')
Esempio n. 13
0
def draw_schedule(g, model, filename):
    zeta, chi, duration, label = model
    TOP = 30
    BOTTOM = 30
    LEFT = 100
    RIGHT = 30
    WIDTH = 800
    HEIGHT = 800
    ROW_OFFSET = 20
    TEXT_OFFSET = 40
    FONTSIZE = 30
    d = Drawing(WIDTH, HEIGHT)
    d.append(Rectangle(0, 0, WIDTH, HEIGHT, fill='white'))
    N_tasks = g.num_vertices()
    N_rounds = len(zeta) - N_tasks
    min_t = min(zeta[:N_tasks] - g.vertex_properties['durations'].get_array())
    max_t = max(zeta)
    quantum = (WIDTH - RIGHT - LEFT) / (max_t - min_t)
    block_height = (HEIGHT - TOP - BOTTOM - ROW_OFFSET *
                    (N_tasks)) / (N_tasks + 1)
    for i in range(N_tasks):
        color = list(Color('red').range_to('green', 1000))[max(
            0, int(g.vertex_properties['soft'][i] * 999))]
        d.append(
            Rectangle(quantum * abs(min_t) + LEFT + quantum *
                      (zeta[i] - g.vertex_properties['durations'][i]),
                      HEIGHT - TOP - ROW_OFFSET * i - block_height * (i + 1),
                      quantum * g.vertex_properties['durations'][i],
                      block_height,
                      fill=color.get_hex_l(),
                      stroke_width=2,
                      stroke='black'))
        if g.vertex_properties['deadlines'][i] >= 0:
            x = quantum * abs(min_t) + LEFT + quantum * (
                g.vertex_properties['deadlines'][i])
            d.append(
                Line(x,
                     HEIGHT - TOP - ROW_OFFSET * i - block_height * (i + 1),
                     x,
                     HEIGHT - TOP - ROW_OFFSET * i - block_height * i,
                     stroke_width=4,
                     stroke='purple'))

        d.append(
            Text(str(i),
                 FONTSIZE,
                 TEXT_OFFSET,
                 HEIGHT - TOP - ROW_OFFSET * i - block_height * (i + 1) +
                 block_height / 2,
                 center=True,
                 fill='black'))
    for i in range(N_rounds):
        if duration[i] == 0:
            continue
        d.append(
            Rectangle(quantum * abs(min_t) + LEFT + quantum *
                      (zeta[N_tasks + i] - duration[i]),
                      HEIGHT - TOP - ROW_OFFSET * N_tasks - block_height *
                      (N_tasks + 1),
                      quantum * duration[i],
                      block_height,
                      fill='gray',
                      stroke_width=2,
                      stroke='black'))
    d.append(
        Text('LWB',
             FONTSIZE,
             TEXT_OFFSET,
             HEIGHT - TOP - ROW_OFFSET * N_tasks - block_height *
             (N_tasks + 1) + block_height / 2,
             center=True,
             fill='black'))
    d.savePng(filename)