- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 * Version: MPL 2.0 / LGPLv2.1+
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * Major Contributor(s):
10 * Copyright (C) 2002 William Lachance (william.lachance@sympatico.ca)
11 * Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net)
12 *
13 * For minor contributions see the git repository.
14 *
15 * Alternatively, the contents of this file may be used under the terms
16 * of the GNU Lesser General Public License Version 2.1 or later
17 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
18 * applicable instead of those above.
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>
69 struct WPS_shared_ptr_noop_deleter
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
87 class VersionException
88 {
89 // needless to say, we could flesh this class out a bit
90 };
91
92 class FileException
93 {
94 // needless to say, we could flesh this class out a bit
95 };
96
97 class ParseException
98 {
99 // needless to say, we could flesh this class out a bit
100 };
101
102 class GenericException
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("") {}
165 static WPSFont getDefault()
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;
199 int m_languageId;
200
202 std::string m_extra;
203 };
204
205 struct WPSColumnDefinition
206 {
207 WPSColumnDefinition() : m_width(0), m_leftGutter(0), m_rightGutter(0)
208 {
209 }
210 double m_width;
211 double m_leftGutter;
212 double m_rightGutter;
213 };
214
215 struct WPSColumnProperties
216 {
217 WPSColumnProperties() : m_attributes(0), m_alignment(0)
218 {
219 }
220 uint32_t m_attributes;
221 uint8_t m_alignment;
222 };
223
225 struct WPSBorder
226 {
227 enum Style { None, Single, Double, Dot, LargeDot, Dash };
228 enum Pos { Left = 0, Right = 1, Top = 2, Bottom = 3 };
229 enum { LeftBit = 0x01, RightBit = 0x02, TopBit=0x4, BottomBit = 0x08 };
230
232 WPSBorder() : m_style(Single), m_width(1), m_color(0) { }
234 std::string getPropertyValue() const;
235
237 bool operator==(WPSBorder const &orig) const
238 {
239 return m_style == orig.m_style && m_width == orig.m_width
240 && m_color == orig.m_color;
241 }
243 bool operator!=(WPSBorder const &orig) const
244 {
245 return !operator==(orig);
246 }
248 int compare(WPSBorder const &orig) const;
249
251 friend std::ostream &operator<< (std::ostream &o, WPSBorder const &border);
253 Style m_style;
255 int m_width;
257 uint32_t m_color;
258
259 };
260
261 namespace libwps
262 {
263 enum NumberingType { NONE, BULLET, ARABIC, LOWERCASE, UPPERCASE, LOWERCASE_ROMAN, UPPERCASE_ROMAN };
264 std::string numberingTypeToString(NumberingType type);
265 enum SubDocumentType { DOC_NONE, DOC_HEADER_FOOTER, DOC_NOTE, DOC_TABLE, DOC_TEXT_BOX, DOC_COMMENT_ANNOTATION };
266 enum Justification { JustificationLeft, JustificationFull, JustificationCenter,
267 JustificationRight, JustificationFullAllLines
268 };
269 enum { NoBreakBit = 0x1, NoBreakWithNextBit=0x2};
270 }
271
272 // ATTRIBUTE bits
273 #define WPS_EXTRA_LARGE_BIT 1
274 #define WPS_VERY_LARGE_BIT 2
275 #define WPS_LARGE_BIT 4
276 #define WPS_SMALL_PRINT_BIT 8
277 #define WPS_FINE_PRINT_BIT 0x10
278 #define WPS_SUPERSCRIPT_BIT 0x20
279 #define WPS_SUBSCRIPT_BIT 0x40
280 #define WPS_OUTLINE_BIT 0x80
281 #define WPS_ITALICS_BIT 0x100
282 #define WPS_SHADOW_BIT 0x200
283 #define WPS_REDLINE_BIT 0x400
284 #define WPS_DOUBLE_UNDERLINE_BIT 0x800
285 #define WPS_BOLD_BIT 0x1000
286 #define WPS_STRIKEOUT_BIT 0x2000
287 #define WPS_UNDERLINE_BIT 0x4000
288 #define WPS_SMALL_CAPS_BIT 0x8000
289 #define WPS_BLINK_BIT 0x10000L
290 #define WPS_REVERSEVIDEO_BIT 0x20000L
291 #define WPS_ALL_CAPS_BIT 0x40000L
292 #define WPS_EMBOSS_BIT 0x80000L
293 #define WPS_ENGRAVE_BIT 0x100000L
294 #define WPS_OVERLINE_BIT 0x400000L
295 #define WPS_HIDDEN_BIT 0x800000L
296
297 // BREAK bits
298 #define WPS_PAGE_BREAK 0x00
299 #define WPS_SOFT_PAGE_BREAK 0x01
300 #define WPS_COLUMN_BREAK 0x02
301
302 // Generic bits
303 #define WPS_LEFT 0x00
304 #define WPS_RIGHT 0x01
305 #define WPS_CENTER 0x02
306 #define WPS_TOP 0x03
307 #define WPS_BOTTOM 0x04
308
309 /* ---------- vec2/box2f ------------- */
313 template <class T> class Vec2
314 {
315 public:
317 Vec2(T xx=0,T yy=0) : m_x(xx), m_y(yy) { }
319 template <class U> Vec2(Vec2<U> const &p) : m_x(T(p.x())), m_y(T(p.y())) {}
320
322 T x() const
323 {
324 return m_x;
325 }
327 T y() const
328 {
329 return m_y;
330 }
332 T operator[](int c) const
333 {
334 assert(c >= 0 && c <= 1);
335 return (c==0) ? m_x : m_y;
336 }
338 T &operator[](int c)
339 {
340 assert(c >= 0 && c <= 1);
341 return (c==0) ? m_x : m_y;
342 }
343
345 void set(T xx, T yy)
346 {
347 m_x = xx;
348 m_y = yy;
349 }
351 void setX(T xx)
352 {
353 m_x = xx;
354 }
356 void setY(T yy)
357 {
358 m_y = yy;
359 }
360
362 void add(T dx, T dy)
363 {
364 m_x += dx;
365 m_y += dy;
366 }
367
369 Vec2<T> &operator+=(Vec2<T> const &p)
370 {
371 m_x += p.m_x;
372 m_y += p.m_y;
373 return *this;
374 }
376 Vec2<T> &operator-=(Vec2<T> const &p)
377 {
378 m_x -= p.m_x;
379 m_y -= p.m_y;
380 return *this;
381 }
383 template <class U>
384 Vec2<T> &operator*=(U scale)
385 {
386 m_x = T(m_x*scale);
387 m_y = T(m_y*scale);
388 return *this;
389 }
390
392 friend Vec2<T> operator+(Vec2<T> const &p1, Vec2<T> const &p2)
393 {
394 Vec2<T> p(p1);
395 return p+=p2;
396 }
398 friend Vec2<T> operator-(Vec2<T> const &p1, Vec2<T> const &p2)
399 {
400 Vec2<T> p(p1);
401 return p-=p2;
402 }
404 template <class U>
405 friend Vec2<T> operator*(U scale, Vec2<T> const &p1)
406 {
407 Vec2<T> p(p1);
408 return p *= scale;
409 }
410
412 bool operator==(Vec2<T> const &p) const
413 {
414 return cmpY(p) == 0;
415 }
417 bool operator!=(Vec2<T> const &p) const
418 {
419 return cmpY(p) != 0;
420 }
422 bool operator<(Vec2<T> const &p) const
423 {
424 return cmpY(p) < 0;
425 }
427 int cmp(Vec2<T> const &p) const
428 {
429 T diff = m_x-p.m_x;
430 if (diff < 0) return -1;
431 if (diff > 0) return 1;
432 diff = m_y-p.m_y;
433 if (diff < 0) return -1;
434 if (diff > 0) return 1;
435 return 0;
436 }
438 int cmpY(Vec2<T> const &p) const
439 {
440 T diff = m_y-p.m_y;
441 if (diff < 0) return -1;
442 if (diff > 0) return 1;
443 diff = m_x-p.m_x;
444 if (diff < 0) return -1;
445 if (diff > 0) return 1;
446 return 0;
447 }
448
450 friend std::ostream &operator<< (std::ostream &o, Vec2<T> const &f)
451 {
452 o << f.m_x << "x" << f.m_y;
453 return o;
454 }
455
459 struct PosSizeLtX
460 {
462 bool operator()(Vec2<T> const &s1, Vec2<T> const &s2) const
463 {
464 return s1.cmp(s2) < 0;
465 }
466 };
470 typedef std::map<Vec2<T>, T,struct PosSizeLtX> MapX;
471
475 struct PosSizeLtY
476 {
478 bool operator()(Vec2<T> const &s1, Vec2<T> const &s2) const
479 {
480 return s1.cmpY(s2) < 0;
481 }
482 };
486 typedef std::map<Vec2<T>, T,struct PosSizeLtY> MapY;
487 protected:
488 T m_x, m_y;
489 };
490
492 typedef Vec2<bool> Vec2b;
494 typedef Vec2<int> Vec2i;
496 typedef Vec2<float> Vec2f;
497
501 template <class T> class Box2
502 {
503 public:
505 Box2(Vec2<T> minPt=Vec2<T>(), Vec2<T> maxPt=Vec2<T>())
506 {
507 m_pt[0] = minPt;
508 m_pt[1] = maxPt;
509 }
511 template <class U> Box2(Box2<U> const &p)
512 {
513 for (int c=0; c < 2; c++) m_pt[c] = p[c];
514 }
515
517 Vec2<T> const &min() const
518 {
519 return m_pt[0];
520 }
522 Vec2<T> const &max() const
523 {
524 return m_pt[1];
525 }
527 Vec2<T> &min()
528 {
529 return m_pt[0];
530 }
532 Vec2<T> &max()
533 {
534 return m_pt[1];
535 }
536
541 Vec2<T> const &operator[](int c) const
542 {
543 assert(c >= 0 && c <= 1);
544 return m_pt[c];
545 }
547 Vec2<T> size() const
548 {
549 return m_pt[1]-m_pt[0];
550 }
552 Vec2<T> center() const
553 {
554 return 0.5*(m_pt[0]+m_pt[1]);
555 }
556
558 void set(Vec2<T> const &x, Vec2<T> const &y)
559 {
560 m_pt[0] = x;
561 m_pt[1] = y;
562 }
564 void setMin(Vec2<T> const &x)
565 {
566 m_pt[0] = x;
567 }
569 void setMax(Vec2<T> const &y)
570 {
571 m_pt[1] = y;
572 }
573
575 void resizeFromMin(Vec2<T> const &sz)
576 {
577 m_pt[1] = m_pt[0]+sz;
578 }
580 void resizeFromMax(Vec2<T> const &sz)
581 {
582 m_pt[0] = m_pt[1]-sz;
583 }
585 void resizeFromCenter(Vec2<T> const &sz)
586 {
587 Vec2<T> ctr = 0.5*(m_pt[0]+m_pt[1]);
588 m_pt[0] = ctr - 0.5*sz;
589 m_pt[1] = ctr + (sz - 0.5*sz);
590 }
591
593 template <class U> void scale(U factor)
594 {
595 m_pt[0] *= factor;
596 m_pt[1] *= factor;
597 }
598
600 void extend(T val)
601 {
602 m_pt[0] -= Vec2<T>(val/2,val/2);
603 m_pt[1] += Vec2<T>(val-(val/2),val-(val/2));
604 }
605
607 bool operator==(Box2<T> const &p) const
608 {
609 return cmp(p) == 0;
610 }
612 bool operator!=(Box2<T> const &p) const
613 {
614 return cmp(p) != 0;
615 }
617 bool operator<(Box2<T> const &p) const
618 {
619 return cmp(p) < 0;
620 }
621
623 int cmp(Box2<T> const &p) const
624 {
625 int diff = m_pt[0].cmpY(p.m_pt[0]);
626 if (diff) return diff;
627 diff = m_pt[1].cmpY(p.m_pt[1]);
628 if (diff) return diff;
629 return 0;
630 }
631
633 friend std::ostream &operator<< (std::ostream &o, Box2<T> const &f)
634 {
635 o << "(" << f.m_pt[0] << "<->" << f.m_pt[1] << ")";
636 return o;
637 }
638
642 struct PosSizeLt
643 {
645 bool operator()(Box2<T> const &s1, Box2<T> const &s2) const
646 {
647 return s1.cmp(s2) < 0;
648 }
649 };
653 typedef std::map<Box2<T>, T,struct PosSizeLt> Map;
654
655 protected:
657 Vec2<T> m_pt[2];
658 };
659
661 typedef Box2<int> Box2i;
663 typedef Box2<float> Box2f;
664
665 #endif /* LIBWPS_INTERNAL_H */
666 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */