Skip to content

Commit 922d42d

Browse files
authored
Add support for variables in text style. (#225)
1 parent 4570326 commit 922d42d

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

cloudinary-core/src/main/java/com/cloudinary/transformation/TextLayer.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class TextLayer extends AbstractLayer<TextLayer> {
2121
protected String letterSpacing = null;
2222
protected Integer lineSpacing = null;
2323
protected String text = null;
24+
protected Object textStyle = null;
2425

2526
@Override
2627
TextLayer getThis() {
@@ -118,6 +119,28 @@ public TextLayer text(String text) {
118119
return getThis();
119120
}
120121

122+
/**
123+
* Sets a text style identifier,
124+
* Note: If this is used, all other style attributes are ignored in favor of this identifier
125+
* @param textStyleIdentifier A variable string or an explicit style (e.g. "Arial_17_bold_antialias_best")
126+
* @return Itself for chaining
127+
*/
128+
public TextLayer textStyle(String textStyleIdentifier) {
129+
this.textStyle = textStyleIdentifier;
130+
return getThis();
131+
}
132+
133+
/**
134+
* Sets a text style identifier using an expression.
135+
* Note: If this is used, all other style attributes are ignored in favor of this identifier
136+
* @param textStyleIdentifier An expression instance referencing the style.
137+
* @return Itself for chaining
138+
*/
139+
public TextLayer textStyle(Expression textStyleIdentifier) {
140+
this.textStyle = textStyleIdentifier;
141+
return getThis();
142+
}
143+
121144
@Override
122145
public String toString() {
123146
if (this.publicId == null && this.text == null) {
@@ -144,6 +167,11 @@ public String toString() {
144167
}
145168

146169
protected String textStyleIdentifier() {
170+
// Note: if a text-style argument is provided as a whole, it overrides everything else, no mix and match.
171+
if (StringUtils.isNotBlank(this.textStyle)) {
172+
return textStyle.toString();
173+
}
174+
147175
ArrayList<String> components = new ArrayList<String>();
148176

149177
if (StringUtils.isNotBlank(this.fontWeight) && !this.fontWeight.equals("normal"))
@@ -181,6 +209,5 @@ protected String textStyleIdentifier() {
181209
components.add(0, this.fontFamily);
182210

183211
return StringUtils.join(components, "_");
184-
185212
}
186213
}

cloudinary-core/src/test/java/com/cloudinary/test/CloudinaryTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,30 @@ public void testSecureDistribution() {
101101
assertEquals("https://res.cloudinary.com/test123/image/upload/test", result);
102102
}
103103

104+
@Test
105+
public void testTextLayerStyleIdentifierVariables() {
106+
String url = cloudinary.url().transformation(
107+
new Transformation()
108+
.variable("$style", "!Arial_12!")
109+
.chain()
110+
.overlay(
111+
new TextLayer().text("hello-world").textStyle("$style")
112+
)).generate("sample");
113+
114+
assertEquals("http://res.cloudinary.com/test123/image/upload/$style_!Arial_12!/l_text:$style:hello-world/sample", url);
115+
116+
url = cloudinary.url().transformation(
117+
new Transformation()
118+
.variable("$style", "!Arial_12!")
119+
.chain()
120+
.overlay(
121+
new TextLayer().text("hello-world").textStyle(new Expression("$style"))
122+
)).generate("sample");
123+
124+
assertEquals("http://res.cloudinary.com/test123/image/upload/$style_!Arial_12!/l_text:$style:hello-world/sample", url);
125+
}
126+
127+
104128
@Test
105129
public void testSecureDistributionOverwrite() {
106130
// should allow overwriting secure distribution if secure=TRUE

0 commit comments

Comments
 (0)