Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7643 views
1
#include "common.h"
2
#import "MuPageViewReflow.h"
3
4
NSString *textAsHtml(fz_document *doc, int pageNum)
5
{
6
NSString *str = nil;
7
fz_page *page = NULL;
8
fz_text_sheet *sheet = NULL;
9
fz_text_page *text = NULL;
10
fz_device *dev = NULL;
11
fz_matrix ctm;
12
fz_buffer *buf = NULL;
13
fz_output *out = NULL;
14
15
fz_var(page);
16
fz_var(sheet);
17
fz_var(text);
18
fz_var(dev);
19
fz_var(buf);
20
fz_var(out);
21
22
fz_try(ctx)
23
{
24
ctm = fz_identity;
25
sheet = fz_new_text_sheet(ctx);
26
text = fz_new_text_page(ctx);
27
dev = fz_new_text_device(ctx, sheet, text);
28
page = fz_load_page(ctx, doc, pageNum);
29
fz_run_page(ctx, page, dev, &ctm, NULL);
30
fz_drop_device(ctx, dev);
31
dev = NULL;
32
33
fz_analyze_text(ctx, sheet, text);
34
35
buf = fz_new_buffer(ctx, 256);
36
out = fz_new_output_with_buffer(ctx, buf);
37
fz_printf(ctx, out, "<html>\n");
38
fz_printf(ctx, out, "<style>\n");
39
fz_printf(ctx, out, "body{margin:0;}\n");
40
fz_printf(ctx, out, "div.page{background-color:white;}\n");
41
fz_printf(ctx, out, "div.block{margin:0pt;padding:0pt;}\n");
42
fz_printf(ctx, out, "div.metaline{display:table;width:100%%}\n");
43
fz_printf(ctx, out, "div.line{display:table-row;}\n");
44
fz_printf(ctx, out, "div.cell{display:table-cell;padding-left:0.25em;padding-right:0.25em}\n");
45
//fz_printf(ctx, out, "p{margin:0;padding:0;}\n");
46
fz_printf(ctx, out, "</style>\n");
47
fz_printf(ctx, out, "<body style=\"margin:0\"><div style=\"padding:10px\" id=\"content\">");
48
fz_print_text_page_html(ctx, out, text);
49
fz_printf(ctx, out, "</div></body>\n");
50
fz_printf(ctx, out, "<style>\n");
51
fz_print_text_sheet(ctx, out, sheet);
52
fz_printf(ctx, out, "</style>\n</html>\n");
53
54
out = NULL;
55
56
str = [[[NSString alloc] initWithBytes:buf->data length:buf->len encoding:NSUTF8StringEncoding] autorelease];
57
}
58
fz_always(ctx)
59
{
60
fz_drop_text_page(ctx, text);
61
fz_drop_text_sheet(ctx, sheet);
62
fz_drop_device(ctx, dev);
63
fz_drop_output(ctx, out);
64
fz_drop_buffer(ctx, buf);
65
fz_drop_page(ctx, page);
66
}
67
fz_catch(ctx)
68
{
69
str = nil;
70
}
71
72
return str;
73
}
74
75
@implementation MuPageViewReflow
76
{
77
int number;
78
float scale;
79
}
80
81
- (id)initWithFrame:(CGRect)frame document:(MuDocRef *)aDoc page:(int)aNumber
82
{
83
self = [super initWithFrame:frame];
84
if (self) {
85
number = aNumber;
86
scale = 1.0;
87
self.scalesPageToFit = NO;
88
[self setDelegate:self];
89
dispatch_async(queue, ^{
90
__block NSString *cont = [textAsHtml(aDoc->doc, aNumber) retain];
91
dispatch_async(dispatch_get_main_queue(), ^{
92
[self loadHTMLString:cont baseURL:nil];
93
});
94
});
95
}
96
return self;
97
}
98
99
-(void) webViewDidFinishLoad:(UIWebView *)webView
100
{
101
[self stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('content').style.zoom=\"%f\"", scale]];
102
}
103
104
-(void) dealloc
105
{
106
[self setDelegate:nil];
107
[super dealloc];
108
}
109
110
-(int) number
111
{
112
return number;
113
}
114
115
-(void) willRotate {}
116
-(void) showLinks {}
117
-(void) hideLinks {}
118
-(void) showSearchResults: (int)count {}
119
-(void) clearSearchResults {}
120
-(void) textSelectModeOn {}
121
-(void) textSelectModeOff {}
122
-(void) inkModeOn {}
123
-(void) inkModeOff {}
124
-(void) saveSelectionAsMarkup:(int)type {}
125
-(void) saveInk {}
126
-(void) deselectAnnotation {}
127
-(void) deleteSelectedAnnotation {}
128
-(void) update {}
129
130
-(void) resetZoomAnimated: (BOOL)animated
131
{
132
[self.scrollView setContentOffset:CGPointZero animated:NO];
133
}
134
135
-(void) setScale:(float)aFloat
136
{
137
scale = aFloat;
138
[self stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('content').style.zoom=\"%f\"", scale]];
139
}
140
141
-(MuTapResult *) handleTap:(CGPoint)pt
142
{
143
return nil;
144
}
145
146
@end
147
148