Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7639 views
1
#include "common.h"
2
3
fz_context *ctx;
4
dispatch_queue_t queue;
5
float screenScale = 1;
6
7
CGSize fitPageToScreen(CGSize page, CGSize screen)
8
{
9
float hscale = screen.width / page.width;
10
float vscale = screen.height / page.height;
11
float scale = fz_min(hscale, vscale);
12
hscale = floorf(page.width * scale) / page.width;
13
vscale = floorf(page.height * scale) / page.height;
14
return CGSizeMake(hscale, vscale);
15
}
16
17
static int hit_count = 0;
18
static fz_rect hit_bbox[500];
19
20
int search_page(fz_document *doc, int number, char *needle, fz_cookie *cookie)
21
{
22
fz_page *page = fz_load_page(ctx, doc, number);
23
24
fz_text_sheet *sheet = fz_new_text_sheet(ctx);
25
fz_text_page *text = fz_new_text_page(ctx);
26
fz_device *dev = fz_new_text_device(ctx, sheet, text);
27
fz_run_page(ctx, page, dev, &fz_identity, cookie);
28
fz_drop_device(ctx, dev);
29
30
hit_count = fz_search_text_page(ctx, text, needle, hit_bbox, nelem(hit_bbox));
31
32
fz_drop_text_page(ctx, text);
33
fz_drop_text_sheet(ctx, sheet);
34
fz_drop_page(ctx, page);
35
36
return hit_count;
37
}
38
39
fz_rect search_result_bbox(fz_document *doc, int i)
40
{
41
return hit_bbox[i];
42
}
43
44
static void releasePixmap(void *info, const void *data, size_t size)
45
{
46
if (queue)
47
dispatch_async(queue, ^{
48
fz_drop_pixmap(ctx, info);
49
});
50
else
51
{
52
fz_drop_pixmap(ctx, info);
53
}
54
}
55
56
CGDataProviderRef CreateWrappedPixmap(fz_pixmap *pix)
57
{
58
unsigned char *samples = fz_pixmap_samples(ctx, pix);
59
int w = fz_pixmap_width(ctx, pix);
60
int h = fz_pixmap_height(ctx, pix);
61
return CGDataProviderCreateWithData(pix, samples, w * 4 * h, releasePixmap);
62
}
63
64
CGImageRef CreateCGImageWithPixmap(fz_pixmap *pix, CGDataProviderRef cgdata)
65
{
66
int w = fz_pixmap_width(ctx, pix);
67
int h = fz_pixmap_height(ctx, pix);
68
CGColorSpaceRef cgcolor = CGColorSpaceCreateDeviceRGB();
69
CGImageRef cgimage = CGImageCreate(w, h, 8, 32, 4 * w, cgcolor, kCGBitmapByteOrderDefault, cgdata, NULL, NO, kCGRenderingIntentDefault);
70
CGColorSpaceRelease(cgcolor);
71
return cgimage;
72
}
73
74