From bceb520539f93506515538119a4cfbf212d2bb64 Mon Sep 17 00:00:00 2001 From: Jeff Seibert Date: Mon, 4 Nov 2024 00:00:41 -0500 Subject: [PATCH] fix table outline rendering --- Source/API/Utils/PDFRenderObject.swift | 5 ++ .../Graphics/PDFRectangleObject.swift | 15 ++---- .../Internal/Section/PDFSectionObject.swift | 3 +- Source/Internal/Table/PDFTableObject.swift | 47 ++++++++++--------- 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/Source/API/Utils/PDFRenderObject.swift b/Source/API/Utils/PDFRenderObject.swift index 42d1346e..a931ee0c 100644 --- a/Source/API/Utils/PDFRenderObject.swift +++ b/Source/API/Utils/PDFRenderObject.swift @@ -22,6 +22,11 @@ public class PDFRenderObject: CustomStringConvertible { /// Attributes set for this object, and their calculated frame var attributes: [(attribute: PDFObjectAttribute, frame: CGRect)] = [] + + init(frame: CGRect = .null, attributes: [(attribute: PDFObjectAttribute, frame: CGRect)] = []) { + self.frame = frame + self.attributes = attributes + } /** * Calculates the object and returns all calculated objects which are created by this calculated. diff --git a/Source/Internal/Graphics/PDFRectangleObject.swift b/Source/Internal/Graphics/PDFRectangleObject.swift index 532d7b57..cc1ef59a 100644 --- a/Source/Internal/Graphics/PDFRectangleObject.swift +++ b/Source/Internal/Graphics/PDFRectangleObject.swift @@ -22,11 +22,6 @@ class PDFRectangleObject: PDFRenderObject { */ var lineStyle: PDFLineStyle - /** - Defines the size of the rectangle - */ - var size: CGSize - /** Defines the fill color the rectangle */ @@ -39,10 +34,10 @@ class PDFRectangleObject: PDFRenderObject { - Parameter size: Size of rectangle, defaults to `CGSize.zero` - Parameter fillColor: Fill color, defaults to `Color.clear` */ - init(lineStyle: PDFLineStyle = PDFLineStyle(), size: CGSize = CGSize.zero, fillColor: Color = Color.clear) { + init(lineStyle: PDFLineStyle = PDFLineStyle(), frame: CGRect = CGRect.zero, fillColor: Color = Color.clear) { self.lineStyle = lineStyle - self.size = size self.fillColor = fillColor + super.init(frame: frame) } /** @@ -56,9 +51,9 @@ class PDFRectangleObject: PDFRenderObject { - Returns: Self */ override func calculate(generator: PDFGenerator, container: PDFContainer) throws -> [PDFLocatedRenderObject] { - let position = PDFCalculations.calculateElementPosition(for: generator, in: container, with: size) + let position = PDFCalculations.calculateElementPosition(for: generator, in: container, with: frame.size) - frame = CGRect(origin: position, size: size) + frame = CGRect(origin: position, size: frame.size) return [(container, self)] } @@ -80,6 +75,6 @@ class PDFRectangleObject: PDFRenderObject { Creates new `PDFRectangleObject` with the same properties */ override var copy: PDFRenderObject { - PDFRectangleObject(lineStyle: lineStyle, size: size, fillColor: fillColor) + PDFRectangleObject(lineStyle: lineStyle, frame: frame, fillColor: fillColor) } } diff --git a/Source/Internal/Section/PDFSectionObject.swift b/Source/Internal/Section/PDFSectionObject.swift index 7a842b95..02de3b3e 100644 --- a/Source/Internal/Section/PDFSectionObject.swift +++ b/Source/Internal/Section/PDFSectionObject.swift @@ -167,8 +167,7 @@ class PDFSectionObject: PDFRenderObject { continue } let frame = CGRect(x: met.minX, y: sectionMinY, width: met.width, height: sectionMaxY - sectionMinY) - let rect = PDFRectangleObject(lineStyle: .none, size: frame.size, fillColor: backgroundColor) - rect.frame = frame + let rect = PDFRectangleObject(lineStyle: .none, frame: frame, fillColor: backgroundColor) result += [(container, rect)] + columnObjects } } diff --git a/Source/Internal/Table/PDFTableObject.swift b/Source/Internal/Table/PDFTableObject.swift index 5389e41f..779b837d 100644 --- a/Source/Internal/Table/PDFTableObject.swift +++ b/Source/Internal/Table/PDFTableObject.swift @@ -289,6 +289,7 @@ class PDFTableObject: PDFRenderObject { repeat { var pageStart = CGPoint.null + pageEnd = CGPoint() // Calculate top page inset var minOffset = PDFCalculations.calculateTopMinimum(for: generator) @@ -303,9 +304,12 @@ class PDFTableObject: PDFRenderObject { cellFrame.origin.y += table.margin contentFrame.origin.y -= startPosition.y - minOffset contentFrame.origin.y += table.margin - - pageStart = pageStart == .null ? cellFrame.origin : pageStart - pageEnd = CGPoint(x: cellFrame.maxX, y: cellFrame.maxY) + CGPoint(x: table.margin, y: table.margin) + + // Maintain pageStart and pageEnd + pageStart.x = min(cellFrame.minX, pageStart.x) + pageStart.y = min(cellFrame.minY, pageStart.y) + pageEnd.x = max(cellFrame.maxX, pageEnd.x) + pageEnd.y = max(cellFrame.maxY, pageEnd.y) var cellElements = [PDFRenderObject]() @@ -362,13 +366,14 @@ class PDFTableObject: PDFRenderObject { throw PDFError.tableCellTooBig(cell: firstInvalidCell.cell) } - for (idx, item) in onPageCells.enumerated() { + for item in onPageCells { let cellFrame = item.frames.cell - if pageStart == CGPoint.null { - pageStart = cellFrame.origin - CGPoint(x: table.margin, y: table.margin) - } - pageEnd = CGPoint(x: cellFrame.maxX, y: cellFrame.maxY) + CGPoint(x: table.margin, y: table.margin) + // Maintain pageStart and pageEnd + pageStart.x = min(cellFrame.minX, pageStart.x) + pageStart.y = min(cellFrame.minY, pageStart.y) + pageEnd.x = max(cellFrame.maxX, pageEnd.x) + pageEnd.y = max(cellFrame.maxY, pageEnd.y) var cellElements = [PDFRenderObject]() @@ -395,24 +400,24 @@ class PDFTableObject: PDFRenderObject { minOffset: minOffset, maxOffset: maxOffset) result += try sliceObject.calculate(generator: generator, container: container) - - if nextPageCells.isEmpty && idx == cells.count - 1 { - let tableOutlineObject = PDFRectangleObject(lineStyle: table.style.outline, size: CGSize.zero) - tableOutlineObject.frame = CGRect( - x: pageStart.x, - y: pageStart.y, - width: pageEnd.x - pageStart.x, - height: pageEnd.y - pageStart.y - ) - result += try tableOutlineObject.calculate(generator: generator, container: container) - } } + + // Draw the table outline for the page + let tableOutlineObject = PDFRectangleObject(lineStyle: table.style.outline, frame: CGRect( + x: pageStart.x - table.margin, + y: pageStart.y - table.margin, + width: pageEnd.x - pageStart.x + table.margin*2, + height: pageEnd.y - pageStart.y + table.margin*2 + )) + result += [(container, tableOutlineObject)] + if !nextPageCells.isEmpty { result += try PDFPageBreakObject().calculate(generator: generator, container: container) firstPage = false pageEnd = .null } } while !nextPageCells.isEmpty + return (objects: result, offset: pageEnd.y) } @@ -511,9 +516,7 @@ class PDFTableObject: PDFRenderObject { /// - frame: Frame of cell /// - Returns: Calculated `PDFRectangleObject` func createCellBackgroundObject(style: PDFTableCellStyle, frame: CGRect) -> PDFRenderObject { - let object = PDFRectangleObject(lineStyle: .none, size: frame.size, fillColor: style.colors.fill) - object.frame = frame - return object + return PDFRectangleObject(lineStyle: .none, frame: frame, fillColor: style.colors.fill) } /**