@@ -21,250 +21,250 @@ |
||
21 | 21 | */ |
22 | 22 | abstract class Constraint implements Countable, SelfDescribing |
23 | 23 | { |
24 | - /** |
|
25 | - * @var ?Exporter |
|
26 | - */ |
|
27 | - private $exporter; |
|
24 | + /** |
|
25 | + * @var ?Exporter |
|
26 | + */ |
|
27 | + private $exporter; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Evaluates the constraint for parameter $other. |
|
31 | - * |
|
32 | - * If $returnResult is set to false (the default), an exception is thrown |
|
33 | - * in case of a failure. null is returned otherwise. |
|
34 | - * |
|
35 | - * If $returnResult is true, the result of the evaluation is returned as |
|
36 | - * a boolean value instead: true in case of success, false in case of a |
|
37 | - * failure. |
|
38 | - * |
|
39 | - * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
|
40 | - * @throws ExpectationFailedException |
|
41 | - */ |
|
42 | - public function evaluate($other, string $description = '', bool $returnResult = false): ?bool |
|
43 | - { |
|
44 | - $success = false; |
|
29 | + /** |
|
30 | + * Evaluates the constraint for parameter $other. |
|
31 | + * |
|
32 | + * If $returnResult is set to false (the default), an exception is thrown |
|
33 | + * in case of a failure. null is returned otherwise. |
|
34 | + * |
|
35 | + * If $returnResult is true, the result of the evaluation is returned as |
|
36 | + * a boolean value instead: true in case of success, false in case of a |
|
37 | + * failure. |
|
38 | + * |
|
39 | + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
|
40 | + * @throws ExpectationFailedException |
|
41 | + */ |
|
42 | + public function evaluate($other, string $description = '', bool $returnResult = false): ?bool |
|
43 | + { |
|
44 | + $success = false; |
|
45 | 45 | |
46 | - if ($this->matches($other)) { |
|
47 | - $success = true; |
|
48 | - } |
|
46 | + if ($this->matches($other)) { |
|
47 | + $success = true; |
|
48 | + } |
|
49 | 49 | |
50 | - if ($returnResult) { |
|
51 | - return $success; |
|
52 | - } |
|
50 | + if ($returnResult) { |
|
51 | + return $success; |
|
52 | + } |
|
53 | 53 | |
54 | - if (!$success) { |
|
55 | - $this->fail($other, $description); |
|
56 | - } |
|
54 | + if (!$success) { |
|
55 | + $this->fail($other, $description); |
|
56 | + } |
|
57 | 57 | |
58 | - return null; |
|
59 | - } |
|
58 | + return null; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Counts the number of constraint elements. |
|
63 | - */ |
|
64 | - public function count(): int |
|
65 | - { |
|
66 | - return 1; |
|
67 | - } |
|
61 | + /** |
|
62 | + * Counts the number of constraint elements. |
|
63 | + */ |
|
64 | + public function count(): int |
|
65 | + { |
|
66 | + return 1; |
|
67 | + } |
|
68 | 68 | |
69 | - protected function exporter(): Exporter |
|
70 | - { |
|
71 | - if ($this->exporter === null) { |
|
72 | - $this->exporter = new Exporter; |
|
73 | - } |
|
69 | + protected function exporter(): Exporter |
|
70 | + { |
|
71 | + if ($this->exporter === null) { |
|
72 | + $this->exporter = new Exporter; |
|
73 | + } |
|
74 | 74 | |
75 | - return $this->exporter; |
|
76 | - } |
|
75 | + return $this->exporter; |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * Evaluates the constraint for parameter $other. Returns true if the |
|
80 | - * constraint is met, false otherwise. |
|
81 | - * |
|
82 | - * This method can be overridden to implement the evaluation algorithm. |
|
83 | - * |
|
84 | - * @param mixed $other value or object to evaluate |
|
85 | - * |
|
86 | - * @codeCoverageIgnore |
|
87 | - */ |
|
88 | - protected function matches($other): bool |
|
89 | - { |
|
90 | - return false; |
|
91 | - } |
|
78 | + /** |
|
79 | + * Evaluates the constraint for parameter $other. Returns true if the |
|
80 | + * constraint is met, false otherwise. |
|
81 | + * |
|
82 | + * This method can be overridden to implement the evaluation algorithm. |
|
83 | + * |
|
84 | + * @param mixed $other value or object to evaluate |
|
85 | + * |
|
86 | + * @codeCoverageIgnore |
|
87 | + */ |
|
88 | + protected function matches($other): bool |
|
89 | + { |
|
90 | + return false; |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Throws an exception for the given compared value and test description. |
|
95 | - * |
|
96 | - * @param mixed $other evaluated value or object |
|
97 | - * @param string $description Additional information about the test |
|
98 | - * @param ComparisonFailure $comparisonFailure |
|
99 | - * |
|
100 | - * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
|
101 | - * @throws ExpectationFailedException |
|
102 | - * |
|
103 | - * @psalm-return never-return |
|
104 | - */ |
|
105 | - protected function fail($other, $description, ComparisonFailure $comparisonFailure = null): void |
|
106 | - { |
|
107 | - $failureDescription = sprintf( |
|
108 | - 'Failed asserting that %s.', |
|
109 | - $this->failureDescription($other) |
|
110 | - ); |
|
93 | + /** |
|
94 | + * Throws an exception for the given compared value and test description. |
|
95 | + * |
|
96 | + * @param mixed $other evaluated value or object |
|
97 | + * @param string $description Additional information about the test |
|
98 | + * @param ComparisonFailure $comparisonFailure |
|
99 | + * |
|
100 | + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
|
101 | + * @throws ExpectationFailedException |
|
102 | + * |
|
103 | + * @psalm-return never-return |
|
104 | + */ |
|
105 | + protected function fail($other, $description, ComparisonFailure $comparisonFailure = null): void |
|
106 | + { |
|
107 | + $failureDescription = sprintf( |
|
108 | + 'Failed asserting that %s.', |
|
109 | + $this->failureDescription($other) |
|
110 | + ); |
|
111 | 111 | |
112 | - $additionalFailureDescription = $this->additionalFailureDescription($other); |
|
112 | + $additionalFailureDescription = $this->additionalFailureDescription($other); |
|
113 | 113 | |
114 | - if ($additionalFailureDescription) { |
|
115 | - $failureDescription .= "\n" . $additionalFailureDescription; |
|
116 | - } |
|
114 | + if ($additionalFailureDescription) { |
|
115 | + $failureDescription .= "\n" . $additionalFailureDescription; |
|
116 | + } |
|
117 | 117 | |
118 | - if (!empty($description)) { |
|
119 | - $failureDescription = $description . "\n" . $failureDescription; |
|
120 | - } |
|
118 | + if (!empty($description)) { |
|
119 | + $failureDescription = $description . "\n" . $failureDescription; |
|
120 | + } |
|
121 | 121 | |
122 | - throw new ExpectationFailedException( |
|
123 | - $failureDescription, |
|
124 | - $comparisonFailure |
|
125 | - ); |
|
126 | - } |
|
122 | + throw new ExpectationFailedException( |
|
123 | + $failureDescription, |
|
124 | + $comparisonFailure |
|
125 | + ); |
|
126 | + } |
|
127 | 127 | |
128 | - /** |
|
129 | - * Return additional failure description where needed. |
|
130 | - * |
|
131 | - * The function can be overridden to provide additional failure |
|
132 | - * information like a diff |
|
133 | - * |
|
134 | - * @param mixed $other evaluated value or object |
|
135 | - */ |
|
136 | - protected function additionalFailureDescription($other): string |
|
137 | - { |
|
138 | - return ''; |
|
139 | - } |
|
128 | + /** |
|
129 | + * Return additional failure description where needed. |
|
130 | + * |
|
131 | + * The function can be overridden to provide additional failure |
|
132 | + * information like a diff |
|
133 | + * |
|
134 | + * @param mixed $other evaluated value or object |
|
135 | + */ |
|
136 | + protected function additionalFailureDescription($other): string |
|
137 | + { |
|
138 | + return ''; |
|
139 | + } |
|
140 | 140 | |
141 | - /** |
|
142 | - * Returns the description of the failure. |
|
143 | - * |
|
144 | - * The beginning of failure messages is "Failed asserting that" in most |
|
145 | - * cases. This method should return the second part of that sentence. |
|
146 | - * |
|
147 | - * To provide additional failure information additionalFailureDescription |
|
148 | - * can be used. |
|
149 | - * |
|
150 | - * @param mixed $other evaluated value or object |
|
151 | - * |
|
152 | - * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
|
153 | - */ |
|
154 | - protected function failureDescription($other): string |
|
155 | - { |
|
156 | - return $this->exporter()->export($other) . ' ' . $this->toString(); |
|
157 | - } |
|
141 | + /** |
|
142 | + * Returns the description of the failure. |
|
143 | + * |
|
144 | + * The beginning of failure messages is "Failed asserting that" in most |
|
145 | + * cases. This method should return the second part of that sentence. |
|
146 | + * |
|
147 | + * To provide additional failure information additionalFailureDescription |
|
148 | + * can be used. |
|
149 | + * |
|
150 | + * @param mixed $other evaluated value or object |
|
151 | + * |
|
152 | + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
|
153 | + */ |
|
154 | + protected function failureDescription($other): string |
|
155 | + { |
|
156 | + return $this->exporter()->export($other) . ' ' . $this->toString(); |
|
157 | + } |
|
158 | 158 | |
159 | - /** |
|
160 | - * Returns a custom string representation of the constraint object when it |
|
161 | - * appears in context of an $operator expression. |
|
162 | - * |
|
163 | - * The purpose of this method is to provide meaningful descriptive string |
|
164 | - * in context of operators such as LogicalNot. Native PHPUnit constraints |
|
165 | - * are supported out of the box by LogicalNot, but externally developed |
|
166 | - * ones had no way to provide correct strings in this context. |
|
167 | - * |
|
168 | - * The method shall return empty string, when it does not handle |
|
169 | - * customization by itself. |
|
170 | - * |
|
171 | - * @param Operator $operator the $operator of the expression |
|
172 | - * @param mixed $role role of $this constraint in the $operator expression |
|
173 | - */ |
|
174 | - protected function toStringInContext(Operator $operator, $role): string |
|
175 | - { |
|
176 | - return ''; |
|
177 | - } |
|
159 | + /** |
|
160 | + * Returns a custom string representation of the constraint object when it |
|
161 | + * appears in context of an $operator expression. |
|
162 | + * |
|
163 | + * The purpose of this method is to provide meaningful descriptive string |
|
164 | + * in context of operators such as LogicalNot. Native PHPUnit constraints |
|
165 | + * are supported out of the box by LogicalNot, but externally developed |
|
166 | + * ones had no way to provide correct strings in this context. |
|
167 | + * |
|
168 | + * The method shall return empty string, when it does not handle |
|
169 | + * customization by itself. |
|
170 | + * |
|
171 | + * @param Operator $operator the $operator of the expression |
|
172 | + * @param mixed $role role of $this constraint in the $operator expression |
|
173 | + */ |
|
174 | + protected function toStringInContext(Operator $operator, $role): string |
|
175 | + { |
|
176 | + return ''; |
|
177 | + } |
|
178 | 178 | |
179 | - /** |
|
180 | - * Returns the description of the failure when this constraint appears in |
|
181 | - * context of an $operator expression. |
|
182 | - * |
|
183 | - * The purpose of this method is to provide meaningful failure description |
|
184 | - * in context of operators such as LogicalNot. Native PHPUnit constraints |
|
185 | - * are supported out of the box by LogicalNot, but externally developed |
|
186 | - * ones had no way to provide correct messages in this context. |
|
187 | - * |
|
188 | - * The method shall return empty string, when it does not handle |
|
189 | - * customization by itself. |
|
190 | - * |
|
191 | - * @param Operator $operator the $operator of the expression |
|
192 | - * @param mixed $role role of $this constraint in the $operator expression |
|
193 | - * @param mixed $other evaluated value or object |
|
194 | - */ |
|
195 | - protected function failureDescriptionInContext(Operator $operator, $role, $other): string |
|
196 | - { |
|
197 | - $string = $this->toStringInContext($operator, $role); |
|
179 | + /** |
|
180 | + * Returns the description of the failure when this constraint appears in |
|
181 | + * context of an $operator expression. |
|
182 | + * |
|
183 | + * The purpose of this method is to provide meaningful failure description |
|
184 | + * in context of operators such as LogicalNot. Native PHPUnit constraints |
|
185 | + * are supported out of the box by LogicalNot, but externally developed |
|
186 | + * ones had no way to provide correct messages in this context. |
|
187 | + * |
|
188 | + * The method shall return empty string, when it does not handle |
|
189 | + * customization by itself. |
|
190 | + * |
|
191 | + * @param Operator $operator the $operator of the expression |
|
192 | + * @param mixed $role role of $this constraint in the $operator expression |
|
193 | + * @param mixed $other evaluated value or object |
|
194 | + */ |
|
195 | + protected function failureDescriptionInContext(Operator $operator, $role, $other): string |
|
196 | + { |
|
197 | + $string = $this->toStringInContext($operator, $role); |
|
198 | 198 | |
199 | - if ($string === '') { |
|
200 | - return ''; |
|
201 | - } |
|
199 | + if ($string === '') { |
|
200 | + return ''; |
|
201 | + } |
|
202 | 202 | |
203 | - return $this->exporter()->export($other) . ' ' . $string; |
|
204 | - } |
|
203 | + return $this->exporter()->export($other) . ' ' . $string; |
|
204 | + } |
|
205 | 205 | |
206 | - /** |
|
207 | - * Reduces the sub-expression starting at $this by skipping degenerate |
|
208 | - * sub-expression and returns first descendant constraint that starts |
|
209 | - * a non-reducible sub-expression. |
|
210 | - * |
|
211 | - * Returns $this for terminal constraints and for operators that start |
|
212 | - * non-reducible sub-expression, or the nearest descendant of $this that |
|
213 | - * starts a non-reducible sub-expression. |
|
214 | - * |
|
215 | - * A constraint expression may be modelled as a tree with non-terminal |
|
216 | - * nodes (operators) and terminal nodes. For example: |
|
217 | - * |
|
218 | - * LogicalOr (operator, non-terminal) |
|
219 | - * + LogicalAnd (operator, non-terminal) |
|
220 | - * | + IsType('int') (terminal) |
|
221 | - * | + GreaterThan(10) (terminal) |
|
222 | - * + LogicalNot (operator, non-terminal) |
|
223 | - * + IsType('array') (terminal) |
|
224 | - * |
|
225 | - * A degenerate sub-expression is a part of the tree, that effectively does |
|
226 | - * not contribute to the evaluation of the expression it appears in. An example |
|
227 | - * of degenerate sub-expression is a BinaryOperator constructed with single |
|
228 | - * operand or nested BinaryOperators, each with single operand. An |
|
229 | - * expression involving a degenerate sub-expression is equivalent to a |
|
230 | - * reduced expression with the degenerate sub-expression removed, for example |
|
231 | - * |
|
232 | - * LogicalAnd (operator) |
|
233 | - * + LogicalOr (degenerate operator) |
|
234 | - * | + LogicalAnd (degenerate operator) |
|
235 | - * | + IsType('int') (terminal) |
|
236 | - * + GreaterThan(10) (terminal) |
|
237 | - * |
|
238 | - * is equivalent to |
|
239 | - * |
|
240 | - * LogicalAnd (operator) |
|
241 | - * + IsType('int') (terminal) |
|
242 | - * + GreaterThan(10) (terminal) |
|
243 | - * |
|
244 | - * because the subexpression |
|
245 | - * |
|
246 | - * + LogicalOr |
|
247 | - * + LogicalAnd |
|
248 | - * + - |
|
249 | - * |
|
250 | - * is degenerate. Calling reduce() on the LogicalOr object above, as well |
|
251 | - * as on LogicalAnd, shall return the IsType('int') instance. |
|
252 | - * |
|
253 | - * Other specific reductions can be implemented, for example cascade of |
|
254 | - * LogicalNot operators |
|
255 | - * |
|
256 | - * + LogicalNot |
|
257 | - * + LogicalNot |
|
258 | - * +LogicalNot |
|
259 | - * + IsTrue |
|
260 | - * |
|
261 | - * can be reduced to |
|
262 | - * |
|
263 | - * LogicalNot |
|
264 | - * + IsTrue |
|
265 | - */ |
|
266 | - protected function reduce(): self |
|
267 | - { |
|
268 | - return $this; |
|
269 | - } |
|
206 | + /** |
|
207 | + * Reduces the sub-expression starting at $this by skipping degenerate |
|
208 | + * sub-expression and returns first descendant constraint that starts |
|
209 | + * a non-reducible sub-expression. |
|
210 | + * |
|
211 | + * Returns $this for terminal constraints and for operators that start |
|
212 | + * non-reducible sub-expression, or the nearest descendant of $this that |
|
213 | + * starts a non-reducible sub-expression. |
|
214 | + * |
|
215 | + * A constraint expression may be modelled as a tree with non-terminal |
|
216 | + * nodes (operators) and terminal nodes. For example: |
|
217 | + * |
|
218 | + * LogicalOr (operator, non-terminal) |
|
219 | + * + LogicalAnd (operator, non-terminal) |
|
220 | + * | + IsType('int') (terminal) |
|
221 | + * | + GreaterThan(10) (terminal) |
|
222 | + * + LogicalNot (operator, non-terminal) |
|
223 | + * + IsType('array') (terminal) |
|
224 | + * |
|
225 | + * A degenerate sub-expression is a part of the tree, that effectively does |
|
226 | + * not contribute to the evaluation of the expression it appears in. An example |
|
227 | + * of degenerate sub-expression is a BinaryOperator constructed with single |
|
228 | + * operand or nested BinaryOperators, each with single operand. An |
|
229 | + * expression involving a degenerate sub-expression is equivalent to a |
|
230 | + * reduced expression with the degenerate sub-expression removed, for example |
|
231 | + * |
|
232 | + * LogicalAnd (operator) |
|
233 | + * + LogicalOr (degenerate operator) |
|
234 | + * | + LogicalAnd (degenerate operator) |
|
235 | + * | + IsType('int') (terminal) |
|
236 | + * + GreaterThan(10) (terminal) |
|
237 | + * |
|
238 | + * is equivalent to |
|
239 | + * |
|
240 | + * LogicalAnd (operator) |
|
241 | + * + IsType('int') (terminal) |
|
242 | + * + GreaterThan(10) (terminal) |
|
243 | + * |
|
244 | + * because the subexpression |
|
245 | + * |
|
246 | + * + LogicalOr |
|
247 | + * + LogicalAnd |
|
248 | + * + - |
|
249 | + * |
|
250 | + * is degenerate. Calling reduce() on the LogicalOr object above, as well |
|
251 | + * as on LogicalAnd, shall return the IsType('int') instance. |
|
252 | + * |
|
253 | + * Other specific reductions can be implemented, for example cascade of |
|
254 | + * LogicalNot operators |
|
255 | + * |
|
256 | + * + LogicalNot |
|
257 | + * + LogicalNot |
|
258 | + * +LogicalNot |
|
259 | + * + IsTrue |
|
260 | + * |
|
261 | + * can be reduced to |
|
262 | + * |
|
263 | + * LogicalNot |
|
264 | + * + IsTrue |
|
265 | + */ |
|
266 | + protected function reduce(): self |
|
267 | + { |
|
268 | + return $this; |
|
269 | + } |
|
270 | 270 | } |
@@ -22,105 +22,105 @@ |
||
22 | 22 | */ |
23 | 23 | final class IsIdentical extends Constraint |
24 | 24 | { |
25 | - /** |
|
26 | - * @var mixed |
|
27 | - */ |
|
28 | - private $value; |
|
29 | - |
|
30 | - public function __construct($value) |
|
31 | - { |
|
32 | - $this->value = $value; |
|
33 | - } |
|
34 | - |
|
35 | - /** |
|
36 | - * Evaluates the constraint for parameter $other. |
|
37 | - * |
|
38 | - * If $returnResult is set to false (the default), an exception is thrown |
|
39 | - * in case of a failure. null is returned otherwise. |
|
40 | - * |
|
41 | - * If $returnResult is true, the result of the evaluation is returned as |
|
42 | - * a boolean value instead: true in case of success, false in case of a |
|
43 | - * failure. |
|
44 | - * |
|
45 | - * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
|
46 | - * @throws ExpectationFailedException |
|
47 | - */ |
|
48 | - public function evaluate($other, string $description = '', bool $returnResult = false): ?bool |
|
49 | - { |
|
50 | - $success = $this->value === $other; |
|
51 | - |
|
52 | - if ($returnResult) { |
|
53 | - return $success; |
|
54 | - } |
|
55 | - |
|
56 | - if (!$success) { |
|
57 | - $f = null; |
|
58 | - |
|
59 | - // if both values are strings, make sure a diff is generated |
|
60 | - if (is_string($this->value) && is_string($other)) { |
|
61 | - $f = new ComparisonFailure( |
|
62 | - $this->value, |
|
63 | - $other, |
|
64 | - sprintf("'%s'", $this->value), |
|
65 | - sprintf("'%s'", $other) |
|
66 | - ); |
|
67 | - } |
|
68 | - |
|
69 | - // if both values are array, make sure a diff is generated |
|
70 | - if (is_array($this->value) && is_array($other)) { |
|
71 | - $f = new ComparisonFailure( |
|
72 | - $this->value, |
|
73 | - $other, |
|
74 | - $this->exporter()->export($this->value), |
|
75 | - $this->exporter()->export($other) |
|
76 | - ); |
|
77 | - } |
|
78 | - |
|
79 | - $this->fail($other, $description, $f); |
|
80 | - } |
|
81 | - |
|
82 | - return null; |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Returns a string representation of the constraint. |
|
87 | - * |
|
88 | - * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
|
89 | - */ |
|
90 | - public function toString(): string |
|
91 | - { |
|
92 | - if (is_object($this->value)) { |
|
93 | - return 'is identical to an object of class "' . |
|
94 | - get_class($this->value) . '"'; |
|
95 | - } |
|
96 | - |
|
97 | - return 'is identical to ' . $this->exporter()->export($this->value); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Returns the description of the failure. |
|
102 | - * |
|
103 | - * The beginning of failure messages is "Failed asserting that" in most |
|
104 | - * cases. This method should return the second part of that sentence. |
|
105 | - * |
|
106 | - * @param mixed $other evaluated value or object |
|
107 | - * |
|
108 | - * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
|
109 | - */ |
|
110 | - protected function failureDescription($other): string |
|
111 | - { |
|
112 | - if (is_object($this->value) && is_object($other)) { |
|
113 | - return 'two variables reference the same object'; |
|
114 | - } |
|
115 | - |
|
116 | - if (is_string($this->value) && is_string($other)) { |
|
117 | - return 'two strings are identical'; |
|
118 | - } |
|
119 | - |
|
120 | - if (is_array($this->value) && is_array($other)) { |
|
121 | - return 'two arrays are identical'; |
|
122 | - } |
|
123 | - |
|
124 | - return parent::failureDescription($other); |
|
125 | - } |
|
25 | + /** |
|
26 | + * @var mixed |
|
27 | + */ |
|
28 | + private $value; |
|
29 | + |
|
30 | + public function __construct($value) |
|
31 | + { |
|
32 | + $this->value = $value; |
|
33 | + } |
|
34 | + |
|
35 | + /** |
|
36 | + * Evaluates the constraint for parameter $other. |
|
37 | + * |
|
38 | + * If $returnResult is set to false (the default), an exception is thrown |
|
39 | + * in case of a failure. null is returned otherwise. |
|
40 | + * |
|
41 | + * If $returnResult is true, the result of the evaluation is returned as |
|
42 | + * a boolean value instead: true in case of success, false in case of a |
|
43 | + * failure. |
|
44 | + * |
|
45 | + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
|
46 | + * @throws ExpectationFailedException |
|
47 | + */ |
|
48 | + public function evaluate($other, string $description = '', bool $returnResult = false): ?bool |
|
49 | + { |
|
50 | + $success = $this->value === $other; |
|
51 | + |
|
52 | + if ($returnResult) { |
|
53 | + return $success; |
|
54 | + } |
|
55 | + |
|
56 | + if (!$success) { |
|
57 | + $f = null; |
|
58 | + |
|
59 | + // if both values are strings, make sure a diff is generated |
|
60 | + if (is_string($this->value) && is_string($other)) { |
|
61 | + $f = new ComparisonFailure( |
|
62 | + $this->value, |
|
63 | + $other, |
|
64 | + sprintf("'%s'", $this->value), |
|
65 | + sprintf("'%s'", $other) |
|
66 | + ); |
|
67 | + } |
|
68 | + |
|
69 | + // if both values are array, make sure a diff is generated |
|
70 | + if (is_array($this->value) && is_array($other)) { |
|
71 | + $f = new ComparisonFailure( |
|
72 | + $this->value, |
|
73 | + $other, |
|
74 | + $this->exporter()->export($this->value), |
|
75 | + $this->exporter()->export($other) |
|
76 | + ); |
|
77 | + } |
|
78 | + |
|
79 | + $this->fail($other, $description, $f); |
|
80 | + } |
|
81 | + |
|
82 | + return null; |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Returns a string representation of the constraint. |
|
87 | + * |
|
88 | + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
|
89 | + */ |
|
90 | + public function toString(): string |
|
91 | + { |
|
92 | + if (is_object($this->value)) { |
|
93 | + return 'is identical to an object of class "' . |
|
94 | + get_class($this->value) . '"'; |
|
95 | + } |
|
96 | + |
|
97 | + return 'is identical to ' . $this->exporter()->export($this->value); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Returns the description of the failure. |
|
102 | + * |
|
103 | + * The beginning of failure messages is "Failed asserting that" in most |
|
104 | + * cases. This method should return the second part of that sentence. |
|
105 | + * |
|
106 | + * @param mixed $other evaluated value or object |
|
107 | + * |
|
108 | + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
|
109 | + */ |
|
110 | + protected function failureDescription($other): string |
|
111 | + { |
|
112 | + if (is_object($this->value) && is_object($other)) { |
|
113 | + return 'two variables reference the same object'; |
|
114 | + } |
|
115 | + |
|
116 | + if (is_string($this->value) && is_string($other)) { |
|
117 | + return 'two strings are identical'; |
|
118 | + } |
|
119 | + |
|
120 | + if (is_array($this->value) && is_array($other)) { |
|
121 | + return 'two arrays are identical'; |
|
122 | + } |
|
123 | + |
|
124 | + return parent::failureDescription($other); |
|
125 | + } |
|
126 | 126 | } |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | return $success; |
54 | 54 | } |
55 | 55 | |
56 | - if (!$success) { |
|
56 | + if ( ! $success) { |
|
57 | 57 | $f = null; |
58 | 58 | |
59 | 59 | // if both values are strings, make sure a diff is generated |
@@ -90,11 +90,11 @@ discard block |
||
90 | 90 | public function toString(): string |
91 | 91 | { |
92 | 92 | if (is_object($this->value)) { |
93 | - return 'is identical to an object of class "' . |
|
94 | - get_class($this->value) . '"'; |
|
93 | + return 'is identical to an object of class "'. |
|
94 | + get_class($this->value).'"'; |
|
95 | 95 | } |
96 | 96 | |
97 | - return 'is identical to ' . $this->exporter()->export($this->value); |
|
97 | + return 'is identical to '.$this->exporter()->export($this->value); |
|
98 | 98 | } |
99 | 99 | |
100 | 100 | /** |
@@ -11,230 +11,230 @@ |
||
11 | 11 | |
12 | 12 | class CssToInlineStyles |
13 | 13 | { |
14 | - private $cssConverter; |
|
15 | - |
|
16 | - public function __construct() |
|
17 | - { |
|
18 | - if (class_exists('Symfony\Component\CssSelector\CssSelectorConverter')) { |
|
19 | - $this->cssConverter = new CssSelectorConverter(); |
|
20 | - } |
|
21 | - } |
|
22 | - |
|
23 | - /** |
|
24 | - * Will inline the $css into the given $html |
|
25 | - * |
|
26 | - * Remark: if the html contains <style>-tags those will be used, the rules |
|
27 | - * in $css will be appended. |
|
28 | - * |
|
29 | - * @param string $html |
|
30 | - * @param string $css |
|
31 | - * |
|
32 | - * @return string |
|
33 | - */ |
|
34 | - public function convert($html, $css = null) |
|
35 | - { |
|
36 | - $document = $this->createDomDocumentFromHtml($html); |
|
37 | - $processor = new Processor(); |
|
38 | - |
|
39 | - // get all styles from the style-tags |
|
40 | - $rules = $processor->getRules( |
|
41 | - $processor->getCssFromStyleTags($html) |
|
42 | - ); |
|
43 | - |
|
44 | - if ($css !== null) { |
|
45 | - $rules = $processor->getRules($css, $rules); |
|
46 | - } |
|
47 | - |
|
48 | - $document = $this->inline($document, $rules); |
|
49 | - |
|
50 | - return $this->getHtmlFromDocument($document); |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Inline the given properties on an given DOMElement |
|
55 | - * |
|
56 | - * @param \DOMElement $element |
|
57 | - * @param Css\Property\Property[] $properties |
|
58 | - * |
|
59 | - * @return \DOMElement |
|
60 | - */ |
|
61 | - public function inlineCssOnElement(\DOMElement $element, array $properties) |
|
62 | - { |
|
63 | - if (empty($properties)) { |
|
64 | - return $element; |
|
65 | - } |
|
66 | - |
|
67 | - $cssProperties = array(); |
|
68 | - $inlineProperties = array(); |
|
69 | - |
|
70 | - foreach ($this->getInlineStyles($element) as $property) { |
|
71 | - $inlineProperties[$property->getName()] = $property; |
|
72 | - } |
|
73 | - |
|
74 | - foreach ($properties as $property) { |
|
75 | - if (!isset($inlineProperties[$property->getName()])) { |
|
76 | - $cssProperties[$property->getName()] = $property; |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - $rules = array(); |
|
81 | - foreach (array_merge($cssProperties, $inlineProperties) as $property) { |
|
82 | - $rules[] = $property->toString(); |
|
83 | - } |
|
84 | - $element->setAttribute('style', implode(' ', $rules)); |
|
85 | - |
|
86 | - return $element; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Get the current inline styles for a given DOMElement |
|
91 | - * |
|
92 | - * @param \DOMElement $element |
|
93 | - * |
|
94 | - * @return Css\Property\Property[] |
|
95 | - */ |
|
96 | - public function getInlineStyles(\DOMElement $element) |
|
97 | - { |
|
98 | - $processor = new PropertyProcessor(); |
|
99 | - |
|
100 | - return $processor->convertArrayToObjects( |
|
101 | - $processor->splitIntoSeparateProperties( |
|
102 | - $element->getAttribute('style') |
|
103 | - ) |
|
104 | - ); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * @param string $html |
|
109 | - * |
|
110 | - * @return \DOMDocument |
|
111 | - */ |
|
112 | - protected function createDomDocumentFromHtml($html) |
|
113 | - { |
|
114 | - $document = new \DOMDocument('1.0', 'UTF-8'); |
|
115 | - $internalErrors = libxml_use_internal_errors(true); |
|
116 | - $document->loadHTML(mb_encode_numericentity($html, [0x80, 0xFFFF, 0, 0xFFFF], 'UTF-8')); |
|
117 | - libxml_use_internal_errors($internalErrors); |
|
118 | - $document->formatOutput = true; |
|
119 | - |
|
120 | - return $document; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * @param \DOMDocument $document |
|
125 | - * |
|
126 | - * @return string |
|
127 | - */ |
|
128 | - protected function getHtmlFromDocument(\DOMDocument $document) |
|
129 | - { |
|
130 | - // retrieve the document element |
|
131 | - // we do it this way to preserve the utf-8 encoding |
|
132 | - $htmlElement = $document->documentElement; |
|
133 | - $html = $document->saveHTML($htmlElement); |
|
134 | - $html = trim($html); |
|
135 | - |
|
136 | - // retrieve the doctype |
|
137 | - $document->removeChild($htmlElement); |
|
138 | - $doctype = $document->saveHTML(); |
|
139 | - $doctype = trim($doctype); |
|
140 | - |
|
141 | - // if it is the html5 doctype convert it to lowercase |
|
142 | - if ($doctype === '<!DOCTYPE html>') { |
|
143 | - $doctype = strtolower($doctype); |
|
144 | - } |
|
145 | - |
|
146 | - return $doctype."\n".$html; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * @param \DOMDocument $document |
|
151 | - * @param Css\Rule\Rule[] $rules |
|
152 | - * |
|
153 | - * @return \DOMDocument |
|
154 | - */ |
|
155 | - protected function inline(\DOMDocument $document, array $rules) |
|
156 | - { |
|
157 | - if (empty($rules)) { |
|
158 | - return $document; |
|
159 | - } |
|
160 | - |
|
161 | - $propertyStorage = new \SplObjectStorage(); |
|
162 | - |
|
163 | - $xPath = new \DOMXPath($document); |
|
164 | - |
|
165 | - usort($rules, array(RuleProcessor::class, 'sortOnSpecificity')); |
|
166 | - |
|
167 | - foreach ($rules as $rule) { |
|
168 | - try { |
|
169 | - if (null !== $this->cssConverter) { |
|
170 | - $expression = $this->cssConverter->toXPath($rule->getSelector()); |
|
171 | - } else { |
|
172 | - // Compatibility layer for Symfony 2.7 and older |
|
173 | - $expression = CssSelector::toXPath($rule->getSelector()); |
|
174 | - } |
|
175 | - } catch (ExceptionInterface $e) { |
|
176 | - continue; |
|
177 | - } |
|
178 | - |
|
179 | - $elements = $xPath->query($expression); |
|
180 | - |
|
181 | - if ($elements === false) { |
|
182 | - continue; |
|
183 | - } |
|
184 | - |
|
185 | - foreach ($elements as $element) { |
|
186 | - $propertyStorage[$element] = $this->calculatePropertiesToBeApplied( |
|
187 | - $rule->getProperties(), |
|
188 | - $propertyStorage->contains($element) ? $propertyStorage[$element] : array() |
|
189 | - ); |
|
190 | - } |
|
191 | - } |
|
192 | - |
|
193 | - foreach ($propertyStorage as $element) { |
|
194 | - $this->inlineCssOnElement($element, $propertyStorage[$element]); |
|
195 | - } |
|
196 | - |
|
197 | - return $document; |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * Merge the CSS rules to determine the applied properties. |
|
202 | - * |
|
203 | - * @param Css\Property\Property[] $properties |
|
204 | - * @param Css\Property\Property[] $cssProperties existing applied properties indexed by name |
|
205 | - * |
|
206 | - * @return Css\Property\Property[] updated properties, indexed by name |
|
207 | - */ |
|
208 | - private function calculatePropertiesToBeApplied(array $properties, array $cssProperties) |
|
209 | - { |
|
210 | - if (empty($properties)) { |
|
211 | - return $cssProperties; |
|
212 | - } |
|
213 | - |
|
214 | - foreach ($properties as $property) { |
|
215 | - if (isset($cssProperties[$property->getName()])) { |
|
216 | - $existingProperty = $cssProperties[$property->getName()]; |
|
217 | - |
|
218 | - //skip check to overrule if existing property is important and current is not |
|
219 | - if ($existingProperty->isImportant() && !$property->isImportant()) { |
|
220 | - continue; |
|
221 | - } |
|
222 | - |
|
223 | - //overrule if current property is important and existing is not, else check specificity |
|
224 | - $overrule = !$existingProperty->isImportant() && $property->isImportant(); |
|
225 | - if (!$overrule) { |
|
226 | - $overrule = $existingProperty->getOriginalSpecificity()->compareTo($property->getOriginalSpecificity()) <= 0; |
|
227 | - } |
|
228 | - |
|
229 | - if ($overrule) { |
|
230 | - unset($cssProperties[$property->getName()]); |
|
231 | - $cssProperties[$property->getName()] = $property; |
|
232 | - } |
|
233 | - } else { |
|
234 | - $cssProperties[$property->getName()] = $property; |
|
235 | - } |
|
236 | - } |
|
237 | - |
|
238 | - return $cssProperties; |
|
239 | - } |
|
14 | + private $cssConverter; |
|
15 | + |
|
16 | + public function __construct() |
|
17 | + { |
|
18 | + if (class_exists('Symfony\Component\CssSelector\CssSelectorConverter')) { |
|
19 | + $this->cssConverter = new CssSelectorConverter(); |
|
20 | + } |
|
21 | + } |
|
22 | + |
|
23 | + /** |
|
24 | + * Will inline the $css into the given $html |
|
25 | + * |
|
26 | + * Remark: if the html contains <style>-tags those will be used, the rules |
|
27 | + * in $css will be appended. |
|
28 | + * |
|
29 | + * @param string $html |
|
30 | + * @param string $css |
|
31 | + * |
|
32 | + * @return string |
|
33 | + */ |
|
34 | + public function convert($html, $css = null) |
|
35 | + { |
|
36 | + $document = $this->createDomDocumentFromHtml($html); |
|
37 | + $processor = new Processor(); |
|
38 | + |
|
39 | + // get all styles from the style-tags |
|
40 | + $rules = $processor->getRules( |
|
41 | + $processor->getCssFromStyleTags($html) |
|
42 | + ); |
|
43 | + |
|
44 | + if ($css !== null) { |
|
45 | + $rules = $processor->getRules($css, $rules); |
|
46 | + } |
|
47 | + |
|
48 | + $document = $this->inline($document, $rules); |
|
49 | + |
|
50 | + return $this->getHtmlFromDocument($document); |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Inline the given properties on an given DOMElement |
|
55 | + * |
|
56 | + * @param \DOMElement $element |
|
57 | + * @param Css\Property\Property[] $properties |
|
58 | + * |
|
59 | + * @return \DOMElement |
|
60 | + */ |
|
61 | + public function inlineCssOnElement(\DOMElement $element, array $properties) |
|
62 | + { |
|
63 | + if (empty($properties)) { |
|
64 | + return $element; |
|
65 | + } |
|
66 | + |
|
67 | + $cssProperties = array(); |
|
68 | + $inlineProperties = array(); |
|
69 | + |
|
70 | + foreach ($this->getInlineStyles($element) as $property) { |
|
71 | + $inlineProperties[$property->getName()] = $property; |
|
72 | + } |
|
73 | + |
|
74 | + foreach ($properties as $property) { |
|
75 | + if (!isset($inlineProperties[$property->getName()])) { |
|
76 | + $cssProperties[$property->getName()] = $property; |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + $rules = array(); |
|
81 | + foreach (array_merge($cssProperties, $inlineProperties) as $property) { |
|
82 | + $rules[] = $property->toString(); |
|
83 | + } |
|
84 | + $element->setAttribute('style', implode(' ', $rules)); |
|
85 | + |
|
86 | + return $element; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Get the current inline styles for a given DOMElement |
|
91 | + * |
|
92 | + * @param \DOMElement $element |
|
93 | + * |
|
94 | + * @return Css\Property\Property[] |
|
95 | + */ |
|
96 | + public function getInlineStyles(\DOMElement $element) |
|
97 | + { |
|
98 | + $processor = new PropertyProcessor(); |
|
99 | + |
|
100 | + return $processor->convertArrayToObjects( |
|
101 | + $processor->splitIntoSeparateProperties( |
|
102 | + $element->getAttribute('style') |
|
103 | + ) |
|
104 | + ); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * @param string $html |
|
109 | + * |
|
110 | + * @return \DOMDocument |
|
111 | + */ |
|
112 | + protected function createDomDocumentFromHtml($html) |
|
113 | + { |
|
114 | + $document = new \DOMDocument('1.0', 'UTF-8'); |
|
115 | + $internalErrors = libxml_use_internal_errors(true); |
|
116 | + $document->loadHTML(mb_encode_numericentity($html, [0x80, 0xFFFF, 0, 0xFFFF], 'UTF-8')); |
|
117 | + libxml_use_internal_errors($internalErrors); |
|
118 | + $document->formatOutput = true; |
|
119 | + |
|
120 | + return $document; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * @param \DOMDocument $document |
|
125 | + * |
|
126 | + * @return string |
|
127 | + */ |
|
128 | + protected function getHtmlFromDocument(\DOMDocument $document) |
|
129 | + { |
|
130 | + // retrieve the document element |
|
131 | + // we do it this way to preserve the utf-8 encoding |
|
132 | + $htmlElement = $document->documentElement; |
|
133 | + $html = $document->saveHTML($htmlElement); |
|
134 | + $html = trim($html); |
|
135 | + |
|
136 | + // retrieve the doctype |
|
137 | + $document->removeChild($htmlElement); |
|
138 | + $doctype = $document->saveHTML(); |
|
139 | + $doctype = trim($doctype); |
|
140 | + |
|
141 | + // if it is the html5 doctype convert it to lowercase |
|
142 | + if ($doctype === '<!DOCTYPE html>') { |
|
143 | + $doctype = strtolower($doctype); |
|
144 | + } |
|
145 | + |
|
146 | + return $doctype."\n".$html; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * @param \DOMDocument $document |
|
151 | + * @param Css\Rule\Rule[] $rules |
|
152 | + * |
|
153 | + * @return \DOMDocument |
|
154 | + */ |
|
155 | + protected function inline(\DOMDocument $document, array $rules) |
|
156 | + { |
|
157 | + if (empty($rules)) { |
|
158 | + return $document; |
|
159 | + } |
|
160 | + |
|
161 | + $propertyStorage = new \SplObjectStorage(); |
|
162 | + |
|
163 | + $xPath = new \DOMXPath($document); |
|
164 | + |
|
165 | + usort($rules, array(RuleProcessor::class, 'sortOnSpecificity')); |
|
166 | + |
|
167 | + foreach ($rules as $rule) { |
|
168 | + try { |
|
169 | + if (null !== $this->cssConverter) { |
|
170 | + $expression = $this->cssConverter->toXPath($rule->getSelector()); |
|
171 | + } else { |
|
172 | + // Compatibility layer for Symfony 2.7 and older |
|
173 | + $expression = CssSelector::toXPath($rule->getSelector()); |
|
174 | + } |
|
175 | + } catch (ExceptionInterface $e) { |
|
176 | + continue; |
|
177 | + } |
|
178 | + |
|
179 | + $elements = $xPath->query($expression); |
|
180 | + |
|
181 | + if ($elements === false) { |
|
182 | + continue; |
|
183 | + } |
|
184 | + |
|
185 | + foreach ($elements as $element) { |
|
186 | + $propertyStorage[$element] = $this->calculatePropertiesToBeApplied( |
|
187 | + $rule->getProperties(), |
|
188 | + $propertyStorage->contains($element) ? $propertyStorage[$element] : array() |
|
189 | + ); |
|
190 | + } |
|
191 | + } |
|
192 | + |
|
193 | + foreach ($propertyStorage as $element) { |
|
194 | + $this->inlineCssOnElement($element, $propertyStorage[$element]); |
|
195 | + } |
|
196 | + |
|
197 | + return $document; |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * Merge the CSS rules to determine the applied properties. |
|
202 | + * |
|
203 | + * @param Css\Property\Property[] $properties |
|
204 | + * @param Css\Property\Property[] $cssProperties existing applied properties indexed by name |
|
205 | + * |
|
206 | + * @return Css\Property\Property[] updated properties, indexed by name |
|
207 | + */ |
|
208 | + private function calculatePropertiesToBeApplied(array $properties, array $cssProperties) |
|
209 | + { |
|
210 | + if (empty($properties)) { |
|
211 | + return $cssProperties; |
|
212 | + } |
|
213 | + |
|
214 | + foreach ($properties as $property) { |
|
215 | + if (isset($cssProperties[$property->getName()])) { |
|
216 | + $existingProperty = $cssProperties[$property->getName()]; |
|
217 | + |
|
218 | + //skip check to overrule if existing property is important and current is not |
|
219 | + if ($existingProperty->isImportant() && !$property->isImportant()) { |
|
220 | + continue; |
|
221 | + } |
|
222 | + |
|
223 | + //overrule if current property is important and existing is not, else check specificity |
|
224 | + $overrule = !$existingProperty->isImportant() && $property->isImportant(); |
|
225 | + if (!$overrule) { |
|
226 | + $overrule = $existingProperty->getOriginalSpecificity()->compareTo($property->getOriginalSpecificity()) <= 0; |
|
227 | + } |
|
228 | + |
|
229 | + if ($overrule) { |
|
230 | + unset($cssProperties[$property->getName()]); |
|
231 | + $cssProperties[$property->getName()] = $property; |
|
232 | + } |
|
233 | + } else { |
|
234 | + $cssProperties[$property->getName()] = $property; |
|
235 | + } |
|
236 | + } |
|
237 | + |
|
238 | + return $cssProperties; |
|
239 | + } |
|
240 | 240 | } |
@@ -29,378 +29,378 @@ |
||
29 | 29 | |
30 | 30 | class Document extends AbstractTag |
31 | 31 | { |
32 | - protected $filename; |
|
33 | - public $inDefs = false; |
|
34 | - |
|
35 | - protected $x; |
|
36 | - protected $y; |
|
37 | - protected $width; |
|
38 | - protected $height; |
|
39 | - |
|
40 | - protected $subPathInit; |
|
41 | - protected $pathBBox; |
|
42 | - protected $viewBox; |
|
43 | - |
|
44 | - /** @var SurfaceInterface */ |
|
45 | - protected $surface; |
|
46 | - |
|
47 | - /** @var AbstractTag[] */ |
|
48 | - protected $stack = array(); |
|
49 | - |
|
50 | - /** @var AbstractTag[] */ |
|
51 | - protected $defs = array(); |
|
52 | - |
|
53 | - /** @var \Sabberworm\CSS\CSSList\Document[] */ |
|
54 | - protected $styleSheets = array(); |
|
55 | - |
|
56 | - public function loadFile($filename) |
|
57 | - { |
|
58 | - $this->filename = $filename; |
|
59 | - } |
|
60 | - |
|
61 | - protected function initParser() { |
|
62 | - $parser = xml_parser_create("utf-8"); |
|
63 | - xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false); |
|
64 | - xml_set_element_handler( |
|
65 | - $parser, |
|
66 | - array($this, "_tagStart"), |
|
67 | - array($this, "_tagEnd") |
|
68 | - ); |
|
69 | - xml_set_character_data_handler( |
|
70 | - $parser, |
|
71 | - array($this, "_charData") |
|
72 | - ); |
|
73 | - |
|
74 | - return $parser; |
|
75 | - } |
|
76 | - |
|
77 | - public function __construct() { |
|
78 | - |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * @return SurfaceInterface |
|
83 | - */ |
|
84 | - public function getSurface() |
|
85 | - { |
|
86 | - return $this->surface; |
|
87 | - } |
|
88 | - |
|
89 | - public function getStack() |
|
90 | - { |
|
91 | - return $this->stack; |
|
92 | - } |
|
93 | - |
|
94 | - public function getWidth() |
|
95 | - { |
|
96 | - return $this->width; |
|
97 | - } |
|
98 | - |
|
99 | - public function getHeight() |
|
100 | - { |
|
101 | - return $this->height; |
|
102 | - } |
|
103 | - |
|
104 | - public function getDiagonal() |
|
105 | - { |
|
106 | - return sqrt(($this->width)**2 + ($this->height)**2) / sqrt(2); |
|
107 | - } |
|
108 | - |
|
109 | - public function getDimensions() { |
|
110 | - $rootAttributes = null; |
|
111 | - |
|
112 | - $parser = xml_parser_create("utf-8"); |
|
113 | - xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false); |
|
114 | - xml_set_element_handler( |
|
115 | - $parser, |
|
116 | - function ($parser, $name, $attributes) use (&$rootAttributes) { |
|
117 | - if ($name === "svg" && $rootAttributes === null) { |
|
118 | - $attributes = array_change_key_case($attributes, CASE_LOWER); |
|
119 | - |
|
120 | - $rootAttributes = $attributes; |
|
121 | - } |
|
122 | - }, |
|
123 | - function ($parser, $name) {} |
|
124 | - ); |
|
125 | - |
|
126 | - $fp = fopen($this->filename, "r"); |
|
127 | - while ($line = fread($fp, 8192)) { |
|
128 | - xml_parse($parser, $line, false); |
|
129 | - |
|
130 | - if ($rootAttributes !== null) { |
|
131 | - break; |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - xml_parser_free($parser); |
|
136 | - |
|
137 | - return $this->handleSizeAttributes($rootAttributes); |
|
138 | - } |
|
139 | - |
|
140 | - public function handleSizeAttributes($attributes){ |
|
141 | - if ($this->width === null) { |
|
142 | - if (isset($attributes["width"])) { |
|
143 | - $width = $this->convertSize($attributes["width"], 400); |
|
144 | - $this->width = $width; |
|
145 | - } |
|
146 | - |
|
147 | - if (isset($attributes["height"])) { |
|
148 | - $height = $this->convertSize($attributes["height"], 300); |
|
149 | - $this->height = $height; |
|
150 | - } |
|
151 | - |
|
152 | - if (isset($attributes['viewbox'])) { |
|
153 | - $viewBox = preg_split('/[\s,]+/is', trim($attributes['viewbox'])); |
|
154 | - if (count($viewBox) == 4) { |
|
155 | - $this->x = $viewBox[0]; |
|
156 | - $this->y = $viewBox[1]; |
|
157 | - |
|
158 | - if (!$this->width) { |
|
159 | - $this->width = $viewBox[2]; |
|
160 | - } |
|
161 | - if (!$this->height) { |
|
162 | - $this->height = $viewBox[3]; |
|
163 | - } |
|
164 | - } |
|
165 | - } |
|
166 | - } |
|
167 | - |
|
168 | - return array( |
|
169 | - 0 => $this->width, |
|
170 | - 1 => $this->height, |
|
171 | - |
|
172 | - "width" => $this->width, |
|
173 | - "height" => $this->height, |
|
174 | - ); |
|
175 | - } |
|
176 | - |
|
177 | - public function getDocument(){ |
|
178 | - return $this; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Append a style sheet |
|
183 | - * |
|
184 | - * @param \Sabberworm\CSS\CSSList\Document $stylesheet |
|
185 | - */ |
|
186 | - public function appendStyleSheet($stylesheet) { |
|
187 | - $this->styleSheets[] = $stylesheet; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Get the document style sheets |
|
192 | - * |
|
193 | - * @return \Sabberworm\CSS\CSSList\Document[] |
|
194 | - */ |
|
195 | - public function getStyleSheets() { |
|
196 | - return $this->styleSheets; |
|
197 | - } |
|
198 | - |
|
199 | - protected function before($attributes) |
|
200 | - { |
|
201 | - $surface = $this->getSurface(); |
|
202 | - |
|
203 | - $style = new DefaultStyle(); |
|
204 | - $style->inherit($this); |
|
205 | - $style->fromAttributes($attributes); |
|
206 | - |
|
207 | - $this->setStyle($style); |
|
208 | - |
|
209 | - $surface->setStyle($style); |
|
210 | - } |
|
211 | - |
|
212 | - public function render(SurfaceInterface $surface) |
|
213 | - { |
|
214 | - $this->inDefs = false; |
|
215 | - $this->surface = $surface; |
|
216 | - |
|
217 | - $parser = $this->initParser(); |
|
218 | - |
|
219 | - if ($this->x || $this->y) { |
|
220 | - $surface->translate(-$this->x, -$this->y); |
|
221 | - } |
|
222 | - |
|
223 | - $fp = fopen($this->filename, "r"); |
|
224 | - while ($line = fread($fp, 8192)) { |
|
225 | - xml_parse($parser, $line, false); |
|
226 | - } |
|
227 | - |
|
228 | - xml_parse($parser, "", true); |
|
229 | - |
|
230 | - xml_parser_free($parser); |
|
231 | - } |
|
232 | - |
|
233 | - protected function svgOffset($attributes) |
|
234 | - { |
|
235 | - $this->attributes = $attributes; |
|
236 | - |
|
237 | - $this->handleSizeAttributes($attributes); |
|
238 | - } |
|
239 | - |
|
240 | - public function getDef($id) { |
|
241 | - $id = ltrim($id, "#"); |
|
242 | - |
|
243 | - return isset($this->defs[$id]) ? $this->defs[$id] : null; |
|
244 | - } |
|
245 | - |
|
246 | - private function _tagStart($parser, $name, $attributes) |
|
247 | - { |
|
248 | - $this->x = 0; |
|
249 | - $this->y = 0; |
|
250 | - |
|
251 | - $tag = null; |
|
252 | - |
|
253 | - $attributes = array_change_key_case($attributes, CASE_LOWER); |
|
254 | - |
|
255 | - switch (strtolower($name)) { |
|
256 | - case 'defs': |
|
257 | - $this->inDefs = true; |
|
258 | - return; |
|
259 | - |
|
260 | - case 'svg': |
|
261 | - if (count($this->attributes)) { |
|
262 | - $tag = new Group($this, $name); |
|
263 | - } |
|
264 | - else { |
|
265 | - $tag = $this; |
|
266 | - $this->svgOffset($attributes); |
|
267 | - } |
|
268 | - break; |
|
269 | - |
|
270 | - case 'path': |
|
271 | - $tag = new Path($this, $name); |
|
272 | - break; |
|
273 | - |
|
274 | - case 'rect': |
|
275 | - $tag = new Rect($this, $name); |
|
276 | - break; |
|
277 | - |
|
278 | - case 'circle': |
|
279 | - $tag = new Circle($this, $name); |
|
280 | - break; |
|
281 | - |
|
282 | - case 'ellipse': |
|
283 | - $tag = new Ellipse($this, $name); |
|
284 | - break; |
|
285 | - |
|
286 | - case 'image': |
|
287 | - $tag = new Image($this, $name); |
|
288 | - break; |
|
289 | - |
|
290 | - case 'line': |
|
291 | - $tag = new Line($this, $name); |
|
292 | - break; |
|
293 | - |
|
294 | - case 'polyline': |
|
295 | - $tag = new Polyline($this, $name); |
|
296 | - break; |
|
297 | - |
|
298 | - case 'polygon': |
|
299 | - $tag = new Polygon($this, $name); |
|
300 | - break; |
|
301 | - |
|
302 | - case 'lineargradient': |
|
303 | - $tag = new LinearGradient($this, $name); |
|
304 | - break; |
|
305 | - |
|
306 | - case 'radialgradient': |
|
307 | - $tag = new LinearGradient($this, $name); |
|
308 | - break; |
|
309 | - |
|
310 | - case 'stop': |
|
311 | - $tag = new Stop($this, $name); |
|
312 | - break; |
|
313 | - |
|
314 | - case 'style': |
|
315 | - $tag = new StyleTag($this, $name); |
|
316 | - break; |
|
317 | - |
|
318 | - case 'a': |
|
319 | - $tag = new Anchor($this, $name); |
|
320 | - break; |
|
321 | - |
|
322 | - case 'g': |
|
323 | - case 'symbol': |
|
324 | - $tag = new Group($this, $name); |
|
325 | - break; |
|
326 | - |
|
327 | - case 'clippath': |
|
328 | - $tag = new ClipPath($this, $name); |
|
329 | - break; |
|
330 | - |
|
331 | - case 'use': |
|
332 | - $tag = new UseTag($this, $name); |
|
333 | - break; |
|
334 | - |
|
335 | - case 'text': |
|
336 | - $tag = new Text($this, $name); |
|
337 | - break; |
|
338 | - |
|
339 | - case 'desc': |
|
340 | - return; |
|
341 | - } |
|
342 | - |
|
343 | - if ($tag) { |
|
344 | - if (isset($attributes["id"])) { |
|
345 | - $this->defs[$attributes["id"]] = $tag; |
|
346 | - } |
|
347 | - else { |
|
348 | - /** @var AbstractTag $top */ |
|
349 | - $top = end($this->stack); |
|
350 | - if ($top && $top != $tag) { |
|
351 | - $top->children[] = $tag; |
|
352 | - } |
|
353 | - } |
|
354 | - |
|
355 | - $this->stack[] = $tag; |
|
356 | - |
|
357 | - $tag->handle($attributes); |
|
358 | - } |
|
359 | - } |
|
360 | - |
|
361 | - function _charData($parser, $data) |
|
362 | - { |
|
363 | - $stack_top = end($this->stack); |
|
364 | - |
|
365 | - if ($stack_top instanceof Text || $stack_top instanceof StyleTag) { |
|
366 | - $stack_top->appendText($data); |
|
367 | - } |
|
368 | - } |
|
369 | - |
|
370 | - function _tagEnd($parser, $name) |
|
371 | - { |
|
372 | - /** @var AbstractTag $tag */ |
|
373 | - $tag = null; |
|
374 | - switch (strtolower($name)) { |
|
375 | - case 'defs': |
|
376 | - $this->inDefs = false; |
|
377 | - return; |
|
378 | - |
|
379 | - case 'svg': |
|
380 | - case 'path': |
|
381 | - case 'rect': |
|
382 | - case 'circle': |
|
383 | - case 'ellipse': |
|
384 | - case 'image': |
|
385 | - case 'line': |
|
386 | - case 'polyline': |
|
387 | - case 'polygon': |
|
388 | - case 'radialgradient': |
|
389 | - case 'lineargradient': |
|
390 | - case 'stop': |
|
391 | - case 'style': |
|
392 | - case 'text': |
|
393 | - case 'g': |
|
394 | - case 'symbol': |
|
395 | - case 'clippath': |
|
396 | - case 'use': |
|
397 | - case 'a': |
|
398 | - $tag = array_pop($this->stack); |
|
399 | - break; |
|
400 | - } |
|
401 | - |
|
402 | - if (!$this->inDefs && $tag) { |
|
403 | - $tag->handleEnd(); |
|
404 | - } |
|
405 | - } |
|
32 | + protected $filename; |
|
33 | + public $inDefs = false; |
|
34 | + |
|
35 | + protected $x; |
|
36 | + protected $y; |
|
37 | + protected $width; |
|
38 | + protected $height; |
|
39 | + |
|
40 | + protected $subPathInit; |
|
41 | + protected $pathBBox; |
|
42 | + protected $viewBox; |
|
43 | + |
|
44 | + /** @var SurfaceInterface */ |
|
45 | + protected $surface; |
|
46 | + |
|
47 | + /** @var AbstractTag[] */ |
|
48 | + protected $stack = array(); |
|
49 | + |
|
50 | + /** @var AbstractTag[] */ |
|
51 | + protected $defs = array(); |
|
52 | + |
|
53 | + /** @var \Sabberworm\CSS\CSSList\Document[] */ |
|
54 | + protected $styleSheets = array(); |
|
55 | + |
|
56 | + public function loadFile($filename) |
|
57 | + { |
|
58 | + $this->filename = $filename; |
|
59 | + } |
|
60 | + |
|
61 | + protected function initParser() { |
|
62 | + $parser = xml_parser_create("utf-8"); |
|
63 | + xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false); |
|
64 | + xml_set_element_handler( |
|
65 | + $parser, |
|
66 | + array($this, "_tagStart"), |
|
67 | + array($this, "_tagEnd") |
|
68 | + ); |
|
69 | + xml_set_character_data_handler( |
|
70 | + $parser, |
|
71 | + array($this, "_charData") |
|
72 | + ); |
|
73 | + |
|
74 | + return $parser; |
|
75 | + } |
|
76 | + |
|
77 | + public function __construct() { |
|
78 | + |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * @return SurfaceInterface |
|
83 | + */ |
|
84 | + public function getSurface() |
|
85 | + { |
|
86 | + return $this->surface; |
|
87 | + } |
|
88 | + |
|
89 | + public function getStack() |
|
90 | + { |
|
91 | + return $this->stack; |
|
92 | + } |
|
93 | + |
|
94 | + public function getWidth() |
|
95 | + { |
|
96 | + return $this->width; |
|
97 | + } |
|
98 | + |
|
99 | + public function getHeight() |
|
100 | + { |
|
101 | + return $this->height; |
|
102 | + } |
|
103 | + |
|
104 | + public function getDiagonal() |
|
105 | + { |
|
106 | + return sqrt(($this->width)**2 + ($this->height)**2) / sqrt(2); |
|
107 | + } |
|
108 | + |
|
109 | + public function getDimensions() { |
|
110 | + $rootAttributes = null; |
|
111 | + |
|
112 | + $parser = xml_parser_create("utf-8"); |
|
113 | + xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false); |
|
114 | + xml_set_element_handler( |
|
115 | + $parser, |
|
116 | + function ($parser, $name, $attributes) use (&$rootAttributes) { |
|
117 | + if ($name === "svg" && $rootAttributes === null) { |
|
118 | + $attributes = array_change_key_case($attributes, CASE_LOWER); |
|
119 | + |
|
120 | + $rootAttributes = $attributes; |
|
121 | + } |
|
122 | + }, |
|
123 | + function ($parser, $name) {} |
|
124 | + ); |
|
125 | + |
|
126 | + $fp = fopen($this->filename, "r"); |
|
127 | + while ($line = fread($fp, 8192)) { |
|
128 | + xml_parse($parser, $line, false); |
|
129 | + |
|
130 | + if ($rootAttributes !== null) { |
|
131 | + break; |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + xml_parser_free($parser); |
|
136 | + |
|
137 | + return $this->handleSizeAttributes($rootAttributes); |
|
138 | + } |
|
139 | + |
|
140 | + public function handleSizeAttributes($attributes){ |
|
141 | + if ($this->width === null) { |
|
142 | + if (isset($attributes["width"])) { |
|
143 | + $width = $this->convertSize($attributes["width"], 400); |
|
144 | + $this->width = $width; |
|
145 | + } |
|
146 | + |
|
147 | + if (isset($attributes["height"])) { |
|
148 | + $height = $this->convertSize($attributes["height"], 300); |
|
149 | + $this->height = $height; |
|
150 | + } |
|
151 | + |
|
152 | + if (isset($attributes['viewbox'])) { |
|
153 | + $viewBox = preg_split('/[\s,]+/is', trim($attributes['viewbox'])); |
|
154 | + if (count($viewBox) == 4) { |
|
155 | + $this->x = $viewBox[0]; |
|
156 | + $this->y = $viewBox[1]; |
|
157 | + |
|
158 | + if (!$this->width) { |
|
159 | + $this->width = $viewBox[2]; |
|
160 | + } |
|
161 | + if (!$this->height) { |
|
162 | + $this->height = $viewBox[3]; |
|
163 | + } |
|
164 | + } |
|
165 | + } |
|
166 | + } |
|
167 | + |
|
168 | + return array( |
|
169 | + 0 => $this->width, |
|
170 | + 1 => $this->height, |
|
171 | + |
|
172 | + "width" => $this->width, |
|
173 | + "height" => $this->height, |
|
174 | + ); |
|
175 | + } |
|
176 | + |
|
177 | + public function getDocument(){ |
|
178 | + return $this; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Append a style sheet |
|
183 | + * |
|
184 | + * @param \Sabberworm\CSS\CSSList\Document $stylesheet |
|
185 | + */ |
|
186 | + public function appendStyleSheet($stylesheet) { |
|
187 | + $this->styleSheets[] = $stylesheet; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Get the document style sheets |
|
192 | + * |
|
193 | + * @return \Sabberworm\CSS\CSSList\Document[] |
|
194 | + */ |
|
195 | + public function getStyleSheets() { |
|
196 | + return $this->styleSheets; |
|
197 | + } |
|
198 | + |
|
199 | + protected function before($attributes) |
|
200 | + { |
|
201 | + $surface = $this->getSurface(); |
|
202 | + |
|
203 | + $style = new DefaultStyle(); |
|
204 | + $style->inherit($this); |
|
205 | + $style->fromAttributes($attributes); |
|
206 | + |
|
207 | + $this->setStyle($style); |
|
208 | + |
|
209 | + $surface->setStyle($style); |
|
210 | + } |
|
211 | + |
|
212 | + public function render(SurfaceInterface $surface) |
|
213 | + { |
|
214 | + $this->inDefs = false; |
|
215 | + $this->surface = $surface; |
|
216 | + |
|
217 | + $parser = $this->initParser(); |
|
218 | + |
|
219 | + if ($this->x || $this->y) { |
|
220 | + $surface->translate(-$this->x, -$this->y); |
|
221 | + } |
|
222 | + |
|
223 | + $fp = fopen($this->filename, "r"); |
|
224 | + while ($line = fread($fp, 8192)) { |
|
225 | + xml_parse($parser, $line, false); |
|
226 | + } |
|
227 | + |
|
228 | + xml_parse($parser, "", true); |
|
229 | + |
|
230 | + xml_parser_free($parser); |
|
231 | + } |
|
232 | + |
|
233 | + protected function svgOffset($attributes) |
|
234 | + { |
|
235 | + $this->attributes = $attributes; |
|
236 | + |
|
237 | + $this->handleSizeAttributes($attributes); |
|
238 | + } |
|
239 | + |
|
240 | + public function getDef($id) { |
|
241 | + $id = ltrim($id, "#"); |
|
242 | + |
|
243 | + return isset($this->defs[$id]) ? $this->defs[$id] : null; |
|
244 | + } |
|
245 | + |
|
246 | + private function _tagStart($parser, $name, $attributes) |
|
247 | + { |
|
248 | + $this->x = 0; |
|
249 | + $this->y = 0; |
|
250 | + |
|
251 | + $tag = null; |
|
252 | + |
|
253 | + $attributes = array_change_key_case($attributes, CASE_LOWER); |
|
254 | + |
|
255 | + switch (strtolower($name)) { |
|
256 | + case 'defs': |
|
257 | + $this->inDefs = true; |
|
258 | + return; |
|
259 | + |
|
260 | + case 'svg': |
|
261 | + if (count($this->attributes)) { |
|
262 | + $tag = new Group($this, $name); |
|
263 | + } |
|
264 | + else { |
|
265 | + $tag = $this; |
|
266 | + $this->svgOffset($attributes); |
|
267 | + } |
|
268 | + break; |
|
269 | + |
|
270 | + case 'path': |
|
271 | + $tag = new Path($this, $name); |
|
272 | + break; |
|
273 | + |
|
274 | + case 'rect': |
|
275 | + $tag = new Rect($this, $name); |
|
276 | + break; |
|
277 | + |
|
278 | + case 'circle': |
|
279 | + $tag = new Circle($this, $name); |
|
280 | + break; |
|
281 | + |
|
282 | + case 'ellipse': |
|
283 | + $tag = new Ellipse($this, $name); |
|
284 | + break; |
|
285 | + |
|
286 | + case 'image': |
|
287 | + $tag = new Image($this, $name); |
|
288 | + break; |
|
289 | + |
|
290 | + case 'line': |
|
291 | + $tag = new Line($this, $name); |
|
292 | + break; |
|
293 | + |
|
294 | + case 'polyline': |
|
295 | + $tag = new Polyline($this, $name); |
|
296 | + break; |
|
297 | + |
|
298 | + case 'polygon': |
|
299 | + $tag = new Polygon($this, $name); |
|
300 | + break; |
|
301 | + |
|
302 | + case 'lineargradient': |
|
303 | + $tag = new LinearGradient($this, $name); |
|
304 | + break; |
|
305 | + |
|
306 | + case 'radialgradient': |
|
307 | + $tag = new LinearGradient($this, $name); |
|
308 | + break; |
|
309 | + |
|
310 | + case 'stop': |
|
311 | + $tag = new Stop($this, $name); |
|
312 | + break; |
|
313 | + |
|
314 | + case 'style': |
|
315 | + $tag = new StyleTag($this, $name); |
|
316 | + break; |
|
317 | + |
|
318 | + case 'a': |
|
319 | + $tag = new Anchor($this, $name); |
|
320 | + break; |
|
321 | + |
|
322 | + case 'g': |
|
323 | + case 'symbol': |
|
324 | + $tag = new Group($this, $name); |
|
325 | + break; |
|
326 | + |
|
327 | + case 'clippath': |
|
328 | + $tag = new ClipPath($this, $name); |
|
329 | + break; |
|
330 | + |
|
331 | + case 'use': |
|
332 | + $tag = new UseTag($this, $name); |
|
333 | + break; |
|
334 | + |
|
335 | + case 'text': |
|
336 | + $tag = new Text($this, $name); |
|
337 | + break; |
|
338 | + |
|
339 | + case 'desc': |
|
340 | + return; |
|
341 | + } |
|
342 | + |
|
343 | + if ($tag) { |
|
344 | + if (isset($attributes["id"])) { |
|
345 | + $this->defs[$attributes["id"]] = $tag; |
|
346 | + } |
|
347 | + else { |
|
348 | + /** @var AbstractTag $top */ |
|
349 | + $top = end($this->stack); |
|
350 | + if ($top && $top != $tag) { |
|
351 | + $top->children[] = $tag; |
|
352 | + } |
|
353 | + } |
|
354 | + |
|
355 | + $this->stack[] = $tag; |
|
356 | + |
|
357 | + $tag->handle($attributes); |
|
358 | + } |
|
359 | + } |
|
360 | + |
|
361 | + function _charData($parser, $data) |
|
362 | + { |
|
363 | + $stack_top = end($this->stack); |
|
364 | + |
|
365 | + if ($stack_top instanceof Text || $stack_top instanceof StyleTag) { |
|
366 | + $stack_top->appendText($data); |
|
367 | + } |
|
368 | + } |
|
369 | + |
|
370 | + function _tagEnd($parser, $name) |
|
371 | + { |
|
372 | + /** @var AbstractTag $tag */ |
|
373 | + $tag = null; |
|
374 | + switch (strtolower($name)) { |
|
375 | + case 'defs': |
|
376 | + $this->inDefs = false; |
|
377 | + return; |
|
378 | + |
|
379 | + case 'svg': |
|
380 | + case 'path': |
|
381 | + case 'rect': |
|
382 | + case 'circle': |
|
383 | + case 'ellipse': |
|
384 | + case 'image': |
|
385 | + case 'line': |
|
386 | + case 'polyline': |
|
387 | + case 'polygon': |
|
388 | + case 'radialgradient': |
|
389 | + case 'lineargradient': |
|
390 | + case 'stop': |
|
391 | + case 'style': |
|
392 | + case 'text': |
|
393 | + case 'g': |
|
394 | + case 'symbol': |
|
395 | + case 'clippath': |
|
396 | + case 'use': |
|
397 | + case 'a': |
|
398 | + $tag = array_pop($this->stack); |
|
399 | + break; |
|
400 | + } |
|
401 | + |
|
402 | + if (!$this->inDefs && $tag) { |
|
403 | + $tag->handleEnd(); |
|
404 | + } |
|
405 | + } |
|
406 | 406 | } |
@@ -113,14 +113,14 @@ discard block |
||
113 | 113 | xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false); |
114 | 114 | xml_set_element_handler( |
115 | 115 | $parser, |
116 | - function ($parser, $name, $attributes) use (&$rootAttributes) { |
|
116 | + function($parser, $name, $attributes) use (&$rootAttributes) { |
|
117 | 117 | if ($name === "svg" && $rootAttributes === null) { |
118 | 118 | $attributes = array_change_key_case($attributes, CASE_LOWER); |
119 | 119 | |
120 | 120 | $rootAttributes = $attributes; |
121 | 121 | } |
122 | 122 | }, |
123 | - function ($parser, $name) {} |
|
123 | + function($parser, $name) {} |
|
124 | 124 | ); |
125 | 125 | |
126 | 126 | $fp = fopen($this->filename, "r"); |
@@ -137,11 +137,11 @@ discard block |
||
137 | 137 | return $this->handleSizeAttributes($rootAttributes); |
138 | 138 | } |
139 | 139 | |
140 | - public function handleSizeAttributes($attributes){ |
|
140 | + public function handleSizeAttributes($attributes) { |
|
141 | 141 | if ($this->width === null) { |
142 | 142 | if (isset($attributes["width"])) { |
143 | 143 | $width = $this->convertSize($attributes["width"], 400); |
144 | - $this->width = $width; |
|
144 | + $this->width = $width; |
|
145 | 145 | } |
146 | 146 | |
147 | 147 | if (isset($attributes["height"])) { |
@@ -155,10 +155,10 @@ discard block |
||
155 | 155 | $this->x = $viewBox[0]; |
156 | 156 | $this->y = $viewBox[1]; |
157 | 157 | |
158 | - if (!$this->width) { |
|
158 | + if ( ! $this->width) { |
|
159 | 159 | $this->width = $viewBox[2]; |
160 | 160 | } |
161 | - if (!$this->height) { |
|
161 | + if ( ! $this->height) { |
|
162 | 162 | $this->height = $viewBox[3]; |
163 | 163 | } |
164 | 164 | } |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | ); |
175 | 175 | } |
176 | 176 | |
177 | - public function getDocument(){ |
|
177 | + public function getDocument() { |
|
178 | 178 | return $this; |
179 | 179 | } |
180 | 180 | |
@@ -399,7 +399,7 @@ discard block |
||
399 | 399 | break; |
400 | 400 | } |
401 | 401 | |
402 | - if (!$this->inDefs && $tag) { |
|
402 | + if ( ! $this->inDefs && $tag) { |
|
403 | 403 | $tag->handleEnd(); |
404 | 404 | } |
405 | 405 | } |
@@ -10,93 +10,93 @@ |
||
10 | 10 | |
11 | 11 | class UseTag extends AbstractTag |
12 | 12 | { |
13 | - protected $x = 0; |
|
14 | - protected $y = 0; |
|
15 | - protected $width; |
|
16 | - protected $height; |
|
17 | - |
|
18 | - /** @var AbstractTag */ |
|
19 | - protected $reference; |
|
20 | - |
|
21 | - protected function before($attributes) |
|
22 | - { |
|
23 | - if (isset($attributes['x'])) { |
|
24 | - $this->x = $attributes['x']; |
|
25 | - } |
|
26 | - if (isset($attributes['y'])) { |
|
27 | - $this->y = $attributes['y']; |
|
28 | - } |
|
29 | - |
|
30 | - if (isset($attributes['width'])) { |
|
31 | - $this->width = $attributes['width']; |
|
32 | - } |
|
33 | - if (isset($attributes['height'])) { |
|
34 | - $this->height = $attributes['height']; |
|
35 | - } |
|
36 | - |
|
37 | - parent::before($attributes); |
|
38 | - |
|
39 | - $document = $this->getDocument(); |
|
40 | - |
|
41 | - $link = $attributes["href"] ?? $attributes["xlink:href"]; |
|
42 | - $this->reference = $document->getDef($link); |
|
43 | - |
|
44 | - if ($this->reference) { |
|
45 | - $this->reference->before($attributes); |
|
46 | - } |
|
47 | - |
|
48 | - $surface = $document->getSurface(); |
|
49 | - $surface->save(); |
|
50 | - |
|
51 | - $surface->translate($this->x, $this->y); |
|
52 | - } |
|
53 | - |
|
54 | - protected function after() { |
|
55 | - parent::after(); |
|
56 | - |
|
57 | - if ($this->reference) { |
|
58 | - $this->reference->after(); |
|
59 | - } |
|
60 | - |
|
61 | - $this->getDocument()->getSurface()->restore(); |
|
62 | - } |
|
63 | - |
|
64 | - public function handle($attributes) |
|
65 | - { |
|
66 | - parent::handle($attributes); |
|
67 | - |
|
68 | - if (!$this->reference) { |
|
69 | - return; |
|
70 | - } |
|
71 | - |
|
72 | - $mergedAttributes = $this->reference->attributes; |
|
73 | - $attributesToNotMerge = ['x', 'y', 'width', 'height']; |
|
74 | - foreach ($attributes as $attrKey => $attrVal) { |
|
75 | - if (!in_array($attrKey, $attributesToNotMerge) && !isset($mergedAttributes[$attrKey])) { |
|
76 | - $mergedAttributes[$attrKey] = $attrVal; |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - $this->reference->handle($mergedAttributes); |
|
81 | - |
|
82 | - foreach ($this->reference->children as $_child) { |
|
83 | - $_attributes = array_merge($_child->attributes, $mergedAttributes); |
|
84 | - $_child->handle($_attributes); |
|
85 | - } |
|
86 | - } |
|
87 | - |
|
88 | - public function handleEnd() |
|
89 | - { |
|
90 | - parent::handleEnd(); |
|
91 | - |
|
92 | - if (!$this->reference) { |
|
93 | - return; |
|
94 | - } |
|
95 | - |
|
96 | - $this->reference->handleEnd(); |
|
97 | - |
|
98 | - foreach ($this->reference->children as $_child) { |
|
99 | - $_child->handleEnd(); |
|
100 | - } |
|
101 | - } |
|
13 | + protected $x = 0; |
|
14 | + protected $y = 0; |
|
15 | + protected $width; |
|
16 | + protected $height; |
|
17 | + |
|
18 | + /** @var AbstractTag */ |
|
19 | + protected $reference; |
|
20 | + |
|
21 | + protected function before($attributes) |
|
22 | + { |
|
23 | + if (isset($attributes['x'])) { |
|
24 | + $this->x = $attributes['x']; |
|
25 | + } |
|
26 | + if (isset($attributes['y'])) { |
|
27 | + $this->y = $attributes['y']; |
|
28 | + } |
|
29 | + |
|
30 | + if (isset($attributes['width'])) { |
|
31 | + $this->width = $attributes['width']; |
|
32 | + } |
|
33 | + if (isset($attributes['height'])) { |
|
34 | + $this->height = $attributes['height']; |
|
35 | + } |
|
36 | + |
|
37 | + parent::before($attributes); |
|
38 | + |
|
39 | + $document = $this->getDocument(); |
|
40 | + |
|
41 | + $link = $attributes["href"] ?? $attributes["xlink:href"]; |
|
42 | + $this->reference = $document->getDef($link); |
|
43 | + |
|
44 | + if ($this->reference) { |
|
45 | + $this->reference->before($attributes); |
|
46 | + } |
|
47 | + |
|
48 | + $surface = $document->getSurface(); |
|
49 | + $surface->save(); |
|
50 | + |
|
51 | + $surface->translate($this->x, $this->y); |
|
52 | + } |
|
53 | + |
|
54 | + protected function after() { |
|
55 | + parent::after(); |
|
56 | + |
|
57 | + if ($this->reference) { |
|
58 | + $this->reference->after(); |
|
59 | + } |
|
60 | + |
|
61 | + $this->getDocument()->getSurface()->restore(); |
|
62 | + } |
|
63 | + |
|
64 | + public function handle($attributes) |
|
65 | + { |
|
66 | + parent::handle($attributes); |
|
67 | + |
|
68 | + if (!$this->reference) { |
|
69 | + return; |
|
70 | + } |
|
71 | + |
|
72 | + $mergedAttributes = $this->reference->attributes; |
|
73 | + $attributesToNotMerge = ['x', 'y', 'width', 'height']; |
|
74 | + foreach ($attributes as $attrKey => $attrVal) { |
|
75 | + if (!in_array($attrKey, $attributesToNotMerge) && !isset($mergedAttributes[$attrKey])) { |
|
76 | + $mergedAttributes[$attrKey] = $attrVal; |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + $this->reference->handle($mergedAttributes); |
|
81 | + |
|
82 | + foreach ($this->reference->children as $_child) { |
|
83 | + $_attributes = array_merge($_child->attributes, $mergedAttributes); |
|
84 | + $_child->handle($_attributes); |
|
85 | + } |
|
86 | + } |
|
87 | + |
|
88 | + public function handleEnd() |
|
89 | + { |
|
90 | + parent::handleEnd(); |
|
91 | + |
|
92 | + if (!$this->reference) { |
|
93 | + return; |
|
94 | + } |
|
95 | + |
|
96 | + $this->reference->handleEnd(); |
|
97 | + |
|
98 | + foreach ($this->reference->children as $_child) { |
|
99 | + $_child->handleEnd(); |
|
100 | + } |
|
101 | + } |
|
102 | 102 | } |
@@ -65,14 +65,14 @@ discard block |
||
65 | 65 | { |
66 | 66 | parent::handle($attributes); |
67 | 67 | |
68 | - if (!$this->reference) { |
|
68 | + if ( ! $this->reference) { |
|
69 | 69 | return; |
70 | 70 | } |
71 | 71 | |
72 | 72 | $mergedAttributes = $this->reference->attributes; |
73 | 73 | $attributesToNotMerge = ['x', 'y', 'width', 'height']; |
74 | 74 | foreach ($attributes as $attrKey => $attrVal) { |
75 | - if (!in_array($attrKey, $attributesToNotMerge) && !isset($mergedAttributes[$attrKey])) { |
|
75 | + if ( ! in_array($attrKey, $attributesToNotMerge) && ! isset($mergedAttributes[$attrKey])) { |
|
76 | 76 | $mergedAttributes[$attrKey] = $attrVal; |
77 | 77 | } |
78 | 78 | } |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | { |
90 | 90 | parent::handleEnd(); |
91 | 91 | |
92 | - if (!$this->reference) { |
|
92 | + if ( ! $this->reference) { |
|
93 | 93 | return; |
94 | 94 | } |
95 | 95 |
@@ -14,223 +14,223 @@ |
||
14 | 14 | |
15 | 15 | abstract class AbstractTag |
16 | 16 | { |
17 | - /** @var Document */ |
|
18 | - protected $document; |
|
19 | - |
|
20 | - public $tagName; |
|
21 | - |
|
22 | - /** @var Style */ |
|
23 | - protected $style; |
|
24 | - |
|
25 | - protected $attributes = array(); |
|
26 | - |
|
27 | - protected $hasShape = true; |
|
28 | - |
|
29 | - /** @var self[] */ |
|
30 | - protected $children = array(); |
|
31 | - |
|
32 | - public function __construct(Document $document, $tagName) |
|
33 | - { |
|
34 | - $this->document = $document; |
|
35 | - $this->tagName = $tagName; |
|
36 | - } |
|
37 | - |
|
38 | - public function getDocument(){ |
|
39 | - return $this->document; |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * @return Group|null |
|
44 | - */ |
|
45 | - public function getParentGroup() { |
|
46 | - $stack = $this->getDocument()->getStack(); |
|
47 | - for ($i = count($stack)-2; $i >= 0; $i--) { |
|
48 | - $tag = $stack[$i]; |
|
49 | - |
|
50 | - if ($tag instanceof Group || $tag instanceof Document) { |
|
51 | - return $tag; |
|
52 | - } |
|
53 | - } |
|
54 | - |
|
55 | - return null; |
|
56 | - } |
|
57 | - |
|
58 | - public function handle($attributes) |
|
59 | - { |
|
60 | - $this->attributes = $attributes; |
|
61 | - |
|
62 | - if (!$this->getDocument()->inDefs) { |
|
63 | - $this->before($attributes); |
|
64 | - $this->start($attributes); |
|
65 | - } |
|
66 | - } |
|
67 | - |
|
68 | - public function handleEnd() |
|
69 | - { |
|
70 | - if (!$this->getDocument()->inDefs) { |
|
71 | - $this->end(); |
|
72 | - $this->after(); |
|
73 | - } |
|
74 | - } |
|
75 | - |
|
76 | - protected function before($attributes) |
|
77 | - { |
|
78 | - } |
|
79 | - |
|
80 | - protected function start($attributes) |
|
81 | - { |
|
82 | - } |
|
83 | - |
|
84 | - protected function end() |
|
85 | - { |
|
86 | - } |
|
87 | - |
|
88 | - protected function after() |
|
89 | - { |
|
90 | - } |
|
91 | - |
|
92 | - public function getAttributes() |
|
93 | - { |
|
94 | - return $this->attributes; |
|
95 | - } |
|
96 | - |
|
97 | - protected function setStyle(Style $style) |
|
98 | - { |
|
99 | - $this->style = $style; |
|
100 | - |
|
101 | - if ($style->display === "none") { |
|
102 | - $this->hasShape = false; |
|
103 | - } |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @return Style |
|
108 | - */ |
|
109 | - public function getStyle() |
|
110 | - { |
|
111 | - return $this->style; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Make a style object from the tag and its attributes |
|
116 | - * |
|
117 | - * @param array $attributes |
|
118 | - * |
|
119 | - * @return Style |
|
120 | - */ |
|
121 | - protected function makeStyle($attributes) { |
|
122 | - $style = new Style(); |
|
123 | - $style->inherit($this); |
|
124 | - $style->fromStyleSheets($this, $attributes); |
|
125 | - $style->fromAttributes($attributes); |
|
126 | - |
|
127 | - return $style; |
|
128 | - } |
|
129 | - |
|
130 | - protected function applyTransform($attributes) |
|
131 | - { |
|
132 | - |
|
133 | - if (isset($attributes["transform"])) { |
|
134 | - $surface = $this->document->getSurface(); |
|
135 | - |
|
136 | - $transform = $attributes["transform"]; |
|
137 | - |
|
138 | - $matches = array(); |
|
139 | - preg_match_all( |
|
140 | - '/(matrix|translate|scale|rotate|skew|skewX|skewY)\((.*?)\)/is', |
|
141 | - $transform, |
|
142 | - $matches, |
|
143 | - PREG_SET_ORDER |
|
144 | - ); |
|
145 | - |
|
146 | - $transformations = array(); |
|
147 | - foreach ($matches as $match) { |
|
148 | - $arguments = preg_split('/[ ,]+/', $match[2]); |
|
149 | - array_unshift($arguments, $match[1]); |
|
150 | - $transformations[] = $arguments; |
|
151 | - } |
|
152 | - |
|
153 | - foreach ($transformations as $t) { |
|
154 | - switch ($t[0]) { |
|
155 | - case "matrix": |
|
156 | - $surface->transform($t[1], $t[2], $t[3], $t[4], $t[5], $t[6]); |
|
157 | - break; |
|
158 | - |
|
159 | - case "translate": |
|
160 | - $surface->translate($t[1], isset($t[2]) ? $t[2] : 0); |
|
161 | - break; |
|
162 | - |
|
163 | - case "scale": |
|
164 | - $surface->scale($t[1], isset($t[2]) ? $t[2] : $t[1]); |
|
165 | - break; |
|
166 | - |
|
167 | - case "rotate": |
|
168 | - if (isset($t[2])) { |
|
169 | - $t[3] = isset($t[3]) ? $t[3] : 0; |
|
170 | - $surface->translate($t[2], $t[3]); |
|
171 | - $surface->rotate($t[1]); |
|
172 | - $surface->translate(-$t[2], -$t[3]); |
|
173 | - } else { |
|
174 | - $surface->rotate($t[1]); |
|
175 | - } |
|
176 | - break; |
|
177 | - |
|
178 | - case "skewX": |
|
179 | - $tan_x = tan(deg2rad($t[1])); |
|
180 | - $surface->transform(1, 0, $tan_x, 1, 0, 0); |
|
181 | - break; |
|
182 | - |
|
183 | - case "skewY": |
|
184 | - $tan_y = tan(deg2rad($t[1])); |
|
185 | - $surface->transform(1, $tan_y, 0, 1, 0, 0); |
|
186 | - break; |
|
187 | - } |
|
188 | - } |
|
189 | - } |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Convert the given size for the context of this current tag. |
|
194 | - * Takes a pixel-based reference, which is usually specific to the context of the size, |
|
195 | - * but the actual reference size will be decided based upon the unit used. |
|
196 | - * |
|
197 | - * @param string $size |
|
198 | - * @param float $pxReference |
|
199 | - * |
|
200 | - * @return float |
|
201 | - */ |
|
202 | - protected function convertSize(string $size, float $pxReference): float |
|
203 | - { |
|
204 | - $length = new CssLength($size); |
|
205 | - $reference = $pxReference; |
|
206 | - $defaultFontSize = 12; |
|
207 | - |
|
208 | - switch ($length->getUnit()) { |
|
209 | - case "em": |
|
210 | - $reference = $this->style->fontSize ?? $defaultFontSize; |
|
211 | - break; |
|
212 | - case "rem": |
|
213 | - $reference = $this->document->style->fontSize ?? $defaultFontSize; |
|
214 | - break; |
|
215 | - case "ex": |
|
216 | - case "ch": |
|
217 | - $emRef = $this->style->fontSize ?? $defaultFontSize; |
|
218 | - $reference = $emRef * 0.5; |
|
219 | - break; |
|
220 | - case "vw": |
|
221 | - $reference = $this->getDocument()->getWidth(); |
|
222 | - break; |
|
223 | - case "vh": |
|
224 | - $reference = $this->getDocument()->getHeight(); |
|
225 | - break; |
|
226 | - case "vmin": |
|
227 | - $reference = min($this->getDocument()->getHeight(), $this->getDocument()->getWidth()); |
|
228 | - break; |
|
229 | - case "vmax": |
|
230 | - $reference = max($this->getDocument()->getHeight(), $this->getDocument()->getWidth()); |
|
231 | - break; |
|
232 | - } |
|
233 | - |
|
234 | - return (new CssLength($size))->toPixels($reference); |
|
235 | - } |
|
17 | + /** @var Document */ |
|
18 | + protected $document; |
|
19 | + |
|
20 | + public $tagName; |
|
21 | + |
|
22 | + /** @var Style */ |
|
23 | + protected $style; |
|
24 | + |
|
25 | + protected $attributes = array(); |
|
26 | + |
|
27 | + protected $hasShape = true; |
|
28 | + |
|
29 | + /** @var self[] */ |
|
30 | + protected $children = array(); |
|
31 | + |
|
32 | + public function __construct(Document $document, $tagName) |
|
33 | + { |
|
34 | + $this->document = $document; |
|
35 | + $this->tagName = $tagName; |
|
36 | + } |
|
37 | + |
|
38 | + public function getDocument(){ |
|
39 | + return $this->document; |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * @return Group|null |
|
44 | + */ |
|
45 | + public function getParentGroup() { |
|
46 | + $stack = $this->getDocument()->getStack(); |
|
47 | + for ($i = count($stack)-2; $i >= 0; $i--) { |
|
48 | + $tag = $stack[$i]; |
|
49 | + |
|
50 | + if ($tag instanceof Group || $tag instanceof Document) { |
|
51 | + return $tag; |
|
52 | + } |
|
53 | + } |
|
54 | + |
|
55 | + return null; |
|
56 | + } |
|
57 | + |
|
58 | + public function handle($attributes) |
|
59 | + { |
|
60 | + $this->attributes = $attributes; |
|
61 | + |
|
62 | + if (!$this->getDocument()->inDefs) { |
|
63 | + $this->before($attributes); |
|
64 | + $this->start($attributes); |
|
65 | + } |
|
66 | + } |
|
67 | + |
|
68 | + public function handleEnd() |
|
69 | + { |
|
70 | + if (!$this->getDocument()->inDefs) { |
|
71 | + $this->end(); |
|
72 | + $this->after(); |
|
73 | + } |
|
74 | + } |
|
75 | + |
|
76 | + protected function before($attributes) |
|
77 | + { |
|
78 | + } |
|
79 | + |
|
80 | + protected function start($attributes) |
|
81 | + { |
|
82 | + } |
|
83 | + |
|
84 | + protected function end() |
|
85 | + { |
|
86 | + } |
|
87 | + |
|
88 | + protected function after() |
|
89 | + { |
|
90 | + } |
|
91 | + |
|
92 | + public function getAttributes() |
|
93 | + { |
|
94 | + return $this->attributes; |
|
95 | + } |
|
96 | + |
|
97 | + protected function setStyle(Style $style) |
|
98 | + { |
|
99 | + $this->style = $style; |
|
100 | + |
|
101 | + if ($style->display === "none") { |
|
102 | + $this->hasShape = false; |
|
103 | + } |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @return Style |
|
108 | + */ |
|
109 | + public function getStyle() |
|
110 | + { |
|
111 | + return $this->style; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Make a style object from the tag and its attributes |
|
116 | + * |
|
117 | + * @param array $attributes |
|
118 | + * |
|
119 | + * @return Style |
|
120 | + */ |
|
121 | + protected function makeStyle($attributes) { |
|
122 | + $style = new Style(); |
|
123 | + $style->inherit($this); |
|
124 | + $style->fromStyleSheets($this, $attributes); |
|
125 | + $style->fromAttributes($attributes); |
|
126 | + |
|
127 | + return $style; |
|
128 | + } |
|
129 | + |
|
130 | + protected function applyTransform($attributes) |
|
131 | + { |
|
132 | + |
|
133 | + if (isset($attributes["transform"])) { |
|
134 | + $surface = $this->document->getSurface(); |
|
135 | + |
|
136 | + $transform = $attributes["transform"]; |
|
137 | + |
|
138 | + $matches = array(); |
|
139 | + preg_match_all( |
|
140 | + '/(matrix|translate|scale|rotate|skew|skewX|skewY)\((.*?)\)/is', |
|
141 | + $transform, |
|
142 | + $matches, |
|
143 | + PREG_SET_ORDER |
|
144 | + ); |
|
145 | + |
|
146 | + $transformations = array(); |
|
147 | + foreach ($matches as $match) { |
|
148 | + $arguments = preg_split('/[ ,]+/', $match[2]); |
|
149 | + array_unshift($arguments, $match[1]); |
|
150 | + $transformations[] = $arguments; |
|
151 | + } |
|
152 | + |
|
153 | + foreach ($transformations as $t) { |
|
154 | + switch ($t[0]) { |
|
155 | + case "matrix": |
|
156 | + $surface->transform($t[1], $t[2], $t[3], $t[4], $t[5], $t[6]); |
|
157 | + break; |
|
158 | + |
|
159 | + case "translate": |
|
160 | + $surface->translate($t[1], isset($t[2]) ? $t[2] : 0); |
|
161 | + break; |
|
162 | + |
|
163 | + case "scale": |
|
164 | + $surface->scale($t[1], isset($t[2]) ? $t[2] : $t[1]); |
|
165 | + break; |
|
166 | + |
|
167 | + case "rotate": |
|
168 | + if (isset($t[2])) { |
|
169 | + $t[3] = isset($t[3]) ? $t[3] : 0; |
|
170 | + $surface->translate($t[2], $t[3]); |
|
171 | + $surface->rotate($t[1]); |
|
172 | + $surface->translate(-$t[2], -$t[3]); |
|
173 | + } else { |
|
174 | + $surface->rotate($t[1]); |
|
175 | + } |
|
176 | + break; |
|
177 | + |
|
178 | + case "skewX": |
|
179 | + $tan_x = tan(deg2rad($t[1])); |
|
180 | + $surface->transform(1, 0, $tan_x, 1, 0, 0); |
|
181 | + break; |
|
182 | + |
|
183 | + case "skewY": |
|
184 | + $tan_y = tan(deg2rad($t[1])); |
|
185 | + $surface->transform(1, $tan_y, 0, 1, 0, 0); |
|
186 | + break; |
|
187 | + } |
|
188 | + } |
|
189 | + } |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Convert the given size for the context of this current tag. |
|
194 | + * Takes a pixel-based reference, which is usually specific to the context of the size, |
|
195 | + * but the actual reference size will be decided based upon the unit used. |
|
196 | + * |
|
197 | + * @param string $size |
|
198 | + * @param float $pxReference |
|
199 | + * |
|
200 | + * @return float |
|
201 | + */ |
|
202 | + protected function convertSize(string $size, float $pxReference): float |
|
203 | + { |
|
204 | + $length = new CssLength($size); |
|
205 | + $reference = $pxReference; |
|
206 | + $defaultFontSize = 12; |
|
207 | + |
|
208 | + switch ($length->getUnit()) { |
|
209 | + case "em": |
|
210 | + $reference = $this->style->fontSize ?? $defaultFontSize; |
|
211 | + break; |
|
212 | + case "rem": |
|
213 | + $reference = $this->document->style->fontSize ?? $defaultFontSize; |
|
214 | + break; |
|
215 | + case "ex": |
|
216 | + case "ch": |
|
217 | + $emRef = $this->style->fontSize ?? $defaultFontSize; |
|
218 | + $reference = $emRef * 0.5; |
|
219 | + break; |
|
220 | + case "vw": |
|
221 | + $reference = $this->getDocument()->getWidth(); |
|
222 | + break; |
|
223 | + case "vh": |
|
224 | + $reference = $this->getDocument()->getHeight(); |
|
225 | + break; |
|
226 | + case "vmin": |
|
227 | + $reference = min($this->getDocument()->getHeight(), $this->getDocument()->getWidth()); |
|
228 | + break; |
|
229 | + case "vmax": |
|
230 | + $reference = max($this->getDocument()->getHeight(), $this->getDocument()->getWidth()); |
|
231 | + break; |
|
232 | + } |
|
233 | + |
|
234 | + return (new CssLength($size))->toPixels($reference); |
|
235 | + } |
|
236 | 236 | } |
@@ -12,31 +12,31 @@ |
||
12 | 12 | |
13 | 13 | class Ellipse extends Shape |
14 | 14 | { |
15 | - protected $cx = 0; |
|
16 | - protected $cy = 0; |
|
17 | - protected $rx = 0; |
|
18 | - protected $ry = 0; |
|
15 | + protected $cx = 0; |
|
16 | + protected $cy = 0; |
|
17 | + protected $rx = 0; |
|
18 | + protected $ry = 0; |
|
19 | 19 | |
20 | - public function start($attributes) |
|
21 | - { |
|
22 | - parent::start($attributes); |
|
20 | + public function start($attributes) |
|
21 | + { |
|
22 | + parent::start($attributes); |
|
23 | 23 | |
24 | - $width = $this->document->getWidth(); |
|
25 | - $height = $this->document->getHeight(); |
|
24 | + $width = $this->document->getWidth(); |
|
25 | + $height = $this->document->getHeight(); |
|
26 | 26 | |
27 | - if (isset($attributes['cx'])) { |
|
28 | - $this->cx = $this->convertSize($attributes['cx'], $width); |
|
29 | - } |
|
30 | - if (isset($attributes['cy'])) { |
|
31 | - $this->cy = $this->convertSize($attributes['cy'], $height); |
|
32 | - } |
|
33 | - if (isset($attributes['rx'])) { |
|
34 | - $this->rx = $this->convertSize($attributes['rx'], $width); |
|
35 | - } |
|
36 | - if (isset($attributes['ry'])) { |
|
37 | - $this->ry = $this->convertSize($attributes['ry'], $height); |
|
38 | - } |
|
27 | + if (isset($attributes['cx'])) { |
|
28 | + $this->cx = $this->convertSize($attributes['cx'], $width); |
|
29 | + } |
|
30 | + if (isset($attributes['cy'])) { |
|
31 | + $this->cy = $this->convertSize($attributes['cy'], $height); |
|
32 | + } |
|
33 | + if (isset($attributes['rx'])) { |
|
34 | + $this->rx = $this->convertSize($attributes['rx'], $width); |
|
35 | + } |
|
36 | + if (isset($attributes['ry'])) { |
|
37 | + $this->ry = $this->convertSize($attributes['ry'], $height); |
|
38 | + } |
|
39 | 39 | |
40 | - $this->document->getSurface()->ellipse($this->cx, $this->cy, $this->rx, $this->ry, 0, 0, 360, false); |
|
41 | - } |
|
40 | + $this->document->getSurface()->ellipse($this->cx, $this->cy, $this->rx, $this->ry, 0, 0, 360, false); |
|
41 | + } |
|
42 | 42 | } |
@@ -12,25 +12,25 @@ |
||
12 | 12 | |
13 | 13 | class Circle extends Shape |
14 | 14 | { |
15 | - protected $cx = 0; |
|
16 | - protected $cy = 0; |
|
17 | - protected $r; |
|
15 | + protected $cx = 0; |
|
16 | + protected $cy = 0; |
|
17 | + protected $r; |
|
18 | 18 | |
19 | - public function start($attributes) |
|
20 | - { |
|
21 | - if (isset($attributes['cx'])) { |
|
22 | - $width = $this->document->getWidth(); |
|
23 | - $this->cx = $this->convertSize($attributes['cx'], $width); |
|
24 | - } |
|
25 | - if (isset($attributes['cy'])) { |
|
26 | - $height = $this->document->getHeight(); |
|
27 | - $this->cy = $this->convertSize($attributes['cy'], $height); |
|
28 | - } |
|
29 | - if (isset($attributes['r'])) { |
|
30 | - $diagonal = $this->document->getDiagonal(); |
|
31 | - $this->r = $this->convertSize($attributes['r'], $diagonal); |
|
32 | - } |
|
19 | + public function start($attributes) |
|
20 | + { |
|
21 | + if (isset($attributes['cx'])) { |
|
22 | + $width = $this->document->getWidth(); |
|
23 | + $this->cx = $this->convertSize($attributes['cx'], $width); |
|
24 | + } |
|
25 | + if (isset($attributes['cy'])) { |
|
26 | + $height = $this->document->getHeight(); |
|
27 | + $this->cy = $this->convertSize($attributes['cy'], $height); |
|
28 | + } |
|
29 | + if (isset($attributes['r'])) { |
|
30 | + $diagonal = $this->document->getDiagonal(); |
|
31 | + $this->r = $this->convertSize($attributes['r'], $diagonal); |
|
32 | + } |
|
33 | 33 | |
34 | - $this->document->getSurface()->circle($this->cx, $this->cy, $this->r); |
|
35 | - } |
|
34 | + $this->document->getSurface()->circle($this->cx, $this->cy, $this->r); |
|
35 | + } |
|
36 | 36 | } |
@@ -12,61 +12,61 @@ |
||
12 | 12 | |
13 | 13 | class Text extends Shape |
14 | 14 | { |
15 | - protected $x = 0; |
|
16 | - protected $y = 0; |
|
17 | - protected $text = ""; |
|
15 | + protected $x = 0; |
|
16 | + protected $y = 0; |
|
17 | + protected $text = ""; |
|
18 | 18 | |
19 | - public function start($attributes) |
|
20 | - { |
|
21 | - $height = $this->document->getHeight(); |
|
22 | - $this->y = $height; |
|
19 | + public function start($attributes) |
|
20 | + { |
|
21 | + $height = $this->document->getHeight(); |
|
22 | + $this->y = $height; |
|
23 | 23 | |
24 | - if (isset($attributes['x'])) { |
|
25 | - $width = $this->document->getWidth(); |
|
26 | - $this->x = $this->convertSize($attributes['x'], $width); |
|
27 | - } |
|
28 | - if (isset($attributes['y'])) { |
|
29 | - $this->y = $height - $this->convertSize($attributes['y'], $height); |
|
30 | - } |
|
24 | + if (isset($attributes['x'])) { |
|
25 | + $width = $this->document->getWidth(); |
|
26 | + $this->x = $this->convertSize($attributes['x'], $width); |
|
27 | + } |
|
28 | + if (isset($attributes['y'])) { |
|
29 | + $this->y = $height - $this->convertSize($attributes['y'], $height); |
|
30 | + } |
|
31 | 31 | |
32 | - $this->document->getSurface()->transform(1, 0, 0, -1, 0, $height); |
|
33 | - } |
|
32 | + $this->document->getSurface()->transform(1, 0, 0, -1, 0, $height); |
|
33 | + } |
|
34 | 34 | |
35 | - public function end() |
|
36 | - { |
|
37 | - $surface = $this->document->getSurface(); |
|
38 | - $x = $this->x; |
|
39 | - $y = $this->y; |
|
40 | - $style = $surface->getStyle(); |
|
41 | - $surface->setFont($style->fontFamily, $style->fontStyle, $style->fontWeight); |
|
35 | + public function end() |
|
36 | + { |
|
37 | + $surface = $this->document->getSurface(); |
|
38 | + $x = $this->x; |
|
39 | + $y = $this->y; |
|
40 | + $style = $surface->getStyle(); |
|
41 | + $surface->setFont($style->fontFamily, $style->fontStyle, $style->fontWeight); |
|
42 | 42 | |
43 | - switch ($style->textAnchor) { |
|
44 | - case "middle": |
|
45 | - $width = $surface->measureText($this->text); |
|
46 | - $x -= $width / 2; |
|
47 | - break; |
|
43 | + switch ($style->textAnchor) { |
|
44 | + case "middle": |
|
45 | + $width = $surface->measureText($this->text); |
|
46 | + $x -= $width / 2; |
|
47 | + break; |
|
48 | 48 | |
49 | - case "end": |
|
50 | - $width = $surface->measureText($this->text); |
|
51 | - $x -= $width; |
|
52 | - break; |
|
53 | - } |
|
49 | + case "end": |
|
50 | + $width = $surface->measureText($this->text); |
|
51 | + $x -= $width; |
|
52 | + break; |
|
53 | + } |
|
54 | 54 | |
55 | - $surface->fillText($this->getText(), $x, $y); |
|
56 | - } |
|
55 | + $surface->fillText($this->getText(), $x, $y); |
|
56 | + } |
|
57 | 57 | |
58 | - protected function after() |
|
59 | - { |
|
60 | - $this->document->getSurface()->restore(); |
|
61 | - } |
|
58 | + protected function after() |
|
59 | + { |
|
60 | + $this->document->getSurface()->restore(); |
|
61 | + } |
|
62 | 62 | |
63 | - public function appendText($text) |
|
64 | - { |
|
65 | - $this->text .= $text; |
|
66 | - } |
|
63 | + public function appendText($text) |
|
64 | + { |
|
65 | + $this->text .= $text; |
|
66 | + } |
|
67 | 67 | |
68 | - public function getText() |
|
69 | - { |
|
70 | - return trim($this->text); |
|
71 | - } |
|
68 | + public function getText() |
|
69 | + { |
|
70 | + return trim($this->text); |
|
71 | + } |
|
72 | 72 | } |