Skip to content

Commit

Permalink
[269] Removed PDFTable size defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Niedertscheider committed Apr 11, 2021
1 parent bf6f40e commit d53c9ee
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
**Fixed bugs:**

- Fixed `PDFList` not calculating available content height correctly (#267)
- Removed `PDFTable` initializer size defaults (#269)

**Closed issues:**

- Issue #267
- Issue #269

**Merged pull requests:**

Expand Down
4 changes: 2 additions & 2 deletions Documentation/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ document.add(list: list)

### Table

Create a `PDFTable` element, which will now be customized.
Create a `PDFTable` element of a given size, which will now be customized.

```swift
let table = PDFTable()
let table = PDFTable(rows: 3, columns: 4)
```

Each cell has an optional `content` of type `PDFTableContent`, an optional `style` of `PDFTableCellStyle` and always an `alignment` of `PDFTableCellAlignment`.
Expand Down
2 changes: 0 additions & 2 deletions Source/API/PDFGenerator+Generation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ extension PDFGenerator {

// Iterate all objects and let them calculate the required rendering bounds
var needsPageBreak = false
var prevPDFObject: PDFLocatedRenderObject?
for (pdfObjectIdx, locatedPdfObject) in contentObjects.enumerated() {
let (container, pdfObject) = locatedPdfObject
if let tocObject = pdfObject as? PDFTableOfContentObject {
Expand Down Expand Up @@ -183,7 +182,6 @@ extension PDFGenerator {
prevObj = obj
}
progress.completedUnitCount += 1
prevPDFObject = locatedPdfObject
}

let result = currentPage
Expand Down
18 changes: 12 additions & 6 deletions Source/API/Table/PDFTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,20 @@ public class PDFTable: PDFDocumentObject {
*/
public private(set) var size: (rows: Int, columns: Int)

public convenience init(size: (rows: Int, columns: Int) = (0, 0)) {
/// Creates a new table with the given size and populates it with empty cells
///
/// - Parameter size: Row and column count of table
public convenience init(size: (rows: Int, columns: Int)) {
self.init(rows: size.rows, columns: size.columns)
}

/**
Creates a new table with the given size and populates it with empty cells.
*/
public init(rows: Int = 0, columns: Int = 0) {
/// Creates a new table with the given size and populates it with empty cells.
/// - Parameters:
/// - rows: Rows of table, must be greater than 0
/// - columns: Columns of table, must be greater than 0
public init(rows: Int, columns: Int) {
assert(rows >= 0, "Can't create a table with negative row count")
assert(columns >= 0, "Can't create a table with negative column count")
self.size = (rows: rows, columns: columns)
self.cells = (0..<rows).map({ _ in (0..<columns).map({ _ in PDFTableCell() }) })
self.widths = (0..<columns).map({ _ in 1.0 / CGFloat(columns) })
Expand All @@ -74,7 +80,7 @@ public class PDFTable: PDFDocumentObject {
Creates a new `PDFTable` with the same properties
*/
internal var copy: PDFTable {
let table = PDFTable()
let table = PDFTable(size: self.size)
table.style = self.style
table.cells = self.cells
table.widths = self.widths
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PDFTableMergeUtilSpec: QuickSpec {
let ROWS = 10
let COLS = 10

var table = PDFTable()
var table = PDFTable(rows: ROWS, columns: COLS)

beforeEach {
table = PDFTable(rows: ROWS, columns: COLS)
Expand Down
2 changes: 1 addition & 1 deletion Tests/TPPDFTests/PDFDocument+Objects_Spec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class PDFDocument_Objects_Spec: QuickSpec {

context("table") {

let table = PDFTable()
let table = PDFTable(rows: 0, columns: 0)

it("is possible to add a table into the default container") {
document.add(table: table)
Expand Down
16 changes: 8 additions & 8 deletions Tests/TPPDFTests/Table/PDFTable+Equatable_Spec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ class PDFTable_Equatable_Spec: QuickSpec {
var table: PDFTable!

beforeEach {
table = PDFTable()
table = PDFTable(rows: 0, columns: 0)
}

it("is equal") {
let otherTable = PDFTable()
let otherTable = PDFTable(rows: 0, columns: 0)
expect(table) == otherTable
}

it("is not equal with different style") {
let otherTable = PDFTable()
let otherTable = PDFTable(rows: 0, columns: 0)
otherTable.style = PDFTableStyle(rowHeaderCount: 2)
expect(table) != otherTable
}

it("is not equal with different cells") {
let otherTable = PDFTable()
let otherTable = PDFTable(rows: 0, columns: 0)
otherTable.cells = [[PDFTableCell(), PDFTableCell()]]
expect(table) != otherTable

Expand All @@ -44,25 +44,25 @@ class PDFTable_Equatable_Spec: QuickSpec {
}

it("is not equal with different widths") {
let otherTable = PDFTable()
let otherTable = PDFTable(rows: 0, columns: 0)
otherTable.widths = [0.25, 0.75]
expect(table) != otherTable
}

it("is not equal with different padding") {
let otherTable = PDFTable()
let otherTable = PDFTable(rows: 0, columns: 0)
otherTable.padding = 100
expect(table) != otherTable
}

it("is not equal with different margin") {
let otherTable = PDFTable()
let otherTable = PDFTable(rows: 0, columns: 0)
otherTable.margin = 100
expect(table) != otherTable
}

it("is not equal with different showHeadersOnEveryPage") {
let otherTable = PDFTable()
let otherTable = PDFTable(rows: 0, columns: 0)
otherTable.showHeadersOnEveryPage = !table.showHeadersOnEveryPage
expect(table) != otherTable
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/TPPDFTests/Table/PDFTable_Spec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PDFTable_Spec: QuickSpec {

context("variables") {

let table = PDFTable()
let table = PDFTable(rows: 0, columns: 0)

it("has a default style") {
expect(table.style) == PDFTableStyleDefaults.simple
Expand Down Expand Up @@ -48,7 +48,7 @@ class PDFTable_Spec: QuickSpec {
context("cell style") {

it("can set style of cell") {
let table = PDFTable()
let table = PDFTable(rows: 0, columns: 0)
table.cells = [[PDFTableCell()]]

let style = PDFTableCellStyle(colors: (fill: Color.green, text: Color.orange))
Expand Down

0 comments on commit d53c9ee

Please sign in to comment.