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

Documentation generated on Sun, 27 Feb 2011 16:31:14 -0800 by phpDocumentor 1.4.3