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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
|
//
// MuPageView.m
// MuPDF
//
// Copyright (c) 2013 Artifex Software, Inc. All rights reserved.
//
#include "common.h"
#include "mupdf/pdf.h"
#import "MuTextFieldController.h"
static void releasePixmap(void *info, const void *data, size_t size)
{
if (queue)
dispatch_async(queue, ^{
fz_drop_pixmap(ctx, info);
});
else
fz_drop_pixmap(ctx, info);
}
static UIImage *newImageWithPixmap(fz_pixmap *pix)
{
unsigned char *samples = fz_pixmap_samples(ctx, pix);
int w = fz_pixmap_width(ctx, pix);
int h = fz_pixmap_height(ctx, pix);
CGDataProviderRef cgdata = CGDataProviderCreateWithData(pix, samples, w * 4 * h, releasePixmap);
CGColorSpaceRef cgcolor = CGColorSpaceCreateDeviceRGB();
CGImageRef cgimage = CGImageCreate(w, h, 8, 32, 4 * w,
cgcolor, kCGBitmapByteOrderDefault,
cgdata, NULL, NO, kCGRenderingIntentDefault);
UIImage *image = [[UIImage alloc]
initWithCGImage: cgimage
scale: screenScale
orientation: UIImageOrientationUp];
CGDataProviderRelease(cgdata);
CGColorSpaceRelease(cgcolor);
CGImageRelease(cgimage);
return image;
}
static NSArray *enumerateWidgetRects(fz_document *doc, fz_page *page, CGSize pageSize, CGSize screenSize)
{
pdf_document *idoc = pdf_specifics(doc);
pdf_widget *widget;
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:10];
CGSize scale = fitPageToScreen(pageSize, screenSize);
if (!idoc)
return nil;
for (widget = pdf_first_widget(idoc, (pdf_page *)page); widget; widget = pdf_next_widget(widget))
{
fz_rect rect;
pdf_bound_widget(widget, &rect);
[arr addObject:[NSValue valueWithCGRect:CGRectMake(
rect.x0 * scale.width,
rect.y0 * scale.height,
(rect.x1-rect.x0) * scale.width,
(rect.y1-rect.y0) * scale.height)]];
}
return [arr retain];
}
static int setFocussedWidgetText(fz_document *doc, fz_page *page, const char *text)
{
int accepted;
fz_try(ctx)
{
pdf_document *idoc = pdf_specifics(doc);
if (idoc)
{
pdf_widget *focus = pdf_focused_widget(idoc);
if (focus)
{
accepted = pdf_text_widget_set_text(idoc, focus, (char *)text);
}
}
}
fz_catch(ctx)
{
accepted = 0;
}
return accepted;
}
static fz_display_list *create_page_list(fz_document *doc, fz_page *page)
{
fz_display_list *list;
fz_device *dev = NULL;
fz_var(dev);
fz_try(ctx)
{
list = fz_new_display_list(ctx);
dev = fz_new_list_device(ctx, list);
fz_run_page_contents(doc, page, dev, &fz_identity, NULL);
}
fz_always(ctx)
{
fz_free_device(dev);
}
fz_catch(ctx)
{
return NULL;
}
return list;
}
static fz_display_list *create_annot_list(fz_document *doc, fz_page *page)
{
fz_display_list *list;
fz_device *dev = NULL;
fz_var(dev);
fz_try(ctx)
{
fz_annot *annot;
pdf_document *idoc = pdf_specifics(doc);
if (idoc)
pdf_update_page(idoc, (pdf_page *)page);
list = fz_new_display_list(ctx);
dev = fz_new_list_device(ctx, list);
for (annot = fz_first_annot(doc, page); annot; annot = fz_next_annot(doc, annot))
fz_run_annot(doc, page, annot, dev, &fz_identity, NULL);
}
fz_always(ctx)
{
fz_free_device(dev);
}
fz_catch(ctx)
{
return NULL;
}
return list;
}
static UIImage *renderPage(fz_document *doc, fz_display_list *page_list, fz_display_list *annot_list, CGSize pageSize, CGSize screenSize, CGRect tileRect, float zoom)
{
fz_irect bbox;
fz_rect rect;
fz_matrix ctm;
fz_device *dev = NULL;
fz_pixmap *pix = NULL;
CGSize scale;
screenSize.width *= screenScale;
screenSize.height *= screenScale;
tileRect.origin.x *= screenScale;
tileRect.origin.y *= screenScale;
tileRect.size.width *= screenScale;
tileRect.size.height *= screenScale;
scale = fitPageToScreen(pageSize, screenSize);
fz_scale(&ctm, scale.width * zoom, scale.height * zoom);
bbox.x0 = tileRect.origin.x;
bbox.y0 = tileRect.origin.y;
bbox.x1 = tileRect.origin.x + tileRect.size.width;
bbox.y1 = tileRect.origin.y + tileRect.size.height;
fz_rect_from_irect(&rect, &bbox);
fz_var(dev);
fz_var(pix);
fz_try(ctx)
{
pix = fz_new_pixmap_with_bbox(ctx, fz_device_rgb(ctx), &bbox);
fz_clear_pixmap_with_value(ctx, pix, 255);
dev = fz_new_draw_device(ctx, pix);
fz_run_display_list(page_list, dev, &ctm, &rect, NULL);
fz_run_display_list(annot_list, dev, &ctm, &rect, NULL);
}
fz_always(ctx)
{
fz_free_device(dev);
}
fz_catch(ctx)
{
fz_drop_pixmap(ctx, pix);
return nil;
}
return newImageWithPixmap(pix);
}
#import "MuPageViewNormal.h"
@implementation MuPageViewNormal
- (void) ensurePageLoaded
{
if (page)
return;
fz_try(ctx)
{
fz_rect bounds;
page = fz_load_page(doc, number);
fz_bound_page(doc, page, &bounds);
pageSize.width = bounds.x1 - bounds.x0;
pageSize.height = bounds.y1 - bounds.y0;
}
fz_catch(ctx)
{
return;
}
}
- (void) ensureDisplaylists
{
[self ensurePageLoaded];
if (!page)
return;
if (!page_list)
page_list = create_page_list(doc, page);
if (!annot_list)
annot_list = create_annot_list(doc, page);
}
-(id) initWithFrame:(CGRect)frame dialogCreator:(id<MuDialogCreator>)dia document:(MuDocRef *)aDoc page:(int)aNumber
{
self = [super initWithFrame: frame];
if (self) {
docRef = [aDoc retain];
doc = docRef->doc;
number = aNumber;
cancel = NO;
dialogCreator = dia;
[self setShowsVerticalScrollIndicator: NO];
[self setShowsHorizontalScrollIndicator: NO];
[self setDecelerationRate: UIScrollViewDecelerationRateFast];
[self setDelegate: self];
// zoomDidFinish/Begin events fire before bounce animation completes,
// making a mess when we rearrange views during the animation.
[self setBouncesZoom: NO];
[self resetZoomAnimated: NO];
// TODO: use a one shot timer to delay the display of this?
loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[loadingView startAnimating];
[self addSubview: loadingView];
[self loadPage];
}
return self;
}
- (void) dealloc
{
// dealloc can trigger in background thread when the queued block is
// our last owner, and releases us on completion.
// Send the dealloc back to the main thread so we don't mess up UIKit.
if (dispatch_get_current_queue() != dispatch_get_main_queue()) {
__block id block_self = self; // don't auto-retain self!
dispatch_async(dispatch_get_main_queue(), ^{ [block_self dealloc]; });
} else {
__block fz_display_list *block_page_list = page_list;
__block fz_display_list *block_annot_list = annot_list;
__block fz_page *block_page = page;
__block fz_document *block_doc = docRef->doc;
dispatch_async(queue, ^{
if (block_page_list)
fz_drop_display_list(ctx, block_page_list);
if (block_annot_list)
fz_drop_display_list(ctx, block_annot_list);
if (block_page)
fz_free_page(block_doc, block_page);
block_page = nil;
});
[docRef release];
[widgetRects release];
[linkView release];
[hitView release];
[tileView release];
[loadingView release];
[imageView release];
[super dealloc];
}
}
- (int) number
{
return number;
}
- (void) showLinks
{
if (!linkView) {
dispatch_async(queue, ^{
[self ensurePageLoaded];
fz_link *links = fz_load_links(doc, page);
dispatch_async(dispatch_get_main_queue(), ^{
linkView = [[MuHitView alloc] initWithLinks: links forDocument: doc];
dispatch_async(queue, ^{
fz_drop_link(ctx, links);
});
if (imageView) {
[linkView setFrame: [imageView frame]];
[linkView setPageSize: pageSize];
}
[self addSubview: linkView];
});
});
}
}
- (void) hideLinks
{
[linkView removeFromSuperview];
[linkView release];
linkView = nil;
}
- (void) showSearchResults: (int)count
{
if (hitView) {
[hitView removeFromSuperview];
[hitView release];
hitView = nil;
}
hitView = [[MuHitView alloc] initWithSearchResults: count forDocument: doc];
if (imageView) {
[hitView setFrame: [imageView frame]];
[hitView setPageSize: pageSize];
}
[self addSubview: hitView];
}
- (void) clearSearchResults
{
if (hitView) {
[hitView removeFromSuperview];
[hitView release];
hitView = nil;
}
}
- (void) resetZoomAnimated: (BOOL)animated
{
// discard tile and any pending tile jobs
tileFrame = CGRectZero;
tileScale = 1;
if (tileView) {
[tileView removeFromSuperview];
[tileView release];
tileView = nil;
}
[self setMinimumZoomScale: 1];
[self setMaximumZoomScale: 5];
[self setZoomScale: 1 animated: animated];
}
- (void) removeFromSuperview
{
cancel = YES;
[super removeFromSuperview];
}
- (void) loadPage
{
if (number < 0 || number >= fz_count_pages(doc))
return;
dispatch_async(queue, ^{
if (!cancel) {
printf("render page %d\n", number);
[self ensureDisplaylists];
CGSize scale = fitPageToScreen(pageSize, self.bounds.size);
CGRect rect = (CGRect){{0.0, 0.0},{pageSize.width * scale.width, pageSize.height * scale.height}};
UIImage *image = renderPage(doc, page_list, annot_list, pageSize, self.bounds.size, rect, 1.0);
widgetRects = enumerateWidgetRects(doc, page, pageSize, self.bounds.size);
dispatch_async(dispatch_get_main_queue(), ^{
[self displayImage: image];
[image release];
});
} else {
printf("cancel page %d\n", number);
}
});
}
- (void) displayImage: (UIImage*)image
{
if (loadingView) {
[loadingView removeFromSuperview];
[loadingView release];
loadingView = nil;
}
if (hitView)
[hitView setPageSize: pageSize];
if (!imageView) {
imageView = [[UIImageView alloc] initWithImage: image];
imageView.opaque = YES;
[self addSubview: imageView];
if (hitView)
[self bringSubviewToFront: hitView];
} else {
[imageView setImage: image];
}
[self resizeImage];
}
- (void) resizeImage
{
if (imageView) {
CGSize imageSize = imageView.image.size;
CGSize scale = fitPageToScreen(imageSize, self.bounds.size);
if (fabs(scale.width - 1) > 0.1) {
CGRect frame = [imageView frame];
frame.size.width = imageSize.width * scale.width;
frame.size.height = imageSize.height * scale.height;
[imageView setFrame: frame];
printf("resized view; queuing up a reload (%d)\n", number);
dispatch_async(queue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
CGSize scale = fitPageToScreen(imageView.image.size, self.bounds.size);
if (fabs(scale.width - 1) > 0.01)
[self loadPage];
});
});
} else {
[imageView sizeToFit];
}
[self setContentSize: imageView.frame.size];
[self layoutIfNeeded];
}
}
- (void) willRotate
{
if (imageView) {
[self resetZoomAnimated: NO];
[self resizeImage];
}
}
- (void) layoutSubviews
{
[super layoutSubviews];
// center the image as it becomes smaller than the size of the screen
CGSize boundsSize = self.bounds.size;
CGRect frameToCenter = loadingView ? loadingView.frame : imageView.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width)
frameToCenter.origin.x = floor((boundsSize.width - frameToCenter.size.width) / 2);
else
frameToCenter.origin.x = 0;
// center vertically
if (frameToCenter.size.height < boundsSize.height)
frameToCenter.origin.y = floor((boundsSize.height - frameToCenter.size.height) / 2);
else
frameToCenter.origin.y = 0;
if (loadingView)
loadingView.frame = frameToCenter;
else
imageView.frame = frameToCenter;
if (imageView)
{
if (hitView)
[hitView setFrame: [imageView frame]];
if (linkView)
[linkView setFrame:[imageView frame]];
}
}
- (UIView*) viewForZoomingInScrollView: (UIScrollView*)scrollView
{
return imageView;
}
- (void) loadTile
{
CGSize screenSize = self.bounds.size;
tileFrame.origin = self.contentOffset;
tileFrame.size = self.bounds.size;
tileFrame = CGRectIntersection(tileFrame, imageView.frame);
tileScale = self.zoomScale;
CGRect frame = tileFrame;
float scale = tileScale;
CGRect viewFrame = frame;
// Adjust viewFrame to be relative to imageView's origin
viewFrame.origin.x -= imageView.frame.origin.x;
viewFrame.origin.y -= imageView.frame.origin.y;
if (scale < 1.01)
return;
dispatch_async(queue, ^{
__block BOOL isValid;
dispatch_sync(dispatch_get_main_queue(), ^{
isValid = CGRectEqualToRect(frame, tileFrame) && scale == tileScale;
});
if (!isValid) {
printf("cancel tile\n");
return;
}
[self ensureDisplaylists];
printf("render tile\n");
UIImage *image = renderPage(doc, page_list, annot_list, pageSize, screenSize, viewFrame, scale);
dispatch_async(dispatch_get_main_queue(), ^{
isValid = CGRectEqualToRect(frame, tileFrame) && scale == tileScale;
if (isValid) {
tileFrame = CGRectZero;
tileScale = 1;
if (tileView) {
[tileView removeFromSuperview];
[tileView release];
tileView = nil;
}
tileView = [[UIImageView alloc] initWithFrame: frame];
[tileView setImage: image];
[self addSubview: tileView];
if (hitView)
[self bringSubviewToFront: hitView];
if (linkView)
[self bringSubviewToFront:linkView];
} else {
printf("discard tile\n");
}
[image release];
});
});
}
- (void) scrollViewDidScrollToTop:(UIScrollView *)scrollView { [self loadTile]; }
- (void) scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { [self loadTile]; }
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView { [self loadTile]; }
- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (!decelerate)
[self loadTile];
}
- (void) scrollViewWillBeginZooming: (UIScrollView*)scrollView withView: (UIView*)view
{
// discard tile and any pending tile jobs
tileFrame = CGRectZero;
tileScale = 1;
if (tileView) {
[tileView removeFromSuperview];
[tileView release];
tileView = nil;
}
}
- (void) scrollViewDidEndZooming: (UIScrollView*)scrollView withView: (UIView*)view atScale: (float)scale
{
[self loadTile];
}
- (void) scrollViewDidZoom: (UIScrollView*)scrollView
{
if (hitView && imageView)
[hitView setFrame: [imageView frame]];
}
- (void) setScale:(float)scale {}
- (void) invokeTextDialog:(NSString *)text
{
[dialogCreator invokeTextDialog:text okayAction:^(NSString *newText) {
dispatch_async(queue, ^{
BOOL accepted = setFocussedWidgetText(doc, page, [newText UTF8String]);
fz_drop_display_list(ctx, annot_list);
annot_list = NULL;
dispatch_async(dispatch_get_main_queue(), ^{
if (accepted)
[self loadPage];
else
[self invokeTextDialog:newText];
});
});
}];
}
- (void) passTapToPage:(CGPoint)pt
{
pdf_document *idoc = pdf_specifics(doc);
CGSize scale = fitPageToScreen(pageSize, self.bounds.size);
pdf_ui_event event;
int changed = 0;
pdf_widget *focus;
if (!idoc)
return;
fz_try(ctx)
{
event.etype = PDF_EVENT_TYPE_POINTER;
event.event.pointer.pt.x = pt.x / scale.width;
event.event.pointer.pt.y = pt.y / scale.height;
event.event.pointer.ptype = PDF_POINTER_DOWN;
changed = pdf_pass_event(idoc, (pdf_page *)page, &event);
event.event.pointer.ptype = PDF_POINTER_UP;
changed |= pdf_pass_event(idoc, (pdf_page *)page, &event);
if (changed)
{
fz_drop_display_list(ctx, annot_list);
annot_list = NULL;
}
focus = pdf_focused_widget(idoc);
if (focus)
{
switch (pdf_widget_get_type(focus))
{
case PDF_WIDGET_TYPE_TEXT:
{
char *text = pdf_text_widget_text(idoc, focus);
NSString *stext = [NSString stringWithUTF8String:text?text:""];
fz_free(ctx, text);
dispatch_async(dispatch_get_main_queue(), ^{
[self invokeTextDialog:stext];
});
break;
}
case PDF_WIDGET_TYPE_LISTBOX:
case PDF_WIDGET_TYPE_COMBOBOX:
break;
case PDF_WIDGET_TYPE_SIGNATURE:
break;
default:
break;
}
}
}
fz_catch(ctx)
{
}
}
- (MuTapResult *) handleTap:(CGPoint)pt
{
CGPoint ipt = [self convertPoint:pt toView:imageView];
for (int i = 0; i < widgetRects.count; i++)
{
CGRect r = [[widgetRects objectAtIndex:i] CGRectValue];
if (CGRectContainsPoint([[widgetRects objectAtIndex:i] CGRectValue], ipt))
{
dispatch_async(queue, ^{
[self passTapToPage:ipt];
});
return [[[MuTapResultWidget alloc] init] autorelease];
}
}
CGPoint lpt = [self convertPoint:pt toView:linkView];
return linkView ? [linkView handleTap:lpt] : nil;
}
@end
|