class MapPrint::LegendHandler

Constants

HORIZONTAL
OVERFLOW
VERTICAL

Public Class Methods

new(legend) click to toggle source
# File lib/map_print/legend_handler.rb, line 17
def initialize(legend)
  @legend = legend
  validate_data!
  overflow_option_adjustments
  @x_step = @legend[:size][:width] / @legend[:columns]
  @y_step = @legend[:size][:height] / @legend[:rows]
  @elements_in_block = @legend[:orientation] == VERTICAL ? @legend[:rows] : @legend[:columns]
  @legend[:textbox_style] ||= {}

  if @legend[:textbox_size]
    @legend[:textbox_style][:size] = "#{@legend[:textbox_size][:width]}x#{@legend[:textbox_size][:height]}"
  end
end

Public Instance Methods

process() click to toggle source
# File lib/map_print/legend_handler.rb, line 31
def process
  size = @legend[:size]
  tempfile = Tempfile.new ['legend', '.png']
  `convert -density 300 -size #{size[:width]}x#{size[:height]} xc:white #{tempfile.path}`
  image = MiniMagick::Image.new tempfile.path

  image_geometry = ''
  textbox_offset = 0

  if @legend[:image_size]
    image_geometry += "#{@legend[:image_size][:width]}x#{@legend[:image_size][:height]}"
    textbox_offset += @legend[:image_size][:width] if @legend[:image_size][:width]
  end
  textbox_offset += @legend[:textbox_offset] if @legend[:textbox_offset]

  print(image, image_geometry, textbox_offset)
  image
end

Private Instance Methods

available_legend_spots() click to toggle source
# File lib/map_print/legend_handler.rb, line 125
def available_legend_spots
  @legend[:columns] * @legend[:rows]
end
compact_adjustments() click to toggle source
# File lib/map_print/legend_handler.rb, line 153
def compact_adjustments
  if vertical_orientation?
    @legend[:columns] = (@legend[:elements].size / @legend[:rows].to_f).ceil
  elsif horizontal_orientation?
    @legend[:rows] = (@legend[:elements].size / @legend[:columns].to_f).ceil
  end
end
expand_adjustments() click to toggle source
# File lib/map_print/legend_handler.rb, line 141
def expand_adjustments
  if vertical_orientation?
    column_width = @legend[:size][:width] / @legend[:columns]
    compact_adjustments
    @legend[:size][:width] = @legend[:columns] * column_width
  elsif horizontal_orientation?
    row_height = @legend[:size][:height] / @legend[:rows]
    compact_adjustments
    @legend[:size][:height] = @legend[:rows] * row_height
  end
end
get_next_x_y(x, y, z) click to toggle source
# File lib/map_print/legend_handler.rb, line 84
def get_next_x_y(x, y, z)
  if @legend[:orientation] == VERTICAL
    y, x = next_step(y, x, @y_step, @x_step, z)
  else
    x, y = next_step(x, y, @x_step, @y_step, z)
  end

  return x, y
end
horizontal_orientation?() click to toggle source
# File lib/map_print/legend_handler.rb, line 121
def horizontal_orientation?
  @legend[:orientation] == HORIZONTAL
end
next_step(small_step_value, big_step_value, small_step, big_step, z) click to toggle source
# File lib/map_print/legend_handler.rb, line 94
def next_step(small_step_value, big_step_value, small_step, big_step, z)
  if z % @elements_in_block == 0
    big_step_value += big_step
    small_step_value = 0
  else
    small_step_value += small_step
  end

  return small_step_value, big_step_value
end
overflow_compact?() click to toggle source
# File lib/map_print/legend_handler.rb, line 113
def overflow_compact?
  @legend[:overflow].downcase == OVERFLOW[:compact]
end
overflow_expand?() click to toggle source
# File lib/map_print/legend_handler.rb, line 109
def overflow_expand?
  @legend[:overflow].downcase == OVERFLOW[:expand]
end
overflow_hidden?() click to toggle source
# File lib/map_print/legend_handler.rb, line 105
def overflow_hidden?
  @legend[:overflow].nil? || @legend[:overflow].downcase == OVERFLOW[:hidden]
end
overflow_option_adjustments() click to toggle source
# File lib/map_print/legend_handler.rb, line 129
def overflow_option_adjustments
  return unless @legend[:elements].size > available_legend_spots
  case
  when overflow_hidden?
    @legend[:elements] = @legend[:elements][0..(available_legend_spots-1)]
  when overflow_expand?
    expand_adjustments
  when overflow_compact?
    compact_adjustments
  end
end
print(legend_image, image_geometry, textbox_offset) click to toggle source
validate_data!() click to toggle source
# File lib/map_print/legend_handler.rb, line 51
def validate_data!
  raise NoLegendData.new('No legend data present') if @legend.nil? || @legend.empty?
  validate_size!(@legend[:size], InvalidLegendSize)
  validate_layout!
end
validate_layout!() click to toggle source
# File lib/map_print/legend_handler.rb, line 57
def validate_layout!
  raise MissingLayoutInformation.new('Missing column layout information') unless @legend[:columns]
  raise MissingLayoutInformation.new('Missing rows layout information') unless @legend[:rows]
end
vertical_orientation?() click to toggle source
# File lib/map_print/legend_handler.rb, line 117
def vertical_orientation?
  @legend[:orientation] == VERTICAL
end