File: update_catalog.cc

package info (click to toggle)
derivations 0.56.20180123.1-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,388 kB
  • sloc: cpp: 1,633; perl: 692; makefile: 158; sh: 153
file content (265 lines) | stat: -rw-r--r-- 7,121 bytes parent folder | download
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

#include "update_catalog.h"
#include <utility>
#include <set>
#include <string>
#include <sstream>
#include <iomanip>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include "../TOC/def.h"
#include "PDF.h"
#include "PDF_rep.h"

using std::move;
using std::string;
typedef std::set<string> set;
const int magic = 0x9f05; // deprecated

// Beware that Libpoppler's Dict::add() method does not copy the key-string you
// feed it.  In an earlier Libpoppler, didn't it used to?  Maybe not; I can't
// clearly remember.  In any case, letting the key-string go out of scope now
// invalidates the Dict.  ---THB, March 2010---
//
// Well, Poppler's hardly documented private API has changed again.
//
// I do not critcize, of course.  On the contrary, one appreciates Poppler's
// continued development, nor would now (nor maybe ever) seem a good time for
// Poppler's developers to document the private API in question.  Premature
// stabilization of an API can cause much trouble, after all.  If and when the
// time comes to stabilize and to document, they'll know.  Meanwhile, for me at
// least, access to Poppler's source suffices, yet the observation that little
// private-API documentation exists remains an observation of fact as far
// as I know.  The fact, or at any rate the observation, incidentally affects
// the present program as follows.
//
// I am unlikely to relearn Poppler's latest pattern of memory allocation with
// each version-0.* Poppler release.  Maybe when 1.0 arrives?  Maybe never.
// Meanwhile, I am letting the program leak some memory.  For a program of this
// kind, whose execution time is short and resource needs are modest, such
// leaking probably has no practical effect, for of course the kernel
// automatically reclaims all leaked memory as soon as the program exits.
// Still, on principle, leaking is not neat, is it?
//
// Where you see below a construct like
//
//     Dict &dict = *( new Dict( static_cast<XRef*>(0) ) );
//
// the construct's purpose is to delay or prevent a destructor of Poppler's.
// As far as I know, this is wrong, or is at any rate a workaround, but insofar
// as I am unlikely to relearn Poppler's latest pattern of memory allocation
// with each version-0.* Poppler release, especially while Poppler's
// private API (understandably) remains so little documented, why, such a
// construct does what the present program needs it
// to do.  ---THB, Jan. 2019---
//
//

namespace {

  // This function copies key-value pairs from source to destination
  // Poppler Dicts, excepting pairs with the specified keys.
  // It expects the caller to have preinitialized the destination
  // as an empty Dict.
  int copy_but(
    Dict *const dest,
    Dict *const src,
    const set &keys
  ) {
    const int size = src->getLength();
    for ( int i = 0; i < size; ++i ) {
      const char *const key = src->getKey(i);
      if ( keys.count(key) ) continue;
      dest->add( key, Object( src->getValNF( i ) ) );
    }
    return size;
  }

}

string PDF::update_catalog(
  PDF &pdf,
  const Page_no::PS_page_numbering &nog
) {

  PDF_rep *const rep = pdf.get_PDF_rep(magic);

  // To understand this code, refer to the Libpoppler headers and
  // to Adobe's PDF Reference 1.7, sect. 8.3.1.

  Dict &catalog = *( new Dict ( static_cast<XRef*>(0) ) );
  {
    set keys;
    { char s[] = "PageLabels"; keys.insert(s); }
    { char s[] = "Outlines"  ; keys.insert(s); }
    copy_but( &catalog, rep->catalog, keys );
  }

  Dict &dict_Roman = *( new Dict( static_cast<XRef*>(0) ) );
  dict_Roman.add( "S", Object( objName, "r" ) );

  Dict &dict_Arabic = *( new Dict( ( static_cast<XRef*>(0) ) ) );
  dict_Arabic.add( "S", Object( objName, "D" ) );

  Array &array = *( new Array( static_cast<XRef*>(0) ) );
  array.add( Object( 0                          ) );
  array.add( Object( &dict_Roman                ) );
  array.add( Object( nog.count_prefatory_page() ) );
  array.add( Object( &dict_Arabic               ) );

  Dict &dict = *( new Dict( static_cast<XRef*>(0) ) );
  dict.add( "Nums", Object( &array ) );

  Object ref( n_obj(pdf), 0 );

  catalog.add( "PageLabels", Object(&dict) );
  catalog.add( "Outlines"  , move(ref)     );

  string res;
  {
    Iref iref = iref_catalog(pdf);
    std::ostringstream oss;
    oss
      << std::setw(TOC::width_i_obj) << iref.i
      << " " << iref.gen << " obj\n";
    res += oss.str();
  }

  // Do print() to a string rather than to stdout or a file.
  Object catalog_obj( &catalog );
  {
    int fd[2];
    pipe(fd);
    {
      FILE *q = fdopen( fd[1], "w" );
      catalog_obj.print(q);
      fclose(q);
    }
    {
      FILE *q = fdopen( fd[0], "r" );
      int c;
      while ( (c=fgetc(q)) != EOF ) res += c;
      fclose(q);
    }
  }

  res += "\nendobj\n";

  return res;

}

string PDF::add_title_to_info(
  PDF &pdf,
  const string &title
) {

  PDF_rep *const rep = pdf.get_PDF_rep(magic);

  Dict &info = *( new Dict( static_cast<XRef*>(0) ) );
  {
    set keys;
    copy_but( &info, rep->info, keys );
  }

  GooString &gs = *( new GooString );
  const char s_Title[] = "Title";
  {
    Object &obj_old = *( new Object( info.lookup( s_Title, 0 ) ) );
    if ( obj_old.isNull() ) {
      gs.Set( title.c_str() );
      info.add( s_Title, Object( &gs ) );
    }
  }

  string res;
  {
    const Iref iref = iref_info(pdf);
    std::ostringstream oss;
    oss
      << std::setw(TOC::width_i_obj) << iref.i
      << " " << iref.gen << " obj\n";
    res += oss.str();
  }

  // Do print() to a string rather than to stdout or a file.
  Object info_obj( &info );
  {
    int fd[2];
    pipe(fd);
    {
      FILE *q = fdopen( fd[1], "w" );
      info_obj.print(q);
      fclose(q);
    }
    {
      FILE *q = fdopen( fd[0], "r" );
      int c;
      while ( (c=fgetc(q)) != EOF ) res += c;
      fclose(q);
    }
  }

  res += "\nendobj\n";

  return res;

}

string PDF::update_trailer(
  PDF &pdf,
  const int n_pdf_obj,
  const int offset_xref,
  const std::string &pdf_filename
) {

  PDF_rep *const rep = pdf.get_PDF_rep(magic);

  // To understand this code, refer to the Libpoppler headers and
  // to Adobe's PDF Reference 1.7, sect. 3.4.4.

  Dict &new_trailer = *( new Dict( static_cast<XRef*>(0) ) );
  {
    set keys;
    { char s[] = "Size"; keys.insert(s); }
    { char s[] = "Prev"; keys.insert(s); }
    { char s[] = "ID"  ; keys.insert(s); }
    copy_but( &new_trailer, rep->trailer, keys );
  }

  new_trailer.add( "Size", Object( n_pdf_obj ) );
  new_trailer.add( "Prev", Object( offset_last_xref_table(pdf_filename) ) );

  string res = "trailer\n";

  // Do print() to a string rather than to stdout or a file.
  Object new_trailer_obj( &new_trailer );
  {
    int fd[2];
    pipe(fd);
    {
      FILE *q = fdopen( fd[1], "w" );
      new_trailer_obj.print(q);
      fclose(q);
    }
    {
      FILE *q = fdopen( fd[0], "r" );
      int c;
      while ( (c=fgetc(q)) != EOF ) res += c;
      fclose(q);
    }
  }

  res += "\nstartxref\n";
  {
    std::ostringstream oss;
    oss << offset_xref << '\n';
    res += oss.str();
  }
  res += "%%EOF\n";

  return res;

}