Exemplo n.º 1
0
    def draw_character(self, mode, **kwargs):
        side_ending = self.side_ending_class(
            self,
            self.side_flipped,
        )

        paper = Paper()

        pen = Pen()
        pen.set_mode(mode)
        pen.move_to((0, TOP - mode.width / 2))
        pen.turn_to(0)
        pen.line_forward(2.0)
        pen.last_segment().start_cap = stub_cap

        side_ending.draw(pen)
        paper.merge(pen.paper)

        bounds = paper.bounds()
        bounds.top = OVER
        bounds.bottom = MIDDLE
        bounds.left = 0
        paper.override_bounds(bounds)

        return paper
Exemplo n.º 2
0
def test_text_centered():
    p = Pen()
    p.move_to((0, 0))
    p.text('abcd', 1, 'sans-serif', centered=True)
    svg_data = p.paper.format_svg(0)
    assert (
        '<text x="0" y="0" font-family="sans-serif" font-size="1" '
        'fill="#000000" text-anchor="middle">abcd</text>'
    ) in svg_data
Exemplo n.º 3
0
def test_text():
    p = Pen()
    p.move_to((0, 0))
    p.text('abcd', 1, 'sans-serif')
    svg_data = p.paper.format_svg(0)
    assert (
        '<text x="0" y="0" font-family="sans-serif" font-size="1" '
        'fill="#000000">abcd</text>'
    ) in svg_data
Exemplo n.º 4
0
def test_draw_bounds():
    p = Pen()
    p.fill_mode()
    Bounds(-2, -3, 1, 2).draw(p)

    assert_path_data(
        p, 0,
        'M-2,3 L1,3 L1,-2 L-2,-2 L-2,3 z'
    )
def handle_letter(letter, mode):
    letter_paper = draw_letter(
        letter,
        mode,
        fixed_width=30.0,
        show_template=True,
        fuse=False,
    )
    letter_paper.translate((5, 0), bounds=False)

    p = Pen()
    p.move_to((-8, 3))
    p.text(
        letter.case,
        6.0,
        font,
        gray,
        centered=True,
    )
    p.move_to((-8, -3))
    p.text(
        ', '.join(lookup(letter.case)),
        6.0,
        font,
        gray,
        centered=True,
    )
    letter_paper.merge(p.paper)

    return letter_paper
Exemplo n.º 6
0
def test_copy_no_mode():
    p = Pen()
    assert_raises(
        AttributeError,
        lambda: p.mode
    )
    p = p.copy()
    assert_raises(
        AttributeError,
        lambda: p.mode
    )
Exemplo n.º 7
0
def test_copy_arc_to():
    p = Pen()
    p.fill_mode()
    p.move_to((0, 0))
    p.turn_to(0)
    p.arc_to((5, 5))
    p = p.copy(paper=True)

    assert_path_data(
        p, 0,
        'M0,0 A 5,5 0 0 0 5,-5'
    )
Exemplo n.º 8
0
def test_text_translate():
    p = Pen()
    p.move_to((0, 0))
    p.text('abcd', 1)
    paper = p.paper

    paper.translate((2, 3))
    svg_data = paper.format_svg(0)
    assert (
        '<text x="2" y="-3" font-family="sans-serif" font-size="1" '
        'fill="#000000">abcd</text>'
    ) in svg_data
Exemplo n.º 9
0
def test_translate_override_bounds():
    # Translate a paper that has overridden bounds. The bounds update as well.
    paper = Paper()
    paper.override_bounds(0, 0, 1, 1)
    paper.translate((3, 4))
    assert_equal(
        paper.bounds(),
        Bounds(3, 4, 4, 5)
    )

    # When bounds=False is passed, then the bounds do not update.
    paper = Paper()
    paper.override_bounds(0, 0, 1, 1)
    paper.translate((3, 4), bounds=False)
    assert_equal(paper.bounds(), Bounds(0, 0, 1, 1))

    # This also works if the bounds are not overridden.
    p = Pen()
    p.fill_mode()
    p.move_to((0.5, 0.5))
    p.circle(0.5)
    assert_equal(p.paper.bounds(), Bounds(0, 0, 1, 1))

    p.paper.translate((3, 4), bounds=False)

    assert_equal(p.paper.bounds(), Bounds(0, 0, 1, 1))
    assert_equal(p.last_path().bounds(), Bounds(3, 4, 4, 5))
Exemplo n.º 10
0
def test_copy_loop():
    p = Pen()
    p.stroke_mode(0.2)

    def square():
        p.turn_to(180)
        p.line_forward(1)
        p.turn_left(90)
        p.line_forward(1)
        p.turn_left(90)
        p.line_forward(1)
        p.turn_left(90)
        p.line_forward(1)

    p.move_to((0, 0))
    square()
    p = p.copy(paper=True)
    p.move_to((2, 0))
    square()

    assert_path_data(
        p, 1,
        (
            'M0.1,-0.1 L-1.1,-0.1 L-1.1,1.1 L0.1,1.1 L0.1,-0.1 z '
            'M-0.1,0.1 L-0.1,0.9 L-0.9,0.9 L-0.9,0.1 L-0.1,0.1 z '
            'M2.1,-0.1 L0.9,-0.1 L0.9,1.1 L2.1,1.1 L2.1,-0.1 z '
            'M1.9,0.1 L1.9,0.9 L1.1,0.9 L1.1,0.1 L1.9,0.1 z'
        )
    )
Exemplo n.º 11
0
def test_text_merge():
    p = Pen()
    p.move_to((0, 0))
    p.text('abcd', 1)
    paper1 = p.paper
    assert '<text' in paper1.format_svg(0)

    paper2 = Paper()
    paper2.merge(paper1)
    assert '<text' in paper2.format_svg(0)

    paper3 = Paper()
    paper3.merge_under(paper1)
    assert '<text' in paper3.format_svg(0)
Exemplo n.º 12
0
def draw_letter(
    letter,
    mode,
    fixed_width=None,
    show_template=False,
    show_bounds=False,
    fuse=True,
):
    """
    Draw the given letter and return a Paper.

    The letter is located centered on x=0, and with y=0 as the
    character baseline.

    If `fixed_width` is specified, use that for the paper width.
    """
    if DEBUG_OUTPUT:
        print(str(letter), file=sys.stderr)

    try:
        character_paper = letter.draw_character(mode, fuse=fuse)
    except Exception:
        if DEBUG_OUTPUT:
            traceback.print_exc()
            # Return an error pattern.
            pen = Pen()
            pen.fill_mode()
            pen.square(1)
            character_paper = pen.paper
        else:
            raise

    if fixed_width is not None:
        bounds = character_paper.bounds()
        bounds.left = -fixed_width / 2
        bounds.right = +fixed_width / 2
        character_paper.override_bounds(bounds)

    template_paper = Paper()
    if show_template:
        template_paper = draw_template_path()
    else:
        template_paper = Paper()

    letter_paper = Paper()
    letter_paper.merge(template_paper)
    letter_paper.merge(character_paper)

    # Set proper bounds for typesetting. Use the character bounds as our basis
    # so the template doesn't increase the size.
    bounds = character_paper.bounds()
    letter_paper.override_bounds(bounds)

    if show_bounds:
        pen = Pen()
        pen.fill_mode('#aaa')
        bounds.draw(pen)
        letter_paper.merge_under(pen.paper)

    return letter_paper
Exemplo n.º 13
0
def test_copy_no_paper():
    p1 = Pen()
    p1.fill_mode()
    p1.move_to((0, 0))
    p1.turn_to(0)
    p1.line_forward(5)
    p2 = p1.copy()
    p2.line_forward(5)

    assert_path_data(
        p1, 0,
        'M0,0 L5,0'
    )
    assert_path_data(
        p2, 0,
        'M5,0 L10,0'
    )
Exemplo n.º 14
0
def test_override_bounds_copy():
    # Get the bounds of a Paper, modify them, then set them back changed.
    paper = Paper()
    paper.override_bounds(0, 0, 1, 1)

    bounds = paper.bounds()
    bounds.right = 5

    assert_equal(paper.bounds(), Bounds(0, 0, 1, 1))
    paper.override_bounds(bounds)
    assert_equal(paper.bounds(), Bounds(0, 0, 5, 1))

    # This works on non-overridden Papers as well.
    paper = Paper()

    p = Pen()
    p.fill_mode()
    p.move_to((0.5, 0.5))
    p.circle(0.5)

    bounds = p.paper.bounds()
    bounds.right = 5

    assert_equal(p.paper.bounds(), Bounds(0, 0, 1, 1))
    p.paper.override_bounds(bounds)
    assert_equal(p.paper.bounds(), Bounds(0, 0, 5, 1))
Exemplo n.º 15
0
def test_copy_arc():
    p1 = Pen()
    p1.fill_mode()

    p1.move_to((0, 0))
    p1.turn_to(0)
    p1.arc_left(90, radius=5)

    p2 = p1.copy(paper=True)
    p2.arc_left(90, radius=5)

    assert_path_data(
        p1, 0,
        'M0,0 A 5,5 0 0 0 5,-5'
    )
    assert_path_data(
        p2, 0,
        'M0,0 A 5,5 0 0 0 5,-5 A 5,5 0 0 0 0,-10'
    )
Exemplo n.º 16
0
    def draw_character(self, mode, fuse=True):
        bottom_ending = bottom_ending_class(
            self,
            self.bottom_straight,
            self.bottom_flipped,
        )

        pen = Pen()
        pen.set_mode(mode)
        pen.move_to((0, TOP))
        pen.turn_to(-90)
        pen.line_to_y(MIDDLE, start_slant=45)
        bottom_ending.draw(pen)
        if fuse:
            pen.paper.fuse_paths()
        pen.paper.center_on_x(0)
        return pen.paper
Exemplo n.º 17
0
def test_circle_bounds():
    p = Pen()
    p.fill_mode()
    p.move_to((1, 1))
    p.circle(1.5)

    assert_equal(
        p.paper.bounds(),
        Bounds(-0.5, -0.5, 2.5, 2.5)
    )
Exemplo n.º 18
0
    def draw_character(self, mode, **kwargs):
        bottom_ending = self.bottom_ending_class(
            self,
            self.bottom_straight,
            self.bottom_flipped,
        )

        pen = Pen()
        pen.set_mode(mode)
        pen.move_to((0, MIDDLE - 0.5))
        self.draw(pen)
        bottom_ending.draw(pen)

        paper = pen.paper

        bounds = paper.bounds()
        bounds.top = MIDDLE
        bounds.bottom = UNDER
        paper.override_bounds(bounds)

        return paper
Exemplo n.º 19
0
def test_square_bounds():
    p = Pen()
    p.fill_mode()
    p.move_to((1, 1))
    p.square(4)

    assert_equal(
        p.paper.bounds(),
        Bounds(-1, -1, 3, 3)
    )
Exemplo n.º 20
0
def test_text_translate():
    p = Pen()
    p.move_to((0, 0))
    p.text('abcd', 1)
    paper = p.paper

    paper.translate((2, 3))
    svg_data = paper.format_svg(0)
    assert ('<text x="2" y="-3" font-family="sans-serif" font-size="1" '
            'fill="#000000">abcd</text>') in svg_data
Exemplo n.º 21
0
def test_translate_override_bounds():
    # Translate a paper that has overridden bounds. The bounds update as well.
    paper = Paper()
    paper.override_bounds(0, 0, 1, 1)
    paper.translate((3, 4))
    assert_equal(paper.bounds(), Bounds(3, 4, 4, 5))

    # When bounds=False is passed, then the bounds do not update.
    paper = Paper()
    paper.override_bounds(0, 0, 1, 1)
    paper.translate((3, 4), bounds=False)
    assert_equal(paper.bounds(), Bounds(0, 0, 1, 1))

    # This also works if the bounds are not overridden.
    p = Pen()
    p.fill_mode()
    p.move_to((0.5, 0.5))
    p.circle(0.5)
    assert_equal(p.paper.bounds(), Bounds(0, 0, 1, 1))

    p.paper.translate((3, 4), bounds=False)

    assert_equal(p.paper.bounds(), Bounds(0, 0, 1, 1))
    assert_equal(p.last_path().bounds(), Bounds(3, 4, 4, 5))
Exemplo n.º 22
0
def draw():

    p = Pen()

    # Draw sine waves in various widths.
    for width in [0.01, 0.1, 0.3, 0.5, 0.8, 1.0]:
        p.stroke_mode(width)

        func = sine_func_factory(
            amplitude=1.0,
            frequency=4 / math.pi,
            phase=0,
        )
        p.parametric(
            func,
            start=0,
            end=10,
            step=0.1,
        )
        # Next line.
        p.turn_to(-90)
        p.move_forward(1.0 + 2 * width)

    return p.paper
Exemplo n.º 23
0
def test_text_merge():
    p = Pen()
    p.move_to((0, 0))
    p.text('abcd', 1)
    paper1 = p.paper
    assert '<text' in paper1.format_svg(0)

    paper2 = Paper()
    paper2.merge(paper1)
    assert '<text' in paper2.format_svg(0)

    paper3 = Paper()
    paper3.merge_under(paper1)
    assert '<text' in paper3.format_svg(0)
Exemplo n.º 24
0
    def draw_character(self, mode, **kwargs):
        bottom_ending = self.bottom_ending_class(
            self,
            self.bottom_straight,
            self.bottom_flipped,
        )

        pen = Pen()
        pen.set_mode(mode)
        pen.move_to((0, MIDDLE - 0.5))
        self.draw(pen)
        bottom_ending.draw(pen)

        paper = pen.paper

        bounds = paper.bounds()
        bounds.top = MIDDLE
        bounds.bottom = UNDER
        paper.override_bounds(bounds)

        return paper
Exemplo n.º 25
0
    def draw():
        p = Pen()
        p.fill_mode()
        p.move_to((0, 0))
        p.circle(2)
        paper1 = p.paper

        p = Pen()
        p.fill_mode()
        p.move_to((3, 0))
        p.circle(1)
        paper2 = p.paper

        return paper1, paper2
Exemplo n.º 26
0
def test_fuse_with_joint():
    p = Pen()
    p.stroke_mode(2.0)

    p.move_to((0, 0))
    p.turn_to(180)
    p.line_forward(5)
    p.turn_left(90)
    p.line_forward(5)

    p.break_stroke()

    p.move_to((0, 0))
    p.turn_to(0)
    p.line_forward(5)

    assert_path_data(p, 0, [
        'M0,1 L0,-1 L-6,-1 L-6,5 L-4,5 L-4,1 L0,1 z',
        'M0,-1 L0,1 L5,1 L5,-1 L0,-1 z',
    ])

    p.paper.join_paths()
    p.paper.fuse_paths()

    assert_path_data(p, 0, 'M-6,5 L-4,5 L-4,1 L5,1 L5,-1 L-6,-1 L-6,5 z')
Exemplo n.º 27
0
def test_join_paths():
    # Join two paths starting from the same point.
    p = Pen()
    p.fill_mode()

    p.move_to((1, 0))
    p.line_to((0, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((2, 0))

    p.paper.join_paths()

    assert_path_data(
        p, 0,
        'M0,0 L1,0 L2,0',
    )

    # Join two paths that end in the same point.
    p = Pen()
    p.fill_mode()

    p.move_to((1, 0))
    p.line_to((0, 0))
    p.break_stroke()
    p.move_to((2, 0))
    p.line_to((1, 0))

    p.paper.join_paths()

    assert_path_data(
        p, 0,
        'M0,0 L1,0 L2,0',
    )

    # Join three paths going left in normal order.
    p = Pen()
    p.fill_mode()

    p.move_to((3, 0))
    p.line_to((2, 0))
    p.break_stroke()
    p.move_to((2, 0))
    p.line_to((1, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((0, 0))

    p.paper.join_paths()

    assert_path_data(
        p, 0,
        'M3,0 L2,0 L1,0 L0,0',
    )

    # Join three paths going right in normal order.
    p = Pen()
    p.fill_mode()

    p.move_to((0, 0))
    p.line_to((1, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((2, 0))
    p.break_stroke()
    p.move_to((2, 0))
    p.line_to((3, 0))

    p.paper.join_paths()

    assert_path_data(
        p, 0,
        'M0,0 L1,0 L2,0 L3,0',
    )

    # Join three paths going left in reverse order.
    p = Pen()
    p.fill_mode()

    p.move_to((1, 0))
    p.line_to((0, 0))
    p.break_stroke()
    p.move_to((2, 0))
    p.line_to((1, 0))
    p.break_stroke()
    p.move_to((3, 0))
    p.line_to((2, 0))

    p.paper.join_paths()

    assert_path_data(
        p, 0,
        'M0,0 L1,0 L2,0 L3,0',
    )

    # Join three paths going right in reverse order.
    p = Pen()
    p.fill_mode()

    p.move_to((2, 0))
    p.line_to((3, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((2, 0))
    p.break_stroke()
    p.move_to((0, 0))
    p.line_to((1, 0))

    p.paper.join_paths()

    assert_path_data(
        p, 0,
        'M3,0 L2,0 L1,0 L0,0',
    )

    # Join multiple paths together.
    p = Pen()
    p.fill_mode()

    p.move_to((1, 0))
    p.line_to((0, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((1, 1))
    p.break_stroke()
    p.move_to((1, 1))
    p.line_to((2, 1))
    p.break_stroke()
    p.move_to((2, 2))
    p.line_to((2, 1))
    p.break_stroke()

    p.paper.join_paths()

    assert_path_data(
        p, 0,
        'M0,0 L1,0 L1,-1 L2,-1 L2,-2'
    )

    # Join three paths so one path must reverse multiple times.
    p = Pen()
    p.fill_mode()

    p.move_to((2, 0))
    p.line_to((1, 0))
    p.break_stroke()
    p.move_to((2, 0))
    p.line_to((3, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((0, 0))

    p.paper.join_paths()

    assert_path_data(
        p, 0,
        'M3,0 L2,0 L1,0 L0,0',
    )
Exemplo n.º 28
0
def test_fuse_with_joint():
    p = Pen()
    p.stroke_mode(2.0)

    p.move_to((0, 0))
    p.turn_to(180)
    p.line_forward(5)
    p.turn_left(90)
    p.line_forward(5)

    p.break_stroke()

    p.move_to((0, 0))
    p.turn_to(0)
    p.line_forward(5)

    assert_path_data(
        p, 0,
        [
            'M0,1 L0,-1 L-6,-1 L-6,5 L-4,5 L-4,1 L0,1 z',
            'M0,-1 L0,1 L5,1 L5,-1 L0,-1 z',
        ]
    )

    p.paper.join_paths()
    p.paper.fuse_paths()

    assert_path_data(
        p, 0,
        'M-6,5 L-4,5 L-4,1 L5,1 L5,-1 L-6,-1 L-6,5 z'
    )
Exemplo n.º 29
0
def test_fuse_paths():
    # Create two halves of a stroke in the same direction.
    p = Pen()
    p.stroke_mode(sqrt2)

    p.move_to((-3, 3))
    p.turn_to(-45)
    p.line_forward(3 * sqrt2, start_slant=0)
    p.line_forward(3 * sqrt2, end_slant=0)

    p.paper.fuse_paths()

    assert_path_data(
        p, 1,
        ['M-2.0,-3.0 L-4.0,-3.0 L2.0,3.0 L4.0,3.0 L-2.0,-3.0 z']
    )
Exemplo n.º 30
0
def test_join_paths_loop():
    # Already looped paths should not be affected by join_paths.
    p = Pen()
    p.fill_mode()

    p.move_to((0, 0))
    p.square(2)

    target = 'M-1,1 L1,1 L1,-1 L-1,-1 L-1,1 z'
    assert_path_data(p, 0, target)
    p.paper.join_paths()
    assert_path_data(p, 0, target)

    # Loops can also be created by joining paths.
    p = Pen()
    p.fill_mode()

    p.move_to((0, 0))
    p.line_to((1, 0))
    p.line_to((1, 1))
    p.break_stroke()
    p.line_to((0, 1))
    p.line_to((0, 0))

    p.paper.join_paths()
    assert_path_data(
        p, 0,
        'M1,-1 L1,0 L0,0 L0,-1 L1,-1 z'
    )

    # The joins can get complicated.
    p = Pen()
    p.fill_mode()

    p.move_to((3, 0))
    p.line_to((2, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((2, 2))
    p.break_stroke()
    p.move_to((4, 0))
    p.line_to((3, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((2, 0))
    p.break_stroke()
    p.move_to((4, 0))
    p.line_to((2, 2))

    p.paper.join_paths()

    assert_path_data(
        p, 0,
        'M1,0 L2,-2 L4,0 L3,0 L2,0 L1,0 z',
    )
Exemplo n.º 31
0
def test_join_paths_reference():
    # Join paths in such a way that a single path object must be
    # used as both the "left" and "right" path in different joins.
    p = Pen()
    p.fill_mode()

    p.move_to((3, 0))
    p.line_to((2, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((0, 0))
    p.break_stroke()
    p.move_to((4, 0))
    p.line_to((3, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((2, 0))
    p.break_stroke()
    p.move_to((4, 0))
    p.line_to((5, 0))

    p.paper.join_paths()

    assert_path_data(p, 0, 'M5,0 L4,0 L3,0 L2,0 L1,0 L0,0')
Exemplo n.º 32
0
def test_join_paths_thick():
    # Segments join together if possible when join_paths is called.
    p = Pen()
    p.stroke_mode(2.0)
    p.move_to((0, 0))
    p.turn_to(0)
    p.line_forward(5)
    p.break_stroke()
    p.turn_left(90)
    p.line_forward(5)
    p.paper.join_paths()
    assert_path_data(p, 0, 'M0,-1 L0,1 L6,1 L6,-5 L4,-5 L4,-1 L0,-1 z')
Exemplo n.º 33
0
def test_copy_no_mode():
    p = Pen()
    assert_raises(AttributeError, lambda: p.mode)
    p = p.copy()
    assert_raises(AttributeError, lambda: p.mode)
Exemplo n.º 34
0
def test_fuse_paths():
    # Create two halves of a stroke in the same direction.
    p = Pen()
    p.stroke_mode(sqrt2)

    p.move_to((-3, 3))
    p.turn_to(-45)
    p.line_forward(3 * sqrt2, start_slant=0)
    p.line_forward(3 * sqrt2, end_slant=0)

    p.paper.fuse_paths()

    assert_path_data(p, 1,
                     ['M-2.0,-3.0 L-4.0,-3.0 L2.0,3.0 L4.0,3.0 L-2.0,-3.0 z'])
Exemplo n.º 35
0
def test_copy_custom_cap():
    # Regression test for a bug where doing pen.copy() in a cap function would
    # break outline drawing.
    p = Pen()
    p.stroke_mode(2.0)

    p.move_to((0, 0))
    p.turn_to(0)
    p.line_forward(5)
    p.turn_left(90)
    p.line_forward(5)

    def copy_cap(pen, end):
        pen.copy()
        pen.line_to(end)

    p.last_segment().end_cap = copy_cap

    assert_path_data(p, 0, 'M0,-1 L0,1 L6,1 L6,-5 L4,-5 L4,-1 L0,-1 z')
Exemplo n.º 36
0
def test_join_paths():
    # Join two paths starting from the same point.
    p = Pen()
    p.fill_mode()

    p.move_to((1, 0))
    p.line_to((0, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((2, 0))

    p.paper.join_paths()

    assert_path_data(
        p,
        0,
        'M0,0 L1,0 L2,0',
    )

    # Join two paths that end in the same point.
    p = Pen()
    p.fill_mode()

    p.move_to((1, 0))
    p.line_to((0, 0))
    p.break_stroke()
    p.move_to((2, 0))
    p.line_to((1, 0))

    p.paper.join_paths()

    assert_path_data(
        p,
        0,
        'M0,0 L1,0 L2,0',
    )

    # Join three paths going left in normal order.
    p = Pen()
    p.fill_mode()

    p.move_to((3, 0))
    p.line_to((2, 0))
    p.break_stroke()
    p.move_to((2, 0))
    p.line_to((1, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((0, 0))

    p.paper.join_paths()

    assert_path_data(
        p,
        0,
        'M3,0 L2,0 L1,0 L0,0',
    )

    # Join three paths going right in normal order.
    p = Pen()
    p.fill_mode()

    p.move_to((0, 0))
    p.line_to((1, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((2, 0))
    p.break_stroke()
    p.move_to((2, 0))
    p.line_to((3, 0))

    p.paper.join_paths()

    assert_path_data(
        p,
        0,
        'M0,0 L1,0 L2,0 L3,0',
    )

    # Join three paths going left in reverse order.
    p = Pen()
    p.fill_mode()

    p.move_to((1, 0))
    p.line_to((0, 0))
    p.break_stroke()
    p.move_to((2, 0))
    p.line_to((1, 0))
    p.break_stroke()
    p.move_to((3, 0))
    p.line_to((2, 0))

    p.paper.join_paths()

    assert_path_data(
        p,
        0,
        'M0,0 L1,0 L2,0 L3,0',
    )

    # Join three paths going right in reverse order.
    p = Pen()
    p.fill_mode()

    p.move_to((2, 0))
    p.line_to((3, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((2, 0))
    p.break_stroke()
    p.move_to((0, 0))
    p.line_to((1, 0))

    p.paper.join_paths()

    assert_path_data(
        p,
        0,
        'M3,0 L2,0 L1,0 L0,0',
    )

    # Join multiple paths together.
    p = Pen()
    p.fill_mode()

    p.move_to((1, 0))
    p.line_to((0, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((1, 1))
    p.break_stroke()
    p.move_to((1, 1))
    p.line_to((2, 1))
    p.break_stroke()
    p.move_to((2, 2))
    p.line_to((2, 1))
    p.break_stroke()

    p.paper.join_paths()

    assert_path_data(p, 0, 'M0,0 L1,0 L1,-1 L2,-1 L2,-2')

    # Join three paths so one path must reverse multiple times.
    p = Pen()
    p.fill_mode()

    p.move_to((2, 0))
    p.line_to((1, 0))
    p.break_stroke()
    p.move_to((2, 0))
    p.line_to((3, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((0, 0))

    p.paper.join_paths()

    assert_path_data(
        p,
        0,
        'M3,0 L2,0 L1,0 L0,0',
    )
Exemplo n.º 37
0
def test_join_paths_turn_back_no_joint():
    p = Pen()
    p.stroke_mode(1.0)
    p.move_to((0, 0))
    p.turn_to(0)
    p.line_forward(10)
    p.turn_right(180)
    p.break_stroke()
    p.line_forward(5)
    p.paper.join_paths()

    line1, line2 = p.last_path().segments
    assert line1.end_joint_illegal
    assert line2.start_joint_illegal

    assert_path_data(p, 1,
                     ('M0.0,-0.5 L0.0,0.5 L10.0,0.5 L10.0,-0.5 '
                      'L5.0,-0.5 L5.0,0.5 L10.0,0.5 L10.0,-0.5 L0.0,-0.5 z'))
Exemplo n.º 38
0
def test_two_pens_one_paper():
    paper = Paper()
    p1 = Pen(paper)
    p2 = Pen(paper)
    p1.fill_mode()
    p2.fill_mode()
    p1.move_to((0, 0))
    p2.move_to((0, 0))
    p1.line_to((0, 1))
    p2.line_to((2, 0))

    assert_path_data(paper, 0, ['M0,0 L0,-1', 'M0,0 L2,0'])
Exemplo n.º 39
0
def test_translate():
    p = Pen()
    p.stroke_mode(1.0)

    p.move_to((0, 0))
    p.turn_to(0)
    p.line_forward(3)
    p.arc_left(90, 3)
    p.turn_left(90)
    p.move_forward(3)
    p.fill_mode()
    p.circle(0.5)
    p.move_forward(3)
    p.square(1)

    p.paper.translate((1, 1))

    assert_equal(p.paper.svg_elements(1), [
        ('<path d="M1.0,-1.5 L1.0,-0.5 L4.0,-0.5 A 3.5,3.5 0 0 0 '
         '7.5,-4.0 L6.5,-4.0 A 2.5,2.5 0 0 1 4.0,-1.5 L1.0,-1.5 z" '
         'fill="#000000" />'),
        ('<path d="M4.5,-4.0 A 0.5,0.5 0 0 0 3.5,-4.0 '
         'A 0.5,0.5 0 0 0 4.5,-4.0 z" fill="#000000" />'),
        ('<path d="M0.5,-3.5 L1.5,-3.5 L1.5,-4.5 L0.5,-4.5 L0.5,-3.5 z" '
         'fill="#000000" />'),
    ])
Exemplo n.º 40
0
    def draw_character(self, mode, **kwargs):
        side_ending = self.side_ending_class(
            self,
            self.side_flipped,
        )

        paper = Paper()

        pen = Pen()
        pen.set_mode(mode)
        pen.move_to((0, TOP - mode.width / 2))
        pen.turn_to(0)
        pen.line_forward(2.0)
        pen.last_segment().start_cap = stub_cap

        side_ending.draw(pen)
        paper.merge(pen.paper)

        bounds = paper.bounds()
        bounds.top = OVER
        bounds.bottom = MIDDLE
        bounds.left = 0
        paper.override_bounds(bounds)

        return paper
Exemplo n.º 41
0
def test_join_paths_reference():
    # Join paths in such a way that a single path object must be
    # used as both the "left" and "right" path in different joins.
    p = Pen()
    p.fill_mode()

    p.move_to((3, 0))
    p.line_to((2, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((0, 0))
    p.break_stroke()
    p.move_to((4, 0))
    p.line_to((3, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((2, 0))
    p.break_stroke()
    p.move_to((4, 0))
    p.line_to((5, 0))

    p.paper.join_paths()

    assert_path_data(
        p, 0,
        'M5,0 L4,0 L3,0 L2,0 L1,0 L0,0'
    )
Exemplo n.º 42
0
def test_join_paths_loop():
    # Already looped paths should not be affected by join_paths.
    p = Pen()
    p.fill_mode()

    p.move_to((0, 0))
    p.square(2)

    target = 'M-1,1 L1,1 L1,-1 L-1,-1 L-1,1 z'
    assert_path_data(p, 0, target)
    p.paper.join_paths()
    assert_path_data(p, 0, target)

    # Loops can also be created by joining paths.
    p = Pen()
    p.fill_mode()

    p.move_to((0, 0))
    p.line_to((1, 0))
    p.line_to((1, 1))
    p.break_stroke()
    p.line_to((0, 1))
    p.line_to((0, 0))

    p.paper.join_paths()
    assert_path_data(p, 0, 'M1,-1 L1,0 L0,0 L0,-1 L1,-1 z')

    # The joins can get complicated.
    p = Pen()
    p.fill_mode()

    p.move_to((3, 0))
    p.line_to((2, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((2, 2))
    p.break_stroke()
    p.move_to((4, 0))
    p.line_to((3, 0))
    p.break_stroke()
    p.move_to((1, 0))
    p.line_to((2, 0))
    p.break_stroke()
    p.move_to((4, 0))
    p.line_to((2, 2))

    p.paper.join_paths()

    assert_path_data(
        p,
        0,
        'M1,0 L2,-2 L4,0 L3,0 L2,0 L1,0 z',
    )
Exemplo n.º 43
0
def test_join_paths_thick():
    # Segments join together if possible when join_paths is called.
    p = Pen()
    p.stroke_mode(2.0)
    p.move_to((0, 0))
    p.turn_to(0)
    p.line_forward(5)
    p.break_stroke()
    p.turn_left(90)
    p.line_forward(5)
    p.paper.join_paths()
    assert_path_data(
        p, 0,
        'M0,-1 L0,1 L6,1 L6,-5 L4,-5 L4,-1 L0,-1 z'
    )
Exemplo n.º 44
0
    return numpy.column_stack((t, c, s))


def draw_parametric_func(pen, f, t_range):
    txy_values = f(t_range)
    t, x, y = txy_values[0]
    pen.move_to((x, y))
    for t, x, y in txy_values[1:]:
        pen.line_to((x, y))
        mod = t % 1.0
        if float_equal(mod, 0) or float_equal(mod, 1.0):
            pen.circle(0.01)


step = 0.01
t_range = numpy.arange(-4 + step, 4, step)

pen = Pen()
pen.stroke_mode(0.01, 'green')
draw_parametric_func(pen, euler_spiral_parametric, t_range)

pen.fill_mode('green')
pen.move_to((0.5, 0.5))
pen.circle(0.01)
pen.move_to((-0.5, -0.5))
pen.circle(0.01)

print(pen.paper.format_svg(5, resolution=500))

# TODO: euler spiral solver to end at a particular point. newton-raphson method for root finding convergence?
Exemplo n.º 45
0
def test_join_and_fuse_simple():
    # Create two halves of a stroke in separate directions.
    p = Pen()
    p.stroke_mode(sqrt2)

    p.move_to((0, 0))
    p.turn_to(-45)
    p.line_forward(3 * sqrt2, end_slant=0)

    p.break_stroke()

    p.move_to((0, 0))
    p.turn_to(-45 + 180)
    p.line_forward(3 * sqrt2, end_slant=0)

    p.paper.join_paths()
    p.paper.fuse_paths()

    assert_path_data(
        p, 1,
        'M2.0,3.0 L4.0,3.0 L-2.0,-3.0 L-4.0,-3.0 L2.0,3.0 z'
    )
Exemplo n.º 46
0
def test_mirror_end_slant():
    paper = Paper()

    p = Pen()
    p.stroke_mode(sqrt2)
    p.move_to((0, 0))
    p.turn_to(-45)
    p.line_forward(5 * sqrt2, end_slant=45)
    p.paper.mirror_x(0)
    paper.merge(p.paper)

    p = Pen()
    p.stroke_mode(sqrt2)
    p.move_to((0, 0))
    p.turn_to(45)
    p.line_forward(5 * sqrt2)
    paper.merge(p.paper)

    paper.join_paths()
    paper.fuse_paths()

    assert_path_data(paper, 1,
                     'M-5.5,4.5 L-4.5,5.5 L5.5,-4.5 L4.5,-5.5 L-5.5,4.5 z')
Exemplo n.º 47
0
def test_join_paths_turn_back_no_joint():
    p = Pen()
    p.stroke_mode(1.0)
    p.move_to((0, 0))
    p.turn_to(0)
    p.line_forward(10)
    p.turn_right(180)
    p.break_stroke()
    p.line_forward(5)
    p.paper.join_paths()

    line1, line2 = p.last_path().segments
    assert line1.end_joint_illegal
    assert line2.start_joint_illegal

    assert_path_data(
        p, 1,
        (
            'M0.0,-0.5 L0.0,0.5 L10.0,0.5 L10.0,-0.5 '
            'L5.0,-0.5 L5.0,0.5 L10.0,0.5 L10.0,-0.5 L0.0,-0.5 z'
        )
    )
Exemplo n.º 48
0
import math
from canoepaddle import Pen
from canoepaddle.heading import Heading, Angle

p = Pen()
p.paper.override_bounds(-120, -120, 120, 120)
p.stroke_mode(1.0, '#15A')

p.move_to((0.5, 0.5))


def f(n):
    a = 12
    b = 0.03
    c = 0.2
    d = 1.5
    e = 0.5
    wobble = a * math.exp(-b * n) * math.sin(c * n + d * n**e)
    return (
        Angle(-24 + wobble),
        Angle(24 + wobble),
    )


center_heading = Heading(90)
center = p.position

p.turn_to(center_heading)

num_layers = 26
for layer in range(num_layers):
Exemplo n.º 49
0
    def draw_character(self, mode, fuse=True):
        side_ending = self.side_ending_class(self, self.side_flipped)
        bottom_ending = self.bottom_ending_class(
            self,
            self.bottom_straight,
            self.bottom_flipped,
        )

        paper = Paper()

        # When drawing the body of the consonant, subclasses will start
        # where the side ending is, and end where the bottom ending is.
        pen = Pen()
        pen.set_mode(mode)
        pen.move_to((0, TOP - pen.mode.width / 2))
        side_ending_position = pen.position
        self.draw(pen)
        bottom_ending_position = pen.position
        bottom_ending_heading = pen.heading
        paper.merge(pen.paper)

        # Draw the side ending.
        pen = Pen()
        pen.set_mode(mode)
        pen.move_to(side_ending_position)
        pen.turn_to(0)
        side_ending.draw(pen)
        paper.merge(pen.paper)

        # Draw the bottom ending.
        pen = Pen()
        pen.set_mode(mode)
        pen.move_to(bottom_ending_position)

        # If the bottom orientation is slanted left, then we have to
        # start the bottom ending from a flipped heading so when it flips
        # again later it will be correct.
        if not self.bottom_straight and self.bottom_flipped:
            bottom_ending_heading = bottom_ending_heading.flipped_x()
        pen.turn_to(bottom_ending_heading)

        # Draw the ending, and maybe flip it horizontally.
        bottom_ending.draw(pen)
        if not self.bottom_straight and self.bottom_flipped:
            pen.paper.mirror_x(bottom_ending_position.x)
        paper.merge(pen.paper)

        if fuse:
            paper.join_paths()
            paper.fuse_paths()

        # Override the bounds so that the letter reaches the full line height.
        bounds = paper.bounds()
        bounds.bottom = UNDER
        bounds.top = OVER
        paper.override_bounds(bounds)

        # We need to center on x=0 here because otherwise flipped
        # consonants wouldn't flip at the right x value.
        paper.center_on_x(0)

        return paper
Exemplo n.º 50
0
def test_join_and_fuse_simple():
    # Create two halves of a stroke in separate directions.
    p = Pen()
    p.stroke_mode(sqrt2)

    p.move_to((0, 0))
    p.turn_to(-45)
    p.line_forward(3 * sqrt2, end_slant=0)

    p.break_stroke()

    p.move_to((0, 0))
    p.turn_to(-45 + 180)
    p.line_forward(3 * sqrt2, end_slant=0)

    p.paper.join_paths()
    p.paper.fuse_paths()

    assert_path_data(p, 1,
                     'M2.0,3.0 L4.0,3.0 L-2.0,-3.0 L-4.0,-3.0 L2.0,3.0 z')
black = '#260003'
gray = '#233042'
mode = StrokeOutlineMode(1.0, 0.2, red, black)

papers = []
for consonant_class in consonants:
    letter = consonant_class(se.Normal, be.Normal)
    letter_paper = draw_letter(
        letter,
        mode,
        fixed_width=27.0,
        show_template=True,
    )
    letter_paper.translate((5, 0), bounds=False)

    p = Pen()
    p.move_to((-5, 0))
    p.text(
        convert_ascii_to_html(consonant_class.pronunciation),
        8.0,
        'Caudex',
        '#233042',
        centered=True,
    )
    letter_paper.merge(p.paper)
    papers.append(letter_paper)

page = typeset(
    papers,
    letter_spacing=1.0,
    letters_per_line=6,
Exemplo n.º 52
0
from canoepaddle import Pen

p = Pen()
p.stroke_mode(0.15)


def trefoil(origin, radius, num_leaves, leaf_angle, step=1):
    p.turn_to(90)
    points = []
    for i in range(num_leaves):
        p.move_to(origin)
        p.turn_right(360 / num_leaves)
        p.move_forward(radius)
        points.append(p.position)

    p.move_to(points[0])
    for i in range(num_leaves):
        next_point = points[((i + 1) * step) % num_leaves]
        p.turn_toward(origin)
        p.turn_right(leaf_angle / 2)
        p.arc_to(next_point)


trefoil((-6, 6), 3, 3, 110)
trefoil((0, 6), 2.7, 4, 120)
trefoil((6, 6), 2.7, 4, 70)
trefoil((-6, 0), 2.7, 5, 70)
trefoil((0, 0), 2.7, 5, 130)
trefoil((6, 0), 2.7, 5, 110, step=2)
trefoil((-6, -6), 2.7, 31, 20, step=14)
trefoil((0, -6), 3, 8, 120, step=3)
Exemplo n.º 53
0
def test_arc_segment_bounds():
    # Arc which occupies its entire circle.
    p = Pen()
    p.fill_mode()
    p.move_to((1, 0))
    p.turn_to(90)
    p.arc_left(359, 1)

    arc = p.last_segment()
    assert_equal(
        arc.bounds(),
        Bounds(-1, -1, 1, 1)
    )

    # Arc which pushes the boundary only with the endpoints.
    p = Pen()
    p.fill_mode()
    p.move_to((0, 0))
    p.turn_to(30)
    p.move_forward(1)
    p.turn_left(90)
    p.arc_left(30, center=(0, 0))

    arc = p.last_segment()
    assert_equal(
        arc.bounds(),
        Bounds(0.5, 0.5, sqrt3 / 2, sqrt3 / 2)
    )

    # Arc which pushes the boundary with the middle in one spot.
    p = Pen()
    p.fill_mode()
    p.move_to((0, 0))
    p.turn_to(-45)
    p.move_forward(1)
    p.turn_left(90)
    p.arc_left(90, center=(0, 0))

    arc = p.last_segment()
    assert_equal(
        arc.bounds(),
        Bounds(sqrt2 / 2, -sqrt2 / 2, 1, sqrt2 / 2)
    )

    # Arc which goes right.
    p = Pen()
    p.fill_mode()
    p.move_to((0, 0))
    p.turn_to(45)
    p.arc_right(90, 3)

    arc = p.last_segment()
    assert_equal(
        arc.bounds(),
        Bounds(0, 0, 3 * sqrt2, 3 - 1.5 * sqrt2)
    )

    # Arc which pushes the boundary with the middle in two spots.
    p = Pen()
    p.fill_mode()
    p.move_to((0, 0))
    p.turn_to(-45)
    p.move_forward(1)
    p.turn_left(90)
    p.arc_left(180, center=(0, 0))

    arc = p.last_segment()
    assert_equal(
        arc.bounds(),
        Bounds(-sqrt2 / 2, -sqrt2 / 2, 1, 1)
    )

    # Half circle, right side
    p = Pen()
    p.fill_mode()
    p.move_to((0, 0))
    p.turn_to(0)
    p.arc_right(180, 5)

    arc = p.last_segment()
    assert_equal(
        arc.bounds(),
        Bounds(0, -10, 5, 0)
    )

    # Thick circle,
    p = Pen()
    p.stroke_mode(1.0)
    p.move_to((0, 0))
    p.turn_to(0)
    p.move_forward(5)
    p.turn_left(90)
    p.arc_left(180, 5, start_slant=45)

    arc = p.last_segment()
    assert_equal(
        arc.bounds(),
        Bounds(-5.5, -0.5314980314970469, 5.5, 5.5)
    )
Exemplo n.º 54
0
def test_center_on_xy():
    p = Pen()
    p.stroke_mode(2.0)
    p.move_to((0, 0))
    p.turn_to(0)
    p.line_forward(4)

    p.move_to((2, 1))
    p.circle(1)

    p.paper.center_on_x(0)

    assert_equal(p.paper.svg_elements(0), [
        '<path d="M-2,-1 L-2,1 L2,1 L2,-1 L-2,-1 z" fill="#000000" />',
        '<path d="M2,-1 A 2,2 0 0 0 -2,-1 A 2,2 0 0 0 2,-1 z" fill="#000000" />',
    ])

    p.paper.center_on_y(0)

    assert_equal(p.paper.svg_elements(1), [
        ('<path d="M-2.0,0.0 L-2.0,2.0 L2.0,2.0 L2.0,0.0 L-2.0,0.0 z" '
         'fill="#000000" />'),
        ('<path d="M2.0,0.0 A 2.0,2.0 0 0 0 -2.0,0.0 '
         'A 2.0,2.0 0 0 0 2.0,0.0 z" fill="#000000" />'),
    ])
Exemplo n.º 55
0
def test_paper_merge():
    # Merge two drawings together.
    paper = Paper()

    p = Pen()
    p.fill_mode()
    p.turn_to(0)
    p.arc_left(180, 5)
    p.paper.center_on_x(0)
    paper.merge(p.paper)

    p = Pen()
    p.fill_mode()
    p.turn_to(180)
    p.arc_left(180, 5)
    p.paper.center_on_x(0)
    paper.merge(p.paper)

    assert_path_data(paper, 1, [
        'M-2.5,0.0 A 5.0,5.0 0 0 0 -2.5,-10.0',
        'M2.5,0.0 A 5.0,5.0 0 0 0 2.5,10.0',
    ])
Exemplo n.º 56
0
    def draw_character(self, mode, fuse=True):
        paper = Paper()

        top_ending = self.top_ending_class(
            self,
            self.top_straight,
            self.top_flipped,
        )
        bottom_ending = self.bottom_ending_class(
            self,
            self.bottom_straight,
            self.bottom_flipped,
        )

        # While drawing the body of the primary character, two copies of the
        # pen are created, one ready to draw each of the endings.
        pen = Pen()
        pen.set_mode(mode)

        top_pen, bottom_pen = self.draw(pen)
        top_start_position = top_pen.position
        bottom_start_position = bottom_pen.position

        # Draw the endings.
        # If the ending orientations are to the left, then we have to flip them
        # horizontally.
        # The top ending is drawn as if it were a bottom ending, so
        # mirror it to the top.
        if self.top_flipped:
            top_pen.turn_to(top_pen.heading.flipped_x())
        top_pen.move_to(top_pen.position.flipped_y(MIDDLE))
        top_pen.turn_to(top_pen.heading.flipped_y())
        top_ending.draw(top_pen)
        top_pen.paper.mirror_y(MIDDLE)
        if self.top_flipped:
            top_pen.paper.mirror_x(top_start_position.x)

        if self.bottom_flipped:
            bottom_pen.turn_to(bottom_pen.heading.flipped_x())
        bottom_ending.draw(bottom_pen)
        if self.bottom_flipped:
            bottom_pen.paper.mirror_x(bottom_start_position.x)

        paper.merge(pen.paper)
        paper.merge(top_pen.paper)
        paper.merge(bottom_pen.paper)
        if fuse:
            paper.join_paths()
            paper.fuse_paths()

        # Override the bounds so that the letter reaches the full line height.
        bounds = paper.bounds()
        bounds.bottom = UNDER
        bounds.top = OVER
        paper.override_bounds(bounds)

        # We need to center on x=0 here because otherwise flipped
        # consonants wouldn't flip at the right x value.
        paper.center_on_x(0)

        return paper
Exemplo n.º 57
0
def test_line_segment_bounds():
    # Fill mode segment.
    p = Pen()
    p.fill_mode()
    p.move_to((1, 0))
    p.line_to((2, 3))

    line = p.last_segment()
    assert_equal(
        line.bounds(),
        Bounds(1, 0, 2, 3)
    )

    # Stroke mode segment.
    p = Pen()
    p.stroke_mode(sqrt2)
    p.move_to((0, 0))
    p.line_to((5, 5))

    line = p.last_segment()
    assert_equal(
        line.bounds(),
        Bounds(-0.5, -0.5, 5.5, 5.5)
    )
Exemplo n.º 58
0
        angle = 25
        p.stroke_mode(0.30, gray)
        p.turn_to(90 - angle)
        p.arc_to(top)
        p.turn_left(180 - 2 * angle)
        p.arc_to(bottom)

    p.move_to((-2, 1))
    eye()
    p.move_to((2, 1))
    eye()

    # Ears.
    p.stroke_mode(1.0, gray)
    p.move_to((1, 5))
    p.turn_to(20)
    p.line_forward(3.5)
    p.turn_to(-85)
    p.line_forward(4)
    p.move_to((-1, 5))
    p.turn_to(180 - 20)
    p.line_forward(3.5)
    p.turn_to(180 + 85)
    p.line_forward(4)


if __name__ == '__main__':
    p = Pen()
    draw(p)
    print(p.paper.format_svg())