Sha256: 20a7af98b50e2a015f7428053b666a0169afbf7a3a22f2d21168a8a13d5e1bd8

Contents?: true

Size: 1.63 KB

Versions: 4

Compression:

Stored size: 1.63 KB

Contents

module Hershey
  class Page
    SPACE = Character.new(' ').spacing.freeze
    BUFFER = 20.freeze
    SIDE = 10.freeze
    HEIGHT_STRING = "jfdsklahfdjksjhaljfdskahj".freeze

    def initialize(width: 1000, height: 1300, stroke: 1)
      @close = false
      @height = height
      @width = width
      @current_offset = SIDE
      @line = BUFFER
      @svg = <<-HEADER
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="#{@width}" height="#{HEIGHT_STRING}" version="1.1"
  xmlns="http://www.w3.org/2000/svg" style="stroke-width:#{stroke};">
      HEADER
      @svg << %Q{<g transform="translate(#{SIDE},#{@line})">}
    end

    def <<(word)
      if word.is_a?(Word)
        if word.spacing + @current_offset > @width
          new_line
        end
        @svg << word.to_path(@current_offset)
        @current_offset += word.spacing
      elsif word == :space
        @current_offset += SPACE
      elsif word == :break
        new_line
      end
    end
    alias_method :write, :<<

    def write_out
      close
      @svg
    end
    alias_method :svg, :write_out

    private

    def new_line
      @line += BUFFER * 2
      if @line > @height
        @line -= BUFFER
        raise PageFullError, "The page is full"
      else
        @current_offset = SIDE
        @svg << %Q{</g><g transform="translate(#{SIDE},#{@line})">}
      end
    end

    def close
      unless @close
        @svg << "</g></svg>"
        @svg.gsub!(HEIGHT_STRING, (@line + BUFFER).to_s)
        @close = true
      end
    end
  end

  class PageFullError < StandardError
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hershey-0.0.11 lib/hershey/page.rb
hershey-0.0.10 lib/hershey/page.rb
hershey-0.0.9 lib/hershey/page.rb
hershey-0.0.8 lib/hershey/page.rb