Class: PREP::Core::Image

Inherits:
Drawable show all
Defined in:
lib/core/image.rb

Overview

イメージ描画構成要素クラス

Constant Summary collapse

@@default_values =
{
  :layer  => 1,
  :expand => false,
}

Instance Attribute Summary

Attributes inherited from Drawable

#identifier, #layer

Instance Method Summary collapse

Methods inherited from Drawable

#<=>, #calculate_pos, #key_string_to_symbol, #rewind_current_page, #visible?

Constructor Details

#initialize(identifier, values = { }) ⇒ Image

Returns a new instance of Image.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/core/image.rb', line 21

def initialize(identifier, values = { })
  values = @@default_values.merge(key_string_to_symbol(values))
  super(identifier, values[:layer])

  @region = Region.new(values[:region][:x].mm2pixcel,
                       values[:region][:y].mm2pixcel,
                       values[:region][:width].mm2pixcel,
                       values[:region][:height].mm2pixcel)
  @image_path = values[:image_path].to_s
  @expand = values[:expand]
end

Instance Method Details

#calculate_region(prep, region, value, stop_on_drawable = nil) ⇒ Object

イメージ描画領域の計算



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/core/image.rb', line 34

def calculate_region(prep, region, value, stop_on_drawable = nil)
  if self == stop_on_drawable
    raise ReRenderJump.new(region)
  end

  puts "Calculate region for #{self.class}: #{self.identifier} region: #{region}" if ENV["DEBUG"]
  ret_region = Region.new(0, 0,
                          region.width - (@region.x + @region.width),
                          region.height - (@region.y + @region.height))
  return @region.x + @region.width, @region.y + @region.height
end

#draw(prep, region, value, stop_on_drawable = nil) ⇒ Object

イメージの描画



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/core/image.rb', line 47

def draw(prep, region, value, stop_on_drawable = nil)
  if self === stop_on_drawable
    raise ReRenderJump.new(region)
  end
  STDERR.puts("Draw on #{self.class} #{self.identifier}") if ENV['DEBUG']
  # 領域判定
  calculate_region(prep, region, value)
  
  # イメージファイルの指定があるか確認
  if value.to_s != ""
    # Load png image from specific file path
    image_path = value.to_s
  else
    # Load png image from default path
    image_path = @image_path.dup
  end
  begin
    image_data = prep.pdf.load_png_image_from_file(image_path)
  rescue => e
    raise "Failed to load the PNG image from '#{image_path}'"
  end
  region_backup = @region.dup
  if @expand_region
    @region = @expand_region.dup
    @expand_region = nil
  end
  pos_x, pos_y = calculate_pos(prep.current_page, region, @region.x, @region.y)
  # draw png image
  prep.current_page.draw_image(image_data,
                               pos_x, pos_y - @region.height,
                               @region.width, @region.height)
  @region = region_backup
  prep.current_page.drawed = true
end