libs-gui/Source/NSPDFImageRep.m

130 lines
3 KiB
Mathematica
Raw Normal View History

2019-11-18 10:23:30 +00:00
/* Implementation of class NSPDFImageRep
Copyright (C) 2019 Free Software Foundation, Inc.
By: Gregory Casamento <greg.casamento@gmail.com>
2019-11-18 10:23:30 +00:00
Date: Fri Nov 15 04:24:27 EST 2019
This file is part of the GNUstep Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
2019-11-18 10:23:30 +00:00
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
2019-11-18 10:23:30 +00:00
*/
2019-12-02 15:53:08 +00:00
#import "config.h"
#import <Foundation/NSString.h>
#import <Foundation/NSData.h>
#import "AppKit/NSPasteboard.h"
#import "AppKit/NSPDFImageRep.h"
#import <GNUstepGUI/GSImageMagickImageRep.h>
2019-11-18 10:23:30 +00:00
@implementation NSPDFImageRep
+ (BOOL) canInitWithData: (NSData *)imageData
{
NSData *header = [imageData subdataWithRange: NSMakeRange(0,4)];
NSString *str = [[NSString alloc] initWithData: header encoding: NSUTF8StringEncoding];
2019-12-02 15:53:08 +00:00
BOOL result = [str isEqualToString: @"%PDF"];
AUTORELEASE(str);
2019-12-02 15:53:08 +00:00
return result;
}
+ (NSArray *) imagePasteboardTypes
{
return [NSArray arrayWithObject: NSPDFPboardType];
2019-12-02 15:53:08 +00:00
}
2020-06-08 08:01:28 +00:00
+ (NSArray *) imageUnfilteredPasteboardTypes
2019-12-02 15:53:08 +00:00
{
return [self imagePasteboardTypes];
}
+ (NSArray *) imageFileTypes
{
return [NSArray arrayWithObjects: @"pdf", @"PDF", nil];
}
+ (NSArray *) imageUnfilteredFileTypes
{
return [self imageFileTypes];
}
2019-11-18 10:23:30 +00:00
+ (instancetype) imageRepWithData: (NSData *)imageData
{
2019-12-02 15:53:08 +00:00
return [[[self class] alloc] initWithData: imageData];
2019-11-18 10:23:30 +00:00
}
- (instancetype) initWithData: (NSData *)imageData
{
2019-11-22 07:16:05 +00:00
self = [super init];
if(self != nil)
{
#if HAVE_IMAGEMAGICK
2019-12-02 15:53:08 +00:00
ASSIGN(_pageReps, [GSImageMagickImageRep imageRepsWithData: imageData]);
_size = [[_pageReps objectAtIndex: 0] size];
_currentPage = 1;
#else
ASSIGN(_pageReps, [NSArray array]);
_size = NSMakeSize(0,0);
_currentPage = 0;
#endif
2019-12-02 15:53:08 +00:00
ASSIGNCOPY(_pdfRepresentation, imageData);
2019-11-22 07:16:05 +00:00
}
return self;
2019-11-18 10:23:30 +00:00
}
- (void) dealloc
{
RELEASE(_pageReps);
RELEASE(_pdfRepresentation);
[super dealloc];
}
2019-11-18 10:23:30 +00:00
- (NSRect) bounds
{
2019-12-02 15:53:08 +00:00
NSSize size = [self size];
NSRect rect = NSMakeRect(0, 0, size.width, size.height);
return rect;
2019-11-18 10:23:30 +00:00
}
- (NSInteger) currentPage
{
2019-11-22 07:16:05 +00:00
return _currentPage;
2019-11-18 10:23:30 +00:00
}
- (void) setCurrentPage: (NSInteger)currentPage
{
2019-11-22 07:16:05 +00:00
_currentPage = currentPage;
2019-11-18 10:23:30 +00:00
}
- (NSInteger) pageCount
{
return [_pageReps count];
2019-11-18 10:23:30 +00:00
}
- (NSData *) PDFRepresentation
{
2019-11-22 07:16:05 +00:00
return _pdfRepresentation;
2019-11-18 10:23:30 +00:00
}
2019-12-02 15:53:08 +00:00
// Override to draw the specified page...
- (BOOL) draw
{
NSBitmapImageRep *rep = [_pageReps objectAtIndex: _currentPage - 1];
return [rep draw];
2019-12-02 15:53:08 +00:00
}
2019-11-18 10:23:30 +00:00
@end