Skip to content

Commit f555279

Browse files
committed
Adjust ink annotation stroke scaling
Replace previous area-based stroke scaling with axis-aware logic for ink annotations. Side-handle resizes now scale the stroke along the changed axis, corner/non-uniform resizes use the smaller axis scale, and an epsilon is used to detect axis-only changes. Stroke width is clamped to at most the smallest rect dimension, forced to a minimum of 1, and rounded to one decimal place. Also remove an unused import (calculateRotatedRectAABB). These changes make handle-based resizing more intuitive and avoid excessively large stroke widths.
1 parent 64ff405 commit f555279

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

  • packages/plugin-annotation/src/lib/patching/patches

packages/plugin-annotation/src/lib/patching/patches/ink.patch.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { expandRect, PdfInkAnnoObject, Rect, rectFromPoints } from '@embedpdf/mo
22

33
import { PatchFunction } from '../patch-registry';
44
import {
5-
calculateRotatedRectAABB,
65
calculateRotatedRectAABBAroundPoint,
76
resolveAnnotationRotationCenter,
87
} from '../patch-utils';
@@ -48,12 +47,24 @@ export const patchInk: PatchFunction<PdfInkAnnoObject> = (original, ctx) => {
4847
},
4948
});
5049

51-
const widthScale = resolvedRect.size.width / Math.max(oldRect.size.width, 1e-6);
52-
const heightScale = resolvedRect.size.height / Math.max(oldRect.size.height, 1e-6);
53-
// Area-based stroke scaling keeps side-handle resize reversible:
54-
// shrinking on one axis thins the stroke, and growing that axis restores it.
55-
const strokeScale = Math.sqrt(Math.max(widthScale, 1e-6) * Math.max(heightScale, 1e-6));
56-
const newStrokeWidth = Math.max(1, original.strokeWidth * strokeScale);
50+
// Side handles should scale stroke width on that axis directly; corner
51+
// resize keeps previous min-axis behavior for non-uniform scaling.
52+
const resizeEpsilon = 1e-3;
53+
const widthChanged = Math.abs(scaleX - 1) > resizeEpsilon;
54+
const heightChanged = Math.abs(scaleY - 1) > resizeEpsilon;
55+
const strokeScale =
56+
widthChanged && !heightChanged
57+
? scaleX
58+
: !widthChanged && heightChanged
59+
? scaleY
60+
: Math.min(scaleX, scaleY);
61+
const rawStrokeWidth = Math.max(1, original.strokeWidth * strokeScale);
62+
const maxStrokeWidth = Math.max(
63+
1,
64+
Math.min(resolvedRect.size.width, resolvedRect.size.height),
65+
);
66+
const clampedStrokeWidth = Math.min(rawStrokeWidth, maxStrokeWidth);
67+
const newStrokeWidth = Number(clampedStrokeWidth.toFixed(1));
5768

5869
const innerOld = inset(oldRect, original.strokeWidth / 2);
5970
const innerNew = inset(resolvedRect, newStrokeWidth / 2);

0 commit comments

Comments
 (0)