Coverage Report

Created: 2024-07-05 06:13

/src/mupdf/source/xps/xps-path.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright (C) 2004-2024 Artifex Software, Inc.
2
//
3
// This file is part of MuPDF.
4
//
5
// MuPDF is free software: you can redistribute it and/or modify it under the
6
// terms of the GNU Affero General Public License as published by the Free
7
// Software Foundation, either version 3 of the License, or (at your option)
8
// any later version.
9
//
10
// MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
11
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
13
// details.
14
//
15
// You should have received a copy of the GNU Affero General Public License
16
// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
17
//
18
// Alternative licensing terms are available from the licensor.
19
// For commercial licensing, see <https://www.artifex.com/> or contact
20
// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
21
// CA 94129, USA, for further information.
22
23
#include "mupdf/fitz.h"
24
#include "xps-imp.h"
25
26
#include <math.h>
27
#include <string.h>
28
#include <stdlib.h>
29
30
static char *
31
xps_parse_float_array(fz_context *ctx, xps_document *doc, char *s, int num, int *obtained, float *x)
32
0
{
33
0
  int k = 0;
34
35
0
  if (s == NULL || *s == 0)
36
0
  {
37
0
    if (obtained)
38
0
      *obtained = k;
39
0
    return NULL;
40
0
  }
41
42
0
  while (*s)
43
0
  {
44
0
    while (*s == 0x0d || *s == '\t' || *s == ' ' || *s == 0x0a)
45
0
      s++;
46
0
    x[k] = fz_strtof(s, &s);
47
0
    while (*s == 0x0d || *s == '\t' || *s == ' ' || *s == 0x0a)
48
0
      s++;
49
0
    if (*s == ',')
50
0
      s++;
51
0
    if (++k == num)
52
0
      break;
53
0
  }
54
0
  if (obtained)
55
0
    *obtained = k;
56
0
  return s;
57
0
}
58
59
char *
60
xps_parse_point(fz_context *ctx, xps_document *doc, char *s_in, float *x, float *y)
61
0
{
62
0
  char *s_out = s_in;
63
0
  float xy[2];
64
0
  int obtained = 0;
65
66
0
  s_out = xps_parse_float_array(ctx, doc, s_out, 2, &obtained, &xy[0]);
67
0
  if (obtained >= 2)
68
0
  {
69
0
    *x = xy[0];
70
0
    *y = xy[1];
71
0
  }
72
0
  return s_out;
73
0
}
74
75
/* Draw an arc segment transformed by the matrix, we approximate with straight
76
 * line segments. We cannot use the fz_arc function because they only draw
77
 * circular arcs, we need to transform the line to make them elliptical but
78
 * without transforming the line width.
79
 *
80
 * We are guaranteed that on entry the point is at the point that would be
81
 * calculated by th0, and on exit, a point is generated for us at th0.
82
 */
83
static void
84
xps_draw_arc_segment(fz_context *ctx, xps_document *doc, fz_path *path, fz_matrix mtx, float th0, float th1, int iscw)
85
0
{
86
0
  float t, d;
87
0
  fz_point p;
88
89
0
  while (th1 < th0)
90
0
    th1 += FZ_PI * 2;
91
92
0
  d = FZ_PI / 180; /* 1-degree precision */
93
94
0
  if (iscw)
95
0
  {
96
0
    for (t = th0 + d; t < th1 - d/2; t += d)
97
0
    {
98
0
      p = fz_transform_point_xy(cosf(t), sinf(t), mtx);
99
0
      fz_lineto(ctx, path, p.x, p.y);
100
0
    }
101
0
  }
102
0
  else
103
0
  {
104
0
    th0 += FZ_PI * 2;
105
0
    for (t = th0 - d; t > th1 + d/2; t -= d)
106
0
    {
107
0
      p = fz_transform_point_xy(cosf(t), sinf(t), mtx);
108
0
      fz_lineto(ctx, path, p.x, p.y);
109
0
    }
110
0
  }
111
0
}
112
113
/* Given two vectors find the angle between them. */
114
static float
115
angle_between(fz_point u, fz_point v)
116
0
{
117
0
  float det = u.x * v.y - u.y * v.x;
118
0
  float sign = (det < 0 ? -1 : 1);
119
0
  float magu = u.x * u.x + u.y * u.y;
120
0
  float magv = v.x * v.x + v.y * v.y;
121
0
  float udotv = u.x * v.x + u.y * v.y;
122
0
  float t = udotv / (magu * magv);
123
  /* guard against rounding errors when near |1| (where acos will return NaN) */
124
0
  if (t < -1) t = -1;
125
0
  if (t > 1) t = 1;
126
0
  return sign * acosf(t);
127
0
}
128
129
/*
130
  Some explanation of the parameters here is warranted. See:
131
132
  http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
133
134
  Add an arc segment to path, that describes a section of an elliptical
135
  arc from the current point of path to (point_x,point_y), such that:
136
137
  The arc segment is taken from an elliptical arc of semi major radius
138
  size_x, semi minor radius size_y, where the semi major axis of the
139
  ellipse is rotated by rotation_angle.
140
141
  If is_large_arc, then the arc segment is selected to be > 180 degrees.
142
143
  If is_clockwise, then the arc sweeps clockwise.
144
*/
145
static void
146
xps_draw_arc(fz_context *ctx, xps_document *doc, fz_path *path,
147
  float size_x, float size_y, float rotation_angle,
148
  int is_large_arc, int is_clockwise,
149
  float point_x, float point_y)
150
0
{
151
0
  fz_matrix rotmat, revmat;
152
0
  fz_matrix mtx;
153
0
  fz_point pt;
154
0
  float rx, ry;
155
0
  float x1, y1, x2, y2;
156
0
  float x1t, y1t;
157
0
  float cxt, cyt, cx, cy;
158
0
  float t1, t2, t3;
159
0
  float sign;
160
0
  float th1, dth;
161
162
0
  pt = fz_currentpoint(ctx, path);
163
0
  x1 = pt.x;
164
0
  y1 = pt.y;
165
0
  x2 = point_x;
166
0
  y2 = point_y;
167
0
  rx = size_x;
168
0
  ry = size_y;
169
170
0
  if (is_clockwise != is_large_arc)
171
0
    sign = 1;
172
0
  else
173
0
    sign = -1;
174
175
0
  rotmat = fz_rotate(rotation_angle);
176
0
  revmat = fz_rotate(-rotation_angle);
177
178
  /* http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes */
179
  /* Conversion from endpoint to center parameterization */
180
181
  /* F.6.6.1 -- ensure radii are positive and non-zero */
182
0
  rx = fabsf(rx);
183
0
  ry = fabsf(ry);
184
0
  if (rx < 0.001f || ry < 0.001f || (x1 == x2 && y1 == y2))
185
0
  {
186
0
    fz_lineto(ctx, path, x2, y2);
187
0
    return;
188
0
  }
189
190
  /* F.6.5.1 */
191
0
  pt.x = (x1 - x2) / 2;
192
0
  pt.y = (y1 - y2) / 2;
193
0
  pt = fz_transform_vector(pt, revmat);
194
0
  x1t = pt.x;
195
0
  y1t = pt.y;
196
197
  /* F.6.6.2 -- ensure radii are large enough */
198
0
  t1 = (x1t * x1t) / (rx * rx) + (y1t * y1t) / (ry * ry);
199
0
  if (t1 > 1)
200
0
  {
201
0
    rx = rx * sqrtf(t1);
202
0
    ry = ry * sqrtf(t1);
203
0
  }
204
205
  /* F.6.5.2 */
206
0
  t1 = (rx * rx * ry * ry) - (rx * rx * y1t * y1t) - (ry * ry * x1t * x1t);
207
0
  t2 = (rx * rx * y1t * y1t) + (ry * ry * x1t * x1t);
208
0
  t3 = t1 / t2;
209
  /* guard against rounding errors; sqrt of negative numbers is bad for your health */
210
0
  if (t3 < 0) t3 = 0;
211
0
  t3 = sqrtf(t3);
212
213
0
  cxt = sign * t3 * (rx * y1t) / ry;
214
0
  cyt = sign * t3 * -(ry * x1t) / rx;
215
216
  /* F.6.5.3 */
217
0
  pt.x = cxt;
218
0
  pt.y = cyt;
219
0
  pt = fz_transform_vector(pt, rotmat);
220
0
  cx = pt.x + (x1 + x2) / 2;
221
0
  cy = pt.y + (y1 + y2) / 2;
222
223
  /* F.6.5.4 */
224
0
  {
225
0
    fz_point coord1, coord2, coord3, coord4;
226
0
    coord1.x = 1;
227
0
    coord1.y = 0;
228
0
    coord2.x = (x1t - cxt) / rx;
229
0
    coord2.y = (y1t - cyt) / ry;
230
0
    coord3.x = (x1t - cxt) / rx;
231
0
    coord3.y = (y1t - cyt) / ry;
232
0
    coord4.x = (-x1t - cxt) / rx;
233
0
    coord4.y = (-y1t - cyt) / ry;
234
0
    th1 = angle_between(coord1, coord2);
235
0
    dth = angle_between(coord3, coord4);
236
0
    if (dth < 0 && !is_clockwise)
237
0
      dth += ((FZ_PI / 180) * 360);
238
0
    if (dth > 0 && is_clockwise)
239
0
      dth -= ((FZ_PI / 180) * 360);
240
0
  }
241
242
0
  mtx = fz_pre_scale(fz_pre_rotate(fz_translate(cx, cy), rotation_angle), rx, ry);
243
0
  xps_draw_arc_segment(ctx, doc, path, mtx, th1, th1 + dth, is_clockwise);
244
245
0
  fz_lineto(ctx, path, point_x, point_y);
246
0
}
247
248
fz_path *
249
xps_parse_abbreviated_geometry(fz_context *ctx, xps_document *doc, char *geom, int *fill_rule)
250
0
{
251
0
  fz_path *path;
252
0
  char **args = NULL;
253
0
  char **pargs;
254
0
  char *s = geom;
255
0
  fz_point pt;
256
0
  int i, n;
257
0
  int cmd, old;
258
0
  float x1, y1, x2, y2, x3, y3;
259
0
  float smooth_x, smooth_y; /* saved cubic bezier control point for smooth curves */
260
0
  int reset_smooth;
261
262
0
  fz_var(args);
263
264
0
  path = fz_new_path(ctx);
265
266
0
  fz_try(ctx)
267
0
  {
268
0
    args = fz_malloc_array(ctx, strlen(geom) + 1, char*);
269
0
    pargs = args;
270
271
0
    while (*s)
272
0
    {
273
0
      if ((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z'))
274
0
      {
275
0
        *pargs++ = s++;
276
0
      }
277
0
      else if ((*s >= '0' && *s <= '9') || *s == '.' || *s == '+' || *s == '-' || *s == 'e' || *s == 'E')
278
0
      {
279
0
        *pargs++ = s;
280
0
        while ((*s >= '0' && *s <= '9') || *s == '.' || *s == '+' || *s == '-' || *s == 'e' || *s == 'E')
281
0
          s ++;
282
0
      }
283
0
      else
284
0
      {
285
0
        s++;
286
0
      }
287
0
    }
288
289
0
    *pargs = s;
290
291
0
    n = pargs - args;
292
0
    i = 0;
293
294
0
    old = 0;
295
296
0
    reset_smooth = 1;
297
0
    smooth_x = 0;
298
0
    smooth_y = 0;
299
300
0
    while (i < n)
301
0
    {
302
0
      cmd = args[i][0];
303
0
      if (cmd == '+' || cmd == '.' || cmd == '-' || (cmd >= '0' && cmd <= '9'))
304
0
        cmd = old; /* it's a number, repeat old command */
305
0
      else
306
0
        i ++;
307
308
0
      if (reset_smooth)
309
0
      {
310
0
        smooth_x = 0;
311
0
        smooth_y = 0;
312
0
      }
313
314
0
      reset_smooth = 1;
315
316
0
      switch (cmd)
317
0
      {
318
0
      case 'F':
319
0
        if (i >= n) break;
320
0
        *fill_rule = atoi(args[i]);
321
0
        i ++;
322
0
        break;
323
324
0
      case 'M':
325
0
        if (i + 1 >= n) break;
326
0
        fz_moveto(ctx, path, fz_atof(args[i]), fz_atof(args[i+1]));
327
0
        i += 2;
328
0
        break;
329
0
      case 'm':
330
0
        if (i + 1 >= n) break;
331
0
        pt = fz_currentpoint(ctx, path);
332
0
        fz_moveto(ctx, path, pt.x + fz_atof(args[i]), pt.y + fz_atof(args[i+1]));
333
0
        i += 2;
334
0
        break;
335
336
0
      case 'L':
337
0
        if (i + 1 >= n) break;
338
0
        fz_lineto(ctx, path, fz_atof(args[i]), fz_atof(args[i+1]));
339
0
        i += 2;
340
0
        break;
341
0
      case 'l':
342
0
        if (i + 1 >= n) break;
343
0
        pt = fz_currentpoint(ctx, path);
344
0
        fz_lineto(ctx, path, pt.x + fz_atof(args[i]), pt.y + fz_atof(args[i+1]));
345
0
        i += 2;
346
0
        break;
347
348
0
      case 'H':
349
0
        if (i >= n) break;
350
0
        pt = fz_currentpoint(ctx, path);
351
0
        fz_lineto(ctx, path, fz_atof(args[i]), pt.y);
352
0
        i += 1;
353
0
        break;
354
0
      case 'h':
355
0
        if (i >= n) break;
356
0
        pt = fz_currentpoint(ctx, path);
357
0
        fz_lineto(ctx, path, pt.x + fz_atof(args[i]), pt.y);
358
0
        i += 1;
359
0
        break;
360
361
0
      case 'V':
362
0
        if (i >= n) break;
363
0
        pt = fz_currentpoint(ctx, path);
364
0
        fz_lineto(ctx, path, pt.x, fz_atof(args[i]));
365
0
        i += 1;
366
0
        break;
367
0
      case 'v':
368
0
        if (i >= n) break;
369
0
        pt = fz_currentpoint(ctx, path);
370
0
        fz_lineto(ctx, path, pt.x, pt.y + fz_atof(args[i]));
371
0
        i += 1;
372
0
        break;
373
374
0
      case 'C':
375
0
        if (i + 5 >= n) break;
376
0
        x1 = fz_atof(args[i+0]);
377
0
        y1 = fz_atof(args[i+1]);
378
0
        x2 = fz_atof(args[i+2]);
379
0
        y2 = fz_atof(args[i+3]);
380
0
        x3 = fz_atof(args[i+4]);
381
0
        y3 = fz_atof(args[i+5]);
382
0
        fz_curveto(ctx, path, x1, y1, x2, y2, x3, y3);
383
0
        i += 6;
384
0
        reset_smooth = 0;
385
0
        smooth_x = x3 - x2;
386
0
        smooth_y = y3 - y2;
387
0
        break;
388
389
0
      case 'c':
390
0
        if (i + 5 >= n) break;
391
0
        pt = fz_currentpoint(ctx, path);
392
0
        x1 = fz_atof(args[i+0]) + pt.x;
393
0
        y1 = fz_atof(args[i+1]) + pt.y;
394
0
        x2 = fz_atof(args[i+2]) + pt.x;
395
0
        y2 = fz_atof(args[i+3]) + pt.y;
396
0
        x3 = fz_atof(args[i+4]) + pt.x;
397
0
        y3 = fz_atof(args[i+5]) + pt.y;
398
0
        fz_curveto(ctx, path, x1, y1, x2, y2, x3, y3);
399
0
        i += 6;
400
0
        reset_smooth = 0;
401
0
        smooth_x = x3 - x2;
402
0
        smooth_y = y3 - y2;
403
0
        break;
404
405
0
      case 'S':
406
0
        if (i + 3 >= n) break;
407
0
        pt = fz_currentpoint(ctx, path);
408
0
        x1 = fz_atof(args[i+0]);
409
0
        y1 = fz_atof(args[i+1]);
410
0
        x2 = fz_atof(args[i+2]);
411
0
        y2 = fz_atof(args[i+3]);
412
0
        fz_curveto(ctx, path, pt.x + smooth_x, pt.y + smooth_y, x1, y1, x2, y2);
413
0
        i += 4;
414
0
        reset_smooth = 0;
415
0
        smooth_x = x2 - x1;
416
0
        smooth_y = y2 - y1;
417
0
        break;
418
419
0
      case 's':
420
0
        if (i + 3 >= n) break;
421
0
        pt = fz_currentpoint(ctx, path);
422
0
        x1 = fz_atof(args[i+0]) + pt.x;
423
0
        y1 = fz_atof(args[i+1]) + pt.y;
424
0
        x2 = fz_atof(args[i+2]) + pt.x;
425
0
        y2 = fz_atof(args[i+3]) + pt.y;
426
0
        fz_curveto(ctx, path, pt.x + smooth_x, pt.y + smooth_y, x1, y1, x2, y2);
427
0
        i += 4;
428
0
        reset_smooth = 0;
429
0
        smooth_x = x2 - x1;
430
0
        smooth_y = y2 - y1;
431
0
        break;
432
433
0
      case 'Q':
434
0
        if (i + 3 >= n) break;
435
0
        x1 = fz_atof(args[i+0]);
436
0
        y1 = fz_atof(args[i+1]);
437
0
        x2 = fz_atof(args[i+2]);
438
0
        y2 = fz_atof(args[i+3]);
439
0
        fz_quadto(ctx, path, x1, y1, x2, y2);
440
0
        i += 4;
441
0
        break;
442
0
      case 'q':
443
0
        if (i + 3 >= n) break;
444
0
        pt = fz_currentpoint(ctx, path);
445
0
        x1 = fz_atof(args[i+0]) + pt.x;
446
0
        y1 = fz_atof(args[i+1]) + pt.y;
447
0
        x2 = fz_atof(args[i+2]) + pt.x;
448
0
        y2 = fz_atof(args[i+3]) + pt.y;
449
0
        fz_quadto(ctx, path, x1, y1, x2, y2);
450
0
        i += 4;
451
0
        break;
452
453
0
      case 'A':
454
0
        if (i + 6 >= n) break;
455
0
        xps_draw_arc(ctx, doc, path,
456
0
          fz_atof(args[i+0]), fz_atof(args[i+1]), fz_atof(args[i+2]),
457
0
          atoi(args[i+3]), atoi(args[i+4]),
458
0
          fz_atof(args[i+5]), fz_atof(args[i+6]));
459
0
        i += 7;
460
0
        break;
461
0
      case 'a':
462
0
        if (i + 6 >= n) break;
463
0
        pt = fz_currentpoint(ctx, path);
464
0
        xps_draw_arc(ctx, doc, path,
465
0
          fz_atof(args[i+0]), fz_atof(args[i+1]), fz_atof(args[i+2]),
466
0
          atoi(args[i+3]), atoi(args[i+4]),
467
0
          fz_atof(args[i+5]) + pt.x, fz_atof(args[i+6]) + pt.y);
468
0
        i += 7;
469
0
        break;
470
471
0
      case 'Z':
472
0
      case 'z':
473
0
        fz_closepath(ctx, path);
474
0
        break;
475
476
0
      default:
477
0
        fz_warn(ctx, "ignoring invalid command '%c'", cmd);
478
0
        if (old == cmd) /* avoid infinite loop */
479
0
          i++;
480
0
        break;
481
0
      }
482
483
0
      old = cmd;
484
0
    }
485
0
  }
486
0
  fz_always(ctx)
487
0
    fz_free(ctx, args);
488
0
  fz_catch(ctx)
489
0
  {
490
0
    fz_drop_path(ctx, path);
491
0
    fz_rethrow(ctx);
492
0
  }
493
494
0
  return path;
495
0
}
496
497
static void
498
xps_parse_arc_segment(fz_context *ctx, xps_document *doc, fz_path *path, fz_xml *root, int stroking, int *skipped_stroke)
499
0
{
500
  /* ArcSegment pretty much follows the SVG algorithm for converting an
501
   * arc in endpoint representation to an arc in centerpoint
502
   * representation. Once in centerpoint it can be given to the
503
   * graphics library in the form of a postscript arc. */
504
505
0
  float rotation_angle;
506
0
  int is_large_arc, is_clockwise;
507
0
  float point_x, point_y;
508
0
  float size_x, size_y;
509
0
  int is_stroked;
510
511
0
  char *point_att = fz_xml_att(root, "Point");
512
0
  char *size_att = fz_xml_att(root, "Size");
513
0
  char *rotation_angle_att = fz_xml_att(root, "RotationAngle");
514
0
  char *is_large_arc_att = fz_xml_att(root, "IsLargeArc");
515
0
  char *sweep_direction_att = fz_xml_att(root, "SweepDirection");
516
0
  char *is_stroked_att = fz_xml_att(root, "IsStroked");
517
518
0
  if (!point_att || !size_att || !rotation_angle_att || !is_large_arc_att || !sweep_direction_att)
519
0
  {
520
0
    fz_warn(ctx, "ArcSegment element is missing attributes");
521
0
    return;
522
0
  }
523
524
0
  is_stroked = 1;
525
0
  if (is_stroked_att && !strcmp(is_stroked_att, "false"))
526
0
      is_stroked = 0;
527
0
  if (!is_stroked)
528
0
    *skipped_stroke = 1;
529
530
0
  point_x = point_y = 0;
531
0
  size_x = size_y = 0;
532
533
0
  xps_parse_point(ctx, doc, point_att, &point_x, &point_y);
534
0
  xps_parse_point(ctx, doc, size_att, &size_x, &size_y);
535
0
  rotation_angle = fz_atof(rotation_angle_att);
536
0
  is_large_arc = !strcmp(is_large_arc_att, "true");
537
0
  is_clockwise = !strcmp(sweep_direction_att, "Clockwise");
538
539
0
  if (stroking && !is_stroked)
540
0
  {
541
0
    fz_moveto(ctx, path, point_x, point_y);
542
0
    return;
543
0
  }
544
545
0
  xps_draw_arc(ctx, doc, path, size_x, size_y, rotation_angle, is_large_arc, is_clockwise, point_x, point_y);
546
0
}
547
548
static void
549
xps_parse_poly_quadratic_bezier_segment(fz_context *ctx, xps_document *doc, fz_path *path, fz_xml *root, int stroking, int *skipped_stroke)
550
0
{
551
0
  char *points_att = fz_xml_att(root, "Points");
552
0
  char *is_stroked_att = fz_xml_att(root, "IsStroked");
553
0
  float x[2], y[2];
554
0
  int is_stroked;
555
0
  fz_point pt;
556
0
  char *s;
557
0
  int n;
558
559
0
  if (!points_att)
560
0
  {
561
0
    fz_warn(ctx, "PolyQuadraticBezierSegment element has no points");
562
0
    return;
563
0
  }
564
565
0
  is_stroked = 1;
566
0
  if (is_stroked_att && !strcmp(is_stroked_att, "false"))
567
0
      is_stroked = 0;
568
0
  if (!is_stroked)
569
0
    *skipped_stroke = 1;
570
571
0
  s = points_att;
572
0
  n = 0;
573
0
  while (*s != 0)
574
0
  {
575
0
    while (*s == ' ') s++;
576
0
    x[n] = y[n] = 0;
577
0
    s = xps_parse_point(ctx, doc, s, &x[n], &y[n]);
578
0
    n ++;
579
0
    if (n == 2)
580
0
    {
581
0
      if (stroking && !is_stroked)
582
0
      {
583
0
        fz_moveto(ctx, path, x[1], y[1]);
584
0
      }
585
0
      else
586
0
      {
587
0
        pt = fz_currentpoint(ctx, path);
588
0
        fz_curveto(ctx, path,
589
0
            (pt.x + 2 * x[0]) / 3, (pt.y + 2 * y[0]) / 3,
590
0
            (x[1] + 2 * x[0]) / 3, (y[1] + 2 * y[0]) / 3,
591
0
            x[1], y[1]);
592
0
      }
593
0
      n = 0;
594
0
    }
595
0
  }
596
0
}
597
598
static void
599
xps_parse_poly_bezier_segment(fz_context *ctx, xps_document *doc, fz_path *path, fz_xml *root, int stroking, int *skipped_stroke)
600
0
{
601
0
  char *points_att = fz_xml_att(root, "Points");
602
0
  char *is_stroked_att = fz_xml_att(root, "IsStroked");
603
0
  float x[3], y[3];
604
0
  int is_stroked;
605
0
  char *s;
606
0
  int n;
607
608
0
  if (!points_att)
609
0
  {
610
0
    fz_warn(ctx, "PolyBezierSegment element has no points");
611
0
    return;
612
0
  }
613
614
0
  is_stroked = 1;
615
0
  if (is_stroked_att && !strcmp(is_stroked_att, "false"))
616
0
      is_stroked = 0;
617
0
  if (!is_stroked)
618
0
    *skipped_stroke = 1;
619
620
0
  s = points_att;
621
0
  n = 0;
622
0
  while (*s != 0)
623
0
  {
624
0
    while (*s == ' ') s++;
625
0
    x[n] = y[n] = 0;
626
0
    s = xps_parse_point(ctx, doc, s, &x[n], &y[n]);
627
0
    n ++;
628
0
    if (n == 3)
629
0
    {
630
0
      if (stroking && !is_stroked)
631
0
        fz_moveto(ctx, path, x[2], y[2]);
632
0
      else
633
0
        fz_curveto(ctx, path, x[0], y[0], x[1], y[1], x[2], y[2]);
634
0
      n = 0;
635
0
    }
636
0
  }
637
0
}
638
639
static void
640
xps_parse_poly_line_segment(fz_context *ctx, xps_document *doc, fz_path *path, fz_xml *root, int stroking, int *skipped_stroke)
641
0
{
642
0
  char *points_att = fz_xml_att(root, "Points");
643
0
  char *is_stroked_att = fz_xml_att(root, "IsStroked");
644
0
  int is_stroked;
645
0
  float x, y;
646
0
  char *s;
647
648
0
  if (!points_att)
649
0
  {
650
0
    fz_warn(ctx, "PolyLineSegment element has no points");
651
0
    return;
652
0
  }
653
654
0
  is_stroked = 1;
655
0
  if (is_stroked_att && !strcmp(is_stroked_att, "false"))
656
0
      is_stroked = 0;
657
0
  if (!is_stroked)
658
0
    *skipped_stroke = 1;
659
660
0
  s = points_att;
661
0
  while (*s != 0)
662
0
  {
663
0
    while (*s == ' ') s++;
664
0
    x = y = 0;
665
0
    s = xps_parse_point(ctx, doc, s, &x, &y);
666
0
    if (stroking && !is_stroked)
667
0
      fz_moveto(ctx, path, x, y);
668
0
    else
669
0
      fz_lineto(ctx, path, x, y);
670
0
  }
671
0
}
672
673
static void
674
xps_parse_path_figure(fz_context *ctx, xps_document *doc, fz_path *path, fz_xml *root, int stroking)
675
0
{
676
0
  fz_xml *node;
677
678
0
  char *is_closed_att;
679
0
  char *start_point_att;
680
0
  char *is_filled_att;
681
682
0
  int is_closed = 0;
683
0
  int is_filled = 1;
684
0
  float start_x = 0;
685
0
  float start_y = 0;
686
687
0
  int skipped_stroke = 0;
688
689
0
  is_closed_att = fz_xml_att(root, "IsClosed");
690
0
  start_point_att = fz_xml_att(root, "StartPoint");
691
0
  is_filled_att = fz_xml_att(root, "IsFilled");
692
693
0
  if (is_closed_att)
694
0
    is_closed = !strcmp(is_closed_att, "true");
695
0
  if (is_filled_att)
696
0
    is_filled = !strcmp(is_filled_att, "true");
697
0
  if (start_point_att)
698
0
    xps_parse_point(ctx, doc, start_point_att, &start_x, &start_y);
699
700
0
  if (!stroking && !is_filled) /* not filled, when filling */
701
0
    return;
702
703
0
  fz_moveto(ctx, path, start_x, start_y);
704
705
0
  for (node = fz_xml_down(root); node; node = fz_xml_next(node))
706
0
  {
707
0
    if (fz_xml_is_tag(node, "ArcSegment"))
708
0
      xps_parse_arc_segment(ctx, doc, path, node, stroking, &skipped_stroke);
709
0
    if (fz_xml_is_tag(node, "PolyBezierSegment"))
710
0
      xps_parse_poly_bezier_segment(ctx, doc, path, node, stroking, &skipped_stroke);
711
0
    if (fz_xml_is_tag(node, "PolyLineSegment"))
712
0
      xps_parse_poly_line_segment(ctx, doc, path, node, stroking, &skipped_stroke);
713
0
    if (fz_xml_is_tag(node, "PolyQuadraticBezierSegment"))
714
0
      xps_parse_poly_quadratic_bezier_segment(ctx, doc, path, node, stroking, &skipped_stroke);
715
0
  }
716
717
0
  if (is_closed)
718
0
  {
719
0
    if (stroking && skipped_stroke)
720
0
      fz_lineto(ctx, path, start_x, start_y); /* we've skipped using fz_moveto... */
721
0
    else
722
0
      fz_closepath(ctx, path); /* no skipped segments, safe to closepath properly */
723
0
  }
724
0
}
725
726
fz_path *
727
xps_parse_path_geometry(fz_context *ctx, xps_document *doc, xps_resource *dict, fz_xml *root, int stroking, int *fill_rule)
728
0
{
729
0
  fz_xml *node;
730
731
0
  char *figures_att;
732
0
  char *fill_rule_att;
733
0
  char *transform_att;
734
735
0
  fz_xml *transform_tag = NULL;
736
0
  fz_xml *figures_tag = NULL; /* only used by resource */
737
738
0
  fz_matrix transform;
739
0
  fz_path *path;
740
741
0
  figures_att = fz_xml_att(root, "Figures");
742
0
  fill_rule_att = fz_xml_att(root, "FillRule");
743
0
  transform_att = fz_xml_att(root, "Transform");
744
745
0
  for (node = fz_xml_down(root); node; node = fz_xml_next(node))
746
0
  {
747
0
    if (fz_xml_is_tag(node, "PathGeometry.Transform"))
748
0
      transform_tag = fz_xml_down(node);
749
0
  }
750
751
0
  xps_resolve_resource_reference(ctx, doc, dict, &transform_att, &transform_tag, NULL);
752
0
  xps_resolve_resource_reference(ctx, doc, dict, &figures_att, &figures_tag, NULL);
753
754
0
  if (fill_rule_att)
755
0
  {
756
0
    if (!strcmp(fill_rule_att, "NonZero"))
757
0
      *fill_rule = 1;
758
0
    if (!strcmp(fill_rule_att, "EvenOdd"))
759
0
      *fill_rule = 0;
760
0
  }
761
762
0
  transform = xps_parse_transform(ctx, doc, transform_att, transform_tag, fz_identity);
763
764
0
  if (figures_att)
765
0
    path = xps_parse_abbreviated_geometry(ctx, doc, figures_att, fill_rule);
766
0
  else
767
0
    path = fz_new_path(ctx);
768
769
0
  fz_try(ctx)
770
0
  {
771
0
    if (figures_tag)
772
0
      xps_parse_path_figure(ctx, doc, path, figures_tag, stroking);
773
774
0
    for (node = fz_xml_down(root); node; node = fz_xml_next(node))
775
0
    {
776
0
      if (fz_xml_is_tag(node, "PathFigure"))
777
0
        xps_parse_path_figure(ctx, doc, path, node, stroking);
778
0
    }
779
780
0
    if (transform_att || transform_tag)
781
0
      fz_transform_path(ctx, path, transform);
782
0
  }
783
0
  fz_catch(ctx)
784
0
  {
785
0
    fz_drop_path(ctx, path);
786
0
    fz_rethrow(ctx);
787
0
  }
788
789
0
  return path;
790
0
}
791
792
static int
793
xps_parse_line_cap(char *attr)
794
0
{
795
0
  if (attr)
796
0
  {
797
0
    if (!strcmp(attr, "Flat")) return 0;
798
0
    if (!strcmp(attr, "Round")) return 1;
799
0
    if (!strcmp(attr, "Square")) return 2;
800
0
    if (!strcmp(attr, "Triangle")) return 3;
801
0
  }
802
0
  return 0;
803
0
}
804
805
void
806
xps_clip(fz_context *ctx, xps_document *doc, fz_matrix ctm, xps_resource *dict, char *clip_att, fz_xml *clip_tag)
807
0
{
808
0
  fz_device *dev = doc->dev;
809
0
  fz_path *path;
810
0
  int fill_rule = 0;
811
812
0
  if (clip_att)
813
0
    path = xps_parse_abbreviated_geometry(ctx, doc, clip_att, &fill_rule);
814
0
  else if (clip_tag)
815
0
    path = xps_parse_path_geometry(ctx, doc, dict, clip_tag, 0, &fill_rule);
816
0
  else
817
0
    path = fz_new_path(ctx);
818
0
  fz_try(ctx)
819
0
    fz_clip_path(ctx, dev, path, fill_rule == 0, ctm, fz_infinite_rect);
820
0
  fz_always(ctx)
821
0
    fz_drop_path(ctx, path);
822
0
  fz_catch(ctx)
823
0
    fz_rethrow(ctx);
824
0
}
825
826
void
827
xps_parse_path(fz_context *ctx, xps_document *doc, fz_matrix ctm, char *base_uri, xps_resource *dict, fz_xml *root)
828
0
{
829
0
  fz_device *dev = doc->dev;
830
831
0
  fz_xml *node;
832
833
0
  char *fill_uri;
834
0
  char *stroke_uri;
835
0
  char *opacity_mask_uri;
836
837
0
  char *transform_att;
838
0
  char *clip_att;
839
0
  char *data_att;
840
0
  char *fill_att;
841
0
  char *stroke_att;
842
0
  char *opacity_att;
843
0
  char *opacity_mask_att;
844
845
0
  fz_xml *transform_tag = NULL;
846
0
  fz_xml *clip_tag = NULL;
847
0
  fz_xml *data_tag = NULL;
848
0
  fz_xml *fill_tag = NULL;
849
0
  fz_xml *stroke_tag = NULL;
850
0
  fz_xml *opacity_mask_tag = NULL;
851
852
0
  char *fill_opacity_att = NULL;
853
0
  char *stroke_opacity_att = NULL;
854
855
0
  char *stroke_dash_array_att;
856
0
  char *stroke_dash_cap_att;
857
0
  char *stroke_dash_offset_att;
858
0
  char *stroke_end_line_cap_att;
859
0
  char *stroke_start_line_cap_att;
860
0
  char *stroke_line_join_att;
861
0
  char *stroke_miter_limit_att;
862
0
  char *stroke_thickness_att;
863
864
0
  fz_stroke_state *stroke = NULL;
865
0
  float samples[FZ_MAX_COLORS];
866
0
  fz_colorspace *colorspace;
867
0
  fz_path *path = NULL;
868
0
  fz_path *stroke_path = NULL;
869
0
  fz_rect area;
870
0
  int fill_rule;
871
0
  int dash_len = 0;
872
873
  /*
874
   * Extract attributes and extended attributes.
875
   */
876
877
0
  transform_att = fz_xml_att(root, "RenderTransform");
878
0
  clip_att = fz_xml_att(root, "Clip");
879
0
  data_att = fz_xml_att(root, "Data");
880
0
  fill_att = fz_xml_att(root, "Fill");
881
0
  stroke_att = fz_xml_att(root, "Stroke");
882
0
  opacity_att = fz_xml_att(root, "Opacity");
883
0
  opacity_mask_att = fz_xml_att(root, "OpacityMask");
884
885
0
  stroke_dash_array_att = fz_xml_att(root, "StrokeDashArray");
886
0
  stroke_dash_cap_att = fz_xml_att(root, "StrokeDashCap");
887
0
  stroke_dash_offset_att = fz_xml_att(root, "StrokeDashOffset");
888
0
  stroke_end_line_cap_att = fz_xml_att(root, "StrokeEndLineCap");
889
0
  stroke_start_line_cap_att = fz_xml_att(root, "StrokeStartLineCap");
890
0
  stroke_line_join_att = fz_xml_att(root, "StrokeLineJoin");
891
0
  stroke_miter_limit_att = fz_xml_att(root, "StrokeMiterLimit");
892
0
  stroke_thickness_att = fz_xml_att(root, "StrokeThickness");
893
894
0
  for (node = fz_xml_down(root); node; node = fz_xml_next(node))
895
0
  {
896
0
    if (fz_xml_is_tag(node, "Path.RenderTransform"))
897
0
      transform_tag = fz_xml_down(node);
898
0
    if (fz_xml_is_tag(node, "Path.OpacityMask"))
899
0
      opacity_mask_tag = fz_xml_down(node);
900
0
    if (fz_xml_is_tag(node, "Path.Clip"))
901
0
      clip_tag = fz_xml_down(node);
902
0
    if (fz_xml_is_tag(node, "Path.Fill"))
903
0
      fill_tag = fz_xml_down(node);
904
0
    if (fz_xml_is_tag(node, "Path.Stroke"))
905
0
      stroke_tag = fz_xml_down(node);
906
0
    if (fz_xml_is_tag(node, "Path.Data"))
907
0
      data_tag = fz_xml_down(node);
908
0
  }
909
910
0
  fill_uri = base_uri;
911
0
  stroke_uri = base_uri;
912
0
  opacity_mask_uri = base_uri;
913
914
0
  xps_resolve_resource_reference(ctx, doc, dict, &data_att, &data_tag, NULL);
915
0
  xps_resolve_resource_reference(ctx, doc, dict, &clip_att, &clip_tag, NULL);
916
0
  xps_resolve_resource_reference(ctx, doc, dict, &transform_att, &transform_tag, NULL);
917
0
  xps_resolve_resource_reference(ctx, doc, dict, &fill_att, &fill_tag, &fill_uri);
918
0
  xps_resolve_resource_reference(ctx, doc, dict, &stroke_att, &stroke_tag, &stroke_uri);
919
0
  xps_resolve_resource_reference(ctx, doc, dict, &opacity_mask_att, &opacity_mask_tag, &opacity_mask_uri);
920
921
  /*
922
   * Act on the information we have gathered:
923
   */
924
925
0
  if (!data_att && !data_tag)
926
0
    return;
927
928
0
  if (fz_xml_is_tag(fill_tag, "SolidColorBrush"))
929
0
  {
930
0
    fill_opacity_att = fz_xml_att(fill_tag, "Opacity");
931
0
    fill_att = fz_xml_att(fill_tag, "Color");
932
0
    fill_tag = NULL;
933
0
  }
934
935
0
  if (fz_xml_is_tag(stroke_tag, "SolidColorBrush"))
936
0
  {
937
0
    stroke_opacity_att = fz_xml_att(stroke_tag, "Opacity");
938
0
    stroke_att = fz_xml_att(stroke_tag, "Color");
939
0
    stroke_tag = NULL;
940
0
  }
941
942
0
  if (stroke_att || stroke_tag)
943
0
  {
944
0
    if (stroke_dash_array_att)
945
0
    {
946
0
      char *s = stroke_dash_array_att;
947
948
0
      while (*s)
949
0
      {
950
0
        while (*s == ' ')
951
0
          s++;
952
0
        if (*s) /* needed in case of a space before the last quote */
953
0
          dash_len++;
954
955
0
        while (*s && *s != ' ')
956
0
          s++;
957
0
      }
958
0
    }
959
0
    stroke = fz_new_stroke_state_with_dash_len(ctx, dash_len);
960
0
    stroke->start_cap = xps_parse_line_cap(stroke_start_line_cap_att);
961
0
    stroke->dash_cap = xps_parse_line_cap(stroke_dash_cap_att);
962
0
    stroke->end_cap = xps_parse_line_cap(stroke_end_line_cap_att);
963
964
0
    stroke->linejoin = FZ_LINEJOIN_MITER_XPS;
965
0
    if (stroke_line_join_att)
966
0
    {
967
0
      if (!strcmp(stroke_line_join_att, "Miter")) stroke->linejoin = FZ_LINEJOIN_MITER_XPS;
968
0
      if (!strcmp(stroke_line_join_att, "Round")) stroke->linejoin = FZ_LINEJOIN_ROUND;
969
0
      if (!strcmp(stroke_line_join_att, "Bevel")) stroke->linejoin = FZ_LINEJOIN_BEVEL;
970
0
    }
971
972
0
    stroke->miterlimit = 10;
973
0
    if (stroke_miter_limit_att)
974
0
      stroke->miterlimit = fz_atof(stroke_miter_limit_att);
975
976
0
    stroke->linewidth = 1;
977
0
    if (stroke_thickness_att)
978
0
      stroke->linewidth = fz_atof(stroke_thickness_att);
979
980
0
    stroke->dash_phase = 0;
981
0
    stroke->dash_len = 0;
982
0
    if (stroke_dash_array_att)
983
0
    {
984
0
      char *s = stroke_dash_array_att;
985
986
0
      if (stroke_dash_offset_att)
987
0
        stroke->dash_phase = fz_atof(stroke_dash_offset_att) * stroke->linewidth;
988
989
0
      while (*s)
990
0
      {
991
0
        while (*s == ' ')
992
0
          s++;
993
0
        if (*s) /* needed in case of a space before the last quote */
994
0
          stroke->dash_list[stroke->dash_len++] = fz_atof(s) * stroke->linewidth;
995
0
        while (*s && *s != ' ')
996
0
          s++;
997
0
      }
998
0
      if (dash_len > 0)
999
0
      {
1000
        /* fz_stroke_path doesn't draw non-empty paths with phase length zero */
1001
0
        float phase_len = 0;
1002
0
        int i;
1003
0
        for (i = 0; i < dash_len; i++)
1004
0
          phase_len += stroke->dash_list[i];
1005
0
        if (phase_len == 0)
1006
0
          dash_len = 0;
1007
0
      }
1008
0
      stroke->dash_len = dash_len;
1009
0
    }
1010
0
  }
1011
1012
0
  ctm = xps_parse_transform(ctx, doc, transform_att, transform_tag, ctm);
1013
1014
0
  if (clip_att || clip_tag)
1015
0
    xps_clip(ctx, doc, ctm, dict, clip_att, clip_tag);
1016
1017
0
  fz_try(ctx)
1018
0
  {
1019
0
    fill_rule = 0;
1020
0
    if (data_att)
1021
0
      path = xps_parse_abbreviated_geometry(ctx, doc, data_att, &fill_rule);
1022
0
    else if (data_tag)
1023
0
    {
1024
0
      path = xps_parse_path_geometry(ctx, doc, dict, data_tag, 0, &fill_rule);
1025
      // /home/sebras/src/jxr/fts_06xx.xps
1026
0
      if (stroke_att || stroke_tag)
1027
0
        stroke_path = xps_parse_path_geometry(ctx, doc, dict, data_tag, 1, &fill_rule);
1028
0
    }
1029
0
    if (!stroke_path)
1030
0
      stroke_path = path;
1031
1032
0
    if (stroke_att || stroke_tag)
1033
0
    {
1034
0
      area = fz_bound_path(ctx, stroke_path, stroke, ctm);
1035
0
      if (stroke_path != path && (fill_att || fill_tag)) {
1036
0
        fz_rect bounds = fz_bound_path(ctx, path, NULL, ctm);
1037
0
        area = fz_union_rect(area, bounds);
1038
0
      }
1039
0
    }
1040
0
    else
1041
0
      area = fz_bound_path(ctx, path, NULL, ctm);
1042
1043
0
    xps_begin_opacity(ctx, doc, ctm, area, opacity_mask_uri, dict, opacity_att, opacity_mask_tag);
1044
1045
0
    if (fill_att)
1046
0
    {
1047
0
      xps_parse_color(ctx, doc, base_uri, fill_att, &colorspace, samples);
1048
0
      if (fill_opacity_att)
1049
0
        samples[0] *= fz_atof(fill_opacity_att);
1050
0
      xps_set_color(ctx, doc, colorspace, samples);
1051
0
      fz_fill_path(ctx, dev, path, fill_rule == 0, ctm,
1052
0
        doc->colorspace, doc->color, doc->alpha, fz_default_color_params);
1053
0
    }
1054
1055
0
    if (fill_tag)
1056
0
    {
1057
0
      fz_clip_path(ctx, dev, path, fill_rule == 0, ctm, area);
1058
0
      xps_parse_brush(ctx, doc, ctm, area, fill_uri, dict, fill_tag);
1059
0
      fz_pop_clip(ctx, dev);
1060
0
    }
1061
1062
0
    if (stroke_att)
1063
0
    {
1064
0
      xps_parse_color(ctx, doc, base_uri, stroke_att, &colorspace, samples);
1065
0
      if (stroke_opacity_att)
1066
0
        samples[0] *= fz_atof(stroke_opacity_att);
1067
0
      xps_set_color(ctx, doc, colorspace, samples);
1068
0
      fz_stroke_path(ctx, dev, stroke_path, stroke, ctm,
1069
0
        doc->colorspace, doc->color, doc->alpha, fz_default_color_params);
1070
0
    }
1071
1072
0
    if (stroke_tag)
1073
0
    {
1074
0
      fz_clip_stroke_path(ctx, dev, stroke_path, stroke, ctm, area);
1075
0
      xps_parse_brush(ctx, doc, ctm, area, stroke_uri, dict, stroke_tag);
1076
0
      fz_pop_clip(ctx, dev);
1077
0
    }
1078
1079
0
    xps_end_opacity(ctx, doc, opacity_mask_uri, dict, opacity_att, opacity_mask_tag);
1080
0
  }
1081
0
  fz_always(ctx)
1082
0
  {
1083
0
    if (stroke_path != path)
1084
0
      fz_drop_path(ctx, stroke_path);
1085
0
    fz_drop_path(ctx, path);
1086
0
    fz_drop_stroke_state(ctx, stroke);
1087
0
  }
1088
0
  fz_catch(ctx)
1089
0
    fz_rethrow(ctx);
1090
1091
0
  if (clip_att || clip_tag)
1092
0
    fz_pop_clip(ctx, dev);
1093
0
}