KMBOTAAnnotationTool.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // KMBOTAAnnotationTool.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/4/6.
  6. //
  7. import Cocoa
  8. class KMBOTAAnnotationTool: NSObject {
  9. static func fetchCellHeight(annotation: CPDFAnnotation, maxSize: CGSize) -> CGFloat {
  10. var height: CGFloat = 48
  11. let contentLabelString = self.fetchContentLabelString(annotation: annotation)
  12. if contentLabelString.count != 0 {
  13. height += 4
  14. height += KMBOTAAnnotationTool.fetchContentLabelHeight(string: contentLabelString, maxSize: maxSize)
  15. }
  16. if annotation.isKind(of: CPDFInkAnnotation.self) {
  17. height += 4
  18. height += KMBOTAAnnotationTool.fetchContentImageHeight()
  19. }
  20. let noteLabelString = self.fetchNoteLabelString(annotation: annotation)
  21. if noteLabelString.count != 0 {
  22. height = height + 4
  23. height += KMBOTAAnnotationTool.fetchNoteHeight(string: noteLabelString, maxSize: maxSize)
  24. }
  25. return height
  26. }
  27. static func fetchTextHeight(string: String, maxSize: CGSize) -> CGFloat {
  28. var height = 0.0
  29. let attributes = KMBOTAAnnotationTool.fetchTextAttributed(annotation: nil)
  30. let text : String = self.fetchText(text: string)
  31. if text.count > 0 {
  32. let size = NSString(string: text).boundingRect(with: maxSize, options: NSString.DrawingOptions(rawValue: 3), attributes: attributes).size
  33. height += size.height
  34. }
  35. return height
  36. }
  37. static func fetchText(text: String) -> String {
  38. var string = text.trimmingCharacters(in: .whitespacesAndNewlines)
  39. while string.hasPrefix("\n") {
  40. string = string.filter{ $0 != "\n" }
  41. }
  42. while string.hasPrefix("\r") {
  43. string = string.filter{ $0 != "\r" }
  44. }
  45. while string.hasPrefix("\n") {
  46. string = string.filter{ $0 != "\n" }
  47. }
  48. while string.hasPrefix("\r") {
  49. string = string.filter{ $0 != "\r" }
  50. }
  51. return string
  52. }
  53. static func fetchTextAttributed(annotation: CPDFAnnotation?) -> [NSAttributedString.Key : Any] {
  54. let paragraphStyle = NSMutableParagraphStyle()
  55. paragraphStyle.lineHeightMultiple = 1.32
  56. paragraphStyle.alignment = .left
  57. let attributes = [NSAttributedString.Key.paragraphStyle: paragraphStyle,
  58. NSAttributedString.Key.font : NSFont.SFProTextRegularFont(14.0)]
  59. return attributes
  60. }
  61. static func fetchContentLabelString(annotation: CPDFAnnotation) -> String {
  62. var contentString: String = ""
  63. if annotation.contents != nil {
  64. if annotation.isKind(of: CPDFMarkupAnnotation.self) {
  65. var markupString : String = annotation.contents
  66. if markupString.count == 0 {
  67. let exproString = KMOCToolClass.exproString(annotation: annotation)
  68. if exproString.count > 0 {
  69. markupString = exproString
  70. }
  71. }
  72. contentString = markupString
  73. } else if annotation.isKind(of: CPDFFreeTextAnnotation.self) {
  74. contentString = annotation.contents
  75. } else if annotation.isKind(of: CPDFTextAnnotation.self) {
  76. contentString = annotation.contents
  77. }
  78. contentString = KMBOTAAnnotationTool.fetchText(text: contentString)
  79. }
  80. if annotation.string() != nil {
  81. if annotation.isKind(of: CPDFLineAnnotation.self) {
  82. if (annotation as! CPDFLineAnnotation).isMeasure {
  83. contentString = annotation.string()
  84. }
  85. } else if annotation.isKind(of: CPDFPolygonAnnotation.self) {
  86. contentString = annotation.string()
  87. } else if annotation.isKind(of: CPDFPolylineAnnotation.self) {
  88. contentString = annotation.string()
  89. }
  90. contentString = KMBOTAAnnotationTool.fetchText(text: contentString)
  91. }
  92. return contentString
  93. }
  94. static func fetchContentLabelHeight(string: String, maxSize: CGSize) -> CGFloat {
  95. return KMBOTAAnnotationTool.fetchTextHeight(string: string, maxSize: maxSize)
  96. }
  97. static func fetchContentImageHeight() -> CGFloat {
  98. return 58
  99. }
  100. static func fetchNoteLabelString(annotation: CPDFAnnotation) -> String {
  101. var contentString: String = ""
  102. if annotation.contents != nil {
  103. contentString = annotation.contents
  104. if annotation.isKind(of: CPDFMarkupAnnotation.self) {
  105. var markupString: String = (annotation as! CPDFMarkupAnnotation).markupText() ?? ""
  106. contentString = contentString.replacingOccurrences(of: " ", with: "")
  107. markupString = markupString.replacingOccurrences(of: " ", with: "")
  108. if contentString == markupString {
  109. contentString = ""
  110. } else {
  111. contentString = markupString
  112. }
  113. } else if annotation.isKind(of: CPDFFreeTextAnnotation.self) {
  114. contentString = ""
  115. } else if annotation.isKind(of: CPDFTextAnnotation.self) {
  116. contentString = ""
  117. }
  118. contentString = KMBOTAAnnotationTool.fetchText(text: contentString)
  119. return contentString
  120. }
  121. return contentString
  122. }
  123. static func fetchNoteHeight(string: String, maxSize: CGSize) -> CGFloat {
  124. return KMBOTAAnnotationTool.fetchTextHeight(string: string, maxSize: maxSize) + 8
  125. }
  126. }