Skip to content

Commit

Permalink
Implement iOS-specific parts of library (lispkit pdf).
Browse files Browse the repository at this point in the history
  • Loading branch information
objecthub committed Dec 23, 2024
1 parent f9440c4 commit 252d59f
Show file tree
Hide file tree
Showing 5 changed files with 570 additions and 28 deletions.
10 changes: 6 additions & 4 deletions LispKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -6194,7 +6194,7 @@
GCC_C_LANGUAGE_STANDARD = gnu11;
INFOPLIST_FILE = "$(SRCROOT)/Sources/LispKit iOS/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
IPHONEOS_DEPLOYMENT_TARGET = 16.4;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -6240,7 +6240,7 @@
GCC_C_LANGUAGE_STANDARD = gnu11;
INFOPLIST_FILE = "$(SRCROOT)/Sources/LispKit iOS/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
IPHONEOS_DEPLOYMENT_TARGET = 16.4;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -6278,12 +6278,13 @@
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_ASSET_PATHS = "\"Sources/LispKitRepl iOS/Preview Content\"";
DEVELOPMENT_TEAM = C72Z63N8M5;
ENABLE_PREVIEWS = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
INFOPLIST_FILE = "Sources/LispKitRepl iOS/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
IPHONEOS_DEPLOYMENT_TARGET = 16.4;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -6312,12 +6313,13 @@
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_ASSET_PATHS = "\"Sources/LispKitRepl iOS/Preview Content\"";
DEVELOPMENT_TEAM = C72Z63N8M5;
ENABLE_PREVIEWS = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
INFOPLIST_FILE = "Sources/LispKitRepl iOS/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
IPHONEOS_DEPLOYMENT_TARGET = 16.4;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
3 changes: 3 additions & 0 deletions Sources/LispKit/Compiler/EvalError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ public enum EvalError: Int, Hashable, Codable {
case invalidPDFAccessIdentifier
case invalidPDFDocGenerationOption
case unknownPDFAnnotationType
case invalidPDFBorder

public var message: String {
switch self {
Expand Down Expand Up @@ -547,6 +548,8 @@ public enum EvalError: Int, Hashable, Codable {
return "invalid PDF document generation option: $0"
case .unknownPDFAnnotationType:
return "unknown PDF annotation type: $0"
case .invalidPDFBorder:
return "invalid PDF border specifier: $0"
}
}

Expand Down
57 changes: 56 additions & 1 deletion Sources/LispKit/Graphics/Drawing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public final class Drawing: NativeObject {
///
/// Enumeration of all supported drawing instructions.
///
public enum DrawingInstruction {
public enum DrawingInstruction: CustomStringConvertible {
case setStrokeColor(Color)
case setFillColor(Color)
case setStrokeWidth(Double)
Expand Down Expand Up @@ -465,6 +465,61 @@ public enum DrawingInstruction {
}
}

public var description: String {
switch self {
case .setStrokeColor(_):
return "setStrokeColor"
case .setFillColor(_):
return "setFillColor"
case .setStrokeWidth(_):
return "setStrokeWidth"
case .setBlendMode(_):
return "setBlendMode"
case .setShadow(_, dx: _, dy: _, blurRadius: _):
return "setShadow"
case .removeShadow:
return "setShadow"
case .setTransformation(_):
return "setTransformation"
case .concatTransformation(_):
return "concatTransformation"
case .undoTransformation(_):
return "undoTransformation"
case .strokeLine(_, _):
return "strokeLine"
case .strokeRect(_):
return "strokeRect"
case .fillRect(_):
return "fillRect"
case .strokeEllipse(_):
return "strokeEllipse"
case .fillEllipse(_):
return "fillEllipse"
case .stroke(_, width: _):
return "stroke"
case .strokeDashed(_, width: _, lengths: _, phase: _):
return "strokeDashed"
case .fill(_):
return "fill"
case .fillLinearGradient(_, _, angle: _):
return "fillLinearGradient"
case .fillRadialGradient(_, _, relativeCenter: _):
return "fillRadialGradient"
case .text(_, font: _, color: _, style: _, at: _):
return "text"
case .attributedText(_, at: _):
return "attributedText"
case .image(_, _, operation: _, opacity: _):
return "image"
case .page(_, _, _):
return "page"
case .inline(_):
return "inline"
case .include(_, clippedTo: _):
return "include"
}
}

func markDirty() {
switch self {
case .stroke(let shape, _):
Expand Down
16 changes: 16 additions & 0 deletions Sources/LispKit/Graphics/Drawing_iOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import CoreGraphics
import Foundation
import UIKit
import PDFKit
import MobileCoreServices
import UniformTypeIdentifiers

Expand Down Expand Up @@ -325,6 +326,7 @@ public enum DrawingInstruction: CustomStringConvertible, @unchecked Sendable {
case text(String, font: Font?, color: Color?, style: NSParagraphStyle?, at: ObjectLocation)
case attributedText(NSAttributedString, at: ObjectLocation)
case image(UIImage, ObjectLocation, operation: CGBlendMode, opacity: Double)
case page(PDFPage, PDFDisplayBox, CGRect)
case inline(Drawing)
case include(Drawing, clippedTo: Shape?)

Expand Down Expand Up @@ -498,6 +500,18 @@ public enum DrawingInstruction: CustomStringConvertible, @unchecked Sendable {
}
UIGraphicsPopContext()
}
case .page(let page, let box, let rect):
context.saveGState()
// PDF coordinate system is Y-flipped from Core Graphics
context.translateBy(x: rect.origin.x, y: rect.origin.y + rect.height)
// Apply the PDF's crop box transform
let bounds = page.bounds(for: box)
page.transform(context, for: box)
// Scale PDF to view size
context.scaleBy(x: rect.width / bounds.width, y: -rect.height / bounds.height)
// Draw
page.draw(with: box, to: context)
context.restoreGState()
case .include(let drawing, let clippingRegion):
drawing.draw(clippedTo: clippingRegion, in: context)
case .inline(let drawing):
Expand Down Expand Up @@ -551,6 +565,8 @@ public enum DrawingInstruction: CustomStringConvertible, @unchecked Sendable {
return "attributedText"
case .image(_, _, operation: _, opacity: _):
return "image"
case .page(_, _, _):
return "page"
case .inline(_):
return "inline"
case .include(_, clippedTo: _):
Expand Down
Loading

0 comments on commit 252d59f

Please sign in to comment.