Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions Source/API/Table/PDFTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ public class PDFTable: PDFDocumentObject {
//// Cells should split when overlapping page
public var shouldSplitCellsOnPageBreak = false

/**
* Optional fixed heights for individual table rows.
*
* The dictionary key is the zero-based row index and the value is the height
* of that row. Rows without an entry keep the default automatic height
* calculation based on their content.
*
* ```swift
* table.fixedRowHeights = [
* 0: 24,
* 1: 28,
* 2: 32
* ]
* ```
*/
public var fixedRowHeights: [Int: CGFloat] = [:]

/**
* Count of rows and columns in this table
*
Expand Down Expand Up @@ -82,6 +99,7 @@ public class PDFTable: PDFDocumentObject {
table.padding = padding
table.margin = margin
table.showHeadersOnEveryPage = showHeadersOnEveryPage
table.fixedRowHeights = fixedRowHeights
return table
}

Expand Down Expand Up @@ -145,6 +163,9 @@ public class PDFTable: PDFDocumentObject {
guard showHeadersOnEveryPage == otherTable.showHeadersOnEveryPage else {
return false
}
guard fixedRowHeights == otherTable.fixedRowHeights else {
return false
}
return true
}
}
27 changes: 26 additions & 1 deletion Source/Internal/Table/PDFTableObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ class PDFTableObject: PDFRenderObject {
// Fetch style of cell based on type
let style = getStyle(tableStyle: table.style, type: type)
// Calculate cell frame
let cell = calculate(generator: generator,
var cell = calculate(generator: generator,
container: container,
cell: node.cell,
style: style,
type: type,
origin: origin,
width: width)
cell = applyFixedRowHeightIfNeeded(to: cell, node: node)
calculatedCells.append(cell)

// Increase bottom offset for columns
Expand Down Expand Up @@ -184,6 +185,30 @@ class PDFTableObject: PDFRenderObject {
return frame
}

/**
Overrides the calculated cell height when every spanned row has a fixed height.

Rows without an entry in ``PDFTable/fixedRowHeights`` keep the content-based height.
*/
func applyFixedRowHeightIfNeeded(to cell: PDFTableCalculatedCell, node: PDFTableNode) -> PDFTableCalculatedCell {
let startRow = node.position.row
let endRow = node.position.row + node.moreRowsSpan
let spannedRows = startRow...endRow
guard spannedRows.allSatisfy({ table.fixedRowHeights[$0] != nil }) else {
return cell
}

var result = cell
let heights = spannedRows.map { table.fixedRowHeights[$0]! }
let fixedHeight = heights.reduce(0, +) + CGFloat(node.moreRowsSpan) * table.margin
result.frames.cell.size.height = fixedHeight
result.frames.content.size.height = min(
result.frames.content.height,
max(0, fixedHeight - 2 * table.padding)
)
return result
}

func calculate(content: PDFTableContent,
style: PDFTableCellStyle,
cell: PDFTableCell,
Expand Down
6 changes: 6 additions & 0 deletions Tests/TPPDFTests/Table/PDFTable+Equatable_Spec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ class PDFTable_Equatable_Spec: QuickSpec {
otherTable.showHeadersOnEveryPage = !table.showHeadersOnEveryPage
expect(table) != otherTable
}

it("is not equal with different fixedRowHeights") {
let otherTable = PDFTable(rows: 0, columns: 0)
otherTable.fixedRowHeights = [0: 24]
expect(table) != otherTable
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions Tests/TPPDFTests/Table/PDFTable_Spec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class PDFTable_Spec: QuickSpec {
it("has a default showHeadersOnEveryPage") {
expect(table.showHeadersOnEveryPage).to(beFalse())
}

it("has no default fixed row heights") {
expect(table.fixedRowHeights.isEmpty).to(beTrue())
}
}

context("cell style") {
Expand Down