Module: RQRCode::Export::SVG
- Included in:
- QRCode
- Defined in:
- lib/rqrcode/export/svg.rb
Defined Under Namespace
Classes: BaseOutputSVG, Path, Rect
Constant Summary collapse
- DEFAULT_SVG_ATTRIBUTES =
[ %(version="1.1"), %(xmlns="http://www.w3.org/2000/svg"), %(xmlns:xlink="http://www.w3.org/1999/xlink"), %(xmlns:ev="http://www.w3.org/2001/xml-events") ]
- SVG_PATH_COMMANDS =
{ move: "M", up: "v-", down: "v", left: "h-", right: "h", close: "z" }
Instance Method Summary collapse
-
#as_svg(options = {}) ⇒ Object
Render the SVG from the Qrcode.
Instance Method Details
#as_svg(options = {}) ⇒ Object
Render the SVG from the Qrcode.
Options: offset - Padding around the QR Code in pixels
(default 0)
offset_x - X Padding around the QR Code in pixels
(default offset)
offset_y - Y Padding around the QR Code in pixels
(default offset)
fill - Background color e.g “ffffff”
(default none)
color - Foreground color e.g “000”
(default "000")
module_size - The Pixel size of each module
(defaults 11)
shape_rendering - SVG Attribute: auto | optimizeSpeed | crispEdges | geometricPrecision
(defaults crispEdges)
standalone - Whether to make this a full SVG file, or only an svg to embed in other svg
(default true)
use_path - Use <path> to render SVG rather than <rect> to significantly reduce size
and quality. This will become the default in future versions.
(default false)
viewbox - replace width and height in <svg> with a viewBox, allows CSS scaling
(default false)
svg_attributes - A optional hash of custom <svg> attributes. Existing attributes will remain.
(default {})
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/rqrcode/export/svg.rb', line 231 def as_svg( = {}) fill = [:fill] use_path = [:use_path] offset = [:offset].to_i offset_x = .key?(:offset_x) ? [:offset_x].to_i : offset offset_y = .key?(:offset_y) ? [:offset_y].to_i : offset color = [:color] || "000" shape_rendering = [:shape_rendering] || "crispEdges" module_size = [:module_size] || 11 standalone = [:standalone].nil? || [:standalone] viewbox = [:viewbox].nil? ? false : [:viewbox] svg_attributes = [:svg_attributes] || {} # height and width dependent on offset and QR complexity width = (@qrcode.module_count * module_size) + (2 * offset_x) height = (@qrcode.module_count * module_size) + (2 * offset_y) dimension = [width, height].max # use dimensions differently if we are using a viewBox dimensions_attr = viewbox ? %(viewBox="0 0 #{width} #{height}") : %(width="#{width}" height="#{height}") svg_tag_attributes = (DEFAULT_SVG_ATTRIBUTES + [ dimensions_attr, %(shape-rendering="#{shape_rendering}") ] + svg_attributes.map { |k, v| %(#{k}="#{v}") }).join(" ") xml_tag = %(<?xml version="1.0" standalone="yes"?>) open_tag = %(<svg #{svg_tag_attributes}>) close_tag = "</svg>" # Prefix hexadecimal colors unless using a named color (symbol) color = "##{color}" unless color.is_a?(Symbol) output_tag = (use_path ? Path : Rect).new(@qrcode) output_tag.build(module_size, offset_x: offset_x, offset_y: offset_y, color: color) if fill # Prefix hexadecimal colors unless using a named color (symbol) fill = "##{fill}" unless fill.is_a?(Symbol) output_tag.result.unshift %(<rect width="#{dimension}" height="#{dimension}" x="0" y="0" fill="#{fill}"/>) end if standalone output_tag.result.unshift(xml_tag, open_tag) output_tag.result << close_tag end output_tag.result.join end |