libwps_internal.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
00002 /* libwps
00003  * Copyright (C) 2002 William Lachance (william.lachance@sympatico.ca)
00004  * Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net)
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
00019  *
00020  * For further information visit http://libwps.sourceforge.net
00021  */
00022 
00023 #ifndef LIBWPS_INTERNAL_H
00024 #define LIBWPS_INTERNAL_H
00025 
00026 #include <assert.h>
00027 #ifdef DEBUG
00028 #include <stdio.h>
00029 #endif
00030 
00031 #include <iostream>
00032 #include <map>
00033 #include <string>
00034 
00035 #include <libwpd-stream/libwpd-stream.h>
00036 #include <libwpd/libwpd.h>
00037 
00038 class WPXBinaryData;
00039 
00040 #if defined(_MSC_VER) || defined(__DJGPP__)
00041 typedef signed char int8_t;
00042 typedef unsigned char uint8_t;
00043 typedef signed short int16_t;
00044 typedef unsigned short uint16_t;
00045 typedef signed int int32_t;
00046 typedef unsigned int uint32_t;
00047 #else /* !_MSC_VER && !__DJGPP__*/
00048 #include <inttypes.h>
00049 #endif /* _MSC_VER || __DJGPP__*/
00050 
00051 /* ---------- memory  --------------- */
00052 #ifdef HAVE_CONFIG_H
00053 #include "config.h"
00054 #endif
00055 
00056 #if defined(SHAREDPTR_TR1)
00057 #include <tr1/memory>
00058 using std::tr1::shared_ptr;
00059 #elif defined(SHAREDPTR_STD)
00060 #include <memory>
00061 using std::shared_ptr;
00062 #else
00063 #include <boost/shared_ptr.hpp>
00064 using boost::shared_ptr;
00065 #endif
00066 
00068 template <class T>
00069 struct WPS_shared_ptr_noop_deleter
00070 {
00071         void operator() (T *) {}
00072 };
00073 
00074 typedef shared_ptr<WPXInputStream> WPXInputStreamPtr;
00075 
00076 /* ---------- debug  --------------- */
00077 #ifdef DEBUG
00078 #define WPS_DEBUG_MSG(M) printf M
00079 #else
00080 #define WPS_DEBUG_MSG(M)
00081 #endif
00082 
00083 /* ---------- exception  ------------ */
00084 namespace libwps
00085 {
00086 // Various exceptions
00087 class VersionException
00088 {
00089         // needless to say, we could flesh this class out a bit
00090 };
00091 
00092 class FileException
00093 {
00094         // needless to say, we could flesh this class out a bit
00095 };
00096 
00097 class ParseException
00098 {
00099         // needless to say, we could flesh this class out a bit
00100 };
00101 
00102 class GenericException
00103 {
00104         // needless to say, we could flesh this class out a bit
00105 };
00106 }
00107 
00108 /* ---------- input ----------------- */
00109 namespace libwps
00110 {
00111 uint8_t readU8(WPXInputStream *input);
00112 uint16_t readU16(WPXInputStream *input);
00113 uint32_t readU32(WPXInputStream *input);
00114 
00115 int8_t read8(WPXInputStream *input);
00116 int16_t read16(WPXInputStream *input);
00117 int32_t read32(WPXInputStream *input);
00118 
00119 inline uint8_t readU8(WPXInputStreamPtr &input)
00120 {
00121         return readU8(input.get());
00122 }
00123 inline uint16_t readU16(WPXInputStreamPtr &input)
00124 {
00125         return readU16(input.get());
00126 }
00127 inline uint32_t readU32(WPXInputStreamPtr &input)
00128 {
00129         return readU32(input.get());
00130 }
00131 
00132 inline int8_t read8(WPXInputStreamPtr &input)
00133 {
00134         return read8(input.get());
00135 }
00136 inline int16_t read16(WPXInputStreamPtr &input)
00137 {
00138         return read16(input.get());
00139 }
00140 inline int32_t read32(WPXInputStreamPtr &input)
00141 {
00142         return read32(input.get());
00143 }
00144 
00145 bool readData(WPXInputStreamPtr &input, unsigned long sz, WPXBinaryData &data);
00146 bool readDataToEnd(WPXInputStreamPtr &input, WPXBinaryData &data);
00147 }
00148 
00149 #define WPS_LE_GET_GUINT16(p)                             \
00150         (uint16_t)((((uint8_t const *)(p))[0] << 0)  |    \
00151                   (((uint8_t const *)(p))[1] << 8))
00152 #define WPS_LE_GET_GUINT32(p) \
00153         (uint32_t)((((uint8_t const *)(p))[0] << 0)  |    \
00154                   (((uint8_t const *)(p))[1] << 8)  |    \
00155                   (((uint8_t const *)(p))[2] << 16) |    \
00156                   (((uint8_t const *)(p))[3] << 24))
00157 
00158 // Various helper structures for the parser..
00159 /* ---------- small enum/class ------------- */
00160 class WPXPropertyListVector;
00161 struct WPSFont
00162 {
00164         WPSFont() : m_name(""), m_size(0), m_attributes(0), m_color(0), m_languageId(-1), m_extra("") {}
00165         static WPSFont getDefault()
00166         {
00167                 WPSFont res;
00168                 res.m_name = "Courier";
00169                 res.m_size = 12;
00170                 return res;
00171         }
00172 
00173         virtual ~WPSFont() {}
00175         friend std::ostream &operator<<(std::ostream &o, WPSFont const &ft);
00176 
00178         bool isSet() const
00179         {
00180                 return !m_name.empty();
00181         }
00182 
00184         bool operator==(WPSFont const &ft) const;
00185         bool operator!=(WPSFont const &ft) const
00186         {
00187                 return !operator==(ft);
00188         }
00189 
00191         std::string m_name;
00193         int m_size;
00195         uint32_t m_attributes;
00197         uint32_t m_color;
00199         int m_languageId;
00200 
00202         std::string m_extra;
00203 };
00204 
00205 struct WPSColumnDefinition
00206 {
00207         WPSColumnDefinition() : m_width(0), m_leftGutter(0), m_rightGutter(0)
00208         {
00209         }
00210         double m_width;
00211         double m_leftGutter;
00212         double m_rightGutter;
00213 };
00214 
00215 struct WPSColumnProperties
00216 {
00217         WPSColumnProperties() : m_attributes(0), m_alignment(0)
00218         {
00219         }
00220         uint32_t m_attributes;
00221         uint8_t m_alignment;
00222 };
00223 
00224 namespace libwps
00225 {
00226 enum NumberingType { NONE, BULLET, ARABIC, LOWERCASE, UPPERCASE, LOWERCASE_ROMAN, UPPERCASE_ROMAN };
00227 std::string numberingTypeToString(NumberingType type);
00228 enum SubDocumentType { DOC_NONE, DOC_HEADER_FOOTER, DOC_NOTE, DOC_TABLE, DOC_TEXT_BOX, DOC_COMMENT_ANNOTATION };
00229 enum Justification { JustificationLeft, JustificationFull, JustificationCenter,
00230                      JustificationRight, JustificationFullAllLines
00231                    };
00232 enum { NoBreakBit = 0x1, NoBreakWithNextBit=0x2};
00233 enum { LeftBorderBit = 0x01,  RightBorderBit = 0x02, TopBorderBit=0x4,
00234        BottomBorderBit = 0x08
00235      };
00236 enum BorderStyle { BorderSingle, BorderDouble, BorderDot, BorderLargeDot, BorderDash };
00237 }
00238 
00239 // ATTRIBUTE bits
00240 #define WPS_EXTRA_LARGE_BIT 1
00241 #define WPS_VERY_LARGE_BIT 2
00242 #define WPS_LARGE_BIT 4
00243 #define WPS_SMALL_PRINT_BIT 8
00244 #define WPS_FINE_PRINT_BIT 0x10
00245 #define WPS_SUPERSCRIPT_BIT 0x20
00246 #define WPS_SUBSCRIPT_BIT 0x40
00247 #define WPS_OUTLINE_BIT 0x80
00248 #define WPS_ITALICS_BIT 0x100
00249 #define WPS_SHADOW_BIT 0x200
00250 #define WPS_REDLINE_BIT 0x400
00251 #define WPS_DOUBLE_UNDERLINE_BIT 0x800
00252 #define WPS_BOLD_BIT 0x1000
00253 #define WPS_STRIKEOUT_BIT 0x2000
00254 #define WPS_UNDERLINE_BIT 0x4000
00255 #define WPS_SMALL_CAPS_BIT 0x8000
00256 #define WPS_BLINK_BIT 0x10000L
00257 #define WPS_REVERSEVIDEO_BIT 0x20000L
00258 #define WPS_ALL_CAPS_BIT 0x40000L
00259 #define WPS_EMBOSS_BIT 0x80000L
00260 #define WPS_ENGRAVE_BIT 0x100000L
00261 #define WPS_OVERLINE_BIT 0x400000L
00262 #define WPS_HIDDEN_BIT 0x800000L
00263 
00264 // BREAK bits
00265 #define WPS_PAGE_BREAK 0x00
00266 #define WPS_SOFT_PAGE_BREAK 0x01
00267 #define WPS_COLUMN_BREAK 0x02
00268 
00269 // Generic bits
00270 #define WPS_LEFT 0x00
00271 #define WPS_RIGHT 0x01
00272 #define WPS_CENTER 0x02
00273 #define WPS_TOP 0x03
00274 #define WPS_BOTTOM 0x04
00275 
00276 /* ---------- vec2/box2f ------------- */
00280 template <class T> class Vec2
00281 {
00282 public:
00284         Vec2(T xx=0,T yy=0) : m_x(xx), m_y(yy) { }
00286         template <class U> Vec2(Vec2<U> const &p) : m_x(T(p.x())), m_y(T(p.y())) {}
00287 
00289         T x() const
00290         {
00291                 return m_x;
00292         }
00294         T y() const
00295         {
00296                 return m_y;
00297         }
00299         T operator[](int c) const
00300         {
00301                 assert(c >= 0 && c <= 1);
00302                 return (c==0) ? m_x : m_y;
00303         }
00305         T &operator[](int c)
00306         {
00307                 assert(c >= 0 && c <= 1);
00308                 return (c==0) ? m_x : m_y;
00309         }
00310 
00312         void set(T xx, T yy)
00313         {
00314                 m_x = xx;
00315                 m_y = yy;
00316         }
00318         void setX(T xx)
00319         {
00320                 m_x = xx;
00321         }
00323         void setY(T yy)
00324         {
00325                 m_y = yy;
00326         }
00327 
00329         void add(T dx, T dy)
00330         {
00331                 m_x += dx;
00332                 m_y += dy;
00333         }
00334 
00336         Vec2<T> &operator+=(Vec2<T> const &p)
00337         {
00338                 m_x += p.m_x;
00339                 m_y += p.m_y;
00340                 return *this;
00341         }
00343         Vec2<T> &operator-=(Vec2<T> const &p)
00344         {
00345                 m_x -= p.m_x;
00346                 m_y -= p.m_y;
00347                 return *this;
00348         }
00350         template <class U>
00351         Vec2<T> &operator*=(U scale)
00352         {
00353                 m_x = T(m_x*scale);
00354                 m_y = T(m_y*scale);
00355                 return *this;
00356         }
00357 
00359         friend Vec2<T> operator+(Vec2<T> const &p1, Vec2<T> const &p2)
00360         {
00361                 Vec2<T> p(p1);
00362                 return p+=p2;
00363         }
00365         friend Vec2<T> operator-(Vec2<T> const &p1, Vec2<T> const &p2)
00366         {
00367                 Vec2<T> p(p1);
00368                 return p-=p2;
00369         }
00371         template <class U>
00372         friend Vec2<T> operator*(U scale, Vec2<T> const &p1)
00373         {
00374                 Vec2<T> p(p1);
00375                 return p *= scale;
00376         }
00377 
00379         bool operator==(Vec2<T> const &p) const
00380         {
00381                 return cmpY(p) == 0;
00382         }
00384         bool operator!=(Vec2<T> const &p) const
00385         {
00386                 return cmpY(p) != 0;
00387         }
00389         bool operator<(Vec2<T> const &p) const
00390         {
00391                 return cmpY(p) < 0;
00392         }
00394         int cmp(Vec2<T> const &p) const
00395         {
00396                 T diff  = m_x-p.m_x;
00397                 if (diff) return (diff < 0) ? -1 : 1;
00398                 diff = m_y-p.m_y;
00399                 if (diff) return (diff < 0) ? -1 : 1;
00400                 return 0;
00401         }
00403         int cmpY(Vec2<T> const &p) const
00404         {
00405                 T diff  = m_y-p.m_y;
00406                 if (diff) return (diff < 0) ? -1 : 1;
00407                 diff = m_x-p.m_x;
00408                 if (diff) return (diff < 0) ? -1 : 1;
00409                 return 0;
00410         }
00411 
00413         friend std::ostream &operator<< (std::ostream &o, Vec2<T> const &f)
00414         {
00415                 o << f.m_x << "x" << f.m_y;
00416                 return o;
00417         }
00418 
00422         struct PosSizeLtX
00423         {
00425                 bool operator()(Vec2<T> const &s1, Vec2<T> const &s2) const
00426                 {
00427                         return s1.cmp(s2) < 0;
00428                 }
00429         };
00433         typedef std::map<Vec2<T>, T,struct PosSizeLtX> MapX;
00434 
00438         struct PosSizeLtY
00439         {
00441                 bool operator()(Vec2<T> const &s1, Vec2<T> const &s2) const
00442                 {
00443                         return s1.cmpY(s2) < 0;
00444                 }
00445         };
00449         typedef std::map<Vec2<T>, T,struct PosSizeLtY> MapY;
00450 protected:
00451         T m_x, m_y;
00452 };
00453 
00455 typedef Vec2<bool> Vec2b;
00457 typedef Vec2<int> Vec2i;
00459 typedef Vec2<float> Vec2f;
00460 
00464 template <class T> class Box2
00465 {
00466 public:
00468         Box2(Vec2<T> minPt=Vec2<T>(), Vec2<T> maxPt=Vec2<T>())
00469         {
00470                 m_pt[0] = minPt;
00471                 m_pt[1] = maxPt;
00472         }
00474         template <class U> Box2(Box2<U> const &p)
00475         {
00476                 for (int c=0; c < 2; c++) m_pt[c] = p[c];
00477         }
00478 
00480         Vec2<T> const &min() const
00481         {
00482                 return m_pt[0];
00483         }
00485         Vec2<T> const &max() const
00486         {
00487                 return m_pt[1];
00488         }
00490         Vec2<T> &min()
00491         {
00492                 return m_pt[0];
00493         }
00495         Vec2<T> &max()
00496         {
00497                 return m_pt[1];
00498         }
00499 
00504         Vec2<T> const &operator[](int c) const
00505         {
00506                 assert(c >= 0 && c <= 1);
00507                 return m_pt[c];
00508         }
00510         Vec2<T> size() const
00511         {
00512                 return m_pt[1]-m_pt[0];
00513         }
00515         Vec2<T> center() const
00516         {
00517                 return 0.5*(m_pt[0]+m_pt[1]);
00518         }
00519 
00521         void set(Vec2<T> const &x, Vec2<T> const &y)
00522         {
00523                 m_pt[0] = x;
00524                 m_pt[1] = y;
00525         }
00527         void setMin(Vec2<T> const &x)
00528         {
00529                 m_pt[0] = x;
00530         }
00532         void setMax(Vec2<T> const &y)
00533         {
00534                 m_pt[1] = y;
00535         }
00536 
00538         void resizeFromMin(Vec2<T> const &sz)
00539         {
00540                 m_pt[1] = m_pt[0]+sz;
00541         }
00543         void resizeFromMax(Vec2<T> const &sz)
00544         {
00545                 m_pt[0] = m_pt[1]-sz;
00546         }
00548         void resizeFromCenter(Vec2<T> const &sz)
00549         {
00550                 Vec2<T> ctr = 0.5*(m_pt[0]+m_pt[1]);
00551                 m_pt[0] = ctr - 0.5*sz;
00552                 m_pt[1] = ctr + (sz - 0.5*sz);
00553         }
00554 
00556         template <class U> void scale(U factor)
00557         {
00558                 m_pt[0] *= factor;
00559                 m_pt[1] *= factor;
00560         }
00561 
00563         void extend(T val)
00564         {
00565                 m_pt[0] -= Vec2<T>(val/2,val/2);
00566                 m_pt[1] += Vec2<T>(val-(val/2),val-(val/2));
00567         }
00568 
00570         bool operator==(Box2<T> const &p) const
00571         {
00572                 return cmp(p) == 0;
00573         }
00575         bool operator!=(Box2<T> const &p) const
00576         {
00577                 return cmp(p) != 0;
00578         }
00580         bool operator<(Box2<T> const &p) const
00581         {
00582                 return cmp(p) < 0;
00583         }
00584 
00586         int cmp(Box2<T> const &p) const
00587         {
00588                 int diff  = m_pt[0].cmpY(p.m_pt[0]);
00589                 if (diff) return diff;
00590                 diff  = m_pt[1].cmpY(p.m_pt[1]);
00591                 if (diff) return diff;
00592                 return 0;
00593         }
00594 
00596         friend std::ostream &operator<< (std::ostream &o, Box2<T> const &f)
00597         {
00598                 o << "(" << f.m_pt[0] << "<->" << f.m_pt[1] << ")";
00599                 return o;
00600         }
00601 
00605         struct PosSizeLt
00606         {
00608                 bool operator()(Box2<T> const &s1, Box2<T> const &s2) const
00609                 {
00610                         return s1.cmp(s2) < 0;
00611                 }
00612         };
00616         typedef std::map<Box2<T>, T,struct PosSizeLt> Map;
00617 
00618 protected:
00620         Vec2<T> m_pt[2];
00621 };
00622 
00624 typedef Box2<int> Box2i;
00626 typedef Box2<float> Box2f;
00627 
00628 #endif /* LIBWPS_INTERNAL_H */
00629 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */