KMBOTAAnnotationTool.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. } else {
  78. contentString = annotation.contents
  79. }
  80. contentString = KMBOTAAnnotationTool.fetchText(text: contentString)
  81. }
  82. if annotation.string() != nil {
  83. if annotation.isKind(of: CPDFLineAnnotation.self) {
  84. if (annotation as! CPDFLineAnnotation).isMeasure {
  85. contentString = annotation.string()
  86. }
  87. } else if annotation.isKind(of: CPDFPolygonAnnotation.self) {
  88. contentString = annotation.string()
  89. } else if annotation.isKind(of: CPDFPolylineAnnotation.self) {
  90. contentString = annotation.string()
  91. }
  92. contentString = KMBOTAAnnotationTool.fetchText(text: contentString)
  93. }
  94. //限制文字返回数量
  95. contentString = String(contentString.prefix(200))
  96. return contentString
  97. }
  98. static func fetchContentLabelHeight(string: String, maxSize: CGSize) -> CGFloat {
  99. return KMBOTAAnnotationTool.fetchTextHeight(string: string, maxSize: maxSize)
  100. }
  101. static func fetchContentImageHeight() -> CGFloat {
  102. return 58
  103. }
  104. static func fetchNoteLabelString(annotation: CPDFAnnotation) -> String {
  105. var contentString: String = ""
  106. if annotation.contents != nil {
  107. contentString = annotation.contents
  108. if annotation.isKind(of: CPDFMarkupAnnotation.self) {
  109. var markupString: String = (annotation as! CPDFMarkupAnnotation).markupText() ?? ""
  110. contentString = contentString.replacingOccurrences(of: " ", with: "")
  111. markupString = markupString.replacingOccurrences(of: " ", with: "")
  112. if contentString == markupString {
  113. contentString = ""
  114. } else {
  115. contentString = markupString
  116. }
  117. } else if annotation.isKind(of: CPDFFreeTextAnnotation.self) {
  118. contentString = ""
  119. } else if annotation.isKind(of: CPDFTextAnnotation.self) {
  120. contentString = ""
  121. }
  122. contentString = KMBOTAAnnotationTool.fetchText(text: contentString)
  123. return contentString
  124. }
  125. return contentString
  126. }
  127. static func fetchNoteHeight(string: String, maxSize: CGSize) -> CGFloat {
  128. return KMBOTAAnnotationTool.fetchTextHeight(string: string, maxSize: maxSize) + 8
  129. }
  130. }