@@ -26,10 +26,22 @@ This file is part of the iText (R) project.
2626import com .itextpdf .html2pdf .css .CssConstants ;
2727import com .itextpdf .html2pdf .logs .Html2PdfLogMessageConstant ;
2828import com .itextpdf .layout .IPropertyContainer ;
29- import com .itextpdf .layout .properties .GridFlow ;
30- import com .itextpdf .layout .properties .GridValue ;
29+ import com .itextpdf .layout .properties .grid .AutoRepeatValue ;
30+ import com .itextpdf .layout .properties .grid .BreadthValue ;
31+ import com .itextpdf .layout .properties .grid .FixedRepeatValue ;
32+ import com .itextpdf .layout .properties .grid .GridFlow ;
3133import com .itextpdf .layout .properties .Property ;
3234import com .itextpdf .layout .properties .UnitValue ;
35+ import com .itextpdf .layout .properties .grid .AutoValue ;
36+ import com .itextpdf .layout .properties .grid .FitContentValue ;
37+ import com .itextpdf .layout .properties .grid .FlexValue ;
38+ import com .itextpdf .layout .properties .grid .GridValue ;
39+ import com .itextpdf .layout .properties .grid .MaxContentValue ;
40+ import com .itextpdf .layout .properties .grid .MinContentValue ;
41+ import com .itextpdf .layout .properties .grid .MinMaxValue ;
42+ import com .itextpdf .layout .properties .grid .PercentValue ;
43+ import com .itextpdf .layout .properties .grid .PointValue ;
44+ import com .itextpdf .layout .properties .grid .TemplateValue ;
3345import com .itextpdf .styledxmlparser .css .CommonCssConstants ;
3446import com .itextpdf .styledxmlparser .css .util .CssDimensionParsingUtils ;
3547import com .itextpdf .styledxmlparser .css .util .CssUtils ;
@@ -81,7 +93,7 @@ private GridApplierUtil() {
8193 * @param element the element
8294 */
8395 public static void applyGridItemProperties (Map <String , String > cssProps , IStylesContainer stylesContainer ,
84- IPropertyContainer element ) {
96+ IPropertyContainer element ) {
8597 if (!(stylesContainer instanceof JsoupElementNode )
8698 || !(((JsoupElementNode ) stylesContainer ).parentNode () instanceof JsoupElementNode )) {
8799 return ;
@@ -127,7 +139,7 @@ public static void applyGridItemProperties(Map<String, String> cssProps, IStyles
127139 applyGridItemPlacement (cssProps .get (CssConstants .GRID_ROW_END ), element , Property .GRID_ROW_END , Property .GRID_ROW_SPAN );
128140 applyGridItemPlacement (cssProps .get (CssConstants .GRID_ROW_START ), element , Property .GRID_ROW_START , Property .GRID_ROW_SPAN );
129141 }
130-
142+
131143 private static void applyGridItemPlacement (String value , IPropertyContainer element , int property , int spanProperty ) {
132144 if (value == null ) {
133145 return ;
@@ -154,7 +166,7 @@ private static void applyGridItemPlacement(String value, IPropertyContainer elem
154166 * @param context the context
155167 */
156168 public static void applyGridContainerProperties (Map <String , String > cssProps , IPropertyContainer container ,
157- ProcessorContext context ) {
169+ ProcessorContext context ) {
158170
159171 final float emValue = CssDimensionParsingUtils .parseAbsoluteFontSize (cssProps .get (CssConstants .FONT_SIZE ));
160172 final float remValue = context .getCssContext ().getRootFontSize ();
@@ -182,7 +194,7 @@ public static void applyGridContainerProperties(Map<String, String> cssProps, IP
182194
183195 private static void applyAuto (String autoStr , IPropertyContainer container , int property , float emValue , float remValue ) {
184196 if (autoStr != null ) {
185- GridValue value = getGridValue (autoStr , emValue , remValue );
197+ TemplateValue value = parseTemplateValue (autoStr , emValue , remValue );
186198 if (value != null ) {
187199 container .setProperty (property , value );
188200 }
@@ -208,9 +220,9 @@ private static void applyFlow(String flow, IPropertyContainer container) {
208220 private static void applyTemplate (String templateStr , IPropertyContainer container , int property , float emValue , float remValue ) {
209221 if (templateStr != null ) {
210222 final List <String > templateStrArray = CssUtils .extractShorthandProperties (templateStr ).get (0 );
211- final List <GridValue > templateResult = new ArrayList <>();
223+ final List <TemplateValue > templateResult = new ArrayList <>();
212224 for (String str : templateStrArray ) {
213- GridValue value = getGridValue (str , emValue , remValue );
225+ TemplateValue value = parseTemplateValue (str , emValue , remValue );
214226 if (value != null ) {
215227 templateResult .add (value );
216228 }
@@ -221,26 +233,101 @@ private static void applyTemplate(String templateStr, IPropertyContainer contain
221233 }
222234 }
223235
224- private static GridValue getGridValue (String str , float emValue , float remValue ) {
236+ private static TemplateValue parseTemplateValue (String str , float emValue , float remValue ) {
237+ if (str == null ) {
238+ return null ;
239+ }
225240 final UnitValue unit = CssDimensionParsingUtils .parseLengthValueToPt (str , emValue , remValue );
226241 if (unit != null ) {
227242 if (unit .isPointValue ()) {
228- return GridValue . createPointValue (unit .getValue ());
243+ return new PointValue (unit .getValue ());
229244 } else {
230- return GridValue . createPercentValue (unit .getValue ());
245+ return new PercentValue (unit .getValue ());
231246 }
232- } else if (CommonCssConstants .MIN_CONTENT .equals (str )) {
233- return GridValue .createMinContentValue ();
234- } else if (CommonCssConstants .MAX_CONTENT .equals (str )) {
235- return GridValue .createMaxContentValue ();
236- } else if (CommonCssConstants .AUTO .equals (str )) {
237- return GridValue .createAutoValue ();
247+ }
248+ if (CommonCssConstants .MIN_CONTENT .equals (str )) {
249+ return MinContentValue .VALUE ;
250+ }
251+ if (CommonCssConstants .MAX_CONTENT .equals (str )) {
252+ return MaxContentValue .VALUE ;
253+ }
254+ if (CommonCssConstants .AUTO .equals (str )) {
255+ return AutoValue .VALUE ;
238256 }
239257 final Float fr = CssDimensionParsingUtils .parseFlex (str );
240258 if (fr != null ) {
241- return GridValue .createFlexValue ((float ) fr );
259+ return new FlexValue ((float ) fr );
260+ }
261+ if (determineFunction (str , CommonCssConstants .FIT_CONTENT )) {
262+ return parseFitContent (str , emValue , remValue );
263+ }
264+ if (determineFunction (str , CommonCssConstants .REPEAT )) {
265+ return parseRepeat (str , emValue , remValue );
266+ }
267+ if (determineFunction (str , CssConstants .MINMAX )) {
268+ return parseMinMax (str , emValue , remValue );
269+ }
270+ return null ;
271+ }
272+
273+ private static FitContentValue parseFitContent (String str , float emValue , float remValue ) {
274+ UnitValue length = CssDimensionParsingUtils .parseLengthValueToPt (
275+ str .substring (CommonCssConstants .FIT_CONTENT .length () + 1 , str .length () - 1 ), emValue , remValue );
276+ if (length == null ) {
277+ return null ;
278+ }
279+ return new FitContentValue (length );
280+ }
281+
282+ private static boolean determineFunction (String str , String function ) {
283+ return str .startsWith (function )
284+ && str .length () > function .length () + 2 ;
285+ }
286+
287+ private static TemplateValue parseMinMax (String str , float emValue , float remValue ) {
288+ int parameterSeparator = str .indexOf (',' );
289+ if (parameterSeparator < 0 ) {
290+ return null ;
242291 }
292+ TemplateValue min = parseTemplateValue (str .substring (CssConstants .MINMAX .length () + 1 , parameterSeparator ).trim (), emValue , remValue );
293+ TemplateValue max = parseTemplateValue (str .substring (parameterSeparator + 1 , str .length () - 1 ).trim (), emValue , remValue );
294+ if (!(min instanceof BreadthValue ) || !(max instanceof BreadthValue )) {
295+ return null ;
296+ }
297+ return new MinMaxValue ((BreadthValue )min , (BreadthValue )max );
298+ }
243299
300+ private static TemplateValue parseRepeat (String str , float emValue , float remValue ) {
301+ List <GridValue > repeatList = new ArrayList <>();
302+ int repeatCount = -1 ;
303+ int repeatTypeEndIndex = str .indexOf (',' );
304+ if (repeatTypeEndIndex < 0 ) {
305+ return null ;
306+ }
307+ String repeatType = str .substring (CommonCssConstants .REPEAT .length () + 1 , repeatTypeEndIndex ).trim ();
308+ try {
309+ repeatCount = Integer .parseInt (repeatType );
310+ } catch (NumberFormatException ex ) {
311+ //do nothing
312+ }
313+
314+ List <String > repeatStr = CssUtils .extractShorthandProperties (
315+ str .substring (repeatTypeEndIndex + 1 , str .length () - 1 )).get (0 );
316+ for (String strValue : repeatStr ) {
317+ TemplateValue value = parseTemplateValue (strValue , emValue , remValue );
318+ if (value instanceof GridValue ) {
319+ repeatList .add ((GridValue ) value );
320+ } else {
321+ return null ;
322+ }
323+ }
324+ if (repeatCount > 0 ) {
325+ return new FixedRepeatValue (repeatCount , repeatList );
326+ } else if (CssConstants .AUTO_FILL .equals (repeatType )) {
327+ return new AutoRepeatValue (false , repeatList );
328+ } else if (CssConstants .AUTO_FIT .equals (repeatType )) {
329+ return new AutoRepeatValue (true , repeatList );
330+ }
244331 return null ;
245332 }
246333
0 commit comments