Skip to content

Commit

Permalink
update: Added a position option and removed undocumented unequal offs…
Browse files Browse the repository at this point in the history
…et detection
  • Loading branch information
erik-perri committed Jan 20, 2024
1 parent 4151d70 commit db78cab
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 86 deletions.
5 changes: 5 additions & 0 deletions src/commands/addBadge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ void yargs(hideBin(process.argv))
description: 'Badge gravity',
type: 'string',
})
.option('position', {
default: defaultOptions.position,
description: 'Badge position (percent from gravity)',
type: 'number',
})
.option('dry-run', {
alias: 'd',
default: false,
Expand Down
5 changes: 5 additions & 0 deletions src/commands/addBadges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ void yargs(hideBin(process.argv))
description: 'Badge gravity',
type: 'string',
})
.option('position', {
default: defaultOptions.position,
description: 'Badge position (percent from gravity)',
type: 'number',
})
.option('dry-run', {
alias: 'd',
default: false,
Expand Down
1 change: 1 addition & 0 deletions src/defaultOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default {
gravity: BadgeGravity.Southeast,
paddingX: 6,
paddingY: 6,
position: undefined,
shadowColor: 'rgba(0,0,0,0.6)',
shadowSize: 4,
textColor: '#666666',
Expand Down
1 change: 1 addition & 0 deletions src/types/CommonArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default interface CommonArguments {
fontFile?: string;
fontSize: number;
gravity: string;
position?: number;
shadowColor: string;
textColor: string;
}
2 changes: 2 additions & 0 deletions src/utils/addBadgeOverlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function addBadgeOverlay(
badgeOptions: BadgeOptions,
textOptions: TextOptions,
badgeGravity: BadgeGravity,
position: number | undefined,
): void {
ImageMagick.read(readFileSync(inputFile), (image) => {
const insetWidth =
Expand Down Expand Up @@ -48,6 +49,7 @@ export default function addBadgeOverlay(
badgeWithShadow,
badgeGravity,
insetWidth,
position,
);

composite.quality = 80;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import getRotatedBadgeInfo from './getRotatedBadgeInfo';
* Returns the position and rotation of the badge assuming the badge is circular
* with equal insets.
*/
export default function getCircularBadgePosition(
export default function calculateCircularBadgePosition(
container: Rectangle,
badge: Rectangle,
circleRadius: number,
Expand Down
68 changes: 68 additions & 0 deletions src/utils/calculateManualBadgePosition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Point } from '@imagemagick/magick-wasm';

import BadgeGravity from '../types/BadgeGravity';
import BadgePosition from '../types/BadgePosition';
import Rectangle from '../types/Rectangle';
import getRotatedBadgeInfo from './getRotatedBadgeInfo';

function determinePoint(
container: Rectangle,
badge: Rectangle,
position: number,
gravity: BadgeGravity,
): Point {
switch (gravity) {
case BadgeGravity.North:
return new Point(
(container.width - badge.width) / 2,
(container.height - badge.height) * (position / 100),
);
case BadgeGravity.Northeast:
return new Point(
(container.width - badge.width) * (1 - position / 100),
(container.height - badge.height) * (position / 100),
);
case BadgeGravity.Northwest:
return new Point(
(container.width - badge.width) * (position / 100),
(container.height - badge.height) * (position / 100),
);
case BadgeGravity.South:
return new Point(
(container.width - badge.width) / 2,
(container.height - badge.height) * (1 - position / 100),
);
case BadgeGravity.Southeast:
return new Point(
(container.width - badge.width) * (1 - position / 100),
(container.height - badge.height) * (1 - position / 100),
);
case BadgeGravity.Southwest:
return new Point(
(container.width - badge.width) * (position / 100),
(container.height - badge.height) * (1 - position / 100),
);
}
}

export default function calculateManualBadgePosition(
container: Rectangle,
badge: Rectangle,
position: number,
gravity: BadgeGravity,
): BadgePosition {
const { rotation, rotatedWidth, rotatedHeight } = getRotatedBadgeInfo(
badge,
gravity,
);

const rotatedBadge: Rectangle = {
width: rotatedWidth,
height: rotatedHeight,
};

return {
point: determinePoint(container, rotatedBadge, position, gravity),
rotation,
};
}
12 changes: 8 additions & 4 deletions src/utils/createImageBadgeComposite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import {
} from '@imagemagick/magick-wasm';

import BadgeGravity from '../types/BadgeGravity';
import getCircularBadgePosition from './getCircularBadgePosition';
import getUnequalBadgePosition from './getUnequalBadgePosition';
import calculateCircularBadgePosition from './calculateCircularBadgePosition';
import calculateManualBadgePosition from './calculateManualBadgePosition';

export default function createImageBadgeComposite(
image: IMagickImage,
badge: IMagickImage,
gravity: BadgeGravity,
insetWidth: number,
position: number | undefined,
): IMagickImage {
const composite = MagickImage.create();
composite.read(MagickColors.Transparent, image.width, image.height);
Expand All @@ -23,9 +24,12 @@ export default function createImageBadgeComposite(
// We need to set a background before rotating, or it may fill it with white.
badge.backgroundColor = MagickColors.None;

const radius = insetWidth / 2;

const { rotation, point } =
getUnequalBadgePosition(composite, badge, gravity) ??
getCircularBadgePosition(composite, badge, insetWidth / 2, gravity);
position === undefined
? calculateCircularBadgePosition(composite, badge, radius, gravity)
: calculateManualBadgePosition(composite, badge, position, gravity);

if (rotation) {
badge.rotate(rotation);
Expand Down
81 changes: 0 additions & 81 deletions src/utils/getUnequalBadgePosition.ts

This file was deleted.

2 changes: 2 additions & 0 deletions src/utils/processAddBadgeCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default async function processAddBadgeCommand({
gravity,
inputImage,
outputImage,
position,
shadowColor,
textColor,
}: WriteBadgeArguments) {
Expand Down Expand Up @@ -68,6 +69,7 @@ export default async function processAddBadgeCommand({
text: badgeText.replace(/\\n/g, '\n'),
},
getBadgeGravityFromString(gravity),
position,
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/utils/processAddBadgesCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default async function processAddBadgesCommand({
fontSize,
gravity,
inputGlob,
position,
shadowColor,
textColor,
}: WriteBadgesArguments) {
Expand Down Expand Up @@ -64,6 +65,7 @@ export default async function processAddBadgesCommand({
text: badgeText.replace(/\\n/g, '\n'),
},
getBadgeGravityFromString(gravity),
position,
);
}
}
Expand Down

0 comments on commit db78cab

Please sign in to comment.