File: NSPDFImageRep.m

package info (click to toggle)
gnustep-gui 0.32.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,348 kB
  • sloc: objc: 178,763; ansic: 24,089; cpp: 664; yacc: 464; sh: 90; makefile: 72
file content (129 lines) | stat: -rw-r--r-- 3,058 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* Implementation of class NSPDFImageRep
   Copyright (C) 2019 Free Software Foundation, Inc.
   
   By: Gregory Casamento <greg.casamento@gmail.com>
   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.
   
   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.
*/

#import "config.h"

#import <Foundation/NSString.h>
#import <Foundation/NSData.h>
#import "AppKit/NSPasteboard.h"
#import "AppKit/NSPDFImageRep.h"
#import <GNUstepGUI/GSImageMagickImageRep.h>

@implementation NSPDFImageRep

+ (BOOL) canInitWithData: (NSData *)imageData
{
  NSData *header = [imageData subdataWithRange: NSMakeRange(0,4)];
  NSString *str = [[NSString alloc] initWithData: header encoding: NSUTF8StringEncoding];
  BOOL result = [str isEqualToString: @"%PDF"];
  AUTORELEASE(str);
  return result;
}

+ (NSArray *) imagePasteboardTypes
{
  return [NSArray arrayWithObject: NSPDFPboardType];
}

+ (NSArray *) imageUnfilteredPasteboardTypes
{
  return [self imagePasteboardTypes];
}

+ (NSArray *) imageFileTypes
{
  return [NSArray arrayWithObjects: @"pdf", @"PDF", nil];
}

+ (NSArray *) imageUnfilteredFileTypes
{
  return [self imageFileTypes];
}

+ (instancetype) imageRepWithData: (NSData *)imageData
{
  return [[[self class] alloc] initWithData: imageData];
}

- (instancetype) initWithData: (NSData *)imageData
{
  self = [super init];
  if(self != nil)
    {
#if HAVE_IMAGEMAGICK
      ASSIGN(_pageReps, [GSImageMagickImageRep imageRepsWithData: imageData]);
      _size = [[_pageReps objectAtIndex: 0] size];
      _currentPage = 1;
#else
      ASSIGN(_pageReps, [NSArray array]);
      _size = NSMakeSize(0,0);
      _currentPage = 0;
#endif
      ASSIGNCOPY(_pdfRepresentation, imageData);
    }
  return self;
}

- (void) dealloc
{
  RELEASE(_pageReps);
  RELEASE(_pdfRepresentation);
  [super dealloc];
}

- (NSRect) bounds
{
  NSSize size = [self size];
  NSRect rect = NSMakeRect(0, 0, size.width, size.height);
  return rect;
}

- (NSInteger) currentPage
{
  return _currentPage;
}

- (void) setCurrentPage: (NSInteger)currentPage
{
  _currentPage = currentPage;
}

- (NSInteger) pageCount
{
  return [_pageReps count];
}

- (NSData *) PDFRepresentation
{
  return _pdfRepresentation;
}

// Override to draw the specified page...
- (BOOL) draw
{
  NSBitmapImageRep *rep = [_pageReps objectAtIndex: _currentPage - 1];
  return [rep draw];
}
@end