PluginProbe
wpDataTables – WordPress Data Table, Dynamic Tables & Table Charts Plugin / 6.5.1.7
wpDataTables – WordPress Data Table, Dynamic Tables & Table Charts Plugin v6.5.1.7
6.5.1.7 6.5.1.6 6.5.1.5 6.5.1.4 6.5.1.3 6.5.1.2 6.5.1.1 6.5.0.9 6.5.0.8 6.5.0.7 6.5.0.6 trunk 3.4.2.40 3.4.2.41 3.4.2.42 3.4.2.43 3.4.2.44 3.4.2.45 3.4.2.46 3.4.2.47 3.4.2.48 3.4.2.49 3.4.2.50 6.3.2 6.3.3.1 All 47 releases
wpdatatables / lib / phpoffice / phpspreadsheet / src / PhpSpreadsheet / Shared / Xls.php

Xls.php in wpDataTables – WordPress Data Table, Dynamic Tables & Table Charts Plugin 6.5.1.7, at lib/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Shared/Xls.php

278 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace PhpOffice\PhpSpreadsheet\Shared;
4
5 use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
6 use PhpOffice\PhpSpreadsheet\Helper\Dimension;
7 use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
8
9 class Xls
10 {
11 /**
12 * Get the width of a column in pixels. We use the relationship y = ceil(7x) where
13 * x is the width in intrinsic Excel units (measuring width in number of normal characters)
14 * This holds for Arial 10.
15 *
16 * @param Worksheet $worksheet The sheet
17 * @param string $col The column
18 *
19 * @return int The width in pixels
20 */
21 public static function sizeCol(Worksheet $worksheet, $col = 'A')
22 {
23 // default font of the workbook
24 $font = $worksheet->getParentOrThrow()->getDefaultStyle()->getFont();
25
26 $columnDimensions = $worksheet->getColumnDimensions();
27
28 // first find the true column width in pixels (uncollapsed and unhidden)
29 if (isset($columnDimensions[$col]) && $columnDimensions[$col]->getWidth() != -1) {
30 // then we have column dimension with explicit width
31 $columnDimension = $columnDimensions[$col];
32 $width = $columnDimension->getWidth();
33 $pixelWidth = Drawing::cellDimensionToPixels($width, $font);
34 } elseif ($worksheet->getDefaultColumnDimension()->getWidth() != -1) {
35 // then we have default column dimension with explicit width
36 $defaultColumnDimension = $worksheet->getDefaultColumnDimension();
37 $width = $defaultColumnDimension->getWidth();
38 $pixelWidth = Drawing::cellDimensionToPixels($width, $font);
39 } else {
40 // we don't even have any default column dimension. Width depends on default font
41 $pixelWidth = Font::getDefaultColumnWidthByFont($font, true);
42 }
43
44 // now find the effective column width in pixels
45 if (isset($columnDimensions[$col]) && !$columnDimensions[$col]->getVisible()) {
46 $effectivePixelWidth = 0;
47 } else {
48 $effectivePixelWidth = $pixelWidth;
49 }
50
51 return $effectivePixelWidth;
52 }
53
54 /**
55 * Convert the height of a cell from user's units to pixels. By interpolation
56 * the relationship is: y = 4/3x. If the height hasn't been set by the user we
57 * use the default value. If the row is hidden we use a value of zero.
58 *
59 * @param Worksheet $worksheet The sheet
60 * @param int $row The row index (1-based)
61 *
62 * @return int The width in pixels
63 */
64 public static function sizeRow(Worksheet $worksheet, $row = 1)
65 {
66 // default font of the workbook
67 $font = $worksheet->getParentOrThrow()->getDefaultStyle()->getFont();
68
69 $rowDimensions = $worksheet->getRowDimensions();
70
71 // first find the true row height in pixels (uncollapsed and unhidden)
72 if (isset($rowDimensions[$row]) && $rowDimensions[$row]->getRowHeight() != -1) {
73 // then we have a row dimension
74 $rowDimension = $rowDimensions[$row];
75 $rowHeight = $rowDimension->getRowHeight();
76 $pixelRowHeight = (int) ceil(4 * $rowHeight / 3); // here we assume Arial 10
77 } elseif ($worksheet->getDefaultRowDimension()->getRowHeight() != -1) {
78 // then we have a default row dimension with explicit height
79 $defaultRowDimension = $worksheet->getDefaultRowDimension();
80 $pixelRowHeight = $defaultRowDimension->getRowHeight(Dimension::UOM_PIXELS);
81 } else {
82 // we don't even have any default row dimension. Height depends on default font
83 $pointRowHeight = Font::getDefaultRowHeightByFont($font);
84 $pixelRowHeight = Font::fontSizeToPixels((int) $pointRowHeight);
85 }
86
87 // now find the effective row height in pixels
88 if (isset($rowDimensions[$row]) && !$rowDimensions[$row]->getVisible()) {
89 $effectivePixelRowHeight = 0;
90 } else {
91 $effectivePixelRowHeight = $pixelRowHeight;
92 }
93
94 return (int) $effectivePixelRowHeight;
95 }
96
97 /**
98 * Get the horizontal distance in pixels between two anchors
99 * The distanceX is found as sum of all the spanning columns widths minus correction for the two offsets.
100 *
101 * @param string $startColumn
102 * @param int $startOffsetX Offset within start cell measured in 1/1024 of the cell width
103 * @param string $endColumn
104 * @param int $endOffsetX Offset within end cell measured in 1/1024 of the cell width
105 *
106 * @return int Horizontal measured in pixels
107 */
108 public static function getDistanceX(Worksheet $worksheet, $startColumn = 'A', $startOffsetX = 0, $endColumn = 'A', $endOffsetX = 0)
109 {
110 $distanceX = 0;
111
112 // add the widths of the spanning columns
113 $startColumnIndex = Coordinate::columnIndexFromString($startColumn);
114 $endColumnIndex = Coordinate::columnIndexFromString($endColumn);
115 for ($i = $startColumnIndex; $i <= $endColumnIndex; ++$i) {
116 $distanceX += self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($i));
117 }
118
119 // correct for offsetX in startcell
120 $distanceX -= (int) floor(self::sizeCol($worksheet, $startColumn) * $startOffsetX / 1024);
121
122 // correct for offsetX in endcell
123 $distanceX -= (int) floor(self::sizeCol($worksheet, $endColumn) * (1 - $endOffsetX / 1024));
124
125 return $distanceX;
126 }
127
128 /**
129 * Get the vertical distance in pixels between two anchors
130 * The distanceY is found as sum of all the spanning rows minus two offsets.
131 *
132 * @param int $startRow (1-based)
133 * @param int $startOffsetY Offset within start cell measured in 1/256 of the cell height
134 * @param int $endRow (1-based)
135 * @param int $endOffsetY Offset within end cell measured in 1/256 of the cell height
136 *
137 * @return int Vertical distance measured in pixels
138 */
139 public static function getDistanceY(Worksheet $worksheet, $startRow = 1, $startOffsetY = 0, $endRow = 1, $endOffsetY = 0)
140 {
141 $distanceY = 0;
142
143 // add the widths of the spanning rows
144 for ($row = $startRow; $row <= $endRow; ++$row) {
145 $distanceY += self::sizeRow($worksheet, $row);
146 }
147
148 // correct for offsetX in startcell
149 $distanceY -= (int) floor(self::sizeRow($worksheet, $startRow) * $startOffsetY / 256);
150
151 // correct for offsetX in endcell
152 $distanceY -= (int) floor(self::sizeRow($worksheet, $endRow) * (1 - $endOffsetY / 256));
153
154 return $distanceY;
155 }
156
157 /**
158 * Convert 1-cell anchor coordinates to 2-cell anchor coordinates
159 * This function is ported from PEAR Spreadsheet_Writer_Excel with small modifications.
160 *
161 * Calculate the vertices that define the position of the image as required by
162 * the OBJ record.
163 *
164 * +------------+------------+
165 * | A | B |
166 * +-----+------------+------------+
167 * | |(x1,y1) | |
168 * | 1 |(A1)._______|______ |
169 * | | | | |
170 * | | | | |
171 * +-----+----| BITMAP |-----+
172 * | | | | |
173 * | 2 | |______________. |
174 * | | | (B2)|
175 * | | | (x2,y2)|
176 * +---- +------------+------------+
177 *
178 * Example of a bitmap that covers some of the area from cell A1 to cell B2.
179 *
180 * Based on the width and height of the bitmap we need to calculate 8 vars:
181 * $col_start, $row_start, $col_end, $row_end, $x1, $y1, $x2, $y2.
182 * The width and height of the cells are also variable and have to be taken into
183 * account.
184 * The values of $col_start and $row_start are passed in from the calling
185 * function. The values of $col_end and $row_end are calculated by subtracting
186 * the width and height of the bitmap from the width and height of the
187 * underlying cells.
188 * The vertices are expressed as a percentage of the underlying cell width as
189 * follows (rhs values are in pixels):
190 *
191 * x1 = X / W *1024
192 * y1 = Y / H *256
193 * x2 = (X-1) / W *1024
194 * y2 = (Y-1) / H *256
195 *
196 * Where: X is distance from the left side of the underlying cell
197 * Y is distance from the top of the underlying cell
198 * W is the width of the cell
199 * H is the height of the cell
200 *
201 * @param string $coordinates E.g. 'A1'
202 * @param int $offsetX Horizontal offset in pixels
203 * @param int $offsetY Vertical offset in pixels
204 * @param int $width Width in pixels
205 * @param int $height Height in pixels
206 *
207 * @return null|array
208 */
209 public static function oneAnchor2twoAnchor(Worksheet $worksheet, $coordinates, $offsetX, $offsetY, $width, $height)
210 {
211 [$col_start, $row] = Coordinate::indexesFromString($coordinates);
212 $row_start = $row - 1;
213
214 $x1 = $offsetX;
215 $y1 = $offsetY;
216
217 // Initialise end cell to the same as the start cell
218 $col_end = $col_start; // Col containing lower right corner of object
219 $row_end = $row_start; // Row containing bottom right corner of object
220
221 // Zero the specified offset if greater than the cell dimensions
222 if ($x1 >= self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($col_start))) {
223 $x1 = 0;
224 }
225 if ($y1 >= self::sizeRow($worksheet, $row_start + 1)) {
226 $y1 = 0;
227 }
228
229 $width = $width + $x1 - 1;
230 $height = $height + $y1 - 1;
231
232 // Subtract the underlying cell widths to find the end cell of the image
233 while ($width >= self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($col_end))) {
234 $width -= self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($col_end));
235 ++$col_end;
236 }
237
238 // Subtract the underlying cell heights to find the end cell of the image
239 while ($height >= self::sizeRow($worksheet, $row_end + 1)) {
240 $height -= self::sizeRow($worksheet, $row_end + 1);
241 ++$row_end;
242 }
243
244 // Bitmap isn't allowed to start or finish in a hidden cell, i.e. a cell
245 // with zero height or width.
246 if (self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($col_start)) == 0) {
247 return null;
248 }
249 if (self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($col_end)) == 0) {
250 return null;
251 }
252 if (self::sizeRow($worksheet, $row_start + 1) == 0) {
253 return null;
254 }
255 if (self::sizeRow($worksheet, $row_end + 1) == 0) {
256 return null;
257 }
258
259 // Convert the pixel values to the percentage value expected by Excel
260 $x1 = $x1 / self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($col_start)) * 1024;
261 $y1 = $y1 / self::sizeRow($worksheet, $row_start + 1) * 256;
262 $x2 = ($width + 1) / self::sizeCol($worksheet, Coordinate::stringFromColumnIndex($col_end)) * 1024; // Distance to right side of object
263 $y2 = ($height + 1) / self::sizeRow($worksheet, $row_end + 1) * 256; // Distance to bottom of object
264
265 $startCoordinates = Coordinate::stringFromColumnIndex($col_start) . ($row_start + 1);
266 $endCoordinates = Coordinate::stringFromColumnIndex($col_end) . ($row_end + 1);
267
268 return [
269 'startCoordinates' => $startCoordinates,
270 'startOffsetX' => $x1,
271 'startOffsetY' => $y1,
272 'endCoordinates' => $endCoordinates,
273 'endOffsetX' => $x2,
274 'endOffsetY' => $y2,
275 ];
276 }
277 }
278