libmwaw_internal.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2 
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance (wrlach@gmail.com)
18 * Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net)
19 * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr)
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33 
34 #ifndef LIBMWAW_INTERNAL_H
35 #define LIBMWAW_INTERNAL_H
36 #include <assert.h>
37 #ifdef DEBUG
38 #include <stdio.h>
39 #endif
40 
41 #include <map>
42 #include <ostream>
43 #include <string>
44 #include <math.h>
45 #include <vector>
46 
47 #ifndef M_PI
48 #define M_PI 3.14159265358979323846
49 #endif
50 
51 #include <libwpd-stream/libwpd-stream.h>
52 #include <libwpd/libwpd.h>
53 
54 #if defined(_MSC_VER) || defined(__DJGPP__)
55 
56 typedef signed char int8_t;
57 typedef unsigned char uint8_t;
58 typedef signed short int16_t;
59 typedef unsigned short uint16_t;
60 typedef signed int int32_t;
61 typedef unsigned int uint32_t;
62 typedef unsigned __int64 uint64_t;
63 typedef __int64 int64_t;
64 
65 #else /* !_MSC_VER && !__DJGPP__*/
66 
67 #ifdef HAVE_CONFIG_H
68 
69 #include <config.h>
70 
71 #ifdef HAVE_STDINT_H
72 #include <stdint.h>
73 #endif
74 
75 #ifdef HAVE_INTTYPES_H
76 #include <inttypes.h>
77 #endif
78 
79 #else
80 
81 // assume that the headers are there inside LibreOffice build when no HAVE_CONFIG_H is defined
82 #include <stdint.h>
83 #include <inttypes.h>
84 
85 #endif
86 
87 #endif /* _MSC_VER || __DJGPP__ */
88 
89 /* ---------- memory --------------- */
90 #if defined(SHAREDPTR_TR1)
91 #include <tr1/memory>
92 using std::tr1::shared_ptr;
93 #elif defined(SHAREDPTR_STD)
94 #include <memory>
95 using std::shared_ptr;
96 #else
97 #include <boost/shared_ptr.hpp>
98 using boost::shared_ptr;
99 #endif
100 
102 template <class T>
104  void operator() (T *) {}
105 };
106 
107 /* ---------- debug --------------- */
108 #ifdef DEBUG
109 #define MWAW_DEBUG_MSG(M) printf M
110 #else
111 #define MWAW_DEBUG_MSG(M)
112 #endif
113 
114 namespace libmwaw
115 {
116 // Various exceptions:
118 {
119 };
120 
122 {
123 };
124 
126 {
127 };
128 
130 {
131 };
132 
134 {
135 };
136 }
137 
138 /* ---------- input ----------------- */
139 namespace libmwaw
140 {
141 uint8_t readU8(WPXInputStream *input);
143 void appendUnicode(uint32_t val, WPXString &buffer);
144 }
145 
146 /* ---------- small enum/class ------------- */
147 namespace libmwaw
148 {
150 enum Position { Left = 0, Right = 1, Top = 2, Bottom = 3, HMiddle = 4, VMiddle = 5 };
152 enum { LeftBit = 0x01, RightBit = 0x02, TopBit=0x4, BottomBit = 0x08, HMiddleBit = 0x10, VMiddleBit = 0x20 };
153 
155 std::string numberingTypeToString(NumberingType type);
156 std::string numberingValueToString(NumberingType type, int value);
158 }
159 
161 struct MWAWColor {
163  MWAWColor(uint32_t argb=0) : m_value(argb) {
164  }
166  MWAWColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a=0) :
167  m_value(uint32_t((a<<24)+(r<<16)+(g<<8)+b)) {
168  }
170  MWAWColor &operator=(uint32_t argb) {
171  m_value = argb;
172  return *this;
173  }
175  static MWAWColor black() {
176  return MWAWColor(0);
177  }
179  static MWAWColor white() {
180  return MWAWColor(0xFFFFFF);
181  }
182 
184  static MWAWColor barycenter(float alpha, MWAWColor const &colA,
185  float beta, MWAWColor const &colB);
187  uint32_t value() const {
188  return m_value;
189  }
191  bool isBlack() const {
192  return (m_value&0xFFFFFF)==0;
193  }
195  bool isWhite() const {
196  return (m_value&0xFFFFFF)==0xFFFFFF;
197  }
199  bool operator==(MWAWColor const &c) const {
200  return (c.m_value&0xFFFFFF)==(m_value&0xFFFFFF);
201  }
203  bool operator!=(MWAWColor const &c) const {
204  return !operator==(c);
205  }
207  bool operator<(MWAWColor const &c) const {
208  return (c.m_value&0xFFFFFF)<(m_value&0xFFFFFF);
209  }
211  bool operator<=(MWAWColor const &c) const {
212  return (c.m_value&0xFFFFFF)<=(m_value&0xFFFFFF);
213  }
215  bool operator>(MWAWColor const &c) const {
216  return !operator<=(c);
217  }
219  bool operator>=(MWAWColor const &c) const {
220  return !operator<(c);
221  }
223  friend std::ostream &operator<< (std::ostream &o, MWAWColor const &c);
225  std::string str() const;
226 protected:
228  uint32_t m_value;
229 };
230 
232 struct MWAWBorder {
236  enum Type { Single, Double, Triple };
237 
243  bool addTo(WPXPropertyList &propList, std::string which="") const;
245  bool isEmpty() const {
246  return m_style==None || m_width <= 0;
247  }
249  bool operator==(MWAWBorder const &orig) const {
250  return !operator!=(orig);
251  }
253  bool operator!=(MWAWBorder const &orig) const {
254  return m_style != orig.m_style || m_type != orig.m_type ||
255  m_width < orig.m_width || m_width > orig.m_width || m_color != orig.m_color;
256  }
258  int compare(MWAWBorder const &orig) const;
259 
261  friend std::ostream &operator<< (std::ostream &o, MWAWBorder const &border);
263  friend std::ostream &operator<< (std::ostream &o, MWAWBorder::Style const &style);
269  double m_width;
273  std::vector<double> m_widthsList;
276 };
277 
279 struct MWAWField {
282 
284  MWAWField(Type type) : m_type(type), m_DTFormat(""), m_numberingType(libmwaw::ARABIC), m_data("") {
285  }
289  std::string m_DTFormat;
293  std::string m_data;
294 };
295 
297 struct MWAWNote {
299  enum Type { FootNote, EndNote };
301  MWAWNote(Type type) : m_type(type), m_label(""), m_number(-1) {
302  }
306  WPXString m_label;
308  int m_number;
309 };
310 
311 // forward declarations of basic classes and smart pointers
312 class MWAWEntry;
313 class MWAWFont;
314 class MWAWHeader;
315 class MWAWList;
316 class MWAWPageSpan;
317 class MWAWParagraph;
318 class MWAWParser;
319 class MWAWPosition;
320 class MWAWSection;
321 
322 class MWAWContentListener;
323 class MWAWFontConverter;
324 class MWAWInputStream;
325 class MWAWListManager;
326 class MWAWParserState;
327 class MWAWRSRCParser;
330 typedef shared_ptr<MWAWContentListener> MWAWContentListenerPtr;
332 typedef shared_ptr<MWAWFontConverter> MWAWFontConverterPtr;
334 typedef shared_ptr<MWAWInputStream> MWAWInputStreamPtr;
336 typedef shared_ptr<MWAWListManager> MWAWListManagerPtr;
338 typedef shared_ptr<MWAWRSRCParser> MWAWRSRCParserPtr;
340 typedef shared_ptr<MWAWParserState> MWAWParserStatePtr;
342 typedef shared_ptr<MWAWSubDocument> MWAWSubDocumentPtr;
343 
350 template <class T> struct Variable {
352  Variable() : m_data(), m_set(false) {}
354  Variable(T def) : m_data(def), m_set(false) {}
356  Variable(Variable const &orig) : m_data(orig.m_data), m_set(orig.m_set) {}
358  Variable &operator=(Variable const &orig) {
359  if (this != &orig) {
360  m_data = orig.m_data;
361  m_set = orig.m_set;
362  }
363  return *this;
364  }
366  Variable &operator=(T val) {
367  m_data = val;
368  m_set = true;
369  return *this;
370  }
372  void insert(Variable const &orig) {
373  if (orig.m_set) {
374  m_data = orig.m_data;
375  m_set = orig.m_set;
376  }
377  }
379  T const *operator->() const {
380  return &m_data;
381  }
383  T *operator->() {
384  m_set = true;
385  return &m_data;
386  }
388  T const &operator*() const {
389  return m_data;
390  }
392  T &operator*() {
393  m_set = true;
394  return m_data;
395  }
397  T const &get() const {
398  return m_data;
399  }
401  bool isSet() const {
402  return m_set;
403  }
405  void setSet(bool newVal) {
406  m_set=newVal;
407  }
408 protected:
412  bool m_set;
413 };
414 
415 /* ---------- vec2/box2f ------------- */
419 template <class T> class Vec2
420 {
421 public:
423  Vec2(T xx=0,T yy=0) : m_x(xx), m_y(yy) { }
425  template <class U> Vec2(Vec2<U> const &p) : m_x(T(p.x())), m_y(T(p.y())) {}
426 
428  T x() const {
429  return m_x;
430  }
432  T y() const {
433  return m_y;
434  }
436  T operator[](int c) const {
437  assert(c >= 0 && c <= 1);
438  return (c==0) ? m_x : m_y;
439  }
441  T &operator[](int c) {
442  assert(c >= 0 && c <= 1);
443  return (c==0) ? m_x : m_y;
444  }
445 
447  void set(T xx, T yy) {
448  m_x = xx;
449  m_y = yy;
450  }
452  void setX(T xx) {
453  m_x = xx;
454  }
456  void setY(T yy) {
457  m_y = yy;
458  }
459 
461  void add(T dx, T dy) {
462  m_x += dx;
463  m_y += dy;
464  }
465 
468  m_x += p.m_x;
469  m_y += p.m_y;
470  return *this;
471  }
474  m_x -= p.m_x;
475  m_y -= p.m_y;
476  return *this;
477  }
479  template <class U>
480  Vec2<T> &operator*=(U scale) {
481  m_x = T(m_x*scale);
482  m_y = T(m_y*scale);
483  return *this;
484  }
485 
487  friend Vec2<T> operator+(Vec2<T> const &p1, Vec2<T> const &p2) {
488  Vec2<T> p(p1);
489  return p+=p2;
490  }
492  friend Vec2<T> operator-(Vec2<T> const &p1, Vec2<T> const &p2) {
493  Vec2<T> p(p1);
494  return p-=p2;
495  }
497  template <class U>
498  friend Vec2<T> operator*(U scale, Vec2<T> const &p1) {
499  Vec2<T> p(p1);
500  return p *= scale;
501  }
502 
504  bool operator==(Vec2<T> const &p) const {
505  return cmpY(p) == 0;
506  }
508  bool operator!=(Vec2<T> const &p) const {
509  return cmpY(p) != 0;
510  }
512  bool operator<(Vec2<T> const &p) const {
513  return cmpY(p) < 0;
514  }
516  int cmp(Vec2<T> const &p) const {
517  T diff = m_x-p.m_x;
518  if (diff < 0) return -1;
519  if (diff > 0) return 1;
520  diff = m_y-p.m_y;
521  if (diff < 0) return -1;
522  if (diff > 0) return 1;
523  return 0;
524  }
526  int cmpY(Vec2<T> const &p) const {
527  T diff = m_y-p.m_y;
528  if (diff < 0) return -1;
529  if (diff > 0) return 1;
530  diff = m_x-p.m_x;
531  if (diff < 0) return -1;
532  if (diff > 0) return 1;
533  return 0;
534  }
535 
537  friend std::ostream &operator<< (std::ostream &o, Vec2<T> const &f) {
538  o << f.m_x << "x" << f.m_y;
539  return o;
540  }
541 
545  struct PosSizeLtX {
547  bool operator()(Vec2<T> const &s1, Vec2<T> const &s2) const {
548  return s1.cmp(s2) < 0;
549  }
550  };
554  typedef std::map<Vec2<T>, T,struct PosSizeLtX> MapX;
555 
559  struct PosSizeLtY {
561  bool operator()(Vec2<T> const &s1, Vec2<T> const &s2) const {
562  return s1.cmpY(s2) < 0;
563  }
564  };
568  typedef std::map<Vec2<T>, T,struct PosSizeLtY> MapY;
569 protected:
570  T m_x, m_y;
571 };
572 
576 typedef Vec2<int> Vec2i;
581 
585 template <class T> class Vec3
586 {
587 public:
589  Vec3(T xx=0,T yy=0,T zz=0) {
590  m_val[0] = xx;
591  m_val[1] = yy;
592  m_val[2] = zz;
593  }
595  template <class U> Vec3(Vec3<U> const &p) {
596  for (int c = 0; c < 3; c++) m_val[c] = T(p[c]);
597  }
598 
600  T x() const {
601  return m_val[0];
602  }
604  T y() const {
605  return m_val[1];
606  }
608  T z() const {
609  return m_val[2];
610  }
612  T operator[](int c) const {
613  assert(c >= 0 && c <= 2);
614  return m_val[c];
615  }
617  T &operator[](int c) {
618  assert(c >= 0 && c <= 2);
619  return m_val[c];
620  }
621 
623  void set(T xx, T yy, T zz) {
624  m_val[0] = xx;
625  m_val[1] = yy;
626  m_val[2] = zz;
627  }
629  void setX(T xx) {
630  m_val[0] = xx;
631  }
633  void setY(T yy) {
634  m_val[1] = yy;
635  }
637  void setZ(T zz) {
638  m_val[2] = zz;
639  }
640 
642  void add(T dx, T dy, T dz) {
643  m_val[0] += dx;
644  m_val[1] += dy;
645  m_val[2] += dz;
646  }
647 
650  for (int c = 0; c < 3; c++) m_val[c] = T(m_val[c]+p.m_val[c]);
651  return *this;
652  }
655  for (int c = 0; c < 3; c++) m_val[c] = T(m_val[c]-p.m_val[c]);
656  return *this;
657  }
659  template <class U>
660  Vec3<T> &operator*=(U scale) {
661  for (int c = 0; c < 3; c++) m_val[c] = T(m_val[c]*scale);
662  return *this;
663  }
664 
666  friend Vec3<T> operator+(Vec3<T> const &p1, Vec3<T> const &p2) {
667  Vec3<T> p(p1);
668  return p+=p2;
669  }
671  friend Vec3<T> operator-(Vec3<T> const &p1, Vec3<T> const &p2) {
672  Vec3<T> p(p1);
673  return p-=p2;
674  }
676  template <class U>
677  friend Vec3<T> operator*(U scale, Vec3<T> const &p1) {
678  Vec3<T> p(p1);
679  return p *= scale;
680  }
681 
683  bool operator==(Vec3<T> const &p) const {
684  return cmp(p) == 0;
685  }
687  bool operator!=(Vec3<T> const &p) const {
688  return cmp(p) != 0;
689  }
691  bool operator<(Vec3<T> const &p) const {
692  return cmp(p) < 0;
693  }
695  int cmp(Vec3<T> const &p) const {
696  for (int c = 0; c < 3; c++) {
697  T diff = m_val[c]-p.m_val[c];
698  if (diff) return (diff < 0) ? -1 : 1;
699  }
700  return 0;
701  }
702 
704  friend std::ostream &operator<< (std::ostream &o, Vec3<T> const &f) {
705  o << f.m_val[0] << "x" << f.m_val[1] << "x" << f.m_val[2];
706  return o;
707  }
708 
712  struct PosSizeLt {
714  bool operator()(Vec3<T> const &s1, Vec3<T> const &s2) const {
715  return s1.cmp(s2) < 0;
716  }
717  };
721  typedef std::map<Vec3<T>, T,struct PosSizeLt> Map;
722 
723 protected:
725  T m_val[3];
726 };
727 
731 typedef Vec3<int> Vec3i;
734 
738 template <class T> class Box2
739 {
740 public:
742  Box2(Vec2<T> minPt=Vec2<T>(), Vec2<T> maxPt=Vec2<T>()) {
743  m_pt[0] = minPt;
744  m_pt[1] = maxPt;
745  }
747  template <class U> Box2(Box2<U> const &p) {
748  for (int c=0; c < 2; c++) m_pt[c] = p[c];
749  }
750 
752  Vec2<T> const &min() const {
753  return m_pt[0];
754  }
756  Vec2<T> const &max() const {
757  return m_pt[1];
758  }
761  return m_pt[0];
762  }
765  return m_pt[1];
766  }
771  Vec2<T> const &operator[](int c) const {
772  assert(c >= 0 && c <= 1);
773  return m_pt[c];
774  }
776  Vec2<T> size() const {
777  return m_pt[1]-m_pt[0];
778  }
780  Vec2<T> center() const {
781  return 0.5*(m_pt[0]+m_pt[1]);
782  }
783 
785  void set(Vec2<T> const &x, Vec2<T> const &y) {
786  m_pt[0] = x;
787  m_pt[1] = y;
788  }
790  void setMin(Vec2<T> const &x) {
791  m_pt[0] = x;
792  }
794  void setMax(Vec2<T> const &y) {
795  m_pt[1] = y;
796  }
797 
799  void resizeFromMin(Vec2<T> const &sz) {
800  m_pt[1] = m_pt[0]+sz;
801  }
803  void resizeFromMax(Vec2<T> const &sz) {
804  m_pt[0] = m_pt[1]-sz;
805  }
807  void resizeFromCenter(Vec2<T> const &sz) {
808  Vec2<T> centerPt = 0.5*(m_pt[0]+m_pt[1]);
809  m_pt[0] = centerPt - 0.5*sz;
810  m_pt[1] = centerPt + (sz - 0.5*sz);
811  }
812 
814  template <class U> void scale(U factor) {
815  m_pt[0] *= factor;
816  m_pt[1] *= factor;
817  }
818 
820  void extend(T val) {
821  m_pt[0] -= Vec2<T>(val/2,val/2);
822  m_pt[1] += Vec2<T>(val-(val/2),val-(val/2));
823  }
824 
826  bool operator==(Box2<T> const &p) const {
827  return cmp(p) == 0;
828  }
830  bool operator!=(Box2<T> const &p) const {
831  return cmp(p) != 0;
832  }
834  bool operator<(Box2<T> const &p) const {
835  return cmp(p) < 0;
836  }
837 
839  int cmp(Box2<T> const &p) const {
840  int diff = m_pt[0].cmpY(p.m_pt[0]);
841  if (diff) return diff;
842  diff = m_pt[1].cmpY(p.m_pt[1]);
843  if (diff) return diff;
844  return 0;
845  }
846 
848  friend std::ostream &operator<< (std::ostream &o, Box2<T> const &f) {
849  o << "(" << f.m_pt[0] << "<->" << f.m_pt[1] << ")";
850  return o;
851  }
852 
856  struct PosSizeLt {
858  bool operator()(Box2<T> const &s1, Box2<T> const &s2) const {
859  return s1.cmp(s2) < 0;
860  }
861  };
865  typedef std::map<Box2<T>, T,struct PosSizeLt> Map;
866 
867 protected:
870 };
871 
873 typedef Box2<int> Box2i;
878 
879 #endif /* LIBMWAW_INTERNAL_H */
880 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab:

Generated on Wed Jul 10 2013 18:02:03 for libmwaw by doxygen 1.8.4