~ubuntu-branches/ubuntu/precise/ipe/precise

« back to all changes in this revision

Viewing changes to src/tools/pdftoipe/pdftoipe.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve M. Robbins
  • Date: 2004-06-08 00:44:02 UTC
  • Revision ID: james.westby@ubuntu.com-20040608004402-72yu51xlh7vt6p9m
Tags: upstream-6.0pre16
ImportĀ upstreamĀ versionĀ 6.0pre16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// --------------------------------------------------------------------
 
2
// Pdftoipe: convert PDF file to editable Ipe XML file
 
3
// --------------------------------------------------------------------
 
4
/*
 
5
 
 
6
    This file is part of the extensible drawing editor Ipe.
 
7
    Copyright (C) 1993-2004  Otfried Cheong
 
8
 
 
9
    Ipe is free software; you can redistribute it and/or modify it
 
10
    under the terms of the GNU General Public License as published by
 
11
    the Free Software Foundation; either version 2 of the License, or
 
12
    (at your option) any later version.
 
13
 
 
14
    As a special exception, you have permission to link Ipe with the
 
15
    CGAL library and distribute executables, as long as you follow the
 
16
    requirements of the Gnu General Public License in regard to all of
 
17
    the software in the executable aside from CGAL.
 
18
 
 
19
    Ipe is distributed in the hope that it will be useful, but WITHOUT
 
20
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
21
    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
 
22
    License for more details.
 
23
 
 
24
    You should have received a copy of the GNU General Public License
 
25
    along with Ipe; if not, you can find it at
 
26
    "http://www.gnu.org/copyleft/gpl.html", or write to the Free
 
27
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
28
 
 
29
*/
 
30
 
 
31
#include <stdio.h>
 
32
#include "ocfile.h"
 
33
#include <stdlib.h>
 
34
#include <stddef.h>
 
35
#include <string.h>
 
36
#include "parseargs.h"
 
37
#include "gstring.h"
 
38
#include "gmem.h"
 
39
#include "globalparams.h"
 
40
#include "object.h"
 
41
#include "stream.h"
 
42
#include "array.h"
 
43
#include "dict.h"
 
44
#include "xref.h"
 
45
#include "catalog.h"
 
46
#include "page.h"
 
47
#include "pdfdoc.h"
 
48
#include "xmloutputdev.h"
 
49
#include "error.h"
 
50
 
 
51
static int firstPage = 1;
 
52
static int lastPage = 0;
 
53
static char ownerPassword[33] = "";
 
54
static char userPassword[33] = "";
 
55
static GBool quiet = gFalse;
 
56
static GBool printHelp = gFalse;
 
57
static GBool math = gFalse;
 
58
 
 
59
static ArgDesc argDesc[] = {
 
60
  {"-f",      argInt,      &firstPage,      0,
 
61
   "first page to convert"},
 
62
  {"-l",      argInt,      &lastPage,       0,
 
63
   "last page to convert"},
 
64
  {"-opw",    argString,   ownerPassword,   sizeof(ownerPassword),
 
65
   "owner password (for encrypted files)"},
 
66
  {"-upw",    argString,   userPassword,    sizeof(userPassword),
 
67
   "user password (for encrypted files)"},
 
68
  {"-q",      argFlag,     &quiet,          0,
 
69
   "don't print any messages or errors"},
 
70
  {"-math",   argFlag,     &math,           0,
 
71
   "turn all text objects into math formulas"},
 
72
  {"-h",      argFlag,     &printHelp,      0,
 
73
   "print usage information"},
 
74
  {"-help",   argFlag,     &printHelp,      0,
 
75
   "print usage information"},
 
76
  {"--help",  argFlag,     &printHelp,      0,
 
77
   "print usage information"},
 
78
  {"-?",      argFlag,     &printHelp,      0,
 
79
   "print usage information"},
 
80
  {NULL, argFlag, 0, 0, 0}
 
81
};
 
82
 
 
83
int main(int argc, char *argv[])
 
84
{
 
85
  // parse args
 
86
  GBool ok = parseArgs(argDesc, &argc, argv);
 
87
  if (!ok || argc < 2 || argc > 3 || printHelp) {
 
88
    fprintf(stderr, "pdftoipe version %s\n", PDFTOIPE_VERSION);
 
89
    printUsage("pdftoipe", "<PDF-file> [<XML-file>]", argDesc);
 
90
    return 1;
 
91
  }
 
92
 
 
93
  GString *fileName = new GString(argv[1]);
 
94
 
 
95
  // read config file
 
96
  globalParams = new GlobalParams(0);
 
97
  if (quiet) {
 
98
    globalParams->setErrQuiet(quiet);
 
99
  }
 
100
 
 
101
  GString *ownerPW, *userPW;
 
102
  if (ownerPassword[0]) {
 
103
    ownerPW = new GString(ownerPassword);
 
104
  } else {
 
105
    ownerPW = 0;
 
106
  }
 
107
  if (userPassword[0]) {
 
108
    userPW = new GString(userPassword);
 
109
  } else {
 
110
    userPW = 0;
 
111
  }
 
112
 
 
113
  // open PDF file
 
114
  PDFDoc *doc = new PDFDoc(fileName, ownerPW, userPW);
 
115
  delete userPW;
 
116
  delete ownerPW;
 
117
 
 
118
  if (!doc->isOk())
 
119
    return 1;
 
120
 
 
121
  // check for print permission
 
122
  if (!doc->okToPrint()) {
 
123
    error(-1, "Printing this document is not allowed.");
 
124
    return 3;
 
125
  }
 
126
 
 
127
  // construct XML file name
 
128
  GString *xmlFileName;
 
129
  if (argc == 3) {
 
130
    xmlFileName = new GString(argv[2]);
 
131
  } else {
 
132
    char *p = fileName->getCString() + fileName->getLength() - 4;
 
133
    if (!strcmp(p, ".pdf") || !strcmp(p, ".PDF")) {
 
134
      xmlFileName = new GString(fileName->getCString(),
 
135
                                fileName->getLength() - 4);
 
136
    } else {
 
137
      xmlFileName = fileName->copy();
 
138
    }
 
139
    xmlFileName->append(".xml");
 
140
  }
 
141
 
 
142
  // get page range
 
143
  if (firstPage < 1) {
 
144
    firstPage = 1;
 
145
  }
 
146
  if (lastPage < 1 || lastPage > doc->getNumPages()) {
 
147
    lastPage = doc->getNumPages();
 
148
  }
 
149
 
 
150
  // write XML file
 
151
  XmlOutputDev *xmlOut =
 
152
    new XmlOutputDev(xmlFileName->getCString(), doc->getXRef(),
 
153
                     doc->getCatalog(), firstPage, lastPage,
 
154
                     math);
 
155
  int exitCode = 2;
 
156
  if (xmlOut->isOk()) {
 
157
    doc->displayPages(xmlOut, firstPage, lastPage, 72, 0, gFalse);
 
158
    exitCode = 0;
 
159
  }
 
160
 
 
161
  // clean up
 
162
  delete xmlOut;
 
163
  delete xmlFileName;
 
164
  delete doc;
 
165
  delete globalParams;
 
166
 
 
167
  // check for memory leaks
 
168
  Object::memCheck(stderr);
 
169
  gMemReport(stderr);
 
170
 
 
171
  return exitCode;
 
172
}
 
173
 
 
174
// --------------------------------------------------------------------