Skip to content

Commit

Permalink
add rounded_rect & polygon test
Browse files Browse the repository at this point in the history
  • Loading branch information
naitoh committed Dec 28, 2023
1 parent bb48c6d commit c6bf63d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/rbpdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9432,9 +9432,9 @@ def Circle(x0, y0, r, angstr=0, angend=360, style='', line_style={}, fill_color=
# Draws a polygonal line
# [@param array :p] Points 0 to (:np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
# [@param string :style] Style of rendering. See the getPathPaintOperator() function for more information.
# [@param hash :line_style]
# [@param hash or array :line_style]
# Line style of polygon. Hash with keys among the following:
# * all: Line style of all lines. Array like for {@link SetLineStyle SetLineStyle}.
# * all: Line style of all lines. Hash like for {@link SetLineStyle SetLineStyle}.
# * 0 to (:np - 1): Line style of each line. Array like for {@link SetLineStyle SetLineStyle}.
# If a key is not present or is null, not draws the line. Default value is default line style (empty Hash).
# [@param array :fill_color] Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
Expand All @@ -9451,9 +9451,9 @@ def PolyLine(p, style='', line_style={}, fill_color=[])
# Draws a polygon.
# [@param array :p] Points 0 to (np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
# [@param string :style] Style of rendering. See the getPathPaintOperator() function for more information.
# [@param hash :line_style]
# [@param hash or array :line_style]
# Line style of polygon. Hash with keys among the following:
# * all: Line style of all lines. Array like for {@link SetLineStyle SetLineStyle}.
# * all: Line style of all lines. Hash like for {@link SetLineStyle SetLineStyle}.
# * 0 to (:np - 1): Line style of each line. Array like for {@link SetLineStyle SetLineStyle}.
# If a key is not present or is null, not draws the line. Default value is default line style (empty Hash).
# [@param array :fill_color] Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
Expand Down
83 changes: 83 additions & 0 deletions test/rbpdf_graphic_func_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,87 @@ def getPageBuffer(page)
assert_equal "S", content[before_size + 16]
# line end
end

test "rounded_rect basic test" do
pdf = MYPDF.new
pdf.add_page()
page = pdf.get_page
contents = pdf.getPageBuffer(page)
before_size = contents.split("\n").size
pdf.putshaders
style = { 'L' => nil, 'T' => nil, 'R' => nil, 'B' => nil }
pdf.rounded_rect(50, 255, 40, 30, 6.50)

content = []
contents = pdf.getPageBuffer(page)
contents.each_line {|line| content.push line.chomp }

assert_equal "160.16 119.06 m", content[before_size] # outPoint(x + rx, y)
assert_equal "236.69 119.06 l", content[before_size + 1] # outLine(xc, y)
assert_equal "246.87 119.06 255.12 110.81 255.12 100.63 c", content[before_size + 2] # outCurve(xc + (rx * myArc), yc - ry, xc + rx, yc - (ry * myArc), xc + rx, yc)
assert_equal "255.12 52.44 l", content[before_size + 3] # outLine(x + w, yc)
assert_equal "255.12 42.27 246.87 34.02 236.69 34.02 c", content[before_size + 4] # outCurve(xc + rx, yc + (ry * myArc), xc + (rx * myArc), yc + ry, xc, yc + ry)
assert_equal "160.16 34.02 l", content[before_size + 5] # outLine(xc, y + h)
assert_equal "149.98 34.02 141.73 42.27 141.73 52.44 c", content[before_size + 6] # outCurve(xc - (rx * myArc), yc + ry, xc - rx, yc + (ry * myArc), xc - rx, yc)
assert_equal "141.73 100.63 l", content[before_size + 7] # outLine(x, yc)
assert_equal "141.73 110.81 149.98 119.06 160.16 119.06 c", content[before_size + 8] # outCurve(xc - rx, yc - (ry * myArc), xc - (rx * myArc), yc - ry, xc, yc - ry)
assert_equal "S", content[before_size + 9]
end

test "polygon basic test" do
pdf = MYPDF.new
pdf.add_page()
page = pdf.get_page
contents = pdf.getPageBuffer(page)
before_size = contents.split("\n").size
pdf.putshaders
style = { 'L' => nil, 'T' => nil, 'R' => nil, 'B' => nil }
pdf.polygon([5,135,45,135,15,165])

content = []
contents = pdf.getPageBuffer(page)
contents.each_line {|line| content.push line.chomp }

# draw = true
assert_equal "14.17 459.21 m", content[before_size] # outPoint(p[0], p[1])
assert_equal "127.56 459.21 l", content[before_size + 1] # outLine(p[i], p[i + 1])
assert_equal "42.52 374.17 l", content[before_size + 2] # outLine(p[i], p[i + 1])
assert_equal "14.17 459.21 l", content[before_size + 3] # outLine(p[i], p[i + 1])
assert_equal "127.56 459.21 l", content[before_size + 4] # outLine(p[i], p[i + 1])
assert_equal "S", content[before_size + 5]
end

test "polygon line_style test" do
pdf = MYPDF.new
pdf.add_page()
page = pdf.get_page
contents = pdf.getPageBuffer(page)
before_size = contents.split("\n").size
pdf.putshaders
style = { 'L' => nil, 'T' => nil, 'R' => nil, 'B' => nil }
style6 = {'width' => 0.5}
pdf.polygon([5,135,45,135,15,165], 'D', [style6, 0, 0])

content = []
contents = pdf.getPageBuffer(page)
contents.each_line {|line| content.push line.chomp }
# draw = true
assert_equal "14.17 459.21 m", content[before_size] # outPoint(p[0], p[1])
assert_equal "S", content[before_size + 1]

assert_equal "1.42 w", content[before_size + 2]
assert_equal "14.17 459.21 m", content[before_size + 3] # outLine(p[i], p[i + 1])
assert_equal "127.56 459.21 l", content[before_size + 4] # outLine(p[i], p[i + 1])
assert_equal "S", content[before_size + 5]
assert_equal "127.56 459.21 m", content[before_size + 6] # outLine(p[i], p[i + 1])
assert_equal "S", content[before_size + 7]

assert_equal "1.42 w", content[before_size + 8]
assert_equal "14.17 459.21 m", content[before_size + 9] # outLine(p[i], p[i + 1])
assert_equal "127.56 459.21 l", content[before_size + 10] # outLine(p[i], p[i + 1])
assert_equal "S", content[before_size + 11]

assert_equal "127.56 459.21 m", content[before_size + 12] # outLine(p[i], p[i + 1])
assert_equal "S", content[before_size + 13]
end
end

0 comments on commit c6bf63d

Please sign in to comment.