Skip to content

Commit

Permalink
fix defalut value line_style={}, border_style={}, fill_color=[], circ…
Browse files Browse the repository at this point in the history
…le_outLine_style={}, circle_fill_color=[]

- Rect,RoundedRect,RoundedRect
- Curve,Ellipse,Circle
- PolyLine,Polygon,Polygon,RegularPolygon,StarPolygon
  • Loading branch information
naitoh committed Dec 28, 2023
1 parent cf922e6 commit 2dc77d6
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 53 deletions.
8 changes: 4 additions & 4 deletions example/rails/app/controllers/example012_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def index
style = {'width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => '10,20,5,10', 'phase' => 10, 'color' => [255, 0, 0]}
style2 = {'width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => [255, 0, 0]}
style3 = {'width' => 1, 'cap' => 'round', 'join' => 'round', 'dash' => '2,10', 'color' => [255, 0, 0]}
style4 = {'L' => 0,
style4 = {'L' => nil,
'T' => {'width' => 0.25, 'cap' => 'butt', 'join' => 'miter', 'dash' => '20,10', 'phase' => 10, 'color' => [100, 100, 255]},
'R' => {'width' => 0.50, 'cap' => 'round', 'join' => 'miter', 'dash' => 0, 'color' => [50, 50, 127]},
'B' => {'width' => 0.75, 'cap' => 'square', 'join' => 'miter', 'dash' => '30,10,5,10'}}
Expand Down Expand Up @@ -107,7 +107,7 @@ def index

# Polygonal Line
pdf.set_line_style({'width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => [0, 0, 164]})
pdf.poly_line([80,165,90,160,100,165,110,160,120,165,130,160,140,165], 'D', nil, nil)
pdf.poly_line([80,165,90,160,100,165,110,160,120,165,130,160,140,165], 'D')

# Regular polygon
pdf.text(5, 169, 'Regular polygon examples')
Expand All @@ -124,8 +124,8 @@ def index
pdf.set_line_style(style5)
pdf.star_polygon(20, 230, 15, 20, 3, 0, 1, 'F')
pdf.star_polygon(55, 230, 15, 12, 5)
pdf.star_polygon(55, 230, 7, 12, 5, 45, 0, 'DF', {'all' => style7}, [220, 220, 200], 'F', [255, 200, 200])
pdf.star_polygon(90, 230, 15, 20, 6, 0, 1, 'DF', {'all' => style5}, [220, 220, 200], 'F', [255, 200, 200])
pdf.star_polygon(55, 230, 7, 12, 5, 45, 0, 'DF', {'all' => style7}, [220, 220, 200], 'F', nil, [255, 200, 100])
pdf.star_polygon(90, 230, 15, 20, 6, 0, 1, 'DF', {'all' => style5}, [220, 220, 200], 'F', nil, [255, 200, 100])
pdf.star_polygon(125, 230, 15, 5, 2, 30, 1, nil, {'all' => style5}, nil, nil, style6)
pdf.star_polygon(160, 230, 15, 10, 3)
pdf.star_polygon(160, 230, 7, 50, 26)
Expand Down
95 changes: 46 additions & 49 deletions lib/rbpdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9190,31 +9190,28 @@ def Line(x1, y1, x2, y2, style={})
# [@param float :y] Ordinate of upper-left corner (or upper-right corner for RTL language).
# [@param float :w] Width.
# [@param float :h] Height.
# [@param string :style]
# Style of rendering. See the getPathPaintOperator() function for more information.
# [@param string :style] Style of rendering. See the getPathPaintOperator() function for more information.
# [@param hash :border_style] Border style of rectangle. Hash with keys among the following:
# * all: Line style of all borders. Array like for {@link SetLineStyle SetLineStyle}.
# * L, T, R, B or combinations: Line style of left, top, right or bottom border. Array like for {@link SetLineStyle SetLineStyle}.
# If a key is not present or is null, not draws the border. Default value: default line style (empty array).
# [@param array :border_style] Border style of rectangle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
# If a key is not present or is null, not draws the border. Default value: 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).
# [@access public]
# [@since 1.0]
# [@see] SetLineStyle()
#
def Rect(x, y, w, h, style='', border_style={}, fill_color={})
if style.index('F') != nil and !fill_color.empty?
def Rect(x, y, w, h, style='', border_style={}, fill_color=[])
if !style.index('F').nil? && fill_color && !fill_color.empty?
SetFillColorArray(fill_color)
end
op = getPathPaintOperator(style)
if !border_style or !border_style['all'].nil?
if !border_style['all'].nil? and border_style['all']
SetLineStyle(border_style['all'])
border_style = {}
end
if border_style && border_style['all']
SetLineStyle(border_style['all'])
border_style = {}
end
outRect(x, y, w, h, op)

if border_style
if border_style && !border_style.empty?
border_style2 = {}
border_style.each { |line, value|
length = line.length
Expand Down Expand Up @@ -9252,18 +9249,18 @@ def Rect(x, y, w, h, style='', border_style={}, fill_color={})
# [@param float :x3] Abscissa of end point.
# [@param float :y3] Ordinate of end point.
# [@param string :style] Style of rendering. See the getPathPaintOperator() function for more information.
# [@param array :line_style] Line style of curve. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
# [@param hash :line_style] Line style of curve. Array like for {@link SetLineStyle SetLineStyle}. Default value: 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).
# [@access public]
# [@see] SetLineStyle()
# [@since 2.1.000 (2008-01-08)]
#
def Curve(x0, y0, x1, y1, x2, y2, x3, y3, style='', line_style=nil, fill_color=nil)
if style and (style.index('F') != nil) and fill_color
def Curve(x0, y0, x1, y1, x2, y2, x3, y3, style='', line_style={}, fill_color=[])
if style && !style.index('F').nil? && fill_color && !fill_color.empty?
SetFillColorArray(fill_color)
end
op = getPathPaintOperator(style)
if line_style
if line_style && !line_style.empty?
SetLineStyle(line_style)
end
outPoint(x0, y0)
Expand All @@ -9283,19 +9280,19 @@ def Curve(x0, y0, x1, y1, x2, y2, x3, y3, style='', line_style=nil, fill_color=n
# [@param float :astart] Angle start of draw line. Default value: 0.
# [@param float :afinish] Angle finish of draw line. Default value: 360.
# [@param string :style] Style of rendering. See the getPathPaintOperator() function for more information.
# [@param array :line_style] Line style of ellipse. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
# [@param hash :line_style] Line style of ellipse. Array like for {@link SetLineStyle SetLineStyle}. Default value: 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).
# [@param integer :nc] Number of curves used to draw a 90 degrees portion of ellipse.
# [@author] Nicola Asuni
# [@access public]
# [@since 2.1.000 (2008-01-08)]
#
def Ellipse(x0, y0, rx, ry='', angle=0, astart=0, afinish=360, style='', line_style=nil, fill_color=nil, nc=2)
def Ellipse(x0, y0, rx, ry='', angle=0, astart=0, afinish=360, style='', line_style={}, fill_color=[], nc=2)
style = '' if style.nil?
if empty_string(ry) or (ry == 0)
ry = rx
end
if (nil != style.index('F')) and fill_color
if !style.index('F').nil? && fill_color && !fill_color.empty?
SetFillColorArray(fill_color)
end
op = getPathPaintOperator(style)
Expand Down Expand Up @@ -9420,13 +9417,13 @@ def outellipticalarc(xc, yc, rx, ry, xang=0, angs=0, angf=360, pie=false, nc=2)
# [@param float :angstr] Angle start of draw line. Default value: 0.
# [@param float :angend] Angle finish of draw line. Default value: 360.
# [@param string :style] Style of rendering. See the getPathPaintOperator() function for more information.
# [@param array :line_style] Line style of circle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
# [@param hash :line_style] Line style of circle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty Hash).
# [@param array :fill_color] Fill color. Format: array(red, green, blue). Default value: default color (empty array).
# [@param integer :nc] Number of curves used to draw a 90 degrees portion of circle.
# [@access public]
# [@since 2.1.000 (2008-01-08)]
#
def Circle(x0, y0, r, angstr=0, angend=360, style='', line_style=nil, fill_color=nil, nc=2)
def Circle(x0, y0, r, angstr=0, angend=360, style='', line_style={}, fill_color=[], nc=2)
Ellipse(x0, y0, r, r, 0, angstr, angend, style, line_style, fill_color, nc)
end
alias_method :circle, :Circle
Expand All @@ -9435,17 +9432,17 @@ def Circle(x0, y0, r, angstr=0, angend=360, style='', line_style=nil, 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 array :line_style]
# Line style of polygon. Array with keys among the following:
# [@param hash :line_style]
# Line style of polygon. Hash with keys among the following:
# * all: Line style of all lines. Array 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 array).
# 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).
# [@param boolean :closed] if true the polygon is closes, otherwise will remain open
# [@access public]
# [@since 4.8.003 (2009-09-15)]
#
def PolyLine(p, style='', line_style=nil, fill_color=nil)
def PolyLine(p, style='', line_style={}, fill_color=[])
Polygon(p, style, line_style, fill_color, false)
end
alias_method :poly_line, :PolyLine
Expand All @@ -9454,17 +9451,17 @@ def PolyLine(p, style='', line_style=nil, fill_color=nil)
# 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 array :line_style]
# Line style of polygon. Array with keys among the following:
# [@param hash :line_style]
# Line style of polygon. Hash with keys among the following:
# * all: Line style of all lines. Array 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 array).
# 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).
# [@param boolean :closed] if true the polygon is closes, otherwise will remain open
# [@access public]
# [@since 2.1.000 (2008-01-08)]
#
def Polygon(p, style='', line_style=nil, fill_color=nil, closed=true)
def Polygon(p, style='', line_style={}, fill_color=[], closed=true)
style = '' if style.nil?
nc = p.length # number of coordinates
np = nc / 2 # number of points
Expand All @@ -9483,15 +9480,15 @@ def Polygon(p, style='', line_style=nil, fill_color=nil, closed=true)
end
nc += 4
end
if (nil != style.index('F')) and fill_color
if !style.index('F').nil? && fill_color && !fill_color.empty?
SetFillColorArray(fill_color)
end
op = getPathPaintOperator(style)
if op == 'f'
line_style = []
line_style = {}
end
draw = true
if line_style
if line_style && !line_style.empty?
if line_style.is_a? Hash and line_style['all']
SetLineStyle(line_style['all'])
else
Expand Down Expand Up @@ -9548,11 +9545,11 @@ def Polygon(p, style='', line_style=nil, fill_color=nil, closed=true)
# [@param float :angle] Angle oriented (anti-clockwise). Default value: 0.
# [@param boolean :draw_circle] Draw inscribed circle or not. Default value: false.
# [@param string :style] Style of rendering. See the getPathPaintOperator() function for more information.
# [@param array :line_style]
# Line style of polygon sides. Array with keys among the following:
# [@param hash :line_style]
# Line style of polygon sides. Hash with keys among the following:
# * all: Line style of all sides. Array like for {@link SetLineStyle SetLineStyle}.
# * 0 to (:ns - 1): Line style of each side. Array like for {@link SetLineStyle SetLineStyle}.
# If a key is not present or is null, not draws the side. Default value is default line style (empty array).
# If a key is not present or is null, not draws the side. Default value is default line style (empty Hash).
# [@param array :fill_color] Fill color. Format: array(red, green, blue). Default value: default color (empty array).
# [@param string :circle_style]
# Style of rendering of inscribed circle (if draws). Possible values are:
Expand All @@ -9561,12 +9558,12 @@ def Polygon(p, style='', line_style=nil, fill_color=nil, closed=true)
# * DF or FD: Draw and fill.
# * CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).
# * CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).
# [@param array :circle_outLine_style] Line style of inscribed circle (if draws). Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
# [@param hash :circle_outLine_style] Line style of inscribed circle (if draws). Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty Hash).
# [@param array :circle_fill_color] Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
# [@access public]
# [@since 2.1.000 (2008-01-08)]
#
def RegularPolygon(x0, y0, r, ns, angle=0, draw_circle=false, style='', line_style=nil, fill_color=nil, circle_style='', circle_outLine_style=nil, circle_fill_color=nil)
def RegularPolygon(x0, y0, r, ns, angle=0, draw_circle=false, style='', line_style={}, fill_color=[], circle_style='', circle_outLine_style={}, circle_fill_color=[])
draw_circle = false if draw_circle == 0
if 3 > ns
ns = 3
Expand Down Expand Up @@ -9595,11 +9592,11 @@ def RegularPolygon(x0, y0, r, ns, angle=0, draw_circle=false, style='', line_sty
# [@param float :angle] Angle oriented (anti-clockwise). Default value: 0.
# [@param boolean :draw_circle] Draw inscribed circle or not. Default value is false.
# [@param string :style] Style of rendering. See the getPathPaintOperator() function for more information.
# [@param array :line_style]
# Line style of polygon sides. Array with keys among the following:
# [@param hash :line_style]
# Line style of polygon sides. Hash with keys among the following:
# * all: Line style of all sides. Array like for {@link SetLineStyle SetLineStyle}.
# * 0 to (n - 1): Line style of each side. Array like for {@link SetLineStyle SetLineStyle}.
# If a key is not present or is null, not draws the side. Default value is default line style (empty array).
# If a key is not present or is null, not draws the side. Default value is default line style (empty Hash).
# [@param array :fill_color ]Fill color. Format: array(red, green, blue). Default value: default color (empty array).
# [@param string :circle_style]
# Style of rendering of inscribed circle (if draws). Possible values are:
Expand All @@ -9608,12 +9605,12 @@ def RegularPolygon(x0, y0, r, ns, angle=0, draw_circle=false, style='', line_sty
# * DF or FD: Draw and fill.
# * CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).
# * CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).
# [@param array :circle_outLine_style] Line style of inscribed circle (if draws). Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
# [@param hash :circle_outLine_style] Line style of inscribed circle (if draws). Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty Hash).
# [@param array :circle_fill_color] Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
# [@access public]
# [@since 2.1.000 (2008-01-08)]
#
def StarPolygon(x0, y0, r, nv, ng, angle=0, draw_circle=false, style='', line_style=nil, fill_color=nil, circle_style='', circle_outLine_style=nil, circle_fill_color=nil)
def StarPolygon(x0, y0, r, nv, ng, angle=0, draw_circle=false, style='', line_style={}, fill_color=[], circle_style='', circle_outLine_style={}, circle_fill_color=[])
draw_circle = false if draw_circle == 0
if nv < 2
nv = 2
Expand Down Expand Up @@ -9653,12 +9650,12 @@ def StarPolygon(x0, y0, r, nv, ng, angle=0, draw_circle=false, style='', line_st
# [@param float :r] the radius of the circle used to round off the corners of the rectangle.
# [@param string :round_corner] Draws rounded corner or not. String with a 0 (not rounded i-corner) or 1 (rounded i-corner) in i-position. Positions are, in order and begin to 0: top left, top right, bottom right and bottom left. Default value: all rounded corner ("1111").
# [@param string :style] Style of rendering. See the getPathPaintOperator() function for more information.
# [@param array :border_style] Border style of rectangle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
# [@param hash :border_style] Border style of rectangle. Array like for {@link SetLineStyle SetLineStyle}. Default value: 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).
# [@access public]
# [@since 2.1.000 (2008-01-08)]
#
def RoundedRect(x, y, w, h, r, round_corner='1111', style='', border_style=nil, fill_color=nil)
def RoundedRect(x, y, w, h, r, round_corner='1111', style='', border_style={}, fill_color=[])
RoundedRectXY(x, y, w, h, r, r, round_corner, style, border_style, fill_color)
end
alias_method :rounded_rect, :RoundedRect
Expand All @@ -9673,27 +9670,27 @@ def RoundedRect(x, y, w, h, r, round_corner='1111', style='', border_style=nil,
# [@param float :ry] the y-axis radius of the ellipse used to round off the corners of the rectangle.
# [@param string :round_corner] Draws rounded corner or not. String with a 0 (not rounded i-corner) or 1 (rounded i-corner) in i-position. Positions are, in order and begin to 0: top left, top right, bottom right and bottom left. Default value: all rounded corner ("1111").
# [@param string :style] Style of rendering. See the getPathPaintOperator() function for more information.
# [@param array :border_style] Border style of rectangle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
# [@param hash :border_style] Border style of rectangle. Array like for {@link SetLineStyle SetLineStyle}. Default value: 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).
# [@access public]
# [@since 4.9.019 (2010-04-22)]
#
def RoundedRectXY(x, y, w, h, rx, ry, round_corner='1111', style='', border_style=nil, fill_color=nil)
def RoundedRectXY(x, y, w, h, rx, ry, round_corner='1111', style='', border_style={}, fill_color=[])
style = '' if style.nil?
if (round_corner == '0000') or ((rx == ry) and (rx == 0))
# Not rounded
Rect(x, y, w, h, style, border_style, fill_color)
return
end
# Rounded
if (nil != style.index('F')) and fill_color
if !style.index('F').nil? && fill_color && !fill_color.empty?
SetFillColorArray(fill_color)
end
op = getPathPaintOperator(style)
if op == 'f'
border_style = []
border_style = {}
end
if border_style
unless border_style.empty?
SetLineStyle(border_style)
end
myArc = 4 / 3.0 * (::Math.sqrt(2) - 1)
Expand Down
Loading

0 comments on commit 2dc77d6

Please sign in to comment.