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 ------------- */
148 struct MWAWColor {
150  MWAWColor(uint32_t argb=0) : m_value(argb) {
151  }
153  MWAWColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a=0) :
154  m_value(uint32_t((a<<24)+(r<<16)+(g<<8)+b)) {
155  }
157  MWAWColor &operator=(uint32_t argb) {
158  m_value = argb;
159  return *this;
160  }
162  static MWAWColor black() {
163  return MWAWColor(0);
164  }
166  static MWAWColor white() {
167  return MWAWColor(0xFFFFFF);
168  }
169 
171  static MWAWColor barycenter(float alpha, MWAWColor const colA,
172  float beta, MWAWColor const colB);
174  uint32_t value() const {
175  return m_value;
176  }
178  bool isBlack() const {
179  return (m_value&0xFFFFFF)==0;
180  }
182  bool isWhite() const {
183  return (m_value&0xFFFFFF)==0xFFFFFF;
184  }
186  bool operator==(MWAWColor const &c) const {
187  return (c.m_value&0xFFFFFF)==(m_value&0xFFFFFF);
188  }
190  bool operator!=(MWAWColor const &c) const {
191  return !operator==(c);
192  }
194  bool operator<(MWAWColor const &c) const {
195  return (c.m_value&0xFFFFFF)<(m_value&0xFFFFFF);
196  }
198  bool operator<=(MWAWColor const &c) const {
199  return (c.m_value&0xFFFFFF)<=(m_value&0xFFFFFF);
200  }
202  bool operator>(MWAWColor const &c) const {
203  return !operator<=(c);
204  }
206  bool operator>=(MWAWColor const &c) const {
207  return !operator<(c);
208  }
210  friend std::ostream &operator<< (std::ostream &o, MWAWColor const &c);
212  std::string str() const;
213 protected:
215  uint32_t m_value;
216 };
217 
220  double m_width;
221  double m_leftGutter;
223 };
224 
227  uint32_t m_attributes;
228  uint8_t m_alignment;
229 };
230 
232 struct MWAWBorder {
236  enum Type { Single, Double, Triple };
237  enum Pos { Left = 0, Right = 1, Top = 2, Bottom = 3, HMiddle = 4, VMiddle = 5 };
238  enum { LeftBit = 0x01, RightBit = 0x02, TopBit=0x4, BottomBit = 0x08, HMiddleBit = 0x10, VMiddleBit = 0x20 };
239 
243  std::string getPropertyValue() const;
244 
246  bool operator==(MWAWBorder const &orig) const {
247  return !operator!=(orig);
248  }
250  bool operator!=(MWAWBorder const &orig) const {
251  return m_style != orig.m_style || m_type != orig.m_type ||
252  m_width < orig.m_width || m_width > orig.m_width || m_color != orig.m_color;
253  }
255  int compare(MWAWBorder const &orig) const;
256 
258  friend std::ostream &operator<< (std::ostream &o, MWAWBorder const &border);
260  friend std::ostream &operator<< (std::ostream &o, MWAWBorder::Style const &style);
266  float m_width;
269 };
270 
271 namespace libmwaw
272 {
274 std::string numberingTypeToString(NumberingType type);
275 std::string numberingValueToString(NumberingType type, int value);
277 }
278 
279 // Generic bits
280 #define MWAW_LEFT 0x00
281 #define MWAW_RIGHT 0x01
282 #define MWAW_CENTER 0x02
283 #define MWAW_TOP 0x03
284 #define MWAW_BOTTOM 0x04
285 
292 template <class T> struct Variable {
294  Variable() : m_data(), m_set(false) {}
296  Variable(T def) : m_data(def), m_set(false) {}
298  Variable(Variable const &orig) : m_data(orig.m_data), m_set(orig.m_set) {}
300  Variable &operator=(Variable const &orig) {
301  if (this != &orig) {
302  m_data = orig.m_data;
303  m_set = orig.m_set;
304  }
305  return *this;
306  }
308  Variable &operator=(T val) {
309  m_data = val;
310  m_set = true;
311  return *this;
312  }
314  void insert(Variable const &orig) {
315  if (orig.m_set) {
316  m_data = orig.m_data;
317  m_set = orig.m_set;
318  }
319  }
321  T const *operator->() const {
322  return &m_data;
323  }
325  T *operator->() {
326  m_set = true;
327  return &m_data;
328  }
330  T const &operator*() const {
331  return m_data;
332  }
334  T &operator*() {
335  m_set = true;
336  return m_data;
337  }
339  T const &get() const {
340  return m_data;
341  }
343  bool isSet() const {
344  return m_set;
345  }
347  void setSet(bool newVal) {
348  m_set=newVal;
349  }
350 protected:
354  bool m_set;
355 };
356 
357 /* ---------- vec2/box2f ------------- */
361 template <class T> class Vec2
362 {
363 public:
365  Vec2(T xx=0,T yy=0) : m_x(xx), m_y(yy) { }
367  template <class U> Vec2(Vec2<U> const &p) : m_x(T(p.x())), m_y(T(p.y())) {}
368 
370  T x() const {
371  return m_x;
372  }
374  T y() const {
375  return m_y;
376  }
378  T operator[](int c) const {
379  assert(c >= 0 && c <= 1);
380  return (c==0) ? m_x : m_y;
381  }
383  T &operator[](int c) {
384  assert(c >= 0 && c <= 1);
385  return (c==0) ? m_x : m_y;
386  }
387 
389  void set(T xx, T yy) {
390  m_x = xx;
391  m_y = yy;
392  }
394  void setX(T xx) {
395  m_x = xx;
396  }
398  void setY(T yy) {
399  m_y = yy;
400  }
401 
403  void add(T dx, T dy) {
404  m_x += dx;
405  m_y += dy;
406  }
407 
410  m_x += p.m_x;
411  m_y += p.m_y;
412  return *this;
413  }
416  m_x -= p.m_x;
417  m_y -= p.m_y;
418  return *this;
419  }
421  template <class U>
422  Vec2<T> &operator*=(U scale) {
423  m_x = T(m_x*scale);
424  m_y = T(m_y*scale);
425  return *this;
426  }
427 
429  friend Vec2<T> operator+(Vec2<T> const &p1, Vec2<T> const &p2) {
430  Vec2<T> p(p1);
431  return p+=p2;
432  }
434  friend Vec2<T> operator-(Vec2<T> const &p1, Vec2<T> const &p2) {
435  Vec2<T> p(p1);
436  return p-=p2;
437  }
439  template <class U>
440  friend Vec2<T> operator*(U scale, Vec2<T> const &p1) {
441  Vec2<T> p(p1);
442  return p *= scale;
443  }
444 
446  bool operator==(Vec2<T> const &p) const {
447  return cmpY(p) == 0;
448  }
450  bool operator!=(Vec2<T> const &p) const {
451  return cmpY(p) != 0;
452  }
454  bool operator<(Vec2<T> const &p) const {
455  return cmpY(p) < 0;
456  }
458  int cmp(Vec2<T> const &p) const {
459  T diff = m_x-p.m_x;
460  if (diff < 0) return -1;
461  if (diff > 0) return 1;
462  diff = m_y-p.m_y;
463  if (diff < 0) return -1;
464  if (diff > 0) return 1;
465  return 0;
466  }
468  int cmpY(Vec2<T> const &p) const {
469  T diff = m_y-p.m_y;
470  if (diff < 0) return -1;
471  if (diff > 0) return 1;
472  diff = m_x-p.m_x;
473  if (diff < 0) return -1;
474  if (diff > 0) return 1;
475  return 0;
476  }
477 
479  friend std::ostream &operator<< (std::ostream &o, Vec2<T> const &f) {
480  o << f.m_x << "x" << f.m_y;
481  return o;
482  }
483 
487  struct PosSizeLtX {
489  bool operator()(Vec2<T> const &s1, Vec2<T> const &s2) const {
490  return s1.cmp(s2) < 0;
491  }
492  };
496  typedef std::map<Vec2<T>, T,struct PosSizeLtX> MapX;
497 
501  struct PosSizeLtY {
503  bool operator()(Vec2<T> const &s1, Vec2<T> const &s2) const {
504  return s1.cmpY(s2) < 0;
505  }
506  };
510  typedef std::map<Vec2<T>, T,struct PosSizeLtY> MapY;
511 protected:
512  T m_x, m_y;
513 };
514 
518 typedef Vec2<int> Vec2i;
523 
527 template <class T> class Vec3
528 {
529 public:
531  Vec3(T xx=0,T yy=0,T zz=0) {
532  m_val[0] = xx;
533  m_val[1] = yy;
534  m_val[2] = zz;
535  }
537  template <class U> Vec3(Vec3<U> const &p) {
538  for (int c = 0; c < 3; c++) m_val[c] = T(p[c]);
539  }
540 
542  T x() const {
543  return m_val[0];
544  }
546  T y() const {
547  return m_val[1];
548  }
550  T z() const {
551  return m_val[2];
552  }
554  T operator[](int c) const {
555  assert(c >= 0 && c <= 2);
556  return m_val[c];
557  }
559  T &operator[](int c) {
560  assert(c >= 0 && c <= 2);
561  return m_val[c];
562  }
563 
565  void set(T xx, T yy, T zz) {
566  m_val[0] = xx;
567  m_val[1] = yy;
568  m_val[2] = zz;
569  }
571  void setX(T xx) {
572  m_val[0] = xx;
573  }
575  void setY(T yy) {
576  m_val[1] = yy;
577  }
579  void setZ(T zz) {
580  m_val[2] = zz;
581  }
582 
584  void add(T dx, T dy, T dz) {
585  m_val[0] += dx;
586  m_val[1] += dy;
587  m_val[2] += dz;
588  }
589 
592  for (int c = 0; c < 3; c++) m_val[c] = T(m_val[c]+p.m_val[c]);
593  return *this;
594  }
597  for (int c = 0; c < 3; c++) m_val[c] = T(m_val[c]-p.m_val[c]);
598  return *this;
599  }
601  template <class U>
602  Vec3<T> &operator*=(U scale) {
603  for (int c = 0; c < 3; c++) m_val[c] = T(m_val[c]*scale);
604  return *this;
605  }
606 
608  friend Vec3<T> operator+(Vec3<T> const &p1, Vec3<T> const &p2) {
609  Vec3<T> p(p1);
610  return p+=p2;
611  }
613  friend Vec3<T> operator-(Vec3<T> const &p1, Vec3<T> const &p2) {
614  Vec3<T> p(p1);
615  return p-=p2;
616  }
618  template <class U>
619  friend Vec3<T> operator*(U scale, Vec3<T> const &p1) {
620  Vec3<T> p(p1);
621  return p *= scale;
622  }
623 
625  bool operator==(Vec3<T> const &p) const {
626  return cmp(p) == 0;
627  }
629  bool operator!=(Vec3<T> const &p) const {
630  return cmp(p) != 0;
631  }
633  bool operator<(Vec3<T> const &p) const {
634  return cmp(p) < 0;
635  }
637  int cmp(Vec3<T> const &p) const {
638  for (int c = 0; c < 3; c++) {
639  T diff = m_val[c]-p.m_val[c];
640  if (diff) return (diff < 0) ? -1 : 1;
641  }
642  return 0;
643  }
644 
646  friend std::ostream &operator<< (std::ostream &o, Vec3<T> const &f) {
647  o << f.m_val[0] << "x" << f.m_val[1] << "x" << f.m_val[2];
648  return o;
649  }
650 
654  struct PosSizeLt {
656  bool operator()(Vec3<T> const &s1, Vec3<T> const &s2) const {
657  return s1.cmp(s2) < 0;
658  }
659  };
663  typedef std::map<Vec3<T>, T,struct PosSizeLt> Map;
664 
665 protected:
667  T m_val[3];
668 };
669 
673 typedef Vec3<int> Vec3i;
676 
680 template <class T> class Box2
681 {
682 public:
684  Box2(Vec2<T> minPt=Vec2<T>(), Vec2<T> maxPt=Vec2<T>()) {
685  m_pt[0] = minPt;
686  m_pt[1] = maxPt;
687  }
689  template <class U> Box2(Box2<U> const &p) {
690  for (int c=0; c < 2; c++) m_pt[c] = p[c];
691  }
692 
694  Vec2<T> const &min() const {
695  return m_pt[0];
696  }
698  Vec2<T> const &max() const {
699  return m_pt[1];
700  }
703  return m_pt[0];
704  }
707  return m_pt[1];
708  }
713  Vec2<T> const &operator[](int c) const {
714  assert(c >= 0 && c <= 1);
715  return m_pt[c];
716  }
718  Vec2<T> size() const {
719  return m_pt[1]-m_pt[0];
720  }
722  Vec2<T> center() const {
723  return 0.5*(m_pt[0]+m_pt[1]);
724  }
725 
727  void set(Vec2<T> const &x, Vec2<T> const &y) {
728  m_pt[0] = x;
729  m_pt[1] = y;
730  }
732  void setMin(Vec2<T> const &x) {
733  m_pt[0] = x;
734  }
736  void setMax(Vec2<T> const &y) {
737  m_pt[1] = y;
738  }
739 
741  void resizeFromMin(Vec2<T> const &sz) {
742  m_pt[1] = m_pt[0]+sz;
743  }
745  void resizeFromMax(Vec2<T> const &sz) {
746  m_pt[0] = m_pt[1]-sz;
747  }
749  void resizeFromCenter(Vec2<T> const &sz) {
750  Vec2<T> centerPt = 0.5*(m_pt[0]+m_pt[1]);
751  m_pt[0] = centerPt - 0.5*sz;
752  m_pt[1] = centerPt + (sz - 0.5*sz);
753  }
754 
756  template <class U> void scale(U factor) {
757  m_pt[0] *= factor;
758  m_pt[1] *= factor;
759  }
760 
762  void extend(T val) {
763  m_pt[0] -= Vec2<T>(val/2,val/2);
764  m_pt[1] += Vec2<T>(val-(val/2),val-(val/2));
765  }
766 
768  bool operator==(Box2<T> const &p) const {
769  return cmp(p) == 0;
770  }
772  bool operator!=(Box2<T> const &p) const {
773  return cmp(p) != 0;
774  }
776  bool operator<(Box2<T> const &p) const {
777  return cmp(p) < 0;
778  }
779 
781  int cmp(Box2<T> const &p) const {
782  int diff = m_pt[0].cmpY(p.m_pt[0]);
783  if (diff) return diff;
784  diff = m_pt[1].cmpY(p.m_pt[1]);
785  if (diff) return diff;
786  return 0;
787  }
788 
790  friend std::ostream &operator<< (std::ostream &o, Box2<T> const &f) {
791  o << "(" << f.m_pt[0] << "<->" << f.m_pt[1] << ")";
792  return o;
793  }
794 
798  struct PosSizeLt {
800  bool operator()(Box2<T> const &s1, Box2<T> const &s2) const {
801  return s1.cmp(s2) < 0;
802  }
803  };
807  typedef std::map<Box2<T>, T,struct PosSizeLt> Map;
808 
809 protected:
812 };
813 
815 typedef Box2<int> Box2i;
820 
821 #endif /* LIBMWAW_INTERNAL_H */
822 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab:

Generated on Sat May 4 2013 11:47:10 for libmwaw by doxygen 1.8.3.1