Skip to content

Commit 9646b52

Browse files
authored
Add textFit (#523)
1 parent dc67c06 commit 9646b52

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

__TESTS__/unit/actions/Overlay.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {Underlay} from "../../../src/actions/underlay";
1111
import {Timeline} from "../../../src/qualifiers/timeline";
1212
import {base64Encode} from "../../../src/internal/utils/base64Encode";
1313
import {TextStyle} from "../../../src/qualifiers/textStyle";
14+
import {TextFit} from "../../../src/qualifiers/textFit";
1415

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

243+
it("textFit with alias", () => {
244+
const asset = createNewImage('sample');
245+
const text = 'hello hello hello hello';
246+
const textSource = Source.text(text, 'arial_15').textFit(TextFit.size(400, 500));
247+
asset.overlay(Overlay.source(textSource));
248+
expect(asset.toURL()).toContain("c_fit,w_400,h_500,l_text:arial_15:hello%20hello%20hello%20hello");
249+
});
250+
251+
it("textFit with size", () => {
252+
const asset = createNewImage('sample');
253+
const text = 'hello hello hello hello';
254+
const textSource = Source.text(text, 'arial_15').textFit(TextFit.size(400));
255+
asset.overlay(Overlay.source(textSource));
256+
expect(asset.toURL()).toContain("c_fit,w_400,l_text:arial_15:hello%20hello%20hello%20hello");
257+
});
258+
259+
it("textFit with size and height", () => {
260+
const asset = createNewImage('sample');
261+
const text = 'hello hello hello hello';
262+
const textSource = Source.text(text, 'arial_15').textFit(TextFit.size(400).height(600));
263+
asset.overlay(Overlay.source(textSource));
264+
expect(asset.toURL()).toContain("c_fit,w_400,h_600,l_text:arial_15:hello%20hello%20hello%20hello");
265+
});
266+
242267
it("should serialize a fetch source", () => {
243268
const asset = createNewVideo();
244269
const REMOTE_URL = 'http://res.cloudinary.com/demo/sample.jpg';

src/qualifiers/source/sourceTypes/BaseTextSource.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {Action} from "../../../internal/Action.js";
66
import {Qualifier} from "../../../internal/qualifier/Qualifier.js";
77
import {prepareColor} from "../../../internal/utils/prepareColor.js";
88
import {IBaseTextSourceModel} from "../../../internal/models/ITextSourceModel.js";
9+
import {TextFitQualifier} from "../../textFit.js";
910

1011
/**
1112
* @memberOf Qualifiers.Source
@@ -19,6 +20,7 @@ class BaseTextSource extends BaseSource {
1920
protected _backgroundColor: SystemColors
2021
protected type = 'text';
2122
protected _qualifierModel: Partial<IBaseTextSourceModel>;
23+
protected _textFit: TextFitQualifier;
2224

2325
constructor(text: string, textStyle?: TextStyle | string) {
2426
super();
@@ -50,6 +52,11 @@ class BaseTextSource extends BaseSource {
5052
return this;
5153
}
5254

55+
textFit(textFit:TextFitQualifier){
56+
this._textFit = textFit;
57+
return this;
58+
}
59+
5360
/**
5461
* @description
5562
* Returns the opening string of the layer,
@@ -69,6 +76,7 @@ class BaseTextSource extends BaseSource {
6976
tmpAction.addQualifier(new Qualifier(layerType, layerParam));
7077
this._textColor && tmpAction.addQualifier(new Qualifier('co', prepareColor(this._textColor)));
7178
this._backgroundColor && tmpAction.addQualifier(new Qualifier('b', prepareColor(this._backgroundColor)));
79+
this._textFit && tmpAction.addQualifier(this._textFit);
7280

7381
return tmpAction.toString();
7482
}

src/qualifiers/textFit.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {Qualifier} from "../internal/qualifier/Qualifier.js";
2+
3+
/**
4+
* @description Contains functions that Applies automatic multi-line text wrap.
5+
* <b>Learn more</b>: {@link https://cloudinary.com/documentation/layers#adding_multi_line_text|Adding multi line text}
6+
* @memberOf Qualifiers
7+
* @namespace TextFitQualifier
8+
*/
9+
10+
class TextFitQualifier extends Qualifier {
11+
protected _height: number;
12+
protected _width: number;
13+
constructor(width: number, height?: number) {
14+
//@ts-ignore
15+
super();
16+
this._width = width;
17+
this._height = height;
18+
}
19+
20+
height(height: number){
21+
this._height = height;
22+
return this;
23+
}
24+
25+
toString(){
26+
return this._height ? `c_fit,w_${this._width},h_${this._height}` : `c_fit,w_${this._width}`;
27+
}
28+
}
29+
30+
/**
31+
* @summary qualifier Adding an automatic multi-line text wrap.
32+
* @memberOf Qualifiers.TextFitQualifier
33+
* @param {number} width The width in pixels.
34+
* @param {number} height The height in pixels.
35+
*/
36+
function size(width: number, height?: number){
37+
return new TextFitQualifier(width, height);
38+
}
39+
40+
const TextFit = {size};
41+
42+
export {TextFit, size, TextFitQualifier};

0 commit comments

Comments
 (0)