PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Helper / Dimension.php

Dimension.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Helper/Dimension.php

106 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TablePress\PhpOffice\PhpSpreadsheet\Helper;
4
5 use TablePress\PhpOffice\PhpSpreadsheet\Exception;
6 use TablePress\PhpOffice\PhpSpreadsheet\Shared\Drawing;
7 use TablePress\PhpOffice\PhpSpreadsheet\Style\Font;
8
9 class Dimension
10 {
11 public const UOM_CENTIMETERS = 'cm';
12 public const UOM_MILLIMETERS = 'mm';
13 public const UOM_INCHES = 'in';
14 public const UOM_PIXELS = 'px';
15 public const UOM_POINTS = 'pt';
16 public const UOM_PICA = 'pc';
17
18 /**
19 * Based on 96 dpi.
20 */
21 const ABSOLUTE_UNITS = [
22 self::UOM_CENTIMETERS => 96.0 / 2.54,
23 self::UOM_MILLIMETERS => 96.0 / 25.4,
24 self::UOM_INCHES => 96.0,
25 self::UOM_PIXELS => 1.0,
26 self::UOM_POINTS => 96.0 / 72,
27 self::UOM_PICA => 96.0 * 12 / 72,
28 ];
29
30 /**
31 * Based on a standard column width of 8.54 units in MS Excel.
32 */
33 const RELATIVE_UNITS = [
34 'em' => 10.0 / 8.54,
35 'ex' => 10.0 / 8.54,
36 'ch' => 10.0 / 8.54,
37 'rem' => 10.0 / 8.54,
38 'vw' => 8.54,
39 'vh' => 8.54,
40 'vmin' => 8.54,
41 'vmax' => 8.54,
42 '%' => 8.54 / 100,
43 ];
44
45 /**
46 * @var float|int If this is a width, then size is measured in pixels (if is set)
47 * or in Excel's default column width units if $unit is null.
48 * If this is a height, then size is measured in pixels ()
49 * or in points () if $unit is null.
50 */
51 protected $size;
52
53 protected ?string $unit = null;
54
55 public function __construct(string $dimension)
56 {
57 $size = 0.0;
58 $unit = '';
59 $sscanf = sscanf($dimension, '%[1234567890.]%s');
60 if (is_array($sscanf)) {
61 $size = (float) ($sscanf[0] ?? 0.0);
62 $unit = strtolower($sscanf[1] ?? '');
63 }
64
65 // If a UoM is specified, then convert the size to pixels for internal storage
66 if (isset(self::ABSOLUTE_UNITS[$unit])) {
67 $size *= self::ABSOLUTE_UNITS[$unit];
68 $this->unit = self::UOM_PIXELS;
69 } elseif (isset(self::RELATIVE_UNITS[$unit])) {
70 $size *= self::RELATIVE_UNITS[$unit];
71 $size = round($size, 4);
72 }
73
74 $this->size = $size;
75 }
76
77 public function width(): float
78 {
79 return (float) ($this->unit === null)
80 ? $this->size
81 : round(Drawing::pixelsToCellDimension((int) $this->size, new Font(false)), 4);
82 }
83
84 public function height(): float
85 {
86 return (float) ($this->unit === null)
87 ? $this->size
88 : $this->toUnit(self::UOM_POINTS);
89 }
90
91 public function toUnit(string $unitOfMeasure): float
92 {
93 $unitOfMeasure = strtolower($unitOfMeasure);
94 if (!array_key_exists($unitOfMeasure, self::ABSOLUTE_UNITS)) {
95 throw new Exception("{$unitOfMeasure} is not a vaid unit of measure");
96 }
97
98 $size = $this->size;
99 if ($this->unit === null) {
100 $size = Drawing::cellDimensionToPixels($size, new Font(false));
101 }
102
103 return $size / self::ABSOLUTE_UNITS[$unitOfMeasure];
104 }
105 }
106