1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
|
#include "mupdf/fitz.h"
typedef struct svg_device_s svg_device;
typedef struct tile_s tile;
struct tile_s
{
int pattern;
fz_matrix ctm;
fz_rect view;
fz_rect area;
fz_point step;
};
struct svg_device_s
{
fz_context *ctx;
fz_output *out;
int id;
int num_tiles;
int max_tiles;
tile *tiles;
};
/* Helper functions */
static void
svg_dev_path(svg_device *sdev, fz_path *path)
{
fz_output *out = sdev->out;
float x, y;
int i = 0;
fz_printf(out, " d=\"");
while (i < path->len)
{
switch (path->items[i++].k)
{
case FZ_MOVETO:
x = path->items[i++].v;
y = path->items[i++].v;
fz_printf(out, "M %g %g ", x, y);
break;
case FZ_LINETO:
x = path->items[i++].v;
y = path->items[i++].v;
fz_printf(out, "L %g %g ", x, y);
break;
case FZ_CURVETO:
x = path->items[i++].v;
y = path->items[i++].v;
fz_printf(out, "C %g %g ", x, y);
x = path->items[i++].v;
y = path->items[i++].v;
fz_printf(out, "%g %g ", x, y);
x = path->items[i++].v;
y = path->items[i++].v;
fz_printf(out, "%g %g ", x, y);
break;
case FZ_CLOSE_PATH:
fz_printf(out, "Z ");
break;
}
}
fz_printf(out, "\"");
}
static void
svg_dev_ctm(svg_device *sdev, const fz_matrix *ctm)
{
fz_output *out = sdev->out;
if (ctm->a != 1.0 || ctm->b != 0 || ctm->c != 0 || ctm->d != 1.0 || ctm->e != 0 || ctm->f != 0)
{
fz_printf(out, " transform=\"matrix(%g,%g,%g,%g,%g,%g)\"",
ctm->a, ctm->b, ctm->c, ctm->d, ctm->e, ctm->f);
}
}
static void
svg_dev_stroke_state(svg_device *sdev, fz_stroke_state *stroke_state)
{
fz_output *out = sdev->out;
fz_printf(out, " stroke-width=\"%g\"", stroke_state->linewidth);
fz_printf(out, " stroke-linecap=\"%s\"",
(stroke_state->start_cap == FZ_LINECAP_SQUARE ? "square" :
(stroke_state->start_cap == FZ_LINECAP_ROUND ? "round" : "butt")));
if (stroke_state->dash_len != 0)
{
int i;
fz_printf(out, " stroke-dasharray=");
for (i = 0; i < stroke_state->dash_len; i++)
fz_printf(out, "%c%g", (i == 0 ? '\"' : ','), stroke_state->dash_list[i]);
fz_printf(out, "\"");
if (stroke_state->dash_phase != 0)
fz_printf(out, " stroke-dashoffset=\"%g\"", stroke_state->dash_phase);
}
if (stroke_state->linejoin == FZ_LINEJOIN_MITER || stroke_state->linejoin == FZ_LINEJOIN_MITER_XPS)
fz_printf(out, " stroke-miterlimit=\"%g\"", stroke_state->miterlimit);
fz_printf(out, " stroke-linejoin=\"%s\"",
(stroke_state->linejoin == FZ_LINEJOIN_BEVEL ? "bevel" :
(stroke_state->linejoin == FZ_LINEJOIN_ROUND ? "round" : "miter")));
}
static void
svg_dev_fill_color(svg_device *sdev, fz_colorspace *colorspace, float *color, float alpha)
{
fz_context *ctx = sdev->ctx;
fz_output *out = sdev->out;
float rgb[FZ_MAX_COLORS];
if (colorspace != fz_device_rgb(ctx))
{
/* If it's not rgb, make it rgb */
colorspace->to_rgb(ctx, colorspace, color, rgb);
color = rgb;
}
if (color[0] == 0 && color[1] == 0 && color[2] == 0)
{
/* don't send a fill, as it will be assumed to be black */
}
else
fz_printf(out, " fill=\"rgb(%d,%d,%d)\"", (int)(255*color[0] + 0.5), (int)(255*color[1] + 0.5), (int)(255*color[2]+0.5));
if (alpha != 1)
fz_printf(out, " fill-opacity=\"%g\"", alpha);
}
static void
svg_dev_stroke_color(svg_device *sdev, fz_colorspace *colorspace, float *color, float alpha)
{
fz_context *ctx = sdev->ctx;
fz_output *out = sdev->out;
float rgb[FZ_MAX_COLORS];
if (colorspace != fz_device_rgb(ctx))
{
/* If it's not rgb, make it rgb */
colorspace->to_rgb(ctx, colorspace, color, rgb);
color = rgb;
}
fz_printf(out, " fill=\"none\" stroke=\"rgb(%d,%d,%d)\"", (int)(255*color[0] + 0.5), (int)(255*color[1] + 0.5), (int)(255*color[2]+0.5));
if (alpha != 1)
fz_printf(out, " stroke-opacity=\"%g\"", alpha);
}
static void
svg_dev_text(svg_device *sdev, const fz_matrix *ctm, fz_text *text)
{
fz_output *out = sdev->out;
int i;
fz_matrix inverse;
fz_matrix local_trm;
float size;
/* Rely on the fact that trm.{e,f} == 0 */
size = fz_matrix_expansion(&text->trm);
local_trm.a = text->trm.a / size;
local_trm.b = text->trm.b / size;
local_trm.c = -text->trm.c / size;
local_trm.d = -text->trm.d / size;
local_trm.e = 0;
local_trm.f = 0;
fz_invert_matrix(&inverse, &local_trm);
fz_concat(&local_trm, &local_trm, ctm);
fz_printf(out, " transform=\"matrix(%g,%g,%g,%g,%g,%g)\"",
local_trm.a, local_trm.b, local_trm.c, local_trm.d, local_trm.e, local_trm.f);
fz_printf(out, " font-size=\"%g\"", size);
fz_printf(out, " font-family=\"%s\"", text->font->name);
fz_printf(out, " x=");
for (i=0; i < text->len; i++)
{
fz_text_item *it = &text->items[i];
fz_point p;
p.x = it->x;
p.y = it->y;
fz_transform_point(&p, &inverse);
fz_printf(out, "%c%g", i == 0 ? '\"' : ' ', p.x);
}
fz_printf(out, "\" y=");
for (i=0; i < text->len; i++)
{
fz_text_item *it = &text->items[i];
fz_point p;
p.x = it->x;
p.y = it->y;
fz_transform_point(&p, &inverse);
fz_printf(out, "%c%g", i == 0 ? '\"' : ' ', p.y);
}
fz_printf(out, "\">\n");
for (i=0; i < text->len; i++)
{
fz_text_item *it = &text->items[i];
int c = it->ucs;
if (c >= 32 && c <= 127 && c != '<' && c != '&')
fz_printf(out, "%c", c);
else
fz_printf(out, "&#x%04x;", c);
}
fz_printf(out, "\n</text>\n");
}
/* Entry points */
static void
svg_dev_fill_path(fz_device *dev, fz_path *path, int even_odd, const fz_matrix *ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
svg_device *sdev = dev->user;
fz_output *out = sdev->out;
fz_printf(out, "<path");
svg_dev_ctm(sdev, ctm);
svg_dev_path(sdev, path);
svg_dev_fill_color(sdev, colorspace, color, alpha);
if (even_odd)
fz_printf(out, " fill-rule=\"evenodd\" ");
fz_printf(out, "/>\n");
}
static void
svg_dev_stroke_path(fz_device *dev, fz_path *path, fz_stroke_state *stroke, const fz_matrix *ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
svg_device *sdev = dev->user;
fz_output *out = sdev->out;
fz_printf(out, "<path");
svg_dev_ctm(sdev, ctm);
svg_dev_stroke_state(sdev, stroke);
svg_dev_stroke_color(sdev, colorspace, color, alpha);
svg_dev_path(sdev, path);
fz_printf(out, "/>\n");
}
static void
svg_dev_clip_path(fz_device *dev, fz_path *path, const fz_rect *rect, int even_odd, const fz_matrix *ctm)
{
svg_device *sdev = dev->user;
fz_output *out = sdev->out;
int num = sdev->id++;
fz_printf(out, "<clipPath id=\"cp%d\">\n", num);
fz_printf(out, "<path");
svg_dev_ctm(sdev, ctm);
svg_dev_path(sdev, path);
if (even_odd)
fz_printf(out, " fill-rule=\"evenodd\" ");
fz_printf(out, "/>\n</clipPath>\n<g clip-path=\"url(#cp%d)\">\n", num);
}
static void
svg_dev_clip_stroke_path(fz_device *dev, fz_path *path, const fz_rect *rect, fz_stroke_state *stroke, const fz_matrix *ctm)
{
svg_device *sdev = dev->user;
fz_output *out = sdev->out;
fz_context *ctx = dev->ctx;
fz_rect bounds;
int num = sdev->id++;
float white[3] = { 255, 255, 255 };
fz_bound_path(ctx, path, stroke, ctm, &bounds);
fz_printf(out, "<mask id=\"ma%d\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\" maskUnits=\"userSpaceOnUse\" maskContentUnits=\"userSpaceOnUse\" >\n",
num, bounds.x0, bounds.y0, bounds.x1 - bounds.x0, bounds.y1 - bounds.y0);
fz_printf(out, "<path");
svg_dev_ctm(sdev, ctm);
svg_dev_stroke_state(sdev, stroke);
svg_dev_stroke_color(sdev, fz_device_rgb(ctx), white, 1);
svg_dev_path(sdev, path);
fz_printf(out, "/>\n</mask>\n<g mask=\"url(#ma%d)\">\n", num);
}
static void
svg_dev_fill_text(fz_device *dev, fz_text *text, const fz_matrix *ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
svg_device *sdev = dev->user;
fz_output *out = sdev->out;
fz_printf(out, "<text");
svg_dev_fill_color(sdev, colorspace, color, alpha);
svg_dev_text(sdev, ctm, text);
}
static void
svg_dev_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, const fz_matrix *ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
svg_device *sdev = dev->user;
fz_output *out = sdev->out;
fz_printf(out, "<text");
svg_dev_ctm(sdev, ctm);
svg_dev_stroke_state(sdev, stroke);
svg_dev_stroke_color(sdev, colorspace, color, alpha);
svg_dev_text(sdev, ctm, text);
}
static void
svg_dev_clip_text(fz_device *dev, fz_text *text, const fz_matrix *ctm, int accumulate)
{
svg_device *sdev = dev->user;
fz_output *out = sdev->out;
fz_context *ctx = dev->ctx;
fz_rect bounds;
int num = sdev->id++;
float white[3] = { 255, 255, 255 };
fz_bound_text(ctx, text, NULL, ctm, &bounds);
fz_printf(out, "<mask id=\"ma%d\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\" maskUnits=\"userSpaceOnUse\" maskContentUnits=\"userSpaceOnUse\" >\n",
num, bounds.x0, bounds.y0, bounds.x1 - bounds.x0, bounds.y1 - bounds.y0);
fz_printf(out, "<text");
svg_dev_fill_color(sdev, fz_device_rgb(ctx), white, 1.0f);
svg_dev_text(sdev, ctm, text);
fz_printf(out, "</mask>\n<g mask=\"url(#ma%d)\">\n", num);
}
static void
svg_dev_clip_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, const fz_matrix *ctm)
{
svg_device *sdev = dev->user;
fz_output *out = sdev->out;
fz_context *ctx = dev->ctx;
fz_rect bounds;
int num = sdev->id++;
float white[3] = { 255, 255, 255 };
fz_bound_text(ctx, text, NULL, ctm, &bounds);
fz_printf(out, "<mask id=\"ma%d\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\" maskUnits=\"userSpaceOnUse\" maskContentUnits=\"userSpaceOnUse\" >\n",
num, bounds.x0, bounds.y0, bounds.x1 - bounds.x0, bounds.y1 - bounds.y0);
fz_printf(out, "<text");
svg_dev_stroke_state(sdev, stroke);
svg_dev_stroke_color(sdev, fz_device_rgb(ctx), white, 1.0f);
svg_dev_text(sdev, ctm, text);
fz_printf(out, "</mask>\n<g mask=\"url(#ma%d)\">\n", num);
}
static void
svg_dev_ignore_text(fz_device *dev, fz_text *text, const fz_matrix *ctm)
{
}
static void
send_data_base64(fz_output *out, fz_buffer *buffer)
{
int i, len;
static const char set[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
len = buffer->len/3;
for (i = 0; i < len; i++)
{
int c = buffer->data[3*i];
int d = buffer->data[3*i+1];
int e = buffer->data[3*i+2];
if ((i & 15) == 0)
fz_printf(out, "\n");
fz_printf(out, "%c%c%c%c", set[c>>2], set[((c&3)<<4)|(d>>4)], set[((d&15)<<2)|(e>>6)], set[e & 63]);
}
i *= 3;
switch (buffer->len-i)
{
case 2:
{
int c = buffer->data[i];
int d = buffer->data[i+1];
fz_printf(out, "%c%c%c=", set[c>>2], set[((c&3)<<4)|(d>>4)], set[((d&15)<<2)]);
break;
}
case 1:
{
int c = buffer->data[i];
fz_printf(out, "%c%c==", set[c>>2], set[(c&3)<<4]);
break;
}
default:
case 0:
break;
}
}
static void
svg_dev_fill_image(fz_device *dev, fz_image *image, const fz_matrix *ctm, float alpha)
{
svg_device *sdev = (svg_device *)dev->user;
fz_context *ctx = dev->ctx;
fz_output *out = sdev->out;
fz_matrix local_ctm = *ctm;
fz_matrix scale = { 1.0f/image->w, 0, 0, 1.0f/image->h, 0, 0};
fz_concat(&local_ctm, &scale, ctm);
fz_printf(out, "<image");
svg_dev_ctm(sdev, &local_ctm);
fz_printf(out, "width=\"%dpx\" height=\"%dpx\" xlink:href=\"data:", image->w, image->h);
switch (image->buffer == NULL ? FZ_IMAGE_JPX : image->buffer->params.type)
{
case FZ_IMAGE_JPEG:
fz_printf(out, "image/jpeg;base64,");
send_data_base64(out, image->buffer->buffer);
break;
case FZ_IMAGE_PNG:
fz_printf(out, "image/png;base64,");
send_data_base64(out, image->buffer->buffer);
break;
default:
{
fz_buffer *buf = fz_image_as_png(ctx, image, image->w, image->h);
fz_printf(out, "image/png;base64,");
send_data_base64(out, buf);
fz_drop_buffer(ctx, buf);
break;
}
}
fz_printf(out, "\"/>\n");
}
static void
svg_dev_fill_shade(fz_device *dev, fz_shade *shade, const fz_matrix *ctm, float alpha)
{
}
static void
svg_dev_fill_image_mask(fz_device *dev, fz_image *image, const fz_matrix *ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
}
static void
svg_dev_clip_image_mask(fz_device *dev, fz_image *image, const fz_rect *rect, const fz_matrix *ctm)
{
svg_device *sdev = dev->user;
fz_output *out = sdev->out;
fz_printf(out, "<g>\n");
}
static void
svg_dev_pop_clip(fz_device *dev)
{
svg_device *sdev = (svg_device *)dev->user;
fz_output *out = sdev->out;
/* FIXME */
fz_printf(out, "</g>\n");
}
static void
svg_dev_begin_mask(fz_device *dev, const fz_rect *bbox, int luminosity, fz_colorspace *colorspace, float *color)
{
}
static void
svg_dev_end_mask(fz_device *dev)
{
}
static void
svg_dev_begin_group(fz_device *dev, const fz_rect *bbox, int isolated, int knockout, int blendmode, float alpha)
{
}
static void
svg_dev_end_group(fz_device *dev)
{
}
static int
svg_dev_begin_tile(fz_device *dev, const fz_rect *area, const fz_rect *view, float xstep, float ystep, const fz_matrix *ctm, int id)
{
svg_device *sdev = (svg_device *)dev->user;
fz_output *out = sdev->out;
fz_context *ctx = dev->ctx;
fz_matrix inverse;
int num;
tile *t;
if (sdev->num_tiles == sdev->max_tiles)
{
int n = (sdev->num_tiles == 0 ? 4 : sdev->num_tiles * 2);
sdev->tiles = fz_resize_array(ctx, sdev->tiles, n, sizeof(tile));
sdev->max_tiles = n;
}
num = sdev->num_tiles++;
t = &sdev->tiles[num];
t->area = *area;
t->view = *view;
t->ctm = *ctm;
t->pattern = sdev->id++;
t->step.x = xstep;
t->step.y = ystep;
/* view = area of our reference tile in pattern space.
* area = area to tile into in pattern space.
* xstep/ystep = pattern repeat step in pattern space.
* All of these need to be transformed by ctm to get to device space.
* SVG only allows us to specify pattern tiles as axis aligned
* rectangles, so we send these through as is, and ensure that the
* correct matrix is used on the fill.
*/
/* In svg, the reference tile is taken from (x,y) to (x+width,y+height)
* and is repeated at (x+n*width,y+m*height) for all integer n and m.
* This means that width and height correspond to xstep and ystep. */
fz_printf(out, "<pattern id=\"pa%d\" patternUnits=\"userSpaceOnUse\" patternContentUnits=\"userSpaceOnUse\"",
t->pattern);
fz_printf(out, " x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\">\n",
view->x0, view->y0, xstep, ystep);
/* All the pattern contents will have their own ctm applied. Let's
* undo the current one to allow for this */
fz_invert_matrix(&inverse, ctm);
fz_printf(out, "<g");
svg_dev_ctm(sdev, &inverse);
fz_printf(out, ">\n");
return 0;
}
static void
svg_dev_end_tile(fz_device *dev)
{
svg_device *sdev = (svg_device *)dev->user;
fz_output *out = sdev->out;
int num;
tile *t;
if (sdev->num_tiles == 0)
return;
num = --sdev->num_tiles;
t = &sdev->tiles[num];
fz_printf(out, "</g>\n</pattern>\n");
fz_printf(out, "<rect");
svg_dev_ctm(sdev, &t->ctm);
fz_printf(out, " fill=\"url(#pa%d)\" x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\"/>\n",
t->pattern, t->area.x0, t->area.y0, t->area.x1 - t->area.x0, t->area.y1 - t->area.y0);
}
static void
svg_dev_free_user(fz_device *dev)
{
svg_device *sdev = dev->user;
fz_context *ctx = sdev->ctx;
fz_output *out = sdev->out;
fz_free(ctx, sdev->tiles);
fz_printf(out, "</svg>\n");
fz_free(ctx, sdev);
}
fz_device *fz_new_svg_device(fz_context *ctx, fz_output *out, float page_width, float page_height)
{
svg_device *sdev = fz_malloc_struct(ctx, svg_device);
fz_device *dev;
fz_try(ctx)
{
sdev->ctx = ctx;
sdev->out = out;
sdev->id = 0;
dev = fz_new_device(ctx, sdev);
}
fz_catch(ctx)
{
fz_free(ctx, sdev);
fz_rethrow(ctx);
}
dev->free_user = svg_dev_free_user;
dev->fill_path = svg_dev_fill_path;
dev->stroke_path = svg_dev_stroke_path;
dev->clip_path = svg_dev_clip_path;
dev->clip_stroke_path = svg_dev_clip_stroke_path;
dev->fill_text = svg_dev_fill_text;
dev->stroke_text = svg_dev_stroke_text;
dev->clip_text = svg_dev_clip_text;
dev->clip_stroke_text = svg_dev_clip_stroke_text;
dev->ignore_text = svg_dev_ignore_text;
dev->fill_shade = svg_dev_fill_shade;
dev->fill_image = svg_dev_fill_image;
dev->fill_image_mask = svg_dev_fill_image_mask;
dev->clip_image_mask = svg_dev_clip_image_mask;
dev->pop_clip = svg_dev_pop_clip;
dev->begin_mask = svg_dev_begin_mask;
dev->end_mask = svg_dev_end_mask;
dev->begin_group = svg_dev_begin_group;
dev->end_group = svg_dev_end_group;
dev->begin_tile = svg_dev_begin_tile;
dev->end_tile = svg_dev_end_tile;
fz_printf(out, "<?xml version=\"1.0\" standalone=\"no\"?>\n");
fz_printf(out, "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
fz_printf(out, "<svg xmlns=\"http://www.w3.org/2000/svg\" "
"xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.1\" "
"width=\"%gcm\" height=\"%gcm\" viewBox=\"0 0 %g %g\">\n",
page_width*2.54/72, page_height*2.54/72, page_width, page_height);
return dev;
}
|