libwps_internal.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* libwps
3  * Copyright (C) 2002 William Lachance (william.lachance@sympatico.ca)
4  * Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * For further information visit http://libwps.sourceforge.net
21  */
22 
23 #ifndef LIBWPS_INTERNAL_H
24 #define LIBWPS_INTERNAL_H
25 
26 #include <assert.h>
27 #ifdef DEBUG
28 #include <stdio.h>
29 #endif
30 
31 #include <iostream>
32 #include <map>
33 #include <string>
34 
35 #include <libwpd-stream/libwpd-stream.h>
36 #include <libwpd/libwpd.h>
37 
38 class WPXBinaryData;
39 
40 #if defined(_MSC_VER) || defined(__DJGPP__)
41 typedef signed char int8_t;
42 typedef unsigned char uint8_t;
43 typedef signed short int16_t;
44 typedef unsigned short uint16_t;
45 typedef signed int int32_t;
46 typedef unsigned int uint32_t;
47 #else /* !_MSC_VER && !__DJGPP__*/
48 #include <inttypes.h>
49 #endif /* _MSC_VER || __DJGPP__*/
50 
51 /* ---------- memory --------------- */
52 #ifdef HAVE_CONFIG_H
53 #include "config.h"
54 #endif
55 
56 #if defined(SHAREDPTR_TR1)
57 #include <tr1/memory>
58 using std::tr1::shared_ptr;
59 #elif defined(SHAREDPTR_STD)
60 #include <memory>
61 using std::shared_ptr;
62 #else
63 #include <boost/shared_ptr.hpp>
64 using boost::shared_ptr;
65 #endif
66 
68 template <class T>
70 {
71  void operator() (T *) {}
72 };
73 
74 typedef shared_ptr<WPXInputStream> WPXInputStreamPtr;
75 
76 /* ---------- debug --------------- */
77 #ifdef DEBUG
78 #define WPS_DEBUG_MSG(M) printf M
79 #else
80 #define WPS_DEBUG_MSG(M)
81 #endif
82 
83 /* ---------- exception ------------ */
84 namespace libwps
85 {
86 // Various exceptions
88 {
89  // needless to say, we could flesh this class out a bit
90 };
91 
93 {
94  // needless to say, we could flesh this class out a bit
95 };
96 
98 {
99  // needless to say, we could flesh this class out a bit
100 };
101 
103 {
104  // needless to say, we could flesh this class out a bit
105 };
106 }
107 
108 /* ---------- input ----------------- */
109 namespace libwps
110 {
111 uint8_t readU8(WPXInputStream *input);
112 uint16_t readU16(WPXInputStream *input);
113 uint32_t readU32(WPXInputStream *input);
114 
115 int8_t read8(WPXInputStream *input);
116 int16_t read16(WPXInputStream *input);
117 int32_t read32(WPXInputStream *input);
118 
119 inline uint8_t readU8(WPXInputStreamPtr &input)
120 {
121  return readU8(input.get());
122 }
123 inline uint16_t readU16(WPXInputStreamPtr &input)
124 {
125  return readU16(input.get());
126 }
127 inline uint32_t readU32(WPXInputStreamPtr &input)
128 {
129  return readU32(input.get());
130 }
131 
132 inline int8_t read8(WPXInputStreamPtr &input)
133 {
134  return read8(input.get());
135 }
136 inline int16_t read16(WPXInputStreamPtr &input)
137 {
138  return read16(input.get());
139 }
140 inline int32_t read32(WPXInputStreamPtr &input)
141 {
142  return read32(input.get());
143 }
144 
145 bool readData(WPXInputStreamPtr &input, unsigned long sz, WPXBinaryData &data);
146 bool readDataToEnd(WPXInputStreamPtr &input, WPXBinaryData &data);
147 }
148 
149 #define WPS_LE_GET_GUINT16(p) \
150  (uint16_t)((((uint8_t const *)(p))[0] << 0) | \
151  (((uint8_t const *)(p))[1] << 8))
152 #define WPS_LE_GET_GUINT32(p) \
153  (uint32_t)((((uint8_t const *)(p))[0] << 0) | \
154  (((uint8_t const *)(p))[1] << 8) | \
155  (((uint8_t const *)(p))[2] << 16) | \
156  (((uint8_t const *)(p))[3] << 24))
157 
158 // Various helper structures for the parser..
159 /* ---------- small enum/class ------------- */
160 class WPXPropertyListVector;
161 struct WPSFont
162 {
164  WPSFont() : m_name(""), m_size(0), m_attributes(0), m_color(0), m_languageId(-1), m_extra("") {}
166  {
167  WPSFont res;
168  res.m_name = "Courier";
169  res.m_size = 12;
170  return res;
171  }
172 
173  virtual ~WPSFont() {}
175  friend std::ostream &operator<<(std::ostream &o, WPSFont const &ft);
176 
178  bool isSet() const
179  {
180  return !m_name.empty();
181  }
182 
184  bool operator==(WPSFont const &ft) const;
185  bool operator!=(WPSFont const &ft) const
186  {
187  return !operator==(ft);
188  }
189 
191  std::string m_name;
193  int m_size;
195  uint32_t m_attributes;
197  uint32_t m_color;
200 
202  std::string m_extra;
203 };
204 
206 {
208  {
209  }
210  double m_width;
211  double m_leftGutter;
213 };
214 
216 {
218  {
219  }
220  uint32_t m_attributes;
221  uint8_t m_alignment;
222 };
223 
224 namespace libwps
225 {
227 std::string numberingTypeToString(NumberingType type);
231  };
232 enum { NoBreakBit = 0x1, NoBreakWithNextBit=0x2};
233 enum { LeftBorderBit = 0x01, RightBorderBit = 0x02, TopBorderBit=0x4,
235  };
237 }
238 
239 // ATTRIBUTE bits
240 #define WPS_EXTRA_LARGE_BIT 1
241 #define WPS_VERY_LARGE_BIT 2
242 #define WPS_LARGE_BIT 4
243 #define WPS_SMALL_PRINT_BIT 8
244 #define WPS_FINE_PRINT_BIT 0x10
245 #define WPS_SUPERSCRIPT_BIT 0x20
246 #define WPS_SUBSCRIPT_BIT 0x40
247 #define WPS_OUTLINE_BIT 0x80
248 #define WPS_ITALICS_BIT 0x100
249 #define WPS_SHADOW_BIT 0x200
250 #define WPS_REDLINE_BIT 0x400
251 #define WPS_DOUBLE_UNDERLINE_BIT 0x800
252 #define WPS_BOLD_BIT 0x1000
253 #define WPS_STRIKEOUT_BIT 0x2000
254 #define WPS_UNDERLINE_BIT 0x4000
255 #define WPS_SMALL_CAPS_BIT 0x8000
256 #define WPS_BLINK_BIT 0x10000L
257 #define WPS_REVERSEVIDEO_BIT 0x20000L
258 #define WPS_ALL_CAPS_BIT 0x40000L
259 #define WPS_EMBOSS_BIT 0x80000L
260 #define WPS_ENGRAVE_BIT 0x100000L
261 #define WPS_OVERLINE_BIT 0x400000L
262 #define WPS_HIDDEN_BIT 0x800000L
263 
264 // BREAK bits
265 #define WPS_PAGE_BREAK 0x00
266 #define WPS_SOFT_PAGE_BREAK 0x01
267 #define WPS_COLUMN_BREAK 0x02
268 
269 // Generic bits
270 #define WPS_LEFT 0x00
271 #define WPS_RIGHT 0x01
272 #define WPS_CENTER 0x02
273 #define WPS_TOP 0x03
274 #define WPS_BOTTOM 0x04
275 
276 /* ---------- vec2/box2f ------------- */
280 template <class T> class Vec2
281 {
282 public:
284  Vec2(T xx=0,T yy=0) : m_x(xx), m_y(yy) { }
286  template <class U> Vec2(Vec2<U> const &p) : m_x(T(p.x())), m_y(T(p.y())) {}
287 
289  T x() const
290  {
291  return m_x;
292  }
294  T y() const
295  {
296  return m_y;
297  }
299  T operator[](int c) const
300  {
301  assert(c >= 0 && c <= 1);
302  return (c==0) ? m_x : m_y;
303  }
305  T &operator[](int c)
306  {
307  assert(c >= 0 && c <= 1);
308  return (c==0) ? m_x : m_y;
309  }
310 
312  void set(T xx, T yy)
313  {
314  m_x = xx;
315  m_y = yy;
316  }
318  void setX(T xx)
319  {
320  m_x = xx;
321  }
323  void setY(T yy)
324  {
325  m_y = yy;
326  }
327 
329  void add(T dx, T dy)
330  {
331  m_x += dx;
332  m_y += dy;
333  }
334 
337  {
338  m_x += p.m_x;
339  m_y += p.m_y;
340  return *this;
341  }
344  {
345  m_x -= p.m_x;
346  m_y -= p.m_y;
347  return *this;
348  }
350  template <class U>
351  Vec2<T> &operator*=(U scale)
352  {
353  m_x = T(m_x*scale);
354  m_y = T(m_y*scale);
355  return *this;
356  }
357 
359  friend Vec2<T> operator+(Vec2<T> const &p1, Vec2<T> const &p2)
360  {
361  Vec2<T> p(p1);
362  return p+=p2;
363  }
365  friend Vec2<T> operator-(Vec2<T> const &p1, Vec2<T> const &p2)
366  {
367  Vec2<T> p(p1);
368  return p-=p2;
369  }
371  template <class U>
372  friend Vec2<T> operator*(U scale, Vec2<T> const &p1)
373  {
374  Vec2<T> p(p1);
375  return p *= scale;
376  }
377 
379  bool operator==(Vec2<T> const &p) const
380  {
381  return cmpY(p) == 0;
382  }
384  bool operator!=(Vec2<T> const &p) const
385  {
386  return cmpY(p) != 0;
387  }
389  bool operator<(Vec2<T> const &p) const
390  {
391  return cmpY(p) < 0;
392  }
394  int cmp(Vec2<T> const &p) const
395  {
396  T diff = m_x-p.m_x;
397  if (diff) return (diff < 0) ? -1 : 1;
398  diff = m_y-p.m_y;
399  if (diff) return (diff < 0) ? -1 : 1;
400  return 0;
401  }
403  int cmpY(Vec2<T> const &p) const
404  {
405  T diff = m_y-p.m_y;
406  if (diff) return (diff < 0) ? -1 : 1;
407  diff = m_x-p.m_x;
408  if (diff) return (diff < 0) ? -1 : 1;
409  return 0;
410  }
411 
413  friend std::ostream &operator<< (std::ostream &o, Vec2<T> const &f)
414  {
415  o << f.m_x << "x" << f.m_y;
416  return o;
417  }
418 
422  struct PosSizeLtX
423  {
425  bool operator()(Vec2<T> const &s1, Vec2<T> const &s2) const
426  {
427  return s1.cmp(s2) < 0;
428  }
429  };
433  typedef std::map<Vec2<T>, T,struct PosSizeLtX> MapX;
434 
438  struct PosSizeLtY
439  {
441  bool operator()(Vec2<T> const &s1, Vec2<T> const &s2) const
442  {
443  return s1.cmpY(s2) < 0;
444  }
445  };
449  typedef std::map<Vec2<T>, T,struct PosSizeLtY> MapY;
450 protected:
451  T m_x, m_y;
452 };
453 
457 typedef Vec2<int> Vec2i;
460 
464 template <class T> class Box2
465 {
466 public:
468  Box2(Vec2<T> minPt=Vec2<T>(), Vec2<T> maxPt=Vec2<T>())
469  {
470  m_pt[0] = minPt;
471  m_pt[1] = maxPt;
472  }
474  template <class U> Box2(Box2<U> const &p)
475  {
476  for (int c=0; c < 2; c++) m_pt[c] = p[c];
477  }
478 
480  Vec2<T> const &min() const
481  {
482  return m_pt[0];
483  }
485  Vec2<T> const &max() const
486  {
487  return m_pt[1];
488  }
491  {
492  return m_pt[0];
493  }
496  {
497  return m_pt[1];
498  }
499 
504  Vec2<T> const &operator[](int c) const
505  {
506  assert(c >= 0 && c <= 1);
507  return m_pt[c];
508  }
510  Vec2<T> size() const
511  {
512  return m_pt[1]-m_pt[0];
513  }
515  Vec2<T> center() const
516  {
517  return 0.5*(m_pt[0]+m_pt[1]);
518  }
519 
521  void set(Vec2<T> const &x, Vec2<T> const &y)
522  {
523  m_pt[0] = x;
524  m_pt[1] = y;
525  }
527  void setMin(Vec2<T> const &x)
528  {
529  m_pt[0] = x;
530  }
532  void setMax(Vec2<T> const &y)
533  {
534  m_pt[1] = y;
535  }
536 
538  void resizeFromMin(Vec2<T> const &sz)
539  {
540  m_pt[1] = m_pt[0]+sz;
541  }
543  void resizeFromMax(Vec2<T> const &sz)
544  {
545  m_pt[0] = m_pt[1]-sz;
546  }
548  void resizeFromCenter(Vec2<T> const &sz)
549  {
550  Vec2<T> ctr = 0.5*(m_pt[0]+m_pt[1]);
551  m_pt[0] = ctr - 0.5*sz;
552  m_pt[1] = ctr + (sz - 0.5*sz);
553  }
554 
556  template <class U> void scale(U factor)
557  {
558  m_pt[0] *= factor;
559  m_pt[1] *= factor;
560  }
561 
563  void extend(T val)
564  {
565  m_pt[0] -= Vec2<T>(val/2,val/2);
566  m_pt[1] += Vec2<T>(val-(val/2),val-(val/2));
567  }
568 
570  bool operator==(Box2<T> const &p) const
571  {
572  return cmp(p) == 0;
573  }
575  bool operator!=(Box2<T> const &p) const
576  {
577  return cmp(p) != 0;
578  }
580  bool operator<(Box2<T> const &p) const
581  {
582  return cmp(p) < 0;
583  }
584 
586  int cmp(Box2<T> const &p) const
587  {
588  int diff = m_pt[0].cmpY(p.m_pt[0]);
589  if (diff) return diff;
590  diff = m_pt[1].cmpY(p.m_pt[1]);
591  if (diff) return diff;
592  return 0;
593  }
594 
596  friend std::ostream &operator<< (std::ostream &o, Box2<T> const &f)
597  {
598  o << "(" << f.m_pt[0] << "<->" << f.m_pt[1] << ")";
599  return o;
600  }
601 
605  struct PosSizeLt
606  {
608  bool operator()(Box2<T> const &s1, Box2<T> const &s2) const
609  {
610  return s1.cmp(s2) < 0;
611  }
612  };
616  typedef std::map<Box2<T>, T,struct PosSizeLt> Map;
617 
618 protected:
621 };
622 
624 typedef Box2<int> Box2i;
627 
628 #endif /* LIBWPS_INTERNAL_H */
629 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */

Generated on Wed Aug 8 2012 16:07:54 for libwps by doxygen 1.8.1.2