summaryrefslogtreecommitdiffstats
path: root/src/extension/internal/pdfinput/pdf-utils.cpp
blob: 05c4ac5d16cfde0b6aa7d0dee6dd94307ec12a64 (plain)
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
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * Utility structures and functions for pdf parsing.
 *//*
 * 
 * Copyright (C) 2023 Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <glib.h>
#include "pdf-utils.h"

#include "poppler-utils.h"

//------------------------------------------------------------------------
// ClipHistoryEntry
//------------------------------------------------------------------------

ClipHistoryEntry::ClipHistoryEntry(GfxPath *clipPathA, GfxClipType clipTypeA)
    : saved(nullptr)
    , clipPath((clipPathA) ? clipPathA->copy() : nullptr)
    , clipType(clipTypeA)
{}

ClipHistoryEntry::~ClipHistoryEntry()
{
    if (clipPath) {
        delete clipPath;
        clipPath = nullptr;
    }
}

void ClipHistoryEntry::setClip(GfxState *state, GfxClipType clipTypeA, bool bbox)
{
    const GfxPath *clipPathA = state->getPath();

    if (clipPath) {
        if (copied) {
            // Free previously copied clip path.
            delete clipPath;
        } else {
            // This indicates a bad use of the ClipHistory API
            g_error("Clip path is already set!");
            return;
        }
    }

    cleared = false;
    copied = false;
    if (clipPathA) {
        affine = stateToAffine(state);
        clipPath = clipPathA->copy();
        clipType = clipTypeA;
        is_bbox = bbox;
    } else {
        affine = Geom::identity();
        clipPath = nullptr;
        clipType = clipNormal;
        is_bbox = false;
    }
}

/**
 * Create a new clip-history, appending it to the stack.
 *
 * If cleared is set to true, it will not remember the current clipping path.
 */
ClipHistoryEntry *ClipHistoryEntry::save(bool cleared)
{
    ClipHistoryEntry *newEntry = new ClipHistoryEntry(this, cleared);
    newEntry->saved = this;
    return newEntry;
}

ClipHistoryEntry *ClipHistoryEntry::restore()
{
    ClipHistoryEntry *oldEntry;

    if (saved) {
        oldEntry = saved;
        saved = nullptr;
        delete this; // TODO really should avoid deleting from inside.
    } else {
        oldEntry = this;
    }

    return oldEntry;
}

ClipHistoryEntry::ClipHistoryEntry(ClipHistoryEntry *other, bool cleared)
{
    if (other && other->clipPath) {
        this->affine = other->affine;
        this->clipPath = other->clipPath->copy();
        this->clipType = other->clipType;
        this->cleared = cleared;
        this->copied = true;
        this->is_bbox = other->is_bbox;
    } else {
        this->affine = Geom::identity();
        this->clipPath = nullptr;
        this->clipType = clipNormal;
        this->cleared = false;
        this->copied = false;
        this->is_bbox = false;
    }
    saved = nullptr;
}

Geom::Rect getRect(_POPPLER_CONST PDFRectangle *box)
{
    return Geom::Rect(box->x1, box->y1, box->x2, box->y2);
}