Skip to content

Commit

Permalink
fix table outline rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
jseibert committed Nov 4, 2024
1 parent d069276 commit bceb520
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
5 changes: 5 additions & 0 deletions Source/API/Utils/PDFRenderObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 5 additions & 10 deletions Source/Internal/Graphics/PDFRectangleObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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)
}

/**
Expand All @@ -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)]
}
Expand All @@ -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)
}
}
3 changes: 1 addition & 2 deletions Source/Internal/Section/PDFSectionObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
47 changes: 25 additions & 22 deletions Source/Internal/Table/PDFTableObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ class PDFTableObject: PDFRenderObject {

repeat {
var pageStart = CGPoint.null
pageEnd = CGPoint()

// Calculate top page inset
var minOffset = PDFCalculations.calculateTopMinimum(for: generator)
Expand All @@ -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]()

Expand Down Expand Up @@ -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]()

Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}

/**
Expand Down

0 comments on commit bceb520

Please sign in to comment.