Skip to content

Commit b21927a

Browse files
authored
Add support for variables in text style
1 parent 62f21a9 commit b21927a

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

CloudinaryDotNet.Tests/Asset/UrlBuilderTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,5 +576,25 @@ public void TestApiUrlWithPrivateCdn()
576576

577577
StringAssert.StartsWith("https://api.cloudinary.com", urlZipImage);
578578
}
579+
580+
[Test]
581+
public void TestTextLayerStyleIdentifierVariables()
582+
{
583+
string buildUrl(Func<TextLayer, TextLayer> setTextStyleAction) =>
584+
m_api.UrlImgUp.Transform(
585+
new Transformation()
586+
.Variable("$style", "!Arial_12!")
587+
.Chain()
588+
.Overlay(
589+
setTextStyleAction(new TextLayer().Text("hello-world"))
590+
)
591+
).BuildUrl("sample");
592+
593+
var expected =
594+
"http://res.cloudinary.com/testcloud/image/upload/$style_!Arial_12!/l_text:$style:hello-world/sample";
595+
596+
Assert.AreEqual(expected, buildUrl(l => l.TextStyle("$style")));
597+
Assert.AreEqual(expected, buildUrl(l => l.TextStyle(new Expression("$style"))));
598+
}
579599
}
580600
}

CloudinaryDotNet/Transforms/TextLayer.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ public class TextLayer : BaseLayer<TextLayer>
9292
/// </summary>
9393
protected string m_text;
9494

95+
/// <summary>
96+
/// The text style to generate an image for.
97+
/// </summary>
98+
protected object m_textStyle;
99+
95100
/// <summary>
96101
/// Required name of the font family. e.g. "arial".
97102
/// </summary>
@@ -209,6 +214,30 @@ public TextLayer Text(string text)
209214
return this;
210215
}
211216

217+
/// <summary>
218+
/// Sets a text style identifier.
219+
/// Note: If this is used, all other style attributes are ignored in favor of this identifier.
220+
/// </summary>
221+
/// <param name="textStyleIdentifier">A variable string or an explicit style (e.g. "Arial_17_bold_antialias_best").</param>
222+
/// <returns>The layer with text style applied.</returns>
223+
public TextLayer TextStyle(string textStyleIdentifier)
224+
{
225+
this.m_textStyle = textStyleIdentifier;
226+
return this;
227+
}
228+
229+
/// <summary>
230+
/// Sets a text style identifier.
231+
/// Note: If this is used, all other style attributes are ignored in favor of this identifier.
232+
/// </summary>
233+
/// <param name="textStyleIdentifier">An expression instance referencing the style..</param>
234+
/// <returns>The layer with text style applied.</returns>
235+
public TextLayer TextStyle(Expression textStyleIdentifier)
236+
{
237+
this.m_textStyle = textStyleIdentifier;
238+
return this;
239+
}
240+
212241
/// <summary>
213242
/// Type of font antialiasing to use.
214243
/// </summary>
@@ -396,6 +425,12 @@ private string OverlayTextEncode(string text)
396425

397426
private string TextStyleIdentifier()
398427
{
428+
var ts = m_textStyle?.ToString();
429+
if (!string.IsNullOrEmpty(ts))
430+
{
431+
return ts;
432+
}
433+
399434
List<string> components = new List<string>();
400435

401436
if (!string.IsNullOrEmpty(m_fontWeight) && !m_fontWeight.Equals("normal", StringComparison.Ordinal))

0 commit comments

Comments
 (0)