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

Documentation generated on Mon, 11 Jan 2010 08:08:24 +0100 by phpDocumentor 1.4.1