diff --git a/README.md b/README.md index 9b43fa8..4426ab2 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,9 @@ standalone - whether to make this a full SVG file, or only an svg to embed use_path - Use to render SVG rather than to significantly reduce size and quality. This will become the default in future versions. (default false) +viewbox - replace the `svg.width` and `svg.height` attribute with `svg.viewBox` to + allow CSS scaling + (default false) ``` Example ```ruby @@ -104,11 +107,11 @@ qrcode = RQRCode::QRCode.new("http://github.com/") # NOTE: showing with default options specified explicitly svg = qrcode.as_svg( - offset: 0, color: '000', shape_rendering: 'crispEdges', module_size: 11, - standalone: true + standalone: true, + use_path: true ) ``` diff --git a/lib/rqrcode/export/svg.rb b/lib/rqrcode/export/svg.rb index 4576adc..78b9f59 100644 --- a/lib/rqrcode/export/svg.rb +++ b/lib/rqrcode/export/svg.rb @@ -143,27 +143,33 @@ def end_y # use_path - Use to render SVG rather than to significantly reduce size # and quality. This will become the default in future versions. # (default false) + # viewbox - replace `width` and `height` in with a viewBox, allows CSS scaling + # (default false) # def as_svg(options = {}) + fill = options[:fill] use_path = options[:use_path] offset = options[:offset].to_i || 0 color = options[:color] || "000" shape_rendering = options[:shape_rendering] || "crispEdges" module_size = options[:module_size] || 11 standalone = options[:standalone].nil? ? true : options[:standalone] + viewbox = options[:viewbox].nil? ? false : options[:viewbox] # height and width dependent on offset and QR complexity dimension = (@qrcode.module_count * module_size) + (2 * offset) + # use dimensions differently if we are using a viewBox + dimensions_attr = viewbox ? %(viewBox="0 0 #{dimension} #{dimension}") : %(width="#{dimension}" height="#{dimension}") xml_tag = %() - open_tag = %() + open_tag = %() close_tag = "" output_tag = (use_path ? Path : Rect).new(@qrcode) output_tag.build(module_size, offset, color) - if options[:fill] - output_tag.result.unshift %() + if fill + output_tag.result.unshift %() end if standalone diff --git a/spec/rqrcode/data.rb b/spec/rqrcode/data.rb index 39f95d6..130da00 100644 --- a/spec/rqrcode/data.rb +++ b/spec/rqrcode/data.rb @@ -18,6 +18,10 @@ SVG +AS_SVG4 = <<~SVG.chomp + +SVG + AS_BASIC = <<~STR XXXXXXXOOOXOXOOXXXOOOOXXXXXXX XOOOOOXOOXXXXXOOXOXOOOXOOOOOX diff --git a/spec/rqrcode/export_svg_spec.rb b/spec/rqrcode/export_svg_spec.rb index 816bc74..6306310 100644 --- a/spec/rqrcode/export_svg_spec.rb +++ b/spec/rqrcode/export_svg_spec.rb @@ -33,11 +33,20 @@ end context "standalone false" do - it "must export to svg" do + it "will not include the " do expect(RQRCode::QRCode.new("https://kyan.com").as_svg( standalone: false, use_path: true )).to eq(AS_SVG3) end end + + context "viewbox true" do + it "will use the viewBox attr" do + expect(RQRCode::QRCode.new("https://kyan.com").as_svg( + viewbox: true, + use_path: true + )).to eq(AS_SVG4) + end + end end