Coverage Report

Created: 2024-02-11 06:24

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