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
countpdffilepages()57 countpdffilepages()
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 int
find_pic_file(char ** path,realrect * bounds,int pdfBoxType,int page)80 find_pic_file(char** path, realrect* bounds, int pdfBoxType, int page)
81 {
82 int err = -1;
83 FILE* fp = NULL;
84 char* pic_path = kpse_find_file((char*)nameoffile + 1, kpse_pict_format, 1);
85
86 *path = NULL;
87 bounds->x = bounds->y = bounds->wd = bounds->ht = 0.0;
88
89 if (pic_path == NULL)
90 goto done;
91
92 /* if cmd was \XeTeXpdffile, use xpdflib to read it */
93 if (pdfBoxType != 0) {
94 err = pdf_get_rect(pic_path, page, pdfBoxType, bounds);
95 goto done;
96 }
97
98 /* otherwise try graphics formats that we know */
99 fp = fopen(pic_path, FOPEN_RBIN_MODE);
100 if (fp == NULL)
101 goto done;
102
103 if (check_for_jpeg(fp)) {
104 struct JPEG_info info;
105 err = JPEG_scan_file(&info, fp);
106 if (err == 0) {
107 bounds->wd = (info.width * 72.27) / info.xdpi;
108 bounds->ht = (info.height * 72.27) / info.ydpi;
109 }
110 goto done;
111 }
112
113 if (check_for_bmp(fp)) {
114 struct bmp_info info;
115 err = bmp_scan_file(&info, fp);
116 if (err == 0) {
117 bounds->wd = (info.width * 72.27) / info.xdpi;
118 bounds->ht = (info.height * 72.27) / info.ydpi;
119 }
120 goto done;
121 }
122
123 if (check_for_png(fp)) {
124 struct png_info info;
125 err = png_scan_file(&info, fp);
126 if (err == 0) {
127 bounds->wd = (info.width * 72.27) / info.xdpi;
128 bounds->ht = (info.height * 72.27) / info.ydpi;
129 }
130 goto done;
131 }
132
133 /* could support other file types here (TIFF, WMF, etc?) */
134
135 done:
136 if (fp != NULL)
137 fclose(fp);
138
139 if (err == 0)
140 *path = pic_path;
141 else {
142 if (pic_path != NULL)
143 free(pic_path);
144 }
145
146 return err;
147 }
148