22
33import java .util .ArrayList ;
44import java .util .List ;
5- import java .util .Objects ;
6-
75import com .openhtmltopdf .css .constants .CSSName ;
8- import com .openhtmltopdf .css .constants .Idents ;
96import com .openhtmltopdf .css .parser .CSSPrimitiveValue ;
107import com .openhtmltopdf .css .parser .FSColor ;
118import com .openhtmltopdf .css .parser .FSFunction ;
129import com .openhtmltopdf .css .parser .PropertyValue ;
13- import com .openhtmltopdf .css .parser .property .AbstractPropertyBuilder ;
1410import com .openhtmltopdf .css .parser .property .Conversions ;
1511import com .openhtmltopdf .css .style .CalculatedStyle ;
1612import com .openhtmltopdf .css .style .CssContext ;
@@ -62,9 +58,9 @@ public String toString() {
6258
6359 public FSLinearGradient (CalculatedStyle style , FSFunction function , int boxWidth , int boxHeight , CssContext ctx ) {
6460 List <PropertyValue > params = function .getParameters ();
65- int stopsStartIndex = getStopsStartIndex (params );
61+ int stopsStartIndex = FSLinearGradientUtil . getStopsStartIndex (params );
6662
67- float prelimAngle = calculateAngle (params , stopsStartIndex );
63+ float prelimAngle = FSLinearGradientUtil . calculateAngle (params , stopsStartIndex );
6864 prelimAngle = prelimAngle % 360f ;
6965 if (prelimAngle < 0 ) {
7066 prelimAngle += 360f ;
@@ -75,10 +71,6 @@ public FSLinearGradient(CalculatedStyle style, FSFunction function, int boxWidth
7571 endPointsFromAngle (_angle , boxWidth , boxHeight );
7672 }
7773
78- private float deg2rad (final float deg ) {
79- return (float ) Math .toRadians (deg );
80- }
81-
8274 // Compute the endpoints so that a gradient of the given angle
8375 // covers a box of the given size.
8476 // From: https://github.com/WebKit/webkit/blob/master/Source/WebCore/css/CSSGradientValue.cpp
@@ -121,7 +113,7 @@ private void endPointsFromAngle(float angleDeg, final int w, final int h) {
121113
122114 // angleDeg is a "bearing angle" (0deg = N, 90deg = E),
123115 // but tan expects 0deg = E, 90deg = N.
124- final float slope = (float ) Math .tan (deg2rad (90 - angleDeg ));
116+ final float slope = (float ) Math .tan (FSLinearGradientUtil . deg2rad (90 - angleDeg ));
125117
126118 // We find the endpoint by computing the intersection of the line formed by the
127119 // slope,
@@ -163,11 +155,6 @@ private void endPointsFromAngle(float angleDeg, final int w, final int h) {
163155 y1 = (int ) (halfHeight + endY );
164156 }
165157
166- private boolean isLengthOrPercentage (PropertyValue value ) {
167- return AbstractPropertyBuilder .isLengthHelper (value ) ||
168- value .getPrimitiveType () == CSSPrimitiveValue .CSS_PERCENTAGE ;
169- }
170-
171158 private List <StopPoint > calculateStopPoints (
172159 List <PropertyValue > params , CalculatedStyle style , CssContext ctx , float boxWidth , int stopsStartIndex ) {
173160
@@ -183,7 +170,7 @@ private List<StopPoint> calculateStopPoints(
183170 color = value .getFSColor ();
184171 }
185172
186- if (i + 1 < params .size () && isLengthOrPercentage (params .get (i + 1 ))) {
173+ if (i + 1 < params .size () && FSLinearGradientUtil . isLengthOrPercentage (params .get (i + 1 ))) {
187174
188175 PropertyValue lengthValue = params .get (i + 1 );
189176 float length = LengthValue .calcFloatProportionalValue (style , CSSName .BACKGROUND_IMAGE , "" ,
@@ -207,7 +194,7 @@ private List<StopPoint> calculateStopPoints(
207194 } else if (i == 0 ) {
208195 ret .add (new StopPoint (pt .getColor (), 0f ));
209196 } else if (i == points .size () - 1 ) {
210- float len = get100PercentDefaultStopLength (style , ctx , boxWidth );
197+ float len = FSLinearGradientUtil . get100PercentDefaultStopLength (style , ctx , boxWidth );
211198 ret .add (new StopPoint (pt .getColor (), len ));
212199 } else {
213200 // Poo, we've got a length-less stop in the middle.
@@ -219,7 +206,7 @@ private List<StopPoint> calculateStopPoints(
219206 int prevWithLengthIndex = getPrevStopPointWithLengthIndex (points , i - 1 );
220207
221208 float nextLength = nextWithLengthIndex == -1 ?
222- get100PercentDefaultStopLength (style , ctx , boxWidth ) :
209+ FSLinearGradientUtil . get100PercentDefaultStopLength (style , ctx , boxWidth ) :
223210 ((StopPoint ) points .get (nextWithLengthIndex )).getLength ();
224211
225212 float prevLength = prevWithLengthIndex == -1 ? 0 :
@@ -254,11 +241,6 @@ private int getPrevStopPointWithLengthIndex(List<IntermediateStopPoint> points,
254241 return -1 ;
255242 }
256243
257- private float get100PercentDefaultStopLength (CalculatedStyle style , CssContext ctx , float boxWidth ) {
258- return LengthValue .calcFloatProportionalValue (style , CSSName .BACKGROUND_IMAGE , "100%" ,
259- 100f , CSSPrimitiveValue .CSS_PERCENTAGE , boxWidth , ctx );
260- }
261-
262244 private boolean isStopPointWithLength (IntermediateStopPoint pt ) {
263245 return pt .getClass () == StopPoint .class ;
264246 }
@@ -272,69 +254,6 @@ private int getNextStopPointWithLengthIndex(List<IntermediateStopPoint> points,
272254 return -1 ;
273255 }
274256
275- private int getStopsStartIndex (List <PropertyValue > params ) {
276- if (Objects .equals (params .get (0 ).getStringValue (), "to" )) {
277- int i = 1 ;
278- while (i < params .size () &&
279- params .get (i ).getStringValue () != null &&
280- Idents .looksLikeABGPosition (params .get (i ).getStringValue ())) {
281- i ++;
282- }
283-
284- return i ;
285- } else {
286- return 1 ;
287- }
288- }
289-
290- /**
291- * Calculates the angle of the linear gradient in degrees.
292- */
293- private float calculateAngle (List <PropertyValue > params , int stopsStartIndex ) {
294- if (Objects .equals (params .get (0 ).getStringValue (), "to" )) {
295- // The to keyword is followed by one or two position
296- // idents (in any order).
297- // linear-gradient( to left top, blue, red);
298- // linear-gradient( to top right, blue, red);
299- List <String > positions = new ArrayList <>(2 );
300-
301- for (int i = 1 ; i < stopsStartIndex ; i ++) {
302- positions .add (params .get (i ).getStringValue ());
303- }
304-
305- if (positions .contains ("top" ) && positions .contains ("left" ))
306- return 315f ;
307- else if (positions .contains ("top" ) && positions .contains ("right" ))
308- return 45f ;
309- else if (positions .contains ("bottom" ) && positions .contains ("left" ))
310- return 225f ;
311- else if (positions .contains ("bottom" ) && positions .contains ("right" ))
312- return 135f ;
313- else if (positions .contains ("bottom" ))
314- return 180f ;
315- else if (positions .contains ("left" ))
316- return 270f ;
317- else if (positions .contains ("right" ))
318- return 90f ;
319- else
320- return 0f ;
321- }
322- else if (params .get (0 ).getPrimitiveType () == CSSPrimitiveValue .CSS_DEG )
323- {
324- // linear-gradient(45deg, ...)
325- return params .get (0 ).getFloatValue ();
326- }
327- else if (params .get (0 ).getPrimitiveType () == CSSPrimitiveValue .CSS_RAD )
328- {
329- // linear-gradient(2rad)
330- return params .get (0 ).getFloatValue () * (float ) (180 / Math .PI );
331- }
332- else
333- {
334- return 0f ;
335- }
336- }
337-
338257 public List <StopPoint > getStopPoints () {
339258 return _stopPoints ;
340259 }
0 commit comments