Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #44

Merged
merged 4 commits into from
Dec 3, 2023
Merged
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
9 changes: 4 additions & 5 deletions Sources/Csv2Img/Csv.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ extension Csv {
/**
`ExportType` is a enum that expresses
*/
public enum ExportType: String, Hashable {
public enum ExportType: String, Hashable, CaseIterable {
/// `png` output
case png
/// `pdf` output (Work In Progress)
Expand Down Expand Up @@ -365,10 +365,9 @@ extension Csv {
}
if let maker = maker as? ImageMaker {
if let fontSize = fontSize {
maker.setFontSize(fontSize)
maker.set(fontSize: fontSize)
}
// TODO: When Swift5.7 is supported officailly, replace `CGImage` with `any CsvExportable`.
let exportable: CGImage = try await withCheckedThrowingContinuation { continuation in
let exportable: any CsvExportable = try await withCheckedThrowingContinuation { continuation in
queue.async { [weak self] in
guard let self = self else {
continuation.resume(throwing: Csv.Error.underlying(nil))
Expand All @@ -389,7 +388,7 @@ extension Csv {
return AnyCsvExportable(exportable)
} else if let maker = maker as? PdfMaker {
if let fontSize = fontSize {
maker.setFontSize(fontSize)
maker.set(fontSize: fontSize)
}
let exportable: PDFDocument = try await withCheckedThrowingContinuation { continuation in
queue.async { [weak self] in
Expand Down
4 changes: 2 additions & 2 deletions Sources/Csv2Img/CsvColumn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension Csv {
/// →Column is [1, 2, 3, 4] and Row is [5, 6, 7, 8].
///
/// Because this class is usually initialized via ``Csv``, you do not have to take care about ``Column`` in detail.
public struct Column {
public struct Column: Sendable {
public var name: Name
public var style: Style

Expand All @@ -37,7 +37,7 @@ extension Csv {

extension Csv.Column {
/// ``Style`` decides the appearance of certain ``Column`` group.
public struct Style {
public struct Style: Sendable {
/// `color` is a ``CGColor`` corresponding to textColor which is used when drawing
public var color: CGColor
/// `applyOnlyColumn` determines whether this style affects both `Column` and `Row` or not.
Expand Down
6 changes: 2 additions & 4 deletions Sources/Csv2Img/ImageMaker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ public enum ImageMakingError: Error {
/// No overview available
protocol ImageMakerType: Maker {
var latestOutput: CGImage? { get }
func make(columns: [Csv.Column], rows: [Csv.Row], progress: @escaping (Double) -> Void) throws -> CGImage
func setFontSize(_ size: Double)
}

/// `ImageMarker` generate png-image from ``Csv``.
class ImageMaker: ImageMakerType {
final class ImageMaker: ImageMakerType {

typealias Exportable = CGImage

Expand All @@ -48,7 +46,7 @@ class ImageMaker: ImageMakerType {

var latestOutput: CGImage?

func setFontSize(_ size: Double) {
func set(fontSize size: Double) {
self.fontSize = size
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/Csv2Img/Maker.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Foundation

public protocol Maker {
public protocol Maker: Sendable {
associatedtype Exportable: CsvExportable

var maximumRowCount: Int? { get }

func make(columns: [Csv.Column], rows: [Csv.Row], progress: @escaping (Double) -> Void) throws -> Exportable
func setFontSize(_ size: Double)
func make(columns: [Csv.Column], rows: [Csv.Row], progress: @escaping @Sendable (Double) -> Void) throws -> Exportable
func set(fontSize size: Double)
}
19 changes: 9 additions & 10 deletions Sources/Csv2Img/PdfMaker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ public enum PdfMakingError: Error {
/// Failed to get/create `CGContext`.
case noContextAvailabe
case failedToGeneratePdf
case failedToSavePdf(at : String)
case emptyRows
case underlying(Error)
}

/// No overview available
protocol PdfMakerType: Maker {
var latestOutput: PDFDocument? { get }
func make(columns: [Csv.Column], rows: [Csv.Row], progress: @escaping (Double) -> Void) throws -> PDFDocument
func setMetadata(_ metadata: PDFMetadata)
func setFontSize(_ size: Double)
}

/// ``PdfMaker`` generate pdf from ``Csv`` (Work In Progress).
class PdfMaker: PdfMakerType {
final class PdfMaker: PdfMakerType {

typealias Exportable = PDFDocument

Expand All @@ -33,17 +32,16 @@ class PdfMaker: PdfMakerType {
self.metadata = metadata
}

var maximumRowCount: Int?
var fontSize: Double
let maximumRowCount: Int?
private(set) var fontSize: Double
var metadata: PDFMetadata

var latestOutput: PDFDocument?

func setFontSize(_ size: Double) {
func set(fontSize size: Double) {
self.fontSize = size
}


/// generate png-image data from ``Csv``.
func make(
columns: [Csv.Column],
Expand All @@ -53,6 +51,7 @@ class PdfMaker: PdfMakerType {
// NOTE: Anchor is bottom-left.
let horizontalSpace: Double = 8
let verticalSpace: Double = 12
let maxRowsHeight: Double = 480

let size = min(maximumRowCount ?? rows.count, rows.count)
let rows = rows[..<size].map { $0 }
Expand Down Expand Up @@ -82,13 +81,13 @@ class PdfMaker: PdfMakerType {
let width = (longestWidth + horizontalSpace) * Double(columns.count)
let allRowsHeight = Double(rows.count) * (longestHeight + verticalSpace)

let maxRowsHeight = min(480, allRowsHeight)
let largestRowsHeight = min(maxRowsHeight, allRowsHeight)

let totalPageNumber = Int(allRowsHeight / maxRowsHeight)
let totalPageNumber = Int(allRowsHeight / largestRowsHeight)

let totalHeight = allRowsHeight + Double(totalPageNumber) * rowHeight

let pageHeight = min(maxRowsHeight + rowHeight, totalHeight)
let pageHeight = min(largestRowsHeight + rowHeight, totalHeight)

var mediaBox = CGRect(
origin: .zero,
Expand Down
7 changes: 4 additions & 3 deletions Sources/Csv2Img/TypeConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import CoreFoundation
import CoreGraphics
import PDFKit

public protocol CsvExportable {
public protocol CsvExportable: Sendable {
}

public class AnyCsvExportable: CsvExportable {
public final class AnyCsvExportable: CsvExportable {

public var base: CsvExportable
public let base: CsvExportable

public init(_ csvExportable: CsvExportable) {
self.base = csvExportable
Expand All @@ -16,4 +16,5 @@ public class AnyCsvExportable: CsvExportable {

extension CGImage: CsvExportable { }
extension Data: CsvExportable { }
extension PDFDocument: @unchecked Sendable {}
extension PDFDocument: CsvExportable { }