Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for variables in text style. #371

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import Foundation
internal var fontWeight: String?
internal var textDecoration: String?
internal var textAlign: String?
internal var textStyle: String?
internal var stroke: String?
internal var letterSpacing: String?
internal var lineSpacing: String?
Expand Down Expand Up @@ -66,6 +67,19 @@ import Foundation
return self
}

/**
Sets a text style identifier.
Note: If this is used, all other style attributes are ignored in favor of this identifier

- parameter text: A variable string or an explicit style (e.g. "Arial_17_bold_antialias_best").

- returns: The same instance of CLDTextLayer.
*/
open func setTextStyle(textStyle: String) -> CLDTextLayer {
self.textStyle = textStyle
return self
}

/**
Set the name of a font family. e.g. `arial`.

Expand Down Expand Up @@ -333,7 +347,10 @@ import Foundation

let optionalTextParams = getOptionalTextPropertiesArray()
let mandatoryTextParams = getMandatoryTextPropertiesArray()
if optionalTextParams.isEmpty {
if let textStyle = textStyle {
components.append([textStyle].joined(separator: "_"))
}
else if optionalTextParams.isEmpty {
if !mandatoryTextParams.isEmpty {
components.append(mandatoryTextParams.joined(separator: "_"))
}
Expand Down
21 changes: 21 additions & 0 deletions Example/Tests/GenerateUrlTests/UrlTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,27 @@ class UrlTests: BaseTestCase {
XCTAssertEqual(sut?.createUrl().setTransformation(CLDTransformation().setOverlayWithLayer(CLDFetchLayer(url: "https://res.cloudinary.com/demo/image/upload/sample"))).generate("test"),
"\(prefix)/image/upload/l_fetch:aHR0cHM6Ly9yZXMuY2xvdWRpbmFyeS5jb20vZGVtby9pbWFnZS91cGxvYWQvc2FtcGxl/test")
}

func testTextStyle() {
XCTAssertEqual(sut?.createUrl().setTransformation(CLDTransformation().setVariable("$style", string: "!Arial_12!").chain().setOverlayWithLayer(CLDTextLayer().setText(text: "hello-world").setTextStyle(textStyle: "$style"))).generate("test"), "\(prefix)/image/upload/$style_!Arial_12!/l_text:$style:hello-world/test")
}

func testTextStyleOverpowerOtherTextAttributes() {
let layer = CLDTextLayer()
.setText(text: "hello_world")
.setFontFamily(fontFamily: "Arial")
.setFontSize(18)
.setFontStyle(.italic)
.setFontWeight(.bold)
.setLetterSpacing(4)
.setTextStyle(textStyle: "$style")

let transformation = CLDTransformation().setVariable("$style", string: "!Arial_12!").chain().setOverlayWithLayer(layer)
let url = sut?.createUrl().setTransformation(transformation).generate("test")

XCTAssertNotNil(url)
XCTAssertFalse(url!.contains("Arial_18_bold_italic_letter_spacing_4"), "$style should overpower other text attributes.")
}

func testOverlayErrors() {
XCTAssertNil(sut?.createUrl().setTransformation(CLDTransformation().setOverlayWithLayer(CLDTextLayer().setText(text: "text").setFontStyle(.italic))).generate("test"))
Expand Down