From 2bcf0041e385e5b15544c260b59b2fa0f49d222c Mon Sep 17 00:00:00 2001 From: Nitzan Jaitman Date: Sun, 8 Nov 2020 14:33:49 +0200 Subject: [PATCH 1/2] Add support for variables in text style. --- .../cloudinary/transformation/TextLayer.java | 16 ++++++++++++- .../com/cloudinary/test/CloudinaryTest.java | 24 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/cloudinary-core/src/main/java/com/cloudinary/transformation/TextLayer.java b/cloudinary-core/src/main/java/com/cloudinary/transformation/TextLayer.java index df2986c4..c911f705 100644 --- a/cloudinary-core/src/main/java/com/cloudinary/transformation/TextLayer.java +++ b/cloudinary-core/src/main/java/com/cloudinary/transformation/TextLayer.java @@ -21,6 +21,7 @@ public class TextLayer extends AbstractLayer { protected String letterSpacing = null; protected Integer lineSpacing = null; protected String text = null; + protected Object textStyle = null; @Override TextLayer getThis() { @@ -118,6 +119,16 @@ public TextLayer text(String text) { return getThis(); } + public TextLayer textStyle(String textStyleIdentifier) { + this.textStyle = textStyleIdentifier; + return getThis(); + } + + public TextLayer textStyle(Expression textStyleIdentifier) { + this.textStyle = textStyleIdentifier; + return getThis(); + } + @Override public String toString() { if (this.publicId == null && this.text == null) { @@ -144,6 +155,10 @@ public String toString() { } protected String textStyleIdentifier() { + if (StringUtils.isNotBlank(this.textStyle)) { + return textStyle.toString(); + } + ArrayList components = new ArrayList(); if (StringUtils.isNotBlank(this.fontWeight) && !this.fontWeight.equals("normal")) @@ -181,6 +196,5 @@ protected String textStyleIdentifier() { components.add(0, this.fontFamily); return StringUtils.join(components, "_"); - } } diff --git a/cloudinary-core/src/test/java/com/cloudinary/test/CloudinaryTest.java b/cloudinary-core/src/test/java/com/cloudinary/test/CloudinaryTest.java index 7202e993..e0875980 100644 --- a/cloudinary-core/src/test/java/com/cloudinary/test/CloudinaryTest.java +++ b/cloudinary-core/src/test/java/com/cloudinary/test/CloudinaryTest.java @@ -101,6 +101,30 @@ public void testSecureDistribution() { assertEquals("https://res.cloudinary.com/test123/image/upload/test", result); } + @Test + public void testTextLayerStyleIdentifierVariables() { + String url = cloudinary.url().transformation( + new Transformation() + .variable("$style", "!Arial_12!") + .chain() + .overlay( + new TextLayer().text("hello-world").textStyle("$style") + )).generate("sample"); + + assertEquals("http://res.cloudinary.com/test123/image/upload/$style_!Arial_12!/l_text:$style:hello-world/sample", url); + + url = cloudinary.url().transformation( + new Transformation() + .variable("$style", "!Arial_12!") + .chain() + .overlay( + new TextLayer().text("hello-world").textStyle(new Expression("$style")) + )).generate("sample"); + + assertEquals("http://res.cloudinary.com/test123/image/upload/$style_!Arial_12!/l_text:$style:hello-world/sample", url); + } + + @Test public void testSecureDistributionOverwrite() { // should allow overwriting secure distribution if secure=TRUE From 109c94aac613fb439ecc249d206dafd44fd8d922 Mon Sep 17 00:00:00 2001 From: Nitzan Jaitman Date: Mon, 9 Nov 2020 12:16:18 +0200 Subject: [PATCH 2/2] Add docstrings and comments. --- .../com/cloudinary/transformation/TextLayer.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cloudinary-core/src/main/java/com/cloudinary/transformation/TextLayer.java b/cloudinary-core/src/main/java/com/cloudinary/transformation/TextLayer.java index c911f705..55380df3 100644 --- a/cloudinary-core/src/main/java/com/cloudinary/transformation/TextLayer.java +++ b/cloudinary-core/src/main/java/com/cloudinary/transformation/TextLayer.java @@ -119,11 +119,23 @@ public TextLayer text(String text) { return getThis(); } + /** + * Sets a text style identifier, + * Note: If this is used, all other style attributes are ignored in favor of this identifier + * @param textStyleIdentifier A variable string or an explicit style (e.g. "Arial_17_bold_antialias_best") + * @return Itself for chaining + */ public TextLayer textStyle(String textStyleIdentifier) { this.textStyle = textStyleIdentifier; return getThis(); } + /** + * Sets a text style identifier using an expression. + * Note: If this is used, all other style attributes are ignored in favor of this identifier + * @param textStyleIdentifier An expression instance referencing the style. + * @return Itself for chaining + */ public TextLayer textStyle(Expression textStyleIdentifier) { this.textStyle = textStyleIdentifier; return getThis(); @@ -155,6 +167,7 @@ public String toString() { } protected String textStyleIdentifier() { + // Note: if a text-style argument is provided as a whole, it overrides everything else, no mix and match. if (StringUtils.isNotBlank(this.textStyle)) { return textStyle.toString(); }