KMEditPDFModel.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // KMEditPDFModel.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2024/6/25.
  6. //
  7. import Cocoa
  8. class KMEditPDFModel: NSObject {
  9. //
  10. // var cFont: CPDFFont?
  11. var editingAreas: [CPDFEditArea]?
  12. var textAreas: [CPDFEditTextArea]? {
  13. get {
  14. guard let areas = self.editingAreas else {
  15. return nil
  16. }
  17. var datas: [CPDFEditTextArea] = []
  18. for area in areas {
  19. if let data = area as? CPDFEditTextArea {
  20. datas.append(data)
  21. continue
  22. }
  23. }
  24. return datas
  25. }
  26. }
  27. var imageAreas: [CPDFEditImageArea]? {
  28. get {
  29. guard let areas = self.editingAreas else {
  30. return nil
  31. }
  32. var datas: [CPDFEditImageArea] = []
  33. for area in areas {
  34. if let data = area as? CPDFEditImageArea {
  35. datas.append(data)
  36. continue
  37. }
  38. }
  39. return datas
  40. }
  41. }
  42. var fontColors: [NSColor] = []
  43. var fontNames: [String] = []
  44. var fontSizes: [CGFloat] = []
  45. var fontBolds: [Bool] = []
  46. var fontItalics: [Bool] = []
  47. var textAlignments: [NSTextAlignment] = []
  48. var boundsArray: [NSRect] = []
  49. var rotates: [CGFloat] = []
  50. var opacitys: [CGFloat] = []
  51. func editAreasFontColorIsEqual() -> Bool {
  52. let datas = self.fontColors
  53. if datas.isEmpty {
  54. return false
  55. }
  56. if datas.count == 1 {
  57. return true
  58. }
  59. let color = datas.first
  60. for (i, d) in datas.enumerated() {
  61. if i == 0 {
  62. continue
  63. }
  64. if d != color {
  65. return false
  66. }
  67. }
  68. return true
  69. }
  70. func editAreasFontNameIsEqual() -> Bool {
  71. let datas = self.fontNames
  72. if datas.isEmpty {
  73. return false
  74. }
  75. if datas.count == 1 {
  76. return true
  77. }
  78. let data = datas.first
  79. for (i, d) in datas.enumerated() {
  80. if i == 0 {
  81. continue
  82. }
  83. if d != data {
  84. return false
  85. }
  86. }
  87. return true
  88. }
  89. func editAreasFontSizeIsEqual() -> Bool {
  90. let datas = self.fontSizes
  91. if datas.isEmpty {
  92. return false
  93. }
  94. if datas.count == 1 {
  95. return true
  96. }
  97. let data = datas.first
  98. for (i, d) in datas.enumerated() {
  99. if i == 0 {
  100. continue
  101. }
  102. if d != data {
  103. return false
  104. }
  105. }
  106. return true
  107. }
  108. func editAreasFontBoldIsEqual() -> Bool {
  109. let datas = self.fontBolds
  110. if datas.isEmpty {
  111. return false
  112. }
  113. if datas.count == 1 {
  114. return true
  115. }
  116. let data = datas.first
  117. for (i, d) in datas.enumerated() {
  118. if i == 0 {
  119. continue
  120. }
  121. if d != data {
  122. return false
  123. }
  124. }
  125. return true
  126. }
  127. func editAreasFontItalicIsEqual() -> Bool {
  128. let datas = self.fontItalics
  129. if datas.isEmpty {
  130. return false
  131. }
  132. if datas.count == 1 {
  133. return true
  134. }
  135. let data = datas.first
  136. for (i, d) in datas.enumerated() {
  137. if i == 0 {
  138. continue
  139. }
  140. if d != data {
  141. return false
  142. }
  143. }
  144. return true
  145. }
  146. func editAreasTextAlignmentIsEqual() -> Bool {
  147. let datas = self.textAlignments
  148. if datas.isEmpty {
  149. return false
  150. }
  151. if datas.count == 1 {
  152. return true
  153. }
  154. let data = datas.first?.rawValue ?? 0
  155. for (i, d) in datas.enumerated() {
  156. if i == 0 {
  157. continue
  158. }
  159. if d.rawValue != data {
  160. return false
  161. }
  162. }
  163. return true
  164. }
  165. func editAreasBoundsIsEqualForWidth() -> Bool {
  166. let datas = self.boundsArray
  167. if datas.isEmpty {
  168. return false
  169. }
  170. if datas.count == 1 {
  171. return true
  172. }
  173. let width = datas.first?.width ?? 0
  174. for (i, rect) in datas.enumerated() {
  175. if i == 0 {
  176. continue
  177. }
  178. if width != rect.size.width {
  179. return false
  180. }
  181. }
  182. return true
  183. }
  184. func editAreasBoundsIsEqualForHeight() -> Bool {
  185. let datas = self.boundsArray
  186. if datas.isEmpty {
  187. return false
  188. }
  189. if datas.count == 1 {
  190. return true
  191. }
  192. let height = datas.first?.height ?? 0
  193. for (i, rect) in datas.enumerated() {
  194. if i == 0 {
  195. continue
  196. }
  197. if height != rect.size.height {
  198. return false
  199. }
  200. }
  201. return true
  202. }
  203. func editAreasRotateIsEqual() -> Bool {
  204. let datas = self.rotates
  205. if datas.isEmpty {
  206. return false
  207. }
  208. // if self.editAreasHavTextArea() {
  209. // return false
  210. // }
  211. // let datas = self._editAreasRotates()
  212. if datas.count == 1 {
  213. return true
  214. }
  215. let data = datas.first ?? 0
  216. for (i, d) in datas.enumerated() {
  217. if i == 0 {
  218. continue
  219. }
  220. if data != d {
  221. return false
  222. }
  223. }
  224. return true
  225. }
  226. func editAreasOpacityIsEqual() -> Bool {
  227. let datas = self.opacitys
  228. if datas.isEmpty {
  229. return false
  230. }
  231. if datas.count == 1 {
  232. return true
  233. }
  234. let data = datas.first ?? 0
  235. for (i, d) in datas.enumerated() {
  236. if i == 0 {
  237. continue
  238. }
  239. if data != d {
  240. return false
  241. }
  242. }
  243. return true
  244. }
  245. }