summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/xetexdir/pdfimage.cpp
blob: c76f2372ca127474f62ab6bb43080ea8d2d5cd01 (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
/****************************************************************************\
 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"

#ifdef POPPLER_VERSION
#include <dirent.h>
#include <poppler-config.h>
#include <goo/GooString.h>
#include <goo/gmem.h>
#include <goo/gfile.h>
#define GString GooString
#else
#include "goo/GString.h"
#endif

#include "PDFDoc.h"
#include "Catalog.h"
#include "Page.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)
	/* return the box converted to TeX points */
{
	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.27 / 72 * my_fmin(r->x1, r->x2);
	box->y  = 72.27 / 72 * my_fmin(r->y1, r->y2);
	box->wd = 72.27 / 72 * fabs(r->x2 - r->x1);
	box->ht = 72.27 / 72 * fabs(r->y2 - r->y1);

	delete doc;

	return 0;
}

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

	delete doc;

	return pages;
}