summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/xetexdir/pdfimage.cpp
blob: 21af77c657244da48201be6d373d67dd64d64274 (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
/****************************************************************************\
 Part of the XeTeX typesetting system
 copyright (c) 2006 by SIL International
 written by Jonathan Kew

 This software is distributed under the terms of the Common Public License,
 version 1.0.
 For details, see <http://www.opensource.org/licenses/cpl1.0.php> or the file
 cpl1.0.txt included with the software.
\****************************************************************************/

#include "pdfimage.h"

#include "PDFDoc.h"
#include "Catalog.h"
#include "Page.h"
#include "GString.h"

#include "XeTeX_ext.h"

/* use our own fmin function because it seems to be missing on certain platforms */
inline double
my_fmin(double x, double y)
{
	return (x < y) ? x : y;
}

int
pdf_get_rect(char* filename, int page_num, int pdf_box, realrect* box)
{
	GString*	name = new GString(filename);
	PDFDoc*		doc = new PDFDoc(name);
	
	if (!doc) {
		delete name;
		return -1;
	}
	
	/* if the doc got created, it now owns name, so we mustn't delete it! */
	
	if (!doc->isOk()) {
		delete doc;
		return -1;
	}
	
	int			pages = doc->getNumPages();
	if (page_num > pages)
		page_num = pages;
	if (page_num < 0)
		page_num = pages + 1 + page_num;
	if (page_num < 1)
		page_num = 1;

	Page*		page = doc->getCatalog()->getPage(page_num);

	PDFRectangle*	r;
	switch (pdf_box) {
		default:
		case pdfbox_crop:
			r = page->getCropBox();
			break;
		case pdfbox_media:
			r = page->getMediaBox();
			break;
		case pdfbox_bleed:
			r = page->getBleedBox();
			break;
		case pdfbox_trim:
			r = page->getTrimBox();
			break;
		case pdfbox_art:
			r = page->getArtBox();
			break;
	}

	box->x  = 72 / 72.27 * my_fmin(r->x1, r->x2);
	box->y  = 72 / 72.27 * my_fmin(r->y1, r->y2);
	box->wd = 72 / 72.27 * fabs(r->x2 - r->x1);
	box->ht = 72 / 72.27 * fabs(r->y2 - r->y1);

	delete doc;

	return 0;
}