File: PopplerCachingRenderer.m

package info (click to toggle)
popplerkit.framework 0.0.20051227svn-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,592 kB
  • sloc: objc: 2,303; cpp: 670; sh: 469; ansic: 55; makefile: 29
file content (215 lines) | stat: -rw-r--r-- 5,501 bytes parent folder | download | duplicates (9)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
 * Copyright (C) 2004  Stefan Kleine Stegemann
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#import "PopplerCachingRenderer.h"
#import "PopplerDocument+Rendering.h"
#import "MKLRUCache.h"

#include <math.h>

// default cache size
const unsigned long kDefaultCacheSize = 10485760;

// Helps to build hashcodes. The first number has to
// be an odd prime number.
#define FIRST_TERM(x) 37 * x

/**
 * A key for putting render results in dictionaries.
 */
@interface CacheKey : NSObject
{
   int    pageNum;
   NSRect srcBox;
   float  scale;
}

- (id) initWithPageNum: (unsigned)aPageNum
                srcBox: (NSRect)aBox
                 scale: (float)aScale;
                 
+ (id) keyWithPageNum: (unsigned)aPageNum
               srcBox: (NSRect)aBox
                scale: (float)aScale;

@end


/**
 * Non-Public methods.
 */
@interface PopplerCachingRenderer (Private)
@end


@implementation PopplerCachingRenderer

- (id) initWithDocument: (PopplerDocument*)aDocument
{
   return [self initWithRenderer: [aDocument bufferedRenderer]];
}

- (id) initWithRenderer: (id<PopplerBufferedRenderer>)aRenderer
{
   NSAssert(aRenderer, @"nil renderer");
   
   self = [super init];
   if (self)
   {
      renderer = [(NSObject*)aRenderer retain];
      cache = [[MKLRUCache alloc] initWithMaxSize: [PopplerCachingRenderer defaultCacheSize]];
   }
   return self;
}

- (void) dealloc
{
   [(NSObject*)cache release];
   [(NSObject*)renderer release];
   [super dealloc];
}

+ (unsigned long) defaultCacheSize
{
   return kDefaultCacheSize;
}

- (void) setCacheSize: (unsigned long)aSize
{
   [cache setMaximumSize: aSize];
}

- (id) renderPage: (PopplerPage*)aPage
           srcBox: (NSRect)aBox
            scale: (float)aScale
{
   CacheKey* key = [CacheKey keyWithPageNum: [aPage index]
                                     srcBox: aBox
                                      scale: aScale];

   id cachedResult = [cache objectForKey: key];
   if (!cachedResult)
   {
      cachedResult =  [renderer renderPage: aPage
                                    srcBox: aBox
                                     scale: aScale];
      NS_DURING
         [cache putObject: cachedResult forKey: key];
      NS_HANDLER
         NSLog(@"failed to cache rendered page for key %@: %@",
               [key description], [localException reason]);
      NS_ENDHANDLER
   }

   return cachedResult;
}
            
- (id) renderPage: (PopplerPage*)aPage
            scale: (float)aScale
{
   return [self renderPage: aPage
                    srcBox: NSMakeRect(-1, -1, -1, -1)
                     scale: aScale];
}


@end


/* ----------------------------------------------------- */
/*  Category Private                                     */
/* ----------------------------------------------------- */

@implementation PopplerCachingRenderer (Private)
@end


/* ----------------------------------------------------- */
/*  Class CacheKey                                       */
/* ----------------------------------------------------- */

@implementation CacheKey

- (id) initWithPageNum: (unsigned)aPageNum
                srcBox: (NSRect)aBox
                 scale: (float)aScale
{
   self = [super init];
   if (self)
   {
      srcBox = aBox;
      scale = aScale;
      pageNum = aPageNum;
   }
   return self;
}

                 
+ (id) keyWithPageNum: (unsigned)aPageNum
               srcBox: (NSRect)aBox
                scale: (float)aScale
{
   return [[[CacheKey alloc] initWithPageNum: aPageNum
                                      srcBox: aBox
                                       scale: aScale] autorelease];
}

- (unsigned) hash
{
   unsigned result = 23; // SEED
   result = FIRST_TERM(result) + pageNum;

   // convert the scale factor to int, using 4 decimal
   // precision. This should be sufficient since 0.75411
   // and 0.75412 doesn't really make a difference to the
   // human sitting in front of the screen
   result = FIRST_TERM(result) + (int)(scale * 10000);

   // we also use a 4 decimal precision for the rectangle
   result = FIRST_TERM(result) + (int)(NSMinX(srcBox) * 10000);
   result = FIRST_TERM(result) + (int)(NSMinY(srcBox) * 10000);
   result = FIRST_TERM(result) + (int)(NSWidth(srcBox) * 10000);
   result = FIRST_TERM(result) + (int)(NSHeight(srcBox) * 10000);
   
   return result;
}

- (BOOL) isEqual: (id)anObject
{
   if ((!anObject) || (![anObject isKindOfClass: [CacheKey class]]))
   {
      return NO;
   }
   
   return ([self hash] == [anObject hash]);
}

- (id) copyWithZone: (NSZone*)zone
{
   return [self retain];
}

- (NSString*) description
{
   return [NSString stringWithFormat:
             @"{page=%d, scale=%f, srcBox=(%f, %f, %f, %f)}",
             pageNum, scale, NSMinX(srcBox), NSMinY(srcBox),
             NSWidth(srcBox), NSHeight(srcBox)];
}

@end