KMImageToolTipContext.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // KMImageToolTipContext.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/23.
  6. //
  7. import Foundation
  8. import KMComponentLibrary
  9. let TEXT_MARGIN_X = 2.0
  10. let TEXT_MARGIN_Y = 2.0
  11. let SKToolTipWidthKey = "SKToolTipWidth"
  12. let SKToolTipHeightKey = "SKToolTipHeight"
  13. @objc protocol KMImageToolTipContext: NSObjectProtocol {
  14. func toolTipImage() -> NSImage?
  15. }
  16. extension NSAttributedString: KMImageToolTipContext {
  17. // func toolTipImage() -> NSImage? {
  18. // return nil
  19. // }
  20. func toolTipImage() -> NSImage? {
  21. let backgroundColor: NSColor = {
  22. if #available(OSX 10.9, *) {
  23. return NSColor(calibratedRed: 0.95, green: 0.95, blue: 0.95, alpha: 1.0)
  24. } else {
  25. return NSColor(calibratedRed: 1.0, green: 1.0, blue: 0.75, alpha: 1.0)
  26. }
  27. }()
  28. let width = UserDefaults.standard.double(forKey: SKToolTipWidthKey) - 2.0 * TEXT_MARGIN_X
  29. let height = UserDefaults.standard.double(forKey: SKToolTipHeightKey) - 2.0 * TEXT_MARGIN_Y
  30. var textRect = self.boundingRect(with: NSSize(width: width, height: height), options: .usesLineFragmentOrigin)
  31. textRect.origin = NSPoint(x: TEXT_MARGIN_X, y: TEXT_MARGIN_Y)
  32. textRect.size.height = min(textRect.height, height)
  33. textRect = NSInsetRect(NSIntegralRect(textRect), -TEXT_MARGIN_X, -TEXT_MARGIN_Y)
  34. let image = NSImage(size: textRect.size, flipped: false) { rect in
  35. backgroundColor.setFill()
  36. rect.fill()
  37. self.draw(with: NSRect(x: TEXT_MARGIN_X, y: TEXT_MARGIN_Y, width: rect.width - 2 * TEXT_MARGIN_X, height: rect.height - 2 * TEXT_MARGIN_Y), options: .usesLineFragmentOrigin)
  38. return true
  39. }
  40. return image
  41. }
  42. }
  43. var _km_dest_labelAttributes: [NSAttributedString.Key : Any]?
  44. var _km_dest_labelColor: NSColor?
  45. var _km_text_margin_x: CGFloat = 2
  46. var _km_text_margin_y: CGFloat = 2
  47. extension CPDFDestination: KMImageToolTipContext {
  48. func toolTipImage() -> NSImage? {
  49. return self._toolTipImage(offset: NSMakePoint(-50.0, 20.0))
  50. }
  51. @objc internal func _toolTipImage(offset: NSPoint) -> NSImage? {
  52. guard let page = self.page() else {
  53. return nil
  54. }
  55. let attri = [NSAttributedString.Key.font : ComponentLibrary.shared.getFontFromKey("mac/body-s-regular"),
  56. NSAttributedString.Key.foregroundColor : KMNColorTools.colorText_white1()]
  57. let labelColor = KMNColorTools.colorFill_black40()
  58. let pageImage = page.thumbnail(of: page.bounds(for: .cropBox).size)
  59. let pageSize = page.bounds(for: .cropBox)
  60. let pageImageRect = NSMakeRect(0, 0, pageSize.width, pageSize.height)
  61. let bounds = page.bounds(for: .cropBox)
  62. var sourceRect: NSRect = .zero
  63. let selection = page.selection(for: bounds)
  64. let transform = page.km_affineTransform(for: .cropBox)
  65. // sourceRect.size.width = [[NSUserDefaults standardUserDefaults] doubleForKey:SKToolTipWidthKey];
  66. sourceRect.size.width = 400
  67. // sourceRect.size.height = [[NSUserDefaults standardUserDefaults] doubleForKey:SKToolTipHeightKey];
  68. sourceRect.size.height = 120
  69. sourceRect.origin = KMAddPoints(transform.transform(self.point), offset)
  70. sourceRect.origin.y -= NSHeight(sourceRect)
  71. if let data = selection?.hasCharacters(), data {
  72. var selBounds = selection?.bounds(for: page) ?? .zero
  73. let blPoint = KMBottomLeftPoint(selBounds)
  74. let trPoint = KMTopRightPoint(selBounds)
  75. selBounds = KMRectFromPoints(aPoint: blPoint, bPoint: trPoint)
  76. let top = ceil(fmax(NSMaxY(selBounds), NSMinY(selBounds) + NSHeight(sourceRect)))
  77. let left = floor(fmin(NSMinX(selBounds), NSMaxX(selBounds) - NSWidth(sourceRect)))
  78. if (top < NSMaxY(sourceRect)) {
  79. sourceRect.origin.y = top - NSHeight(sourceRect)
  80. }
  81. if (left > NSMinX(sourceRect)) {
  82. sourceRect.origin.x = left
  83. }
  84. }
  85. sourceRect = KMConstrainRect(rect: sourceRect, boundary: pageImageRect)
  86. let labelString = NSAttributedString(string: String(format: KMLocalizedString("Page %@"), page.km_displayLabel), attributes: attri)
  87. var labelRect = labelString.boundingRect(with: .zero, options: [.usesLineFragmentOrigin])
  88. labelRect.size.width = floor(NSWidth(labelRect)) + 10
  89. labelRect.size.height = 2.0 * floor(0.5 * NSHeight(labelRect)) + 2 * 4 // make sure the cap radius is integral
  90. labelRect.origin.x = 18
  91. labelRect.origin.y = 8
  92. labelRect = NSIntegralRect(labelRect)
  93. let image = NSImage.bitmapImage(with: sourceRect.size) { rect in
  94. pageImage?.draw(in: rect, from: sourceRect, operation: .copy, fraction: 1)
  95. let radius = 0.5 * NSHeight(labelRect)
  96. let path = NSBezierPath()
  97. path.move(to: KMTopLeftPoint(labelRect))
  98. path.appendArc(withCenter: NSMakePoint(NSMinX(labelRect), NSMidY(labelRect)), radius: radius, startAngle: 90.0, endAngle: 270.0)
  99. path.appendArc(withCenter: NSMakePoint(NSMaxX(labelRect), NSMidY(labelRect)), radius: radius, startAngle: -90, endAngle: 90)
  100. path.close()
  101. labelColor.setFill()
  102. path.fill()
  103. labelString.draw(with: labelRect.insetBy(dx: 0, dy: 2), options: [.usesLineFragmentOrigin])
  104. }
  105. return image
  106. }
  107. }
  108. extension CPDFAnnotation: KMImageToolTipContext {
  109. func toolTipImage() -> NSImage? {
  110. if self.isLink() {
  111. if let destination: CPDFDestination = self.linkDestination() {
  112. var image = destination._toolTipImage(offset: .zero)
  113. if image == nil{
  114. let url = self.linkURL()
  115. if url != nil{
  116. let attrString = toolTipAttributedString(url?.absoluteString ?? "")
  117. if attrString.length > 0 {
  118. image = attrString.toolTipImage()
  119. }
  120. }
  121. }
  122. if image != nil {
  123. return image
  124. }
  125. }else{
  126. return nil
  127. }
  128. }
  129. var attrString = self.text()
  130. var string = attrString?.string
  131. var i = 0
  132. var l = string?.count
  133. if l == 0 || ((string?.rangeOfCharacter(from: CharacterSet.whitespacesAndNewlines)) == nil) {
  134. string = self.string()
  135. l = string?.count
  136. if l ?? 0 > 0 {
  137. attrString = toolTipAttributedString(string ?? "")
  138. }else {
  139. attrString = nil
  140. }
  141. }
  142. attrString = KMOCTool.transformAttr(attrString ?? NSAttributedString(string: ""), with: string ?? "")
  143. if attrString?.length ?? 0 > 0 {
  144. return attrString?.toolTipImage()
  145. }else{
  146. return nil
  147. }
  148. }
  149. }
  150. func toolTipAttributedString(_ string: String) -> NSAttributedString {
  151. var attributes: [NSAttributedString.Key: Any]? = nil
  152. if attributes == nil {
  153. attributes = [NSAttributedString.Key.font: NSFont.toolTipsFont(ofSize: 11.0), NSAttributedString.Key.paragraphStyle: NSParagraphStyle.defaultClippingParagraphStyle]
  154. }
  155. return NSAttributedString(string: string, attributes: attributes)
  156. }
  157. extension CPDFPage: KMImageToolTipContext {
  158. func toolTipImage() -> NSImage? {
  159. return nil
  160. }
  161. }
  162. extension CPDFBookmark: KMImageToolTipContext {
  163. func toolTipImage() -> NSImage? {
  164. guard let page = self.document?.page(at: UInt(self.pageIndex)) else {
  165. return nil
  166. }
  167. let image = page.thumbnail(of: page.size)
  168. image?.size = .init(width: 164, height: 224)
  169. let attri = [NSAttributedString.Key.font : ComponentLibrary.shared.getFontFromKey("mac/body-s-regular"),
  170. NSAttributedString.Key.foregroundColor : KMNColorTools.colorText_white1()]
  171. let sourceRect = NSMakeRect(0, 0, 164, 224)
  172. let labelString = NSAttributedString(string: String(format: KMLocalizedString("Page %@"), page.km_displayLabel), attributes: attri)
  173. var labelRect = labelString.boundingRect(with: .zero, options: [.usesLineFragmentOrigin])
  174. labelRect.size.width = floor(NSWidth(labelRect)) + 10
  175. labelRect.size.height = 20
  176. labelRect.origin.x = 18
  177. labelRect.origin.y = 8
  178. labelRect = NSIntegralRect(labelRect)
  179. let labelColor = KMNColorTools.colorFill_black40()
  180. let kimage = NSImage.bitmapImage(with: sourceRect.size) { rect in
  181. image?.draw(in: rect, from: sourceRect, operation: .copy, fraction: 1)
  182. let radius = 0.5 * NSHeight(labelRect)
  183. let path = NSBezierPath()
  184. path.move(to: KMTopLeftPoint(labelRect))
  185. path.appendArc(withCenter: NSMakePoint(NSMinX(labelRect), NSMidY(labelRect)), radius: radius, startAngle: 90.0, endAngle: 270.0)
  186. path.appendArc(withCenter: NSMakePoint(NSMaxX(labelRect), NSMidY(labelRect)), radius: radius, startAngle: -90, endAngle: 90)
  187. path.close()
  188. labelColor.setFill()
  189. path.fill()
  190. labelString.draw(with: labelRect.insetBy(dx: 0, dy: 2), options: [.usesLineFragmentOrigin])
  191. }
  192. return kimage
  193. }
  194. }
  195. extension NSParagraphStyle {
  196. static var defaultClippingParagraphStyle: NSParagraphStyle = {
  197. let tmpParagraphStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
  198. tmpParagraphStyle.lineBreakMode = .byClipping
  199. return tmpParagraphStyle.copy() as! NSParagraphStyle
  200. }()
  201. static var defaultTruncatingTailParagraphStyle: NSParagraphStyle = {
  202. let tmpParagraphStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
  203. tmpParagraphStyle.lineBreakMode = .byTruncatingTail
  204. return tmpParagraphStyle.copy() as! NSParagraphStyle
  205. }()
  206. }
  207. //extension NSParagraphStyle{
  208. // static var defaultClippingParagraphStyle: NSParagraphStyle {
  209. // var style: NSParagraphStyle? = nil
  210. // if style == nil {
  211. // let tmpParagraphStyle = (NSParagraphStyle.default as! NSMutableParagraphStyle).mutableCopy() as! NSMutableParagraphStyle
  212. // tmpParagraphStyle.lineBreakMode = .byClipping
  213. // style = tmpParagraphStyle.copy() as? NSParagraphStyle
  214. // }
  215. // return style!
  216. // }
  217. //
  218. // static var defaultTruncatingTailParagraphStyle: NSParagraphStyle {
  219. // var style: NSParagraphStyle? = nil
  220. // if style == nil {
  221. // let tmpParagraphStyle = (NSParagraphStyle.default as! NSMutableParagraphStyle).mutableCopy() as! NSMutableParagraphStyle
  222. // tmpParagraphStyle.lineBreakMode = .byTruncatingTail
  223. // style = tmpParagraphStyle.copy() as? NSParagraphStyle
  224. // }
  225. // return style!
  226. // }
  227. //}