Skip to content

Commit

Permalink
Merge pull request #448 from danfickle/fix_439_linear_gradient
Browse files Browse the repository at this point in the history
Fixes #439 linear gradient support for PDF output
  • Loading branch information
danfickle authored Feb 24, 2020
2 parents 941e465 + 79bf73c commit 854e03e
Show file tree
Hide file tree
Showing 12 changed files with 671 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ protected void checkIdentLengthNumberOrPercentType(CSSName cssName, CSSPrimitive
}

protected boolean isLength(CSSPrimitiveValue value) {
return isLengthHelper(value);
}

public static boolean isLengthHelper(CSSPrimitiveValue value) {
int unit = value.getPrimitiveType();
return unit == CSSPrimitiveValue.CSS_EMS || unit == CSSPrimitiveValue.CSS_EXS
|| unit == CSSPrimitiveValue.CSS_PX || unit == CSSPrimitiveValue.CSS_IN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,24 @@ public static class BackgroundColor extends GenericColor {
}

public static class BackgroundImage extends GenericURIWithNone {
@Override
public List<PropertyDeclaration> buildDeclarations(
CSSName cssName, List<PropertyValue> values, int origin,
boolean important, boolean inheritAllowed) {

checkValueCount(cssName, 1, values.size());
PropertyValue value = values.get(0);

if (value.getPropertyValueType() == PropertyValue.VALUE_TYPE_FUNCTION &&
Objects.equals(value.getFunction().getName(), "linear-gradient")) {
// TODO: Validation of linear-gradient args.
return Collections.singletonList(
new PropertyDeclaration(cssName, value, important, origin));
} else {
return super.buildDeclarations(cssName, values, origin, important, inheritAllowed);
}
}

}

public static class BackgroundSize extends AbstractPropertyBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.awt.Cursor;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.stream.Collectors;

Expand All @@ -39,6 +40,7 @@
import com.openhtmltopdf.css.style.derived.BorderPropertySet;
import com.openhtmltopdf.css.style.derived.CountersValue;
import com.openhtmltopdf.css.style.derived.DerivedValueFactory;
import com.openhtmltopdf.css.style.derived.FSLinearGradient;
import com.openhtmltopdf.css.style.derived.FunctionValue;
import com.openhtmltopdf.css.style.derived.LengthValue;
import com.openhtmltopdf.css.style.derived.ListValue;
Expand Down Expand Up @@ -1393,9 +1395,22 @@ public static int getCSSMaxHeight(CssContext c, Box box) {
return (int) cssMaxHeight.value();
}
}


}// end class

public boolean isLinearGradient() {
FSDerivedValue value = valueByName(CSSName.BACKGROUND_IMAGE);
return value instanceof FunctionValue &&
Objects.equals(((FunctionValue) value).getFunction().getName(), "linear-gradient");
}

public FSLinearGradient getLinearGradient(CssContext cssContext, int boxWidth, int boxHeight) {
if (!isLinearGradient()) {
return null;
}

FunctionValue value = (FunctionValue) valueByName(CSSName.BACKGROUND_IMAGE);
return new FSLinearGradient(this, value.getFunction(), boxWidth, boxHeight, cssContext);
}
}

/*
* $Id$
Expand Down
Loading

0 comments on commit 854e03e

Please sign in to comment.