lib/prawn/svg/element.rb in prawn-svg-0.17.0 vs lib/prawn/svg/element.rb in prawn-svg-0.18.0

- old
+ new

@@ -53,11 +53,11 @@ def apply_styles parse_transform_attribute_and_call parse_opacity_attributes_and_call parse_clip_path_attribute_and_call draw_types = parse_fill_and_stroke_attributes_and_call - parse_stroke_width_attribute_and_call + parse_stroke_attributes_and_call parse_font_attributes_and_call parse_display_attribute apply_drawing_call(draw_types) end @@ -80,11 +80,11 @@ parse_css_method_calls(transform).each do |name, arguments| case name when 'translate' x, y = arguments - add_call_and_enter name, @document.distance(x), -@document.distance(y) + add_call_and_enter name, @document.distance(x, :x), -@document.distance(y, :y) when 'rotate' r, x, y = arguments.collect {|a| a.to_f} if arguments.length == 3 add_call_and_enter name, -r, :origin => [@document.x(x), @document.y(y)] @@ -100,11 +100,11 @@ when 'matrix' if arguments.length != 6 @document.warnings << "transform 'matrix' must have six arguments" else a, b, c, d, e, f = arguments.collect {|argument| argument.to_f} - add_call_and_enter "transformation_matrix", a, -b, -c, d, @document.distance(e), -@document.distance(f) + add_call_and_enter "transformation_matrix", a, -b, -c, d, @document.distance(e, :x), -@document.distance(f, :y) end else @document.warnings << "Unknown transformation '#{name}'; ignoring" end end @@ -154,12 +154,42 @@ state[type.to_sym] end end - def parse_stroke_width_attribute_and_call - add_call('line_width', @document.distance(@attributes['stroke-width'])) if @attributes['stroke-width'] + CAP_STYLE_TRANSLATIONS = {"butt" => :butt, "round" => :round, "square" => :projecting_square} + + def parse_stroke_attributes_and_call + if width = @attributes['stroke-width'] + add_call('line_width', @document.distance(width)) + end + + if (linecap = attribute_value_as_keyword('stroke-linecap')) && linecap != 'inherit' + add_call('cap_style', CAP_STYLE_TRANSLATIONS.fetch(linecap, :butt)) + end + + if dasharray = attribute_value_as_keyword('stroke-dasharray') + case dasharray + when 'inherit' + # don't do anything + when 'none' + add_call('undash') + else + array = dasharray.split(Prawn::Svg::Parser::COMMA_WSP_REGEXP) + array *= 2 if array.length % 2 == 1 + number_array = array.map {|value| @document.distance(value)} + + if number_array.any? {|number| number < 0} + @document.warnings << "stroke-dasharray cannot have negative numbers; treating as 'none'" + add_call('undash') + elsif number_array.inject(0) {|a, b| a + b} == 0 + add_call('undash') + else + add_call('dash', number_array) + end + end + end end def parse_font_attributes_and_call if size = @attributes['font-size'] @state[:font_size] = size.to_f @@ -229,22 +259,32 @@ else style = element.attributes['style'] || "" end @attributes = parse_css_declarations(style) - element.attributes.each {|n,v| @attributes[n] = v unless @attributes[n]} + + element.attributes.each do |name, value| + name = name.downcase + @attributes[name] = value unless @attributes[name] + end end def parse_css_declarations(declarations) # copied from css_parser declarations.gsub!(/(^[\s]*)|([\s]*$)/, '') output = {} declarations.split(/[\;$]+/m).each do |decs| if matches = decs.match(/\s*(.[^:]*)\s*\:\s*(.[^;]*)\s*(;|\Z)/i) - property, value, end_of_declaration = matches.captures - output[property] = value + property, value, _ = matches.captures + output[property.downcase] = value end end output + end + + def attribute_value_as_keyword(name) + if value = @attributes[name] + value.strip.downcase + end end end