summaryrefslogtreecommitdiff
path: root/libspectre/spectre-page.c
blob: 74267ba0c4bb71cbf23142147fbb66a433fa004c (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* This file is part of Libspectre.
 * 
 * Copyright (C) 2007 Albert Astals Cid <aacid@kde.org>
 * Copyright (C) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
 *
 * Libspectre is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * Libspectre is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <stdio.h>
#include <stdlib.h>

#include "spectre-page.h"

#include "spectre-device.h"
#include "spectre-private.h"
#include "spectre-utils.h"

struct SpectrePage
{
	struct document *doc;
	
	SpectreStatus    status;

	unsigned int     index;
	int              width;
	int              height;
};

SpectrePage *
_spectre_page_new (unsigned int     page_index,
		   struct document *doc)
{
	SpectrePage *page;

	page = calloc (1, sizeof (SpectrePage));
	if (!page)
		return NULL;

	page->index = page_index;
	page->width = -1;
	page->height = -1;
	page->doc = psdocreference (doc);

	return page;
}

void
spectre_page_free (SpectrePage *page)
{
	if (!page)
		return;

	if (page->doc) {
		psdocdestroy (page->doc);
		page->doc = NULL;
	}
	
	free (page);
}

SpectreStatus
spectre_page_status (SpectrePage *page)
{
	_spectre_return_val_if_fail (page != NULL, SPECTRE_STATUS_INVALID_PAGE);
	
	return page->status;
}

unsigned int
spectre_page_get_index (SpectrePage *page)
{
	_spectre_return_val_if_fail (page != NULL, 0);
	
	return page->index;
}

const char *
spectre_page_get_label (SpectrePage *page)
{
	_spectre_return_val_if_fail (page != NULL, NULL);
	
	return page->doc->numpages > 0 ? page->doc->pages[page->index].label : NULL;
}

SpectreOrientation
spectre_page_get_orientation (SpectrePage *page)
{
	int page_orientation = NONE;

	_spectre_return_val_if_fail (page != NULL, SPECTRE_ORIENTATION_PORTRAIT);

	if (page->doc->numpages > 0) {
		page_orientation = page->doc->pages[page->index].orientation != NONE ?
			page->doc->pages[page->index].orientation :
			page->doc->default_page_orientation;
	}

	if (page_orientation == NONE)
		page_orientation = page->doc->orientation;

	switch (page_orientation) {
	default:
	case PORTRAIT:
		return SPECTRE_ORIENTATION_PORTRAIT;
	case LANDSCAPE:
		return SPECTRE_ORIENTATION_LANDSCAPE;
	case SEASCAPE:
		return SPECTRE_ORIENTATION_REVERSE_LANDSCAPE;
	case UPSIDEDOWN:
		return SPECTRE_ORIENTATION_REVERSE_PORTRAIT;
	}
}

void
spectre_page_get_size (SpectrePage *page,
		       int         *width,
		       int         *height)
{
	_spectre_return_if_fail (page != NULL);

	if (page->width == -1 || page->height == -1) {
		int urx, ury, llx, lly;
	
		psgetpagebox (page->doc, page->index,
			      &urx, &ury, &llx, &lly);

		page->width = urx - llx;
		page->height = ury - lly;
	}

	if (width)
		*width = page->width;
	if (height)
		*height = page->height;
}

void
spectre_page_render (SpectrePage          *page,
		     SpectreRenderContext *rc,
		     unsigned char       **page_data,
		     int                  *row_length)
{
	SpectreDevice *device;
	int            width, height;

	_spectre_return_if_fail (page != NULL);
	_spectre_return_if_fail (rc != NULL);

	spectre_page_get_size (page, &width, &height);

	device = spectre_device_new (page->doc);
	page->status = spectre_device_render (device, page->index, rc,
					      0, 0, width, height,
					      page_data, row_length);
	spectre_device_free (device);
}

void
spectre_page_render_slice (SpectrePage          *page,
			   SpectreRenderContext *rc,
			   int                   x,
			   int                   y,
			   int                   width,
			   int                   height,
			   unsigned char       **page_data,
			   int                  *row_length)
{
	SpectreDevice *device;
	int            page_height;

	_spectre_return_if_fail (page != NULL);
	_spectre_return_if_fail (rc != NULL);

	spectre_page_get_size (page, NULL, &page_height);

	device = spectre_device_new (page->doc);
	page->status = spectre_device_render (device, page->index, rc,
					      x, page_height - (y + height),
					      width, height,
					      page_data, row_length);
	spectre_device_free (device);
}