feat: add line and arrow annotations

This commit is contained in:
2026-07-12 21:09:23 +08:00
parent b82108db38
commit 50551de105
4 changed files with 220 additions and 23 deletions
+84 -8
View File
@@ -24,7 +24,7 @@ interface Rect {
h: number
}
type Tool = 'pen' | 'rect' | 'ellipse' | 'text' | 'mosaic-brush' | 'mosaic-rect'
type Tool = 'pen' | 'line' | 'arrow' | 'rect' | 'ellipse' | 'text' | 'mosaic-brush' | 'mosaic-rect'
type MosaicMode = 'brush' | 'rect'
interface Point {
x: number
@@ -150,18 +150,18 @@ const sizeLabel = computed(() => {
return `${Math.round(rect.value.w)} × ${Math.round(rect.value.h)}`
})
const MARK_TOOLBAR_W = 212
const MARK_TOOLBAR_W = 280
const ACTION_TOOLBAR_W = 288
const TOOLBAR_GROUP_GAP = 8
const toolOrder: Tool[] = ['pen', 'rect', 'ellipse', 'text', 'mosaic-brush', 'mosaic-rect']
const toolOrder: Tool[] = ['pen', 'line', 'arrow', 'rect', 'ellipse', 'text', 'mosaic-brush', 'mosaic-rect']
const toolSettingsPos = computed(() => {
if (!leftToolbarPos.value || !rect.value || !activeTool.value) return null
const isMosaic = activeTool.value.startsWith('mosaic-')
const menuW = isMosaic ? (mosaicMode.value === 'brush' ? 328 : 112) : activeTool.value === 'text' ? 462 : 398
const menuH = 58
const buttonIndex = isMosaic ? 4 : toolOrder.indexOf(activeTool.value)
const buttonIndex = isMosaic ? 6 : toolOrder.indexOf(activeTool.value)
const buttonCenter = 4 + buttonIndex * 34 + 14
let x = leftToolbarPos.value.x
let y = leftToolbarPos.value.y + 44
@@ -315,6 +315,28 @@ function textRenderBox(annotation: Annotation) {
}
}
function arrowHeadPoints(annotation: Annotation) {
if (annotation.points.length < 2) return ''
const start = annotation.points[0]
const end = annotation.points[annotation.points.length - 1]
const dx = end.x - start.x
const dy = end.y - start.y
const length = Math.hypot(dx, dy)
if (length < 1) return ''
const headLength = Math.min(length * 0.45, Math.max(10, (annotation.strokeWidth || 3) * 4))
const angle = Math.atan2(dy, dx)
const spread = Math.PI / 6
const left = {
x: end.x - headLength * Math.cos(angle - spread),
y: end.y - headLength * Math.sin(angle - spread),
}
const right = {
x: end.x - headLength * Math.cos(angle + spread),
y: end.y - headLength * Math.sin(angle + spread),
}
return `${left.x},${left.y} ${end.x},${end.y} ${right.x},${right.y}`
}
function textAnnotationIndexAtPoint(p: Point) {
if (!rect.value) return null
for (let i = annotations.value.length - 1; i >= 0; i--) {
@@ -795,6 +817,35 @@ onUnmounted(() => {
stroke-linejoin="round"
fill="none"
/>
<line
v-else-if="annotation.tool === 'line' && annotation.points.length >= 2"
:x1="annotation.points[0].x"
:y1="annotation.points[0].y"
:x2="annotation.points[annotation.points.length - 1].x"
:y2="annotation.points[annotation.points.length - 1].y"
:stroke="annotation.color"
:stroke-width="annotation.strokeWidth || 3"
stroke-linecap="round"
/>
<g v-else-if="annotation.tool === 'arrow' && annotation.points.length >= 2">
<line
:x1="annotation.points[0].x"
:y1="annotation.points[0].y"
:x2="annotation.points[annotation.points.length - 1].x"
:y2="annotation.points[annotation.points.length - 1].y"
:stroke="annotation.color"
:stroke-width="annotation.strokeWidth || 3"
stroke-linecap="round"
/>
<polyline
:points="arrowHeadPoints(annotation)"
:stroke="annotation.color"
:stroke-width="annotation.strokeWidth || 3"
stroke-linecap="round"
stroke-linejoin="round"
fill="none"
/>
</g>
<rect
v-else-if="annotation.tool === 'rect' && annotation.points.length >= 2"
:x="Math.min(annotation.points[0].x, annotation.points[1].x)"
@@ -926,12 +977,37 @@ onUnmounted(() => {
<button
class="icon-btn"
:class="{ active: activeTool === 'pen' }"
title="划线标记"
title="画笔标记"
aria-label="画笔标记"
@click="selectTool('pen')"
>
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M4 20c4-1 6-3 8-7l5-9 3 2-5 9c-2 4-5 6-9 7z" />
<path d="M14 5l5 3" />
<path d="m14.5 5.5 4-4 4 4-4 4" />
<path d="m17.5 8.5-7.8 7.8" />
<path d="M11 14.8c.8 2.8-.7 5.5-3.7 6.3-1.8.5-3.7.1-5.3-.9 2.1-.4 2.8-1.5 3.1-3.3.4-2.5 3.3-3.8 5.9-2.1Z" />
</svg>
</button>
<button
class="icon-btn"
:class="{ active: activeTool === 'line' }"
title="直线标记"
aria-label="直线标记"
@click="selectTool('line')"
>
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M5 19 19 5" />
</svg>
</button>
<button
class="icon-btn"
:class="{ active: activeTool === 'arrow' }"
title="箭头标记"
aria-label="箭头标记"
@click="selectTool('arrow')"
>
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M5 19 19 5" />
<path d="M11 5h8v8" />
</svg>
</button>
<button
@@ -1278,7 +1354,7 @@ onUnmounted(() => {
}
.mark-toolbar {
width: 212px;
width: 280px;
}
.action-toolbar {
box-sizing: border-box;
+32
View File
@@ -75,6 +75,10 @@ func ApplyAnnotationsWithScale(pngBytes []byte, annotations []Annotation, scaleX
switch ann.Tool {
case "pen":
drawPolyline(dst, ann.Points, scaleX, scaleY, width, c)
case "line":
drawStraightLine(dst, ann.Points, scaleX, scaleY, width, c)
case "arrow":
drawArrow(dst, ann.Points, scaleX, scaleY, width, c)
case "rect":
drawRectOutline(dst, ann.Points, scaleX, scaleY, width, c)
case "ellipse":
@@ -95,6 +99,34 @@ func ApplyAnnotationsWithScale(pngBytes []byte, annotations []Annotation, scaleX
return buf.Bytes(), nil
}
func drawStraightLine(img *image.RGBA, points []Point, scaleX, scaleY float64, width int, c color.RGBA) {
if len(points) < 2 {
return
}
drawLine(img, scalePoint(points[0], scaleX, scaleY), scalePoint(points[len(points)-1], scaleX, scaleY), width, c)
}
func drawArrow(img *image.RGBA, points []Point, scaleX, scaleY float64, width int, c color.RGBA) {
if len(points) < 2 {
return
}
start := scalePoint(points[0], scaleX, scaleY)
end := scalePoint(points[len(points)-1], scaleX, scaleY)
dx, dy := end.X-start.X, end.Y-start.Y
length := math.Hypot(dx, dy)
if length < 1 {
return
}
drawLine(img, start, end, width, c)
headLength := math.Min(length*0.45, math.Max(10*math.Max(scaleX, scaleY), float64(width)*4))
angle := math.Atan2(dy, dx)
spread := math.Pi / 6
left := Point{X: end.X - headLength*math.Cos(angle-spread), Y: end.Y - headLength*math.Sin(angle-spread)}
right := Point{X: end.X - headLength*math.Cos(angle+spread), Y: end.Y - headLength*math.Sin(angle+spread)}
drawLine(img, end, left, width, c)
drawLine(img, end, right, width, c)
}
const mosaicBlockSize = 10
func applyMosaicBrush(img *image.RGBA, points []Point, scaleX, scaleY float64, width int) {
+49
View File
@@ -229,3 +229,52 @@ func TestApplyAnnotationsPixelatesOnlyMosaicBrushStroke(t *testing.T) {
t.Fatalf("expected pixels outside brush path to remain unchanged")
}
}
func TestApplyAnnotationsDrawsStraightLine(t *testing.T) {
src := image.NewRGBA(image.Rect(0, 0, 60, 40))
draw.Draw(src, src.Bounds(), &image.Uniform{C: color.White}, image.Point{}, draw.Src)
var buf bytes.Buffer
if err := png.Encode(&buf, src); err != nil {
t.Fatalf("encode source: %v", err)
}
out, err := ApplyAnnotations(buf.Bytes(), []Annotation{{
Tool: "line", Color: "#ef4444", StrokeWidth: 4,
Points: []Point{{X: 5, Y: 20}, {X: 50, Y: 20}},
}}, 1)
if err != nil {
t.Fatalf("apply annotations: %v", err)
}
img, err := png.Decode(bytes.NewReader(out))
if err != nil {
t.Fatalf("decode output: %v", err)
}
got := color.RGBAModel.Convert(img.At(30, 20)).(color.RGBA)
if got.R < 180 || got.G > 180 || got.B > 180 {
t.Fatalf("expected red line at center, got %#v", got)
}
}
func TestApplyAnnotationsDrawsArrowHead(t *testing.T) {
src := image.NewRGBA(image.Rect(0, 0, 80, 60))
draw.Draw(src, src.Bounds(), &image.Uniform{C: color.White}, image.Point{}, draw.Src)
var buf bytes.Buffer
if err := png.Encode(&buf, src); err != nil {
t.Fatalf("encode source: %v", err)
}
out, err := ApplyAnnotations(buf.Bytes(), []Annotation{{
Tool: "arrow", Color: "#3b82f6", StrokeWidth: 3,
Points: []Point{{X: 10, Y: 30}, {X: 60, Y: 30}},
}}, 1)
if err != nil {
t.Fatalf("apply annotations: %v", err)
}
img, err := png.Decode(bytes.NewReader(out))
if err != nil {
t.Fatalf("decode output: %v", err)
}
shaft := color.RGBAModel.Convert(img.At(35, 30)).(color.RGBA)
head := color.RGBAModel.Convert(img.At(52, 25)).(color.RGBA)
if shaft.B < 180 || shaft.R > 140 || head.B < 180 || head.R > 140 {
t.Fatalf("expected blue arrow shaft and head, got shaft=%#v head=%#v", shaft, head)
}
}
+55 -15
View File
@@ -162,6 +162,8 @@ static id nativeOverlayKeyMonitor = nil;
@property(strong) NSButton *summaryButton;
@property(strong) NSButton *uploadButton;
@property(strong) NSButton *penButton;
@property(strong) NSButton *lineButton;
@property(strong) NSButton *arrowButton;
@property(strong) NSButton *rectButton;
@property(strong) NSButton *ellipseButton;
@property(strong) NSButton *textButton;
@@ -253,6 +255,8 @@ static id nativeOverlayKeyMonitor = nil;
[_summaryButton setToolTip:@"复制总结"];
[_uploadButton setToolTip:@"上传 S3"];
_penButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectPen)];
_lineButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectLine)];
_arrowButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectArrow)];
_rectButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectRect)];
_ellipseButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectEllipse)];
_textButton = [NSButton buttonWithTitle:@"" target:self action:@selector(selectText)];
@@ -278,10 +282,10 @@ static id nativeOverlayKeyMonitor = nil;
// Add the action toolbar background BEFORE the buttons so it sits
// behind them in the view hierarchy (AppKit z-order = subview order).
[self addSubview:_actionToolbarBg];
for (NSView *view in @[_cancelButton, _clipboardButton, _saveButton, _saveRemoteButton, _ftpButton, _ocrButton, _summaryButton, _uploadButton, _penButton, _rectButton, _ellipseButton, _textButton, _mosaicButton, _undoButton, _toolSettingsView, _sizeLabel, _hintLabel]) {
for (NSView *view in @[_cancelButton, _clipboardButton, _saveButton, _saveRemoteButton, _ftpButton, _ocrButton, _summaryButton, _uploadButton, _penButton, _lineButton, _arrowButton, _rectButton, _ellipseButton, _textButton, _mosaicButton, _undoButton, _toolSettingsView, _sizeLabel, _hintLabel]) {
[self addSubview:view];
}
for (NSView *view in @[_cancelButton, _clipboardButton, _saveButton, _saveRemoteButton, _ftpButton, _ocrButton, _summaryButton, _uploadButton, _penButton, _rectButton, _ellipseButton, _textButton, _mosaicButton, _undoButton, _toolSettingsView, _actionToolbarBg]) {
for (NSView *view in @[_cancelButton, _clipboardButton, _saveButton, _saveRemoteButton, _ftpButton, _ocrButton, _summaryButton, _uploadButton, _penButton, _lineButton, _arrowButton, _rectButton, _ellipseButton, _textButton, _mosaicButton, _undoButton, _toolSettingsView, _actionToolbarBg]) {
[view setHidden:YES];
}
[_sizeLabel setHidden:YES];
@@ -393,7 +397,12 @@ static id nativeOverlayKeyMonitor = nil;
summaryHB.baseColor = baseWhite; summaryHB.hoverColor = hoverBlue;
uploadHB.baseColor = baseWhite; uploadHB.hoverColor = hoverBlue;
[self styleIconButton:_penButton image:[self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M742.72 752.064c-29.696 28.576-42.976 48.96-42.976 77.12 0 98.4 143.776 142.464 229.728 65.536l-21.344-23.84c-66.976 59.936-176.384 26.4-176.384-41.696 0-16.832 9.28-31.104 33.184-54.08l13.44-12.736c61.024-58.016 75.328-102.72 29.312-179.84-43.84-73.44-96.8-88.288-162.784-50.56-47.232 27.04-68.64 47.456-208.32 190.4-70.624 72.288-153.92 77.088-217.344 26.784-59.712-47.328-79.872-127.36-42.176-188.64 24.512-39.84 62.656-72.896 136.64-124.48 5.952-4.192 27.04-18.816 31.104-21.664 12.16-8.448 21.536-15.104 30.4-21.536 107.552-78.272 140.8-139.136 86.048-219.904-76.992-113.472-202.304-90.016-367.744 61.472l21.6 23.616c153.088-140.224 256.896-159.616 319.68-67.104 41.632 61.408 16.896 106.688-78.4 176.032-8.64 6.304-17.92 12.832-29.888 21.184l-31.136 21.632c-77.504 54.08-117.984 89.184-145.536 133.984-46.784 76.032-22.176 173.664 49.536 230.496 76.224 60.416 177.952 54.56 260.096-29.504 136.16-139.36 158.08-160.224 201.312-184.96 50.656-28.96 84.416-19.52 119.424 39.168 36.896 61.824 27.52 91.392-23.808 140.16 0.096-0.064-10.976 10.368-13.632 12.96z' fill='white'/></svg>"]];
[self styleIconButton:_penButton image:[self iconFromSVG:@"<svg viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'><g fill='none' stroke='white' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'><path d='m14.5 5.5 4-4 4 4-4 4'/><path d='m17.5 8.5-7.8 7.8'/><path d='M11 14.8c.8 2.8-.7 5.5-3.7 6.3-1.8.5-3.7.1-5.3-.9 2.1-.4 2.8-1.5 3.1-3.3.4-2.5 3.3-3.8 5.9-2.1Z'/></g></svg>"]];
[self styleIconButton:_lineButton image:[self iconFromSVG:@"<svg viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'><path d='M5 19 19 5' fill='none' stroke='white' stroke-width='2' stroke-linecap='round'/></svg>"]];
[self styleIconButton:_arrowButton image:[self iconFromSVG:@"<svg viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'><g fill='none' stroke='white' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M5 19 19 5'/><path d='M11 5h8v8'/></g></svg>"]];
[_penButton setToolTip:@"画笔标记"];
[_lineButton setToolTip:@"直线标记"];
[_arrowButton setToolTip:@"箭头标记"];
[self styleIconButton:_rectButton image:[self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M96 96h832v832H96V96z m32 32v768h768V128H128z' fill='white'/></svg>"]];
[self styleIconButton:_ellipseButton image:[self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M512 928c229.76 0 416-186.24 416-416S741.76 96 512 96 96 282.24 96 512s186.24 416 416 416z m0-32C299.936 896 128 724.064 128 512S299.936 128 512 128s384 171.936 384 384-171.936 384-384 384z' fill='white'/></svg>"]];
[self styleIconButton:_textButton image:[self iconFromSVG:@"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'><path d='M224 160h576v96H560v608h-96V256H224V160zM384 864h256v64H384v-64z' fill='white'/></svg>"]];
@@ -752,12 +761,35 @@ static id nativeOverlayKeyMonitor = nil;
} else if (points.count >= 2) {
NSPoint a = [points[0] pointValue];
NSPoint b = [[points lastObject] pointValue];
NSPoint start = NSMakePoint(_selection.origin.x + a.x, _selection.origin.y + a.y);
NSPoint end = NSMakePoint(_selection.origin.x + b.x, _selection.origin.y + b.y);
NSRect r = NSMakeRect(
_selection.origin.x + MIN(a.x, b.x),
_selection.origin.y + MIN(a.y, b.y),
fabs(b.x - a.x),
fabs(b.y - a.y));
if ([tool isEqualToString:@"rect"]) {
if ([tool isEqualToString:@"line"] || [tool isEqualToString:@"arrow"]) {
NSBezierPath *linePath = [NSBezierPath bezierPath];
[linePath moveToPoint:start];
[linePath lineToPoint:end];
[linePath setLineWidth:strokeWidth];
[linePath setLineCapStyle:NSLineCapStyleRound];
[linePath setLineJoinStyle:NSLineJoinStyleRound];
if ([tool isEqualToString:@"arrow"]) {
CGFloat dx = end.x - start.x, dy = end.y - start.y;
CGFloat length = hypot(dx, dy);
if (length >= 1.0) {
CGFloat headLength = MIN(length * 0.45, MAX(10.0, strokeWidth * 4.0));
CGFloat angle = atan2(dy, dx), spread = M_PI / 6.0;
NSPoint left = NSMakePoint(end.x - headLength * cos(angle - spread), end.y - headLength * sin(angle - spread));
NSPoint right = NSMakePoint(end.x - headLength * cos(angle + spread), end.y - headLength * sin(angle + spread));
[linePath moveToPoint:left];
[linePath lineToPoint:end];
[linePath lineToPoint:right];
}
}
[linePath stroke];
} else if ([tool isEqualToString:@"rect"]) {
NSBezierPath *rectPath = [NSBezierPath bezierPathWithRect:r];
[rectPath setLineWidth:strokeWidth];
[rectPath stroke];
@@ -1042,6 +1074,8 @@ static id nativeOverlayKeyMonitor = nil;
[_uploadButton setHidden:!visible];
[_actionToolbarBg setHidden:!visible];
[_penButton setHidden:!visible];
[_lineButton setHidden:!visible];
[_arrowButton setHidden:!visible];
[_rectButton setHidden:!visible];
[_ellipseButton setHidden:!visible];
[_textButton setHidden:!visible];
@@ -1088,15 +1122,17 @@ static id nativeOverlayKeyMonitor = nil;
// Mark toolbar sits to the LEFT of the action toolbar (same row) with an
// 8px gap between the two groups, so they never overlap on tiny selections.
CGFloat markW = 212;
CGFloat markW = 280;
CGFloat markGap = 8;
CGFloat markX = MAX(0, x - markGap - markW);
[_penButton setFrame:NSMakeRect(markX + 4, y + 4, 28, 28)];
[_rectButton setFrame:NSMakeRect(markX + 38, y + 4, 28, 28)];
[_ellipseButton setFrame:NSMakeRect(markX + 72, y + 4, 28, 28)];
[_textButton setFrame:NSMakeRect(markX + 106, y + 4, 28, 28)];
[_mosaicButton setFrame:NSMakeRect(markX + 140, y + 4, 28, 28)];
[_undoButton setFrame:NSMakeRect(markX + 178, y + 4, 28, 28)];
[_lineButton setFrame:NSMakeRect(markX + 38, y + 4, 28, 28)];
[_arrowButton setFrame:NSMakeRect(markX + 72, y + 4, 28, 28)];
[_rectButton setFrame:NSMakeRect(markX + 106, y + 4, 28, 28)];
[_ellipseButton setFrame:NSMakeRect(markX + 140, y + 4, 28, 28)];
[_textButton setFrame:NSMakeRect(markX + 174, y + 4, 28, 28)];
[_mosaicButton setFrame:NSMakeRect(markX + 208, y + 4, 28, 28)];
[_undoButton setFrame:NSMakeRect(markX + 246, y + 4, 28, 28)];
[_undoButton setEnabled:YES];
[[_undoButton layer] setOpacity:_annotations.count > 0 ? 1.0 : 0.35];
@@ -1112,10 +1148,12 @@ static id nativeOverlayKeyMonitor = nil;
settingsY = y - settingsH - 8;
}
CGFloat activeCenter = markX + 18;
if ([_activeTool isEqualToString:@"rect"]) activeCenter = markX + 52;
if ([_activeTool isEqualToString:@"ellipse"]) activeCenter = markX + 86;
if ([_activeTool isEqualToString:@"text"]) activeCenter = markX + 120;
if (isMosaic) activeCenter = markX + 154;
if ([_activeTool isEqualToString:@"line"]) activeCenter = markX + 52;
if ([_activeTool isEqualToString:@"arrow"]) activeCenter = markX + 86;
if ([_activeTool isEqualToString:@"rect"]) activeCenter = markX + 120;
if ([_activeTool isEqualToString:@"ellipse"]) activeCenter = markX + 154;
if ([_activeTool isEqualToString:@"text"]) activeCenter = markX + 188;
if (isMosaic) activeCenter = markX + 222;
[_toolSettingsView setFrame:NSMakeRect(settingsX, settingsY, settingsW, settingsH)];
[_toolSettingsView setArrowX:MAX(18, MIN(activeCenter - settingsX, settingsW - 18))];
[_toolSettingsView setNeedsDisplay:YES];
@@ -1159,7 +1197,7 @@ static id nativeOverlayKeyMonitor = nil;
}
- (void)updateToolButtonStates {
NSDictionary *buttons = @{@"pen": _penButton, @"rect": _rectButton, @"ellipse": _ellipseButton, @"text": _textButton};
NSDictionary *buttons = @{@"pen": _penButton, @"line": _lineButton, @"arrow": _arrowButton, @"rect": _rectButton, @"ellipse": _ellipseButton, @"text": _textButton};
for (NSString *tool in buttons) {
NSButton *button = buttons[tool];
NSColor *bg = [tool isEqualToString:_activeTool]
@@ -1230,6 +1268,8 @@ static id nativeOverlayKeyMonitor = nil;
[self syncControls];
}
- (void)selectPen { [self selectTool:@"pen"]; }
- (void)selectLine { [self selectTool:@"line"]; }
- (void)selectArrow { [self selectTool:@"arrow"]; }
- (void)selectRect { [self selectTool:@"rect"]; }
- (void)selectEllipse { [self selectTool:@"ellipse"]; }
- (void)selectText { [self selectTool:@"text"]; }