PHPExcel_Shared
[ class tree: PHPExcel_Shared ] [ index: PHPExcel_Shared ] [ all elements ]

Source for file Excel5.php

Documentation is available at Excel5.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2009 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category   PHPExcel
  22.  * @package    PHPExcel_Shared
  23.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.7, 2009-04-22
  26.  */
  27.  
  28. /** PHPExcel_Cell */
  29. require_once 'PHPExcel/Cell.php';
  30.  
  31. /**
  32.  * PHPExcel_Shared_Excel5
  33.  *
  34.  * @category   PHPExcel
  35.  * @package    PHPExcel_Shared
  36.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  37.  */
  38. {
  39.     /**
  40.      * Get the width of a column in pixels. We use the relationship y = ceil(7x) where
  41.      * x is the width in intrinsic Excel units (measuring width in number of normal characters)
  42.      * This holds for Arial 10
  43.      *
  44.      * @param PHPExcel_Worksheet $sheet The sheet
  45.      * @param integer $col The column
  46.      * @return integer The width in pixels
  47.     */
  48.     public static function sizeCol($sheet$col 'A')
  49.     {
  50.         $columnDimensions $sheet->getColumnDimensions();
  51.  
  52.         // first find the true column width in pixels (uncollapsed and unhidden)
  53.         if isset($columnDimensions[$col]and $columnDimensions[$col]->getWidth(!= -{
  54.  
  55.             // then we have column dimension with explicit width
  56.             $columnDimension $columnDimensions[$col];
  57.             $width $columnDimension->getWidth();
  58.             $pixelWidth = (int) ceil($width)// here we assume Arial 10
  59.  
  60.         else if ($sheet->getDefaultColumnDimension()->getWidth(!= -1{
  61.  
  62.             // then we have default column dimension with explicit width
  63.             $defaultColumnDimension $sheet->getDefaultColumnDimension();
  64.             $width $defaultColumnDimension->getWidth();
  65.             $pixelWidth = (int) ceil($width)// here we assume Arial 10
  66.  
  67.         else {
  68.             $pixelWidth 64// here we assume Arial 10
  69.         }
  70.  
  71.         // now find the effective column width in pixels
  72.         if (isset($columnDimensions[$col]and !$columnDimensions[$col]->getVisible()) {
  73.             $effectivePixelWidth 0;
  74.         else {
  75.             $effectivePixelWidth $pixelWidth;
  76.         }
  77.  
  78.         return $effectivePixelWidth;
  79.     }
  80.  
  81.     /**
  82.      * Convert the height of a cell from user's units to pixels. By interpolation
  83.      * the relationship is: y = 4/3x. If the height hasn't been set by the user we
  84.      * use the default value. If the row is hidden we use a value of zero.
  85.      *
  86.      * @param PHPExcel_Worksheet $sheet The sheet
  87.      * @param integer $row The row index (1-based)
  88.      * @return integer The width in pixels
  89.      */
  90.     public static function sizeRow($sheet$row 1)
  91.     {
  92.         $rowDimensions $sheet->getRowDimensions();
  93.  
  94.         // first find the true row height in pixels (uncollapsed and unhidden)
  95.         if isset($rowDimensions[$row]and $rowDimensions[$row]->getRowHeight(!= -1{
  96.  
  97.             // then we have a row dimension
  98.             $rowDimension $rowDimensions[$row];
  99.             $rowHeight $rowDimension->getRowHeight();
  100.             $pixelRowHeight = (int) ceil($rowHeight 3)// here we assume Arial 10
  101.  
  102.         else if ($sheet->getDefaultRowDimension()->getRowHeight(!= -1{
  103.  
  104.             // then we have a default row dimension with explicit height
  105.             $defaultRowDimension $sheet->getDefaultRowDimension();
  106.             $rowHeight $defaultRowDimension->getRowHeight();
  107.             $pixelRowHeight = (int) ceil($rowHeight 3)// here we assume Arial 10
  108.  
  109.         else {
  110.             $pixelRowHeight 17// here we assume Arial 10
  111.         }
  112.  
  113.         // now find the effective row height in pixels
  114.         if isset($rowDimensions[$row]and !$rowDimensions[$row]->getVisible() ) {
  115.             $effectivePixelRowHeight 0;
  116.         else {
  117.             $effectivePixelRowHeight $pixelRowHeight;
  118.         }
  119.  
  120.         return $effectivePixelRowHeight;
  121.     }
  122.  
  123.     /**
  124.      * Get the horizontal distance in pixels between two anchors
  125.      * The distanceX is found as sum of all the spanning columns widths minus correction for the two offsets
  126.      *
  127.      * @param PHPExcel_Worksheet $sheet 
  128.      * @param string $startColumn 
  129.      * @param integer $startOffset Offset within start cell measured in 1/1024 of the cell width
  130.      * @param string $endColumn 
  131.      * @param integer $endOffset Offset within end cell measured in 1/1024 of the cell width
  132.      * @return integer Horizontal measured in pixels
  133.      */
  134.     public static function getDistanceX(PHPExcel_Worksheet $sheet$startColumn 'A'$startOffsetX 0$endColumn 'A'$endOffsetX 0)
  135.     {
  136.         $distanceX 0;
  137.  
  138.         // add the widths of the spanning columns
  139.         $startColumnIndex PHPExcel_Cell::columnIndexFromString($startColumn1// 1-based
  140.         $endColumnIndex PHPExcel_Cell::columnIndexFromString($endColumn1// 1-based
  141.         for ($i $startColumnIndex$i <= $endColumnIndex++$i{
  142.             $distanceX += self::sizeCol($sheetPHPExcel_Cell::stringFromColumnIndex($i));
  143.         }
  144.  
  145.         // correct for offsetX in startcell
  146.         $distanceX -= (int) floor(self::sizeCol($sheet$startColumn$startOffsetX 1024);
  147.  
  148.         // correct for offsetX in endcell
  149.         $distanceX -= (int) floor(self::sizeCol($sheet$endColumn($endOffsetX 1024));
  150.  
  151.         return $distanceX;
  152.     }
  153.  
  154.     /**
  155.      * Get the vertical distance in pixels between two anchors
  156.      * The distanceY is found as sum of all the spanning rows minus two offsets
  157.      *
  158.      * @param PHPExcel_Worksheet $sheet 
  159.      * @param string $startRow (1-based)
  160.      * @param integer $startOffset Offset within start cell measured in 1/256 of the cell height
  161.      * @param string $endRow (1-based)
  162.      * @param integer $endOffset Offset within end cell measured in 1/256 of the cell height
  163.      * @return integer Vertical distance measured in pixels
  164.      */
  165.     public static function getDistanceY(PHPExcel_Worksheet $sheet$startRow 1$startOffsetY 0$endRow 1$endOffsetY 0)
  166.     {
  167.         $distanceY 0;
  168.  
  169.         // add the widths of the spanning rows
  170.         for ($row $startRow$row <= $endRow++$row{
  171.             $distanceY += self::sizeRow($sheet$row);
  172.         }
  173.  
  174.         // correct for offsetX in startcell
  175.         $distanceY -= (int) floor(self::sizeRow($sheet$startRow$startOffsetY 256);
  176.  
  177.         // correct for offsetX in endcell
  178.         $distanceY -= (int) floor(self::sizeRow($sheet$endRow($endOffsetY 256));
  179.  
  180.         return $distanceY;
  181.     }
  182.  
  183.     /**
  184.      * Convert 1-cell anchor coordinates to 2-cell anchor coordinates
  185.      * This function is ported from PEAR Spreadsheet_Writer_Excel with small modifications
  186.      *
  187.      * Calculate the vertices that define the position of the image as required by
  188.      * the OBJ record.
  189.      *
  190.      *         +------------+------------+
  191.      *         |     A      |      B     |
  192.      *   +-----+------------+------------+
  193.      *   |     |(x1,y1)     |            |
  194.      *   |  1  |(A1)._______|______      |
  195.      *   |     |    |              |     |
  196.      *   |     |    |              |     |
  197.      *   +-----+----|    BITMAP    |-----+
  198.      *   |     |    |              |     |
  199.      *   |  2  |    |______________.     |
  200.      *   |     |            |        (B2)|
  201.      *   |     |            |     (x2,y2)|
  202.      *   +---- +------------+------------+
  203.      *
  204.      * Example of a bitmap that covers some of the area from cell A1 to cell B2.
  205.      *
  206.      * Based on the width and height of the bitmap we need to calculate 8 vars:
  207.      *     $col_start, $row_start, $col_end, $row_end, $x1, $y1, $x2, $y2.
  208.      * The width and height of the cells are also variable and have to be taken into
  209.      * account.
  210.      * The values of $col_start and $row_start are passed in from the calling
  211.      * function. The values of $col_end and $row_end are calculated by subtracting
  212.      * the width and height of the bitmap from the width and height of the
  213.      * underlying cells.
  214.      * The vertices are expressed as a percentage of the underlying cell width as
  215.      * follows (rhs values are in pixels):
  216.      *
  217.      *       x1 = X / W *1024
  218.      *       y1 = Y / H *256
  219.      *       x2 = (X-1) / W *1024
  220.      *       y2 = (Y-1) / H *256
  221.      *
  222.      *       Where:  X is distance from the left side of the underlying cell
  223.      *               Y is distance from the top of the underlying cell
  224.      *               W is the width of the cell
  225.      *               H is the height of the cell
  226.      *
  227.      * @param PHPExcel_Worksheet $sheet 
  228.      * @param string $coordinates E.g. 'A1'
  229.      * @param integer $offsetX Horizontal offset in pixels
  230.      * @param integer $offsetY Vertical offset in pixels
  231.      * @param integer $width Width in pixels
  232.      * @param integer $height Height in pixels
  233.      * @return array 
  234.      */
  235.     public static function oneAnchor2twoAnchor($sheet$coordinates$offsetX$offsetY$width$height)
  236.     {
  237.         list($column$rowPHPExcel_Cell::coordinateFromString($coordinates);
  238.         $col_start PHPExcel_Cell::columnIndexFromString($column1;
  239.         $row_start $row 1;
  240.         
  241.         $x1 $offsetX;
  242.         $y1 $offsetY;
  243.  
  244.         // Initialise end cell to the same as the start cell
  245.         $col_end    $col_start;  // Col containing lower right corner of object
  246.         $row_end    $row_start;  // Row containing bottom right corner of object
  247.  
  248.         // Zero the specified offset if greater than the cell dimensions
  249.         if ($x1 >= self::sizeCol($sheetPHPExcel_Cell::stringFromColumnIndex($col_start))) {
  250.             $x1 0;
  251.         }
  252.         if ($y1 >= self::sizeRow($sheet$row_start 1)) {
  253.             $y1 0;
  254.         }
  255.  
  256.         $width      $width  $x1 -1;
  257.         $height     $height $y1 -1;
  258.  
  259.         // Subtract the underlying cell widths to find the end cell of the image
  260.         while ($width >= self::sizeCol($sheetPHPExcel_Cell::stringFromColumnIndex($col_end))) {
  261.             $width -= self::sizeCol($sheetPHPExcel_Cell::stringFromColumnIndex($col_end));
  262.             ++$col_end;
  263.         }
  264.  
  265.         // Subtract the underlying cell heights to find the end cell of the image
  266.         while ($height >= self::sizeRow($sheet$row_end 1)) {
  267.             $height -= self::sizeRow($sheet$row_end 1);
  268.             ++$row_end;
  269.         }
  270.  
  271.         // Bitmap isn't allowed to start or finish in a hidden cell, i.e. a cell
  272.         // with zero height or width.
  273.         if (self::sizeCol($sheetPHPExcel_Cell::stringFromColumnIndex($col_start)) == 0{
  274.             return;
  275.         }
  276.         if (self::sizeCol($sheetPHPExcel_Cell::stringFromColumnIndex($col_end))   == 0{
  277.             return;
  278.         }
  279.         if (self::sizeRow($sheet$row_start 1== 0{
  280.             return;
  281.         }
  282.         if (self::sizeRow($sheet$row_end 1)   == 0{
  283.             return;
  284.         }
  285.  
  286.         // Convert the pixel values to the percentage value expected by Excel
  287.         $x1 $x1     self::sizeCol($sheetPHPExcel_Cell::stringFromColumnIndex($col_start))   1024;
  288.         $y1 $y1     self::sizeRow($sheet$row_start 1)   *  256;
  289.         $x2 $width  self::sizeCol($sheetPHPExcel_Cell::stringFromColumnIndex($col_end))     1024// Distance to right side of object
  290.         $y2 $height self::sizeRow($sheet$row_end 1)     *  256// Distance to bottom of object
  291.  
  292.         $startCoordinates PHPExcel_Cell::stringFromColumnIndex($col_start($row_start 1);
  293.         $endCoordinates PHPExcel_Cell::stringFromColumnIndex($col_end($row_end 1);
  294.  
  295.         $twoAnchor array(
  296.             'startCoordinates' => $startCoordinates,
  297.             'startOffsetX' => $x1,
  298.             'startOffsetY' => $y1,
  299.             'endCoordinates' => $endCoordinates,
  300.             'endOffsetX' => $x2,
  301.             'endOffsetY' => $y2,
  302.         );
  303.  
  304.         return  $twoAnchor;
  305.     }
  306.  
  307. }

Documentation generated on Wed, 22 Apr 2009 08:57:39 +0200 by phpDocumentor 1.4.1