KMImageToolTipContext.swift 5.3 KB

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