mif-table.prl 6.53 KB
#!/usr/local/bin/perl5 -w
#
# Copyright (C) 1996-1998 by the Board of Trustees
#    of Leland Stanford Junior University.
# 
# This file is part of the SimOS distribution. 
# See LICENSE file for terms of the license. 
#



###
### Documentation
###

#  Basically the input to this script is a file with the table described
#  as perl data structures. There are four global variables you need to define.
#
#  mifTable - an associative array of table attributes
#     tableFormat - the name of the maker table format
#     cols        - the number of columns
#     caption     - the caption for the table
#
#  The following three variables are arrays of associatve arrays. In mifCol
#  there is an element for each column. Each element is an associative array
#  describing the columns attributes. In mifHeaderCell and mifBodyCell
#  there is an element for each cell. These arrays are one dimensional and
#  are listed in row major order. Each element is an associative array
#  describing the cells attributes.
#
#  mifCol - array of associative arrays of:
#     width - the width of the column in inches
#
#  mifHeaderCell - array of <cell description>
#
#  mifBodyCell - array of <cell description>
#
#  <cell description> - associative array of:
#     text         - the text of the cell
#     bold         - 1 means bold
#     alignment    - (Left, Center, Right)
#     angle        - angle in degrees of text
#     rowStraddle  - the number of rows to straddle
#     colStraddle  - the number of columns to straddle
#     leftBorder   - <ruling description>
#     rightBorder  - <ruling description>
#     bottomBorder - <ruling description>
#     topBorder    - <ruling description>
#
#     <ruling description> - (Thin, Thick, Double, None, Very Thin, Medium)

###
### Code
###

# need to avoid errors
$mifCol[0] = 0;
$mifBodyCell[0]{'text'} = "";
$mifHeaderCell[0]{'text'} = "";

# include user table definition
$ARGV[0] || die "Need a file to input!\n";
do $ARGV[0];

###
### MIF header
###
$format = (defined($mifTable{"tableFormat"})) ? $mifTable{"tableFormat"} : "Format A";

print STDOUT "<MIFFile 4.0>\n";
print STDOUT "<Tbls\n";
print STDOUT "    <Tbl\n";
print STDOUT "        <TblID 1>\n";
print STDOUT "        <TblTag `$format'>\n";


###
### column info
###
defined($mifTable{"cols"}) || 
    die "You need to specify  the number of columns!\n";

print STDOUT "        <TblNumColumns ", $mifTable{"cols"}, ">\n"; 

foreach $i (0..($mifTable{"cols"}-1)) {
    print STDOUT "        <TblColumnWidth $mifCol[$i]{'width'}\">\n";
}


###
### table title
###
if (defined($mifTable{'caption'})) {
    print STDOUT "        <TblTitleContent\n";
    print STDOUT "            <Para\n";
    print STDOUT "                <PgfTag `TableTitle'>\n";
    print STDOUT "                <ParaLine\n";
    print STDOUT "                    <String `$mifTable{'caption'}'>\n";
    print STDOUT "                >\n";
    print STDOUT "            >\n";
    print STDOUT "        >\n";
}


###
### header cells
###
doCellArray(\@mifHeaderCell, "TblH", "CellHeading");


###
### body cells
###
doCellArray(\@mifBodyCell, "TblBody", "CellBody");


###
### table footer
###
print STDOUT "    >\n";
print STDOUT ">\n";


###
### table reference
###
print STDOUT "<Para\n";
print STDOUT "    <PgfTag `Body'>\n";
print STDOUT "    <ParaLine\n";
print STDOUT "        <String `Table1#'>\n";
print STDOUT "        <ATbl 1>\n";
print STDOUT "        <String `#'>\n";
print STDOUT "    >\n";
print STDOUT ">\n";


exit;


sub doCellArray {
    my($cellArrayRef, $cellType, $cellParaTag) = @_;
    
    print STDOUT "        <$cellType\n";

    $cell = 0;
    while ($cell < $#$cellArrayRef) {

        print STDOUT "            <Row\n";

        foreach $i (0..($mifTable{"cols"}-1)) {
            print STDOUT "                <Cell\n";

            $rows = $cellArrayRef->[$cell]{"rowStraddle"};
            if (defined($rows) && ($rows != 1)) {
                print STDOUT "                    <CellRows $rows>\n";                
            }

            $cols = $cellArrayRef->[$cell]{"colStraddle"};
            if (defined($cols) && ($cols != 1)) {
                print STDOUT "                    <CellColumns $cols>\n";
            }

            $angle = $cellArrayRef->[$cell]{"angle"};
            if (defined($angle)) {
                print STDOUT "                    <CellAngle $angle>\n";
            }

            $lborder = $cellArrayRef->[$cell]{"leftBorder"};
            if (defined($lborder)) {
                print STDOUT "                    <CellLRuling `$lborder'>\n";
            }

            $rborder = $cellArrayRef->[$cell]{"rightBorder"};
            if (defined($rborder)) {
                print STDOUT "                    <CellRRuling `$rborder'>\n";
            }

            $bborder = $cellArrayRef->[$cell]{"bottomBorder"};
            if (defined($bborder)) {
                print STDOUT "                    <CellBRuling `$bborder'>\n";
            }

            $tborder = $cellArrayRef->[$cell]{"topBorder"};
            if (defined($tborder)) {
                print STDOUT "                    <CellTRuling `$tborder'>\n";
            }

            $text = $cellArrayRef->[$cell]{"text"};
            if (defined($text)) {
                print STDOUT "                    <CellContent\n";
                print STDOUT "                        <Para\n";
                print STDOUT "                            <PgfTag `$cellParaTag'>\n";

                $align = $cellArrayRef->[$cell]{'alignment'};
                $bold = $cellArrayRef->[$cell]{'bold'};
                if (defined($align) || defined($bold)) {
                    print STDOUT "                            <Pgf\n";
                    if (defined($align)) {
                        print STDOUT "                                <PgfAlignment $align>\n";
                    }
                    if (defined($bold) && $bold) {
                        print STDOUT "                                <PgfFont <FWeight `Bold'>>\n";
                    }
                    print STDOUT "                            >\n";
                }

                print STDOUT "                            <ParaLine\n";
                print STDOUT "                                <String `$text'>\n";
                print STDOUT "                            >\n";
                print STDOUT "                        >\n";
                print STDOUT "                    >\n";
            }
            
            print STDOUT "                >\n";
            $cell++;
        }

        print STDOUT "            >\n"; 
    }
    
    print STDOUT "        >\n";
}