class Mosaiq::Svg

Generate SVG version of the Mosaiq

Public Class Methods

new(canvas) click to toggle source
# File lib/mosaiq/svg.rb, line 8
def initialize(canvas)
  @canvas = canvas
  @width = canvas[0].count
  @height = canvas.count
end

Public Instance Methods

raw() click to toggle source
# File lib/mosaiq/svg.rb, line 14
def raw
  %(#{svg_header}#{svg_body}).delete("\n").gsub(/ +/, ' ')
end
to_base64() click to toggle source
# File lib/mosaiq/svg.rb, line 18
def to_base64
  Base64.encode64(raw).delete("\n")
end

Private Instance Methods

svg_body() click to toggle source
# File lib/mosaiq/svg.rb, line 39
def svg_body
  %(<svg width="100%"
         height="100%"
         viewBox="0 0 #{@width} #{@height}"
         version="1.1"
         xmlns="http://www.w3.org/2000/svg">#{svg_rectangles.join}</svg>)
end
svg_header() click to toggle source
# File lib/mosaiq/svg.rb, line 24
def svg_header
  "#{svg_header_xml}#{svg_header_doctype}"
end
svg_header_doctype() click to toggle source
# File lib/mosaiq/svg.rb, line 32
def svg_header_doctype
  %(<!DOCTYPE svg
              PUBLIC
              "-//W3C//DTD SVG 1.1//EN"
              "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">)
end
svg_header_xml() click to toggle source
# File lib/mosaiq/svg.rb, line 28
def svg_header_xml
  %(<?xml version="1.0" encoding="UTF-8"?>)
end
svg_rectangle(x_coordinates, y_coordinates, color) click to toggle source
# File lib/mosaiq/svg.rb, line 55
def svg_rectangle(x_coordinates, y_coordinates, color)
  %(<rect x="#{x_coordinates}"
          y="#{y_coordinates}"
          width="1"
          height="1"
          style="fill: #{color};"/>)
end
svg_rectangles() click to toggle source
# File lib/mosaiq/svg.rb, line 47
def svg_rectangles
  @canvas.each_with_index.map do |row, y|
    row.each_with_index.map do |color, x|
      svg_rectangle(x, y, color)
    end
  end
end