Commit Diff


commit - cb71f9f244da8cf54205b4b31904f729d9ed6f82
commit + 6f7574b1fe03cfa5eec20f861309ad858032e576
blob - 62ebe0575fee7d7abe32d37247262bf874970de0
blob + 00a1ab9a4e6eff0bd830c81a806c0811221f99ce
--- ComicSaver/ComicSaverView.h
+++ ComicSaver/ComicSaverView.h
@@ -11,7 +11,12 @@
 @interface ComicSaverView : ScreenSaverView
 {
     CGPDFDocumentRef document;
-    CGPDFPageRef page;
+    long numberOfPages;
+    long currentPage;
+
+    NSMutableArray *pages;
+
+    CGFloat offset;
 }
 
 @end
blob - e860e423f77917c12ad3834b63486eb195aea14d
blob + 90685bb31194a852eb892780f8060df7f1358f01
--- ComicSaver/ComicSaverView.m
+++ ComicSaver/ComicSaverView.m
@@ -15,46 +15,91 @@
     self = [super initWithFrame:frame isPreview:isPreview];
     
     if (self) {
-        //        [self setAnimationTimeInterval:1/30.0];
+        NSString *localPath = @"/Documents/Shared/Comics/Letter 44/letter44_vol1_1412097863.pdf";
+        NSString *docPath = [NSString stringWithFormat:@"%@%@", NSHomeDirectory(), localPath];
+        document = getPDFDocument (docPath);
+        numberOfPages = CGPDFDocumentGetNumberOfPages (document);
+        currentPage = 1;
+        
+        pages = [[NSMutableArray alloc] init];
+        
+        [self setAnimationTimeInterval:1/500.0];
     }
     return self;
 }
 
 - (void)startAnimation
 {
-    NSString *localPath = @"/Documents/Shared/Comics/Letter 44/letter44_vol1_1412097863.pdf";
-    NSString *docPath = [NSString stringWithFormat:@"%@%@", NSHomeDirectory(), localPath];
-    document = MyGetPDFDocumentRef ([docPath UTF8String]);
     
-    long numberOfPages = CGPDFDocumentGetNumberOfPages (document);
-    page = CGPDFDocumentGetPage (document, (random()%numberOfPages)+1);
+    currentPage = 1;
+    offset = 0.0;
+    
     [super startAnimation];
 }
 
 - (void)stopAnimation
 {
     [super stopAnimation];
+    
+    for(id pageRef in pages) CGPDFPageRelease((CGPDFPageRef)pageRef);
     CGPDFDocumentRelease (document);
 }
 
 - (void)drawRect:(NSRect)rect
 {
-    CGRect cropBox = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
+    if(!numberOfPages) return;
     
     CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
-    CGRect clip = CGContextGetClipBoundingBox(context);
+    //printf("display size: [%f,%f]\n", rect.size.width, rect.size.height);
     
-    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
-    CGContextFillRect(context, clip);
-    CGContextTranslateCTM(context, 0.0, 0.0);
-    CGContextScaleCTM(context, 1.0, 1.0);
+//    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
+//    CGContextFillRect(context, rect);
     
-    CGFloat xScale = clip.size.width / cropBox.size.width;
-    CGFloat yScale = clip.size.height / cropBox.size.height;
-    CGFloat scaleToApply = xScale < yScale ? xScale : yScale;
-    CGContextConcatCTM(context, CGAffineTransformMakeScale(scaleToApply, scaleToApply));
-    //    CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, clip, 0, true));
-    CGContextDrawPDFPage (context, page);
+    
+    CGFloat pageOffset = offset;
+    long visiblePage = 0;
+    while(numberOfPages > (currentPage + visiblePage) && pageOffset < rect.size.width) {
+        CGPDFPageRef page = nil;
+        
+        //        printf("page %lu/%lu\n", visiblePage, [pages count]);
+        
+        if(visiblePage < [pages count])
+            page = (__bridge CGPDFPageRef)([pages objectAtIndex:visiblePage]);
+        
+        if(page == nil) {
+            printf("adding page %lu\n", currentPage + (visiblePage));
+            page = CGPDFDocumentGetPage (document, currentPage + visiblePage);
+            [pages addObject:(__bridge id)(page)];
+        }
+        
+        CGContextSaveGState(context);
+        CGRect cropBox = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
+        
+        CGContextTranslateCTM(context, pageOffset, 0.0);
+        CGContextScaleCTM(context, 1.0, 1.0);
+        
+        CGFloat xScale = rect.size.width / cropBox.size.width;
+        CGFloat yScale = rect.size.height / cropBox.size.height;
+        CGFloat scaleToApply = xScale < yScale ? xScale : yScale;
+        CGContextScaleCTM(context, scaleToApply, scaleToApply);
+        
+        if(pageOffset + (cropBox.size.width * scaleToApply) <= 0) {
+            printf("removing page\n");
+            CGPDFPageRelease((CGPDFPageRef)[pages objectAtIndex:0]);
+            [pages removeObjectAtIndex:0];
+            currentPage += 1;
+            pageOffset = offset = pageOffset + (cropBox.size.width * scaleToApply);
+        } else {
+            CGContextDrawPDFPage (context, page);
+            pageOffset += cropBox.size.width * scaleToApply;
+            visiblePage ++;
+        }
+        CGContextRestoreGState(context);
+        
+    }
+    
+    offset -= 3;
+    
 }
 
 - (void)animateOneFrame
@@ -72,26 +117,36 @@
     return nil;
 }
 
-CGPDFDocumentRef MyGetPDFDocumentRef (const char *filename)
+CGPDFDocumentRef getPDFDocument (NSString* filename)
 {
     CFStringRef path;
     CFURLRef url;
     CGPDFDocumentRef document;
-    size_t count;
     
-    path = CFStringCreateWithCString (NULL, filename,
-                                      kCFStringEncodingUTF8);
-    url = CFURLCreateWithFileSystemPath (NULL, path, // 1
-                                         kCFURLPOSIXPathStyle, 0);
+    path = CFStringCreateWithCString (NULL, [filename UTF8String], kCFStringEncodingUTF8);
+    url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
+    
     CFRelease (path);
-    document = CGPDFDocumentCreateWithURL (url);// 2
+    document = CGPDFDocumentCreateWithURL (url);
     CFRelease(url);
-    count = CGPDFDocumentGetNumberOfPages (document);// 3
-    if (count == 0) {
-        printf("`%s' needs at least one page!", filename);
-        return NULL;
-    }
+    
     return document;
 }
 
+
+NSArray* getListOfPDFs(NSString *sourcePath)
+{
+    NSArray* dirs = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:sourcePath error:NULL];
+    
+    NSMutableArray *pdfFiles = [[NSMutableArray alloc] init];
+    [dirs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
+        NSString *filename = (NSString *)obj;
+        NSString *extension = [[filename pathExtension] lowercaseString];
+        if ([extension isEqualToString:@"pdf"]) {
+            [pdfFiles addObject:[sourcePath stringByAppendingPathComponent:filename]];
+        }
+    }];
+    return pdfFiles;
+}
+
 @end
\ No newline at end of file