Debian Codegraph @

texk/web2c/xetexdir/pdfimage.cpp

raw
1 /****************************************************************************\
2 Part of the XeTeX typesetting system
3 Copyright (c) 1994-2008 by SIL International
4 Copyright (c) 2009-2018 by Jonathan Kew
5
6 SIL Author(s): Jonathan Kew
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
23 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
24 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
27 Except as contained in this notice, the name of the copyright holders
28 shall not be used in advertising or otherwise to promote the sale,
29 use or other dealings in this Software without prior written
30 authorization from the copyright holders.
31 \****************************************************************************/
32
33 #include <w2c/config.h>
34
35 #include "XeTeX_ext.h"
36
37 #include "pdfimage.h"
38
39 /*
40 * From TeX Live 2021, we use pplib by Pawe\l Jackowski instead of
41 * libpoppler
42 */
43 extern "C" {
44 #include "ppapi.h"
45 }
46
47 #include "XeTeX_ext.h"
48
49 /* use our own fmin function because it seems to be missing on certain platforms */
50 inline double
51 my_fmin(double x, double y)
52 {
53 return (x < y) ? x : y;
54 }
55
56 int
57 pdf_get_rect(char* filename, int page_num, int pdf_box, realrect* box)
58 /* return the box converted to TeX points */
59 {
60 ppdoc* doc = ppdoc_load(filename);
61
62 if (!doc) {
63 return -1;
64 }
65
66 int pages = ppdoc_page_count(doc);
67
68 if (page_num > pages)
69 page_num = pages;
70 if (page_num < 0)
71 page_num = pages + 1 + page_num;
72 if (page_num < 1)
73 page_num = 1;
74
75 ppref* page = ppdoc_page(doc, page_num);
76 ppdict* pdict = page->object.dict;
77
78 pprect Rect;
79 pprect* r;
80
81 switch (pdf_box) {
82 default:
83 case pdfbox_crop:
84 r = ppdict_get_box(pdict, "CropBox", &Rect);
85 break;
86 case pdfbox_media:
87 r = ppdict_get_box(pdict, "MediaBox", &Rect);
88 break;
89 case pdfbox_bleed:
90 r = ppdict_get_box(pdict, "BleedBox", &Rect);
91 break;
92 case pdfbox_trim:
93 r = ppdict_get_box(pdict, "TrimBox", &Rect);
94 break;
95 case pdfbox_art:
96 r = ppdict_get_box(pdict, "ArtBox", &Rect);
97 break;
98 }
99
100 /*
101 * In pplib, r can be NULL. If r == NULL, we try "CropBox",
102 * "MediaBox", "BleedBox", "TrimBox", "ArtBox" in this order.
103 */
104 if (!r) {
105 r = ppdict_get_box(pdict, "CropBox", &Rect);
106 }
107 if (!r) {
108 r = ppdict_get_box(pdict, "MediaBox", &Rect);
109 }
110 if (!r) {
111 r = ppdict_get_box(pdict, "BleedBox", &Rect);
112 }
113 if (!r) {
114 r = ppdict_get_box(pdict, "TrimBox", &Rect);
115 }
116 if (!r) {
117 r = ppdict_get_box(pdict, "ArtBox", &Rect);
118 }
119
120 /*
121 * If r == NULL, return error.
122 */
123 if (!r) {
124 return -1;
125 }
126
127 ppint RotAngle = 0;
128 (void)ppdict_get_int(pdict, "Rotate", &RotAngle);
129 RotAngle = RotAngle % 360;
130 if (RotAngle < 0)
131 RotAngle += 360;
132 if (RotAngle == 90 || RotAngle == 270) {
133 box->wd = 72.27 / 72 * fabs(r->ry - r->ly);
134 box->ht = 72.27 / 72 * fabs(r->rx - r->lx);
135 } else {
136 box->wd = 72.27 / 72 * fabs(r->rx - r->lx);
137 box->ht = 72.27 / 72 * fabs(r->ry - r->ly);
138 }
139 box->x = 72.27 / 72 * my_fmin(r->lx, r->rx);
140 box->y = 72.27 / 72 * my_fmin(r->ly, r->ry);
141
142 ppdoc_free(doc);
143
144 return 0;
145 }
146
147 int
148 pdf_count_pages(char* filename)
149 {
150 int pages = 0;
151 ppdoc* doc = ppdoc_load(filename);
152
153 if (!doc) {
154 return 0;
155 }
156
157 pages = ppdoc_page_count(doc);
158 if (doc) ppdoc_free(doc);
159
160 return pages;
161 }