summaryrefslogtreecommitdiff
path: root/platform/ios/Classes/MuPrintPageRenderer.m
blob: b92b99c0a20dbcae583172998dbf62970fd22c52 (plain)
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
#include "common.h"
#import "MuPrintPageRenderer.h"

const int MaxStripPixels = 1024*1024;

@implementation MuPrintPageRenderer
{
	MuDocRef *docRef;
}

-(id) initWithDocRef:(MuDocRef *)aDocRef
{
	self = [super init];
	if (self)
	{
		docRef = [aDocRef retain];
	}
	return self;
}

-(void) dealloc
{
	[docRef release];
	[super dealloc];
}

-(NSInteger) numberOfPages
{
	__block NSInteger npages = 0;
	dispatch_sync(queue, ^{
		fz_try(ctx)
		{
			npages = fz_count_pages(ctx, docRef->doc);
		}
		fz_catch(ctx)
		{
		}
	});
	return npages;
}

static fz_page *getPage(fz_document *doc, NSInteger pageIndex)
{
	__block fz_page *page = NULL;

	dispatch_sync(queue, ^{
		fz_try(ctx)
		{
			page = fz_load_page(ctx, doc, (int)pageIndex);
		}
		fz_catch(ctx)
		{
			printf("Failed to load page\n");
		}
	});

	return page;
}

static CGSize getPageSize(fz_document *doc, fz_page *page)
{
	__block CGSize size = {0.0,0.0};

	dispatch_sync(queue, ^{
		fz_try(ctx)
		{
			fz_rect bounds;
			fz_bound_page(ctx, page, &bounds);
			size.width = bounds.x1 - bounds.x0;
			size.height = bounds.y1 - bounds.y0;
		}
		fz_catch(ctx)
		{
			printf("Failed to find page bounds\n");
		}
	});

	return size;
}

static fz_pixmap *createPixMap(CGSize size)
{
	__block fz_pixmap *pix = NULL;

	dispatch_sync(queue, ^{
		fz_try(ctx)
		{
			pix = fz_new_pixmap(ctx, fz_device_rgb(ctx), size.width, size.height, 1);
		}
		fz_catch(ctx)
		{
			printf("Failed to create pixmap\n");
		}
	});

	return pix;
}

static void freePage(fz_document *doc, fz_page *page)
{
	dispatch_sync(queue, ^{
		fz_drop_page(ctx, page);
	});
}

static void renderPage(fz_document *doc, fz_page *page, fz_pixmap *pix, fz_matrix *ctm)
{
	dispatch_sync(queue, ^{
		fz_device *dev = NULL;
		fz_var(dev);
		fz_try(ctx)
		{
			dev = fz_new_draw_device(ctx, pix);
			fz_clear_pixmap_with_value(ctx, pix, 0xFF);
			fz_run_page(ctx, page, dev, ctm, NULL);
		}
		fz_always(ctx)
		{
			fz_drop_device(ctx, dev);
		}
		fz_catch(ctx)
		{
			printf("Failed to render page\n");
		}
	});
}

-(void) drawPageAtIndex:(NSInteger)pageIndex inRect:(CGRect)printableRect
{
	fz_page *page = NULL;
	fz_pixmap *pix = NULL;
	CGDataProviderRef dataref = NULL;
	CGImageRef img = NULL;
	CGContextRef cgctx = UIGraphicsGetCurrentContext();
	float dpi = 300.0;
	float ppi = 72.0;

	if (!cgctx) return;

	CGSize paperSize = self.paperRect.size;
	page = getPage(docRef->doc, pageIndex);
	if (page == NULL) return;

	CGSize pageSize = getPageSize(docRef->doc, page);
	if (pageSize.width == 0.0 || pageSize.height == 0.0)
		goto exit;

	CGSize scale = fitPageToScreen(pageSize, paperSize);
	pageSize.width *= scale.width;
	pageSize.height *= scale.height;

	CGSize pageSizePix = {roundf(pageSize.width * dpi / ppi), roundf(pageSize.height * dpi /ppi)};
	int max_strip_height = MaxStripPixels / (int)pageSizePix.width;
	if (pageSizePix.height > max_strip_height)
		pageSizePix.height = max_strip_height;
	CGSize stripSize = {pageSize.width, pageSizePix.height * ppi / dpi};

	float cursor = 0.0;

	while (cursor < pageSize.height)
	{
		// Overlap strips by 1 point
		if (cursor > 0.0)
			cursor -= 1.0;

		pix = createPixMap(pageSizePix);
		if (!pix)
			goto exit;

		dataref = CreateWrappedPixmap(pix);
		if (dataref == NULL)
			goto exit;

		img = CreateCGImageWithPixmap(pix, dataref);
		if (img == NULL)
			goto exit;

		fz_matrix ctm;
		fz_scale(&ctm, dpi / ppi, -dpi / ppi);
		fz_pre_translate(&ctm, 0, -stripSize.height-cursor);
		fz_pre_scale(&ctm, scale.width, scale.height);

		renderPage(docRef->doc, page, pix, &ctm);

		CGRect rect = {{0.0,cursor},stripSize};
		CGContextDrawImage(cgctx, rect, img);

		CGImageRelease(img);
		img = NULL;
		CGDataProviderRelease(dataref); // releases pix
		dataref = NULL;

		cursor += stripSize.height;
	}

exit:
	freePage(docRef->doc, page);
	CGImageRelease(img);
	CGDataProviderRelease(dataref); //releases pix
}

@end