summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/xetexdir/XeTeX_pic.c
blob: ff4266be2804d4d700daeda8f476c2a1a1e3c8d6 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/****************************************************************************\
 Part of the XeTeX typesetting system
 Copyright (c) 1994-2008 by SIL International
 Copyright (c) 2009 by Jonathan Kew

 SIL Author(s): Jonathan Kew

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the copyright holders
shall not be used in advertising or otherwise to promote the sale,
use or other dealings in this Software without prior written
authorization from the copyright holders.
\****************************************************************************/

/*
XeTeX_pic.c
   interface between xetex and graphics files
   (not used on OS X -- we use ImageIO graphics importers instead)
   only needs to get image dimensions, not actually load/process the file
*/

#define EXTERN extern
#include "xetexd.h"

#include "XeTeX_ext.h"

#include <kpathsea/c-ctype.h>
#include <kpathsea/line.h>
#include <kpathsea/readable.h>
#include <kpathsea/variable.h>
#include <kpathsea/absolute.h>

#include "pdfimage.h"
#include "image/pngimage.h"
#include "image/jpegimage.h"
#include "image/bmpimage.h"


int
countpdffilepages()
{
	int	rval = 0;

    char*		pic_path = kpse_find_file((char*)nameoffile + 1, kpse_pict_format, 1);
	if (pic_path) {
		rval = pdf_count_pages(pic_path);
		free(pic_path);
	}

	return rval;
}


/*
	locate picture file from /nameoffile+1/ using kpathsearch
	pdfBoxType indicates which pdf bounding box to use (0 for \XeTeXpicfile)
	page indicates which page is wanted (0-based)
	return 0 for success, or non-zero error code for failure
	return full path in *path
	return bounds (tex points) in *bounds
*/
int
find_pic_file(char** path, realrect* bounds, int pdfBoxType, int page)
{
	int		err = -1;
	FILE*	fp = NULL;
    char*	pic_path = kpse_find_file((char*)nameoffile + 1, kpse_pict_format, 1);

	*path = NULL;
	bounds->x = bounds->y = bounds->wd = bounds->ht = 0.0;

	if (pic_path == NULL)
		goto done;

	/* if cmd was \XeTeXpdffile, use xpdflib to read it */
	if (pdfBoxType != 0) {
		err = pdf_get_rect(pic_path, page, pdfBoxType, bounds);
		goto done;
	}

	/* otherwise try graphics formats that we know */
	fp = fopen(pic_path, FOPEN_RBIN_MODE);
	if (fp == NULL)
		goto done;

	if (check_for_jpeg(fp)) {
		struct JPEG_info	info;
		err = JPEG_scan_file(&info, fp);
		if (err == 0) {
			bounds->wd = (info.width * 72.27) / info.xdpi;
			bounds->ht = (info.height * 72.27) / info.ydpi;
		}
		goto done;
	}
	
	if (check_for_bmp(fp)) {
		struct bmp_info	info;
		err = bmp_scan_file(&info, fp);
		if (err == 0) {
			bounds->wd = (info.width * 72.27) / info.xdpi;
			bounds->ht = (info.height * 72.27) / info.ydpi;
		}
		goto done;
	}
	
	if (check_for_png(fp)) {
		struct png_info	info;
		err = png_scan_file(&info, fp);
		if (err == 0) {
			bounds->wd = (info.width * 72.27) / info.xdpi;
			bounds->ht = (info.height * 72.27) / info.ydpi;
		}
		goto done;
	}	

	/* could support other file types here (TIFF, WMF, etc?) */
	
done:
	if (fp != NULL)
		fclose(fp);

	if (err == 0)
		*path = pic_path;
	else {
		if (pic_path != NULL)
			free(pic_path);
	}
	
	return err;
}