This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
pdf-backend.hh
435 lines (372 loc) · 9.81 KB
/
pdf-backend.hh
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/* Copyright © 2007-2022 Jakub Wilk <[email protected]>
* Copyright © 2009 Mateusz Turcza
*
* This file is part of pdf2djvu.
*
* pdf2djvu is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* pdf2djvu 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.
*/
#ifndef PDF2DJVU_PDF_BACKEND_H
#define PDF2DJVU_PDF_BACKEND_H
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <ctime>
#include <ostream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#include "autoconf.hh"
// Poppler:
#include <PDFDoc.h>
#include <Annot.h>
#include <Catalog.h>
#include <Dict.h>
#include <GfxState.h>
#include <Link.h>
#include <Object.h>
#include <OutputDev.h>
#include <SplashOutputDev.h>
#include <Stream.h>
#include <goo/GooString.h>
#include <goo/gfile.h>
#include <splash/Splash.h>
#include <splash/SplashBitmap.h>
#include <splash/SplashClip.h>
#include <splash/SplashFont.h>
#include <splash/SplashGlyphBitmap.h>
#include <splash/SplashPath.h>
#include <splash/SplashTypes.h>
#include "i18n.hh"
namespace pdf
{
/* type definitions — splash output device
* =======================================
*/
namespace splash
{
typedef ::Splash Splash;
typedef ::SplashColor Color;
typedef ::SplashFont Font;
typedef ::SplashCoord Coord;
typedef ::SplashPath Path;
typedef ::SplashGlyphBitmap GlyphBitmap;
typedef ::SplashBitmap Bitmap;
typedef ::SplashOutputDev OutputDevice;
typedef ::SplashClipResult ClipResult;
}
/* miscellaneous type definitions
* ==============================
*/
typedef ::OutputDev OutputDevice;
typedef ::Stream Stream;
typedef ::Object Object;
typedef ::Dict Dict;
typedef ::Catalog Catalog;
typedef ::GooString String;
typedef ::Goffset Offset;
typedef ::Ref Ref;
/* type definitions — annotations
* ==============================
*/
namespace ant
{
typedef ::Annot Annotation;
typedef ::AnnotColor Color;
}
/* type definitions — hyperlinks
* =============================
*/
namespace link
{
typedef ::AnnotLink Link;
typedef ::LinkAction Action;
typedef ::LinkDest Destination;
typedef ::LinkGoTo GoTo;
typedef ::LinkURI URI;
}
/* type definitions — rendering subsystem
* ======================================
*/
namespace gfx
{
typedef ::GfxSubpath Subpath;
typedef ::GfxPath Path;
typedef ::GfxState State;
typedef ::GfxImageColorMap ImageColorMap;
typedef ::GfxColorComp ColorComponent;
typedef ::GfxColor Color;
typedef ::GfxRGB RgbColor;
typedef ::GfxDeviceCMYKColorSpace DeviceCmykColorSpace;
}
/* class pdf::Renderer : pdf::splash::OutputDevice
* ===============================================
*/
class Renderer : public pdf::splash::OutputDevice
{
public:
explicit Renderer(pdf::splash::Color &paper_color, bool monochrome = false);
void processLink(pdf::link::Link *link)
{
this->drawLink(link, nullptr);
}
virtual void drawLink(pdf::link::Link *link, pdf::Catalog *catalog);
virtual void draw_link(pdf::link::Link *link, const std::string &border_color)
{ }
std::vector<std::string> link_border_colors;
void start_doc(::PDFDoc *doc)
{
this->startDoc(doc);
this->catalog = doc->getCatalog();
}
protected:
pdf::Catalog *catalog;
static void convert_path(gfx::State *state, pdf::splash::Path &splash_path);
};
/* class pdf::Pixmap::iterator
* ===========================
*/
class PixmapIterator
{
protected:
const uint8_t *row_ptr;
const uint8_t *ptr;
size_t row_size;
public:
PixmapIterator(const uint8_t *raw_data, size_t row_size)
{
this->row_ptr = this->ptr = raw_data;
this->row_size = row_size;
}
PixmapIterator &operator ++(int)
{
ptr += 3;
return *this;
}
void next_row()
{
ptr = row_ptr = row_ptr + row_size;
}
uint8_t operator[](int n) const
{
return this->ptr[n];
}
};
/* class pdf::Pixmap
* =================
*/
class Pixmap
{
private:
Pixmap(const Pixmap&) = delete;
Pixmap& operator=(const Pixmap&) = delete;
protected:
const uint8_t *raw_data;
pdf::splash::Bitmap *bmp;
size_t row_size;
size_t byte_width;
bool monochrome;
int width, height;
public:
typedef PixmapIterator iterator;
int get_width() const
{
return width;
}
int get_height() const
{
return height;
}
explicit Pixmap(Renderer *renderer)
{
bmp = renderer->takeBitmap();
raw_data = const_cast<const uint8_t*>(bmp->getDataPtr());
width = bmp->getWidth();
height = bmp->getHeight();
row_size = bmp->getRowSize();
this->monochrome = false;
switch (bmp->getMode())
{
case splashModeMono1:
this->byte_width = (width + 7) / 8;
this->monochrome = true;
break;
case splashModeMono8:
this->byte_width = width;
break;
case splashModeRGB8:
case splashModeBGR8:
this->byte_width = width * 3;
break;
case splashModeXBGR8:
#if POPPLER_VERSION >= 8100 || defined(SPLASH_CMYK)
case splashModeCMYK8:
#endif
this->byte_width = width * 4;
break;
#if POPPLER_VERSION >= 8100 || defined(SPLASH_CMYK)
case splashModeDeviceN8:
#endif
default:
assert(0 && "unexpected splash mode");
}
}
~Pixmap()
{
delete bmp;
}
PixmapIterator begin() const
{
return PixmapIterator(raw_data, row_size);
}
friend std::ostream &operator<<(std::ostream &, const Pixmap &);
};
/* class pdf::Environment
* ======================
*/
class Environment
{
public:
Environment();
static bool antialias;
void set_antialias(bool value);
class UnableToSetParameter : public std::runtime_error
{
public:
explicit UnableToSetParameter(const std::string &message)
: std::runtime_error(message)
{ }
};
};
/* class pdf::Document
* ===================
*/
class Document : public ::PDFDoc
{
public:
explicit Document(const std::string &file_name);
void display_page(Renderer *renderer, int npage, double hdpi, double vdpi, bool crop, bool do_links);
void get_page_size(int n, bool crop, double &width, double &height);
const std::string get_xmp();
void get_doc_info(pdf::Object &info)
{
#if POPPLER_VERSION < 5800
this->getDocInfo(&info);
#else
info = this->getDocInfo();
#endif
}
class LoadError : public std::runtime_error
{
public:
LoadError()
: std::runtime_error(_("Unable to load document"))
{ }
};
};
/* class pdf::Timestamp
* ====================
*/
class Timestamp
{
protected:
bool dummy;
struct tm timestamp;
char tz_sign;
int tz_hour;
int tz_minute;
public:
Timestamp();
Timestamp(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, char tz_sign = 0, int tz_hour = 0, int tz_minute = 0);
std::string format(char separator = 'T') const;
static Timestamp now();
class Invalid : public std::runtime_error
{
public:
Invalid()
: std::runtime_error(_("Invalid date format"))
{ }
};
};
/* class pdf::Metadata
* ===================
*/
class Metadata
{
protected:
typedef std::pair<const char *, std::string *> string_field;
std::vector<string_field> string_fields;
typedef std::pair<const char *, pdf::Timestamp *> date_field;
std::vector<date_field> date_fields;
public:
explicit Metadata(pdf::Document &document);
std::string title;
std::string subject;
std::string keywords;
std::string author;
std::string creator;
std::string producer;
Timestamp creation_date;
Timestamp mod_date;
template <typename T>
void iterate(
void (*string_callback)(const char *, const std::string &, T &),
void (*date_callback)(const char *, const Timestamp &, T &),
T &extra
) const;
};
template <typename T>
void Metadata::iterate(
void (*string_callback)(const char *, const std::string &, T &),
void (*date_callback)(const char *, const Timestamp &, T &),
T &extra
) const
{
for (const auto &field : this->string_fields)
string_callback(field.first, *field.second, extra);
for (const auto &field : this->date_fields)
date_callback(field.first, *field.second, extra);
}
/* utility functions
* =================
*/
void set_color(pdf::splash::Color &result, uint8_t r, uint8_t g, uint8_t b);
namespace gfx
{
static inline double color_component_as_double(pdf::gfx::ColorComponent c)
{
return ::colToDbl(c);
}
static inline pdf::gfx::ColorComponent double_as_color_component(double x)
{
return ::dblToCol(x);
}
}
/* glyph-related functions
* =======================
*/
bool get_glyph(pdf::splash::Splash *splash, pdf::splash::Font *font,
double x, double y, // x, y are transformed (i.e. output device) coordinates
int code, pdf::splash::GlyphBitmap *bitmap);
/* dictionary lookup
* =================
*/
pdf::Object *dict_lookup(pdf::Object &dict, const char *key, pdf::Object *object);
pdf::Object *dict_lookup(pdf::Object *dict, const char *key, pdf::Object *object);
pdf::Object *dict_lookup(pdf::Dict *dict, const char *key, pdf::Object *object);
/* path-related functions
* ======================
*/
double get_path_area(pdf::splash::Path &path);
const char * get_c_string(const pdf::String *str);
int find_page(pdf::Catalog *catalog, pdf::Ref pgref);
}
#endif
// vim:ts=2 sts=2 sw=2 et