@react-native-ohos/react-native-clippathview 1.1.9-rc.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,7 @@
4
4
  * you may not use this file except in compliance with the License.
5
5
  * You may obtain a copy of the License at
6
6
  *
7
- * http://www.apache.org/licenses/LICENSE-2.0
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
8
  *
9
9
  * Unless required by applicable law or agreed to in writing, software
10
10
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,7 +17,7 @@
17
17
  #include <native_drawing/drawing_path.h>
18
18
  #include <stdexcept>
19
19
  #include <sstream>
20
- #include <cmath>
20
+ #include "math.h"
21
21
  #include <cctype>
22
22
  #include "cmath"
23
23
  #include "vector"
@@ -36,13 +36,12 @@ float SVGPathParser::mPenDownX = 0.0f;
36
36
  float SVGPathParser::mPenDownY = 0.0f;
37
37
  bool SVGPathParser::mPenDown = false;
38
38
 
39
- OH_Drawing_Path *SVGPathParser::parse(const std::string d)
40
- {
39
+ OH_Drawing_Path *SVGPathParser::parse(const std::string d) {
41
40
  if (d.empty()) {
42
41
  return nullptr;
43
42
  }
44
43
  OH_Drawing_Path *cPath_ = OH_Drawing_PathCreate();
45
- char prevCmd = ' ';
44
+ char prev_cmd = ' ';
46
45
  l = d.length();
47
46
  s = d;
48
47
  i = 0;
@@ -56,11 +55,13 @@ OH_Drawing_Path *SVGPathParser::parse(const std::string d)
56
55
  mPenDown = false;
57
56
 
58
57
  while (i < l) {
59
- SkipSpaces();
58
+ skip_spaces();
59
+
60
60
  if (i >= l) {
61
61
  break;
62
62
  }
63
- bool has_prev_cmd = prevCmd != ' ';
63
+
64
+ bool has_prev_cmd = prev_cmd != ' ';
64
65
  char first_char = s.at(i);
65
66
 
66
67
  if (!has_prev_cmd && first_char != 'M' && first_char != 'm') {
@@ -70,33 +71,34 @@ OH_Drawing_Path *SVGPathParser::parse(const std::string d)
70
71
  throw std::logic_error(oss.str());
71
72
  }
72
73
 
74
+ // TODO: simplify
73
75
  bool is_implicit_move_to;
74
76
  char cmd;
75
- if (IsCmd(first_char)) {
77
+ if (is_cmd(first_char)) {
76
78
  is_implicit_move_to = false;
77
79
  cmd = first_char;
78
80
  i += 1;
79
81
  } else if (is_number_start(first_char) && has_prev_cmd) {
80
- if (prevCmd == 'Z' || prevCmd == 'z') {
82
+ if (prev_cmd == 'Z' || prev_cmd == 'z') {
81
83
  // ClosePath cannot be followed by a number.
82
84
  std::ostringstream oss;
83
85
  oss << "Unexpected number after 'z' (s=" << s << ")";
84
86
  throw std::logic_error(oss.str());
85
87
  }
86
88
 
87
- if (prevCmd == 'M' || prevCmd == 'm') {
89
+ if (prev_cmd == 'M' || prev_cmd == 'm') {
88
90
  // 'If a moveto is followed by multiple pairs of coordinates,
89
91
  // the subsequent pairs are treated as implicit lineto commands.'
90
92
  // So we parse them as LineTo.
91
93
  is_implicit_move_to = true;
92
- if (is_absolute(prevCmd)) {
94
+ if (is_absolute(prev_cmd)) {
93
95
  cmd = 'L';
94
96
  } else {
95
97
  cmd = 'l';
96
98
  }
97
99
  } else {
98
100
  is_implicit_move_to = false;
99
- cmd = prevCmd;
101
+ cmd = prev_cmd;
100
102
  }
101
103
  } else {
102
104
  std::ostringstream oss;
@@ -105,219 +107,203 @@ OH_Drawing_Path *SVGPathParser::parse(const std::string d)
105
107
  }
106
108
  bool absolute = is_absolute(cmd);
107
109
  switch (cmd) {
108
- case 'm': {
109
- rnoh:
110
- move(parse_list_number(), parse_list_number(), cPath_);
111
- break;
112
- }
113
- case 'M': {
114
- moveTo(parse_list_number(), parse_list_number(), cPath_);
115
- break;
116
- }
117
- case 'l': {
118
- line(parse_list_number(), parse_list_number(), cPath_);
119
- break;
120
- }
121
- case 'L': {
122
- lineTo(parse_list_number(), parse_list_number(), cPath_);
123
- break;
124
- }
125
- case 'h': {
126
- line(parse_list_number(), 0, cPath_);
127
- break;
128
- }
129
- case 'H': {
130
- lineTo(parse_list_number(), mPenY, cPath_);
131
- break;
132
- }
133
- case 'v': {
134
- line(0, parse_list_number(), cPath_);
135
- break;
136
- }
137
- case 'V': {
138
- lineTo(mPenX, parse_list_number(), cPath_);
139
- break;
140
- }
141
- case 'c': {
142
- curve(parse_list_number(), parse_list_number(), parse_list_number(), parse_list_number(),
143
- parse_list_number(), parse_list_number(), cPath_);
144
- break;
145
- }
146
- case 'C': {
147
- curveTo(parse_list_number(), parse_list_number(), parse_list_number(), parse_list_number(),
148
- parse_list_number(), parse_list_number(), cPath_);
149
- break;
150
- }
151
- case 's': {
152
- smoothCurve(parse_list_number(), parse_list_number(), parse_list_number(), parse_list_number(), cPath_);
153
- break;
154
- }
155
- case 'S': {
156
- smoothCurveTo(parse_list_number(), parse_list_number(), parse_list_number(), parse_list_number(),
157
- cPath_);
158
- break;
159
- }
160
- case 'q': {
161
- quadraticBezierCurve(parse_list_number(), parse_list_number(), parse_list_number(), parse_list_number(),
162
- cPath_);
163
- break;
164
- }
165
- case 'Q': {
166
- quadraticBezierCurveTo(parse_list_number(), parse_list_number(), parse_list_number(),
167
- parse_list_number(), cPath_);
168
- break;
169
- }
170
- case 't': {
171
- smoothQuadraticBezierCurve(parse_list_number(), parse_list_number(), cPath_);
172
- break;
173
- }
174
- case 'T': {
175
- smoothQuadraticBezierCurveTo(parse_list_number(), parse_list_number(), cPath_);
176
- break;
177
- }
178
- case 'a': {
179
- arc(parse_list_number(), parse_list_number(), parse_list_number(), parse_flag(), parse_flag(),
180
- parse_list_number(), parse_list_number(), cPath_);
181
- break;
182
- }
183
- case 'A': {
184
- arcTo(parse_list_number(), parse_list_number(), parse_list_number(), parse_flag(), parse_flag(),
110
+ case 'm': {
111
+ rnoh:
112
+ move(parse_list_number(), parse_list_number(), cPath_);
113
+ break;
114
+ }
115
+ case 'M': {
116
+ moveTo(parse_list_number(), parse_list_number(), cPath_);
117
+ break;
118
+ }
119
+ case 'l': {
120
+ line(parse_list_number(), parse_list_number(), cPath_);
121
+ break;
122
+ }
123
+ case 'L': {
124
+ lineTo(parse_list_number(), parse_list_number(), cPath_);
125
+ break;
126
+ }
127
+ case 'h': {
128
+ line(parse_list_number(), 0, cPath_);
129
+ break;
130
+ }
131
+ case 'H': {
132
+ lineTo(parse_list_number(), mPenY, cPath_);
133
+ break;
134
+ }
135
+ case 'v': {
136
+ line(0, parse_list_number(), cPath_);
137
+ break;
138
+ }
139
+ case 'V': {
140
+ lineTo(mPenX, parse_list_number(), cPath_);
141
+ break;
142
+ }
143
+ case 'c': {
144
+ curve(parse_list_number(), parse_list_number(), parse_list_number(), parse_list_number(),
145
+ parse_list_number(), parse_list_number(), cPath_);
146
+ break;
147
+ }
148
+ case 'C': {
149
+ curveTo(parse_list_number(), parse_list_number(), parse_list_number(), parse_list_number(),
185
150
  parse_list_number(), parse_list_number(), cPath_);
186
- break;
187
- }
188
- case 'z':
189
- case 'Z': {
190
- close(cPath_);
191
- break;
192
- }
193
- default: {
194
- std::ostringstream oss;
195
- oss << "Unexpected comand " << cmd << ",s=" << s << ")";
196
- throw std::logic_error(oss.str());
197
- }
151
+ break;
152
+ }
153
+ case 's': {
154
+ smoothCurve(parse_list_number(), parse_list_number(), parse_list_number(), parse_list_number(), cPath_);
155
+ break;
156
+ }
157
+ case 'S': {
158
+ smoothCurveTo(parse_list_number(), parse_list_number(), parse_list_number(), parse_list_number(), cPath_);
159
+ break;
160
+ }
161
+ case 'q': {
162
+ quadraticBezierCurve(parse_list_number(), parse_list_number(), parse_list_number(), parse_list_number(),
163
+ cPath_);
164
+ break;
165
+ }
166
+ case 'Q': {
167
+ quadraticBezierCurveTo(parse_list_number(), parse_list_number(), parse_list_number(), parse_list_number(),
168
+ cPath_);
169
+ break;
170
+ }
171
+ case 't': {
172
+ smoothQuadraticBezierCurve(parse_list_number(), parse_list_number(), cPath_);
173
+ break;
174
+ }
175
+ case 'T': {
176
+ smoothQuadraticBezierCurveTo(parse_list_number(), parse_list_number(), cPath_);
177
+ break;
178
+ }
179
+ case 'a': {
180
+ arc(parse_list_number(), parse_list_number(), parse_list_number(), parse_flag(), parse_flag(),
181
+ parse_list_number(), parse_list_number(), cPath_);
182
+ break;
183
+ }
184
+ case 'A': {
185
+ arcTo(parse_list_number(), parse_list_number(), parse_list_number(), parse_flag(), parse_flag(),
186
+ parse_list_number(), parse_list_number(), cPath_);
187
+ break;
188
+ }
189
+ case 'z':
190
+ case 'Z': {
191
+ close(cPath_);
192
+ break;
193
+ }
194
+ default: {
195
+ std::ostringstream oss;
196
+ oss << "Unexpected comand " << cmd << ",s=" << s << ")";
197
+ throw std::logic_error(oss.str());
198
+ }
198
199
  }
199
200
 
200
201
  if (is_implicit_move_to) {
201
202
  if (absolute) {
202
- prevCmd = 'M';
203
+ prev_cmd = 'M';
203
204
  } else {
204
- prevCmd = 'm';
205
+ prev_cmd = 'm';
205
206
  }
206
207
  } else {
207
- prevCmd = cmd;
208
+ prev_cmd = cmd;
208
209
  }
209
210
  }
210
211
  return cPath_;
211
212
  }
212
213
 
213
- void SVGPathParser::move(float x, float y, OH_Drawing_Path *cPath_)
214
- {
215
- moveTo(x + mPenX, y + mPenY, cPath_);
216
- }
214
+ void SVGPathParser::move(float x, float y, OH_Drawing_Path *cPath_) { moveTo(x + mPenX, y + mPenY, cPath_); }
217
215
 
218
- void SVGPathParser::moveTo(float x, float y, OH_Drawing_Path *cPath_)
219
- {
216
+ void SVGPathParser::moveTo(float x, float y, OH_Drawing_Path *cPath_) {
220
217
  mPenDownX = mPivotX = mPenX = x;
221
218
  mPenDownY = mPivotY = mPenY = y;
222
219
  OH_Drawing_PathMoveTo(cPath_, x * mScale, y * mScale);
223
220
  }
224
221
 
225
- void SVGPathParser::line(float x, float y, OH_Drawing_Path *cPath_)
226
- {
227
- lineTo(x + mPenX, y + mPenY, cPath_);
228
- }
229
- void SVGPathParser::lineTo(float x, float y, OH_Drawing_Path *cPath_)
230
- {
231
- SetPenDown();
222
+ void SVGPathParser::line(float x, float y, OH_Drawing_Path *cPath_) { lineTo(x + mPenX, y + mPenY, cPath_); }
223
+ void SVGPathParser::lineTo(float x, float y, OH_Drawing_Path *cPath_) {
224
+ // FLog.w(ReactConstants.TAG, "line x: " + x + " y: " + y);
225
+ setPenDown();
232
226
  mPivotX = mPenX = x;
233
227
  mPivotY = mPenY = y;
234
228
  OH_Drawing_PathLineTo(cPath_, x * mScale, y * mScale);
235
229
  }
236
230
 
237
- void SVGPathParser::curve(float c1x, float c1y, float c2x, float c2y, float ex, float ey, OH_Drawing_Path *cPath_)
238
- {
231
+ void SVGPathParser::curve(float c1x, float c1y, float c2x, float c2y, float ex, float ey, OH_Drawing_Path *cPath_) {
239
232
  curveTo(c1x + mPenX, c1y + mPenY, c2x + mPenX, c2y + mPenY, ex + mPenX, ey + mPenY, cPath_);
240
233
  }
241
234
 
242
- void SVGPathParser::curveTo(float c1x, float c1y, float c2x, float c2y, float ex, float ey, OH_Drawing_Path *cPath_)
243
- {
235
+ void SVGPathParser::curveTo(float c1x, float c1y, float c2x, float c2y, float ex, float ey, OH_Drawing_Path *cPath_) {
244
236
  mPivotX = c2x;
245
237
  mPivotY = c2y;
246
238
  cubicTo(c1x, c1y, c2x, c2y, ex, ey, cPath_);
247
239
  }
248
240
 
249
- void SVGPathParser::cubicTo(float c1x, float c1y, float c2x, float c2y, float ex, float ey, OH_Drawing_Path *cPath_)
250
- {
251
- SetPenDown();
241
+ void SVGPathParser::cubicTo(float c1x, float c1y, float c2x, float c2y, float ex, float ey, OH_Drawing_Path *cPath_) {
242
+ setPenDown();
252
243
  mPenX = ex;
253
244
  mPenY = ey;
254
245
  OH_Drawing_PathCubicTo(cPath_, c1x * mScale, c1y * mScale, c2x * mScale, c2y * mScale, ex * mScale, ey * mScale);
255
246
  }
256
247
 
257
248
 
258
- void SVGPathParser::smoothCurve(float c1x, float c1y, float ex, float ey, OH_Drawing_Path *cPath_)
259
- {
249
+ void SVGPathParser::smoothCurve(float c1x, float c1y, float ex, float ey, OH_Drawing_Path *cPath_) {
260
250
  smoothCurveTo(c1x + mPenX, c1y + mPenY, ex + mPenX, ey + mPenY, cPath_);
261
251
  }
262
- void SVGPathParser::smoothCurveTo(float c1x, float c1y, float ex, float ey, OH_Drawing_Path *cPath_)
263
- {
252
+ void SVGPathParser::smoothCurveTo(float c1x, float c1y, float ex, float ey, OH_Drawing_Path *cPath_) {
253
+ // FLog.w(ReactConstants.TAG, "smoothcurve c1x: " + c1x + " c1y: " + c1y + "ex: " + ex + " ey: " + ey);
264
254
  float c2x = c1x;
265
255
  float c2y = c1y;
266
- int count2 = 2;
267
- c1x = (mPenX * count2) - mPivotX;
268
- c1y = (mPenY * count2) - mPivotY;
256
+ c1x = (mPenX * 2) - mPivotX;
257
+ c1y = (mPenY * 2) - mPivotY;
269
258
  mPivotX = c2x;
270
259
  mPivotY = c2y;
271
260
  cubicTo(c1x, c1y, c2x, c2y, ex, ey, cPath_);
272
261
  }
273
262
 
274
- void SVGPathParser::quadraticBezierCurve(float c1x, float c1y, float c2x, float c2y, OH_Drawing_Path *cPath_)
275
- {
263
+ void SVGPathParser::quadraticBezierCurve(float c1x, float c1y, float c2x, float c2y, OH_Drawing_Path *cPath_) {
276
264
  quadraticBezierCurveTo(c1x + mPenX, c1y + mPenY, c2x + mPenX, c2y + mPenY, cPath_);
277
265
  }
278
266
 
279
- void SVGPathParser::quadraticBezierCurveTo(float c1x, float c1y, float c2x, float c2y, OH_Drawing_Path *cPath_)
280
- {
281
- int count2 = 2;
282
- int count3 = 3;
267
+ void SVGPathParser::quadraticBezierCurveTo(float c1x, float c1y, float c2x, float c2y, OH_Drawing_Path *cPath_) {
268
+ // FLog.w(ReactConstants.TAG, "quad c1x: " + c1x + " c1y: " + c1y + "c2x: " + c2x + " c2y: " + c2y);
283
269
  mPivotX = c1x;
284
270
  mPivotY = c1y;
285
271
  float ex = c2x;
286
272
  float ey = c2y;
287
- c2x = (ex + c1x * count2) / count3;
288
- c2y = (ey + c1y * count2) / count3;
289
- c1x = (mPenX + c1x * count2) / count3;
290
- c1y = (mPenY + c1y * count2) / count3;
273
+ c2x = (ex + c1x * 2) / 3;
274
+ c2y = (ey + c1y * 2) / 3;
275
+ c1x = (mPenX + c1x * 2) / 3;
276
+ c1y = (mPenY + c1y * 2) / 3;
291
277
  cubicTo(c1x, c1y, c2x, c2y, ex, ey, cPath_);
292
278
  }
293
279
 
294
- void SVGPathParser::smoothQuadraticBezierCurve(float c1x, float c1y, OH_Drawing_Path *cPath_)
295
- {
280
+ void SVGPathParser::smoothQuadraticBezierCurve(float c1x, float c1y, OH_Drawing_Path *cPath_) {
296
281
  smoothQuadraticBezierCurveTo(c1x + mPenX, c1y + mPenY, cPath_);
297
282
  }
298
283
 
299
- void SVGPathParser::smoothQuadraticBezierCurveTo(float c1x, float c1y, OH_Drawing_Path *cPath_)
300
- {
284
+ void SVGPathParser::smoothQuadraticBezierCurveTo(float c1x, float c1y, OH_Drawing_Path *cPath_) {
285
+ // FLog.w(ReactConstants.TAG, "smoothquad c1x: " + c1x + " c1y: " + c1y);
301
286
  float c2x = c1x;
302
287
  float c2y = c1y;
303
- int count2 = 2;
304
- c1x = (mPenX * count2) - mPivotX;
305
- c1y = (mPenY * count2) - mPivotY;
288
+ c1x = (mPenX * 2) - mPivotX;
289
+ c1y = (mPenY * 2) - mPivotY;
306
290
  quadraticBezierCurveTo(c1x, c1y, c2x, c2y, cPath_);
307
291
  }
308
292
  void SVGPathParser::arc(float rx, float ry, float rotation, bool outer, bool clockwise, float x, float y,
309
- OH_Drawing_Path *cPath_)
310
- {
293
+ OH_Drawing_Path *cPath_) {
311
294
  arcTo(rx, ry, rotation, outer, clockwise, x + mPenX, y + mPenY, cPath_);
312
295
  }
313
296
 
314
297
  void SVGPathParser::arcTo(float rx, float ry, float rotation, bool outer, bool clockwise, float x, float y,
315
- OH_Drawing_Path *cPath_)
316
- {
298
+ OH_Drawing_Path *cPath_) {
299
+ // FLog.w(ReactConstants.TAG, "arc rx: " + rx + " ry: " + ry + " rotation: " + rotation + " outer: " + outer + "
300
+ // clockwise: " + clockwise + " x: " + x + " y: " + y);
317
301
  float tX = mPenX;
318
302
  float tY = mPenY;
303
+
319
304
  ry = abs(ry == 0 ? (rx == 0 ? (y - tY) : rx) : ry);
320
305
  rx = abs(rx == 0 ? (x - tX) : rx);
306
+
321
307
  if (rx == 0 || ry == 0 || (x == tX && y == tY)) {
322
308
  lineTo(x, y, cPath_);
323
309
  return;
@@ -330,9 +316,8 @@ void SVGPathParser::arcTo(float rx, float ry, float rotation, bool outer, bool c
330
316
  y -= tY;
331
317
 
332
318
  // Ellipse Center
333
- int count2 = 2;
334
- float cx = cos * x / count2 + sin * y / count2;
335
- float cy = -sin * x / count2 + cos * y / count2;
319
+ float cx = cos * x / 2 + sin * y / 2;
320
+ float cy = -sin * x / 2 + cos * y / 2;
336
321
  float rxry = rx * rx * ry * ry;
337
322
  float rycx = ry * ry * cx * cx;
338
323
  float rxcy = rx * rx * cy * cy;
@@ -342,8 +327,8 @@ void SVGPathParser::arcTo(float rx, float ry, float rotation, bool outer, bool c
342
327
  a = std::sqrt(1 - a / rxry);
343
328
  rx *= a;
344
329
  ry *= a;
345
- cx = x / count2;
346
- cy = y / count2;
330
+ cx = x / 2;
331
+ cy = y / 2;
347
332
  } else {
348
333
  a = std::sqrt(a / (rxcy + rycx));
349
334
 
@@ -352,8 +337,8 @@ void SVGPathParser::arcTo(float rx, float ry, float rotation, bool outer, bool c
352
337
  }
353
338
  float cxd = -a * cy * rx / ry;
354
339
  float cyd = a * cx * ry / rx;
355
- cx = cos * cxd - sin * cyd + x / count2;
356
- cy = sin * cxd + cos * cyd + y / count2;
340
+ cx = cos * cxd - sin * cyd + x / 2;
341
+ cy = sin * cxd + cos * cyd + y / 2;
357
342
  }
358
343
 
359
344
  // Rotation + Scale Transform
@@ -371,29 +356,27 @@ void SVGPathParser::arcTo(float rx, float ry, float rotation, bool outer, bool c
371
356
  x += tX;
372
357
  y += tY;
373
358
 
374
- SetPenDown();
359
+ setPenDown();
375
360
 
376
361
  mPenX = mPivotX = x;
377
362
  mPenY = mPivotY = y;
378
- int count180 = 180;
379
- int count360 = 360;
380
- double point180 = 180.0;
381
363
 
382
364
  if (rx != ry || rad != 0.f) {
383
365
  arcToBezier(cx, cy, rx, ry, sa, ea, clockwise, rad, cPath_);
384
366
  } else {
385
- float start = static_cast<float>(sa * point180 / M_PI);
386
- float end = static_cast<float>(ea * point180 / M_PI);
387
- float sweep = abs((start - end) - std::floor((start - end) / count360) * count360);
367
+
368
+ float start = static_cast<float>(sa * 180.0 / M_PI);
369
+ float end = static_cast<float>(ea * 180.0 / M_PI);
370
+ float sweep = abs((start - end) - std::floor((start - end) / 360) * 360);
388
371
 
389
372
 
390
373
  if (outer) {
391
- if (sweep < count180) {
392
- sweep = count360 - sweep;
374
+ if (sweep < 180) {
375
+ sweep = 360 - sweep;
393
376
  }
394
377
  } else {
395
- if (sweep > count180) {
396
- sweep = count360 - sweep;
378
+ if (sweep > 180) {
379
+ sweep = 360 - sweep;
397
380
  }
398
381
  }
399
382
 
@@ -401,19 +384,17 @@ void SVGPathParser::arcTo(float rx, float ry, float rotation, bool outer, bool c
401
384
  sweep = -sweep;
402
385
  }
403
386
  OH_Drawing_PathArcTo(cPath_, (cx - rx) * mScale, (cy - rx) * mScale, (cx + rx) * mScale, (cy + rx) * mScale,
404
- start, sweep);
387
+ start, sweep);
405
388
  }
406
389
  }
407
- void SVGPathParser::SetPenDown()
408
- {
390
+ void SVGPathParser::setPenDown() {
409
391
  if (!mPenDown) {
410
392
  mPenDownX = mPenX;
411
393
  mPenDownY = mPenY;
412
394
  mPenDown = true;
413
395
  }
414
396
  }
415
- void SVGPathParser::close(OH_Drawing_Path *cPath_)
416
- {
397
+ void SVGPathParser::close(OH_Drawing_Path *cPath_) {
417
398
  if (mPenDown) {
418
399
  mPenX = mPenDownX;
419
400
  mPenY = mPenDownY;
@@ -422,8 +403,7 @@ void SVGPathParser::close(OH_Drawing_Path *cPath_)
422
403
  }
423
404
  }
424
405
  void SVGPathParser::arcToBezier(float cx, float cy, float rx, float ry, float sa, float ea, bool clockwise, float rad,
425
- OH_Drawing_Path *cPath_)
426
- {
406
+ OH_Drawing_Path *cPath_) {
427
407
  // Inverse Rotation + Scale Transform
428
408
  float cos = std::cos(rad);
429
409
  float sin = std::sin(rad);
@@ -434,126 +414,109 @@ void SVGPathParser::arcToBezier(float cx, float cy, float rx, float ry, float sa
434
414
 
435
415
  // Bezier Curve Approximation
436
416
  float arc = ea - sa;
437
- int count2 = 2;
438
- int count4 = 4;
439
- double point3 = 3.0;
440
417
  if (arc < 0 && clockwise) {
441
- arc += M_PI * count2;
418
+ arc += M_PI * 2;
442
419
  } else if (arc > 0 && !clockwise) {
443
- arc -= M_PI * count2;
420
+ arc -= M_PI * 2;
444
421
  }
445
422
 
446
- int n = static_cast<int>(std::ceil(std::abs(round(arc / (M_PI / count2)))));
423
+ int n = (int)std::ceil(std::abs(round(arc / (M_PI / 2))));
447
424
 
448
425
  float step = arc / n;
449
- float k = static_cast<float>((count4 / point3) * std::tan(step / count4));
426
+ float k = (float)((4 / 3.0) * std::tan(step / 4));
450
427
  float x = std::cos(sa);
451
428
  float y = std::sin(sa);
452
429
 
453
- for (int counti = 0; counti < n; counti++) {
430
+ for (int i = 0; i < n; i++) {
454
431
  float cp1x = x - k * y;
455
432
  float cp1y = y + k * x;
456
433
 
457
434
  sa += step;
458
- float newX = std::cos(sa);
459
- float newY = std::sin(sa);
435
+ x = std::cos(sa);
436
+ y = std::sin(sa);
460
437
 
461
- float cp2x = newX + k * newY;
462
- float cp2y = newY - k * newX;
438
+ float cp2x = x + k * y;
439
+ float cp2y = y - k * x;
463
440
 
464
441
  float c1x = (cx + xx * cp1x + yx * cp1y);
465
442
  float c1y = (cy + xy * cp1x + yy * cp1y);
466
443
  float c2x = (cx + xx * cp2x + yx * cp2y);
467
444
  float c2y = (cy + xy * cp2x + yy * cp2y);
468
- float ex = (cx + xx * newX + yx * newY);
469
- float ey = (cy + xy * newX + yy * newY);
445
+ float ex = (cx + xx * x + yx * y);
446
+ float ey = (cy + xy * x + yy * y);
470
447
  OH_Drawing_PathCubicTo(cPath_, c1x * mScale, c1y * mScale, c2x * mScale, c2y * mScale, ex * mScale,
471
- ey * mScale);
448
+ ey * mScale);
472
449
  }
473
450
  }
474
451
 
475
- double SVGPathParser::_round(double val)
476
- {
452
+ double SVGPathParser::_round(double val) {
477
453
  double multiplier = pow(10, 4);
478
454
  return round(val * multiplier) / multiplier;
479
455
  }
480
456
 
481
- void SVGPathParser::SkipSpaces()
482
- {
483
- while (i < l && std::isspace(s.at(i))) {
457
+ void SVGPathParser::skip_spaces() {
458
+ while (i < l && std::isspace(s.at(i)))
484
459
  i++;
485
- }
486
460
  }
487
- bool SVGPathParser::IsCmd(char c)
488
- {
461
+ bool SVGPathParser::is_cmd(char c) {
489
462
  switch (c) {
490
- case 'M':
491
- case 'm':
492
- case 'Z':
493
- case 'z':
494
- case 'L':
495
- case 'l':
496
- case 'H':
497
- case 'h':
498
- case 'V':
499
- case 'v':
500
- case 'C':
501
- case 'c':
502
- case 'S':
503
- case 's':
504
- case 'Q':
505
- case 'q':
506
- case 'T':
507
- case 't':
508
- case 'A':
509
- case 'a':
510
- return true;
511
- default:
512
- return false;
463
+ case 'M':
464
+ case 'm':
465
+ case 'Z':
466
+ case 'z':
467
+ case 'L':
468
+ case 'l':
469
+ case 'H':
470
+ case 'h':
471
+ case 'V':
472
+ case 'v':
473
+ case 'C':
474
+ case 'c':
475
+ case 'S':
476
+ case 's':
477
+ case 'Q':
478
+ case 'q':
479
+ case 'T':
480
+ case 't':
481
+ case 'A':
482
+ case 'a':
483
+ return true;
513
484
  }
514
485
  return false;
515
486
  }
516
487
 
517
488
 
518
- bool SVGPathParser::is_number_start(char c)
519
- {
520
- return (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '+';
521
- }
489
+ bool SVGPathParser::is_number_start(char c) { return (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '+'; }
522
490
 
523
- bool SVGPathParser::is_absolute(char c)
524
- {
525
- return std::isupper(c);
526
- }
491
+ bool SVGPathParser::is_absolute(char c) { return std::isupper(c); }
527
492
  // By the SVG spec 'large-arc' and 'sweep' must contain only one char
528
493
  // and can be written without any separators, e.g.: 10 20 30 01 10 20.
529
494
 
530
- bool SVGPathParser::parse_flag()
531
- {
532
- SkipSpaces();
495
+ bool SVGPathParser::parse_flag() {
496
+ skip_spaces();
533
497
 
534
498
  char c = s.at(i);
535
499
  switch (c) {
536
- case '0':
537
- case '1': {
500
+ case '0':
501
+ case '1': {
502
+ i += 1;
503
+ if (i < l && s.at(i) == ',') {
538
504
  i += 1;
539
- if (i < l && s.at(i) == ',') {
540
- i += 1;
541
- }
542
- SkipSpaces();
543
- break;
544
505
  }
545
- default:
546
- std::ostringstream oss;
547
- oss << "Unexpected flag" << c << ",i=" << i << ",s=" << s;
548
- throw std::logic_error(oss.str());
506
+ skip_spaces();
507
+ break;
508
+ }
509
+ default:
510
+ std::ostringstream oss;
511
+ oss << "Unexpected flag" << c << ",i=" << i << ",s=" << s;
512
+ throw std::logic_error(oss.str());
549
513
  }
550
514
 
551
515
  return c == '1';
552
516
  }
553
517
 
554
518
 
555
- float SVGPathParser::parse_list_number()
556
- {
519
+ float SVGPathParser::parse_list_number() {
557
520
  if (i == l) {
558
521
  std::ostringstream oss;
559
522
  oss << "Unexpected end (s=" << s << ")";
@@ -561,16 +524,15 @@ float SVGPathParser::parse_list_number()
561
524
  }
562
525
 
563
526
  float n = parse_number();
564
- SkipSpaces();
527
+ skip_spaces();
565
528
  parse_list_separator();
566
529
 
567
530
  return n;
568
531
  }
569
532
 
570
- float SVGPathParser::parse_number()
571
- {
533
+ float SVGPathParser::parse_number() {
572
534
  // Strip off leading whitespaces.
573
- SkipSpaces();
535
+ skip_spaces();
574
536
 
575
537
  if (i == l) {
576
538
  std::ostringstream oss;
@@ -590,7 +552,7 @@ float SVGPathParser::parse_number()
590
552
 
591
553
  // Consume integer.
592
554
  if (c >= '0' && c <= '9') {
593
- SkipDigits();
555
+ skip_digits();
594
556
  if (i < l) {
595
557
  c = s.at(i);
596
558
  }
@@ -603,7 +565,7 @@ float SVGPathParser::parse_number()
603
565
  // Consume fraction.
604
566
  if (c == '.') {
605
567
  i += 1;
606
- SkipDigits();
568
+ skip_digits();
607
569
  if (i < l) {
608
570
  c = s.at(i);
609
571
  }
@@ -618,9 +580,9 @@ float SVGPathParser::parse_number()
618
580
 
619
581
  if (c == '+' || c == '-') {
620
582
  i += 1;
621
- SkipDigits();
583
+ skip_digits();
622
584
  } else if (c >= '0' && c <= '9') {
623
- SkipDigits();
585
+ skip_digits();
624
586
  } else {
625
587
  std::ostringstream oss;
626
588
  oss << "Invalid number formating character " << c << ",i=" << i << ",s=" << s;
@@ -628,8 +590,10 @@ float SVGPathParser::parse_number()
628
590
  }
629
591
  }
630
592
  }
593
+
631
594
  std::string num = s.substr(start, i);
632
595
  float n = std::stof(num);
596
+
633
597
  // inf, nan, etc. are an error.
634
598
  if (std::isinf(n) || std::isnan(n)) {
635
599
  std::ostringstream oss;
@@ -640,17 +604,14 @@ float SVGPathParser::parse_number()
640
604
  return n;
641
605
  }
642
606
 
643
- void SVGPathParser::parse_list_separator()
644
- {
607
+ void SVGPathParser::parse_list_separator() {
645
608
  if (i < l && s.at(i) == ',') {
646
609
  i += 1;
647
610
  }
648
611
  }
649
612
 
650
- void SVGPathParser::SkipDigits()
651
- {
652
- while (i < l && std::isdigit(s.at(i))) {
613
+ void SVGPathParser::skip_digits() {
614
+ while (i < l && std::isdigit(s.at(i)))
653
615
  i++;
654
- }
655
616
  }
656
617
  }; // namespace rnoh