1
/* This file is part of Libspectre.
3
* Copyright (C) 2007 Albert Astals Cid <aacid@kde.org>
4
* Copyright (C) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
6
* Libspectre is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2, or (at your option)
11
* Libspectre is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26
#include <sys/types.h>
30
#include "spectre-document.h"
31
#include "spectre-private.h"
32
#include "spectre-exporter.h"
33
#include "spectre-utils.h"
35
struct SpectreDocument
45
spectre_document_new (void)
49
doc = calloc (1, sizeof (SpectreDocument));
54
spectre_document_load (SpectreDocument *document,
57
_spectre_return_if_fail (document != NULL);
58
_spectre_return_if_fail (filename != NULL);
60
if (document->doc && strcmp (filename, document->doc->filename) == 0) {
61
document->status = SPECTRE_STATUS_SUCCESS;
66
psdocdestroy (document->doc);
70
document->doc = psscan (filename, SCANSTYLE_NORMAL);
72
/* FIXME: OOM | INVALID_PS */
73
document->status = SPECTRE_STATUS_LOAD_ERROR;
77
document->structured = ((!document->doc->epsf && document->doc->numpages > 0) ||
78
(document->doc->epsf && document->doc->numpages > 1));
80
if (document->status != SPECTRE_STATUS_SUCCESS)
81
document->status = SPECTRE_STATUS_SUCCESS;
85
spectre_document_free (SpectreDocument *document)
91
psdocdestroy (document->doc);
99
spectre_document_status (SpectreDocument *document)
101
_spectre_return_val_if_fail (document != NULL, SPECTRE_STATUS_DOCUMENT_NOT_LOADED);
103
return document->status;
107
spectre_document_get_n_pages (SpectreDocument *document)
109
_spectre_return_val_if_fail (document != NULL, 0);
111
if (!document->doc) {
112
document->status = SPECTRE_STATUS_DOCUMENT_NOT_LOADED;
116
return document->structured ? document->doc->numpages : 1;
120
spectre_document_get_orientation (SpectreDocument *document)
124
_spectre_return_val_if_fail (document != NULL, SPECTRE_ORIENTATION_PORTRAIT);
126
if (!document->doc) {
127
document->status = SPECTRE_STATUS_DOCUMENT_NOT_LOADED;
128
return SPECTRE_ORIENTATION_PORTRAIT;
131
doc_orientation = document->doc->orientation != NONE ?
132
document->doc->orientation : document->doc->default_page_orientation;
134
switch (doc_orientation) {
137
return SPECTRE_ORIENTATION_PORTRAIT;
139
return SPECTRE_ORIENTATION_LANDSCAPE;
141
return SPECTRE_ORIENTATION_REVERSE_LANDSCAPE;
143
return SPECTRE_ORIENTATION_REVERSE_PORTRAIT;
148
spectre_document_get_title (SpectreDocument *document)
150
_spectre_return_val_if_fail (document != NULL, NULL);
152
if (!document->doc) {
153
document->status = SPECTRE_STATUS_DOCUMENT_NOT_LOADED;
157
return document->doc->title;
161
spectre_document_get_creator (SpectreDocument *document)
163
_spectre_return_val_if_fail (document != NULL, NULL);
165
if (!document->doc) {
166
document->status = SPECTRE_STATUS_DOCUMENT_NOT_LOADED;
170
return document->doc->creator;
174
spectre_document_get_for (SpectreDocument *document)
176
_spectre_return_val_if_fail (document != NULL, NULL);
178
if (!document->doc) {
179
document->status = SPECTRE_STATUS_DOCUMENT_NOT_LOADED;
183
return document->doc->fortext;
187
spectre_document_get_creation_date (SpectreDocument *document)
189
_spectre_return_val_if_fail (document != NULL, NULL);
191
if (!document->doc) {
192
document->status = SPECTRE_STATUS_DOCUMENT_NOT_LOADED;
196
return document->doc->date;
200
spectre_document_get_format (SpectreDocument *document)
202
_spectre_return_val_if_fail (document != NULL, NULL);
204
if (!document->doc) {
205
document->status = SPECTRE_STATUS_DOCUMENT_NOT_LOADED;
209
return document->doc->format;
213
spectre_document_is_eps (SpectreDocument *document)
215
_spectre_return_val_if_fail (document != NULL, FALSE);
217
if (!document->doc) {
218
document->status = SPECTRE_STATUS_DOCUMENT_NOT_LOADED;
222
return document->doc->epsf;
226
spectre_document_get_language_level (SpectreDocument *document)
228
_spectre_return_val_if_fail (document != NULL, 0);
230
if (!document->doc) {
231
document->status = SPECTRE_STATUS_DOCUMENT_NOT_LOADED;
235
return document->doc->languagelevel ? atoi (document->doc->languagelevel) : 0;
239
spectre_document_get_page (SpectreDocument *document,
240
unsigned int page_index)
244
_spectre_return_val_if_fail (document != NULL, NULL);
246
if (page_index >= spectre_document_get_n_pages (document)) {
247
document->status = SPECTRE_STATUS_INVALID_PAGE;
251
if (!document->doc) {
252
document->status = SPECTRE_STATUS_DOCUMENT_NOT_LOADED;
256
page = _spectre_page_new (page_index, document->doc);
258
document->status = SPECTRE_STATUS_NO_MEMORY;
262
if (document->status != SPECTRE_STATUS_SUCCESS)
263
document->status = SPECTRE_STATUS_SUCCESS;
269
spectre_document_get_page_by_label (SpectreDocument *document,
275
_spectre_return_val_if_fail (document != NULL, NULL);
278
document->status = SPECTRE_STATUS_INVALID_PAGE;
282
if (!document->doc) {
283
document->status = SPECTRE_STATUS_DOCUMENT_NOT_LOADED;
287
for (i = 0; i < document->doc->numpages; i++) {
288
if (strcmp (document->doc->pages[i].label, label) == 0) {
294
if (page_index == -1) {
295
document->status = SPECTRE_STATUS_INVALID_PAGE;
299
return spectre_document_get_page (document, page_index);
303
spectre_document_render_full (SpectreDocument *document,
304
SpectreRenderContext *rc,
305
unsigned char **page_data,
310
_spectre_return_if_fail (document != NULL);
311
_spectre_return_if_fail (rc != NULL);
313
if (!document->doc) {
314
document->status = SPECTRE_STATUS_DOCUMENT_NOT_LOADED;
318
page = spectre_document_get_page (document, 0);
319
if (!page || document->status != SPECTRE_STATUS_SUCCESS) {
320
spectre_page_free (page);
324
spectre_page_render (page, rc, page_data, row_length);
325
document->status = spectre_page_status (page);
327
spectre_page_free (page);
331
spectre_document_render (SpectreDocument *document,
332
unsigned char **page_data,
335
SpectreRenderContext *rc;
337
_spectre_return_if_fail (document != NULL);
339
rc = spectre_render_context_new ();
340
spectre_document_render_full (document, rc, page_data, row_length);
341
spectre_render_context_free (rc);
345
spectre_document_get_page_size (SpectreDocument *document,
352
_spectre_return_if_fail (document != NULL);
354
if (!document->doc) {
355
document->status = SPECTRE_STATUS_DOCUMENT_NOT_LOADED;
359
page = spectre_document_get_page (document, 0);
360
if (!page || document->status != SPECTRE_STATUS_SUCCESS) {
361
spectre_page_free (page);
365
spectre_page_get_size (page, &w, &h);
371
spectre_page_free (page);
375
spectre_document_save (SpectreDocument *document,
376
const char *filename)
378
struct stat stat_buf;
382
_spectre_return_if_fail (document != NULL);
383
_spectre_return_if_fail (filename != NULL);
385
if (!document->doc) {
386
document->status = SPECTRE_STATUS_DOCUMENT_NOT_LOADED;
390
if (stat (document->doc->filename, &stat_buf) != 0) {
391
document->status = SPECTRE_STATUS_SAVE_ERROR;
395
from = fopen (document->doc->filename, "r");
397
document->status = SPECTRE_STATUS_SAVE_ERROR;
401
to = fopen (filename, "w");
403
document->status = SPECTRE_STATUS_SAVE_ERROR;
408
pscopy (from, to, document->doc, 0, stat_buf.st_size - 1);
413
document->status = SPECTRE_STATUS_SUCCESS;
417
spectre_document_save_to_pdf (SpectreDocument *document,
418
const char *filename)
420
SpectreExporter *exporter;
421
SpectreStatus status;
424
_spectre_return_if_fail (document != NULL);
425
_spectre_return_if_fail (filename != NULL);
427
if (!document->doc) {
428
document->status = SPECTRE_STATUS_DOCUMENT_NOT_LOADED;
432
exporter = spectre_exporter_new (document, SPECTRE_EXPORTER_FORMAT_PDF);
434
document->status = SPECTRE_STATUS_NO_MEMORY;
438
status = spectre_exporter_begin (exporter, filename);
440
document->status = status == SPECTRE_STATUS_NO_MEMORY ?
441
SPECTRE_STATUS_NO_MEMORY : SPECTRE_STATUS_SAVE_ERROR;
442
spectre_exporter_free (exporter);
446
for (i = 0; i < spectre_document_get_n_pages (document); i++) {
447
status = spectre_exporter_do_page (exporter, i);
453
document->status = status == SPECTRE_STATUS_NO_MEMORY ?
454
SPECTRE_STATUS_NO_MEMORY : SPECTRE_STATUS_SAVE_ERROR;
455
spectre_exporter_free (exporter);
459
status = spectre_exporter_end (exporter);
460
spectre_exporter_free (exporter);
463
document->status = status == SPECTRE_STATUS_NO_MEMORY ?
464
SPECTRE_STATUS_NO_MEMORY : SPECTRE_STATUS_SAVE_ERROR;
466
document->status = SPECTRE_STATUS_SUCCESS;
471
_spectre_document_get_doc (SpectreDocument *document)
473
return document->doc;