-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
PDFContextGraphics.swift
79 lines (70 loc) · 2.75 KB
/
PDFContextGraphics.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// PDFContextGraphics.swift
// TPPDF
//
// Created by Philip Niedertscheider on 24.06.20.
// Copyright © 2020 Philip Niedertscheider. All rights reserved.
//
import CoreGraphics
import Foundation
enum PDFContextGraphics {
static func createBitmapContext(size: CGSize) -> CGContext? {
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bytesPerPixel = 4
let bytesPerRow = bytesPerPixel * Int(size.width)
let rawData = malloc(Int(size.height) * bytesPerRow)
let bitsPerComponent = 8
return CGContext(data: rawData,
width: Int(size.width),
height: Int(size.height),
bitsPerComponent: bitsPerComponent,
bytesPerRow: bytesPerRow,
space: colorSpace,
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)
}
static func getImage(from context: CGContext, size: CGSize) -> Image? {
guard let cgImage = context.makeImage() else {
return nil
}
#if os(macOS)
return Image(cgImage: cgImage, size: size)
#elseif os(iOS)
return Image(cgImage: cgImage)
#endif
}
static func createPDFContext(url: URL, bounds: CGRect, info: PDFInfo) -> PDFContext {
var mediaBox = bounds
guard let context = CGContext(url as CFURL, mediaBox: &mediaBox, info.generate() as CFDictionary) else {
fatalError("Failed to create PDF rendering context for URL")
}
return PDFContext(cgContext: context)
}
static func createPDFDataContext(bounds: CGRect, info: PDFInfo) -> (NSMutableData, PDFContext) {
let data = NSMutableData()
let contextInfo = info.generate()
var mediaBox = bounds
guard let consumer = CGDataConsumer(data: data),
let context = CGContext(consumer: consumer, mediaBox: &mediaBox, contextInfo as CFDictionary) else {
fatalError("Failed to create PDF rendering context")
}
return (data, PDFContext(cgContext: context))
}
static func closePDFContext(_ context: PDFContext) {
context.closePDF()
}
static func beginPDFPage(in context: PDFContext, for bounds: CGRect, flipped: Bool = true) {
var mediaBox = bounds
let boxData = NSData(bytes: &mediaBox, length: MemoryLayout.size(ofValue: mediaBox))
let pageInfo = [
kCGPDFContextMediaBox as String: boxData
]
context.beginPDFPage(pageInfo as CFDictionary)
if flipped {
context.translateBy(x: 0, y: bounds.height)
context.scaleBy(x: 1, y: -1)
}
}
static func endPDFPage(in context: PDFContext) {
context.endPDFPage()
}
}