Exemple #1
0
class TestPointsToStringFullProfile(unittest.TestCase):
    def setUp(self):
        self.polyline = Polyline(debug=True, profile='full')

    def test_valid_points(self):
        # valid units: cm|em|ex|in|mm|pc|pt|px|%
        # dont't know if '%' is valid for points?
        result = self.polyline.points_to_string([(10, 10), ('20cm', '20em'),
                                                 ('30ex', '30in'),
                                                 ('40mm', '40pc'),
                                                 ('50pt', '50px'),
                                                 ('60%', '60%')])
        # it's an unicode string
        self.assertEqual(
            result, "10,10 20cm,20em 30ex,30in 40mm,40pc 50pt,50px 60%,60%")
        # e-notation is valid
        result = self.polyline.points_to_string([('1e10pt', '1e-10in')])
        self.assertEqual(result, "1e10pt,1e-10in")

    def test_invalid_points(self):
        # invalid unit #'p'
        self.assertRaises(TypeError, self.polyline.points_to_string,
                          [(10, 10), ('20p', '20px')])

    def test_invalid_tuple_count(self):
        # 3-tuple not allowed
        self.assertRaises(TypeError, self.polyline.points_to_string,
                          [(10, 10), ('20px', '20px', '20px')])
        # 1-tuple not allowed
        self.assertRaises(TypeError, self.polyline.points_to_string,
                          [(10, 10), ('20px', )])
Exemple #2
0
def markup_measure(staff_prop: StaffProperties, staves_position, height,
                   measure_number, measure_placement: MeasurePosition):
    return [
        Polyline(points=[(measure_placement.start, staves_position.y),
                         (measure_placement.start,
                          staves_position.y + height)]).stroke(
                              color=svgwrite.rgb(0, 0, 0),
                              width=2,
                              linejoin='bevel',
                          )
    ] + ([
        Polyline(points=[(measure_placement.end, staves_position.y),
                         (measure_placement.end,
                          staves_position.y + height)]).stroke(
                              color=svgwrite.rgb(0, 0, 0),
                              width=2,
                              linejoin='bevel',
                          )
    ] if measure_placement.last_on_staff else []) + ([
        Text(
            str(measure_number),
            insert=(measure_placement.start - 5, staves_position.y - 10),
            fill="rgb(0,0,0)",
            style=
            f"font-size:{staff_prop.staff_line_offset}; font-family:Arial",
        )
    ] if measure_placement.first_on_staff or 'debug' else [])
Exemple #3
0
class TestPointsToStringTinyProfile(unittest.TestCase):
    def setUp(self):
        self.polyline = Polyline(debug=True, profile='tiny')

    def test_valid_points(self):
        # valid units: cm|em|ex|in|mm|pc|pt|px|%
        # dont't know if '%' is valid for points?
        result = self.polyline.points_to_string([(10, 10), ('20cm', '20em'),
                                                 ('30ex', '30in'),
                                                 ('40mm', '40pc'),
                                                 ('50pt', '50px'),
                                                 ('60%', '60%')])
        # it's an unicode string
        self.assertEqual(
            result, "10,10 20cm,20em 30ex,30in 40mm,40pc 50pt,50px 60%,60%")

    def test_float_points(self):
        result = self.polyline.points_to_string([(10.12345, 10.12345),
                                                 (3.14151, 3.14151)])
        self.assertEqual(result, "10.1235,10.1235 3.1415,3.1415")

    def test_value_range(self):
        # invalid unit #'p'
        self.assertRaises(TypeError, self.polyline.points_to_string,
                          [(100000, 10)])
        self.assertRaises(TypeError, self.polyline.points_to_string,
                          [(-100000, 10)])
        self.assertRaises(TypeError, self.polyline.points_to_string,
                          [(10, 100000)])
        self.assertRaises(TypeError, self.polyline.points_to_string,
                          [(-10, -100000)])
Exemple #4
0
    def _define_zero_symbol(self):
        zero = Symbol(id="zero", class_="bit-0", stroke_width=1, stroke="black", fill="none")
        zero.add(Circle(center=(0, 0), r=self.dimens.bit_radius, fill="none", stroke="none"))
        r = 0.75

        n = self.poly.num_vertices
        for i in range(n):
            vertex = Point(
                self.dimens.bit_radius * math.sin(2 * math.pi * i / n),
                self.dimens.bit_radius * math.cos(2 * math.pi * i / n))

            previous_vertex = Point(
                self.dimens.bit_radius * math.sin(2 * math.pi * (i + n - 1) / n),
                self.dimens.bit_radius * math.cos(2 * math.pi * (i + n - 1) / n))
            start_point = Point(
                (1 - r) * previous_vertex.x + r * vertex.x,
                (1 - r) * previous_vertex.y + r * vertex.y
            )

            next_vertex = Point(
                self.dimens.bit_radius * math.sin(2 * math.pi * (i + n + 1) / n),
                self.dimens.bit_radius * math.cos(2 * math.pi * (i + n + 1) / n))
            end_point = Point(
                (1 - r) * next_vertex.x + r * vertex.x,
                (1 - r) * next_vertex.y + r * vertex.y
            )

            line = Polyline([start_point, vertex, end_point], stroke_linejoin="bevel")
            zero.add(line)
        self.dwg.defs.add(zero)
Exemple #5
0
def staff_line(page_prop: PageProperties, staff_prop: StaffProperties,
               point: Point):
    x, y = point
    lines = []
    lenght = page_prop.width - point.x - staff_prop.right_offset
    for i in range(7):
        dotted_lines = LINE_SET_SETUP
        line = Polyline(
            points=[(x, y + i * staff_prop.staff_line_offset
                     ), (x + lenght,
                         y + i * staff_prop.staff_line_offset)]).stroke(
                             color=svgwrite.rgb(0, 0, 0),
                             width=2,
                             linejoin='bevel',
                         )
        if dotted_lines[i]:
            line.dasharray([5, 7])
        lines.append(line)
    return lines
Exemple #6
0
class TestPointsToStringTinyProfile(unittest.TestCase):
    def setUp(self):
        self.polyline = Polyline(debug=True, profile='tiny')

    def test_valid_points(self):
        # valid units: cm|em|ex|in|mm|pc|pt|px|%
        # dont't know if '%' is valid for points?
        result = self.polyline.points_to_string([(10,10), ('20cm', '20em'), ('30ex', '30in'), ('40mm', '40pc'), ('50pt', '50px'), ('60%', '60%')])
        # it's an unicode string
        self.assertEqual(result, "10,10 20cm,20em 30ex,30in 40mm,40pc 50pt,50px 60%,60%")

    def test_float_points(self):
        result = self.polyline.points_to_string([(10.12345,10.12345),(3.14151, 3.14151)])
        self.assertEqual(result, "10.1235,10.1235 3.1415,3.1415")

    def test_value_range(self):
        # invalid unit #'p'
        self.assertRaises(TypeError, self.polyline.points_to_string, [(100000,10)])
        self.assertRaises(TypeError, self.polyline.points_to_string, [(-100000,10)])
        self.assertRaises(TypeError, self.polyline.points_to_string, [(10,100000)])
        self.assertRaises(TypeError, self.polyline.points_to_string, [(-10,-100000)])
def generate(glyph: Glyph, metadata: Metadata):
    """Uses the glyph and the metadata to create the SVGs.

    The subfolder is the font name.
    The SVG name contains the metadata necessary to import the glyph to the font.
    """
    print(
        f'Generating character \'{metadata.character}\' for font {metadata.font_name}'
    )

    # Assumes font folder already exists
    font_path = os.path.join(os.getcwd(), 'svgs', metadata.font_name)

    left_side_bearing, right_side_bearing = _calculate_bearings(glyph)
    svg_filename = f'ascii{str(metadata.ascii_value)}_l{left_side_bearing}_r{right_side_bearing}.svg'
    dwg = svgwrite.Drawing(os.path.join(font_path, svg_filename),
                           viewBox=f"0 0 {svg_size} {svg_size}")

    points = []
    for coordinate in glyph.coordinates:
        if coordinate:
            x, y = coordinate
            x = _map_to_svg(x)
            y = _map_to_svg(y)
            points.append((x, y))
        else:
            # Pen up
            # Always check if line is empty (i.e. points array is empty).
            # Empty lines can cause problems.
            if points:
                dwg.add(Polyline(points, **line_style))
            points = []

    # Finalize the drawing
    if points:
        dwg.add(Polyline(points, **line_style))

    dwg.save()
Exemple #8
0
class TestPointsToStringFullProfile(unittest.TestCase):
    def setUp(self):
        self.polyline = Polyline(debug=True, profile='full')

    def test_valid_points(self):
        # valid units: cm|em|ex|in|mm|pc|pt|px|%
        # dont't know if '%' is valid for points?
        result = self.polyline.points_to_string([(10,10), ('20cm', '20em'), ('30ex', '30in'), ('40mm', '40pc'), ('50pt', '50px'), ('60%', '60%')])
        # it's an unicode string
        self.assertEqual(result, "10,10 20cm,20em 30ex,30in 40mm,40pc 50pt,50px 60%,60%")
        # e-notation is valid
        result = self.polyline.points_to_string([('1e10pt','1e-10in')])
        self.assertEqual(result, "1e10pt,1e-10in")

    def test_invalid_points(self):
        # invalid unit #'p'
        self.assertRaises(TypeError, self.polyline.points_to_string, [(10,10), ('20p', '20px')])

    def test_invalid_tuple_count(self):
        # 3-tuple not allowed
        self.assertRaises(TypeError, self.polyline.points_to_string, [(10,10), ('20px', '20px', '20px')])
        # 1-tuple not allowed
        self.assertRaises(TypeError, self.polyline.points_to_string, [(10,10), ('20px', )])
Exemple #9
0
def markup_extra_staffs(staff_prop: StaffProperties, staff_start_position,
                        horizontal_note_position, note_vertical_offset):
    objects = []
    if note_vertical_offset < 0:
        count = abs(int(
            (note_vertical_offset) // staff_prop.staff_line_offset))

        for idx in range(1, count + 1):
            y = staff_start_position - idx * staff_prop.staff_line_offset
            line = Polyline(points=[(horizontal_note_position - 40,
                                     y), (horizontal_note_position + 50,
                                          y)]).stroke(
                                              color=svgwrite.rgb(0, 0, 0),
                                              width=2,
                                              linejoin='bevel',
                                          )

            # 7 lines in LINE_SET_SETUP, last - bottom.
            # Get for next top last with skipping 1 doubled starting and 1 existing.
            if LINE_SET_SETUP[-((idx) % 3) - 1]:
                line.dasharray([5, 7])
            objects += [line]

    if note_vertical_offset > staff_prop.staff_height:
        count = int((note_vertical_offset - staff_prop.staff_height) //
                    staff_prop.staff_line_offset)
        for idx in range(1, count + 1):
            y = staff_start_position + staff_prop.staff_height + idx * staff_prop.staff_line_offset
            line = Polyline(points=[(horizontal_note_position - 40,
                                     y), (horizontal_note_position + 50,
                                          y)]).stroke(
                                              color=svgwrite.rgb(0, 0, 0),
                                              width=2,
                                              linejoin='bevel',
                                          )

            # 7 lines in LINE_SET_SETUP, first - top. Get for next bottom from start, skipping 1 existing.
            if LINE_SET_SETUP[idx % 3]:
                line.dasharray([5, 7])
            objects += [line]
    return objects
Exemple #10
0
    def _draw_changed_pattern(self, dx_node_type, dx_nodes, offset=0):

        for _e in base.gen_dx_nodes(dx_nodes, dx_node_type):
            _start_svg1 = _e.svg_node

            _x1 = float(_start_svg1['x']) + offset
            _y1 = float(_start_svg1['y'])

            _p01 = (_x1, _y1)
            _p02 = (_x1 + _start_svg1['width'], _y1)
            _p03 = (_x1 + _start_svg1['width'], _y1 + _start_svg1['height'])
            _p04 = (_x1, _y1 + _start_svg1['height'])

            _line = Polyline(points=[_p01, _p02, _p03, _p04, _p01],
                             stroke_width="1",
                             stroke=dx_node_type.fill,
                             fill=dx_node_type.fill,
                             opacity=dx_node_type.opacity)

            self.dwg.add(_line)
Exemple #11
0
    def _draw_move_pattern(self, dx_node_type):

        for _e in base.gen_dx_nodes(self.differ.first_dx_nodes, dx_node_type):
            _start_svg1 = _e.svg_node

            if _e.get_dx_nodes():
                _stop_svg2 = _e.get_dx_nodes().svg_node

                _x1 = float(_start_svg1['x'])
                _y1 = float(_start_svg1['y'])

                _x2 = float(self.report2.dwg['x'])
                _y2 = float(self.report2.dwg['y'])

                _x3 = float(_stop_svg2['x'])
                _y3 = float(_stop_svg2['y'])

                _h1 = float(_start_svg1['height'])
                _h2 = float(_stop_svg2['height'])

                _p01 = (_x1, _y1)
                _p02 = (self.report1.pos_x_max, _y1)
                _p03 = (float(self.report2.dwg['x']), _y3)
                _p04 = (_x3 + _x2 + float(_stop_svg2['width']), _y3)
                _p05 = (_x3 + _x2 + float(_stop_svg2['width']), _y3 + _h2)
                _p06 = (float(self.report2.dwg['x']), _y3 + _h2)
                _p07 = (self.report1.pos_x_max, _y1 + _h1)
                _p08 = (_x1, _y1 + _h1)

                _line = Polyline(points=[
                    _p01, _p02, _p03, _p04, _p05, _p06, _p07, _p08, _p01
                ],
                                 stroke_width="0.5",
                                 stroke=dx_node_type.fill,
                                 fill=dx_node_type.fill,
                                 opacity=dx_node_type.opacity)

                self.dwg.add(_line)
Exemple #12
0
 def test_numbers(self):
     polyline = Polyline(points=[(0, 0), (1, 1)])
     self.assertEqual(polyline.tostring(), '<polyline points="0,0 1,1" />')
Exemple #13
0
# Function:
def mapFromTo(x, a, b, c, d):
    y = (x - a) / (b - a) * (d - c) + c
    return y


for y, row in enumerate(all_pixels):
    if (y % y_spacing == 0):

        d = ''

        # obj = spath.Path(stroke=svgwrite.rgb(10, 10, 16, '%'))

        obj = Polyline(
            points=[(0, y)],
            stroke=svgwrite.rgb(10, 10, 16, '%'),
            fill_opacity=0,
            # fill='white'
            debug=False)
        # obj.debug = False

        # s1 = 'M {} {}'.format(0, y)
        # p3 = dwg.path(d=s1, stroke_width=1, stroke='black', fill='none')
        # top left to top right

        l1l = []
        for x, pixel in enumerate(row):

            if (x % x_spacing == 0):

                o_v = (pixel - 255) * -1  # * scale
Exemple #14
0
def markup_note(staff_prop: StaffProperties, staff_start_position,
                staff_octave, horizontal_note_position, chord_offset, note,
                chords_notes):
    not_chord_note = note.id not in chords_notes
    chord_note = note.id in chords_notes
    last_chord_note = chord_note and chords_notes.get(note.id, {}).last

    objects = []

    note_offset = get_note_position(staff_prop, staff_octave, note.pitch)

    vertical_note_position = staff_start_position + note_offset

    note_sign = get_note_sign(note)
    objects += [
        markup_note_body(
            note_sign,
            Point(horizontal_note_position + chord_offset,
                  vertical_note_position))
    ]

    if note.dot:
        addition = (
            note_offset - 0.5
        ) % staff_prop.staff_line_offset - staff_prop.staff_line_offset / 2
        objects += [
            Circle(center=(horizontal_note_position + 35 + chord_offset,
                           vertical_note_position + addition),
                   r=4)
        ]

    if note.time_modification:
        objects += [
            Text(
                str(note.time_modification['actual-notes']),
                insert=(horizontal_note_position,
                        staff_start_position - staff_prop.staff_offset // 2),
                fill="rgb(110,110,110)",
                style="font-size:15px; font-family:Arial",
            )
        ]

    objects += []
    flag = {
        'whole': (0, 0),
        'half': (0.83, 0),
        'quarter': (0.83, 0),
        'eighth': (0.9, 1),
        '16th': (1, 2),
        '32nd': (1.2, 3),
    }

    stem_length_multiplier, beam_count = flag[note.type]
    if stem_length_multiplier:
        half_note_offset = 18.2
        stem_width = 3
        stem_lenght = 85 * stem_length_multiplier
        stem_offset = -0.5
        objects += [
            Polyline(points=[(horizontal_note_position + half_note_offset,
                              vertical_note_position + stem_offset),
                             (horizontal_note_position + half_note_offset,
                              vertical_note_position - stem_lenght +
                              stem_offset)]).stroke(
                                  color=svgwrite.rgb(0, 0, 0),
                                  width=stem_width,
                                  linejoin='bevel',
                                  linecap="round",
                              )
        ]

        # TODO extract beam|stemm drawing into note groups drawing
        # logger.debug(f'{not_chord_note=} {last_chord_note=} {first_chord_note=}')
        if not_chord_note or last_chord_note:
            assert beam_count <= 3, f'max 32nd note, {beam_count=} given'
            beam = hooks[beam_count]
            if beam:
                beam_length = 13
                beam_offset = -0.5
                objects += [
                    Path(d=moved_path(
                        beam, horizontal_note_position + half_note_offset -
                        stem_width / 2, vertical_note_position - stem_lenght +
                        beam_offset))
                ]
    return objects
Exemple #15
0
 def setUp(self):
     self.polyline = Polyline(debug=True, profile='tiny')
Exemple #16
0
 def test_numbers(self):
     polyline = Polyline(points=[(0,0), (1,1)])
     self.assertEqual(polyline.tostring(), '<polyline points="0,0 1,1" />')
Exemple #17
0
 def setUp(self):
     self.polyline = Polyline(debug=True, profile='tiny')
Exemple #18
0
                organic_x = True
                o_x = 0
                if organic_x:
                    import random

                    o_x = random.uniform(-1.5, 1.5)

                value = (value - 255) * -1
                size = value * circle_size

                obj_add = None

                if size > high_clip:
                    obj_add = Polyline(
                        [(y, x + o_x), (y, x + 1 + o_x)],
                        stroke='black',
                        fill_opacity=0,
                    )

                if size > low_clip:
                    obj_add = dwg.circle(
                        center=(y, x + o_x),
                        r=size,
                        stroke='black',
                        fill_opacity=0,
                    )

                if obj_add:
                    g.add(obj_add)
                    obj += 1