123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- //
- // KMMergeFileModel.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2022/11/24.
- //
- import Cocoa
- import PDFKit
- enum KMMergeFilePageRange: String {
- case all = "All Page"
- case oddPages = "Odd pages Only"
- case evenPages = "Even pages Only"
- case custom = ""
- }
- class KMMergeFileModel: NSObject {
- var documentURL: URL!
- var document: CPDFDocument!
- var open : Bool = false
- var page: CPDFPage!
-
- var password: String!
-
- var myDocument: CPDFDocument!
-
- var selected: Bool = false
- var pageRange: KMMergeFilePageRange = .all
- var pageRangeString: String = ""
-
- var pages: [KMMergePageModel] = []
-
- func canExpand() -> Bool {
- return document.pageCount > 1
- }
-
- func updateChildPageRange(pageRange: KMMergeFilePageRange) {
- if pageRange == .all {
- for pageModel in pages {
- pageModel.selected = true
- }
- } else if pageRange == .oddPages {
- var i = 0
- for pageModel in pages {
- if i % 2 == 0 {
- pageModel.selected = true
- } else {
- pageModel.selected = false
- }
- i += 1
- }
- } else if pageRange == .evenPages {
- var i = 0
- for pageModel in pages {
- if i % 2 == 0 {
- pageModel.selected = false
- } else {
- pageModel.selected = true
- }
- i += 1
- }
- } else if pageRange == .custom {
-
- }
- }
-
- func updatePagePange() {
- var allPage = true
- var oddPage = true
- var evenPage = true
- var selectedPages: [Int] = []
-
- for model in pages {
- if model.selected {
- selectedPages.append(model.pageID+1)
-
- if model.pageID % 2 == 1 { /// 偶数
- oddPage = false
- } else { /// 奇数
- evenPage = false
- }
- } else {
- allPage = false
-
- if model.pageID % 2 == 0 { /// 奇数
- oddPage = false
- } else { /// 偶数
- evenPage = false
- }
- }
- }
-
- if allPage {
- pageRange = .all
- } else if oddPage {
- pageRange = .oddPages
- } else if evenPage {
- pageRange = .evenPages
- } else {
- pageRange = .custom
- var string: String = ""
- var index = 0
- var prePage = 0
- var count = 0
- var cubeStart = NSNotFound
- var cubeEnd = NSNotFound
- for i in selectedPages {
- if selectedPages.count == 1 {
- string.append(String(i))
- } else if selectedPages.count == 2 {
- string.append(String(i))
- if index != selectedPages.count-1 {
- string.append(",")
- }
- } else {
- if index == 0 {
- string.append(String(i))
- } else {
- if i - prePage == 1 {
- if count == 0 {
- cubeStart = prePage
- }
-
- count += 1
- } else {
- count = 0
- }
-
- if count == 0 || index == selectedPages.count-1 { /// 块结束
- if cubeStart != NSNotFound && cubeEnd != NSNotFound { /// 有效块
- if cubeStart == selectedPages.first {
- string.removeFirst()
- } else {
- string.append(",")
- }
-
-
- if index == selectedPages.count-1 {
- if count == 0 {
- string.append(String(cubeStart))
- string.append("-")
- string.append(String(cubeEnd))
-
- string.append(",")
- string.append(String(i))
- } else {
- string.append(String(cubeStart))
- string.append("-")
- string.append(String(i))
- }
- } else {
- string.append(String(cubeStart))
- string.append("-")
- string.append(String(cubeEnd))
- }
- } else {
- if prePage == selectedPages.first {
- // string.append(",")
- } else {
- string.append(",")
- string.append(String(prePage))
- }
-
- cubeStart = NSNotFound
- cubeEnd = NSNotFound
- }
- }
- // else if count == 1 {
- // if index == selectedPages.count-1 {
- // if prePage == selectedPages.first {
- //// string.append(",")
- // } else {
- // string.append(",")
- // string.append(String(prePage))
- // }
- // }
- // }
- else if count >= 2 { /// 可成块
- cubeEnd = i
- }
- }
- }
-
- prePage = i
- index += 1
- }
- pageRangeString = string
- }
- }
- }
|