~ubuntu-branches/ubuntu/quantal/cups-filters/quantal-proposed

« back to all changes in this revision

Viewing changes to filter/pdftopdf/P2POutput.cxx

  • Committer: Package Import Robot
  • Author(s): Till Kamppeter
  • Date: 2012-08-20 14:53:42 UTC
  • mfrom: (2.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20120820145342-bddzpwqv0klmt84d
Tags: 1.0.22-1
* New upstream release
   - pdftopdf filter replaced by new QPDF-based filter from Tobias
     Hoffmann's Google Summer of Code project. The former Poppler-based
     pdftopdf duplicated a lot of Poppler's code. The old filter is
     still in the package as pdftopdf.old with source code in
     filter/pdftopdf.old. It will be removed in a later release.
   - bannertopdf: Page duplication routine fixed.
   - bannertopdf: Fixed invalid output of a direct stream object.
   - Added most recent contributors to AUTHORS and COPYING files.
* debian/control: Added build dependency on libqpdf-dev.
* debian/copyright: Updated for the addition of the new pdftopdf filter.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 
3
 
Copyright (c) 2006-2007, BBR Inc.  All rights reserved.
4
 
 
5
 
Permission is hereby granted, free of charge, to any person obtaining
6
 
a copy of this software and associated documentation files (the
7
 
"Software"), to deal in the Software without restriction, including
8
 
without limitation the rights to use, copy, modify, merge, publish,
9
 
distribute, sublicense, and/or sell copies of the Software, and to
10
 
permit persons to whom the Software is furnished to do so, subject to
11
 
the following conditions:
12
 
 
13
 
The above copyright notice and this permission notice shall be included
14
 
in all copies or substantial portions of the Software.
15
 
 
16
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
 
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
 
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
 
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
 
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
 
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
 
 
24
 
*/
25
 
/*
26
 
 P2POutput.cc
27
 
 pdftopdf output pdf routines
28
 
*/
29
 
 
30
 
#include <config.h>
31
 
#include <string.h>
32
 
#include "goo/gmem.h"
33
 
#include "UGooString.h"
34
 
#include "P2POutput.h"
35
 
#include "P2PObject.h"
36
 
#include <ctype.h>
37
 
#include "P2PError.h"
38
 
 
39
 
void P2POutput::outputDict(Dict *dict, const char **keys,
40
 
  P2PObject **objs, int len, P2POutputStream *str, XRef *xref)
41
 
{
42
 
  int i;
43
 
  int n = dict->getLength();
44
 
  int j;
45
 
 
46
 
  str->puts("<<");
47
 
  for (i = 0;i < n;i++) {
48
 
#ifdef HAVE_UGOOSTRING_H
49
 
    char *key = dict->getKey(i)->getCString();
50
 
#else
51
 
    char *key = dict->getKey(i);
52
 
#endif
53
 
    Object obj;
54
 
 
55
 
    for (j = 0;j < len;j++) {
56
 
      if (strcmp(keys[j],key) == 0) {
57
 
        goto next_key;
58
 
      }
59
 
    }
60
 
    str->putchar('\n');
61
 
    outputName(key,str);
62
 
    str->putchar(' ');
63
 
    dict->getValNF(i,&obj);
64
 
    outputObject(&obj,str,xref);
65
 
    obj.free();
66
 
next_key:;
67
 
#ifdef HAVE_UGOOSTRING_H
68
 
    delete[] key;
69
 
#endif
70
 
  }
71
 
  for (j = 0;j < len;j++) {
72
 
    int num, gen;
73
 
 
74
 
    str->putchar('\n');
75
 
    outputName(keys[j],str);
76
 
    str->putchar(' ');
77
 
    objs[j]->getNum(&num,&gen);
78
 
    if (num > 0) {
79
 
      objs[j]->outputRef(str);
80
 
    } else {
81
 
      objs[j]->output(str,xref);
82
 
    }
83
 
  }
84
 
  str->puts("\n>>");
85
 
}
86
 
 
87
 
void P2POutput::outputDict(Dict *dict, P2POutputStream *str, XRef *xref)
88
 
{
89
 
  int i;
90
 
  int n = dict->getLength();
91
 
 
92
 
  str->puts("<< ");
93
 
  for (i = 0;i < n;i++) {
94
 
#ifdef HAVE_UGOOSTRING_H
95
 
    char *key = dict->getKey(i)->getCString();
96
 
#else
97
 
    char *key = dict->getKey(i);
98
 
#endif
99
 
    Object obj;
100
 
 
101
 
    outputName(key,str);
102
 
    str->putchar(' ');
103
 
    dict->getValNF(i,&obj);
104
 
    outputObject(&obj,str,xref);
105
 
    obj.free();
106
 
    str->putchar('\n');
107
 
#ifdef HAVE_UGOOSTRING_H
108
 
    delete[] key;
109
 
#endif
110
 
  }
111
 
  str->puts(">>");
112
 
}
113
 
 
114
 
void P2POutput::outputStream(Stream *stream, P2POutputStream *str, XRef *xref)
115
 
{
116
 
  Dict *dict;
117
 
  int len;
118
 
  int c;
119
 
 
120
 
#ifdef HAVE_GETUNDECODEDSTREAM
121
 
  stream = stream->getUndecodedStream();
122
 
#else
123
 
  stream = stream->getBaseStream();
124
 
#endif
125
 
  dict = stream->getDict();
126
 
  /* output dictionary part */
127
 
  outputDict(dict,str,xref);
128
 
  str->puts("\nstream\n");
129
 
  stream->reset();
130
 
  for (len = 0;(c = stream->getChar()) != EOF;len++) {
131
 
    str->putchar(c);
132
 
  }
133
 
  str->puts("endstream");
134
 
}
135
 
 
136
 
void P2POutput::outputRef(Ref *ref, P2POutputStream *str, XRef *xref)
137
 
{
138
 
  P2PObject *p2pobj;
139
 
  Object obj;
140
 
 
141
 
  if ((p2pobj = P2PObject::find(ref->num,ref->gen)) == 0) {
142
 
    obj.initRef(ref->num, ref->gen);
143
 
    p2pobj = new P2PObj(&obj, ref->num, ref->gen);
144
 
    obj.free();
145
 
  }
146
 
  P2PXRef::put(p2pobj);
147
 
  p2pobj->outputRef(str);
148
 
}
149
 
 
150
 
void P2POutput::outputArray(Array *array, P2POutputStream *str, XRef *xref,
151
 
  Dict *mapDict)
152
 
{
153
 
  int i;
154
 
  int n = array->getLength();
155
 
 
156
 
  str->puts("[ ");
157
 
  for (i = 0;i < n;i++) {
158
 
    Object obj;
159
 
 
160
 
    array->getNF(i,&obj);
161
 
    outputObject(&obj,str,xref,mapDict);
162
 
    obj.free();
163
 
    str->putchar('\n');
164
 
  }
165
 
  str->puts("]");
166
 
}
167
 
 
168
 
void P2POutput::outputName(const char *name, P2POutputStream *str, Dict *mapDict)
169
 
{
170
 
  const char *p;
171
 
#ifdef HAVE_UGOOSTRING_H
172
 
  UGooString nameStr(name);
173
 
#else
174
 
  char *nameStr = const_cast<char *>(name);
175
 
#endif
176
 
  Object obj;
177
 
  static const char *punctures = "()<>[]{}/%#";
178
 
 
179
 
  if (mapDict != 0 && mapDict->lookupNF(nameStr,&obj) != 0) {
180
 
    if (obj.isName()) {
181
 
      /* map name */
182
 
      name = obj.getName();
183
 
    }
184
 
  }
185
 
  str->putchar('/');
186
 
  for (p = name;*p != '\0';p++) {
187
 
    if (*p >= 33 && *p <= 126 && strchr(punctures,*p) == 0) {
188
 
      str->putchar(*p);
189
 
    } else {
190
 
      str->printf("#%02x",*p & 0xff);
191
 
    }
192
 
  }
193
 
  obj.free();
194
 
}
195
 
 
196
 
void P2POutput::outputString(const char *s, int len, P2POutputStream *str)
197
 
{
198
 
  const char *p = s;
199
 
  int i;
200
 
 
201
 
  str->putchar('(');
202
 
  for (i = 0;i < len;i++,p++) {
203
 
    switch (*p) {
204
 
    case '\n':
205
 
      str->puts("\\n");
206
 
      break;
207
 
    case '\r':
208
 
      str->puts("\\r");
209
 
      break;
210
 
    case '\t':
211
 
      str->puts("\\t");
212
 
      break;
213
 
    case '\b':
214
 
      str->puts("\\b");
215
 
      break;
216
 
    case '\f':
217
 
      str->puts("\\f");
218
 
      break;
219
 
    case '(':
220
 
      str->puts("\\(");
221
 
      break;
222
 
    case ')':
223
 
      str->puts("\\)");
224
 
      break;
225
 
    case '\\':
226
 
      str->puts("\\\\");
227
 
      break;
228
 
    default:
229
 
      if (isprint(*p)) {
230
 
        str->putchar(*p);
231
 
      } else {
232
 
        str->printf("\\%03o",*p & 0xff);
233
 
      }
234
 
      break;
235
 
    }
236
 
  }
237
 
  str->putchar(')');
238
 
}
239
 
 
240
 
void P2POutput::outputObject(Object *obj, P2POutputStream *str, XRef *xref,
241
 
  Dict *mapDict)
242
 
{
243
 
  switch (obj->getType()) {
244
 
  case objBool:
245
 
    if (obj->getBool()) {
246
 
      str->puts("true");
247
 
    } else {
248
 
      str->puts("false");
249
 
    }
250
 
    break;
251
 
  case objInt:
252
 
    str->printf("%d",obj->getInt());
253
 
    break;
254
 
  case objUint:
255
 
    str->printf("%u",obj->getUint());
256
 
    break;
257
 
  case objReal:
258
 
    str->printf("%f",obj->getReal());
259
 
    break;
260
 
  case objString:
261
 
    outputString(obj->getString()->getCString(),
262
 
      obj->getString()->getLength(), str);
263
 
    break;
264
 
  case objName:
265
 
    outputName(obj->getName(),str,mapDict);
266
 
    break;
267
 
  case objNull:
268
 
    str->puts("null");
269
 
    break;
270
 
  case objArray:
271
 
    outputArray(obj->getArray(),str,xref,mapDict);
272
 
    break;
273
 
  case objDict:
274
 
    outputDict(obj->getDict(),str,xref);
275
 
    break;
276
 
  case objStream:
277
 
    outputStream(obj->getStream(),str,xref);
278
 
    break;
279
 
  case objRef:
280
 
    {
281
 
      Ref ref = obj->getRef();
282
 
 
283
 
      outputRef(&ref,str,xref);
284
 
    }
285
 
    break;
286
 
  case objCmd:
287
 
    str->puts(obj->getCmd());
288
 
    break;
289
 
  case objError:
290
 
    str->puts("<error>");
291
 
    break;
292
 
  case objEOF:
293
 
    str->puts("<EOF>");
294
 
    break;
295
 
  case objNone:
296
 
    str->puts("<none>");
297
 
    break;
298
 
  }
299
 
}