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

Documentation generated on Mon, 10 Aug 2009 08:03:37 +0200 by phpDocumentor 1.4.1