Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7640 views
1
#import "common.h"
2
#import "MuHitView.h"
3
4
@implementation MuHitView
5
{
6
CGSize pageSize;
7
int hitCount;
8
CGRect hitRects[500];
9
int linkPage[500];
10
char *linkUrl[500];
11
UIColor *color;
12
}
13
14
- (id) initWithSearchResults: (int)n forDocument: (fz_document *)doc
15
{
16
self = [super initWithFrame: CGRectMake(0,0,100,100)];
17
if (self) {
18
[self setOpaque: NO];
19
20
color = [[UIColor colorWithRed: 0x25/255.0 green: 0x72/255.0 blue: 0xAC/255.0 alpha: 0.5] retain];
21
22
pageSize = CGSizeMake(100,100);
23
24
for (int i = 0; i < n && i < nelem(hitRects); i++) {
25
fz_rect bbox = search_result_bbox(doc, i); // this is thread-safe enough
26
hitRects[i].origin.x = bbox.x0;
27
hitRects[i].origin.y = bbox.y0;
28
hitRects[i].size.width = bbox.x1 - bbox.x0;
29
hitRects[i].size.height = bbox.y1 - bbox.y0;
30
}
31
hitCount = n;
32
}
33
return self;
34
}
35
36
- (id) initWithLinks: (fz_link*)link forDocument: (fz_document *)doc
37
{
38
self = [super initWithFrame: CGRectMake(0,0,100,100)];
39
if (self) {
40
[self setOpaque: NO];
41
42
color = [[UIColor colorWithRed: 0xAC/255.0 green: 0x72/255.0 blue: 0x25/255.0 alpha: 0.5] retain];
43
44
pageSize = CGSizeMake(100,100);
45
46
while (link && hitCount < nelem(hitRects)) {
47
if (link->dest.kind == FZ_LINK_GOTO || link->dest.kind == FZ_LINK_URI) {
48
fz_rect bbox = link->rect;
49
hitRects[hitCount].origin.x = bbox.x0;
50
hitRects[hitCount].origin.y = bbox.y0;
51
hitRects[hitCount].size.width = bbox.x1 - bbox.x0;
52
hitRects[hitCount].size.height = bbox.y1 - bbox.y0;
53
linkPage[hitCount] = link->dest.kind == FZ_LINK_GOTO ? link->dest.ld.gotor.page : -1;
54
linkUrl[hitCount] = link->dest.kind == FZ_LINK_URI ? strdup(link->dest.ld.uri.uri) : nil;
55
hitCount++;
56
}
57
link = link->next;
58
}
59
}
60
return self;
61
}
62
63
- (void) setPageSize: (CGSize)s
64
{
65
pageSize = s;
66
// if page takes a long time to load we may have drawn at the initial (wrong) size
67
[self setNeedsDisplay];
68
}
69
70
- (MuTapResult *) handleTap:(CGPoint)pt
71
{
72
CGSize scale = fitPageToScreen(pageSize, self.bounds.size);
73
pt.x /= scale.width;
74
pt.y /= scale.height;
75
76
for (int i = 0; i < hitCount; i++)
77
{
78
if (CGRectContainsPoint(hitRects[i], pt))
79
{
80
if (linkPage[i] >= 0)
81
{
82
return [[[MuTapResultInternalLink alloc] initWithPageNumber:linkPage[i]] autorelease];
83
}
84
if (linkUrl[i])
85
{
86
NSString *url = [NSString stringWithUTF8String:linkUrl[i]];
87
return [[[MuTapResultExternalLink alloc] initWithUrl:url] autorelease];
88
}
89
}
90
}
91
92
return nil;
93
}
94
95
- (void) drawRect: (CGRect)r
96
{
97
CGSize scale = fitPageToScreen(pageSize, self.bounds.size);
98
99
[color set];
100
101
for (int i = 0; i < hitCount; i++) {
102
CGRect rect = hitRects[i];
103
rect.origin.x *= scale.width;
104
rect.origin.y *= scale.height;
105
rect.size.width *= scale.width;
106
rect.size.height *= scale.height;
107
UIRectFill(rect);
108
}
109
}
110
111
- (void) dealloc
112
{
113
int i;
114
[color release];
115
for (i = 0; i < hitCount; i++)
116
free(linkUrl[i]);
117
[super dealloc];
118
}
119
120
@end
121
122