Source

CocoaPDF / TronPDFView.mm

Full commit
//
//  TronPDFView.mm
//  cocoapdf
//
//  Created by Quincy on 10-07-25.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "TronPDFView.h"

void beginRendering(void* data) {
    // NSLog(@"%@", "begin rendering");
    //printf("begin rendering");
}

void endRendering(void* data, bool canceled) {
    //printf("end rendering");
}

@implementation TronPDFView

@synthesize tronPDFView = _tronPDFView;

- (pdftron::PDF::PDFView *) getPDFView {
    return _tronPDFView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    _tronPDFView = new pdftron::PDF::PDFView();
    /*
    NSRect bound = [self bounds];
    double width = (double) bound.size.width;
    double height = (double) bound.size.height;
    _tronPDFView->OnSize(width, height);
     */
}

- (void)drawRect:(NSRect)dirtyRect {
    /*
    [[NSColor redColor] set];
    NSRectFill([self bounds]);
     */

    if (_tronPDFView->GetDoc()) {
        /*
        double width = _tronPDFView->GetCanvasWidth();
        double height = _tronPDFView->GetCanvasHeight();
        
        [self setFrame:NSMakeRect(0, 0, width, height)];
        [self setBounds:NSMakeRect(0, 0, width, height)];
        */
        
        //_tronPDFView->OnSize((int) svFrame.size.width, (int) svFrame.size.height);

        
        const char* bgra = _tronPDFView->GetBuffer();
        if (bgra) {
            int width = _tronPDFView->GetBufferWidth();
            int height = _tronPDFView->GetBufferHeight();
            char * rgba = (char *) malloc(width * height * 4);
            for (int i = 0; i < width * height; ++i) {
                rgba[4*i] = bgra[4*i + 2];
                rgba[4*i + 1] = bgra[4*i + 1];
                rgba[4*i + 2] = bgra[4*i];
                rgba[4*i + 3] = bgra[4*i + 3];
            }
            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
            CGContextRef bitmapContext = CGBitmapContextCreate(rgba,
                                                               width,
                                                               height,
                                                               8, // bitsPerComponent
                                                               4*width, // bytesPerRow
                                                               colorSpace,
                                                               kCGImageAlphaPremultipliedLast);
            
            
            CFRelease(colorSpace);
            
            CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);
            
            __strong NSGraphicsContext	*	nsGraphicsContext	= [NSGraphicsContext currentContext];
            __strong CGContextRef			zCgContextRef		= (CGContextRef) [nsGraphicsContext graphicsPort];
            
            
            NSRect imageRect;
            imageRect.origin = NSZeroPoint;
            imageRect.size.width = [[self superview] bounds].size.width;
            imageRect.size.height = [[self superview] bounds].size.height;
            CGRect	zCgRect	= NSRectToCGRect(imageRect);

            CGContextDrawImage(zCgContextRef, zCgRect, cgImage);
            
            CFRelease(cgImage);
            CFRelease(bitmapContext);
            free(rgba);
        }
    }

}

/*
-(BOOL)isFlipped {
    return YES;
}
*/

- (BOOL)acceptsFirstResponder 
{
    return YES;
}

- (void)dealloc {
    if (_tronPDFView) {
        free(_tronPDFView);
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (void)setDocument:(MyPDFDoc*) myDoc {
    if (_tronPDFView) {
        _tronPDFView->SetDoc(*(myDoc.tronPDFDoc));
        _tronPDFView->SetRenderBeginProc(beginRendering, NULL);
        _tronPDFView->SetRenderFinishProc(endRendering, NULL);
        _tronPDFView->SetPageSpacing(10, 10, 20, 10);
        [self windowResized:nil];
      /*
        double w = _tronPDFView->GetCanvasWidth();
        double h = _tronPDFView->GetCanvasHeight();
        NSLog(@"setFrame width: %d, height: %d", w, h);
        [self setFrame:NSMakeRect(0.0, 0.0, w, h)];
       */
    }
}

- (void)viewDidMoveToWindow
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowResized:) name:NSWindowDidResizeNotification object:[self window]];
}

- (void)windowResized:(NSNotification *)notification;
{
    NSSize win_size = [[self window] frame].size;
    
    NSLog(@"window width = %f, window height = %f", win_size.width, win_size.height);
    
    NSSize size = [self bounds].size;
    
    NSRect svFrame = [[self superview] frame];
    NSRect svBound = [[self superview] bounds];
    NSRect rect = [self bounds];
    NSPoint p = rect.origin;
    NSSize s = rect.size;
    NSLog(@"self bound: w: %0.4f, h: %0.4f", s.width, s.height);
    NSLog(@"self frame: w: %0.4f, h: %0.4f", [self frame].size.width, [self frame].size.height);
    NSLog(@"super frame: x: w: %0.4f, h: %0.4f", svFrame.size.width, svFrame.size.height);
    NSLog(@"super bound: x: w: %0.4f, h: %0.4f", svBound.size.width, svBound.size.height);
    
    
    if (_tronPDFView)
    {
        _tronPDFView->OnSize((int) svBound.size.width, (int) svBound.size.height);
    
        double w = _tronPDFView->GetCanvasWidth();
        double h = _tronPDFView->GetCanvasHeight();
        NSLog(@"setFrame width: %d, height: %d", w, h);
        NSLog(@"setFrame width: %0.4f, height: %0.4f", w, h);
        [self setFrame:NSMakeRect(0.0, 0.0, w, h)];
        NSLog(@"result frame: w: %d, h: %d", [self frame].size.width, [self frame].size.height);
        NSLog(@"result frame: w: %0.4f, h: %0.4f", [self frame].size.width, [self frame].size.height);        
    }
}

@end