Sha256: 033166e5c6760c81c73389cb8e63779742b232b4774c136f37171524648b3e86

Contents?: true

Size: 1.6 KB

Versions: 4

Compression:

Stored size: 1.6 KB

Contents

module Nineteen
  module Eighty
    module Two
      module Formats
        class SVG
          def self.format text, options = {}
            text = [text] unless text.is_a? Array

            t = File.read File.open File.join Nineteen::Eighty::Two.templates_dir, 'svg', 'document.eruby'
            context = {
              width: text.longest * 8,
              height: text.count * 8,
              fill_colour: options.fetch(:colour, '#000000'),
              body: body(text)
            }
            Erubis::Eruby.new(t).evaluate(context)
          end

          def self.body text
            rows = []
            text.each_with_index do |line, count|
              Spectrum[line].each_with_index do |line, index|
                rows.push row(line, index + (count * 8))
              end
            end
            rows.flatten
          end

          def self.row list, index = 0
            x = 0
            cells = []
            Decorators::RunLengthEncoder.encode(list).each do |item|
              if item.type == 1
                t = File.read File.open File.join Nineteen::Eighty::Two.templates_dir, 'svg', 'cell.eruby'
                context = {
                  x: x,
                  y: index,
                  width: item.width,
                  height: 1,
                  style: 'on'
                }
                cells.push Erubis::Eruby.new(t).evaluate(context).strip
              end
              x += item.width
            end

            cells
          end

          def self.longest list
            list.map { |i| i.length }.max
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
nineteen-eighty-two-0.1.7 lib/nineteen/eighty/two/formatters/svg_formatter.rb
nineteen-eighty-two-0.1.6 lib/nineteen/eighty/two/formatters/svg_formatter.rb
nineteen-eighty-two-0.1.5 lib/nineteen/eighty/two/formatters/svg_formatter.rb
nineteen-eighty-two-0.1.4 lib/nineteen/eighty/two/formatters/svg_formatter.rb