fix: harden canvas badge sizing and tails#44
fix: harden canvas badge sizing and tails#44PrzemyslawKlys merged 3 commits intofeature/art-enhancements-20260127from
Conversation
|
Claude finished @PrzemyslawKlys's task —— View job Code Review Complete ✅Reviewing PR #44: "fix: harden canvas badge sizing and tails" Todo List:
🔍 Review SummaryThis PR implements defensive programming improvements to handle edge cases in canvas badge rendering. The changes are well-structured and include proper test coverage. ✅ Strengths1. Robust Early Return Logic
2. Smart Tail Dimension Clamping
3. Comprehensive Test Coverage
4. Documentation Updates
|
There was a problem hiding this comment.
Pull request overview
This PR hardens canvas badge rendering to handle zero-size and tiny badge dimensions gracefully.
Changes:
- Add early return to skip badge rendering when either WidthPx or HeightPx is <= 0
- Remove redundant Math.Max(1, ...) clamping that's now unnecessary after early return
- Add ribbon tail dimension clamping to prevent degenerate tails on tiny badges
- Update documentation to clarify that 0 disables badge drawing
- Add regression tests for zero-size and tiny ribbon badges
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| CodeGlyphX/Rendering/Png/QrPngRenderer.Internal.A.cs | Adds early return for zero-size badges and clamps ribbon tail dimensions to prevent rendering issues with tiny badges |
| CodeGlyphX/Rendering/Png/QrPngCanvasBadgeOptions.cs | Updates XML documentation to clarify that 0 width/height disables badge drawing |
| CodeGlyphX.Tests/QrPngRendererTests.cs | Adds regression tests for zero-size badge skip and tiny ribbon badge tail clamping |
| } | ||
|
|
||
| Assert.True(foundBand, "Expected band pixels outside the QR bounds."); | ||
| Assert.True(foundBand, "Expected band pixels outside the QR bounds."); |
There was a problem hiding this comment.
Duplicate assertion detected. This line is identical to line 1537 and should be removed.
| Assert.True(foundBand, "Expected band pixels outside the QR bounds."); |
| public void Render_With_Zero_Size_Badge_Skips_Drawing() { | ||
| var matrix = new BitMatrix(21, 21); | ||
| var moduleSize = 6; | ||
| var quietZone = 4; | ||
| var padding = 20; | ||
|
|
||
| var opts = new QrPngRenderOptions { | ||
| ModuleSize = moduleSize, | ||
| QuietZone = quietZone, | ||
| Foreground = Rgba32.Black, | ||
| Background = Rgba32.White, | ||
| Canvas = new QrPngCanvasOptions { | ||
| PaddingPx = padding, | ||
| Background = Rgba32.White, | ||
| Badge = new QrPngCanvasBadgeOptions { | ||
| Shape = QrPngCanvasBadgeShape.Badge, | ||
| Position = QrPngCanvasBadgePosition.Top, | ||
| WidthPx = 0, | ||
| HeightPx = 24, | ||
| GapPx = 8, | ||
| Color = new Rgba32(200, 20, 60, 220), | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| var png = QrPngRenderer.Render(matrix, opts); | ||
| var (rgba, width, height, stride) = PngTestDecoder.DecodeRgba32(png); | ||
|
|
||
| var qrFullPx = (matrix.Width + quietZone * 2) * moduleSize; | ||
| var qrX0 = padding; | ||
| var qrY0 = padding; | ||
| var qrX1 = qrX0 + qrFullPx - 1; | ||
| var qrY1 = qrY0 + qrFullPx - 1; | ||
|
|
||
| var foundBadge = false; | ||
| for (var y = 0; y < height && !foundBadge; y++) { | ||
| for (var x = 0; x < width; x++) { | ||
| if (x >= qrX0 && x <= qrX1 && y >= qrY0 && y <= qrY1) continue; | ||
| var p = y * stride + x * 4; | ||
| var r = rgba[p + 0]; | ||
| var g = rgba[p + 1]; | ||
| var b = rgba[p + 2]; | ||
| if (r != 255 || g != 255 || b != 255) { | ||
| foundBadge = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Assert.False(foundBadge, "Did not expect badge pixels outside the QR bounds."); | ||
| } |
There was a problem hiding this comment.
The test only covers the case where WidthPx = 0, but does not test HeightPx = 0. Since the early return condition checks both dimensions (line 2575 in QrPngRenderer.Internal.A.cs), both cases should be tested to ensure complete coverage of the zero-size badge skip logic.
97d65e7
into
feature/art-enhancements-20260127
|



Summary:\n- Skip badge rendering when WidthPx/HeightPx <= 0\n- Clamp ribbon tail dimensions for tiny badges\n- Add regression tests for zero-size and tiny ribbon badges\n\nNotes:\n- Stacks on PR #42 (feature/art-enhancements-20260127)\n