class PREP::Core::Prep

PREP の中心クラス

Attributes

page_pos_x[R]
page_pos_y[R]
pages[R]
pdf[R]
values[R]

Public Class Methods

new(configuration_file_path = nil) click to toggle source

初期化

設定ファイルのパスが与えられれば初期化 無ければブランクの設定をロード

# File lib/core/prep.rb, line 42
def initialize(configuration_file_path = nil)
  if configuration_file_path
    if File.exists?(configuration_file_path)
      load_configuration(configuration_file_path)
    else
      raise "File not found \"#{configuration_file_path}\"."
    end
  else
    init_configuration
  end
end

Public Instance Methods

current_page() click to toggle source

現在描画中のページインスタンスを返却

# File lib/core/prep.rb, line 207
def current_page
  return @flat_pages[@page_pos_y][@page_pos_x]
end
current_page=(pos) click to toggle source

現在のページを強制的に変更

存在しないページへの移動は不可(例外) 引数の形式はハッシュ: Ex.) { :x => 0, :y => 0 }

# File lib/core/prep.rb, line 225
def current_page=(pos)
  if exists_page?(pos[:x], pos[:y])
    puts "[#{@page_pos_x}:#{@page_pos_y}] => [#{pos[:x]}:#{pos[:y]}]" if ENV["DEBUG"]
    @page_pos_x, @page_pos_y = pos[:x], pos[:y]
    print_flat_pages if ENV["DEBUG"]
    return current_page
  else
    print_flat_pages if ENV["DEBUG"]
    raise "Unknown page index [#{pos[:x]},#{pos[:y]}]."
  end
end
current_page_number() click to toggle source

現在の通しページ番号を返却

# File lib/core/prep.rb, line 212
def current_page_number
  @pages.each_with_index do |page, index|
    if page === current_page
      return index + 1
    end
  end
  raise "Unknown Page instance \"#{page}\"."
end
draw_contents(values) click to toggle source

コンテンツの埋め込み

# File lib/core/prep.rb, line 110
def draw_contents(values)
  content = @content[:content]

  # 描画領域を含めて描画開始
  content.draw(self, page_content_region, values[:content])
end
draw_footers(values) click to toggle source

フッタの埋め込み

# File lib/core/prep.rb, line 133
def draw_footers(values)
  return unless @content.has_identifier?(:footer)

  footer = @content[:footer]

  # 全てのページに対してインデックスを切り替えながら実行
  @flat_pages.each_with_index do |row_pages, y|
    row_pages.each_with_index do |page, x|
      self.current_page = { :x => x, :y => y }
      footer.draw(self, page_footer_region, values[:footer])
    end
  end
end
draw_headers(values) click to toggle source

ヘッダの埋め込み

# File lib/core/prep.rb, line 118
def draw_headers(values)
  return unless @content.has_identifier?(:header)

  header = @content[:header]

  # 全てのページに対してインデックスを切り替えながら実行
  @flat_pages.each_with_index do |row_pages, y|
    row_pages.each_with_index do |page, x|
      self.current_page = { :x => x, :y => y }
      header.draw(self, page_header_region, values[:header])
    end
  end
end
exists_and_drawed_page?(x, y) click to toggle source

指定されたページが存在し描画済みであるかどうかをチェック

# File lib/core/prep.rb, line 185
def exists_and_drawed_page?(x, y)
  if exists_page?(x, y)
    return @flat_pages[y][x].drawed?
  else
    return false
  end
end
exists_move_to_page?(x, y) click to toggle source

移動先のページが存在するかどうかをチェック

# File lib/core/prep.rb, line 166
def exists_move_to_page?(x, y)
  x += @page_pos_x
  y += @page_pos_y

  return exists_and_drawed_page?(x, y)
end
exists_page?(x, y) click to toggle source

指定されたページが存在するかどうかをチェック

# File lib/core/prep.rb, line 174
def exists_page?(x, y)
  if @flat_pages[y].nil?
    return false
  elsif @flat_pages[y][x].nil?
    return false
  else
    return true
  end
end
generate(output_file_path, values = { }) click to toggle source

帳票の生成

# File lib/core/prep.rb, line 70
def generate(output_file_path, values = { })
  # 再描画用に初期値を保持
  @values = values.dup
  @pdf = HPDFDoc.new
  # 日本語対応
  @pdf.use_jp_fonts
  @pdf.use_jp_encodings
  # ページの初期化
  initialize_pages

  draw_contents(values)
  draw_headers(values)
  draw_footers(values)

  # 指定されたファイルへの書込
  @pdf.save_to_file(output_file_path)
rescue => e
  @pdf.save_to_file(output_file_path) if ENV["DEBUG"]
  puts "Error occured!!\n#{e}"
  raise PrepError.new(e)
end
generate_page() click to toggle source

ページオブジェクトの作成とページ設定

# File lib/core/prep.rb, line 194
def generate_page
  page = @pdf.add_page
  page.set_size(@page_config.size, @page_config.orientation)

  return page
end
generate_sample_dataset() click to toggle source

データセット雛形を生成して返却

# File lib/core/prep.rb, line 55
def generate_sample_dataset
  # ヘッダのデータセット生成
  dataset = { }
  if @content.has_identifier?(:header)
    dataset[:header] = @content[:header].generate_sample_dataset(self)
  end
  dataset[:content] = @content[:content].generate_sample_dataset(self)
  if @content.has_identifier?(:footer)
    dataset[:footer] = @content[:footer].generate_sample_dataset(self)
  end

  return dataset
end
group(group_identifiy) click to toggle source

指定されたグループ識別子を検索して返却 存在しない場合は例外発生

# File lib/core/prep.rb, line 353
def group(group_identifiy)
  return @content[group_identifiy]
end
has_group?(group_identifiy) click to toggle source

指定されたグループ識別子の存在確認

# File lib/core/prep.rb, line 358
def has_group?(group_identifiy)
  return @content.drawables.has_key?(group_identifiy.to_sym)
end
init_configuration() click to toggle source

設定の初期化

# File lib/core/prep.rb, line 307
def init_configuration
  raise "Need configuration file!"
end
initialize_pages() click to toggle source

ページの初期化

# File lib/core/prep.rb, line 93
def initialize_pages
  # 一次元配列
  @pages = []
  # 二次元配列
  @flat_pages = []
  # ページの作成
  page = generate_page
  # 1ページ目の登録
  @pages << page
  @flat_pages = [[page]] # [0][0] 位置への追加
  # 現在のページの位置情報を初期化
  @page_pos_x, @page_pos_y = 0, 0

  return page
end
load_configuration(configuration_file_path) click to toggle source

設定ファイルのロード

# File lib/core/prep.rb, line 312
def load_configuration(configuration_file_path)
  # YAML からハッシュ形式に変換
  config_values = YAML.load_file(configuration_file_path)

  # ページ設定情報を取り込み
  @page_config = Page.new
  if config_values["page"]
    values = config_values["page"]
    if !values["size"].nil? && values["size"] != ""
      @page_config.size = Page::SIZES[values["size"].to_sym]
    end
    if !values["orientation"].nil? && values["orientation"] != ""
      @page_config.orientation = Page::ORIENTATIONS[values["orientation"].to_sym]
    end
    if !values["margin"].nil?
      margin_values = values["margin"].keys.inject({ }) do |hash, key|
        hash[key.to_sym] = values["margin"][key].mm2pixcel
        next hash
      end
      @page_config.margin = margin_values
    end
    if !values["header_height"].nil?
      @page_config.header_height = values["header_height"].mm2pixcel
    end
    if !values["footer_height"].nil?
      @page_config.footer_height = values["footer_height"].mm2pixcel
    end
  end

  # コンテンツ定義情報を読み込む
  # page 以外について読み込みを実施
  @content = Group.new
  config_values.keys.each do |identifier|
    unless identifier == "page"
      @content.add_drawable(identifier, config_values[identifier], true)
    end
  end
end
move_page_to(x, y) click to toggle source

ページの移動および追加

指定された位置への移動に際してページが存在しなければページを追加

# File lib/core/prep.rb, line 150
def move_page_to(x, y)
  puts "[#{@page_pos_x}:#{@page_pos_y}] => [#{@page_pos_x + x}:#{@page_pos_y + y}]" if ENV["DEBUG"]
  @page_pos_x, @page_pos_y = @page_pos_x + x, @page_pos_y + y

  @flat_pages[@page_pos_y] ||= []
  if @flat_pages[@page_pos_y][@page_pos_x].nil?
    @flat_pages[@page_pos_y][@page_pos_x] = (page = generate_page)
    @pages << page
  end

  print_flat_pages if ENV["DEBUG"]

  return @flat_pages[@page_pos_y][@page_pos_x]
end
page_content_region() click to toggle source

コンテンツ描画領域の取得

# File lib/core/prep.rb, line 253
def page_content_region
  # 全体の描画領域を取得
  width = current_page.get_width
  height = current_page.get_height
  x = 0
  y = 0
  # マージンを含める
  x += @page_config.margin[:left]
  width -= (@page_config.margin[:left] + @page_config.margin[:right])
  y += @page_config.margin[:top]
  height -= (@page_config.margin[:top] + @page_config.margin[:bottom])
  # ヘッダ、および、フッタ領域を含める
  y += @page_config.header_height
  height -= (@page_config.header_height + @page_config.footer_height)

  return Region.new(x, y, width, height)
end
page_header_region() click to toggle source

ヘッダ領域の取得

# File lib/core/prep.rb, line 272
def page_header_region
  # 全体の描画領域を取得
  width = current_page.get_width
  height = current_page.get_height
  x = 0
  y = 0
  # マージンを含める
  x += @page_config.margin[:left]
  width -= (@page_config.margin[:left] + @page_config.margin[:right])
  y += @page_config.margin[:top]
  # 高さをヘッダ領域に変更
  height = @page_config.header_height

  return Region.new(x, y, width, height)
end
print_flat_pages() click to toggle source

ページ構成を模式印字するデバッグ用メソッド

total_pages() click to toggle source

現在の総ページ数を返却

# File lib/core/prep.rb, line 202
def total_pages
  return @pages.size
end