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\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): int|float
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, \PhpOffice\PhpSpreadsheet\Style\Font $defaultFont): int|float
48
    {
49
        // Font name and size
50
        $name = $defaultFont->getName();
51
        $size = $defaultFont->getSize();
52
 
53
        if (isset(Font::DEFAULT_COLUMN_WIDTHS[$name][$size])) {
54
            // Exact width can be determined
55
            return $pixelValue * Font::DEFAULT_COLUMN_WIDTHS[$name][$size]['width']
56
                / Font::DEFAULT_COLUMN_WIDTHS[$name][$size]['px'];
57
        }
58
 
59
        // We don't have data for this particular font and size, use approximation by
60
        // extrapolating from Calibri 11
61
        return $pixelValue * 11 * Font::DEFAULT_COLUMN_WIDTHS['Calibri'][11]['width']
62
            / Font::DEFAULT_COLUMN_WIDTHS['Calibri'][11]['px'] / $size;
63
    }
64
 
65
    /**
66
     * Convert column width from (intrinsic) Excel units to pixels.
67
     *
68
     * @param float $cellWidth Value in cell dimension
69
     * @param \PhpOffice\PhpSpreadsheet\Style\Font $defaultFont Default font of the workbook
70
     *
71
     * @return int Value in pixels
72
     */
73
    public static function cellDimensionToPixels(float $cellWidth, \PhpOffice\PhpSpreadsheet\Style\Font $defaultFont): int
74
    {
75
        // Font name and size
76
        $name = $defaultFont->getName();
77
        $size = $defaultFont->getSize();
78
 
79
        if (isset(Font::DEFAULT_COLUMN_WIDTHS[$name][$size])) {
80
            // Exact width can be determined
81
            $colWidth = $cellWidth * Font::DEFAULT_COLUMN_WIDTHS[$name][$size]['px']
82
                / Font::DEFAULT_COLUMN_WIDTHS[$name][$size]['width'];
83
        } else {
84
            // We don't have data for this particular font and size, use approximation by
85
            // extrapolating from Calibri 11
86
            $colWidth = $cellWidth * $size * Font::DEFAULT_COLUMN_WIDTHS['Calibri'][11]['px']
87
                / Font::DEFAULT_COLUMN_WIDTHS['Calibri'][11]['width'] / 11;
88
        }
89
 
90
        // Round pixels to closest integer
91
        $colWidth = (int) round($colWidth);
92
 
93
        return $colWidth;
94
    }
95
 
96
    /**
97
     * Convert pixels to points.
98
     *
99
     * @param int $pixelValue Value in pixels
100
     *
101
     * @return float Value in points
102
     */
103
    public static function pixelsToPoints(int $pixelValue): float
104
    {
105
        return $pixelValue * 0.75;
106
    }
107
 
108
    /**
109
     * Convert points to pixels.
110
     *
111
     * @param float|int $pointValue Value in points
112
     *
113
     * @return int Value in pixels
114
     */
115
    public static function pointsToPixels($pointValue): int
116
    {
117
        if ($pointValue != 0) {
118
            return (int) ceil($pointValue / 0.75);
119
        }
120
 
121
        return 0;
122
    }
123
 
124
    /**
125
     * Convert degrees to angle.
126
     *
127
     * @param int $degrees Degrees
128
     *
129
     * @return int Angle
130
     */
131
    public static function degreesToAngle(int $degrees): int
132
    {
133
        return (int) round($degrees * 60000);
134
    }
135
 
136
    /**
137
     * Convert angle to degrees.
138
     *
139
     * @param int|SimpleXMLElement $angle Angle
140
     *
141
     * @return int Degrees
142
     */
143
    public static function angleToDegrees($angle): int
144
    {
145
        $angle = (int) $angle;
146
        if ($angle != 0) {
147
            return (int) round($angle / 60000);
148
        }
149
 
150
        return 0;
151
    }
152
}