From 150f986b2367e9a333014a6c643c83816b413831 Mon Sep 17 00:00:00 2001 From: Alex Pinhasov Date: Mon, 2 May 2022 21:26:40 +0300 Subject: [PATCH] Add support for test style variables --- .../Core/Features/Helpers/CLDExpression.swift | 10 ++++-- .../Helpers/Layers/CLDTextLayer.swift | 34 +++++++++++++++++-- Example/Tests/GenerateUrlTests/UrlTests.swift | 23 +++++++++++++ 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/Cloudinary/Classes/Core/Features/Helpers/CLDExpression.swift b/Cloudinary/Classes/Core/Features/Helpers/CLDExpression.swift index aa3c8cf0..cf07a0db 100644 --- a/Cloudinary/Classes/Core/Features/Helpers/CLDExpression.swift +++ b/Cloudinary/Classes/Core/Features/Helpers/CLDExpression.swift @@ -286,12 +286,16 @@ import Foundation // MARK: - provide content public func asString() -> String { - guard !currentKey.isEmpty && !currentValue.isEmpty else { - + guard !currentKey.isEmpty else { return String() } - let key = replaceAllExpressionKeys(in: currentKey) + let key = removeExtraDashes(from: replaceAllExpressionKeys(in: currentKey)) + + if currentValue.isEmpty { + return "\(key)" + } + let value = removeExtraDashes(from: replaceAllUnencodeChars(in: currentValue)) return "\(key)_\(value)" diff --git a/Cloudinary/Classes/Core/Features/Helpers/Layers/CLDTextLayer.swift b/Cloudinary/Classes/Core/Features/Helpers/Layers/CLDTextLayer.swift index fe34b61c..a1a587a7 100644 --- a/Cloudinary/Classes/Core/Features/Helpers/Layers/CLDTextLayer.swift +++ b/Cloudinary/Classes/Core/Features/Helpers/Layers/CLDTextLayer.swift @@ -34,6 +34,7 @@ import Foundation internal var textDecoration: String? internal var textAlign: String? internal var stroke: String? + internal var textStyle: String? internal var letterSpacing: String? internal var lineSpacing: String? internal var fontAntialiasing: String? @@ -65,7 +66,33 @@ import Foundation self.text = text 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 + } + + /** + Sets a text style identifier using an expression. + Note: If this is used, all other style attributes are ignored in favor of this identifier + + - parameter text: An expression instance referencing the style. + + - returns: The same instance of CLDTextLayer. + */ + open func setTextStyle(expression: CLDExpression) -> CLDTextLayer { + self.textStyle = expression.asString() + return self + } + /** Set the name of a font family. e.g. `arial`. @@ -333,7 +360,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: "_")) } diff --git a/Example/Tests/GenerateUrlTests/UrlTests.swift b/Example/Tests/GenerateUrlTests/UrlTests.swift index fcb3ca17..8e9f325f 100644 --- a/Example/Tests/GenerateUrlTests/UrlTests.swift +++ b/Example/Tests/GenerateUrlTests/UrlTests.swift @@ -1075,6 +1075,29 @@ class UrlTests: BaseTestCase { "\(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") + + XCTAssertEqual(sut?.createUrl().setTransformation(CLDTransformation().setVariable("$style", string: "!Arial_12!").chain().setOverlayWithLayer(CLDTextLayer().setText(text: "hello-world").setTextStyle(expression: CLDExpression(value: "$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"))