00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef MISC_H
00021 #define MISC_H
00022
00023 #include <math.h>
00024
00025 #include <qcolor.h>
00026 #include <qdom.h>
00027 #include <qsize.h>
00028 #include <qvaluevector.h>
00029 #include <qdict.h>
00030 #include <qmap.h>
00031
00032 class LinkAction;
00033 class Catalog;
00034 class GfxRGB;
00035 class GfxState;
00036
00037 namespace PDFImport
00038 {
00039
00040
00041
00042 template <class Container>
00043 inline void qHeapSort2( Container &c )
00044 {
00045 if ( c.begin() == c.end() )
00046 return;
00047
00048
00049
00050 qHeapSortHelper( c.begin(), c.end(), *(c.begin()), (uint)c.size() );
00051 }
00052
00053 enum ParagraphType { Body = 0, Header, Footer, Nb_ParagraphTypes };
00054
00055
00056 enum FontFamily { Times = 0, Helvetica, Courier, Symbol, Nb_Family };
00057 enum FontStyle { Regular, Bold, Italic, BoldItalic };
00058
00059 inline FontStyle toStyle(bool bold, bool italic) {
00060 return (bold ? (italic ? BoldItalic : Bold)
00061 : (italic ? Italic : Regular) );
00062 }
00063 inline bool isItalic(FontStyle style) {
00064 return (style==Italic || style==BoldItalic);
00065 }
00066 inline bool isBold(FontStyle style) {
00067 return (style==Bold || style==BoldItalic);
00068 }
00069
00070
00071 inline double mmToPoint(double mm) { return mm * 72 / 25.4; }
00072
00073 inline bool equal(double d1, double d2, double percent = 0.01) {
00074 double delta = percent * (fabs(d1)+fabs(d2)) / 2;
00075 return ( fabs(d1 - d2)<delta );
00076 }
00077 inline bool more(double d1, double d2, double percent = 0.01) {
00078 double delta = percent * (fabs(d1)+fabs(d2)) / 2;
00079 return ( (d2-d1)<delta );
00080 }
00081 inline bool less(double d1, double d2, double percent = 0.01) {
00082 double delta = percent * (fabs(d1)+fabs(d2)) / 2;
00083 return ( (d1-d2)<delta );
00084 }
00085
00086 QColor toColor(GfxRGB &);
00087
00088
00089 class DRect {
00090 public:
00091 DRect() : _left(0), _right(0), _top(0), _bottom(0) {}
00092 DRect(double left, double right, double top, double bottom)
00093 : _left(left), _right(right), _top(top), _bottom(bottom) {}
00094
00095 bool isValid() const { return ( _right>_left && _bottom>_top ); }
00096
00097 void setTop(double top) { _top = top; }
00098 void setBottom(double bottom) { _bottom = bottom; }
00099 void setRight(double right) { _right = right; }
00100 void setLeft(double left) { _left = left; }
00101
00102 double top() const { return _top; }
00103 double bottom() const { return _bottom; }
00104 double left() const { return _left; }
00105 double right() const { return _right; }
00106
00107 double width() const { return _right - _left; }
00108 double height() const { return _bottom - _top; }
00109
00110 bool operator ==(const DRect &) const;
00111 bool isInside(const DRect &, double percent = 0.01) const;
00112 void unite(const DRect &);
00113 QString toString() const;
00114
00115 private:
00116 double _left, _right, _top, _bottom;
00117 };
00118
00119 struct DPoint {
00120 double x, y;
00121 };
00122
00123 class DPath : public QValueVector<DPoint>
00124 {
00125 public:
00126 DPath() {}
00127
00128 bool isSegment() const { return size()==2; }
00129 bool isHorizontalSegment() const {
00130 return isSegment() && equal(at(0).y, at(1).y);
00131 }
00132 bool isVerticalSegment() const {
00133 return isSegment() && equal(at(0).x, at(1).y);
00134 }
00135 bool isRectangle() const;
00136 DRect boundingRect() const;
00137 };
00138
00139 typedef QValueVector<DPath> DPathVector;
00140
00141
00142 class Font
00143 {
00144 public:
00145 Font();
00146 Font(const GfxState *, double size);
00147
00148 bool operator ==(const Font &) const;
00149 bool format(QDomDocument &, QDomElement &format, uint pos, uint len,
00150 bool all = false) const;
00151 int height() const { return _data->height[_pointSize]; }
00152 const QColor &color() const { return _color; }
00153 bool isLatex() const { return _data->latex; }
00154
00155 void setFamily(FontFamily);
00156
00157 static void init();
00158 static void cleanup();
00159
00160 private:
00161 void init(const QString &name);
00162
00163 private:
00164 uint _pointSize;
00165 QColor _color;
00166
00167 class Data {
00168 public:
00169 QString family;
00170 FontStyle style;
00171 bool latex;
00172 QMap<int, int> height;
00173 };
00174 Data *_data;
00175
00176 static QDict<Data> *_dict;
00177 static const char *FAMILY_DATA[PDFImport::Nb_Family];
00178 };
00179
00180
00181 class Link
00182 {
00183 public:
00184 Link(const DRect &, LinkAction &, Catalog &);
00185
00186 const DRect &rect() const { return _rect; }
00187 void format(QDomDocument &, QDomElement &format,
00188 uint pos, const QString &text) const;
00189
00190 static QString pageLinkName(uint i);
00191
00192 private:
00193 DRect _rect;
00194 QString _href;
00195 };
00196
00197 }
00198
00199 #endif