/** * Calculate an (approximate) OpenXML column width, based on font size and text contained * * @param int $fontSize Font size (in pixels or points) * @param bool $fontSizeInPixels Is the font size specified in pixels (true) or in points (false) ? * @param string $columnText Text to calculate width * @param int $rotation Rotation angle * @return int Column width */ public static function calculateColumnWidth($fontSize = 9, $fontSizeInPixels = false, $columnText = '', $rotation = 0) { if (!$fontSizeInPixels) { // Translate points size to pixel size $fontSize = PHPExcel_Shared_Font::fontSizeToPixels($fontSize); } // If it is rich text, use rich text... if ($columnText instanceof PHPExcel_RichText) { $columnText = $columnText->getPlainText(); } // Only measure the part before the first newline character if (strpos($columnText, "\r") !== false) { $columnText = substr($columnText, 0, strpos($columnText, "\r")); } if (strpos($columnText, "\n") !== false) { $columnText = substr($columnText, 0, strpos($columnText, "\n")); } // Calculate column width $columnWidth = (strlen($columnText) * $fontSize + 5) / $fontSize * 256 / 256; // Calculate approximate rotated column width if ($rotation !== 0) { if ($rotation == -165) { // stacked text $columnWidth = 4; // approximation } else { // rotated text $columnWidth = $columnWidth * cos(deg2rad($rotation)) + $fontSize * abs(sin(deg2rad($rotation))) / 5; // approximation } } // Return return round($columnWidth, 6); }
/** * Calculate an (approximate) OpenXML column width, based on font size and text contained * * @param int $fontSize Font size (in pixels or points) * @param bool $fontSizeInPixels Is the font size specified in pixels (true) or in points (false) ? * @param string $columnText Text to calculate width * @return int Column width */ public static function calculateColumnWidth($fontSize = 9, $fontSizeInPixels = false, $columnText = '') { if (!$fontSizeInPixels) { // Translate points size to pixel size $fontSize = PHPExcel_Shared_Font::fontSizeToPixels($fontSize); } // If it is rich text, use rich text... if ($columnText instanceof PHPExcel_RichText) { $columnText = $columnText->getPlainText(); } // Calculate column width return round((strlen($columnText) * $fontSize + 5) / $fontSize * 256 / 256, 6); }
/** * Calculate an (approximate) OpenXML column width, based on font size and text contained * * @param int $fontSize Font size (in pixels or points) * @param bool $fontSizeInPixels Is the font size specified in pixels (true) or in points (false) ? * @param string $columnText Text to calculate width * @return int Column width */ public static function calculateColumnWidth($fontSize = 9, $fontSizeInPixels = false, $columnText = '') { if (!$fontSizeInPixels) { // Translate points size to pixel size $fontSize = PHPExcel_Shared_Font::fontSizeToPixels($fontSize); } // If it is rich text, use rich text... if ($columnText instanceof PHPExcel_RichText) { $columnText = $columnText->getPlainText(); } // Only measure the part before the first newline character if (strpos($columnText, "\r") !== false) { $columnText = substr($columnText, 0, strpos($columnText, "\r")); } if (strpos($columnText, "\n") !== false) { $columnText = substr($columnText, 0, strpos($columnText, "\n")); } // Calculate column width return round((strlen($columnText) * $fontSize + 5) / $fontSize * 256 / 256, 6); }
/** * Calculate an (approximate) OpenXML column width, based on font size and text contained * * @param int $fontSize Font size (in pixels or points) * @param bool $fontSizeInPixels Is the font size specified in pixels (true) or in points (false) ? * @param string $columnText Text to calculate width * @param int $rotation Rotation angle * @return int Column width */ public static function calculateColumnWidth($fontSize = 9, $fontSizeInPixels = false, $columnText = '', $rotation = 0) { if (!$fontSizeInPixels) { // Translate points size to pixel size $fontSize = PHPExcel_Shared_Font::fontSizeToPixels($fontSize); } // If it is rich text, use rich text... if ($columnText instanceof PHPExcel_RichText) { $columnText = $columnText->getPlainText(); } // Only measure the part before the first newline character if (strpos($columnText, "\r") !== false) { $columnText = substr($columnText, 0, strpos($columnText, "\r")); } if (strpos($columnText, "\n") !== false) { $columnText = substr($columnText, 0, strpos($columnText, "\n")); } // Calculate column width // values 1.025 and 0.584 found via interpolation by inspecting real Excel files with // Calibri font. May need further adjustment $columnWidth = 1.025 * strlen($columnText) + 0.584; // Excel adds some padding // Calculate approximate rotated column width if ($rotation !== 0) { if ($rotation == -165) { // stacked text $columnWidth = 4; // approximation } else { // rotated text $columnWidth = $columnWidth * cos(deg2rad($rotation)) + $fontSize * abs(sin(deg2rad($rotation))) / 5; // approximation } } // Return return round($columnWidth, 6); }
private static function toCSSArray($style) { $style = str_replace("\r", "", $style); $style = str_replace("\n", "", $style); $temp = explode(';', $style); $style = array(); foreach ($temp as $item) { $item = explode(':', $item); if (strpos($item[1], 'px') !== false) { $item[1] = str_replace('px', '', $item[1]); } if (strpos($item[1], 'pt') !== false) { $item[1] = str_replace('pt', '', $item[1]); $item[1] = PHPExcel_Shared_Font::fontSizeToPixels($item[1]); } if (strpos($item[1], 'in') !== false) { $item[1] = str_replace('in', '', $item[1]); $item[1] = PHPExcel_Shared_Font::inchSizeToPixels($item[1]); } if (strpos($item[1], 'cm') !== false) { $item[1] = str_replace('cm', '', $item[1]); $item[1] = PHPExcel_Shared_Font::centimeterSizeToPixels($item[1]); } $style[$item[0]] = $item[1]; } return $style; }
/** * Convert the height of a cell from user's units to pixels. By interpolation * the relationship is: y = 4/3x. If the height hasn't been set by the user we * use the default value. If the row is hidden we use a value of zero. * * @param PHPExcel_Worksheet $sheet The sheet * @param integer $row The row index (1-based) * @return integer The width in pixels */ public static function sizeRow($sheet, $row = 1) { // default font of the workbook $font = $sheet->getParent()->getDefaultStyle()->getFont(); $rowDimensions = $sheet->getRowDimensions(); // first find the true row height in pixels (uncollapsed and unhidden) if (isset($rowDimensions[$row]) and $rowDimensions[$row]->getRowHeight() != -1) { // then we have a row dimension $rowDimension = $rowDimensions[$row]; $rowHeight = $rowDimension->getRowHeight(); $pixelRowHeight = (int) ceil(4 * $rowHeight / 3); // here we assume Arial 10 } else { if ($sheet->getDefaultRowDimension()->getRowHeight() != -1) { // then we have a default row dimension with explicit height $defaultRowDimension = $sheet->getDefaultRowDimension(); $rowHeight = $defaultRowDimension->getRowHeight(); $pixelRowHeight = PHPExcel_Shared_Drawing::pointsToPixels($rowHeight); } else { // we don't even have any default row dimension. Height depends on default font $pointRowHeight = PHPExcel_Shared_Font::getDefaultRowHeightByFont($font); $pixelRowHeight = PHPExcel_Shared_Font::fontSizeToPixels($pointRowHeight); } } // now find the effective row height in pixels if (isset($rowDimensions[$row]) and !$rowDimensions[$row]->getVisible()) { $effectivePixelRowHeight = 0; } else { $effectivePixelRowHeight = $pixelRowHeight; } return $effectivePixelRowHeight; }