Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 25 additions & 0 deletions __TESTS__/unit/actions/Overlay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {Underlay} from "../../../src/actions/underlay";
import {Timeline} from "../../../src/qualifiers/timeline";
import {base64Encode} from "../../../src/internal/utils/base64Encode";
import {TextStyle} from "../../../src/qualifiers/textStyle";
import {TextFit} from "../../../src/qualifiers/textFit";

describe('Tests for overlay actions', () => {
it('Tests Image on Image with publicID encoding', () => {
Expand Down Expand Up @@ -239,6 +240,30 @@ describe('Tests for overlay actions', () => {
expect(asset.toURL()).toContain("l_text:arial_15:!/fl_layer_apply");
});

it("textFit with alias", () => {
const asset = createNewImage('sample');
const text = 'hello hello hello hello';
const textSource = Source.text(text, 'arial_15').textFit(TextFit.size(400, 500));
asset.overlay(Overlay.source(textSource));
expect(asset.toURL()).toContain("c_fit,w_400,h_500,l_text:arial_15:hello%20hello%20hello%20hello");
});

it("textFit with size", () => {
const asset = createNewImage('sample');
const text = 'hello hello hello hello';
const textSource = Source.text(text, 'arial_15').textFit(TextFit.size(400));
asset.overlay(Overlay.source(textSource));
expect(asset.toURL()).toContain("c_fit,w_400,l_text:arial_15:hello%20hello%20hello%20hello");
});

it("textFit with size and height", () => {
const asset = createNewImage('sample');
const text = 'hello hello hello hello';
const textSource = Source.text(text, 'arial_15').textFit(TextFit.size(400).height(600));
asset.overlay(Overlay.source(textSource));
expect(asset.toURL()).toContain("c_fit,w_400,h_600,l_text:arial_15:hello%20hello%20hello%20hello");
});

it("should serialize a fetch source", () => {
const asset = createNewVideo();
const REMOTE_URL = 'http://res.cloudinary.com/demo/sample.jpg';
Expand Down
8 changes: 8 additions & 0 deletions src/qualifiers/source/sourceTypes/BaseTextSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {Action} from "../../../internal/Action.js";
import {Qualifier} from "../../../internal/qualifier/Qualifier.js";
import {prepareColor} from "../../../internal/utils/prepareColor.js";
import {IBaseTextSourceModel} from "../../../internal/models/ITextSourceModel.js";
import {TextFitQualifier} from "../../textFit.js";

/**
* @memberOf Qualifiers.Source
Expand All @@ -19,6 +20,7 @@ class BaseTextSource extends BaseSource {
protected _backgroundColor: SystemColors
protected type = 'text';
protected _qualifierModel: Partial<IBaseTextSourceModel>;
protected _textFit: TextFitQualifier;

constructor(text: string, textStyle?: TextStyle | string) {
super();
Expand Down Expand Up @@ -50,6 +52,11 @@ class BaseTextSource extends BaseSource {
return this;
}

textFit(textFit:TextFitQualifier){
this._textFit = textFit;
return this;
}

/**
* @description
* Returns the opening string of the layer,
Expand All @@ -69,6 +76,7 @@ class BaseTextSource extends BaseSource {
tmpAction.addQualifier(new Qualifier(layerType, layerParam));
this._textColor && tmpAction.addQualifier(new Qualifier('co', prepareColor(this._textColor)));
this._backgroundColor && tmpAction.addQualifier(new Qualifier('b', prepareColor(this._backgroundColor)));
this._textFit && tmpAction.addQualifier(this._textFit);

return tmpAction.toString();
}
Expand Down
42 changes: 42 additions & 0 deletions src/qualifiers/textFit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {Qualifier} from "../internal/qualifier/Qualifier.js";

/**
* @description Contains functions that Applies automatic multi-line text wrap.
* <b>Learn more</b>: {@link https://cloudinary.com/documentation/layers#adding_multi_line_text|Adding multi line text}
* @memberOf Qualifiers
* @namespace TextFitQualifier
*/

class TextFitQualifier extends Qualifier {
protected _height: number;
protected _width: number;
constructor(width: number, height?: number) {
//@ts-ignore
super();
this._width = width;
this._height = height;
}

height(height: number){
this._height = height;
return this;
}

toString(){
return this._height ? `c_fit,w_${this._width},h_${this._height}` : `c_fit,w_${this._width}`;
}
}

/**
* @summary qualifier Adding an automatic multi-line text wrap.
* @memberOf Qualifiers.TextFitQualifier
* @param {number} width The width in pixels.
* @param {number} height The height in pixels.
*/
function size(width: number, height?: number){
return new TextFitQualifier(width, height);
}

const TextFit = {size};

export {TextFit, size, TextFitQualifier};