Skip to content

Commit a0c7e7f

Browse files
committed
Clip borders correctly to avoid zero-width lines. Fix cmps. Minor refactoring
DEVSIX-1311
1 parent 9761a2e commit a0c7e7f

File tree

6 files changed

+56
-48
lines changed

6 files changed

+56
-48
lines changed

layout/src/main/java/com/itextpdf/layout/renderer/AbstractRenderer.java

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -556,59 +556,63 @@ protected boolean clipBorderArea(DrawContext drawContext, Rectangle outerBorderB
556556
}
557557
if (0 != radius) {
558558
float top = outerBorderBox.getTop(), right = outerBorderBox.getRight(), bottom = outerBorderBox.getBottom(), left = outerBorderBox.getLeft();
559-
559+
float verticalRadius = Math.min(outerBorderBox.getHeight() / 2, radius);
560+
float horizontalRadius = Math.min(outerBorderBox.getWidth() / 2, radius);
560561
// radius border bbox
561-
float x1 = right - radius, y1 = top - radius,
562-
x2 = right - radius, y2 = bottom + radius,
563-
x3 = left + radius, y3 = bottom + radius,
564-
x4 = left + radius, y4 = top - radius;
562+
float x1 = right - horizontalRadius, y1 = top - verticalRadius,
563+
x2 = right - horizontalRadius, y2 = bottom + verticalRadius,
564+
x3 = left + horizontalRadius, y3 = bottom + verticalRadius,
565+
x4 = left + horizontalRadius, y4 = top - verticalRadius;
565566

566567
PdfCanvas canvas = drawContext.getCanvas();
567568
canvas.saveState();
568569

569-
570+
// right top corner
570571
canvas
571572
.moveTo(left, top)
572573
.lineTo(x1, top)
573-
.curveTo(x1 + radius * curv, top, right, y1 + radius * curv, right, y1)
574+
.curveTo(x1 + horizontalRadius * curv, top, right, y1 + verticalRadius * curv, right, y1)
574575
.lineTo(right, bottom)
575576
.lineTo(left, bottom)
576577
.lineTo(left, top);
577578
canvas.clip().newPath();
578579

580+
// right bottom corner
579581
canvas
580582
.moveTo(right, top)
581583
.lineTo(right, y2)
582-
.curveTo(right, y2 - radius * curv, x2 + radius * curv, bottom, x2, bottom)
584+
.curveTo(right, y2 - verticalRadius * curv, x2 + horizontalRadius * curv, bottom, x2, bottom)
583585
.lineTo(left, bottom)
584586
.lineTo(left, top)
585587
.lineTo(right, top);
586588
canvas.clip().newPath();
587589

590+
// left bottom corner
588591
canvas
589592
.moveTo(right, bottom)
590593
.lineTo(x3, bottom)
591-
.curveTo(x3 - radius * curv, bottom, left, y3 - radius * curv, left, y3)
594+
.curveTo(x3 - horizontalRadius * curv, bottom, left, y3 - verticalRadius * curv, left, y3)
592595
.lineTo(left, top)
593596
.lineTo(right, top)
594597
.lineTo(right, bottom);
595598
canvas.clip().newPath();
596599

600+
// left top corner
597601
canvas
598602
.moveTo(left, bottom)
599603
.lineTo(left, y4)
600-
.curveTo(left, y4 + radius * curv, x4 - radius * curv, top, x4, top)
604+
.curveTo(left, y4 + verticalRadius * curv, x4 - horizontalRadius * curv, top, x4, top)
601605
.lineTo(right, top)
602606
.lineTo(right, bottom)
603607
.lineTo(left, bottom);
604608
canvas.clip().newPath();
605609

606610
Border[] borders = getBorders();
607611

608-
float radiusTop = radius, radiusRight = radius, radiusBottom = radius, radiusLeft = radius;
609-
float deltaTop = 0, deltaRight = 0, deltaBottom = 0, deltaLeft = 0;
612+
float radiusTop = verticalRadius, radiusRight = horizontalRadius, radiusBottom = verticalRadius, radiusLeft = horizontalRadius;
613+
float topBorderWidth = 0, rightBorderWidth = 0, bottomBorderWidth = 0, leftBorderWidth = 0;
610614
if (borders[0] != null) {
611-
deltaTop = borders[0].getWidth();
615+
topBorderWidth = borders[0].getWidth();
612616
top = top - borders[0].getWidth();
613617
if (y1 > top) {
614618
y1 = top;
@@ -617,7 +621,7 @@ protected boolean clipBorderArea(DrawContext drawContext, Rectangle outerBorderB
617621
radiusTop = Math.max(0, radiusTop - borders[0].getWidth());
618622
}
619623
if (borders[1] != null) {
620-
deltaRight = borders[1].getWidth();
624+
rightBorderWidth = borders[1].getWidth();
621625

622626
right = right - borders[1].getWidth();
623627
if (x1 > right) {
@@ -627,7 +631,7 @@ protected boolean clipBorderArea(DrawContext drawContext, Rectangle outerBorderB
627631
radiusRight = Math.max(0, radiusRight - borders[1].getWidth());
628632
}
629633
if (borders[2] != null) {
630-
deltaBottom = borders[2].getWidth();
634+
bottomBorderWidth = borders[2].getWidth();
631635

632636
bottom = bottom + borders[2].getWidth();
633637
if (x3 < left) {
@@ -638,7 +642,7 @@ protected boolean clipBorderArea(DrawContext drawContext, Rectangle outerBorderB
638642
radiusBottom = Math.max(0, radiusBottom - borders[2].getWidth());
639643
}
640644
if (borders[3] != null) {
641-
deltaLeft = borders[3].getWidth();
645+
leftBorderWidth = borders[3].getWidth();
642646

643647
left = left + borders[3].getWidth();
644648
radiusLeft = Math.max(0, radiusLeft - borders[3].getWidth());
@@ -647,57 +651,61 @@ protected boolean clipBorderArea(DrawContext drawContext, Rectangle outerBorderB
647651
canvas
648652
.moveTo(x1, top)
649653
.curveTo(x1 + Math.min(radiusTop, radiusRight) * curv, top, right, y1 + Math.min(radiusTop, radiusRight) * curv, right, y1)
650-
.lineTo(x4, y1)
651-
.lineTo(x4, top)
654+
.lineTo(right, y2)
655+
.lineTo(x3, y2)
656+
.lineTo(x3, top)
652657
.lineTo(x1, top)
653-
.lineTo(x1, top + deltaTop)
654-
.lineTo(left - deltaLeft, top + deltaTop)
655-
.lineTo(left - deltaLeft, bottom - deltaBottom)
656-
.lineTo(right + deltaRight, bottom - deltaBottom)
657-
.lineTo(right + deltaRight, top + deltaTop)
658-
.lineTo(x1, top + deltaTop);
658+
.lineTo(x1, top + topBorderWidth)
659+
.lineTo(left - leftBorderWidth, top + topBorderWidth)
660+
.lineTo(left - leftBorderWidth, bottom - bottomBorderWidth)
661+
.lineTo(right + rightBorderWidth, bottom - bottomBorderWidth)
662+
.lineTo(right + rightBorderWidth, top + topBorderWidth)
663+
.lineTo(x1, top + topBorderWidth);
659664
canvas.clip().newPath();
660665

661666
canvas
662667
.moveTo(right, y2)
663668
.curveTo(right, y2 - Math.min(radiusRight, radiusBottom) * curv, x2 + Math.min(radiusRight, radiusBottom) * curv, bottom, x2, bottom)
664-
.lineTo(x2, y1)
665-
.lineTo(right, y1)
669+
.lineTo(x3, bottom)
670+
.lineTo(x3, y4)
671+
.lineTo(right, y4)
666672
.lineTo(right, y2)
667-
.lineTo(right + deltaRight, y2)
668-
.lineTo(right + deltaRight, top + deltaTop)
669-
.lineTo(left - deltaLeft, top + deltaTop)
670-
.lineTo(left - deltaLeft, bottom - deltaBottom)
671-
.lineTo(right + deltaRight, bottom - deltaBottom)
672-
.lineTo(right + deltaRight, y2);
673+
.lineTo(right + rightBorderWidth, y2)
674+
.lineTo(right + rightBorderWidth, top + topBorderWidth)
675+
.lineTo(left - leftBorderWidth, top + topBorderWidth)
676+
.lineTo(left - leftBorderWidth, bottom - bottomBorderWidth)
677+
.lineTo(right + rightBorderWidth, bottom - bottomBorderWidth)
678+
.lineTo(right + rightBorderWidth, y2);
673679
canvas.clip().newPath();
674680

675681
canvas
676682
.moveTo(x3, bottom)
677683
.curveTo(x3 - Math.min(radiusBottom, radiusLeft) * curv, bottom, left, y3 - Math.min(radiusBottom, radiusLeft) * curv, left, y3)
678-
.lineTo(x2, y3)
679-
.lineTo(x2, bottom)
684+
.lineTo(left, y4)
685+
.lineTo(x1, y4)
686+
.lineTo(x1, bottom)
680687
.lineTo(x3, bottom)
681-
.lineTo(x3, bottom - deltaBottom)
682-
.lineTo(right + deltaRight, bottom - deltaBottom)
683-
.lineTo(right + deltaRight, top + deltaTop)
684-
.lineTo(left - deltaLeft, top + deltaTop)
685-
.lineTo(left - deltaLeft, bottom - deltaBottom)
686-
.lineTo(x3, bottom - deltaBottom);
688+
.lineTo(x3, bottom - bottomBorderWidth)
689+
.lineTo(right + rightBorderWidth, bottom - bottomBorderWidth)
690+
.lineTo(right + rightBorderWidth, top + topBorderWidth)
691+
.lineTo(left - leftBorderWidth, top + topBorderWidth)
692+
.lineTo(left - leftBorderWidth, bottom - bottomBorderWidth)
693+
.lineTo(x3, bottom - bottomBorderWidth);
687694
canvas.clip().newPath();
688695

689696
canvas
690697
.moveTo(left, y4)
691698
.curveTo(left, y4 + Math.min(radiusLeft, radiusTop) * curv, x4 - Math.min(radiusLeft, radiusTop) * curv, top, x4, top)
692-
.lineTo(x4, y3)
693-
.lineTo(left, y3)
699+
.lineTo(x1, top)
700+
.lineTo(x1, y2)
701+
.lineTo(left, y2)
694702
.lineTo(left, y4)
695-
.lineTo(left - deltaLeft, y4)
696-
.lineTo(left - deltaLeft, bottom - deltaBottom)
697-
.lineTo(right + deltaRight, bottom - deltaBottom)
698-
.lineTo(right + deltaRight, top + deltaTop)
699-
.lineTo(left - deltaLeft, top + deltaTop)
700-
.lineTo(left - deltaLeft, y4);
703+
.lineTo(left - leftBorderWidth, y4)
704+
.lineTo(left - leftBorderWidth, bottom - bottomBorderWidth)
705+
.lineTo(right + rightBorderWidth, bottom - bottomBorderWidth)
706+
.lineTo(right + rightBorderWidth, top + topBorderWidth)
707+
.lineTo(left - leftBorderWidth, top + topBorderWidth)
708+
.lineTo(left - leftBorderWidth, y4);
701709
canvas.clip().newPath();
702710

703711
}

0 commit comments

Comments
 (0)