# -*- perl -*-
# MainCell.pm $Id: MainCell.pm,v 1.9 1999/01/30 15:42:29 jens Exp $
# (C) Copyright Jens G Jensen <jens@arcade.mathematik.uni-freiburg.de>
# This file is part of epsmerge and is distributed under GNU GPL

package MainCell;

use strict;
use Options;
use Cell;
use Formatter;

use vars ('@ISA', '$Page');

@ISA = ('Cell');
$Page = 0;			# For postscript output


# pass ref to array of files to read
sub new {
    my ($class, $fileref) = @_;
    LabelDecoratorCell->newpage( );
    my $opt = Options->new();

    # begin width and height stuff
    my ($w, $h) = ($opt->getopts('ph') || 0, $opt->getopts('pw') || 0);
    if( $w == 0 || $h == 0 ) {
	my $paper = $opt->getopts('p');
	($w, $h) = @$paper;
    }
    # end width and height stuff

    my $self = Cell->new( $opt->getopts('o'), scalar localtime(), 'MainCell', $w, $h);
    my @cells;
    my $place = $opt->getopts('labels');
    foreach (@$fileref) {
	my $cell = EpsCell->new($_);
	# 12 is only _default_ fontsize; can be overridden by user
	$cell = LabelDecoratorCell->new($cell, 12, $place, $opt->getopts('script'))
	    if $opt->getopts('labels');
	push @cells, $cell;
    }

    my $formatter = Formatter->new( );
    ($w, $h) = ($h, $w) if $opt->getopts('O') eq 'L';
    $self->{cont} = CompositeCell->new( $w, $h, @cells, $formatter );
    if( $opt->getopts('header') ) {
	my $headpos = $opt->getopts('header');
	$headpos = "t$headpos" unless $headpos =~ /^b/;
	$self->{cont} = LabelDecoratorCell->new( $self->{cont}, 20, $headpos, $opt->getopts('script'));
    }
    
    bless $self, $class;
    return $self;
}



# This write method does not need a boundingbox since it knows the paper size
# from when the MainCell was created.
# We don't write page size info since Eps'es aren't supposed to know.
sub write {
    my $self = shift;
    my $opt = Options->new();
    if( $Page == 0 ) {
	if( $opt->getopts('ps') ) {
	    print "%!PS-Adobe-3.0\n";
	}
	else {
	    print "%!PS-Adobe-3.0 EPSF-3.0\n";
	}
	printf "%%%%BoundingBox: %d %d %d %d\n", 0, 0, @{$self->{dim}};
	printf "%%%%Creator: (epsmerge %s)\n", $opt->getopts('v');
	print "%%CreationDate: ", scalar localtime, "\n";
	if($ENV{USER}) {
	    if($ENV{HOSTNAME}) {
		print "%%For: $ENV{USER} at $ENV{HOSTNAME}\n";
	    }
	    else {
		print "%%For: $ENV{USER}\n";
	    }
	}
	printf "%%%%Title: (%s)\n", $opt->getopts('o');
	print "%%Orientation: ", $opt->getopts('O') eq 'L' ? "Landscape\n" : "Portrait\n";
	if( $opt->getopts('ps') ) {
	    printf "%%%%Pages: %d\n", $opt->getopts( 'pages' );
	    print "%%PageOrder: Ascend\n";
	}
# TODO: write resources, languagelevel information picked up by the cells, etc.
	print "%%EndComments\n";
    }
    if( $opt->getopts('ps') ) {
	++$Page;
	printf "%%%%Page: %d %d\n", $Page, $Page;
    }
	
print <<SETUP;
0 setgray 0 setlinecap 1 setlinewidth
0 setlinejoin 10 setmiterlimit
[ ] 0 setdash newpath
SETUP
# if( $self->{cont}->getresource('LanguageLevel:') >= 2 ) {
# print "false setoverprint false setstrokeadjust\n"
# }

    my ($w, $h) = $self->dim();
    # rotate image if we print in landscape mode
    if( $opt->getopts('O') eq 'L' ) {
	printf "%d 0 translate 90 rotate\n", $w;
	$self->{cont}->write( 0, 0, $h, $w );
    }
    else {
	$self->{cont}->write( 0, 0, $w, $h );
    }
    # unrotate image if in landscape mode (coordinate system being preserved by save/restore pair)
    if( $opt->getopts('O') eq 'L' ) {
	printf "-90 rotate %d 0 translate\n", $w;
    }
    
    print "showpage\n" if $opt->getopts('print') || $opt->getopts('ps');
    print "%%EOF\n" if ! $opt->getopts('ps') || $Page == $opt->getopts('pages');
}

1;
