Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Helper;
4
 
5
use PhpOffice\PhpSpreadsheet\Exception;
6
use PhpOffice\PhpSpreadsheet\Shared\Drawing;
7
use 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 float|int $size;
52
 
53
    protected ?string $unit = null;
54
 
55
    /**
56
     * Phpstan bug has been fixed; this function allows us to
57
     * pass Phpstan whether fixed or not.
58
     */
59
    private static function stanBugFixed(array|int|null $value): array
60
    {
61
        return is_array($value) ? $value : [null, null];
62
    }
63
 
64
    public function __construct(string $dimension)
65
    {
66
        [$size, $unit] = self::stanBugFixed(sscanf($dimension, '%[1234567890.]%s'));
67
        $unit = strtolower(trim($unit ?? ''));
68
        $size = (float) $size;
69
 
70
        // If a UoM is specified, then convert the size to pixels for internal storage
71
        if (isset(self::ABSOLUTE_UNITS[$unit])) {
72
            $size *= self::ABSOLUTE_UNITS[$unit];
73
            $this->unit = self::UOM_PIXELS;
74
        } elseif (isset(self::RELATIVE_UNITS[$unit])) {
75
            $size *= self::RELATIVE_UNITS[$unit];
76
            $size = round($size, 4);
77
        }
78
 
79
        $this->size = $size;
80
    }
81
 
82
    public function width(): float
83
    {
84
        return (float) ($this->unit === null)
85
            ? $this->size
86
            : round(Drawing::pixelsToCellDimension((int) $this->size, new Font(false)), 4);
87
    }
88
 
89
    public function height(): float
90
    {
91
        return (float) ($this->unit === null)
92
            ? $this->size
93
            : $this->toUnit(self::UOM_POINTS);
94
    }
95
 
96
    public function toUnit(string $unitOfMeasure): float
97
    {
98
        $unitOfMeasure = strtolower($unitOfMeasure);
99
        if (!array_key_exists($unitOfMeasure, self::ABSOLUTE_UNITS)) {
100
            throw new Exception("{$unitOfMeasure} is not a vaid unit of measure");
101
        }
102
 
103
        $size = $this->size;
104
        if ($this->unit === null) {
105
            $size = Drawing::cellDimensionToPixels($size, new Font(false));
106
        }
107
 
108
        return $size / self::ABSOLUTE_UNITS[$unitOfMeasure];
109
    }
110
}