~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/poppler/splash/SplashPath.cc

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//========================================================================
 
2
//
 
3
// SplashPath.cc
 
4
//
 
5
//========================================================================
 
6
 
 
7
#include <config.h>
 
8
 
 
9
#ifdef USE_GCC_PRAGMAS
 
10
#pragma implementation
 
11
#endif
 
12
 
 
13
#include <string.h>
 
14
#include "goo/gmem.h"
 
15
#include "SplashErrorCodes.h"
 
16
#include "SplashPath.h"
 
17
 
 
18
//------------------------------------------------------------------------
 
19
// SplashPath
 
20
//------------------------------------------------------------------------
 
21
 
 
22
// A path can be in three possible states:
 
23
//
 
24
// 1. no current point -- zero or more finished subpaths
 
25
//    [curSubpath == length]
 
26
//
 
27
// 2. one point in subpath
 
28
//    [curSubpath == length - 1]
 
29
//
 
30
// 3. open subpath with two or more points
 
31
//    [curSubpath < length - 1]
 
32
 
 
33
SplashPath::SplashPath() {
 
34
  pts = NULL;
 
35
  flags = NULL;
 
36
  length = size = 0;
 
37
  curSubpath = 0;
 
38
  hints = NULL;
 
39
  hintsLength = hintsSize = 0;
 
40
}
 
41
 
 
42
SplashPath::SplashPath(SplashPath *path) {
 
43
  length = path->length;
 
44
  size = path->size;
 
45
  pts = (SplashPathPoint *)gmallocn(size, sizeof(SplashPathPoint));
 
46
  flags = (Guchar *)gmallocn(size, sizeof(Guchar));
 
47
  memcpy(pts, path->pts, length * sizeof(SplashPathPoint));
 
48
  memcpy(flags, path->flags, length * sizeof(Guchar));
 
49
  curSubpath = path->curSubpath;
 
50
  if (path->hints) {
 
51
    hintsLength = hintsSize = path->hintsLength;
 
52
    hints = (SplashPathHint *)gmallocn(hintsSize, sizeof(SplashPathHint));
 
53
    memcpy(hints, path->hints, hintsLength * sizeof(SplashPathHint));
 
54
  } else {
 
55
    hints = NULL;
 
56
  }
 
57
}
 
58
 
 
59
SplashPath::~SplashPath() {
 
60
  gfree(pts);
 
61
  gfree(flags);
 
62
  gfree(hints);
 
63
}
 
64
 
 
65
// Add space for <nPts> more points.
 
66
void SplashPath::grow(int nPts) {
 
67
  if (length + nPts > size) {
 
68
    if (size == 0) {
 
69
      size = 32;
 
70
    }
 
71
    while (size < length + nPts) {
 
72
      size *= 2;
 
73
    }
 
74
    pts = (SplashPathPoint *)greallocn(pts, size, sizeof(SplashPathPoint));
 
75
    flags = (Guchar *)greallocn(flags, size, sizeof(Guchar));
 
76
  }
 
77
}
 
78
 
 
79
void SplashPath::append(SplashPath *path) {
 
80
  int i;
 
81
 
 
82
  curSubpath = length + path->curSubpath;
 
83
  grow(path->length);
 
84
  for (i = 0; i < path->length; ++i) {
 
85
    pts[length] = path->pts[i];
 
86
    flags[length] = path->flags[i];
 
87
    ++length;
 
88
  }
 
89
}
 
90
 
 
91
SplashError SplashPath::moveTo(SplashCoord x, SplashCoord y) {
 
92
  if (onePointSubpath()) {
 
93
    return splashErrBogusPath;
 
94
  }
 
95
  grow(1);
 
96
  pts[length].x = x;
 
97
  pts[length].y = y;
 
98
  flags[length] = splashPathFirst | splashPathLast;
 
99
  curSubpath = length++;
 
100
  return splashOk;
 
101
}
 
102
 
 
103
SplashError SplashPath::lineTo(SplashCoord x, SplashCoord y) {
 
104
  if (noCurrentPoint()) {
 
105
    return splashErrNoCurPt;
 
106
  }
 
107
  flags[length-1] &= ~splashPathLast;
 
108
  grow(1);
 
109
  pts[length].x = x;
 
110
  pts[length].y = y;
 
111
  flags[length] = splashPathLast;
 
112
  ++length;
 
113
  return splashOk;
 
114
}
 
115
 
 
116
SplashError SplashPath::curveTo(SplashCoord x1, SplashCoord y1,
 
117
                                SplashCoord x2, SplashCoord y2,
 
118
                                SplashCoord x3, SplashCoord y3) {
 
119
  if (noCurrentPoint()) {
 
120
    return splashErrNoCurPt;
 
121
  }
 
122
  flags[length-1] &= ~splashPathLast;
 
123
  grow(3);
 
124
  pts[length].x = x1;
 
125
  pts[length].y = y1;
 
126
  flags[length] = splashPathCurve;
 
127
  ++length;
 
128
  pts[length].x = x2;
 
129
  pts[length].y = y2;
 
130
  flags[length] = splashPathCurve;
 
131
  ++length;
 
132
  pts[length].x = x3;
 
133
  pts[length].y = y3;
 
134
  flags[length] = splashPathLast;
 
135
  ++length;
 
136
  return splashOk;
 
137
}
 
138
 
 
139
SplashError SplashPath::close() {
 
140
  if (noCurrentPoint()) {
 
141
    return splashErrNoCurPt;
 
142
  }
 
143
  if (curSubpath == length - 1 ||
 
144
      pts[length - 1].x != pts[curSubpath].x ||
 
145
      pts[length - 1].y != pts[curSubpath].y) {
 
146
    lineTo(pts[curSubpath].x, pts[curSubpath].y);
 
147
  }
 
148
  flags[curSubpath] |= splashPathClosed;
 
149
  flags[length - 1] |= splashPathClosed;
 
150
  curSubpath = length;
 
151
  return splashOk;
 
152
}
 
153
 
 
154
void SplashPath::addStrokeAdjustHint(int ctrl0, int ctrl1,
 
155
                                     int firstPt, int lastPt) {
 
156
  if (hintsLength == hintsSize) {
 
157
    hintsSize = hintsLength ? 2 * hintsLength : 8;
 
158
    hints = (SplashPathHint *)greallocn(hints, hintsSize,
 
159
                                        sizeof(SplashPathHint));
 
160
  }
 
161
  hints[hintsLength].ctrl0 = ctrl0;
 
162
  hints[hintsLength].ctrl1 = ctrl1;
 
163
  hints[hintsLength].firstPt = firstPt;
 
164
  hints[hintsLength].lastPt = lastPt;
 
165
  ++hintsLength;
 
166
}
 
167
 
 
168
void SplashPath::offset(SplashCoord dx, SplashCoord dy) {
 
169
  int i;
 
170
 
 
171
  for (i = 0; i < length; ++i) {
 
172
    pts[i].x += dx;
 
173
    pts[i].y += dy;
 
174
  }
 
175
}
 
176
 
 
177
GBool SplashPath::getCurPt(SplashCoord *x, SplashCoord *y) {
 
178
  if (noCurrentPoint()) {
 
179
    return gFalse;
 
180
  }
 
181
  *x = pts[length - 1].x;
 
182
  *y = pts[length - 1].y;
 
183
  return gTrue;
 
184
}