test/hexapdf/content/test_canvas.rb in hexapdf-0.6.0 vs test/hexapdf/content/test_canvas.rb in hexapdf-0.7.0

- old
+ new

@@ -81,11 +81,11 @@ @canvas.save_graphics_state assert_operators(@canvas.contents, [[:save_graphics_state]]) end it "is serialized correctly when a block is used" do - @canvas.save_graphics_state { } + @canvas.save_graphics_state {} assert_operators(@canvas.contents, [[:save_graphics_state], [:restore_graphics_state]]) end it "saves needed font state information not stored in the graphics state" do @canvas.save_graphics_state do @@ -712,12 +712,12 @@ describe "draw" do it "draws the, optionally configured, graphic object onto the canvas" do obj = Object.new obj.define_singleton_method(:options) { @options } - obj.define_singleton_method(:configure) {|**kwargs| @options = kwargs; self} - obj.define_singleton_method(:draw) {|canvas| canvas.move_to(@options[:x], @options[:y])} + obj.define_singleton_method(:configure) {|**kwargs| @options = kwargs; self } + obj.define_singleton_method(:draw) {|canvas| canvas.move_to(@options[:x], @options[:y]) } @canvas.draw(obj, x: 5, y: 6) assert_operators(@canvas.contents, [[:move_to, [5, 6]]]) end it "returns the canvas object" do @@ -959,11 +959,11 @@ describe "text_matrix" do it "invokes the operator implementation" do @canvas.text_matrix(1, 2, 3, 4, 5, 6) assert_operators(@canvas.contents, [[:begin_text], - [:set_text_matrix, [1, 2, 3, 4, 5,6]]]) + [:set_text_matrix, [1, 2, 3, 4, 5, 6]]]) end it "returns the canvas object" do assert_equal(@canvas, @canvas.text_matrix(1, 1, 1, 1, 1, 1)) end @@ -1000,11 +1000,11 @@ @canvas.move_text_cursor(offset: [5, 10]) assert_equal([5, 10], @canvas.text_cursor) end it "fails if invoked outside a text object" do - assert_raises_in_graphics_object(:none, :path, :clipping_path) { @canvas.text_cursor } + assert_raises_in_graphics_object(:none, :path, :clipping_path) { @canvas.text_cursor } end end describe "font" do it "returns the set font" do @@ -1013,11 +1013,10 @@ assert_same(@doc.fonts.add("Times"), @canvas.font) @canvas.font(@canvas.font) assert_same(@doc.fonts.add("Times"), @canvas.font) @canvas.font("Helvetica", size: 10) assert_operators(@canvas.contents, [[:set_font_and_size, [:F1, 10]], - [:set_leading, [12.0]], [:set_font_and_size, [:F2, 10]]]) end it "sets the font and optionally the font size" do @canvas.font("Times", size: 12, variant: :italic) @@ -1033,20 +1032,13 @@ assert_equal(0, @canvas.font_size) @canvas.font("Times", size: 10) # calls #font_size assert_equal(10, @canvas.font_size) end - it "sets the font size and, optionally, the leading" do + it "sets the font size" do @canvas.font("Times", size: 10) assert_equal(10, @canvas.font_size) - assert_equal(12, @canvas.leading) - @canvas.font_size(10, leading: 20) - assert_equal(10, @canvas.font_size) - assert_equal(20, @canvas.leading) - @canvas.font_size(10, leading: nil) - assert_equal(10, @canvas.font_size) - assert_equal(20, @canvas.leading) end it "fails if no valid font is already set" do assert_raises(HexaPDF::Error) { @canvas.font_size(10) } end @@ -1064,80 +1056,80 @@ assert_in_delta(116.68, @canvas.text_cursor[0]) assert_equal(0, @canvas.text_cursor[1]) @canvas.font_size(10) @canvas.show_glyphs(font.decode_utf8("Hal")) assert_operators(@canvas.contents, [[:set_font_and_size, [:F1, 20]], - [:set_leading, [24]], [:set_horizontal_scaling, [200]], [:set_character_spacing, [1]], [:set_word_spacing, [2]], [:begin_text], [:show_text_with_positioning, [['', -10, "Ha", -35, "l lo"]]], [:set_font_and_size, [:F1, 10]], - [:set_leading, [12]], - [:show_text_with_positioning, [["Hal"]]], - ]) + [:show_text_with_positioning, [["Hal"]]]]) end - it "fails if no valid font is set" do - error = assert_raises(HexaPDF::Error) { @canvas.show_glyphs([]) } - assert_match(/if a font is set/, error.message) + it "does nothing if there are no glyphs" do + @canvas.show_glyphs_only([]) + assert_operators(@canvas.contents, []) end end describe "show_glyphs_only" do - it "serializes correctly" do + it "serializes positioned glyphs correctly" do @canvas.font("Times", size: 20) font = @canvas.font @canvas.show_glyphs_only(font.decode_utf8("Hal lo").insert(2, -35)) assert_equal(0, @canvas.text_cursor[0]) assert_equal(0, @canvas.text_cursor[1]) assert_operators(@canvas.contents, [[:set_font_and_size, [:F1, 20]], - [:set_leading, [24]], [:begin_text], - [:show_text_with_positioning, [["Ha", -35, "l lo"]]], - ]) + [:show_text_with_positioning, [["Ha", -35, "l lo"]]]]) end - it "fails if no valid font is set" do - error = assert_raises(HexaPDF::Error) { @canvas.show_glyphs([]) } - assert_match(/if a font is set/, error.message) + it "serializes unpositioned glyphs correctly" do + @canvas.font("Times", size: 20) + font = @canvas.font + @canvas.show_glyphs_only(font.decode_utf8("Hallo")) + assert_operators(@canvas.contents, [[:set_font_and_size, [:F1, 20]], + [:begin_text], + [:show_text, ["Hallo"]]]) end + + it "does nothing if there are no glyphs" do + @canvas.show_glyphs_only([]) + assert_operators(@canvas.contents, []) + end end describe "text" do it "sets the text cursor position if instructed" do @canvas.font("Times", size: 10) @canvas.text("Hallo", at: [100, 100]) assert_operators(@canvas.contents, [[:set_font_and_size, [:F1, 10]], - [:set_leading, [12]], [:begin_text], [:set_text_matrix, [1, 0, 0, 1, 100, 100]], - [:show_text_with_positioning, [["Hallo"]]], - ]) + [:show_text_with_positioning, [["Hallo"]]]]) end it "shows text, possibly split over multiple lines" do @canvas.font("Times", size: 10) @canvas.text("H\u{D A}H\u{A}H\u{B}H\u{c}H\u{D}H\u{85}H\u{2028}H\u{2029}H") assert_operators(@canvas.contents, [[:set_font_and_size, [:F1, 10]], - [:set_leading, [12]], [:begin_text], [:show_text_with_positioning, [["H"]]], [:move_text_next_line], [:show_text_with_positioning, [["H"]]], [:move_text_next_line], [:show_text_with_positioning, [["H"]]], [:move_text_next_line], [:show_text_with_positioning, [["H"]]], [:move_text_next_line], [:show_text_with_positioning, [["H"]]], [:move_text_next_line], [:show_text_with_positioning, [["H"]]], [:move_text_next_line], [:show_text_with_positioning, [["H"]]], [:move_text_next_line], [:show_text_with_positioning, [["H"]]], [:move_text_next_line], - [:show_text_with_positioning, [["H"]]], - ]) + [:show_text_with_positioning, [["H"]]]]) end it "fails if no valid font is set" do - error = assert_raises(HexaPDF::Error) { @canvas.show_glyphs([]) } + error = assert_raises(HexaPDF::Error) { @canvas.text("test") } assert_match(/if a font is set/, error.message) end end describe "marked_content_point" do @@ -1170,10 +1162,10 @@ @canvas.marked_content_sequence(:tag) assert_operators(@canvas.contents, [[:begin_marked_content, [:tag]]]) end it "is serialized correctly when a block is used" do - @canvas.marked_content_sequence(:tag, property_list: {key: 5}) { } + @canvas.marked_content_sequence(:tag, property_list: {key: 5}) {} assert_operators(@canvas.contents, [[:begin_marked_content_with_property_list, [:tag, :P1]], [:end_marked_content]]) end it "fails if invoked while in an unsupported graphics objects" do