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" />'), ])
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" />' ), ] )
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
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) )
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', )
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', )