# -*- perl -*-
# MainCell.pm $Id: MainCell.pm,v 1.8 1999/01/23 19:12:15 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 vars ('@ISA');

@ISA = ('Cell');

use Cell;
use Formatter;

# 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;
    }

    # begin orientation stuff
    $self->{'or'} = $opt->getopts('O');
    # end orientation stuff

    my $formatter = Formatter->new( );
    ($w, $h) = ($h, $w) if $self->{'or'} 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'));
    }
	
    $self->{title} = $opt->getopts('o');
    $self->{ver} = $opt->getopts('v');
    $self->{'print'} = $opt->getopts('print');
    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;
    print "%!PS-Adobe-3.0 EPSF-3.0\n";
    printf "%%%%BoundingBox: %d %d %d %d\n", 0, 0, @{$self->{dim}};
    print "%%Creator: (epsmerge $self->{ver})\n" if $self->{ver};
    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";
	}
    }
    print "%%Title: ($self->{title})\n" if $self->{title};
    print "%%Orientation: ", $self->{'or'} eq 'L' ? "Landscape\n" : "Portrait\n";
# TODO: write resources, languagelevel information picked up by the cells, etc.
    print "%%EndComments\n";

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( $self->{'or'} 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( $self->{'or'} eq 'L' ) {
	printf "-90 rotate %d 0 translate\n", $w;
    }
    
    print "showpage\n" if $self->{'print'};
    print "%%EOF\n";
}

1;
