| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Shared; |
| 4 |
|
| 5 |
use SimpleXMLElement; |
| 6 |
|
| 7 |
class Drawing |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Convert pixels to EMU. |
| 11 |
* |
| 12 |
* @param int $pixelValue Value in pixels |
| 13 |
* |
| 14 |
* @return float|int Value in EMU |
| 15 |
*/ |
| 16 |
public static function pixelsToEMU(int $pixelValue) |
| 17 |
{ |
| 18 |
return $pixelValue * 9525; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Convert EMU to pixels. |
| 23 |
* |
| 24 |
* @param int|SimpleXMLElement $emuValue Value in EMU |
| 25 |
* |
| 26 |
* @return int Value in pixels |
| 27 |
*/ |
| 28 |
public static function EMUToPixels($emuValue): int |
| 29 |
{ |
| 30 |
$emuValue = (int) $emuValue; |
| 31 |
if ($emuValue != 0) { |
| 32 |
return (int) round($emuValue / 9525); |
| 33 |
} |
| 34 |
|
| 35 |
return 0; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Convert pixels to column width. Exact algorithm not known. |
| 40 |
* By inspection of a real Excel file using Calibri 11, one finds 1000px ~ 142.85546875 |
| 41 |
* This gives a conversion factor of 7. Also, we assume that pixels and font size are proportional. |
| 42 |
* |
| 43 |
* @param int $pixelValue Value in pixels |
| 44 |
* |
| 45 |
* @return float|int Value in cell dimension |
| 46 |
*/ |
| 47 |
public static function pixelsToCellDimension(int $pixelValue, \TablePress\PhpOffice\PhpSpreadsheet\Style\Font $defaultFont) |
| 48 |
{ |
| 49 |
// Font name and size |
| 50 |
$name = $defaultFont->getName(); |
| 51 |
$size = $defaultFont->getSize(); |
| 52 |
$sizex = ($size !== null && $size == (int) $size) ? ((int) $size) : "$size"; |
| 53 |
|
| 54 |
if (isset(Font::DEFAULT_COLUMN_WIDTHS[$name][$sizex])) { |
| 55 |
// Exact width can be determined |
| 56 |
return $pixelValue * Font::DEFAULT_COLUMN_WIDTHS[$name][$sizex]['width'] |
| 57 |
/ Font::DEFAULT_COLUMN_WIDTHS[$name][$sizex]['px']; |
| 58 |
} |
| 59 |
|
| 60 |
// We don't have data for this particular font and size, use approximation by |
| 61 |
// extrapolating from Calibri 11 |
| 62 |
return $pixelValue * 11 * Font::DEFAULT_COLUMN_WIDTHS['Calibri'][11]['width'] |
| 63 |
/ Font::DEFAULT_COLUMN_WIDTHS['Calibri'][11]['px'] / $size; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Convert column width from (intrinsic) Excel units to pixels. |
| 68 |
* |
| 69 |
* @param float $cellWidth Value in cell dimension |
| 70 |
* @param \TablePress\PhpOffice\PhpSpreadsheet\Style\Font $defaultFont Default font of the workbook |
| 71 |
* |
| 72 |
* @return int Value in pixels |
| 73 |
*/ |
| 74 |
public static function cellDimensionToPixels(float $cellWidth, \TablePress\PhpOffice\PhpSpreadsheet\Style\Font $defaultFont): int |
| 75 |
{ |
| 76 |
// Font name and size |
| 77 |
$name = $defaultFont->getName(); |
| 78 |
$size = $defaultFont->getSize(); |
| 79 |
$sizex = ($size !== null && $size == (int) $size) ? ((int) $size) : "$size"; |
| 80 |
|
| 81 |
if (isset(Font::DEFAULT_COLUMN_WIDTHS[$name][$sizex])) { |
| 82 |
// Exact width can be determined |
| 83 |
$colWidth = $cellWidth * Font::DEFAULT_COLUMN_WIDTHS[$name][$sizex]['px'] |
| 84 |
/ Font::DEFAULT_COLUMN_WIDTHS[$name][$sizex]['width']; |
| 85 |
} else { |
| 86 |
// We don't have data for this particular font and size, use approximation by |
| 87 |
// extrapolating from Calibri 11 |
| 88 |
$colWidth = $cellWidth * $size * Font::DEFAULT_COLUMN_WIDTHS['Calibri'][11]['px'] |
| 89 |
/ Font::DEFAULT_COLUMN_WIDTHS['Calibri'][11]['width'] / 11; |
| 90 |
} |
| 91 |
|
| 92 |
// Round pixels to closest integer |
| 93 |
$colWidth = (int) round($colWidth); |
| 94 |
|
| 95 |
return $colWidth; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Convert pixels to points. |
| 100 |
* |
| 101 |
* @param int $pixelValue Value in pixels |
| 102 |
* |
| 103 |
* @return float Value in points |
| 104 |
*/ |
| 105 |
public static function pixelsToPoints(int $pixelValue): float |
| 106 |
{ |
| 107 |
return $pixelValue * 0.75; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Convert points to pixels. |
| 112 |
* |
| 113 |
* @param float|int $pointValue Value in points |
| 114 |
* |
| 115 |
* @return int Value in pixels |
| 116 |
*/ |
| 117 |
public static function pointsToPixels($pointValue): int |
| 118 |
{ |
| 119 |
return (int) ceil($pointValue / 0.75); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Convert degrees to angle. |
| 124 |
* |
| 125 |
* @param int $degrees Degrees |
| 126 |
* |
| 127 |
* @return int Angle |
| 128 |
*/ |
| 129 |
public static function degreesToAngle(int $degrees): int |
| 130 |
{ |
| 131 |
return (int) round($degrees * 60000); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Convert angle to degrees. |
| 136 |
* |
| 137 |
* @param int|SimpleXMLElement $angle Angle |
| 138 |
* |
| 139 |
* @return int Degrees |
| 140 |
*/ |
| 141 |
public static function angleToDegrees($angle): int |
| 142 |
{ |
| 143 |
$angle = (int) $angle; |
| 144 |
if ($angle != 0) { |
| 145 |
return (int) round($angle / 60000); |
| 146 |
} |
| 147 |
|
| 148 |
return 0; |
| 149 |
} |
| 150 |
} |
| 151 |
|