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 HEADER @svg << %Q{} 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{} end end def close unless @close @svg << "" @svg.gsub!(HEIGHT_STRING, (@line + BUFFER).to_s) @close = true end end end class PageFullError < StandardError end end