-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoordconv.c
70 lines (59 loc) · 1.82 KB
/
coordconv.c
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
#include <stdbool.h>
#include <math.h>
#include "coordconv.h"
#include <poppler.h>
CoordConv coord_conv_create(PopplerPage *p, const Rectangle *r, bool i, int rotation)
{
CoordConv cc;
cc.rect = *r;
cc.inverty = i;
double width, height;
poppler_page_get_size(p, &width, &height);
if (rotation % 180 != 0)
{
double temp = width;
width = height;
height = temp;
}
cc.xscale = width / (double)r->width;
cc.yscale = height / (double)r->height;
return cc;
}
double coord_conv_to_pdf_x(const CoordConv *cc, int x)
{
return (x - cc->rect.x) * cc->xscale;
}
double coord_conv_to_pdf_y(const CoordConv *cc, int y)
{
if (cc->inverty)
return (cc->rect.height - (y - cc->rect.y)) * cc->yscale;
return (y - cc->rect.y) * cc->yscale;
}
Rectangle coord_conv_to_pdf(const CoordConv *cc, const Rectangle *r)
{
return (Rectangle){
(int)coord_conv_to_pdf_x(cc, r->x),
(int)coord_conv_to_pdf_y(cc, r->y),
(int)(coord_conv_to_pdf_x(cc, r->x + r->width) - coord_conv_to_pdf_x(cc, r->x)),
(int)(coord_conv_to_pdf_y(cc, r->y + r->height) - coord_conv_to_pdf_y(cc, r->y))
};
}
double coord_conv_to_screen_x(const CoordConv *cc, int x)
{
return x / cc->xscale + cc->rect.x;
}
double coord_conv_to_screen_y(const CoordConv *cc, int y)
{
if (cc->inverty)
return cc->rect.y + cc->rect.height - y / cc->yscale;
return y / cc->yscale + cc->rect.y;
}
Rectangle coord_conv_to_screen(const CoordConv *cc, const Rectangle *r)
{
return (Rectangle){
(int)coord_conv_to_screen_x(cc, r->x),
(int)coord_conv_to_screen_y(cc, r->y),
(int)(coord_conv_to_screen_x(cc, r->x + r->width) - coord_conv_to_screen_x(cc, r->x)),
(int)(coord_conv_to_screen_y(cc, r->y + r->height) - coord_conv_to_screen_y(cc, r->y))
};
}