// // KMImageToolTipContext.swift // PDF Master // // Created by tangchao on 2023/11/23. // import Foundation protocol KMImageToolTipContext: NSObjectProtocol { func toolTipImage() -> NSImage? } extension NSAttributedString: KMImageToolTipContext { func toolTipImage() -> NSImage? { return nil } } extension CPDFDestination: KMImageToolTipContext { func toolTipImage() -> NSImage? { // static NSDictionary *labelAttributes = nil; // static NSColor *labelColor = nil; // if (labelAttributes == nil) let style = NSMutableParagraphStyle() style.lineBreakMode = .byClipping let labelAttributes: [NSAttributedString.Key : Any] = [.font : NSFont.boldSystemFont(ofSize: 11), .foregroundColor : NSColor.white, .paragraphStyle : style] // if (labelColor == nil) let labelColor = NSColor(calibratedWhite: 0.5, alpha: 0.8) // PDFPage *page = [self page]; let page = self.page() // NSImage *pageImage = [page thumbnailWithSize:0.0 forBox:kPDFDisplayBoxCropBox shadowBlurRadius:0.0 readingBar:nil]; // NSRect pageImageRect = {NSZeroPoint, [pageImage size]}; // NSRect bounds = [page boundsForBox:kPDFDisplayBoxCropBox]; let bounds = page?.bounds(for: .cropBox) ?? .zero var sourceRect: NSRect = .zero // PDFSelection *selection = [page selectionForRect:bounds]; let selection = page?.selection(for: bounds) // NSAffineTransform *transform = [page affineTransformForBox:kPDFDisplayBoxCropBox]; let transform = page?.transform() // sourceRect.size.width = [[NSUserDefaults standardUserDefaults] doubleForKey:SKToolTipWidthKey]; sourceRect.size.width = 260 // sourceRect.size.height = [[NSUserDefaults standardUserDefaults] doubleForKey:SKToolTipHeightKey]; sourceRect.size.height = 120 // sourceRect.origin = SKAddPoints([transform transformPoint:[self point]], offset); sourceRect.origin = self.point sourceRect.origin.y -= NSHeight(sourceRect) // if ([selection hasCharacters]) { // NSRect selBounds = [selection boundsForPage:page]; // selBounds = SKRectFromPoints([transform transformPoint:SKBottomLeftPoint(selBounds)], [transform transformPoint:SKTopRightPoint(selBounds)]); // CGFloat top = ceil(fmax(NSMaxY(selBounds), NSMinY(selBounds) + NSHeight(sourceRect))); // CGFloat left = floor(fmin(NSMinX(selBounds), NSMaxX(selBounds) - NSWidth(sourceRect))); // if (top < NSMaxY(sourceRect)) // sourceRect.origin.y = top - NSHeight(sourceRect); // if (left > NSMinX(sourceRect)) // sourceRect.origin.x = left; // } // sourceRect = SKConstrainRect(sourceRect, pageImageRect); // NSAttributedString *labelString = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"Page %@", @"Tool tip label format"), [page displayLabel]] attributes:labelAttributes]; // NSRect labelRect = [labelString boundingRectWithSize:NSZeroSize options:NSStringDrawingUsesLineFragmentOrigin]; // // labelRect.size.width = floor(NSWidth(labelRect)); // labelRect.size.height = 2.0 * floor(0.5 * NSHeight(labelRect)); // make sure the cap radius is integral // labelRect.origin.x = NSWidth(sourceRect) - NSWidth(labelRect) - 0.5 * NSHeight(labelRect) - TEXT_MARGIN_X; // labelRect.origin.y = TEXT_MARGIN_Y; // labelRect = NSIntegralRect(labelRect); // // NSImage *image = [NSImage bitmapImageWithSize:sourceRect.size drawingHandler:^(NSRect rect){ // // [pageImage drawInRect:rect fromRect:sourceRect operation:NSCompositeCopy fraction:1.0]; // // CGFloat radius = 0.5 * NSHeight(labelRect); // NSBezierPath *path = [NSBezierPath bezierPath]; // // [path moveToPoint:SKTopLeftPoint(labelRect)]; // [path appendBezierPathWithArcWithCenter:NSMakePoint(NSMinX(labelRect), NSMidY(labelRect)) radius:radius startAngle:90.0 endAngle:270.0]; // [path appendBezierPathWithArcWithCenter:NSMakePoint(NSMaxX(labelRect), NSMidY(labelRect)) radius:radius startAngle:-90.0 endAngle:90.0]; // [path closePath]; // // [labelColor setFill]; // [path fill]; // // [labelString drawWithRect:labelRect options:NSStringDrawingUsesLineFragmentOrigin]; // // }]; // // [labelString release]; let image = page?.thumbnail(of: sourceRect.size) return image // return nil } } extension CPDFAnnotation: KMImageToolTipContext { func toolTipImage() -> NSImage? { return nil } } extension CPDFPage: KMImageToolTipContext { func toolTipImage() -> NSImage? { return nil } }