Revision: 13497
http://sourceforge.net/p/skim-app/code/13497
Author: hofman
Date: 2023-06-22 14:15:35 +0000 (Thu, 22 Jun 2023)
Log Message:
-----------
use ps2pdf to generate quicklook preview and thumbnail on 14.0+
Modified Paths:
--------------
trunk/QuickLook-Skim/GeneratePreviewForURL.m
trunk/QuickLook-Skim/GenerateThumbnailForURL.m
trunk/QuickLook-Skim/SKQLConverter.h
trunk/QuickLook-Skim/SKQLConverter.m
Modified: trunk/QuickLook-Skim/GeneratePreviewForURL.m
===================================================================
--- trunk/QuickLook-Skim/GeneratePreviewForURL.m 2023-06-21 14:59:15 UTC
(rev 13496)
+++ trunk/QuickLook-Skim/GeneratePreviewForURL.m 2023-06-22 14:15:35 UTC
(rev 13497)
@@ -85,7 +85,13 @@
} else if (UTTypeEqual(CFSTR("com.adobe.postscript"), contentTypeUTI)) {
- if (floor(NSAppKitVersionNumber) <= 2299.0) {
+ if (floor(NSAppKitVersionNumber) > 2299.0) {
+ NSData *data = [SKQLConverter PDFDataForURL:(NSURL *)url
ofType:(NSString *)contentTypeUTI allPages:YES];
+ if (data) {
+ QLPreviewRequestSetDataRepresentation(preview,
(CFDataRef)data, kUTTypePDF, NULL);
+ err = noErr;
+ }
+ } else {
bool converted = false;
CGPSConverterCallbacks converterCallbacks = { 0, NULL, NULL, NULL,
NULL, NULL, NULL, NULL };
CGPSConverterRef converter = CGPSConverterCreate(NULL,
&converterCallbacks, NULL);
Modified: trunk/QuickLook-Skim/GenerateThumbnailForURL.m
===================================================================
--- trunk/QuickLook-Skim/GenerateThumbnailForURL.m 2023-06-21 14:59:15 UTC
(rev 13496)
+++ trunk/QuickLook-Skim/GenerateThumbnailForURL.m 2023-06-22 14:15:35 UTC
(rev 13497)
@@ -163,48 +163,57 @@
} else if (UTTypeEqual(CFSTR("com.adobe.postscript"), contentTypeUTI)) {
- if (floor(NSAppKitVersionNumber) <= 2299.0) {
- bool converted = false;
+ bool converted = false;
+ CFDataRef pdfData = NULL;
+
+ if (floor(NSAppKitVersionNumber) > 2299.0) {
+ NSData *data = [SKQLConverter PDFDataForURL:(NSURL *)url
ofType:(NSString *)contentTypeUTI allPages:NO];
+ if (data) {
+ pdfData = (CFDataRef)[data retain];
+ converted = true;
+ }
+ } else {
CGPSConverterCallbacks converterCallbacks = { 0, NULL, NULL, NULL,
NULL, NULL, NULL, NULL };
CGPSConverterRef converter = CGPSConverterCreate(NULL,
&converterCallbacks, NULL);
CGDataProviderRef provider = CGDataProviderCreateWithURL(url);
- CFMutableDataRef pdfData = CFDataCreateMutable(NULL, 0);
- CGDataConsumerRef consumer =
CGDataConsumerCreateWithCFData(pdfData);
+ CFMutableDataRef mutableData = CFDataCreateMutable(NULL, 0);
+ CGDataConsumerRef consumer =
CGDataConsumerCreateWithCFData(mutableData);
if (provider != NULL && consumer != NULL)
converted = CGPSConverterConvert(converter, provider,
consumer, NULL);
CGDataProviderRelease(provider);
CGDataConsumerRelease(consumer);
CFRelease(converter);
+ if (converted)
+ pdfData = mutableData;
+ }
+ if (converted) {
+ // sadly, we can't use the system's QL generator from inside
quicklookd, so we don't get the fancy binder on the left edge
+ CGDataProviderRef provider =
CGDataProviderCreateWithCFData(pdfData);
+ CGPDFDocumentRef pdfDoc =
CGPDFDocumentCreateWithProvider(provider);
+ CGPDFPageRef pdfPage = NULL;
+ if (pdfDoc && CGPDFDocumentGetNumberOfPages(pdfDoc) > 0)
+ pdfPage = CGPDFDocumentGetPage(pdfDoc, 1);
- if (converted) {
- // sadly, we can't use the system's QL generator from inside
quicklookd, so we don't get the fancy binder on the left edge
- provider = CGDataProviderCreateWithCFData(pdfData);
- CGPDFDocumentRef pdfDoc =
CGPDFDocumentCreateWithProvider(provider);
- CGPDFPageRef pdfPage = NULL;
- if (pdfDoc && CGPDFDocumentGetNumberOfPages(pdfDoc) > 0)
- pdfPage = CGPDFDocumentGetPage(pdfDoc, 1);
-
- if (pdfPage) {
- CGRect pageRect = CGPDFPageGetBoxRect(pdfPage,
kCGPDFCropBox);
- CGRect thumbRect = {{0.0, 0.0}, {CGRectGetWidth(pageRect),
CGRectGetHeight(pageRect)}};
- CGFloat color[4] = {1.0, 1.0, 1.0, 1.0};
- CGContextRef ctxt =
QLThumbnailRequestCreateContext(thumbnail, thumbRect.size, FALSE, NULL);
- CGAffineTransform t =
CGPDFPageGetDrawingTransform(pdfPage, kCGPDFCropBox, thumbRect, 0, true);
- CGContextConcatCTM(ctxt, t);
- CGContextClipToRect(ctxt, pageRect);
- CGContextSetFillColor(ctxt, color);
- CGContextFillRect(ctxt, pageRect);
- CGContextDrawPDFPage(ctxt, pdfPage);
- QLThumbnailRequestFlushContext(thumbnail, ctxt);
- CGContextRelease(ctxt);
- didGenerate = true;
- }
- CGPDFDocumentRelease(pdfDoc);
- CGDataProviderRelease(provider);
+ if (pdfPage) {
+ CGRect pageRect = CGPDFPageGetBoxRect(pdfPage, kCGPDFCropBox);
+ CGRect thumbRect = {{0.0, 0.0}, {CGRectGetWidth(pageRect),
CGRectGetHeight(pageRect)}};
+ CGFloat color[4] = {1.0, 1.0, 1.0, 1.0};
+ CGContextRef ctxt = QLThumbnailRequestCreateContext(thumbnail,
thumbRect.size, FALSE, NULL);
+ CGAffineTransform t = CGPDFPageGetDrawingTransform(pdfPage,
kCGPDFCropBox, thumbRect, 0, true);
+ CGContextConcatCTM(ctxt, t);
+ CGContextClipToRect(ctxt, pageRect);
+ CGContextSetFillColor(ctxt, color);
+ CGContextFillRect(ctxt, pageRect);
+ CGContextDrawPDFPage(ctxt, pdfPage);
+ QLThumbnailRequestFlushContext(thumbnail, ctxt);
+ CGContextRelease(ctxt);
+ didGenerate = true;
}
- if (pdfData) CFRelease(pdfData);
+ CGPDFDocumentRelease(pdfDoc);
+ CGDataProviderRelease(provider);
}
-
+ if (pdfData) CFRelease(pdfData);
+
} else if (UTTypeEqual(CFSTR("net.sourceforge.skim-app.skimnotes"),
contentTypeUTI)) {
NSData *data = [[NSData alloc] initWithContentsOfURL:(NSURL *)url
options:NSUncachedRead error:NULL];
Modified: trunk/QuickLook-Skim/SKQLConverter.h
===================================================================
--- trunk/QuickLook-Skim/SKQLConverter.h 2023-06-21 14:59:15 UTC (rev
13496)
+++ trunk/QuickLook-Skim/SKQLConverter.h 2023-06-22 14:15:35 UTC (rev
13497)
@@ -40,4 +40,5 @@
+ (NSArray *)notesWithData:(NSData *)data;
+ (NSAttributedString *)attributedStringWithNotes:(NSArray *)notes
forThumbnail:(QLThumbnailRequestRef)thumbnail;
+ (NSString *)htmlStringWithNotes:(NSArray *)notes;
++ (NSData *)PDFDataForURL:(NSURL *)url ofType:(NSString *)type
allPages:(BOOL)allPages;
@end
Modified: trunk/QuickLook-Skim/SKQLConverter.m
===================================================================
--- trunk/QuickLook-Skim/SKQLConverter.m 2023-06-21 14:59:15 UTC (rev
13496)
+++ trunk/QuickLook-Skim/SKQLConverter.m 2023-06-22 14:15:35 UTC (rev
13497)
@@ -268,4 +268,74 @@
return htmlString;
}
++ (NSData *)PDFDataForURL:(NSURL *)url ofType:(NSString *)type
allPages:(BOOL)allPages {
+
+ NSData *output = nil;
+
+ NSString *commandPath = @"/usr/local/bin/ps2pdf";
+ if (UTTypeEqual(CFSTR("com.adobe.postscript"), (CFStringRef)type))
+ commandPath = @"/usr/local/bin/ps2pdf";
+ else if (UTTypeEqual(CFSTR("org.tug.tex.dvi"), (CFStringRef)type))
+ commandPath = floor(NSFoundationVersionNumber) >
NSFoundationVersionNumber10_10 ? @"/Library/TeX/texbin/dvipdfmx" :
@"/usr/texbin/dvipdfmx";
+ else if (UTTypeEqual(CFSTR("org.tug.tex.xdv"), (CFStringRef)type))
+ commandPath = floor(NSFoundationVersionNumber) >
NSFoundationVersionNumber10_10 ? @"/Library/TeX/texbin/xdvipdfmx" :
@"/usr/texbin/xdvipdfmx";
+
+ NSFileManager *fm = [[NSFileManager alloc] init];
+
+ if (commandPath && [fm isExecutableFileAtPath:commandPath]) {
+
+ NSTask *task = [[NSTask alloc] init];
+ [task setLaunchPath:commandPath];
+ [task setStandardError:[NSFileHandle fileHandleWithNullDevice]];
+ [task setStandardOutput:[NSFileHandle fileHandleWithNullDevice]];
+
+ NSString *tempDir = NSTemporaryDirectory() ?: @"/tmp";
+ const char *tmpPath = [[tempDir
stringByAppendingPathComponent:@"skimql.XXXXXX"] fileSystemRepresentation];
+ char *tempName = strdup(tmpPath);
+ int fd = mkstemp(tempName);
+ assert(tempName);
+
+ NSString *outputPath = [fm stringWithFileSystemRepresentation:tempName
length:strlen(tempName)];
+ free(tempName);
+
+ NSMutableArray *arguments = [[NSMutableArray alloc] init];
+ if (UTTypeEqual(CFSTR("com.adobe.postscript"), (CFStringRef)type)) {
+ if (allPages == NO)
+ [arguments addObject:@"-dLastPage=1"];
+ [arguments addObject:[url path]];
+ [arguments addObject:outputPath];
+ } else {
+ [arguments addObject:@"-q"];
+ if (allPages == NO) {
+ [arguments addObject:@"-s"];
+ [arguments addObject:@"1"];
+ }
+ [arguments addObject:@"-o"];
+ [arguments addObject:outputPath];
+ [arguments addObject:[url path]];
+ }
+ [task setArguments:arguments];
+ [arguments release];
+
+ int status = -1;
+ @try {
+ [task launch];
+ close(fd);
+ [task waitUntilExit];
+ status = [task terminationStatus];
+ }
+ @catch (id e) {}
+ [task release];
+
+ if (0 == status)
+ output = [NSData dataWithContentsOfFile:outputPath
options:NSUncachedRead error:NULL];
+
+ [fm removeItemAtPath:outputPath error:NULL];
+ }
+
+ [fm release];
+
+ return output;
+}
+
@end
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit