aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/pdf-rotate.cc
blob: 005566879bb7ad727b2b8a8408fad971ff7a2bc5 (plain) (blame)
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
// This program is not part of QPDF, but it is made available with the
// QPDF software under the same terms as QPDF itself.
//
// Author: Iskander Sharipov <Iskander.Sharipov@tatar.ru>
//
// This program requires a c++11 compiler but otherwise has no
// external dependencies beyond QPDF itself.

#include <qpdf/QPDF.hh>
#include <qpdf/QPDFWriter.hh>

#include <vector>
#include <regex>
#include <algorithm>

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cerrno>

#include <sys/stat.h>

using std::vector;

typedef QPDFObjectHandle QpdfObject;

/*
 * Rotates clockwise/counter-clockwise selected pages or page range.
 * It is also capable of setting absolute page rotation.
 * Check `usage` for details.
 *
 * User should check program return value to handle errors.
 * Check `ErrorCode` for enumeration and `error_message` for descriptions.
 */

enum class ErrorCode: int {
    Success, // <- Not an error
    InvalidArgCount,
    InputFileNotExist,
    InvalidDegreeArg,
    InvalidPageSelectorArg,
    InvalidRangeSelector,
    InvalidPagesSelector,
    BadPageNumber,
    BadLowRangeBound,
    BadHighRangeBound,
    UnexpectedException,
    InternalRangeError,
};

struct Rotation {
    Rotation() = default;
    Rotation(const char* digits);

    int degree;
    bool absolute;
};

struct Arguments {
    const char* in_file_name;
    const char* out_file_name;
    char* pages = nullptr;
    const char* range = nullptr;
    Rotation degree;
};

void print_error(ErrorCode code);
Arguments parse_arguments(int argc, char* argv[]);
void rotate_pages(QPDF& in, QPDF& out, char* selector, Rotation);
void rotate_page_range(QPDF& in, QPDF& out, const char* range, Rotation);
void rotate_all_pages(QPDF& in, QPDF& out, Rotation);

int main(int argc, char* argv[]) {
    try {
        Arguments args = parse_arguments(argc, argv);

        QPDF in_pdf;
        in_pdf.processFile(args.in_file_name);
        QPDF out_pdf;
        out_pdf.emptyPDF();

        if (args.pages) {
            rotate_pages(in_pdf, out_pdf, args.pages, args.degree);
        } else if (args.range) {
            rotate_page_range(in_pdf, out_pdf, args.range, args.degree);
        } else {
            rotate_all_pages(in_pdf, out_pdf, args.degree);
        }

        QPDFWriter out_writer{out_pdf, args.out_file_name};
        out_writer.write();
    } catch (ErrorCode e) {
        print_error(e);
        return static_cast<int>(e);
    } catch (...) {
        print_error(ErrorCode::UnexpectedException);
        return static_cast<int>(ErrorCode::UnexpectedException);
    }

    return static_cast<int>(ErrorCode::Success);
}

const int minExpectedArgs = 4;
const int degreeArgMaxLen = 3;

int try_parse_int(const char* digits, ErrorCode failure_code) {
    char* tail;
    auto result = strtol(digits, &tail, 10);
    auto len = tail - digits;

    if (len == 0 || errno == ERANGE) {
        throw failure_code;
    }

    return result;
}

Rotation::Rotation(const char* digits) {
    absolute = isdigit(*digits);
    degree = try_parse_int(digits, ErrorCode::InvalidDegreeArg);

    if (degree % 90 != 0) {
        throw ErrorCode::InvalidDegreeArg;
    }
}

// If error message printing is not required, compile with -DNO_OUTPUT
#ifdef NO_OUTPUT
void usage() {}
void printError(ErrorCode) {}
#else
void usage() {
    puts(
        "usage: `qpdf-rotate <in-pdf> <degree> <out-pdf> [<page-selector>]`\n"
        "<in-pdf>: path to input pdf file\n"
        "<degree>: any 90 divisible angle; + or - forces relative rotation\n"
        "<out-pdf>: path for output pdf which will be created\n"
        "note: <in-pdf> must be distinct from <out-pdf>\n"
        "\n"
        "optional page selector arg:\n"
        "one of two possible formats: "
        "1) `--range=<low>-<high>` pages in range (use `z` for last page)\n"
        "2) `--pages=<p1>,...,<pn>` only specified pages (trailing comma is OK)\n"
        "note: (2) option needs sorted comma separated list\n"
        "\n"
        "example: `qpdf-rotate foo.pdf +90 bar.pdf --range=1-10\n"
        "example: `qpdf-rotate foo.pdf 0 bar.pdf --pages=1,2,3,7,10`\n"
        "example: `qpdf-rotate foo.pdf -90 bar.pdf --range=5-z"
    );
}

const char* error_message(ErrorCode code) {
    switch (code) {
    case ErrorCode::InvalidArgCount:
        return "<in-pdf> or <degree> or <out-pdf> arg is missing";
    case ErrorCode::InputFileNotExist:
        return "<in-pdf> file not found (not exists or can not be accessed)";
    case ErrorCode::InvalidDegreeArg:
        return "<degree> invalid value given";
    case ErrorCode::InvalidPageSelectorArg:
        return "<page-selector> invalid value given";
    case ErrorCode::InvalidRangeSelector:
        return "invalid range selector";
    case ErrorCode::InvalidPagesSelector:
        return "invalid pages selector";
    case ErrorCode::BadLowRangeBound:
        return "bad low range boundary";
    case ErrorCode::BadHighRangeBound:
        return "bad high range boundary";
    case ErrorCode::UnexpectedException:
        return "unexpected exception during execution";
    case ErrorCode::InternalRangeError:
        return "internal range error";

    default:
        return "";
    }
}

void print_error(ErrorCode code) {
    fprintf(stderr, "%s\n", error_message(code));
}
#endif

void validate_range_selector(const char* selector) {
    if (!std::regex_match(selector, std::regex("^\\d+\\-(\\d+|z)$"))) {
       throw ErrorCode::InvalidRangeSelector;
    }
}

void validate_pages_selector(const char* selector) {
    const char* p = selector;

    while (*p && isdigit(*p)) {
        while (isdigit(*p)) ++p;
        if (*p && *p == ',') ++p;
    }

    if (*p != '\0') {
        throw ErrorCode::InvalidPagesSelector;
    }
}

inline bool file_exists(const char* name) {
  struct stat file_info;
  return stat(name, &file_info) == 0;
}

bool has_substr(const char* haystack, const char* needle) {
    return strncmp(haystack, needle, strlen(needle)) == 0;
}

const char* fetch_range_selector(const char* selector) {
    const char* value = strchr(selector, '=') + 1;

    validate_range_selector(value);

    return value;
}

char* fetch_pages_selector(char* selector) {
    char* value = strchr(selector, '=') + 1;

    validate_pages_selector(value);

    return value;
}

Arguments parse_arguments(int argc, char* argv[]) {
    if (argc < minExpectedArgs) {
        if (argc == 1) { // Launched without args
            usage();
        }

        throw ErrorCode::InvalidArgCount;
    }

    enum Argv: int {
        ProgramName,
        InputFile,
        Degree,
        OutputFile,
        PageSelector
    };

    Arguments args;

    args.in_file_name = argv[Argv::InputFile];
    args.out_file_name = argv[Argv::OutputFile];

    if (!file_exists(args.in_file_name)) throw ErrorCode::InputFileNotExist;

    args.degree = Rotation{argv[Argv::Degree]};

    if (argc > minExpectedArgs) { // Page selector given as an argument
        char* page_selector_arg = argv[Argv::PageSelector];

        if (has_substr(page_selector_arg, "--range=")) {
            args.range = fetch_range_selector(page_selector_arg);
        } else if (has_substr(page_selector_arg, "--pages=")) {
            args.pages = fetch_pages_selector(page_selector_arg);
        } else {
            throw ErrorCode::InvalidPageSelectorArg;
        }
    }

    return args;
}

// Simple wrapper around vector<QpdfObject> range
struct PageRange {
    PageRange(int from, vector<QpdfObject>& pages):
    from_page{from}, to_page{static_cast<int>(pages.size())}, pages{pages} {
        check_invariants();
    }

    PageRange(int from, int to, vector<QpdfObject>& pages):
    from_page{from}, to_page{to}, pages{pages} {
        check_invariants();
    }

    void check_invariants() {
        if (from_page < 1 || to_page < 1) {
            throw ErrorCode::InternalRangeError;
        }
    }

    QpdfObject* begin() const noexcept { return pages.data() + from_page - 1; }
    QpdfObject* end() const noexcept { return pages.data() + to_page; }

private:
    vector<QpdfObject>& pages;
    int to_page;
    int from_page;
};

QpdfObject calculate_degree(QpdfObject& page, Rotation rotation) {
    int degree = rotation.degree;

    if (!rotation.absolute && page.hasKey("/Rotate")) {
        int old_degree = page.getKey("/Rotate").getNumericValue();
        degree += old_degree;
    }

    return QpdfObject::newInteger(degree);
}

void add_rotated_page(QPDF& pdf, QpdfObject& page, Rotation rotation) {
    page.replaceKey("/Rotate", calculate_degree(page, rotation));
    pdf.addPage(page, false);
}

void add_rotated_pages(QPDF& pdf, PageRange pages, Rotation rotation) {
    for (auto page : pages) {
        add_rotated_page(pdf, page, rotation);
    }
}

void add_pages(QPDF& pdf, PageRange pages) {
    for (auto page : pages) {
        pdf.addPage(page, false);
    }
}

void rotate_pages(QPDF& in, QPDF& out, char* selector, Rotation rotation) {
    static const int unparsed = -1;

    auto pages = in.getAllPages();
    auto digits = strtok(selector, ",");
    int n = unparsed;

    for (int page_n = 0; page_n < pages.size(); ++page_n) {
        if (digits && n == unparsed) {
            n = try_parse_int(digits, ErrorCode::BadPageNumber) - 1;
        }

        if (n == page_n) {
            digits = strtok(nullptr, ",");
            n = unparsed;
            add_rotated_page(out, pages[page_n], rotation);
        } else {
            out.addPage(pages[page_n], false);
        }
    }
}

void rotate_page_range(QPDF& in, QPDF& out, const char* range, Rotation rotation) {
    auto pages = in.getAllPages();

    int from_page = try_parse_int(range, ErrorCode::BadLowRangeBound);
    int to_page;

    if (range[(strlen(range)-1)] == 'z') {
        to_page = pages.size();
    } else {
        to_page = try_parse_int(strchr(range, '-') + 1, ErrorCode::BadHighRangeBound);
    }

    if (from_page > 1) {
        add_pages(out, PageRange{1, from_page - 1, pages});
    }
    add_rotated_pages(out, PageRange{from_page, to_page, pages}, rotation);
    if (to_page < pages.size()) {
        add_pages(out, PageRange(to_page + 1, pages));
    }
}

void rotate_all_pages(QPDF& in, QPDF& out, Rotation rotation) {
    auto pages = in.getAllPages();
    add_rotated_pages(out, PageRange{1, pages}, rotation);
}