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;
277  std::string m_extra;
278 };
279 
281 struct MWAWField {
284 
286  MWAWField(Type type) : m_type(type), m_DTFormat(""), m_numberingType(libmwaw::ARABIC), m_data("") {
287  }
291  std::string m_DTFormat;
295  std::string m_data;
296 };
297 
299 struct MWAWNote {
301  enum Type { FootNote, EndNote };
303  MWAWNote(Type type) : m_type(type), m_label(""), m_number(-1) {
304  }
308  WPXString m_label;
310  int m_number;
311 };
312 
313 // forward declarations of basic classes and smart pointers
314 class MWAWEntry;
315 class MWAWFont;
316 class MWAWHeader;
317 class MWAWList;
318 class MWAWPageSpan;
319 class MWAWParagraph;
320 class MWAWParser;
321 class MWAWPosition;
322 class MWAWSection;
323 
324 class MWAWContentListener;
325 class MWAWFontConverter;
326 class MWAWInputStream;
327 class MWAWListManager;
328 class MWAWParserState;
329 class MWAWRSRCParser;
332 typedef shared_ptr<MWAWContentListener> MWAWContentListenerPtr;
334 typedef shared_ptr<MWAWFontConverter> MWAWFontConverterPtr;
336 typedef shared_ptr<MWAWInputStream> MWAWInputStreamPtr;
338 typedef shared_ptr<MWAWListManager> MWAWListManagerPtr;
340 typedef shared_ptr<MWAWRSRCParser> MWAWRSRCParserPtr;
342 typedef shared_ptr<MWAWParserState> MWAWParserStatePtr;
344 typedef shared_ptr<MWAWSubDocument> MWAWSubDocumentPtr;
345 
352 template <class T> struct Variable {
354  Variable() : m_data(), m_set(false) {}
356  Variable(T def) : m_data(def), m_set(false) {}
358  Variable(Variable const &orig) : m_data(orig.m_data), m_set(orig.m_set) {}
360  Variable &operator=(Variable const &orig) {
361  if (this != &orig) {
362  m_data = orig.m_data;
363  m_set = orig.m_set;
364  }
365  return *this;
366  }
368  Variable &operator=(T val) {
369  m_data = val;
370  m_set = true;
371  return *this;
372  }
374  void insert(Variable const &orig) {
375  if (orig.m_set) {
376  m_data = orig.m_data;
377  m_set = orig.m_set;
378  }
379  }
381  T const *operator->() const {
382  return &m_data;
383  }
385  T *operator->() {
386  m_set = true;
387  return &m_data;
388  }
390  T const &operator*() const {
391  return m_data;
392  }
394  T &operator*() {
395  m_set = true;
396  return m_data;
397  }
399  T const &get() const {
400  return m_data;
401  }
403  bool isSet() const {
404  return m_set;
405  }
407  void setSet(bool newVal) {
408  m_set=newVal;
409  }
410 protected:
414  bool m_set;
415 };
416 
417 /* ---------- vec2/box2f ------------- */
421 template <class T> class Vec2
422 {
423 public:
425  Vec2(T xx=0,T yy=0) : m_x(xx), m_y(yy) { }
427  template <class U> Vec2(Vec2<U> const &p) : m_x(T(p.x())), m_y(T(p.y())) {}
428 
430  T x() const {
431  return m_x;
432  }
434  T y() const {
435  return m_y;
436  }
438  T operator[](int c) const {
439  assert(c >= 0 && c <= 1);
440  return (c==0) ? m_x : m_y;
441  }
443  T &operator[](int c) {
444  assert(c >= 0 && c <= 1);
445  return (c==0) ? m_x : m_y;
446  }
447 
449  void set(T xx, T yy) {
450  m_x = xx;
451  m_y = yy;
452  }
454  void setX(T xx) {
455  m_x = xx;
456  }
458  void setY(T yy) {
459  m_y = yy;
460  }
461 
463  void add(T dx, T dy) {
464  m_x += dx;
465  m_y += dy;
466  }
467 
470  m_x += p.m_x;
471  m_y += p.m_y;
472  return *this;
473  }
476  m_x -= p.m_x;
477  m_y -= p.m_y;
478  return *this;
479  }
481  template <class U>
482  Vec2<T> &operator*=(U scale) {
483  m_x = T(m_x*scale);
484  m_y = T(m_y*scale);
485  return *this;
486  }
487 
489  friend Vec2<T> operator+(Vec2<T> const &p1, Vec2<T> const &p2) {
490  Vec2<T> p(p1);
491  return p+=p2;
492  }
494  friend Vec2<T> operator-(Vec2<T> const &p1, Vec2<T> const &p2) {
495  Vec2<T> p(p1);
496  return p-=p2;
497  }
499  template <class U>
500  friend Vec2<T> operator*(U scale, Vec2<T> const &p1) {
501  Vec2<T> p(p1);
502  return p *= scale;
503  }
504 
506  bool operator==(Vec2<T> const &p) const {
507  return cmpY(p) == 0;
508  }
510  bool operator!=(Vec2<T> const &p) const {
511  return cmpY(p) != 0;
512  }
514  bool operator<(Vec2<T> const &p) const {
515  return cmpY(p) < 0;
516  }
518  int cmp(Vec2<T> const &p) const {
519  T diff = m_x-p.m_x;
520  if (diff < 0) return -1;
521  if (diff > 0) return 1;
522  diff = m_y-p.m_y;
523  if (diff < 0) return -1;
524  if (diff > 0) return 1;
525  return 0;
526  }
528  int cmpY(Vec2<T> const &p) const {
529  T diff = m_y-p.m_y;
530  if (diff < 0) return -1;
531  if (diff > 0) return 1;
532  diff = m_x-p.m_x;
533  if (diff < 0) return -1;
534  if (diff > 0) return 1;
535  return 0;
536  }
537 
539  friend std::ostream &operator<< (std::ostream &o, Vec2<T> const &f) {
540  o << f.m_x << "x" << f.m_y;
541  return o;
542  }
543 
547  struct PosSizeLtX {
549  bool operator()(Vec2<T> const &s1, Vec2<T> const &s2) const {
550  return s1.cmp(s2) < 0;
551  }
552  };
556  typedef std::map<Vec2<T>, T,struct PosSizeLtX> MapX;
557 
561  struct PosSizeLtY {
563  bool operator()(Vec2<T> const &s1, Vec2<T> const &s2) const {
564  return s1.cmpY(s2) < 0;
565  }
566  };
570  typedef std::map<Vec2<T>, T,struct PosSizeLtY> MapY;
571 protected:
572  T m_x, m_y;
573 };
574 
578 typedef Vec2<int> Vec2i;
583 
587 template <class T> class Vec3
588 {
589 public:
591  Vec3(T xx=0,T yy=0,T zz=0) {
592  m_val[0] = xx;
593  m_val[1] = yy;
594  m_val[2] = zz;
595  }
597  template <class U> Vec3(Vec3<U> const &p) {
598  for (int c = 0; c < 3; c++) m_val[c] = T(p[c]);
599  }
600 
602  T x() const {
603  return m_val[0];
604  }
606  T y() const {
607  return m_val[1];
608  }
610  T z() const {
611  return m_val[2];
612  }
614  T operator[](int c) const {
615  assert(c >= 0 && c <= 2);
616  return m_val[c];
617  }
619  T &operator[](int c) {
620  assert(c >= 0 && c <= 2);
621  return m_val[c];
622  }
623 
625  void set(T xx, T yy, T zz) {
626  m_val[0] = xx;
627  m_val[1] = yy;
628  m_val[2] = zz;
629  }
631  void setX(T xx) {
632  m_val[0] = xx;
633  }
635  void setY(T yy) {
636  m_val[1] = yy;
637  }
639  void setZ(T zz) {
640  m_val[2] = zz;
641  }
642 
644  void add(T dx, T dy, T dz) {
645  m_val[0] += dx;
646  m_val[1] += dy;
647  m_val[2] += dz;
648  }
649 
652  for (int c = 0; c < 3; c++) m_val[c] = T(m_val[c]+p.m_val[c]);
653  return *this;
654  }
657  for (int c = 0; c < 3; c++) m_val[c] = T(m_val[c]-p.m_val[c]);
658  return *this;
659  }
661  template <class U>
662  Vec3<T> &operator*=(U scale) {
663  for (int c = 0; c < 3; c++) m_val[c] = T(m_val[c]*scale);
664  return *this;
665  }
666 
668  friend Vec3<T> operator+(Vec3<T> const &p1, Vec3<T> const &p2) {
669  Vec3<T> p(p1);
670  return p+=p2;
671  }
673  friend Vec3<T> operator-(Vec3<T> const &p1, Vec3<T> const &p2) {
674  Vec3<T> p(p1);
675  return p-=p2;
676  }
678  template <class U>
679  friend Vec3<T> operator*(U scale, Vec3<T> const &p1) {
680  Vec3<T> p(p1);
681  return p *= scale;
682  }
683 
685  bool operator==(Vec3<T> const &p) const {
686  return cmp(p) == 0;
687  }
689  bool operator!=(Vec3<T> const &p) const {
690  return cmp(p) != 0;
691  }
693  bool operator<(Vec3<T> const &p) const {
694  return cmp(p) < 0;
695  }
697  int cmp(Vec3<T> const &p) const {
698  for (int c = 0; c < 3; c++) {
699  T diff = m_val[c]-p.m_val[c];
700  if (diff) return (diff < 0) ? -1 : 1;
701  }
702  return 0;
703  }
704 
706  friend std::ostream &operator<< (std::ostream &o, Vec3<T> const &f) {
707  o << f.m_val[0] << "x" << f.m_val[1] << "x" << f.m_val[2];
708  return o;
709  }
710 
714  struct PosSizeLt {
716  bool operator()(Vec3<T> const &s1, Vec3<T> const &s2) const {
717  return s1.cmp(s2) < 0;
718  }
719  };
723  typedef std::map<Vec3<T>, T,struct PosSizeLt> Map;
724 
725 protected:
727  T m_val[3];
728 };
729 
733 typedef Vec3<int> Vec3i;
736 
740 template <class T> class Box2
741 {
742 public:
744  Box2(Vec2<T> minPt=Vec2<T>(), Vec2<T> maxPt=Vec2<T>()) {
745  m_pt[0] = minPt;
746  m_pt[1] = maxPt;
747  }
749  template <class U> Box2(Box2<U> const &p) {
750  for (int c=0; c < 2; c++) m_pt[c] = p[c];
751  }
752 
754  Vec2<T> const &min() const {
755  return m_pt[0];
756  }
758  Vec2<T> const &max() const {
759  return m_pt[1];
760  }
763  return m_pt[0];
764  }
767  return m_pt[1];
768  }
773  Vec2<T> const &operator[](int c) const {
774  assert(c >= 0 && c <= 1);
775  return m_pt[c];
776  }
778  Vec2<T> size() const {
779  return m_pt[1]-m_pt[0];
780  }
782  Vec2<T> center() const {
783  return 0.5*(m_pt[0]+m_pt[1]);
784  }
785 
787  void set(Vec2<T> const &x, Vec2<T> const &y) {
788  m_pt[0] = x;
789  m_pt[1] = y;
790  }
792  void setMin(Vec2<T> const &x) {
793  m_pt[0] = x;
794  }
796  void setMax(Vec2<T> const &y) {
797  m_pt[1] = y;
798  }
799 
801  void resizeFromMin(Vec2<T> const &sz) {
802  m_pt[1] = m_pt[0]+sz;
803  }
805  void resizeFromMax(Vec2<T> const &sz) {
806  m_pt[0] = m_pt[1]-sz;
807  }
809  void resizeFromCenter(Vec2<T> const &sz) {
810  Vec2<T> centerPt = 0.5*(m_pt[0]+m_pt[1]);
811  m_pt[0] = centerPt - 0.5*sz;
812  m_pt[1] = centerPt + (sz - 0.5*sz);
813  }
814 
816  template <class U> void scale(U factor) {
817  m_pt[0] *= factor;
818  m_pt[1] *= factor;
819  }
820 
822  void extend(T val) {
823  m_pt[0] -= Vec2<T>(val/2,val/2);
824  m_pt[1] += Vec2<T>(val-(val/2),val-(val/2));
825  }
826 
828  bool operator==(Box2<T> const &p) const {
829  return cmp(p) == 0;
830  }
832  bool operator!=(Box2<T> const &p) const {
833  return cmp(p) != 0;
834  }
836  bool operator<(Box2<T> const &p) const {
837  return cmp(p) < 0;
838  }
839 
841  int cmp(Box2<T> const &p) const {
842  int diff = m_pt[0].cmpY(p.m_pt[0]);
843  if (diff) return diff;
844  diff = m_pt[1].cmpY(p.m_pt[1]);
845  if (diff) return diff;
846  return 0;
847  }
848 
850  friend std::ostream &operator<< (std::ostream &o, Box2<T> const &f) {
851  o << "(" << f.m_pt[0] << "<->" << f.m_pt[1] << ")";
852  return o;
853  }
854 
858  struct PosSizeLt {
860  bool operator()(Box2<T> const &s1, Box2<T> const &s2) const {
861  return s1.cmp(s2) < 0;
862  }
863  };
867  typedef std::map<Box2<T>, T,struct PosSizeLt> Map;
868 
869 protected:
872 };
873 
875 typedef Box2<int> Box2i;
880 
881 #endif /* LIBMWAW_INTERNAL_H */
882 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab:

Generated on Tue Oct 8 2013 19:56:44 for libmwaw by doxygen 1.8.4