Debian Codegraph @

texk/web2c/xetexdir/XeTeX_pic.c

raw
1 /****************************************************************************\
2 Part of the XeTeX typesetting system
3 Copyright (c) 1994-2008 by SIL International
4 Copyright (c) 2009 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 /*
34 XeTeX_pic.c
35 interface between xetex and graphics files
36 only needs to get image dimensions, not actually load/process the file
37 */
38
39 #define EXTERN extern
40 #include "xetexd.h"
41
42 #include "XeTeX_ext.h"
43
44 #include <kpathsea/c-ctype.h>
45 #include <kpathsea/line.h>
46 #include <kpathsea/readable.h>
47 #include <kpathsea/variable.h>
48 #include <kpathsea/absolute.h>
49
50 #include "pdfimage.h"
51 #include "image/pngimage.h"
52 #include "image/jpegimage.h"
53 #include "image/bmpimage.h"
54
55
56 int
57 countpdffilepages(void)
58 {
59 int rval = 0;
60
61 char* pic_path = kpse_find_file((char*)nameoffile + 1, kpse_pict_format, 1);
62 if (pic_path) {
63 rval = pdf_count_pages(pic_path);
64 free(pic_path);
65 }
66
67 return rval;
68 }
69
70
71 /*
72 locate picture file from /nameoffile+1/ using kpathsearch
73 pdfBoxType indicates which pdf bounding box to use (0 for \XeTeXpicfile)
74 page indicates which page is wanted (0-based)
75 return 0 for success, or non-zero error code for failure
76 return full path in *path
77 return bounds (tex points) in *bounds
78 */
79
80 #ifdef _WIN32
81 #undef fopen
82 #define fopen fsyscp_fopen
83 #endif
84
85 int
86 find_pic_file(char** path, realrect* bounds, int pdfBoxType, int page)
87 {
88 int err = -1;
89 FILE* fp = NULL;
90 char* pic_path = kpse_find_file((char*)nameoffile + 1, kpse_pict_format, 1);
91
92 *path = NULL;
93 bounds->x = bounds->y = bounds->wd = bounds->ht = 0.0;
94
95 if (pic_path == NULL)
96 goto done;
97
98 /* if cmd was \XeTeXpdffile, use xpdflib to read it */
99 if (pdfBoxType != 0) {
100 err = pdf_get_rect(pic_path, page, pdfBoxType, bounds);
101 goto done;
102 }
103
104 /* otherwise try graphics formats that we know */
105 fp = fopen(pic_path, FOPEN_RBIN_MODE);
106 if (fp == NULL)
107 goto done;
108
109 if (check_for_jpeg(fp)) {
110 struct JPEG_info info;
111 err = JPEG_scan_file(&info, fp);
112 if (err == 0) {
113 bounds->wd = (info.width * 72.27) / info.xdpi;
114 bounds->ht = (info.height * 72.27) / info.ydpi;
115 }
116 goto done;
117 }
118
119 if (check_for_bmp(fp)) {
120 struct bmp_info info;
121 err = bmp_scan_file(&info, fp);
122 if (err == 0) {
123 bounds->wd = (info.width * 72.27) / info.xdpi;
124 bounds->ht = (info.height * 72.27) / info.ydpi;
125 }
126 goto done;
127 }
128
129 if (check_for_png(fp)) {
130 struct png_info info;
131 err = png_scan_file(&info, fp);
132 if (err == 0) {
133 bounds->wd = (info.width * 72.27) / info.xdpi;
134 bounds->ht = (info.height * 72.27) / info.ydpi;
135 }
136 goto done;
137 }
138
139 /* could support other file types here (TIFF, WMF, etc?) */
140
141 done:
142 if (fp != NULL)
143 fclose(fp);
144
145 if (err == 0)
146 *path = pic_path;
147 else {
148 if (pic_path != NULL)
149 free(pic_path);
150 }
151
152 return err;
153 }