Skip to content

Commit

Permalink
Include x and y in toString when it is specified in one of the constr…
Browse files Browse the repository at this point in the history
…uctors of MagickGeometry.
  • Loading branch information
dlemstra committed Jul 13, 2024
1 parent 88fb5f9 commit 615377d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/types/magick-geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export interface IMagickGeometry {
}

export class MagickGeometry implements IMagickGeometry {
private readonly _includeXyInToString;
private _width = 0;
private _height = 0;
private _x = 0;
Expand All @@ -93,17 +94,20 @@ export class MagickGeometry implements IMagickGeometry {
this._height = height;
this._x = widthOrValueOrX;
this._y = heightOrY ?? 0;
this._includeXyInToString = true;
} else {
this._width = widthOrValueOrX;
this._height = heightOrY ?? this._width;
this._x = 0;
this._y = 0;
this._includeXyInToString = false;
}
if (this._width < 0)
throw new MagickError('negative width is not allowed');
if (this._height < 0)
throw new MagickError('negative height is not allowed');
} else {
this._includeXyInToString = widthOrValueOrX.indexOf('+') >= 0 || widthOrValueOrX.indexOf('-') >= 0;
const instance = ImageMagick._api._MagickGeometry_Create();
try {
_withString(widthOrValueOrX, valuePtr => {
Expand Down Expand Up @@ -161,15 +165,19 @@ export class MagickGeometry implements IMagickGeometry {

let result = '';

if (this._width > 0)
result += this._width.toString();
if (this._width == 0 && this._height == 0) {
result += '0x0';
} else {
if (this._width > 0)
result += this._width.toString();

if (this._height > 0)
result += 'x' + this._height.toString();
else
result += 'x'
if (this._height > 0)
result += 'x' + this._height.toString();
else
result += 'x'
}

if (this._x != 0 || this._y != 0) {
if (this._x != 0 || this._y != 0 || this._includeXyInToString) {
if (this._x >= 0)
result += '+';

Expand Down
27 changes: 27 additions & 0 deletions tests/magick-geometry/to-string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,31 @@ describe('MagickGeometry#toString', () => {

expect(geometry.toString()).toBe('11x6@');
});

it('should return the correct string when both width and height are zero', () => {
const geometry = new MagickGeometry(0, 0);

expect(geometry.toString()).toBe('0x0');
});

it('should return the correct string when all values are zero', () => {
const geometry = new MagickGeometry(0, 0, 0, 0);

expect(geometry.toString()).toBe('0x0+0+0');
});

it('should include zero x and y when specified in string constructor', () => {
const testCases = [
'0x0+0+0',
'0x0-0+0',
'0x0+0-0',
'0x0-0-0',
];

testCases.forEach((testCase) => {
const geometry = new MagickGeometry(testCase);

expect(geometry.toString()).toBe('0x0+0+0');
});
});
});

0 comments on commit 615377d

Please sign in to comment.