summaryrefslogtreecommitdiffstats
path: root/filters/kword/pdf/misc.h
blob: 5af22ed7460eb6d080ca683b4d1d0cfe21e12759 (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
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
/*
 * Copyright (c) 2002-2003 Nicolas HADACEK (hadacek@kde.org)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program 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.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifndef MISC_H
#define MISC_H

#include <math.h>

#include <tqcolor.h>
#include <tqdom.h>
#include <tqsize.h>
#include <tqvaluevector.h>
#include <tqdict.h>
#include <tqmap.h>

class LinkAction;
class Catalog;
class GfxRGB;
class GfxState;

namespace PDFImport
{

// FIX for TQt 3.0
// qvaluevector bug - qheapsort uses 'count' but qvaluevector has only 'size'
template <class Container>
inline void qHeapSort2( Container &c )
{
    if ( c.begin() == c.end() )
        return;

    // The second last parameter is a hack to retrieve the value type
    // Do the real sorting here
    qHeapSortHelper( c.begin(), c.end(), *(c.begin()), (uint)c.size() );
}

enum ParagraphType { Body = 0, Header, Footer, Nb_ParagraphTypes };

//-----------------------------------------------------------------------------
enum FontFamily { Times = 0, Helvetica, Courier, Symbol, Nb_Family };
enum FontStyle { Regular, Bold, Italic, BoldItalic };

inline FontStyle toStyle(bool bold, bool italic) {
    return (bold ? (italic ? BoldItalic : Bold)
            : (italic ? Italic : Regular) );
}
inline bool isItalic(FontStyle style) {
    return (style==Italic || style==BoldItalic);
}
inline bool isBold(FontStyle style) {
    return (style==Bold || style==BoldItalic);
}

//-----------------------------------------------------------------------------
inline double mmToPoint(double mm) { return mm * 72 / 25.4; }

inline bool equal(double d1, double d2, double percent = 0.01) {
    double delta = percent * (fabs(d1)+fabs(d2)) / 2;
    return ( fabs(d1 - d2)<delta );
}
inline bool more(double d1, double d2, double percent = 0.01) {
    double delta = percent * (fabs(d1)+fabs(d2)) / 2;
    return ( (d2-d1)<delta );
}
inline bool less(double d1, double d2, double percent = 0.01) {
    double delta = percent * (fabs(d1)+fabs(d2)) / 2;
    return ( (d1-d2)<delta );
}

TQColor toColor(GfxRGB &);

//-----------------------------------------------------------------------------
class DRect {
public:
    DRect() : _left(0), _right(0), _top(0), _bottom(0) {}
    DRect(double left, double right, double top, double bottom)
        : _left(left), _right(right), _top(top), _bottom(bottom) {}

    bool isValid() const { return ( _right>_left && _bottom>_top ); }

    void setTop(double top) { _top = top; }
    void setBottom(double bottom) { _bottom = bottom; }
    void setRight(double right) { _right = right; }
    void setLeft(double left) { _left = left; }

    double top() const { return _top; }
    double bottom() const { return _bottom; }
    double left() const { return _left; }
    double right() const { return _right; }

    double width() const { return _right - _left; }
    double height() const { return _bottom - _top; }

    bool operator ==(const DRect &) const;
    bool isInside(const DRect &, double percent = 0.01) const;
    void unite(const DRect &);
    TQString toString() const;

private:
    double _left, _right, _top, _bottom;
};

struct DPoint {
    double x, y;
};

class DPath : public TQValueVector<DPoint>
{
public:
    DPath() {}

    bool isSegment() const { return size()==2; }
    bool isHorizontalSegment() const {
        return isSegment() && equal(at(0).y, at(1).y);
    }
    bool isVerticalSegment() const {
        return isSegment() && equal(at(0).x, at(1).y);
    }
    bool isRectangle() const;
    DRect boundingRect() const;
};

typedef TQValueVector<DPath> DPathVector;

//-----------------------------------------------------------------------------
class Font
{
public:
    Font();
    Font(const GfxState *, double size);

    bool operator ==(const Font &) const;
    bool format(TQDomDocument &, TQDomElement &format, uint pos, uint len,
                bool all = false) const;
    int height() const { return _data->height[_pointSize]; }
    const TQColor &color() const { return _color; }
    bool isLatex() const { return _data->latex; }

    void setFamily(FontFamily);

    static void init();
    static void cleanup();

private:
    void init(const TQString &name);

private:
    uint   _pointSize;
    TQColor _color;

    class Data {
    public:
        TQString   family;
        FontStyle style;
        bool      latex;
        TQMap<int, int> height;
    };
    Data *_data;

    static TQDict<Data> *_dict;
    static const char *FAMILY_DATA[PDFImport::Nb_Family];
};

//-----------------------------------------------------------------------------
class Link
{
public:
    Link(const DRect &, LinkAction &, Catalog &);

    const DRect &rect() const { return _rect; }
    void format(TQDomDocument &, TQDomElement &format,
                uint pos, const TQString &text) const;

    static TQString pageLinkName(uint i);

private:
    DRect   _rect;
    TQString _href;
};

} // namespace

#endif