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
20 changes: 0 additions & 20 deletions Source/Internal/Graphics/PDFContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,18 @@ public class PDFContext {

internal func beginPDFPage(_ pageInfo: CFDictionary?) {
// Do not create page immediately, instead invoke it as soon as necessary
print("beginPDFPage")
delayedCommands.append(.beginPDFPage(pageConfig: pageInfo))
currentPageContainsDrawnContent = false
hasActivePage = true
}

internal func endPDFPage() {
print("endPDFPage")
applyDelayedCommands()
cgContext.endPDFPage()
hasActivePage = false
}

internal func closePDF() {
print("closePDFPage")
applyDelayedCommands()
cgContext.closePDF()
hasActivePage = false
Expand All @@ -61,33 +58,28 @@ public class PDFContext {
// MARK: - Translation

internal func translateBy(x: CGFloat, y: CGFloat) {
print("translateBy")
delayedCommands.append(.translateBy(x: x, y: y))
}

internal func scaleBy(x: CGFloat, y: CGFloat) {
print("scaleBy")
delayedCommands.append(.scaleBy(x: x, y: y))
}

// MARK: - Drawing

internal func drawPath(using mode: CGPathDrawingMode) {
print("drawPath")
applyDelayedCommands()
cgContext.drawPath(using: mode)
currentPageContainsDrawnContent = true
}

internal func drawPDFPage(_ page: CGPDFPage) {
print("drawPDFPage")
applyDelayedCommands()
cgContext.drawPDFPage(page)
currentPageContainsDrawnContent = true
}

internal func draw(image: CGImage, in frame: CGRect, flipped: Bool) {
print("draw(image:)")
applyDelayedCommands()
cgContext.draw(image: image, in: frame, flipped: flipped)
currentPageContainsDrawnContent = true
Expand All @@ -96,7 +88,6 @@ public class PDFContext {
// MARK: - Colors

internal func setFillColor(_ color: CGColor) {
print("setFillColor")
applyDelayedCommands()
cgContext.setFillColor(color)
currentPageContainsDrawnContent = true
Expand All @@ -105,42 +96,36 @@ public class PDFContext {
// MARK: - Paths

internal func beginPath() {
print("beginPath")
applyDelayedCommands()
cgContext.beginPath()
currentPageContainsDrawnContent = true
}

internal func addPath(_ path: CGPath) {
print("addPath")
applyDelayedCommands()
cgContext.addPath(path)
currentPageContainsDrawnContent = true
}

internal func setLineDash(phase: CGFloat, lengths: [CGFloat]) {
print("setLineDash")
applyDelayedCommands()
cgContext.setLineDash(phase: phase, lengths: lengths)
currentPageContainsDrawnContent = true
}

internal func setLineCap(_ cap: CGLineCap) {
print("setLineCap")
applyDelayedCommands()
cgContext.setLineCap(cap)
currentPageContainsDrawnContent = true
}

internal func setLineWidth(_ width: CGFloat) {
print("setLineWidth")
applyDelayedCommands()
cgContext.setLineWidth(width)
currentPageContainsDrawnContent = true
}

internal func setStrokeColor(_ color: CGColor) {
print("setStrokeColor")
applyDelayedCommands()
cgContext.setStrokeColor(color)
currentPageContainsDrawnContent = true
Expand All @@ -149,13 +134,11 @@ public class PDFContext {
// MARK: - State

internal func saveGState() {
print("saveGState")
applyDelayedCommands()
cgContext.saveGState()
}

internal func restoreGState() {
print("restoreGState")
applyDelayedCommands()
cgContext.restoreGState()
}
Expand All @@ -172,7 +155,6 @@ public class PDFContext {
}

internal func draw(ctFrame frameRef: CTFrame) {
print("draw(ctFrame:)")
applyDelayedCommands()
CTFrameDraw(frameRef, cgContext)
currentPageContainsDrawnContent = true
Expand All @@ -181,7 +163,6 @@ public class PDFContext {
// MARK: - Masking

internal func clip() {
print("clip")
applyDelayedCommands()
cgContext.clip()
currentPageContainsDrawnContent = true
Expand All @@ -190,7 +171,6 @@ public class PDFContext {
// MARK: - Metadata

internal func setURL(_ url: CFURL, for rect: CGRect) {
print("setURL")
applyDelayedCommands()
cgContext.setURL(url, for: rect)
currentPageContainsDrawnContent = true
Expand Down
33 changes: 33 additions & 0 deletions Source/Internal/Table/PDFTableObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ internal class PDFTableObject: PDFRenderObject {
let startPosition: CGPoint = cells.first?.frames.cell.origin ?? .zero
var nextPageCells: [PDFTableCalculatedCell] = cells
var pageEnd = CGPoint.null
var headerShift = true

repeat {
var pageStart = CGPoint.null
Expand All @@ -296,7 +297,9 @@ internal class PDFTableObject: PDFRenderObject {
var cellFrame = item.frames.cell
var contentFrame = item.frames.content
cellFrame.origin.y -= startPosition.y - minOffset
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)
Expand Down Expand Up @@ -329,6 +332,13 @@ internal class PDFTableObject: PDFRenderObject {
}
minOffset += headerHeight
}
if !firstPage {
// shift the rest of the cells down by headerHeight
if headerShift {
nextPageCells = shiftCellsBy(cells: nextPageCells, shiftValue: headerHeight)
headerShift = false
}
}

let filterResult = filterCellsOnPage(for: generator,
items: nextPageCells,
Expand Down Expand Up @@ -448,9 +458,32 @@ internal class PDFTableObject: PDFRenderObject {
result.remainder.append(nextPageCell)
}
}
// reposition the cells with the top of the page )minOffset)
if let cellFrame = result.remainder.first {
if cellFrame.frames.cell.origin.y < minOffset {
// if cells are higher up on the page than minOffset, shift them down
let shiftValue = minOffset - cellFrame.frames.cell.origin.y
result.remainder = shiftCellsBy(cells: result.remainder, shiftValue: shiftValue)
}
}
return result
}

internal typealias ShiftedCells = ([PDFTableCalculatedCell])

internal func shiftCellsBy(cells: [PDFTableCalculatedCell], shiftValue: CGFloat) -> ShiftedCells {
var shiftedCells: [PDFTableCalculatedCell] = []

for cell in cells {
var shiftedCell = cell

shiftedCell.frames.cell.origin.y += shiftValue
shiftedCell.frames.content.origin.y += shiftValue
shiftedCells.append(shiftedCell)
}
return shiftedCells
}

internal func createSliceObject(frame: CGRect, elements: [PDFRenderObject], minOffset: CGFloat, maxOffset: CGFloat) -> PDFSlicedObject {
let sliceObject = PDFSlicedObject(children: elements, frame: frame)
if frame.maxY > maxOffset {
Expand Down
3 changes: 3 additions & 0 deletions Source/Internal/Utils/PDFCalculations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ internal enum PDFCalculations {
return pageLayout.height
- layout.margin.top
- layout.heights.maxHeaderHeight()
- pageLayout.space.header
- layout.heights.content
- generator.currentPadding.bottom
- layout.heights.maxFooterHeight()
Expand All @@ -153,8 +154,10 @@ internal enum PDFCalculations {

internal static func calculateTopMinimum(for generator: PDFGenerator) -> CGFloat {
let layout = generator.layout
let pageLayout = generator.document.layout
return layout.margin.top
+ layout.heights.maxHeaderHeight()
+ pageLayout.space.header
}

/// Calculates the maximum offset from the top edge when the main content should break to the next page
Expand Down