Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix table outline frame calculations; removed conditional rendering of outline #396

Merged
merged 6 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
philprime marked this conversation as resolved.
Show resolved Hide resolved
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 {
philprime marked this conversation as resolved.
Show resolved Hide resolved
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
Loading