Repository navigation
Expand file tree
/
Copy pathOverlay.cpp
More file actions
9684 lines (9083 loc) · 466 KB
/
Copy pathOverlay.cpp
File metadata and controls
9684 lines (9083 loc) · 466 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "PepperSnapInternal.h"
// ----------------------------------------------------------------------------
// Pixelation & Vector Annotation Renderers
// ----------------------------------------------------------------------------
// `poly` (when given) is a closed polygon in coordinates relative to (rx, ry), the same space
// the block loop works in; it masks the pixelation to a free-form quad instead of the
// rectangle or the ellipse. Only pixels inside it are averaged and written.
static void ApplyPixelateFilterRawBuffer(DWORD* bufPixels, int bw, int bh, int strideBytes, int rx, int ry, int rw, int rh, int blockSize, bool elliptical, const POINT* poly = nullptr, int polyCount = 0) {
if (!bufPixels || rw <= 2 || rh <= 2) return;
int x0 = std::max(0, std::min(bw - 1, rx));
int y0 = std::max(0, std::min(bh - 1, ry));
int x1 = std::max(0, std::min(bw, rx + rw));
int y1 = std::max(0, std::min(bh, ry + rh));
if (x1 <= x0 || y1 <= y0) return;
const bool usePoly = (poly != nullptr && polyCount >= 3);
auto insidePoly = [&](float lx, float ly) {
bool in = false;
for (int i = 0, j = polyCount - 1; i < polyCount; j = i++) {
float yi = (float)poly[i].y, yj = (float)poly[j].y;
if (((yi > ly) != (yj > ly)) &&
(lx < (float)(poly[j].x - poly[i].x) * (ly - yi) / (yj - yi) + (float)poly[i].x)) {
in = !in;
}
}
return in;
};
blockSize = std::max(2, blockSize);
int subW = x1 - x0;
int subH = y1 - y0;
BYTE* scan0 = (BYTE*)bufPixels + (size_t)y0 * strideBytes + (size_t)x0 * 4;
float cx = rw * 0.5f;
float cy = rh * 0.5f;
float invRadX = 1.0f / std::max(1.0f, rw * 0.5f);
float invRadY = 1.0f / std::max(1.0f, rh * 0.5f);
float baseLocalX = (float)(x0 - rx) + 0.5f;
float baseLocalY = (float)(y0 - ry) + 0.5f;
for (int by = 0; by < subH; by += blockSize) {
for (int bx = 0; bx < subW; bx += blockSize) {
long sumR = 0, sumG = 0, sumB = 0, count = 0;
int maxY = std::min(subH, by + blockSize);
int maxX = std::min(subW, bx + blockSize);
for (int py = by; py < maxY; ++py) {
DWORD* row = (DWORD*)(scan0 + (size_t)py * strideBytes);
for (int px = bx; px < maxX; ++px) {
if (usePoly && !insidePoly(baseLocalX + (float)px, baseLocalY + (float)py)) continue;
DWORD argb = row[px];
sumB += (argb & 0xFF);
sumG += ((argb >> 8) & 0xFF);
sumR += ((argb >> 16) & 0xFF);
count++;
}
}
if (count > 0) {
DWORD outPx = (0xFFu << 24) | ((DWORD)(sumR / count) << 16) | ((DWORD)(sumG / count) << 8) | (DWORD)(sumB / count);
if (!elliptical && !usePoly) {
for (int py = by; py < maxY; ++py) {
DWORD* row = (DWORD*)(scan0 + (size_t)py * strideBytes);
for (int px = bx; px < maxX; ++px) {
row[px] = outPx;
}
}
} else if (!elliptical && usePoly) {
for (int py = by; py < maxY; ++py) {
DWORD* row = (DWORD*)(scan0 + (size_t)py * strideBytes);
for (int px = bx; px < maxX; ++px) {
if (insidePoly(baseLocalX + (float)px, baseLocalY + (float)py)) row[px] = outPx;
}
}
} else {
for (int py = by; py < maxY; ++py) {
DWORD* row = (DWORD*)(scan0 + (size_t)py * strideBytes);
float ny = ((baseLocalY + (float)py) - cy) * invRadY;
float nySq = ny * ny;
if (nySq > 1.0f) continue;
for (int px = bx; px < maxX; ++px) {
float nx = ((baseLocalX + (float)px) - cx) * invRadX;
if (nx * nx + nySq <= 1.0f) {
row[px] = outPx;
}
}
}
}
}
}
}
}
void PepperSnapDaemon::ApplyPixelateFilter(Bitmap* bmp, int rx, int ry, int rw, int rh, int blockSize, bool elliptical, const POINT* poly, int polyCount) {
if (!bmp || rw <= 2 || rh <= 2) return;
int bw = (int)bmp->GetWidth();
int bh = (int)bmp->GetHeight();
Rect lockRect(0, 0, bw, bh);
BitmapData data;
if (bmp->LockBits(&lockRect, ImageLockModeRead | ImageLockModeWrite, PixelFormat32bppARGB, &data) != Ok) return;
ApplyPixelateFilterRawBuffer((DWORD*)data.Scan0, bw, bh, data.Stride, rx, ry, rw, rh, blockSize, elliptical, poly, polyCount);
bmp->UnlockBits(&data);
}
// ----------------------------------------------------------------------------
// Text annotation face, size and glyph outline
// ----------------------------------------------------------------------------
//
// A text box has no face or size control of its own: the S / M / L / XL buttons
// set its stroke width, and the type size is derived from that. Drawing,
// measuring and hit-testing a text box have to agree on all of it, or the caret,
// the selection box and the exported pixels drift apart - which is what five
// hand-copied copies of the size formula invited. These five helpers are the
// single source of truth.
// Calibri Light ships with Windows (calibril.ttf). Asking GDI+ for a family that
// is not installed is not a graceful failure: it hands back a Font that draws
// nothing at all, so the text would silently disappear. Probe once, fall back.
static const wchar_t* AnnotationFontFace() {
static const wchar_t* face = nullptr;
if (!face) {
FontFamily probe(L"Calibri Light");
face = (probe.GetLastStatus() == Ok) ? L"Calibri Light" : L"Segoe UI";
}
return face;
}
// Calibri Light is installed without a bold face (calibril.ttf is the only
// upright weight), so asking for bold makes GDI+ synthesise one. That is the
// weight the earlier Text style window shipped, so it is kept; switch this to
// FontStyleRegular for the true thin face.
static const INT kAnnotationFontStyle = FontStyleBold;
static float AnnotationFontSize(float strokeWidth) {
return std::max(10.0f, strokeWidth * 3.8f + 6.0f);
}
// The outline is picked from the fill rather than exposed as a setting, which is
// what made the earlier Text style window inconvenient. Black is the shipped
// look for every swatch except black itself, which needs the light border to
// stay legible over a dark screenshot.
static Color AnnotationOutlineColor(const Color& fill) {
// Lightness, the L of HSL - the value Color::GetBrightness() reports, spelled
// out because the mingw GDI+ headers this builds against do not declare that
// accessor.
BYTE hi = std::max(std::max(fill.GetR(), fill.GetG()), fill.GetB());
BYTE lo = std::min(std::min(fill.GetR(), fill.GetG()), fill.GetB());
float lightness = (float)(hi + lo) / 510.0f;
return (lightness >= 0.35f) ? Color(255, 0, 0, 0) : Color(255, 255, 255, 255);
}
// At least 2 px, at most 5% of the type size. The floor wins where the two
// disagree (below 40 px type), because a border thinner than that on small
// glyphs reads as anti-aliasing rather than as an outline. At the four shipped
// type sizes - 13.6 / 21.2 / 36.4 / 59.2 px - this gives 2 / 2 / 2 / 3 px.
static float AnnotationOutlineWidth(float fontSize) {
return std::max(2.0f, fontSize * 0.05f);
}
// A rectangle or square-mosaic annotation can be deformed into a free quad by dragging one
// of its corners with Alt held. The four corners then live in `points`, in TL, TR, BR, BL
// order - the same order the custom area uses - and `startPt` / `endPt` stay the bounding
// box, so everything that only knows about two points (marquee bounds, export crop maths)
// keeps working. A plain rectangle stores nothing in `points`.
static bool AnnotationIsQuad(const Annotation& a) {
return (a.tool == OverlayTool::Rectangle || a.tool == OverlayTool::MosaicSquare) &&
a.points.size() == 4;
}
static void AnnotationQuadPoints(const Annotation& a, PointF2D out[4]) {
if (AnnotationIsQuad(a)) {
for (int i = 0; i < 4; ++i) out[i] = a.points[i];
return;
}
float x1 = std::min(a.startPt.x, a.endPt.x);
float y1 = std::min(a.startPt.y, a.endPt.y);
float x2 = std::max(a.startPt.x, a.endPt.x);
float y2 = std::max(a.startPt.y, a.endPt.y);
out[0] = { x1, y1 };
out[1] = { x2, y1 };
out[2] = { x2, y2 };
out[3] = { x1, y2 };
}
// Stores a deformed quad and refreshes the two-point form from it.
static void SetAnnotationQuad(Annotation& a, const PointF2D q[4]) {
a.points.assign(q, q + 4);
float minX = std::min(std::min(q[0].x, q[1].x), std::min(q[2].x, q[3].x));
float minY = std::min(std::min(q[0].y, q[1].y), std::min(q[2].y, q[3].y));
float maxX = std::max(std::max(q[0].x, q[1].x), std::max(q[2].x, q[3].x));
float maxY = std::max(std::max(q[0].y, q[1].y), std::max(q[2].y, q[3].y));
a.startPt = { minX, minY };
a.endPt = { maxX, maxY };
}
void PepperSnapDaemon::DrawVectorAnnotation(Graphics& g, const Annotation& ann, int offsetX, int offsetY, bool isSelected, bool isEditingCaret, bool forceOpaqueHighlighter) {
g.SetSmoothingMode(SmoothingModeAntiAlias);
float sx = ann.startPt.x - offsetX;
float sy = ann.startPt.y - offsetY;
float ex = ann.endPt.x - offsetX;
float ey = ann.endPt.y - offsetY;
switch (ann.tool) {
case OverlayTool::Pen:
case OverlayTool::Highlighter: {
if (ann.points.empty()) break;
bool isHi = (ann.tool == OverlayTool::Highlighter);
BYTE alpha = isHi ? (forceOpaqueHighlighter ? 255 : 95) : 255;
Color c = isHi ? Color(alpha, ann.color.GetR(), ann.color.GetG(), ann.color.GetB()) : ann.color;
Pen pen(c, isHi ? ann.strokeWidth * 4.5f : ann.strokeWidth);
pen.SetStartCap(LineCapRound);
pen.SetEndCap(LineCapRound);
pen.SetLineJoin(LineJoinRound);
static thread_local std::vector<PointF> tlsPts;
static thread_local std::vector<PointF> tlsSmooth;
static thread_local std::vector<bool> tlsSharp;
tlsPts.clear();
if (tlsPts.capacity() < ann.points.size() + 2) tlsPts.reserve(ann.points.size() + 2);
for (const auto& p : ann.points) tlsPts.emplace_back(p.x - offsetX, p.y - offsetY);
if (tlsPts.size() == 1) {
tlsPts.emplace_back(tlsPts[0].X + 0.5f, tlsPts[0].Y);
}
GraphicsState gst = g.Save();
g.SetSmoothingMode(SmoothingModeHighQuality);
g.SetPixelOffsetMode(PixelOffsetModeHighQuality);
if (tlsPts.size() == 2) {
float dx = tlsPts[1].X - tlsPts[0].X;
float dy = tlsPts[1].Y - tlsPts[0].Y;
if (dx * dx + dy * dy < 0.25f) tlsPts[1].X += 0.5f;
g.DrawLine(&pen, tlsPts[0], tlsPts[1]);
} else if (!g_Daemon.penSmoothingEnabled || g_Daemon.penSmoothingStrength <= 0) {
// Raw unsmoothed polyline when smoothing is disabled in Options
g.DrawLines(&pen, tlsPts.data(), (INT)tlsPts.size());
} else {
float sNorm = std::max(0.05f, std::min(1.0f, g_Daemon.penSmoothingStrength / 100.0f));
// Mark vertices belonging to long Shift-snapped straight segments (> 48px) so they stay sharp
size_t n = tlsPts.size();
tlsSharp.assign(n, false);
tlsSharp[0] = true;
tlsSharp[n - 1] = true;
for (size_t i = 0; i + 1 < n; ++i) {
float dx = tlsPts[i + 1].X - tlsPts[i].X;
float dy = tlsPts[i + 1].Y - tlsPts[i].Y;
if (dx * dx + dy * dy > 2304.0f) {
tlsSharp[i] = true;
tlsSharp[i + 1] = true;
}
}
// Continuous strength-scaled Gaussian centerline smoothing (0.25..4.5 passes)
float totalGauss = sNorm * 4.5f;
int fullPasses = (int)std::floor(totalGauss);
float fracPass = totalGauss - (float)fullPasses;
tlsSmooth.resize(n);
auto runGaussianPass = [&](float weight) {
float sideW = 0.25f * weight;
float centerW = 1.0f - 2.0f * sideW;
tlsSmooth[0] = tlsPts[0];
tlsSmooth[n - 1] = tlsPts[n - 1];
for (size_t i = 1; i + 1 < n; ++i) {
if (tlsSharp[i]) {
tlsSmooth[i] = tlsPts[i];
} else {
tlsSmooth[i].X = sideW * tlsPts[i - 1].X + centerW * tlsPts[i].X + sideW * tlsPts[i + 1].X;
tlsSmooth[i].Y = sideW * tlsPts[i - 1].Y + centerW * tlsPts[i].Y + sideW * tlsPts[i + 1].Y;
}
}
tlsPts.swap(tlsSmooth);
};
for (int pass = 0; pass < fullPasses; ++pass) {
runGaussianPass(1.0f);
}
if (fracPass > 0.01f) {
runGaussianPass(fracPass);
}
// Chaikin quadratic B-spline corner subdivision (1 pass for <= 35%, 2 passes for > 35%)
int chaikinPasses = (g_Daemon.penSmoothingStrength <= 35) ? 1 : 2;
for (int pass = 0; pass < chaikinPasses; ++pass) {
tlsSmooth.clear();
tlsSmooth.reserve(tlsPts.size() * 2);
tlsSmooth.push_back(tlsPts.front());
for (size_t i = 0; i + 1 < tlsPts.size(); ++i) {
const PointF& p0 = tlsPts[i];
const PointF& p1 = tlsPts[i + 1];
float dx = p1.X - p0.X;
float dy = p1.Y - p0.Y;
if (dx * dx + dy * dy > 2304.0f) {
if (i > 0) tlsSmooth.push_back(p0);
tlsSmooth.push_back(p1);
} else {
tlsSmooth.emplace_back(0.75f * p0.X + 0.25f * p1.X, 0.75f * p0.Y + 0.25f * p1.Y);
tlsSmooth.emplace_back(0.25f * p0.X + 0.75f * p1.X, 0.25f * p0.Y + 0.75f * p1.Y);
}
}
tlsSmooth.push_back(tlsPts.back());
tlsPts.swap(tlsSmooth);
}
g.DrawLines(&pen, tlsPts.data(), (INT)tlsPts.size());
}
g.Restore(gst);
break;
}
case OverlayTool::Line: {
// Pure shadow-free vector line
Pen pen(ann.color, ann.strokeWidth);
pen.SetStartCap(LineCapRound);
pen.SetEndCap(LineCapRound);
g.DrawLine(&pen, sx, sy, ex, ey);
break;
}
case OverlayTool::Arrow: {
// Shadow-free vector arrow geometry calculation
float dx = ex - sx, dy = ey - sy;
float len = std::sqrt(dx * dx + dy * dy);
if (len < 2.0f) break;
float angle = std::atan2(dy, dx);
float headLen = std::max(14.0f, ann.strokeWidth * 3.8f);
float headAngle = 0.48f;
float shaftLen = std::max(0.0f, len - headLen * 0.78f);
if (shaftLen > 1.0f) {
Pen pen(ann.color, ann.strokeWidth);
pen.SetStartCap(LineCapRound);
pen.SetEndCap(LineCapFlat);
g.DrawLine(&pen, sx, sy, sx + shaftLen * std::cos(angle), sy + shaftLen * std::sin(angle));
}
PointF tri[3] = {
PointF(ex, ey),
PointF(ex - headLen * std::cos(angle - headAngle), ey - headLen * std::sin(angle - headAngle)),
PointF(ex - headLen * std::cos(angle + headAngle), ey - headLen * std::sin(angle + headAngle))
};
SolidBrush headBrush(ann.color);
g.FillPolygon(&headBrush, tri, 3);
break;
}
case OverlayTool::NumberArrow: {
// Numbered circle badge at startPt + shadow-free arrow pointing to endPt
float r = std::max(14.0f, ann.strokeWidth * 2.0f + 8.0f);
float dx = ex - sx, dy = ey - sy;
float len = std::sqrt(dx * dx + dy * dy);
if (len > r + 4.0f) {
float angle = std::atan2(dy, dx);
float headLen = std::max(14.0f, ann.strokeWidth * 3.8f);
float headAngle = 0.48f;
float lineStartX = sx + r * std::cos(angle);
float lineStartY = sy + r * std::sin(angle);
float shaftEndDist = std::max(r, len - headLen * 0.78f);
if (shaftEndDist > r + 1.0f) {
Pen pen(ann.color, ann.strokeWidth);
pen.SetStartCap(LineCapFlat);
pen.SetEndCap(LineCapFlat);
g.DrawLine(&pen, lineStartX, lineStartY,
sx + shaftEndDist * std::cos(angle),
sy + shaftEndDist * std::sin(angle));
}
PointF tri[3] = {
PointF(ex, ey),
PointF(ex - headLen * std::cos(angle - headAngle), ey - headLen * std::sin(angle - headAngle)),
PointF(ex - headLen * std::cos(angle + headAngle), ey - headLen * std::sin(angle + headAngle))
};
SolidBrush headBrush(ann.color);
g.FillPolygon(&headBrush, tri, 3);
}
// If White color is selected, change Numbering Arrow number color and circle border to Black so it is clearly visible
bool isWhiteSelected = (ann.color.GetR() > 235 && ann.color.GetG() > 235 && ann.color.GetB() > 235);
Color contrastCol = isWhiteSelected ? Color(255, 0, 0, 0) : Color(255, 255, 255, 255);
SolidBrush fill(ann.color);
g.FillEllipse(&fill, sx - r, sy - r, r * 2.0f, r * 2.0f);
Pen ring(contrastCol, 2.2f);
g.DrawEllipse(&ring, sx - r, sy - r, r * 2.0f, r * 2.0f);
FontFamily ff(L"Segoe UI");
Font font(&ff, r * 0.95f, FontStyleBold, UnitPixel);
StringFormat sf;
sf.SetAlignment(StringAlignmentCenter);
sf.SetLineAlignment(StringAlignmentCenter);
SolidBrush fg(contrastCol);
std::wstring s = std::to_wstring(ann.stepNumber);
RectF rc(sx - r, sy - r, r * 2.0f, r * 2.0f);
g.DrawString(s.c_str(), -1, &font, rc, &sf, &fg);
break;
}
case OverlayTool::Rectangle: {
Pen pen(ann.color, ann.strokeWidth);
pen.SetLineJoin(LineJoinRound);
if (AnnotationIsQuad(ann)) {
PointF pts[4];
for (int i = 0; i < 4; ++i) {
pts[i] = PointF((REAL)(ann.points[i].x - offsetX), (REAL)(ann.points[i].y - offsetY));
}
g.DrawPolygon(&pen, pts, 4);
} else {
float x = std::min(sx, ex), y = std::min(sy, ey);
float w = std::abs(ex - sx), h = std::abs(ey - sy);
g.DrawRectangle(&pen, x, y, w, h);
}
break;
}
case OverlayTool::Ellipse: {
float x = std::min(sx, ex), y = std::min(sy, ey);
float w = std::abs(ex - sx), h = std::abs(ey - sy);
Pen pen(ann.color, ann.strokeWidth);
g.DrawEllipse(&pen, x, y, w, h);
break;
}
case OverlayTool::TextBox: {
float fSize = AnnotationFontSize(ann.strokeWidth);
float lineStep = fSize * 1.32f;
FontFamily ff(AnnotationFontFace());
Font font(&ff, fSize, kAnnotationFontStyle, UnitPixel);
const StringFormat* typoSf = StringFormat::GenericTypographic();
if (ann.text.empty() && !isEditingCaret) break;
std::vector<std::pair<size_t, std::wstring>> lines;
{
size_t start = 0;
for (size_t i = 0; i < ann.text.size(); ++i) {
if (ann.text[i] == L'\n') {
lines.push_back({ start, ann.text.substr(start, i - start) });
start = i + 1;
}
}
lines.push_back({ start, ann.text.substr(start) });
}
float bx = std::min(sx, ex);
float by = std::min(sy, ey);
float maxLineW = 0.0f;
if (ann.text.empty()) {
maxLineW = MeasureMonoPrefixWidth(g, font, L"Type here...", 12);
} else {
for (const auto& ln : lines) {
float w = MeasureMonoPrefixWidth(g, font, ln.second, ln.second.size());
if (w > maxLineW) maxLineW = w;
}
}
float boxW = isEditingCaret ? std::max(maxLineW + 14.0f, 110.0f) : std::max(12.0f, maxLineW + 10.0f);
float boxH = std::max((float)lines.size() * lineStep + 8.0f, fSize + 10.0f);
if (isEditingCaret || isSelected) {
SolidBrush editBg(Color(165, 15, 23, 42));
g.FillRectangle(&editBg, bx - 5.0f, by - 4.0f, boxW, boxH);
Pen dashPen(ann.color, 1.5f);
dashPen.SetDashStyle(DashStyleDash);
g.DrawRectangle(&dashPen, bx - 5.0f, by - 4.0f, boxW, boxH);
}
if (isEditingCaret && ann.text.empty()) {
SolidBrush placeholderFg(Color(145, 203, 213, 225));
g.DrawString(L"Type here...", -1, &font, PointF(bx, by), typoSf, &placeholderFg);
bool blinkOn = ((GetTickCount() / 450) % 2) == 0;
if (blinkOn) {
Pen caretPen(Color(255, 56, 189, 248), 2.0f);
g.DrawLine(&caretPen, bx, by, bx, by + fSize * 1.18f);
}
break;
}
if (isEditingCaret && g_Daemon.textCaretPos != g_Daemon.textSelAnchor && !ann.text.empty()) {
size_t s0 = std::min(g_Daemon.textCaretPos, g_Daemon.textSelAnchor);
size_t s1 = std::max(g_Daemon.textCaretPos, g_Daemon.textSelAnchor);
SolidBrush selHighlight(Color(200, 2, 132, 199));
for (size_t i = 0; i < lines.size(); ++i) {
size_t lStart = lines[i].first;
size_t lEnd = lStart + lines[i].second.size();
if (s1 > lStart && s0 < lEnd) {
size_t c0 = std::max(s0, lStart) - lStart;
size_t c1 = std::min(s1, lEnd) - lStart;
float x0 = bx + MeasureMonoPrefixWidth(g, font, lines[i].second, c0);
float x1 = bx + MeasureMonoPrefixWidth(g, font, lines[i].second, c1);
g.FillRectangle(&selHighlight, x0, by + (float)i * lineStep, std::max(2.0f, x1 - x0), lineStep);
}
}
}
SolidBrush fg(ann.color);
// Each line is stroked along its glyph outlines and then filled over:
// the fill covers the inner half of the stroke, so exactly
// AnnotationOutlineWidth() of it stays showing outside the
// letterforms. Round joins and caps keep corners and stem ends from
// growing spikes at these widths.
Pen outlinePen(AnnotationOutlineColor(ann.color), AnnotationOutlineWidth(fSize) * 2.0f);
outlinePen.SetLineJoin(LineJoinRound);
outlinePen.SetStartCap(LineCapRound);
outlinePen.SetEndCap(LineCapRound);
for (size_t i = 0; i < lines.size(); ++i) {
if (lines[i].second.empty()) continue;
// Non-zero winding is the rule TrueType contours are defined
// with, so counters ("o", "e") stay open in every glyph.
GraphicsPath linePath(FillModeWinding);
if (linePath.AddString(lines[i].second.c_str(), -1, &ff, kAnnotationFontStyle,
fSize, PointF(bx, by + (float)i * lineStep), typoSf) != Ok) {
continue;
}
g.DrawPath(&outlinePen, &linePath);
g.FillPath(&fg, &linePath);
}
if (isEditingCaret) {
bool blinkOn = ((GetTickCount() / 450) % 2) == 0;
if (blinkOn) {
size_t cPos = std::min(g_Daemon.textCaretPos, ann.text.size());
size_t caretLine = 0, caretCol = 0;
for (size_t i = 0; i < lines.size(); ++i) {
size_t lStart = lines[i].first;
size_t lEnd = lStart + lines[i].second.size();
if (cPos >= lStart && cPos <= lEnd) {
caretLine = i;
caretCol = cPos - lStart;
break;
}
}
float cx = bx + MeasureMonoPrefixWidth(g, font, lines[caretLine].second, caretCol);
float cy = by + (float)caretLine * lineStep;
Pen caretPen(Color(255, 56, 189, 248), 2.0f);
g.DrawLine(&caretPen, cx, cy, cx, cy + fSize * 1.18f);
}
}
break;
}
default:
break;
}
if (isSelected && ann.tool != OverlayTool::TextBox) {
Pen selPen(Color(255, 56, 189, 248), 1.5f);
selPen.SetDashStyle(DashStyleDash);
SolidBrush handleFill(Color(255, 255, 255, 255));
Pen handleBorder(Color(255, 56, 189, 248), 1.5f);
// A rectangle or square mosaic offers all four corners, because any of them can be
// pulled free-form with Alt; the other tools keep their two end handles.
if (ann.tool == OverlayTool::Rectangle || ann.tool == OverlayTool::MosaicSquare) {
PointF2D q[4];
AnnotationQuadPoints(ann, q);
PointF pts[4];
for (int i = 0; i < 4; ++i) {
pts[i] = PointF((REAL)(q[i].x - offsetX), (REAL)(q[i].y - offsetY));
}
g.DrawPolygon(&selPen, pts, 4);
for (int i = 0; i < 4; ++i) {
g.FillRectangle(&handleFill, pts[i].X - 4.0f, pts[i].Y - 4.0f, 8.0f, 8.0f);
g.DrawRectangle(&handleBorder, pts[i].X - 4.0f, pts[i].Y - 4.0f, 8.0f, 8.0f);
}
} else {
float x = std::min(sx, ex) - 6.0f;
float y = std::min(sy, ey) - 6.0f;
float w = std::max(16.0f, std::abs(ex - sx) + 12.0f);
float h = std::max(16.0f, std::abs(ey - sy) + 12.0f);
g.DrawRectangle(&selPen, x, y, w, h);
// Endpoint handles for lines, arrows, and number-arrows
g.FillEllipse(&handleFill, sx - 5.0f, sy - 5.0f, 10.0f, 10.0f);
g.DrawEllipse(&handleBorder, sx - 5.0f, sy - 5.0f, 10.0f, 10.0f);
g.FillEllipse(&handleFill, ex - 5.0f, ey - 5.0f, 10.0f, 10.0f);
g.DrawEllipse(&handleBorder, ex - 5.0f, ey - 5.0f, 10.0f, 10.0f);
}
}
}
void PepperSnapDaemon::RenderHighlighterLayer(Graphics& destG, int regionX, int regionY, int regionW, int regionH, const std::vector<Annotation>& anns, const Annotation* draft, bool nonStacking) {
bool hasHi = (draft && draft->tool == OverlayTool::Highlighter);
if (!hasHi) {
for (const auto& a : anns) {
if (a.tool == OverlayTool::Highlighter) { hasHi = true; break; }
}
}
if (!hasHi || regionW <= 0 || regionH <= 0) return;
if (!nonStacking) {
for (const auto& a : anns) {
if (a.tool == OverlayTool::Highlighter) {
DrawVectorAnnotation(destG, a, regionX, regionY, false, false, false);
}
}
if (draft && draft->tool == OverlayTool::Highlighter) {
DrawVectorAnnotation(destG, *draft, regionX, regionY, false, false, false);
}
return;
}
// Non-Stacking Highlighter: render all highlighter strokes at 100% opaque onto a tight-bounds offscreen layer,
// then composite the tight layer once at uniform 37% opacity so overlapping strokes never darken!
float minX = (float)(regionX + regionW);
float minY = (float)(regionY + regionH);
float maxX = (float)regionX;
float maxY = (float)regionY;
auto expandBounds = [&](const Annotation& a) {
float pad = std::max(16.0f, a.strokeWidth * 3.0f + 8.0f);
if (!a.points.empty()) {
for (const auto& pt : a.points) {
minX = std::min(minX, pt.x - pad);
minY = std::min(minY, pt.y - pad);
maxX = std::max(maxX, pt.x + pad);
maxY = std::max(maxY, pt.y + pad);
}
} else {
minX = std::min(minX, std::min(a.startPt.x, a.endPt.x) - pad);
minY = std::min(minY, std::min(a.startPt.y, a.endPt.y) - pad);
maxX = std::max(maxX, std::max(a.startPt.x, a.endPt.x) + pad);
maxY = std::max(maxY, std::max(a.startPt.y, a.endPt.y) + pad);
}
};
for (const auto& a : anns) {
if (a.tool == OverlayTool::Highlighter) expandBounds(a);
}
if (draft && draft->tool == OverlayTool::Highlighter) {
expandBounds(*draft);
}
int bLeft = std::max(regionX, (int)std::floor(minX));
int bTop = std::max(regionY, (int)std::floor(minY));
int bRight = std::min(regionX + regionW, (int)std::ceil(maxX));
int bBottom = std::min(regionY + regionH, (int)std::ceil(maxY));
int bW = bRight - bLeft;
int bH = bBottom - bTop;
if (bW <= 0 || bH <= 0) return;
static Bitmap* s_hiLayer = nullptr;
static int s_hiCapW = 0;
static int s_hiCapH = 0;
if (!s_hiLayer || bW > s_hiCapW || bH > s_hiCapH) {
delete s_hiLayer;
s_hiCapW = std::max(bW, 256);
s_hiCapH = std::max(bH, 256);
s_hiLayer = new Bitmap(s_hiCapW, s_hiCapH, PixelFormat32bppARGB);
}
Graphics hg(s_hiLayer);
hg.SetCompositingMode(CompositingModeSourceCopy);
SolidBrush clearBrush(Color(0, 0, 0, 0));
hg.FillRectangle(&clearBrush, 0, 0, bW, bH);
hg.SetCompositingMode(CompositingModeSourceOver);
hg.SetSmoothingMode(SmoothingModeAntiAlias);
for (const auto& a : anns) {
if (a.tool == OverlayTool::Highlighter) {
DrawVectorAnnotation(hg, a, bLeft, bTop, false, false, true);
}
}
if (draft && draft->tool == OverlayTool::Highlighter) {
DrawVectorAnnotation(hg, *draft, bLeft, bTop, false, false, true);
}
ColorMatrix cm = {
1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 95.0f / 255.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, 1.0f
};
ImageAttributes ia;
ia.SetColorMatrix(&cm, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap);
Rect destRc(bLeft - regionX, bTop - regionY, bW, bH);
destG.DrawImage(s_hiLayer, destRc, 0, 0, bW, bH, UnitPixel, &ia);
}
void PepperSnapDaemon::RecalcNextStepNum() {
int maxStep = 0;
for (const auto& a : annotations) {
if (a.tool == OverlayTool::NumberArrow && a.stepNumber > maxStep) {
maxStep = a.stepNumber;
}
}
nextStepNum = maxStep + 1;
}
void PepperSnapDaemon::CommitActiveTextBox() {
if (!isEditingText) return;
isEditingText = false;
isDraggingTextBoxSel = false;
textCaretPos = textSelAnchor = 0;
if (!editingTextAnn.text.empty()) {
PushUndo();
float fSize = AnnotationFontSize(editingTextAnn.strokeWidth);
float lineStep = fSize * 1.32f;
FontFamily ff(AnnotationFontFace());
Font font(&ff, fSize, kAnnotationFontStyle, UnitPixel);
HDC hdc = GetDC(nullptr);
Graphics g(hdc);
float maxW = 0.0f;
int lineCount = 1;
size_t start = 0;
for (size_t i = 0; i <= editingTextAnn.text.size(); ++i) {
if (i == editingTextAnn.text.size() || editingTextAnn.text[i] == L'\n') {
std::wstring ln = editingTextAnn.text.substr(start, i - start);
float w = MeasureMonoPrefixWidth(g, font, ln, ln.size());
if (w > maxW) maxW = w;
if (i < editingTextAnn.text.size()) lineCount++;
start = i + 1;
}
}
ReleaseDC(nullptr, hdc);
float bx = std::min(editingTextAnn.startPt.x, editingTextAnn.endPt.x);
float by = std::min(editingTextAnn.startPt.y, editingTextAnn.endPt.y);
editingTextAnn.startPt = { bx, by };
editingTextAnn.endPt = { bx + std::max(maxW + 10.0f, 12.0f), by + lineCount * lineStep + 6.0f };
annotations.push_back(editingTextAnn);
}
editingTextAnn = Annotation();
}
RECT PepperSnapDaemon::GetEditingTextBoxRect() const {
float bx = std::min(editingTextAnn.startPt.x, editingTextAnn.endPt.x);
float by = std::min(editingTextAnn.startPt.y, editingTextAnn.endPt.y);
float fSize = AnnotationFontSize(editingTextAnn.strokeWidth);
float lineStep = fSize * 1.32f;
FontFamily ff(AnnotationFontFace());
Font font(&ff, fSize, kAnnotationFontStyle, UnitPixel);
HDC hdc = GetDC(nullptr);
Graphics g(hdc);
float maxW = 0.0f;
int lineCount = 1;
if (editingTextAnn.text.empty()) {
maxW = MeasureMonoPrefixWidth(g, font, L"Type here...", 12);
} else {
size_t start = 0;
for (size_t i = 0; i <= editingTextAnn.text.size(); ++i) {
if (i == editingTextAnn.text.size() || editingTextAnn.text[i] == L'\n') {
std::wstring ln = editingTextAnn.text.substr(start, i - start);
float w = MeasureMonoPrefixWidth(g, font, ln, ln.size());
if (w > maxW) maxW = w;
if (i < editingTextAnn.text.size()) lineCount++;
start = i + 1;
}
}
}
ReleaseDC(nullptr, hdc);
float boxW = std::max(maxW + 14.0f, 110.0f);
float boxH = std::max(lineCount * lineStep + 8.0f, fSize + 10.0f);
return { (LONG)(bx - 6.0f), (LONG)(by - 5.0f), (LONG)(bx + boxW + 6.0f), (LONG)(by + boxH + 5.0f) };
}
size_t PepperSnapDaemon::HitTestTextBoxIndex(int mouseX, int mouseY) const {
const std::wstring& s = editingTextAnn.text;
if (s.empty()) return 0;
float bx = std::min(editingTextAnn.startPt.x, editingTextAnn.endPt.x);
float by = std::min(editingTextAnn.startPt.y, editingTextAnn.endPt.y);
float fSize = AnnotationFontSize(editingTextAnn.strokeWidth);
float lineStep = fSize * 1.32f;
std::vector<std::pair<size_t, std::wstring>> lines;
size_t start = 0;
for (size_t i = 0; i < s.size(); ++i) {
if (s[i] == L'\n') {
lines.push_back({ start, s.substr(start, i - start) });
start = i + 1;
}
}
lines.push_back({ start, s.substr(start) });
int lineIdx = (int)std::floor(((float)mouseY - by) / lineStep);
lineIdx = std::max(0, std::min((int)lines.size() - 1, lineIdx));
const std::wstring& ln = lines[lineIdx].second;
float relX = (float)mouseX - bx;
if (relX <= 0.0f || ln.empty()) return lines[lineIdx].first;
FontFamily ff(AnnotationFontFace());
Font font(&ff, fSize, kAnnotationFontStyle, UnitPixel);
HDC hdc = GetDC(nullptr);
Graphics g(hdc);
size_t col = ln.size();
float prevX = 0.0f;
for (size_t i = 0; i < ln.size(); ++i) {
float nextX = MeasureMonoPrefixWidth(g, font, ln, i + 1);
if (relX < (prevX + nextX) * 0.5f) {
col = i;
break;
}
prevX = nextX;
}
ReleaseDC(nullptr, hdc);
return lines[lineIdx].first + col;
}
void PepperSnapDaemon::ApplyTextBoxKeyDown(WPARAM wParam, bool ctrl, bool shift, HWND hWnd) {
std::wstring& text = editingTextAnn.text;
textCaretPos = std::min(textCaretPos, text.size());
textSelAnchor = std::min(textSelAnchor, text.size());
auto deleteSelection = [&]() -> bool {
if (textCaretPos == textSelAnchor) return false;
size_t s0 = std::min(textCaretPos, textSelAnchor);
size_t s1 = std::max(textCaretPos, textSelAnchor);
text.erase(s0, s1 - s0);
textCaretPos = textSelAnchor = s0;
return true;
};
if (ctrl) {
if (wParam == 'A') {
textSelAnchor = 0;
textCaretPos = text.size();
return;
}
if ((wParam == 'C' || wParam == 'X') && textCaretPos != textSelAnchor) {
size_t s0 = std::min(textCaretPos, textSelAnchor);
size_t s1 = std::max(textCaretPos, textSelAnchor);
std::wstring sub = text.substr(s0, s1 - s0);
if (OpenClipboard(hWnd)) {
EmptyClipboard();
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (sub.size() + 1) * sizeof(wchar_t));
if (hMem) {
wchar_t* dst = (wchar_t*)GlobalLock(hMem);
if (dst) {
memcpy(dst, sub.c_str(), (sub.size() + 1) * sizeof(wchar_t));
GlobalUnlock(hMem);
SetClipboardData(CF_UNICODETEXT, hMem);
}
}
CloseClipboard();
}
if (wParam == 'X') deleteSelection();
return;
}
if (wParam == 'V') {
if (OpenClipboard(hWnd)) {
HANDLE hData = GetClipboardData(CF_UNICODETEXT);
if (hData) {
const wchar_t* psz = (const wchar_t*)GlobalLock(hData);
if (psz) {
deleteSelection();
for (size_t i = 0; psz[i] != 0; ++i) {
wchar_t c = psz[i];
if (c == L'\r') continue;
if (c >= 32 || c == L'\n') {
text.insert(textCaretPos, 1, c);
textCaretPos++;
}
}
textSelAnchor = textCaretPos;
GlobalUnlock(hData);
}
}
CloseClipboard();
}
return;
}
}
std::vector<std::pair<size_t, size_t>> lineSpans;
{
size_t start = 0;
for (size_t i = 0; i < text.size(); ++i) {
if (text[i] == L'\n') {
lineSpans.push_back({ start, i - start });
start = i + 1;
}
}
lineSpans.push_back({ start, text.size() - start });
}
size_t curLine = 0, curCol = 0;
for (size_t i = 0; i < lineSpans.size(); ++i) {
if (textCaretPos >= lineSpans[i].first && textCaretPos <= lineSpans[i].first + lineSpans[i].second) {
curLine = i;
curCol = textCaretPos - lineSpans[i].first;
break;
}
}
switch (wParam) {
case VK_LEFT:
if (!shift && textCaretPos != textSelAnchor) {
textCaretPos = textSelAnchor = std::min(textCaretPos, textSelAnchor);
} else {
if (textCaretPos > 0) textCaretPos--;
if (!shift) textSelAnchor = textCaretPos;
}
break;
case VK_RIGHT:
if (!shift && textCaretPos != textSelAnchor) {
textCaretPos = textSelAnchor = std::max(textCaretPos, textSelAnchor);
} else {
if (textCaretPos < text.size()) textCaretPos++;
if (!shift) textSelAnchor = textCaretPos;
}
break;
case VK_HOME:
textCaretPos = lineSpans[curLine].first;
if (!shift) textSelAnchor = textCaretPos;
break;
case VK_END:
textCaretPos = lineSpans[curLine].first + lineSpans[curLine].second;
if (!shift) textSelAnchor = textCaretPos;
break;
case VK_UP:
if (curLine > 0) {
textCaretPos = lineSpans[curLine - 1].first + std::min(curCol, lineSpans[curLine - 1].second);
} else {
textCaretPos = 0;
}
if (!shift) textSelAnchor = textCaretPos;
break;
case VK_DOWN:
if (curLine + 1 < lineSpans.size()) {
textCaretPos = lineSpans[curLine + 1].first + std::min(curCol, lineSpans[curLine + 1].second);
} else {
textCaretPos = text.size();
}
if (!shift) textSelAnchor = textCaretPos;
break;
case VK_BACK:
if (!deleteSelection() && textCaretPos > 0) {
text.erase(textCaretPos - 1, 1);
textCaretPos--;
textSelAnchor = textCaretPos;
}
break;
case VK_DELETE:
if (!deleteSelection() && textCaretPos < text.size()) {
text.erase(textCaretPos, 1);
}
break;
default:
break;
}
}
void PepperSnapDaemon::ApplyStrokeToSelectedAnnotation(float newStroke, bool recordUndo) {
newStroke = std::max(1.0f, std::min(120.0f, newStroke));
activeStroke = newStroke;
SaveSettings();
if (isEditingText) {
editingTextAnn.strokeWidth = newStroke;
}
if (!HasSelectedAnnotations()) return;
bool pushed = false;
for (auto& a : annotations) {
if (IsAnnotationSelected(a.id)) {
if (recordUndo && !pushed && std::abs(a.strokeWidth - newStroke) > 0.05f) {
PushUndo();
pushed = true;
}
a.strokeWidth = newStroke;
if (a.tool == OverlayTool::TextBox) {
FontFamily ff(AnnotationFontFace());
float fSize = AnnotationFontSize(a.strokeWidth);
Font font(&ff, fSize, kAnnotationFontStyle, UnitPixel);
HDC hdc = GetDC(nullptr);
Graphics g(hdc);
RectF bounds;
g.MeasureString(a.text.c_str(), -1, &font, PointF(a.startPt.x, a.startPt.y), &bounds);
ReleaseDC(nullptr, hdc);
a.endPt = { a.startPt.x + bounds.Width + 12.0f, a.startPt.y + bounds.Height + 8.0f };
}
}
}
}
void PepperSnapDaemon::CommitActiveStrokeInput() {
if (!isEditingStroke) return;
isEditingStroke = false;
isDraggingStrokeText = false;
std::wstring s = editingStrokeText;
editingStrokeText.clear();
strokeCaretPos = strokeSelAnchor = 0;
for (wchar_t& c : s) {
if ((c < L'0' || c > L'9') && c != L'.') c = L' ';
}
std::wstringstream ss(s);
float val = 0.0f;
if ((ss >> val) && val >= 1.0f) {
ApplyStrokeToSelectedAnnotation(std::max(1.0f, std::min(120.0f, val)), true);
}
}
void PepperSnapDaemon::CommitActiveSizeInput() {
if (!isEditingSize) return;
isEditingSize = false;
isDraggingSizeText = false;
std::wstring raw = editingSizeText;
editingSizeText.clear();
sizeCaretPos = sizeSelAnchor = 0;
int curW = std::max(1, (int)std::abs(selRect.right - selRect.left));
int curH = std::max(1, (int)std::abs(selRect.bottom - selRect.top));