Skip to content

Commit

Permalink
draw: add support for dash
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Oct 30, 2023
1 parent a60f2bd commit c1b2673
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 9 additions & 7 deletions lib/rabbit/renderer/engine/cairo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def draw_line(x1, y1, x2, y2, color=nil, params={})
x2, y2 = from_screen(x2, y2)
@context.save do
set_source(color, params)
set_line_options(params)
set_stroke_options(params)
@context.new_path
@context.move_to(x1, y1)
@context.line_to(x2, y2)
Expand All @@ -117,7 +117,7 @@ def draw_rectangle(filled, x, y, w, h, color=nil, params={})
x, y = from_screen(x, y)
@context.save do
set_source(color, params)
set_line_options(params)
set_stroke_options(params)
@context.rectangle(x, y, w, h)
apply_cairo_action(filled, params)
end
Expand All @@ -131,7 +131,7 @@ def draw_rounded_rectangle(filled, x, y, w, h, radius,

@context.save do
set_source(color, params)
set_line_options(params)
set_stroke_options(params)
@context.new_path
@context.rounded_rectangle(x, y, w, h, x_radius, y_radius)
apply_cairo_action(filled, params)
Expand All @@ -149,7 +149,7 @@ def draw_arc_by_radius(filled, x, y, r, a1, a2, color=nil, params={})
a1, a2 = convert_angle(a1, a2)
@context.save do
set_source(color, params)
set_line_options(params)
set_stroke_options(params)
args = [x, y, r, a1, a2]
action, = cairo_action(filled, params)
@context.move_to(x, y) unless action == :stroke
Expand All @@ -167,7 +167,7 @@ def draw_polygon(filled, points, color=nil, params={})
return if points.empty?
@context.save do
set_source(color, params)
set_line_options(params)
set_stroke_options(params)
@context.move_to(*from_screen(*points.first))
points[1..-1].each do |x, y|
@context.line_to(*from_screen(x, y))
Expand All @@ -181,7 +181,7 @@ def draw_layout(layout, x, y, color=nil, params={})
x, y = from_screen(x, y)
@context.save do
set_source(color, params)
set_line_options(params)
set_stroke_options(params)
@context.move_to(x, y)
if params[:stroke]
@context.pango_layout_path(layout)
Expand Down Expand Up @@ -461,12 +461,14 @@ def _draw_reflected_rsvg_handle(handle, x, y, width, height, params)
end
end

def set_line_options(params)
def set_stroke_options(params)
set_line_width(get_line_width(params))
[:line_cap, :line_join].each do |key|
value = params[key]
@context.send("#{key}=", value) if value
end
value = params[:dash]
@context.set_dash(*value) if value
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion sample/rabbit.rd
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
# src = lavie.png
# relative_height = 80
# draw0 = [rectangle, false, 0.05, 0.1, 0.1, 0.2, {color: red, line_width: 3}]
# draw1 = [line, 0.2, 0.1, 0.3, 0.2, {color: blue, line_width: 5}]
# draw1 = [line, 0.2, 0.1, 0.3, 0.2, {color: blue, line_width: 5, dash: [[1, 6], 0]}]
# draw2 = [text, Cute!, 0.05, 0.4, {color: Green, size: 25}]
# draw10 = [line, 0.0, 0.0, 0.5, 0.5, 0.25, 0.5, {line_width: 10, line_cap: butt, line_join: miter}]

Expand Down

0 comments on commit c1b2673

Please sign in to comment.