From 0ad58c5253a675fa73928c371d555a41f958f93d Mon Sep 17 00:00:00 2001 From: danfickle Date: Sun, 14 Feb 2021 22:58:05 +1100 Subject: [PATCH] #649 Multiple values for background-repeat property --- .../property/BackgroundPropertyBuilder.java | 4 ++-- .../PrimitiveBackgroundPropertyBuilders.java | 11 ++++++++++- .../css/style/CalculatedStyle.java | 17 +++++++++-------- .../render/AbstractOutputDevice.java | 4 ++-- .../issue-649-multiple-bg-images-advanced.html | 8 ++++++++ 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/parser/property/BackgroundPropertyBuilder.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/parser/property/BackgroundPropertyBuilder.java index 35ad7352f..addfc0bdb 100644 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/parser/property/BackgroundPropertyBuilder.java +++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/parser/property/BackgroundPropertyBuilder.java @@ -94,7 +94,7 @@ public List buildDeclarations( } backgroundRepeat = new PropertyDeclaration( - CSSName.BACKGROUND_REPEAT, value, important, origin); + CSSName.BACKGROUND_REPEAT, new PropertyValue(Collections.singletonList(value)), important, origin); } if (PrimitivePropertyBuilders.BACKGROUND_ATTACHMENTS.get(ident.FS_ID)) { @@ -182,7 +182,7 @@ public List buildDeclarations( if (backgroundRepeat == null) { backgroundRepeat = new PropertyDeclaration( - CSSName.BACKGROUND_REPEAT, new PropertyValue(IdentValue.REPEAT), important, origin); + CSSName.BACKGROUND_REPEAT, new PropertyValue(Collections.singletonList(new PropertyValue(IdentValue.REPEAT))), important, origin); } if (backgroundAttachment == null) { diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/parser/property/PrimitiveBackgroundPropertyBuilders.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/parser/property/PrimitiveBackgroundPropertyBuilders.java index caf01b673..1242c7aea 100644 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/parser/property/PrimitiveBackgroundPropertyBuilders.java +++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/parser/property/PrimitiveBackgroundPropertyBuilders.java @@ -304,8 +304,17 @@ private BitSet getAllowed() { } } - public static class BackgroundRepeat extends SingleIdent { + public static class BackgroundRepeat extends MultipleBackgroundValueBuilder { @Override + protected List processValue(CSSName cssName, PropertyValue value) { + checkIdentType(cssName, value); + IdentValue ident = checkIdent(cssName, value); + + checkValidity(cssName, getAllowed(), ident); + + return Collections.singletonList(value); + } + protected BitSet getAllowed() { return PrimitivePropertyBuilders.BACKGROUND_REPEATS; } diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/style/CalculatedStyle.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/style/CalculatedStyle.java index 776458e44..5519bf047 100644 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/style/CalculatedStyle.java +++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/style/CalculatedStyle.java @@ -791,8 +791,8 @@ public boolean isCleared() { return ! isIdent(CSSName.CLEAR, IdentValue.NONE); } - public IdentValue getBackgroundRepeat() { - return getIdent(CSSName.BACKGROUND_REPEAT); + public IdentValue getBackgroundRepeat(PropertyValue value) { + return value.getIdentValue(); } public IdentValue getBackgroundAttachment() { @@ -1011,14 +1011,12 @@ public boolean isOverflowVisible() { return valueByName(CSSName.OVERFLOW) == IdentValue.VISIBLE; } - public boolean isHorizontalBackgroundRepeat() { - IdentValue value = getIdent(CSSName.BACKGROUND_REPEAT); - return value == IdentValue.REPEAT_X || value == IdentValue.REPEAT; + public boolean isHorizontalBackgroundRepeat(PropertyValue value) { + return value.getIdentValue() == IdentValue.REPEAT_X || value.getIdentValue() == IdentValue.REPEAT; } - public boolean isVerticalBackgroundRepeat() { - IdentValue value = getIdent(CSSName.BACKGROUND_REPEAT); - return value == IdentValue.REPEAT_Y || value == IdentValue.REPEAT; + public boolean isVerticalBackgroundRepeat(PropertyValue value) { + return value.getIdentValue() == IdentValue.REPEAT_Y || value.getIdentValue() == IdentValue.REPEAT; } public boolean isTopAuto() { @@ -1412,6 +1410,7 @@ public static class BackgroundContainer { public PropertyValue imageGradientOrNone; public BackgroundPosition backgroundPosition; + public PropertyValue backgroundRepeat; } public boolean isLinearGradient(PropertyValue value) { @@ -1432,6 +1431,7 @@ public FSLinearGradient getLinearGradient( public List getBackgroundImages() { List images = ((ListValue) valueByName(CSSName.BACKGROUND_IMAGE)).getValues(); List positions = ((ListValue) valueByName(CSSName.BACKGROUND_POSITION)).getValues(); + List repeats = ((ListValue) valueByName(CSSName.BACKGROUND_REPEAT)).getValues(); assert positions.size() % 2 == 0; @@ -1459,6 +1459,7 @@ public List getBackgroundImages() { // If less background-position values are provided than images, // they must repeat. bg.backgroundPosition = posPairs.get(i % posPairs.size()); + bg.backgroundRepeat = repeats.get(i % repeats.size()); backgrounds.add(bg); } diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/render/AbstractOutputDevice.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/render/AbstractOutputDevice.java index e051517e2..5bc1a1445 100644 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/render/AbstractOutputDevice.java +++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/render/AbstractOutputDevice.java @@ -359,8 +359,8 @@ private void drawBgImage( xoff += calcOffset(c, style, position.getHorizontal(), localBGImageContainer.width, imageWidth); yoff += calcOffset(c, style, position.getVertical(), localBGImageContainer.height, imageHeight); - boolean hrepeat = style.isHorizontalBackgroundRepeat(); - boolean vrepeat = style.isVerticalBackgroundRepeat(); + boolean hrepeat = style.isHorizontalBackgroundRepeat(bgImage.backgroundRepeat); + boolean vrepeat = style.isVerticalBackgroundRepeat(bgImage.backgroundRepeat); if (!hrepeat && !vrepeat) { Rectangle imageBounds = new Rectangle(xoff, yoff, (int) imageWidth, (int) imageHeight); diff --git a/openhtmltopdf-examples/src/main/resources/visualtest/html/issue-649-multiple-bg-images-advanced.html b/openhtmltopdf-examples/src/main/resources/visualtest/html/issue-649-multiple-bg-images-advanced.html index 9504b0c1c..f7afc67c1 100644 --- a/openhtmltopdf-examples/src/main/resources/visualtest/html/issue-649-multiple-bg-images-advanced.html +++ b/openhtmltopdf-examples/src/main/resources/visualtest/html/issue-649-multiple-bg-images-advanced.html @@ -34,6 +34,12 @@ background-image: url(../../demos/images/cc0-cat.png), linear-gradient(to top, red, blue); background-position: 20% top; } +#five { + height: 200px; + background-image: url(../../demos/images/cc0-cat.png), url(../../demos/images/flyingsaucer.png), none; + background-repeat: no-repeat, repeat-y; + background-position: right; +} @@ -45,5 +51,7 @@
+
+