1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.css;
16
17 import static org.htmlunit.css.CssStyleSheet.AUTO;
18
19 import java.util.function.Function;
20 import java.util.function.IntSupplier;
21 import java.util.regex.Pattern;
22
23 import org.htmlunit.html.DomElement;
24 import org.htmlunit.html.DomNode;
25 import org.htmlunit.html.HtmlCanvas;
26 import org.htmlunit.html.HtmlHtml;
27 import org.htmlunit.util.StringUtils;
28
29
30
31
32
33
34 public final class CssPixelValueConverter {
35
36 private static final Pattern TO_FLOAT_PATTERN = Pattern.compile("(\\d+(?:\\.\\d+)?).*");
37
38
39
40
41 private CssPixelValueConverter() {
42
43 }
44
45
46
47
48
49
50
51
52
53
54
55
56 public static int pixelValue(
57 final DomElement element,
58 final Function<ComputedCssStyleDeclaration, String> styleGetter,
59 final IntSupplier defaultValue,
60 final IntSupplier windowDefaultValue) {
61 final ComputedCssStyleDeclaration style =
62 element.getPage().getEnclosingWindow().getComputedStyle(element, null);
63 final String s = styleGetter.apply(style);
64 return pixelValue(element, s, styleGetter, defaultValue, windowDefaultValue, false);
65 }
66
67
68
69
70
71
72
73
74
75
76
77
78 public static String pixelString(
79 final DomElement element,
80 final Function<ComputedCssStyleDeclaration, String> styleGetter,
81 final IntSupplier defaultValue,
82 final IntSupplier windowDefaultValue) {
83 final ComputedCssStyleDeclaration style =
84 element.getPage().getEnclosingWindow().getComputedStyle(element, null);
85 final String styleValue = styleGetter.apply(style);
86 if (styleValue.endsWith("px")) {
87 return styleValue;
88 }
89 return pixelValue(element, styleValue, styleGetter, defaultValue, windowDefaultValue, false) + "px";
90 }
91
92
93
94
95
96
97
98
99
100
101 public static int pixelValue(final String value) {
102 if (CssMathFunctionEvaluator.isMathFunction(value)) {
103
104 final Double result = CssMathFunctionEvaluator.evaluate(value, null, () -> 16);
105 return result == null ? 0 : (int) Math.round(result);
106 }
107
108 final float i = StringUtils.toFloat(TO_FLOAT_PATTERN.matcher(value).replaceAll("$1"), 0);
109 if (value.length() < 2) {
110 return Math.round(i);
111 }
112 return (int) Math.round(toPixels(i, value));
113 }
114
115
116
117
118
119
120
121 static double toPixels(final double number, final String unit) {
122 if (unit.endsWith("px")) {
123 return number;
124 }
125 if (unit.endsWith("em")) {
126 return number * 16;
127 }
128 if (unit.endsWith("%")) {
129 return number * 16 / 100;
130 }
131 if (unit.endsWith("ex")) {
132 return number * 8;
133 }
134 if (unit.endsWith("cm")) {
135 return number * 38;
136 }
137 if (unit.endsWith("mm")) {
138 return number * 4;
139 }
140 if (unit.endsWith("pt")) {
141 return number * 2;
142 }
143 if (unit.endsWith("pc")) {
144 return number * 24;
145 }
146 if (unit.endsWith("ch")) {
147 return number * 8;
148 }
149 if (unit.endsWith("vh")
150 || unit.endsWith("vmin")) {
151
152
153 return number * 6;
154 }
155 if (unit.endsWith("vw")
156 || unit.endsWith("vmax")) {
157
158
159 return number * 12;
160 }
161
162 if (unit.endsWith("in")) {
163 return number * 150;
164 }
165 return number;
166 }
167
168 private static int pixelValue(final DomElement element,
169 final String styleValue,
170 final Function<ComputedCssStyleDeclaration, String> styleGetter,
171 final IntSupplier defaultValue,
172 final IntSupplier windowDefaultValue,
173 final boolean percentMode) {
174 if (CssMathFunctionEvaluator.isMathFunction(styleValue)) {
175 final Double result = CssMathFunctionEvaluator.evaluate(styleValue, element.getPage().getWebClient(),
176 () -> percentBase(element, styleGetter, defaultValue, windowDefaultValue));
177 return result == null ? 0 : (int) Math.round(result);
178 }
179
180 if (styleValue.endsWith("%") || (styleValue.isEmpty() && element instanceof HtmlHtml)) {
181 final float i = StringUtils.toFloat(TO_FLOAT_PATTERN.matcher(styleValue).replaceAll("$1"), 100);
182 final int absoluteValue = percentBase(element, styleGetter, defaultValue, windowDefaultValue);
183 return Math.round((i / 100f) * absoluteValue);
184 }
185 if (AUTO.equals(styleValue)) {
186 return defaultValue.getAsInt();
187 }
188 if (styleValue.isEmpty()) {
189 if (element instanceof HtmlCanvas) {
190 return windowDefaultValue.getAsInt();
191 }
192
193
194
195 if (percentMode) {
196 final DomNode parent = element.getParentNode();
197 if (parent == null || parent instanceof HtmlHtml) {
198 return windowDefaultValue.getAsInt();
199 }
200 final DomElement parentElem = (DomElement) parent;
201 final ComputedCssStyleDeclaration style =
202 parentElem.getPage().getEnclosingWindow().getComputedStyle(parentElem, null);
203 final String parentStyleValue = styleGetter.apply(style);
204 return pixelValue(parentElem, parentStyleValue, styleGetter, defaultValue, windowDefaultValue, true);
205 }
206
207 return 0;
208 }
209 return pixelValue(styleValue);
210 }
211
212
213
214
215
216 private static int percentBase(final DomElement element,
217 final Function<ComputedCssStyleDeclaration, String> styleGetter,
218 final IntSupplier defaultValue,
219 final IntSupplier windowDefaultValue) {
220 final DomNode parent = element.getParentNode();
221 if (parent instanceof DomElement parentElem) {
222 final ComputedCssStyleDeclaration style =
223 parentElem.getPage().getEnclosingWindow().getComputedStyle(parentElem, null);
224 final String parentStyleValue = styleGetter.apply(style);
225 return pixelValue(parentElem, parentStyleValue, styleGetter,
226 defaultValue, windowDefaultValue, true);
227 }
228 return windowDefaultValue.getAsInt();
229 }
230 }