pdfkit-rs 0.2.4

Safe Rust bindings for Apple's PDFKit framework — documents, pages, selections, outlines, annotations, destinations, actions, and view state on macOS
Documentation
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
import AppKit
import CoreGraphics
import Foundation
import PDFKit

public let PDFX_OK: Int32 = 0
public let PDFX_INVALID_ARGUMENT: Int32 = -1
public let PDFX_NULL_RESULT: Int32 = -2
public let PDFX_FRAMEWORK: Int32 = -3

final class PDFDocumentBox { let value: PDFDocument; init(_ value: PDFDocument) { self.value = value } }
final class PDFPageBox { let value: PDFPage; init(_ value: PDFPage) { self.value = value } }
final class PDFAnnotationBox { let value: PDFAnnotation; init(_ value: PDFAnnotation) { self.value = value } }
final class PDFOutlineBox { let value: PDFOutline; init(_ value: PDFOutline) { self.value = value } }
final class PDFSelectionBox { let value: PDFSelection; init(_ value: PDFSelection) { self.value = value } }
final class PDFViewBox { let value: PDFView; init(_ value: PDFView) { self.value = value } }
final class PDFThumbnailViewBox { let value: PDFThumbnailView; init(_ value: PDFThumbnailView) { self.value = value } }
final class PDFActionBox { let value: PDFAction; init(_ value: PDFAction) { self.value = value } }
final class PDFActionURLBox { let value: PDFActionURL; init(_ value: PDFActionURL) { self.value = value } }
final class PDFActionGoToBox { let value: PDFActionGoTo; init(_ value: PDFActionGoTo) { self.value = value } }
final class PDFActionNamedBox { let value: PDFActionNamed; init(_ value: PDFActionNamed) { self.value = value } }
final class PDFActionRemoteGoToBox {
    let value: PDFActionRemoteGoTo
    init(_ value: PDFActionRemoteGoTo) { self.value = value }
}
final class PDFActionResetFormBox {
    let value: PDFActionResetForm
    init(_ value: PDFActionResetForm) { self.value = value }
}
final class PDFBorderBox { let value: PDFBorder; init(_ value: PDFBorder) { self.value = value } }
final class PDFDestinationBox { let value: PDFDestination; init(_ value: PDFDestination) { self.value = value } }
final class PDFPageOverlayViewBox {
    let value: NSView
    init(_ value: NSView) { self.value = value }
}
final class PDFAppearanceCharacteristicsBox {
    let value: PDFAppearanceCharacteristics
    init(_ value: PDFAppearanceCharacteristics) { self.value = value }
}
final class PDFRustDocumentDelegateBox {
    let value: PDFRustDocumentDelegate
    init(_ value: PDFRustDocumentDelegate) { self.value = value }
}
final class PDFRustViewDelegateBox {
    let value: PDFRustViewDelegate
    init(_ value: PDFRustViewDelegate) { self.value = value }
}
final class PDFRustPageOverlayViewProviderBox {
    let value: PDFRustPageOverlayViewProvider
    init(_ value: PDFRustPageOverlayViewProvider) { self.value = value }
}

@inline(__always)
func pdf_retain_any(_ object: AnyObject) -> UnsafeMutableRawPointer {
    Unmanaged.passRetained(object).toOpaque()
}

@inline(__always)
func pdf_retain_document(_ value: PDFDocument) -> UnsafeMutableRawPointer { pdf_retain_any(PDFDocumentBox(value)) }
@inline(__always)
func pdf_retain_page(_ value: PDFPage) -> UnsafeMutableRawPointer { pdf_retain_any(PDFPageBox(value)) }
@inline(__always)
func pdf_retain_annotation(_ value: PDFAnnotation) -> UnsafeMutableRawPointer { pdf_retain_any(PDFAnnotationBox(value)) }
@inline(__always)
func pdf_retain_outline(_ value: PDFOutline) -> UnsafeMutableRawPointer { pdf_retain_any(PDFOutlineBox(value)) }
@inline(__always)
func pdf_retain_selection(_ value: PDFSelection) -> UnsafeMutableRawPointer { pdf_retain_any(PDFSelectionBox(value)) }
@inline(__always)
func pdf_retain_view(_ value: PDFView) -> UnsafeMutableRawPointer { pdf_retain_any(PDFViewBox(value)) }
@inline(__always)
func pdf_retain_thumbnail_view(_ value: PDFThumbnailView) -> UnsafeMutableRawPointer {
    pdf_retain_any(PDFThumbnailViewBox(value))
}
@inline(__always)
func pdf_retain_action(_ value: PDFAction) -> UnsafeMutableRawPointer { pdf_retain_any(PDFActionBox(value)) }
@inline(__always)
func pdf_retain_action_url(_ value: PDFActionURL) -> UnsafeMutableRawPointer { pdf_retain_any(PDFActionURLBox(value)) }
@inline(__always)
func pdf_retain_action_goto(_ value: PDFActionGoTo) -> UnsafeMutableRawPointer { pdf_retain_any(PDFActionGoToBox(value)) }
@inline(__always)
func pdf_retain_action_named(_ value: PDFActionNamed) -> UnsafeMutableRawPointer {
    pdf_retain_any(PDFActionNamedBox(value))
}
@inline(__always)
func pdf_retain_action_remote_goto(_ value: PDFActionRemoteGoTo) -> UnsafeMutableRawPointer {
    pdf_retain_any(PDFActionRemoteGoToBox(value))
}
@inline(__always)
func pdf_retain_action_reset_form(_ value: PDFActionResetForm) -> UnsafeMutableRawPointer {
    pdf_retain_any(PDFActionResetFormBox(value))
}
@inline(__always)
func pdf_retain_border(_ value: PDFBorder) -> UnsafeMutableRawPointer { pdf_retain_any(PDFBorderBox(value)) }
@inline(__always)
func pdf_retain_destination(_ value: PDFDestination) -> UnsafeMutableRawPointer { pdf_retain_any(PDFDestinationBox(value)) }
@inline(__always)
func pdf_retain_page_overlay_view(_ value: NSView) -> UnsafeMutableRawPointer {
    pdf_retain_any(PDFPageOverlayViewBox(value))
}
@inline(__always)
func pdf_retain_appearance_characteristics(_ value: PDFAppearanceCharacteristics) -> UnsafeMutableRawPointer {
    pdf_retain_any(PDFAppearanceCharacteristicsBox(value))
}
@inline(__always)
func pdf_retain_document_delegate(_ value: PDFRustDocumentDelegate) -> UnsafeMutableRawPointer {
    pdf_retain_any(PDFRustDocumentDelegateBox(value))
}
@inline(__always)
func pdf_retain_view_delegate(_ value: PDFRustViewDelegate) -> UnsafeMutableRawPointer {
    pdf_retain_any(PDFRustViewDelegateBox(value))
}
@inline(__always)
func pdf_retain_page_overlay_view_provider(_ value: PDFRustPageOverlayViewProvider) -> UnsafeMutableRawPointer {
    pdf_retain_any(PDFRustPageOverlayViewProviderBox(value))
}

@inline(__always)
func pdf_document_box(_ handle: UnsafeMutableRawPointer?) -> PDFDocumentBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFDocumentBox.self)
    return Unmanaged<PDFDocumentBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_page_box(_ handle: UnsafeMutableRawPointer?) -> PDFPageBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFPageBox.self)
    return Unmanaged<PDFPageBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_annotation_box(_ handle: UnsafeMutableRawPointer?) -> PDFAnnotationBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFAnnotationBox.self)
    return Unmanaged<PDFAnnotationBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_outline_box(_ handle: UnsafeMutableRawPointer?) -> PDFOutlineBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFOutlineBox.self)
    return Unmanaged<PDFOutlineBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_selection_box(_ handle: UnsafeMutableRawPointer?) -> PDFSelectionBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFSelectionBox.self)
    return Unmanaged<PDFSelectionBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_view_box(_ handle: UnsafeMutableRawPointer?) -> PDFViewBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFViewBox.self)
    return Unmanaged<PDFViewBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_thumbnail_view_box(_ handle: UnsafeMutableRawPointer?) -> PDFThumbnailViewBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFThumbnailViewBox.self)
    return Unmanaged<PDFThumbnailViewBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_action_box(_ handle: UnsafeMutableRawPointer?) -> PDFActionBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFActionBox.self)
    return Unmanaged<PDFActionBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_action_url_box(_ handle: UnsafeMutableRawPointer?) -> PDFActionURLBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFActionURLBox.self)
    return Unmanaged<PDFActionURLBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_action_goto_box(_ handle: UnsafeMutableRawPointer?) -> PDFActionGoToBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFActionGoToBox.self)
    return Unmanaged<PDFActionGoToBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_action_named_box(_ handle: UnsafeMutableRawPointer?) -> PDFActionNamedBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFActionNamedBox.self)
    return Unmanaged<PDFActionNamedBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_action_remote_goto_box(_ handle: UnsafeMutableRawPointer?) -> PDFActionRemoteGoToBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFActionRemoteGoToBox.self)
    return Unmanaged<PDFActionRemoteGoToBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_action_reset_form_box(_ handle: UnsafeMutableRawPointer?) -> PDFActionResetFormBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFActionResetFormBox.self)
    return Unmanaged<PDFActionResetFormBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_border_box(_ handle: UnsafeMutableRawPointer?) -> PDFBorderBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFBorderBox.self)
    return Unmanaged<PDFBorderBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_destination_box(_ handle: UnsafeMutableRawPointer?) -> PDFDestinationBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFDestinationBox.self)
    return Unmanaged<PDFDestinationBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_page_overlay_view_box(_ handle: UnsafeMutableRawPointer?) -> PDFPageOverlayViewBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFPageOverlayViewBox.self)
    return Unmanaged<PDFPageOverlayViewBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_appearance_characteristics_box(_ handle: UnsafeMutableRawPointer?) -> PDFAppearanceCharacteristicsBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFAppearanceCharacteristicsBox.self)
    return Unmanaged<PDFAppearanceCharacteristicsBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_document_delegate_box(_ handle: UnsafeMutableRawPointer?) -> PDFRustDocumentDelegateBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFRustDocumentDelegateBox.self)
    return Unmanaged<PDFRustDocumentDelegateBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_view_delegate_box(_ handle: UnsafeMutableRawPointer?) -> PDFRustViewDelegateBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFRustViewDelegateBox.self)
    return Unmanaged<PDFRustViewDelegateBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_page_overlay_view_provider_box(_ handle: UnsafeMutableRawPointer?) -> PDFRustPageOverlayViewProviderBox? {
    guard let handle else { return nil }
    let typed = handle.assumingMemoryBound(to: PDFRustPageOverlayViewProviderBox.self)
    return Unmanaged<PDFRustPageOverlayViewProviderBox>.fromOpaque(UnsafeRawPointer(typed)).takeUnretainedValue()
}

@inline(__always)
func pdf_document_value(_ handle: UnsafeMutableRawPointer?) -> PDFDocument? { pdf_document_box(handle)?.value }
@inline(__always)
func pdf_page_value(_ handle: UnsafeMutableRawPointer?) -> PDFPage? { pdf_page_box(handle)?.value }
@inline(__always)
func pdf_annotation_value(_ handle: UnsafeMutableRawPointer?) -> PDFAnnotation? { pdf_annotation_box(handle)?.value }
@inline(__always)
func pdf_outline_value(_ handle: UnsafeMutableRawPointer?) -> PDFOutline? { pdf_outline_box(handle)?.value }
@inline(__always)
func pdf_selection_value(_ handle: UnsafeMutableRawPointer?) -> PDFSelection? { pdf_selection_box(handle)?.value }
@inline(__always)
func pdf_view_value(_ handle: UnsafeMutableRawPointer?) -> PDFView? { pdf_view_box(handle)?.value }
@inline(__always)
func pdf_thumbnail_view_value(_ handle: UnsafeMutableRawPointer?) -> PDFThumbnailView? { pdf_thumbnail_view_box(handle)?.value }
@inline(__always)
func pdf_action_value(_ handle: UnsafeMutableRawPointer?) -> PDFAction? { pdf_action_box(handle)?.value }
@inline(__always)
func pdf_action_url_value(_ handle: UnsafeMutableRawPointer?) -> PDFActionURL? { pdf_action_url_box(handle)?.value }
@inline(__always)
func pdf_action_goto_value(_ handle: UnsafeMutableRawPointer?) -> PDFActionGoTo? { pdf_action_goto_box(handle)?.value }
@inline(__always)
func pdf_action_named_value(_ handle: UnsafeMutableRawPointer?) -> PDFActionNamed? { pdf_action_named_box(handle)?.value }
@inline(__always)
func pdf_action_remote_goto_value(_ handle: UnsafeMutableRawPointer?) -> PDFActionRemoteGoTo? {
    pdf_action_remote_goto_box(handle)?.value
}
@inline(__always)
func pdf_action_reset_form_value(_ handle: UnsafeMutableRawPointer?) -> PDFActionResetForm? {
    pdf_action_reset_form_box(handle)?.value
}
@inline(__always)
func pdf_any_action_value(_ handle: UnsafeMutableRawPointer?) -> PDFAction? {
    pdf_action_value(handle)
        ?? pdf_action_url_value(handle)
        ?? pdf_action_goto_value(handle)
        ?? pdf_action_named_value(handle)
        ?? pdf_action_remote_goto_value(handle)
        ?? pdf_action_reset_form_value(handle)
}
@inline(__always)
func pdf_border_value(_ handle: UnsafeMutableRawPointer?) -> PDFBorder? { pdf_border_box(handle)?.value }
@inline(__always)
func pdf_destination_value(_ handle: UnsafeMutableRawPointer?) -> PDFDestination? { pdf_destination_box(handle)?.value }
@inline(__always)
func pdf_page_overlay_view_value(_ handle: UnsafeMutableRawPointer?) -> NSView? {
    pdf_page_overlay_view_box(handle)?.value
}
@inline(__always)
func pdf_appearance_characteristics_value(_ handle: UnsafeMutableRawPointer?) -> PDFAppearanceCharacteristics? {
    pdf_appearance_characteristics_box(handle)?.value
}
@inline(__always)
func pdf_document_delegate_value(_ handle: UnsafeMutableRawPointer?) -> PDFRustDocumentDelegate? {
    pdf_document_delegate_box(handle)?.value
}
@inline(__always)
func pdf_view_delegate_value(_ handle: UnsafeMutableRawPointer?) -> PDFRustViewDelegate? {
    pdf_view_delegate_box(handle)?.value
}
@inline(__always)
func pdf_page_overlay_view_provider_value(_ handle: UnsafeMutableRawPointer?) -> PDFRustPageOverlayViewProvider? {
    pdf_page_overlay_view_provider_box(handle)?.value
}

@inline(__always)
public func pdf_string(_ string: String) -> UnsafeMutablePointer<CChar>? {
    string.withCString { strdup($0) }
}

public enum PDFBridgeError: Error, CustomStringConvertible {
    case invalidArgument(String)
    case nullResult(String)
    case framework(String)

    public var description: String {
        switch self {
        case .invalidArgument(let message), .nullResult(let message), .framework(let message):
            return message
        }
    }

    public var statusCode: Int32 {
        switch self {
        case .invalidArgument:
            return PDFX_INVALID_ARGUMENT
        case .nullResult:
            return PDFX_NULL_RESULT
        case .framework:
            return PDFX_FRAMEWORK
        }
    }
}

@inline(__always)
public func pdf_fail(
    _ outError: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?,
    _ error: Error
) -> Int32 {
    let bridgeError = (error as? PDFBridgeError) ?? .framework((error as NSError).localizedDescription)
    outError?.pointee = pdf_string(bridgeError.description)
    return bridgeError.statusCode
}

@inline(__always)
public func pdf_run(
    _ outError: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?,
    _ work: () throws -> Void
) -> Int32 {
    do {
        try work()
        outError?.pointee = nil
        return PDFX_OK
    } catch {
        return pdf_fail(outError, error)
    }
}

func pdf_json_string(from value: Any) -> String? {
    guard JSONSerialization.isValidJSONObject(value) else { return nil }
    do {
        let data = try JSONSerialization.data(withJSONObject: value, options: [.sortedKeys])
        return String(data: data, encoding: .utf8)
    } catch {
        return nil
    }
}

func pdf_optional_string(_ value: UnsafePointer<CChar>?) -> String? {
    guard let value else { return nil }
    return String(cString: value)
}

func pdf_take_string(_ value: UnsafeMutablePointer<CChar>?) -> String? {
    guard let value else { return nil }
    defer { free(value) }
    return String(cString: value)
}

func pdf_rect_dict(_ rect: CGRect) -> [String: Any] {
    [
        "x": rect.origin.x,
        "y": rect.origin.y,
        "width": rect.size.width,
        "height": rect.size.height,
    ]
}

func pdf_point_dict(_ point: CGPoint) -> [String: Any] {
    ["x": point.x, "y": point.y]
}

func pdf_size_dict(_ size: CGSize) -> [String: Any] {
    ["width": size.width, "height": size.height]
}

func pdf_range_dict(_ range: NSRange) -> [String: Any] {
    ["location": range.location, "length": range.length]
}

func pdf_insets_dict(_ insets: NSEdgeInsets) -> [String: Any] {
    [
        "top": insets.top,
        "left": insets.left,
        "bottom": insets.bottom,
        "right": insets.right,
    ]
}

func pdf_color_dict(_ color: NSColor?) -> [String: Any]? {
    guard let color else { return nil }
    let converted = color.usingColorSpace(.sRGB) ?? color.usingColorSpace(.deviceRGB)
    guard let converted else { return nil }
    return [
        "red": converted.redComponent,
        "green": converted.greenComponent,
        "blue": converted.blueComponent,
        "alpha": converted.alphaComponent,
    ]
}

func pdf_color(red: Double, green: Double, blue: Double, alpha: Double) -> NSColor {
    NSColor(
        calibratedRed: CGFloat(red),
        green: CGFloat(green),
        blue: CGFloat(blue),
        alpha: CGFloat(alpha)
    )
}

func pdf_border_info_dict(_ border: PDFBorder?) -> [String: Any]? {
    guard let border else { return nil }
    return [
        "style": border.style.rawValue,
        "line_width": border.lineWidth,
        "dash_pattern": border.dashPattern?.compactMap { ($0 as? NSNumber)?.doubleValue } as Any,
    ]
}

func pdf_display_box(_ rawValue: Int32) throws -> PDFDisplayBox {
    guard let box = PDFDisplayBox(rawValue: Int(rawValue)) else {
        throw PDFBridgeError.invalidArgument("invalid PDFDisplayBox: \(rawValue)")
    }
    return box
}

func pdf_border_style(_ rawValue: Int32) throws -> PDFBorderStyle {
    guard let style = PDFBorderStyle(rawValue: Int(rawValue)) else {
        throw PDFBridgeError.invalidArgument("invalid PDFBorderStyle: \(rawValue)")
    }
    return style
}

func pdf_display_mode(_ rawValue: Int32) throws -> PDFDisplayMode {
    guard let mode = PDFDisplayMode(rawValue: Int(rawValue)) else {
        throw PDFBridgeError.invalidArgument("invalid PDFDisplayMode: \(rawValue)")
    }
    return mode
}

func pdf_display_direction(_ rawValue: Int32) throws -> PDFDisplayDirection {
    guard let direction = PDFDisplayDirection(rawValue: Int(rawValue)) else {
        throw PDFBridgeError.invalidArgument("invalid PDFDisplayDirection: \(rawValue)")
    }
    return direction
}

func pdf_interpolation_quality(_ rawValue: Int32) throws -> PDFInterpolationQuality {
    guard let quality = PDFInterpolationQuality(rawValue: Int(rawValue)) else {
        throw PDFBridgeError.invalidArgument("invalid PDFInterpolationQuality: \(rawValue)")
    }
    return quality
}

func pdf_widget_control_type(_ rawValue: Int32) throws -> PDFWidgetControlType {
    guard let controlType = PDFWidgetControlType(rawValue: Int(rawValue)) else {
        throw PDFBridgeError.invalidArgument("invalid PDFWidgetControlType: \(rawValue)")
    }
    return controlType
}

func pdf_action_named_name(_ rawValue: Int32) throws -> PDFActionNamedName {
    guard let name = PDFActionNamedName(rawValue: Int(rawValue)) else {
        throw PDFBridgeError.invalidArgument("invalid PDFActionNamedName: \(rawValue)")
    }
    return name
}

@_cdecl("pdf_object_retain")
public func pdf_object_retain(_ handle: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? {
    guard let handle else { return nil }
    let object = Unmanaged<AnyObject>.fromOpaque(handle).takeUnretainedValue()
    return Unmanaged.passRetained(object).toOpaque()
}

@_cdecl("pdf_object_release")
public func pdf_object_release(_ handle: UnsafeMutableRawPointer?) {
    guard let handle else { return }
    Unmanaged<AnyObject>.fromOpaque(handle).release()
}