KMImageToolTipContext.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // KMImageToolTipContext.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/11/23.
  6. //
  7. import Foundation
  8. protocol KMImageToolTipContext: NSObjectProtocol {
  9. func toolTipImage() -> NSImage?
  10. }
  11. extension NSAttributedString: KMImageToolTipContext {
  12. func toolTipImage() -> NSImage? {
  13. return nil
  14. }
  15. }
  16. extension CPDFDestination: KMImageToolTipContext {
  17. func toolTipImage() -> NSImage? {
  18. // static NSDictionary *labelAttributes = nil;
  19. // static NSColor *labelColor = nil;
  20. // if (labelAttributes == nil)
  21. let style = NSMutableParagraphStyle()
  22. style.lineBreakMode = .byClipping
  23. let labelAttributes: [NSAttributedString.Key : Any] = [.font : NSFont.boldSystemFont(ofSize: 11), .foregroundColor : NSColor.white, .paragraphStyle : style]
  24. // if (labelColor == nil)
  25. let labelColor = NSColor(calibratedWhite: 0.5, alpha: 0.8)
  26. // PDFPage *page = [self page];
  27. let page = self.page()
  28. // NSImage *pageImage = [page thumbnailWithSize:0.0 forBox:kPDFDisplayBoxCropBox shadowBlurRadius:0.0 readingBar:nil];
  29. // NSRect pageImageRect = {NSZeroPoint, [pageImage size]};
  30. // NSRect bounds = [page boundsForBox:kPDFDisplayBoxCropBox];
  31. let bounds = page?.bounds(for: .cropBox) ?? .zero
  32. var sourceRect: NSRect = .zero
  33. // PDFSelection *selection = [page selectionForRect:bounds];
  34. let selection = page?.selection(for: bounds)
  35. // NSAffineTransform *transform = [page affineTransformForBox:kPDFDisplayBoxCropBox];
  36. let transform = page?.transform()
  37. // sourceRect.size.width = [[NSUserDefaults standardUserDefaults] doubleForKey:SKToolTipWidthKey];
  38. sourceRect.size.width = 260
  39. // sourceRect.size.height = [[NSUserDefaults standardUserDefaults] doubleForKey:SKToolTipHeightKey];
  40. sourceRect.size.height = 120
  41. // sourceRect.origin = SKAddPoints([transform transformPoint:[self point]], offset);
  42. sourceRect.origin = self.point
  43. sourceRect.origin.y -= NSHeight(sourceRect)
  44. // if ([selection hasCharacters]) {
  45. // NSRect selBounds = [selection boundsForPage:page];
  46. // selBounds = SKRectFromPoints([transform transformPoint:SKBottomLeftPoint(selBounds)], [transform transformPoint:SKTopRightPoint(selBounds)]);
  47. // CGFloat top = ceil(fmax(NSMaxY(selBounds), NSMinY(selBounds) + NSHeight(sourceRect)));
  48. // CGFloat left = floor(fmin(NSMinX(selBounds), NSMaxX(selBounds) - NSWidth(sourceRect)));
  49. // if (top < NSMaxY(sourceRect))
  50. // sourceRect.origin.y = top - NSHeight(sourceRect);
  51. // if (left > NSMinX(sourceRect))
  52. // sourceRect.origin.x = left;
  53. // }
  54. // sourceRect = SKConstrainRect(sourceRect, pageImageRect);
  55. // NSAttributedString *labelString = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"Page %@", @"Tool tip label format"), [page displayLabel]] attributes:labelAttributes];
  56. // NSRect labelRect = [labelString boundingRectWithSize:NSZeroSize options:NSStringDrawingUsesLineFragmentOrigin];
  57. //
  58. // labelRect.size.width = floor(NSWidth(labelRect));
  59. // labelRect.size.height = 2.0 * floor(0.5 * NSHeight(labelRect)); // make sure the cap radius is integral
  60. // labelRect.origin.x = NSWidth(sourceRect) - NSWidth(labelRect) - 0.5 * NSHeight(labelRect) - TEXT_MARGIN_X;
  61. // labelRect.origin.y = TEXT_MARGIN_Y;
  62. // labelRect = NSIntegralRect(labelRect);
  63. //
  64. // NSImage *image = [NSImage bitmapImageWithSize:sourceRect.size drawingHandler:^(NSRect rect){
  65. //
  66. // [pageImage drawInRect:rect fromRect:sourceRect operation:NSCompositeCopy fraction:1.0];
  67. //
  68. // CGFloat radius = 0.5 * NSHeight(labelRect);
  69. // NSBezierPath *path = [NSBezierPath bezierPath];
  70. //
  71. // [path moveToPoint:SKTopLeftPoint(labelRect)];
  72. // [path appendBezierPathWithArcWithCenter:NSMakePoint(NSMinX(labelRect), NSMidY(labelRect)) radius:radius startAngle:90.0 endAngle:270.0];
  73. // [path appendBezierPathWithArcWithCenter:NSMakePoint(NSMaxX(labelRect), NSMidY(labelRect)) radius:radius startAngle:-90.0 endAngle:90.0];
  74. // [path closePath];
  75. //
  76. // [labelColor setFill];
  77. // [path fill];
  78. //
  79. // [labelString drawWithRect:labelRect options:NSStringDrawingUsesLineFragmentOrigin];
  80. //
  81. // }];
  82. //
  83. // [labelString release];
  84. let image = page?.thumbnail(of: sourceRect.size)
  85. return image
  86. // return nil
  87. }
  88. }
  89. extension CPDFAnnotation: KMImageToolTipContext {
  90. func toolTipImage() -> NSImage? {
  91. return nil
  92. }
  93. }
  94. extension CPDFPage: KMImageToolTipContext {
  95. func toolTipImage() -> NSImage? {
  96. return nil
  97. }
  98. }