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 # ifdef HAVE_STDINT_H
71 # include <stdint.h>
72 # endif
73 # ifdef HAVE_INTTYPES_H
74 # include <inttypes.h>
75 # endif
76 
77 # else
78 
79 // assume that the headers are there inside LibreOffice build when no HAVE_CONFIG_H is defined
80 # include <stdint.h>
81 # include <inttypes.h>
82 
83 # endif
84 
85 #endif /* _MSC_VER || __DJGPP__ */
86 
87 // define gmtime_r and localtime_r on Windows, so that can use
88 // thread-safe functions on other environments
89 #ifdef _WIN32
90 # define gmtime_r(tp,tmp) (gmtime(tp)?(*(tmp)=*gmtime(tp),(tmp)):0)
91 # define localtime_r(tp,tmp) (localtime(tp)?(*(tmp)=*localtime(tp),(tmp)):0)
92 #endif
93 
94 /* ---------- memory --------------- */
95 #if defined(SHAREDPTR_TR1)
96 #include <tr1/memory>
97 using std::tr1::shared_ptr;
98 #elif defined(SHAREDPTR_STD)
99 #include <memory>
100 using std::shared_ptr;
101 #else
102 #include <boost/shared_ptr.hpp>
103 using boost::shared_ptr;
104 #endif
105 
107 template <class T>
109  void operator() (T *) {}
110 };
111 
112 /* ---------- debug --------------- */
113 #ifdef DEBUG
114 #define MWAW_DEBUG_MSG(M) printf M
115 #else
116 #define MWAW_DEBUG_MSG(M)
117 #endif
118 
119 namespace libmwaw
120 {
121 // Various exceptions:
123 {
124 };
125 
127 {
128 };
129 
131 {
132 };
133 
135 {
136 };
137 
139 {
140 };
141 }
142 
143 /* ---------- input ----------------- */
144 namespace libmwaw
145 {
146 uint8_t readU8(WPXInputStream *input);
148 void appendUnicode(uint32_t val, WPXString &buffer);
149 }
150 
151 /* ---------- small enum/class ------------- */
152 namespace libmwaw
153 {
155 enum Position { Left = 0, Right = 1, Top = 2, Bottom = 3, HMiddle = 4, VMiddle = 5 };
157 enum { LeftBit = 0x01, RightBit = 0x02, TopBit=0x4, BottomBit = 0x08, HMiddleBit = 0x10, VMiddleBit = 0x20 };
158 
160 std::string numberingTypeToString(NumberingType type);
161 std::string numberingValueToString(NumberingType type, int value);
163 }
164 
166 struct MWAWColor {
168  MWAWColor(uint32_t argb=0) : m_value(argb) {
169  }
171  MWAWColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a=0) :
172  m_value(uint32_t((a<<24)+(r<<16)+(g<<8)+b)) {
173  }
175  MWAWColor &operator=(uint32_t argb) {
176  m_value = argb;
177  return *this;
178  }
180  static MWAWColor black() {
181  return MWAWColor(0);
182  }
184  static MWAWColor white() {
185  return MWAWColor(0xFFFFFF);
186  }
187 
189  static MWAWColor barycenter(float alpha, MWAWColor const &colA,
190  float beta, MWAWColor const &colB);
192  uint32_t value() const {
193  return m_value;
194  }
196  bool isBlack() const {
197  return (m_value&0xFFFFFF)==0;
198  }
200  bool isWhite() const {
201  return (m_value&0xFFFFFF)==0xFFFFFF;
202  }
204  bool operator==(MWAWColor const &c) const {
205  return (c.m_value&0xFFFFFF)==(m_value&0xFFFFFF);
206  }
208  bool operator!=(MWAWColor const &c) const {
209  return !operator==(c);
210  }
212  bool operator<(MWAWColor const &c) const {
213  return (c.m_value&0xFFFFFF)<(m_value&0xFFFFFF);
214  }
216  bool operator<=(MWAWColor const &c) const {
217  return (c.m_value&0xFFFFFF)<=(m_value&0xFFFFFF);
218  }
220  bool operator>(MWAWColor const &c) const {
221  return !operator<=(c);
222  }
224  bool operator>=(MWAWColor const &c) const {
225  return !operator<(c);
226  }
228  friend std::ostream &operator<< (std::ostream &o, MWAWColor const &c);
230  std::string str() const;
231 protected:
233  uint32_t m_value;
234 };
235 
237 struct MWAWBorder {
241  enum Type { Single, Double, Triple };
242 
248  bool addTo(WPXPropertyList &propList, std::string which="") const;
250  bool isEmpty() const {
251  return m_style==None || m_width <= 0;
252  }
254  bool operator==(MWAWBorder const &orig) const {
255  return !operator!=(orig);
256  }
258  bool operator!=(MWAWBorder const &orig) const {
259  return m_style != orig.m_style || m_type != orig.m_type ||
260  m_width < orig.m_width || m_width > orig.m_width || m_color != orig.m_color;
261  }
263  int compare(MWAWBorder const &orig) const;
264 
266  friend std::ostream &operator<< (std::ostream &o, MWAWBorder const &border);
268  friend std::ostream &operator<< (std::ostream &o, MWAWBorder::Style const &style);
274  double m_width;
278  std::vector<double> m_widthsList;
282  std::string m_extra;
283 };
284 
286 struct MWAWField {
289 
291  MWAWField(Type type) : m_type(type), m_DTFormat(""), m_numberingType(libmwaw::ARABIC), m_data("") {
292  }
296  std::string m_DTFormat;
300  std::string m_data;
301 };
302 
304 struct MWAWNote {
306  enum Type { FootNote, EndNote };
308  MWAWNote(Type type) : m_type(type), m_label(""), m_number(-1) {
309  }
313  WPXString m_label;
315  int m_number;
316 };
317 
318 // forward declarations of basic classes and smart pointers
319 class MWAWEntry;
320 class MWAWFont;
322 class MWAWGraphicShape;
323 class MWAWGraphicStyle;
324 class MWAWHeader;
325 class MWAWList;
326 class MWAWPageSpan;
327 class MWAWParagraph;
328 class MWAWParser;
329 class MWAWPosition;
330 class MWAWSection;
331 
332 class MWAWContentListener;
333 class MWAWFontConverter;
334 class MWAWGraphicListener;
335 class MWAWInputStream;
336 class MWAWListener;
337 class MWAWListManager;
338 class MWAWParserState;
339 class MWAWRSRCParser;
342 typedef shared_ptr<MWAWContentListener> MWAWContentListenerPtr;
344 typedef shared_ptr<MWAWFontConverter> MWAWFontConverterPtr;
346 typedef shared_ptr<MWAWGraphicListener> MWAWGraphicListenerPtr;
348 typedef shared_ptr<MWAWInputStream> MWAWInputStreamPtr;
350 typedef shared_ptr<MWAWListener> MWAWListenerPtr;
352 typedef shared_ptr<MWAWListManager> MWAWListManagerPtr;
354 typedef shared_ptr<MWAWRSRCParser> MWAWRSRCParserPtr;
356 typedef shared_ptr<MWAWParserState> MWAWParserStatePtr;
358 typedef shared_ptr<MWAWSubDocument> MWAWSubDocumentPtr;
359 
366 template <class T> struct Variable {
368  Variable() : m_data(), m_set(false) {}
370  Variable(T def) : m_data(def), m_set(false) {}
372  Variable(Variable const &orig) : m_data(orig.m_data), m_set(orig.m_set) {}
374  Variable &operator=(Variable const &orig) {
375  if (this != &orig) {
376  m_data = orig.m_data;
377  m_set = orig.m_set;
378  }
379  return *this;
380  }
382  Variable &operator=(T val) {
383  m_data = val;
384  m_set = true;
385  return *this;
386  }
388  void insert(Variable const &orig) {
389  if (orig.m_set) {
390  m_data = orig.m_data;
391  m_set = orig.m_set;
392  }
393  }
395  T const *operator->() const {
396  return &m_data;
397  }
399  T *operator->() {
400  m_set = true;
401  return &m_data;
402  }
404  T const &operator*() const {
405  return m_data;
406  }
408  T &operator*() {
409  m_set = true;
410  return m_data;
411  }
413  T const &get() const {
414  return m_data;
415  }
417  bool isSet() const {
418  return m_set;
419  }
421  void setSet(bool newVal) {
422  m_set=newVal;
423  }
424 protected:
428  bool m_set;
429 };
430 
431 /* ---------- vec2/box2f ------------- */
435 template <class T> class Vec2
436 {
437 public:
439  Vec2(T xx=0,T yy=0) : m_x(xx), m_y(yy) { }
441  template <class U> Vec2(Vec2<U> const &p) : m_x(T(p.x())), m_y(T(p.y())) {}
442 
444  T x() const {
445  return m_x;
446  }
448  T y() const {
449  return m_y;
450  }
452  T operator[](int c) const {
453  assert(c >= 0 && c <= 1);
454  return (c==0) ? m_x : m_y;
455  }
457  T &operator[](int c) {
458  assert(c >= 0 && c <= 1);
459  return (c==0) ? m_x : m_y;
460  }
461 
463  void set(T xx, T yy) {
464  m_x = xx;
465  m_y = yy;
466  }
468  void setX(T xx) {
469  m_x = xx;
470  }
472  void setY(T yy) {
473  m_y = yy;
474  }
475 
477  void add(T dx, T dy) {
478  m_x += dx;
479  m_y += dy;
480  }
481 
484  m_x += p.m_x;
485  m_y += p.m_y;
486  return *this;
487  }
490  m_x -= p.m_x;
491  m_y -= p.m_y;
492  return *this;
493  }
495  template <class U>
496  Vec2<T> &operator*=(U scale) {
497  m_x = T(m_x*scale);
498  m_y = T(m_y*scale);
499  return *this;
500  }
501 
503  friend Vec2<T> operator+(Vec2<T> const &p1, Vec2<T> const &p2) {
504  Vec2<T> p(p1);
505  return p+=p2;
506  }
508  friend Vec2<T> operator-(Vec2<T> const &p1, Vec2<T> const &p2) {
509  Vec2<T> p(p1);
510  return p-=p2;
511  }
513  template <class U>
514  friend Vec2<T> operator*(U scale, Vec2<T> const &p1) {
515  Vec2<T> p(p1);
516  return p *= scale;
517  }
518 
520  bool operator==(Vec2<T> const &p) const {
521  return cmpY(p) == 0;
522  }
524  bool operator!=(Vec2<T> const &p) const {
525  return cmpY(p) != 0;
526  }
528  bool operator<(Vec2<T> const &p) const {
529  return cmpY(p) < 0;
530  }
532  int cmp(Vec2<T> const &p) const {
533  T diff = m_x-p.m_x;
534  if (diff < 0) return -1;
535  if (diff > 0) return 1;
536  diff = m_y-p.m_y;
537  if (diff < 0) return -1;
538  if (diff > 0) return 1;
539  return 0;
540  }
542  int cmpY(Vec2<T> const &p) const {
543  T diff = m_y-p.m_y;
544  if (diff < 0) return -1;
545  if (diff > 0) return 1;
546  diff = m_x-p.m_x;
547  if (diff < 0) return -1;
548  if (diff > 0) return 1;
549  return 0;
550  }
551 
553  friend std::ostream &operator<< (std::ostream &o, Vec2<T> const &f) {
554  o << f.m_x << "x" << f.m_y;
555  return o;
556  }
557 
561  struct PosSizeLtX {
563  bool operator()(Vec2<T> const &s1, Vec2<T> const &s2) const {
564  return s1.cmp(s2) < 0;
565  }
566  };
570  typedef std::map<Vec2<T>, T,struct PosSizeLtX> MapX;
571 
575  struct PosSizeLtY {
577  bool operator()(Vec2<T> const &s1, Vec2<T> const &s2) const {
578  return s1.cmpY(s2) < 0;
579  }
580  };
584  typedef std::map<Vec2<T>, T,struct PosSizeLtY> MapY;
585 protected:
586  T m_x, m_y;
587 };
588 
592 typedef Vec2<int> Vec2i;
597 
601 template <class T> class Vec3
602 {
603 public:
605  Vec3(T xx=0,T yy=0,T zz=0) {
606  m_val[0] = xx;
607  m_val[1] = yy;
608  m_val[2] = zz;
609  }
611  template <class U> Vec3(Vec3<U> const &p) {
612  for (int c = 0; c < 3; c++) m_val[c] = T(p[c]);
613  }
614 
616  T x() const {
617  return m_val[0];
618  }
620  T y() const {
621  return m_val[1];
622  }
624  T z() const {
625  return m_val[2];
626  }
628  T operator[](int c) const {
629  assert(c >= 0 && c <= 2);
630  return m_val[c];
631  }
633  T &operator[](int c) {
634  assert(c >= 0 && c <= 2);
635  return m_val[c];
636  }
637 
639  void set(T xx, T yy, T zz) {
640  m_val[0] = xx;
641  m_val[1] = yy;
642  m_val[2] = zz;
643  }
645  void setX(T xx) {
646  m_val[0] = xx;
647  }
649  void setY(T yy) {
650  m_val[1] = yy;
651  }
653  void setZ(T zz) {
654  m_val[2] = zz;
655  }
656 
658  void add(T dx, T dy, T dz) {
659  m_val[0] += dx;
660  m_val[1] += dy;
661  m_val[2] += dz;
662  }
663 
666  for (int c = 0; c < 3; c++) m_val[c] = T(m_val[c]+p.m_val[c]);
667  return *this;
668  }
671  for (int c = 0; c < 3; c++) m_val[c] = T(m_val[c]-p.m_val[c]);
672  return *this;
673  }
675  template <class U>
676  Vec3<T> &operator*=(U scale) {
677  for (int c = 0; c < 3; c++) m_val[c] = T(m_val[c]*scale);
678  return *this;
679  }
680 
682  friend Vec3<T> operator+(Vec3<T> const &p1, Vec3<T> const &p2) {
683  Vec3<T> p(p1);
684  return p+=p2;
685  }
687  friend Vec3<T> operator-(Vec3<T> const &p1, Vec3<T> const &p2) {
688  Vec3<T> p(p1);
689  return p-=p2;
690  }
692  template <class U>
693  friend Vec3<T> operator*(U scale, Vec3<T> const &p1) {
694  Vec3<T> p(p1);
695  return p *= scale;
696  }
697 
699  bool operator==(Vec3<T> const &p) const {
700  return cmp(p) == 0;
701  }
703  bool operator!=(Vec3<T> const &p) const {
704  return cmp(p) != 0;
705  }
707  bool operator<(Vec3<T> const &p) const {
708  return cmp(p) < 0;
709  }
711  int cmp(Vec3<T> const &p) const {
712  for (int c = 0; c < 3; c++) {
713  T diff = m_val[c]-p.m_val[c];
714  if (diff) return (diff < 0) ? -1 : 1;
715  }
716  return 0;
717  }
718 
720  friend std::ostream &operator<< (std::ostream &o, Vec3<T> const &f) {
721  o << f.m_val[0] << "x" << f.m_val[1] << "x" << f.m_val[2];
722  return o;
723  }
724 
728  struct PosSizeLt {
730  bool operator()(Vec3<T> const &s1, Vec3<T> const &s2) const {
731  return s1.cmp(s2) < 0;
732  }
733  };
737  typedef std::map<Vec3<T>, T,struct PosSizeLt> Map;
738 
739 protected:
741  T m_val[3];
742 };
743 
747 typedef Vec3<int> Vec3i;
750 
754 template <class T> class Box2
755 {
756 public:
758  Box2(Vec2<T> minPt=Vec2<T>(), Vec2<T> maxPt=Vec2<T>()) {
759  m_pt[0] = minPt;
760  m_pt[1] = maxPt;
761  }
763  template <class U> Box2(Box2<U> const &p) {
764  for (int c=0; c < 2; c++) m_pt[c] = p[c];
765  }
766 
768  Vec2<T> const &min() const {
769  return m_pt[0];
770  }
772  Vec2<T> const &max() const {
773  return m_pt[1];
774  }
777  return m_pt[0];
778  }
781  return m_pt[1];
782  }
787  Vec2<T> const &operator[](int c) const {
788  assert(c >= 0 && c <= 1);
789  return m_pt[c];
790  }
792  Vec2<T> size() const {
793  return m_pt[1]-m_pt[0];
794  }
796  Vec2<T> center() const {
797  return 0.5*(m_pt[0]+m_pt[1]);
798  }
799 
801  void set(Vec2<T> const &x, Vec2<T> const &y) {
802  m_pt[0] = x;
803  m_pt[1] = y;
804  }
806  void setMin(Vec2<T> const &x) {
807  m_pt[0] = x;
808  }
810  void setMax(Vec2<T> const &y) {
811  m_pt[1] = y;
812  }
813 
815  void resizeFromMin(Vec2<T> const &sz) {
816  m_pt[1] = m_pt[0]+sz;
817  }
819  void resizeFromMax(Vec2<T> const &sz) {
820  m_pt[0] = m_pt[1]-sz;
821  }
823  void resizeFromCenter(Vec2<T> const &sz) {
824  Vec2<T> centerPt = 0.5*(m_pt[0]+m_pt[1]);
825  m_pt[0] = centerPt - 0.5*sz;
826  m_pt[1] = centerPt + (sz - 0.5*sz);
827  }
828 
830  template <class U> void scale(U factor) {
831  m_pt[0] *= factor;
832  m_pt[1] *= factor;
833  }
834 
836  void extend(T val) {
837  m_pt[0] -= Vec2<T>(val/2,val/2);
838  m_pt[1] += Vec2<T>(val-(val/2),val-(val/2));
839  }
840 
842  Box2<T> getUnion(Box2<T> const &box) const {
843  Box2<T> res;
844  res.m_pt[0]=Vec2<T>(m_pt[0][0]<box.m_pt[0][0]?m_pt[0][0] : box.m_pt[0][0],
845  m_pt[0][1]<box.m_pt[0][1]?m_pt[0][1] : box.m_pt[0][1]);
846  res.m_pt[1]=Vec2<T>(m_pt[1][0]>box.m_pt[1][0]?m_pt[1][0] : box.m_pt[1][0],
847  m_pt[1][1]>box.m_pt[1][1]?m_pt[1][1] : box.m_pt[1][1]);
848  return res;
849  }
851  Box2<T> getIntersection(Box2<T> const &box) const {
852  Box2<T> res;
853  res.m_pt[0]=Vec2<T>(m_pt[0][0]>box.m_pt[0][0]?m_pt[0][0] : box.m_pt[0][0],
854  m_pt[0][1]>box.m_pt[0][1]?m_pt[0][1] : box.m_pt[0][1]);
855  res.m_pt[1]=Vec2<T>(m_pt[1][0]<box.m_pt[1][0]?m_pt[1][0] : box.m_pt[1][0],
856  m_pt[1][1]<box.m_pt[1][1]?m_pt[1][1] : box.m_pt[1][1]);
857  return res;
858  }
860  bool operator==(Box2<T> const &p) const {
861  return cmp(p) == 0;
862  }
864  bool operator!=(Box2<T> const &p) const {
865  return cmp(p) != 0;
866  }
868  bool operator<(Box2<T> const &p) const {
869  return cmp(p) < 0;
870  }
871 
873  int cmp(Box2<T> const &p) const {
874  int diff = m_pt[0].cmpY(p.m_pt[0]);
875  if (diff) return diff;
876  diff = m_pt[1].cmpY(p.m_pt[1]);
877  if (diff) return diff;
878  return 0;
879  }
880 
882  friend std::ostream &operator<< (std::ostream &o, Box2<T> const &f) {
883  o << "(" << f.m_pt[0] << "<->" << f.m_pt[1] << ")";
884  return o;
885  }
886 
890  struct PosSizeLt {
892  bool operator()(Box2<T> const &s1, Box2<T> const &s2) const {
893  return s1.cmp(s2) < 0;
894  }
895  };
899  typedef std::map<Box2<T>, T,struct PosSizeLt> Map;
900 
901 protected:
904 };
905 
907 typedef Box2<int> Box2i;
912 
913 // some geometrical function
914 namespace libmwaw
915 {
917 Box2f rotateBoxFromCenter(Box2f const &box, float angle);
918 }
919 #endif /* LIBMWAW_INTERNAL_H */
920 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab:

Generated on Thu Apr 24 2014 14:47:41 for libmwaw by doxygen 1.8.3.1