Coverage Report

Created: 2026-06-10 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/splash/SplashPath.cc
Line
Count
Source
1
//========================================================================
2
//
3
// SplashPath.cc
4
//
5
// Copyright 2003-2013 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include <string.h>
12
#include "gmem.h"
13
#include "gmempp.h"
14
#include "SplashErrorCodes.h"
15
#include "SplashPath.h"
16
17
//------------------------------------------------------------------------
18
// SplashPath
19
//------------------------------------------------------------------------
20
21
// A path can be in three possible states:
22
//
23
// 1. no current point -- zero or more finished subpaths
24
//    [curSubpath == length]
25
//
26
// 2. one point in subpath
27
//    [curSubpath == length - 1]
28
//
29
// 3. open subpath with two or more points
30
//    [curSubpath < length - 1]
31
32
2.53M
SplashPath::SplashPath() {
33
2.53M
  pts = NULL;
34
2.53M
  flags = NULL;
35
2.53M
  length = size = 0;
36
2.53M
  curSubpath = 0;
37
2.53M
  hints = NULL;
38
2.53M
  hintsLength = hintsSize = 0;
39
2.53M
}
40
41
0
SplashPath::SplashPath(SplashPath *path) {
42
0
  length = path->length;
43
0
  size = path->size;
44
0
  pts = (SplashPathPoint *)gmallocn(size, sizeof(SplashPathPoint));
45
0
  flags = (Guchar *)gmallocn(size, sizeof(Guchar));
46
0
  memcpy(pts, path->pts, length * sizeof(SplashPathPoint));
47
0
  memcpy(flags, path->flags, length * sizeof(Guchar));
48
0
  curSubpath = path->curSubpath;
49
0
  if (path->hints) {
50
0
    hintsLength = hintsSize = path->hintsLength;
51
0
    hints = (SplashPathHint *)gmallocn(hintsSize, sizeof(SplashPathHint));
52
0
    memcpy(hints, path->hints, hintsLength * sizeof(SplashPathHint));
53
0
  } else {
54
0
    hints = NULL;
55
0
    hintsLength = hintsSize = 0;
56
0
  }
57
0
}
58
59
2.53M
SplashPath::~SplashPath() {
60
2.53M
  gfree(pts);
61
2.53M
  gfree(flags);
62
2.53M
  gfree(hints);
63
2.53M
}
64
65
// Add space for <nPts> more points.
66
87.7M
void SplashPath::grow(int nPts) {
67
87.7M
  if (length + nPts > size) {
68
2.20M
    if (size == 0) {
69
2.11M
      size = 32;
70
2.11M
    }
71
2.29M
    while (size < length + nPts) {
72
92.6k
      size *= 2;
73
92.6k
    }
74
2.20M
    pts = (SplashPathPoint *)greallocn(pts, size, sizeof(SplashPathPoint));
75
2.20M
    flags = (Guchar *)greallocn(flags, size, sizeof(Guchar));
76
2.20M
  }
77
87.7M
}
78
79
641k
void SplashPath::append(SplashPath *path) {
80
641k
  int i;
81
82
641k
  curSubpath = length + path->curSubpath;
83
641k
  grow(path->length);
84
5.70M
  for (i = 0; i < path->length; ++i) {
85
5.06M
    pts[length] = path->pts[i];
86
5.06M
    flags[length] = path->flags[i];
87
5.06M
    ++length;
88
5.06M
  }
89
641k
}
90
91
18.5M
SplashError SplashPath::moveTo(SplashCoord x, SplashCoord y) {
92
18.5M
  if (onePointSubpath()) {
93
0
    return splashErrBogusPath;
94
0
  }
95
18.5M
  grow(1);
96
18.5M
  pts[length].x = x;
97
18.5M
  pts[length].y = y;
98
18.5M
  flags[length] = splashPathFirst | splashPathLast;
99
18.5M
  curSubpath = length++;
100
18.5M
  return splashOk;
101
18.5M
}
102
103
61.3M
SplashError SplashPath::lineTo(SplashCoord x, SplashCoord y) {
104
61.3M
  if (noCurrentPoint()) {
105
0
    return splashErrNoCurPt;
106
0
  }
107
61.3M
  flags[length-1] &= (Guchar)~splashPathLast;
108
61.3M
  grow(1);
109
61.3M
  pts[length].x = x;
110
61.3M
  pts[length].y = y;
111
61.3M
  flags[length] = splashPathLast;
112
61.3M
  ++length;
113
61.3M
  return splashOk;
114
61.3M
}
115
116
SplashError SplashPath::curveTo(SplashCoord x1, SplashCoord y1,
117
        SplashCoord x2, SplashCoord y2,
118
7.25M
        SplashCoord x3, SplashCoord y3) {
119
7.25M
  if (noCurrentPoint()) {
120
0
    return splashErrNoCurPt;
121
0
  }
122
7.25M
  flags[length-1] &= (Guchar)~splashPathLast;
123
7.25M
  grow(3);
124
7.25M
  pts[length].x = x1;
125
7.25M
  pts[length].y = y1;
126
7.25M
  flags[length] = splashPathCurve;
127
7.25M
  ++length;
128
7.25M
  pts[length].x = x2;
129
7.25M
  pts[length].y = y2;
130
7.25M
  flags[length] = splashPathCurve;
131
7.25M
  ++length;
132
7.25M
  pts[length].x = x3;
133
7.25M
  pts[length].y = y3;
134
7.25M
  flags[length] = splashPathLast;
135
7.25M
  ++length;
136
7.25M
  return splashOk;
137
7.25M
}
138
139
15.2M
SplashError SplashPath::close(GBool force) {
140
15.2M
  if (noCurrentPoint()) {
141
520
    return splashErrNoCurPt;
142
520
  }
143
15.2M
  if (force ||
144
8.27M
      curSubpath == length - 1 ||
145
8.26M
      pts[length - 1].x != pts[curSubpath].x ||
146
12.0M
      pts[length - 1].y != pts[curSubpath].y) {
147
12.0M
    lineTo(pts[curSubpath].x, pts[curSubpath].y);
148
12.0M
  }
149
15.2M
  flags[curSubpath] |= splashPathClosed;
150
15.2M
  flags[length - 1] |= splashPathClosed;
151
15.2M
  curSubpath = length;
152
15.2M
  return splashOk;
153
15.2M
}
154
155
void SplashPath::addStrokeAdjustHint(int ctrl0, int ctrl1,
156
             int firstPt, int lastPt,
157
19.1M
             GBool projectingCap) {
158
19.1M
  if (hintsLength == hintsSize) {
159
60.8k
    hintsSize = hintsLength ? 2 * hintsLength : 8;
160
60.8k
    hints = (SplashPathHint *)greallocn(hints, hintsSize,
161
60.8k
          sizeof(SplashPathHint));
162
60.8k
  }
163
19.1M
  hints[hintsLength].ctrl0 = ctrl0;
164
19.1M
  hints[hintsLength].ctrl1 = ctrl1;
165
19.1M
  hints[hintsLength].firstPt = firstPt;
166
19.1M
  hints[hintsLength].lastPt = lastPt;
167
19.1M
  hints[hintsLength].projectingCap = projectingCap;
168
19.1M
  ++hintsLength;
169
19.1M
}
170
171
651k
void SplashPath::offset(SplashCoord dx, SplashCoord dy) {
172
651k
  int i;
173
174
5.74M
  for (i = 0; i < length; ++i) {
175
5.09M
    pts[i].x += dx;
176
5.09M
    pts[i].y += dy;
177
5.09M
  }
178
651k
}
179
180
385k
GBool SplashPath::getCurPt(SplashCoord *x, SplashCoord *y) {
181
385k
  if (noCurrentPoint()) {
182
0
    return gFalse;
183
0
  }
184
385k
  *x = pts[length - 1].x;
185
385k
  *y = pts[length - 1].y;
186
385k
  return gTrue;
187
385k
}
188
189
0
GBool SplashPath::containsZeroLengthSubpaths() {
190
0
  GBool zeroLength;
191
0
  int i;
192
193
0
  zeroLength = gTrue;  // make gcc happy
194
0
  for (i = 0; i < length; ++i) {
195
0
    if (flags[i] & splashPathFirst) {
196
0
      zeroLength = gTrue;
197
0
    } else {
198
0
      if (pts[i].x != pts[i-1].x || pts[i].y != pts[i-1].y) {
199
0
  zeroLength = gFalse;
200
0
      }
201
0
      if (flags[i] & splashPathLast) {
202
0
  if (zeroLength) {
203
0
    return gTrue;
204
0
  }
205
0
      }
206
0
    }
207
0
  }
208
0
  return gFalse;
209
0
}