=pod

=head1 NAME

pdfrwt.pm - PDFJインターフェース

=head1 概要

pdf作成モジュールPDFJ呼び出し用のインターフェース群

 # pdfrwt.pm - PDFJ ReportWriterTool module
 # nouhinsho.pl(2002 <nakajima@netstock.co.jp>)より転用
 # 2004 <hori@japannet.co.jp>
 # 

=head1 サブルーチン

=cut

package pdfrwt;

use lib '/sei/sne/jnet/hori/wiki/lib';
use PDFJ 'EUC';
use strict;
my @me;

# 各種のパラメーター
my @pagesize = (842,595); # A4横
my $bodypadding = 20 ; 72; # ページ余白
my $bodywidth = $pagesize[0] - $bodypadding * 2; # 表示領域幅
my $bodyheight = $pagesize[1] - $bodypadding * 2; # 表示領域高さ
my %cellwidth;
my $cellpadding = 2; # セル内余白
my $normalfontsize = 10; # フォントサイズ
my $bigfontsize = 15; # フォントサイズ大

=head2 new

 # コンストラクタ

=cut

sub new {
	my($class) = @_;
	my $self = bless {}, $class;
	$self->initialize;
	$self;
}

=head2 initialize

 # 初期化
 # 文書オブジェクト、フォントオブジェクト、テキストスタイルを作成

=cut

sub initialize {
	my($self) = @_;
	my $doc = PDFJ::Doc->new(1.2, @pagesize);
	$self->{doc} = $doc;
	$self->{font}{mincho} = 
		$doc->new_font('Ryumin-Light', 'EUC-H', 'Times-Roman');
	$self->{font}{gothic} = 
		$doc->new_font('GothicBBB-Medium', 'EUC-H', 'Helvetica');
	$self->{tstyle}{mincho} = 
		TStyle(font => $self->{font}{mincho}, fontsize => $normalfontsize);
	$self->{tstyle}{gothic} = 
		TStyle(font => $self->{font}{gothic}, fontsize => $normalfontsize);
	$self->{tstyle}{gothicbig} = 
		TStyle(font => $self->{font}{gothic}, fontsize => $bigfontsize);
}

=head2 homepos

 # ホームポジションを返す

=cut

sub homepos{
    return $bodypadding,$bodypadding+$bodyheight;
}

=head2 linepare

 # 段落を作成して返す
 # linepara(テキストスタイル名, 配置, preskip, postskip, 文字列リスト)

=cut

sub linepara {
	my($self, $tstylename, $align, $preskip, $postskip, @str) = @_;
	Paragraph(Text(@str, $self->{tstyle}{$tstylename}),
		PStyle(size => $bodywidth, linefeed => '100%', align => $align,
			preskip => $preskip, postskip => $postskip));
}

=head2 makeheader

 # ヘッダブロックを作成
 # makeheader(日付, 顧客名)

=cut

sub makeheader {
	my($self, $date, $customer) = @_;
	$self->{header} = Block('V',
		$self->linepara('gothicbig', 'm', 20, 20, '納品書'),
		$self->linepara('mincho', 'e', 5, 5, $date),
		$self->linepara('gothicbig', 'b', 10, 10, 
			Text($customer, TStyle(withline => 1))),
		$self->linepara('mincho', 'e', 5, 5, @me),
		BStyle());
}

=head2 cellpstyle

 # セルで使われる段落スタイルを作成して返す
 # cellpstyle(セル幅, 配置)

=cut

sub cellpstyle {
	my($self, $cellsize, $align) = @_;
	PStyle(size => $cellsize - $cellpadding * 2, linefeed => '100%',
		align => $align);
}

=head2 cellblock

 # セルブロックを作成して返す
 # cellblock(テキストスタイル名, セル幅, 配置, 線, 文字列リスト)

=cut

sub cellblock {
	my($self, $tstylename, $cellsize, $align,$withbox, @str) = @_;
	Block('V', Paragraph(Text(@str, $self->{tstyle}{$tstylename}), 
		$self->cellpstyle($cellsize, $align)),
		BStyle(padding => $cellpadding, withbox => $withbox, 
		withboxstyle => SStyle(linewidth => 0.5)));
}

=head2 maketable

 # 表ブロックを作成
 # maketable(データ配列参照)
 # データ配列参照の要素は [名称, 数量, 摘要]

=cut

sub maketable {
	my($self, $dataarray) = @_;
	my @rows;
	# 見出し行
	push @rows, Block('H',
		$self->cellblock('gothic', 200, 'm','lr', '名称'),
		$self->cellblock('gothic', 50, 'm','lr', '数量'),
		$self->cellblock('gothic', 200, 'm','lr', '摘要'),
		BStyle(adjust => 1, postnobreak => 1, withbox => 'b', 
			withboxstyle => SStyle(linewidth => 0.5)));
	# データ行
	for my $data(@$dataarray) {
		my($name, $quantity, $note) = @$data;
		push @rows, Block('H',
			$self->cellblock('mincho', 'name', 'b','lr', $name),
			$self->cellblock('mincho', 'quantity', 'e','lr', $quantity),
			$self->cellblock('mincho', 'note', 'b','lr', $note),
			BStyle(adjust => 1));
	}
	$self->{table} = Block('V', @rows, BStyle(repeatheader => 1, 
		withbox => 'sr5', 
		withboxstyle => SStyle(linewidth => 1)));
}

=head2 makepage

 # ヘッダブロックと表ブロックをページに配置する

=cut

sub makepage {
	my($self) = @_;
	my $doc = $self->{doc};
	# 一つのブロックにまとめた上で、
	my $pageblock = Block('V', $self->{header}, 20, $self->{table}, BStyle());
	# 表示領域の高さで分割し、
	my @blocks = $pageblock->break($bodyheight);
	# 各ブロックをページに配置
	my $pages = @blocks;
	for my $block(@blocks) {
		my $page = $doc->new_page;
		$block->show($page, $bodypadding, $bodypadding + $bodyheight, 'tl');
		# ページ上部にページ番号を表示
		my $header = $self->linepara('mincho', 'e', 0, 0, 
			$page->pagenum . "/$pages ページ");
		$header->show($page, $bodypadding, $bodypadding + $bodyheight + 
			$bodypadding / 2, 'tl');
	}
}

=head2 print

 # 出力
 # print(ファイル名)

=cut

sub print {
	my($self, $file) = @_;
	$self->{doc}->print($file);
}
1;

=head1 AUTHOR INFORMATION

 ;######################################################################
 ;#
 ;# pdfrwt.pm: PDFJ用インターフェース群
 ;#
 ;# Copyright (c) 2004 Masashi Hori <hori@japannet.co.jp>
 ;# JapanNet. All Rights Reserved.
 ;#
 ;#
 ;######################################################################

=cut

