/
helper-devices.i
767 lines (685 loc) · 25.8 KB
/
helper-devices.i
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
%{
/*
# ------------------------------------------------------------------------
# Copyright 2020-2022, Harald Lieder, mailto:harald.lieder@outlook.com
# License: GNU AFFERO GPL 3.0, https://www.gnu.org/licenses/agpl-3.0.html
#
# Part of "PyMuPDF", a Python binding for "MuPDF" (http://mupdf.com), a
# lightweight PDF, XPS, and E-book viewer, renderer and toolkit which is
# maintained and developed by Artifex Software, Inc. https://artifex.com.
# ------------------------------------------------------------------------
*/
typedef struct
{
fz_device super;
PyObject *out;
size_t seqno;
} jm_tracedraw_device;
static PyObject *dev_pathdict = NULL;
static float dev_linewidth = 0; // border width if present
static fz_matrix trace_device_ptm; // page transformation matrix
static fz_matrix trace_device_ctm; // trace device matrix
static fz_matrix trace_device_rot;
static fz_point dev_lastpoint = {0, 0};
static fz_rect dev_pathrect;
static float dev_pathfactor = 0;
static int dev_linecount = 0;
static int path_type = 0;
#define FILL_PATH 1
#define STROKE_PATH 2
static void
jm_increase_seqno(fz_context *ctx, fz_device *dev_, ...)
{
jm_tracedraw_device *dev = (jm_tracedraw_device *) dev_;
dev->seqno += 1;
}
/*
--------------------------------------------------------------------------
Check whether the last 4 lines represent a rectangle or quad.
Because of how we count, the lines are a polyline already.
So we check for a polygon (last line's end point equals start point).
If not true, we reduce dev_linecount by 1 and return.
If lines 1 / 3 resp 2 / 4 are parallel to the axes, we have a rect.
--------------------------------------------------------------------------
*/
static int
jm_checkquad()
{
PyObject *items = PyDict_GetItem(dev_pathdict, dictkey_items);
Py_ssize_t i, len = PyList_Size(items);
float f[8];
fz_point temp, lp;
PyObject *rect;
PyObject *line;
for (i = 0; i < 4; i++) { // store line start points
line = PyList_GET_ITEM(items, len - 4 + i);
temp = JM_point_from_py(PyTuple_GET_ITEM(line, 1));
f[i * 2] = temp.x;
f[i * 2 + 1] = temp.y;
lp = JM_point_from_py(PyTuple_GET_ITEM(line, 2));
}
if (lp.x != f[0] || lp.y != f[1]) {
// not a polygon!
//dev_linecount -= 1;
return 0;
}
dev_linecount = 0; // reset this
rect = PyTuple_New(2);
PyTuple_SET_ITEM(rect, 0, PyUnicode_FromString("qu"));
/* ----------------------------------------------------
* relationship of float array to quad points:
* (0, 1) = ul, (2, 3) = ll, (6, 7) = ur, (4, 5) = lr
---------------------------------------------------- */
fz_quad q = fz_make_quad(f[0], f[1], f[6], f[7], f[2], f[3], f[4], f[5]);
PyTuple_SET_ITEM(rect, 1, JM_py_from_quad(q));
PyList_SetItem(items, len - 4, rect); // replace item -4 by rect
PyList_SetSlice(items, len - 3, len, NULL); // delete remaining 3 items
return 1;
}
/*
--------------------------------------------------------------------------
Check whether the last 3 path items represent a rectangle
The following conditions must be true. Note that the 3 lines already are
guaranteed to be a polyline, because of the way we are counting.
Line 1 and 3 must be horizontal, line 2 must be vertical.
If all is true, modify the path accordngly.
If the lines are not parallel to axes, generate a quad.
Returns 1 if we have modified the path, otherwise 0.
--------------------------------------------------------------------------
*/
static int
jm_checkrect()
{
dev_linecount = 0; // reset line count
long orientation = 0;
fz_point ll, lr, ur, ul;
PyObject *rect;
PyObject *line0, *line2;
PyObject *items = PyDict_GetItem(dev_pathdict, dictkey_items);
Py_ssize_t len = PyList_Size(items);
line0 = PyList_GET_ITEM(items, len - 3);
ll = JM_point_from_py(PyTuple_GET_ITEM(line0, 1));
lr = JM_point_from_py(PyTuple_GET_ITEM(line0, 2));
line2 = PyList_GET_ITEM(items, len - 1);
ur = JM_point_from_py(PyTuple_GET_ITEM(line2, 1));
ul = JM_point_from_py(PyTuple_GET_ITEM(line2, 2));
/*
---------------------------------------------------------------------
Three connected lines: at least a quad! Check whether even a rect.
For this, the lines must be parallel to the axes.
Assumption:
For decomposing rects, MuPDF always starts with a horizontal line,
followed by a vertical line, followed by a horizontal line.
We will also check orientation of the enclosed area and add this info
as '+1' for anti-clockwise, '-1' for clockwise orientation.
---------------------------------------------------------------------
*/
if (ll.y != lr.y) { // not horizontal
goto drop_out;
}
if (lr.x != ur.x) { // not vertical
goto drop_out;
}
if (ur.y != ul.y) { // not horizontal
goto drop_out;
}
// we have a rect, determine orientation
if (ll.x < lr.x) { // move left to right
if (lr.y > ur.y) { // move upwards
orientation = 1;
} else {
orientation = -1;
}
} else { // move right to left
if (lr.y < ur.y) { // move downwards
orientation = 1;
} else {
orientation = -1;
}
}
// Replace last 3 "l" items by one "re" item.
fz_rect r = fz_make_rect(ul.x, ul.y, ul.x, ul.y);
r = fz_include_point_in_rect(r, ur);
r = fz_include_point_in_rect(r, ll);
r = fz_include_point_in_rect(r, lr);
rect = PyTuple_New(3);
PyTuple_SET_ITEM(rect, 0, PyUnicode_FromString("re"));
PyTuple_SET_ITEM(rect, 1, JM_py_from_rect(r));
PyTuple_SET_ITEM(rect, 2, PyLong_FromLong(orientation));
PyList_SetItem(items, len - 3, rect); // replace item -3 by rect
PyList_SetSlice(items, len - 2, len, NULL); // delete remaining 2 items
return 1;
drop_out:;
return 0;
}
static PyObject *
jm_tracedraw_color(fz_context *ctx, fz_colorspace *colorspace, const float *color)
{
float rgb[3];
if (colorspace) {
fz_convert_color(ctx, colorspace, color, fz_device_rgb(ctx),
rgb, NULL, fz_default_color_params);
return Py_BuildValue("fff", rgb[0], rgb[1], rgb[2]);
}
return PyTuple_New(0);
}
static void
trace_moveto(fz_context *ctx, void *dev_, float x, float y)
{
dev_lastpoint = fz_transform_point(fz_make_point(x, y), trace_device_ctm);
if (fz_is_infinite_rect(dev_pathrect)) {
dev_pathrect = fz_make_rect(dev_lastpoint.x, dev_lastpoint.y,
dev_lastpoint.x, dev_lastpoint.y);
}
dev_linecount = 0; // reset # of consec. lines
}
static void
trace_lineto(fz_context *ctx, void *dev_, float x, float y)
{
fz_point p1 = fz_transform_point(fz_make_point(x, y), trace_device_ctm);
dev_pathrect = fz_include_point_in_rect(dev_pathrect, p1);
PyObject *list = PyTuple_New(3);
PyTuple_SET_ITEM(list, 0, PyUnicode_FromString("l"));
PyTuple_SET_ITEM(list, 1, JM_py_from_point(dev_lastpoint));
PyTuple_SET_ITEM(list, 2, JM_py_from_point(p1));
dev_lastpoint = p1;
PyObject *items = PyDict_GetItem(dev_pathdict, dictkey_items);
LIST_APPEND_DROP(items, list);
dev_linecount += 1; // counts consecutive lines
if (dev_linecount == 4 && path_type != FILL_PATH) { // shrink to "re" or "qu" item
jm_checkquad();
}
}
static void
trace_curveto(fz_context *ctx, void *dev_, float x1, float y1, float x2, float y2, float x3, float y3)
{
dev_linecount = 0; // reset # of consec. lines
fz_point p1 = fz_make_point(x1, y1);
fz_point p2 = fz_make_point(x2, y2);
fz_point p3 = fz_make_point(x3, y3);
p1 = fz_transform_point(p1, trace_device_ctm);
p2 = fz_transform_point(p2, trace_device_ctm);
p3 = fz_transform_point(p3, trace_device_ctm);
dev_pathrect = fz_include_point_in_rect(dev_pathrect, p1);
dev_pathrect = fz_include_point_in_rect(dev_pathrect, p2);
dev_pathrect = fz_include_point_in_rect(dev_pathrect, p3);
PyObject *list = PyTuple_New(5);
PyTuple_SET_ITEM(list, 0, PyUnicode_FromString("c"));
PyTuple_SET_ITEM(list, 1, JM_py_from_point(dev_lastpoint));
PyTuple_SET_ITEM(list, 2, JM_py_from_point(p1));
PyTuple_SET_ITEM(list, 3, JM_py_from_point(p2));
PyTuple_SET_ITEM(list, 4, JM_py_from_point(p3));
dev_lastpoint = p3;
PyObject *items = PyDict_GetItem(dev_pathdict, dictkey_items);
LIST_APPEND_DROP(items, list);
}
static void
trace_close(fz_context *ctx, void *dev_)
{
if (dev_linecount == 3) {
if (jm_checkrect()) {
return;
}
}
DICT_SETITEMSTR_DROP(dev_pathdict, "closePath", JM_BOOL(1));
dev_linecount = 0; // reset # of consec. lines
}
static const fz_path_walker trace_path_walker =
{
trace_moveto,
trace_lineto,
trace_curveto,
trace_close
};
static void
jm_tracedraw_path(fz_context *ctx, jm_tracedraw_device *dev, const fz_path *path)
{
dev_pathrect = fz_infinite_rect;
dev_linecount = 0;
dev_lastpoint = fz_make_point(0, 0);
if (dev_pathdict) {
Py_CLEAR(dev_pathdict);
}
dev_pathdict = PyDict_New();
DICT_SETITEM_DROP(dev_pathdict, dictkey_items, PyList_New(0));
fz_walk_path(ctx, path, &trace_path_walker, dev);
// Check if any items were added ...
if (!PyList_Size(PyDict_GetItem(dev_pathdict, dictkey_items))) {
Py_CLEAR(dev_pathdict);
}
}
//---------------------------------------------------------------------------
// Append current path to list or merge into last path of list.
// (1) Append if first path, different item list or not 'stroke' version of
// previous
// (2) If new path has the same items, merge its content into previous path
// and indicate this via path["type"] = "fs".
//---------------------------------------------------------------------------
static void
jm_append_merge(PyObject *out)
{
Py_ssize_t len = PyList_Size(out);
if (len == 0) { // 1st path
goto append;
}
const char *thistype = PyUnicode_AsUTF8(PyDict_GetItem(dev_pathdict, dictkey_type));
if (strcmp(thistype, "f") != 0 && strcmp(thistype, "s") != 0) {
goto append;
}
PyObject *prev = PyList_GET_ITEM(out, len - 1); // get prev path
const char *prevtype = PyUnicode_AsUTF8(PyDict_GetItem(prev, dictkey_type));
if ((strcmp(prevtype, "f") != 0 && strcmp(prevtype, "s") != 0)
|| strcmp(prevtype, thistype) == 0) {
goto append;
}
PyObject *previtems = PyDict_GetItem(prev, dictkey_items);
PyObject *thisitems = PyDict_GetItem(dev_pathdict, dictkey_items);
if (PyObject_RichCompareBool(previtems, thisitems, Py_NE)) {
goto append;
}
int rc = PyDict_Merge(dev_pathdict, prev, 0); // merge, do not override
if (rc == 0) {
DICT_SETITEM_DROP(dev_pathdict, dictkey_type, PyUnicode_FromString("fs"));
Py_XINCREF( dev_pathdict); // PyList_SetItem() does not increment refcount.
PyList_SetItem(out, len - 1, dev_pathdict);
return;
} else {
PySys_WriteStderr("could not merge stroke and fill path");
goto append;
}
append:;
PyList_Append(out, dev_pathdict);
Py_CLEAR(dev_pathdict);
}
static void
jm_tracedraw_fill_path(fz_context *ctx, fz_device *dev_, const fz_path *path,
int even_odd, fz_matrix ctm, fz_colorspace *colorspace,
const float *color, float alpha, fz_color_params color_params)
{
jm_tracedraw_device *dev = (jm_tracedraw_device *) dev_;
PyObject *out = dev->out;
trace_device_ctm = ctm; //fz_concat(ctm, trace_device_ptm);
path_type = FILL_PATH;
jm_tracedraw_path(ctx, dev, path);
if (!dev_pathdict) {
return;
}
DICT_SETITEM_DROP(dev_pathdict, dictkey_type, PyUnicode_FromString("f"));
DICT_SETITEMSTR_DROP(dev_pathdict, "even_odd", JM_BOOL(even_odd));
DICT_SETITEMSTR_DROP(dev_pathdict, "fill_opacity", Py_BuildValue("f", alpha));
DICT_SETITEMSTR_DROP(dev_pathdict, "closePath", JM_BOOL(0));
DICT_SETITEMSTR_DROP(dev_pathdict, "fill", jm_tracedraw_color(ctx, colorspace, color));
DICT_SETITEM_DROP(dev_pathdict, dictkey_rect, JM_py_from_rect(dev_pathrect));
DICT_SETITEMSTR_DROP(dev_pathdict, "seqno", PyLong_FromSize_t(dev->seqno));
jm_append_merge(out);
dev->seqno += 1;
}
static void
jm_tracedraw_stroke_path(fz_context *ctx, fz_device *dev_, const fz_path *path,
const fz_stroke_state *stroke, fz_matrix ctm,
fz_colorspace *colorspace, const float *color, float alpha,
fz_color_params color_params)
{
jm_tracedraw_device *dev = (jm_tracedraw_device *)dev_;
PyObject *out = dev->out;
int i;
dev_pathfactor = 1;
if (fz_abs(ctm.a) == fz_abs(ctm.d)) {
dev_pathfactor = fz_abs(ctm.a);
}
trace_device_ctm = ctm; // fz_concat(ctm, trace_device_ptm);
path_type = STROKE_PATH;
DICT_SETITEMSTR_DROP(dev_pathdict, "closePath", JM_BOOL(0));
jm_tracedraw_path(ctx, dev, path);
if (!dev_pathdict) {
return;
}
DICT_SETITEM_DROP(dev_pathdict, dictkey_type, PyUnicode_FromString("s"));
DICT_SETITEMSTR_DROP(dev_pathdict, "stroke_opacity", Py_BuildValue("f", alpha));
DICT_SETITEMSTR_DROP(dev_pathdict, "color", jm_tracedraw_color(ctx, colorspace, color));
DICT_SETITEM_DROP(dev_pathdict, dictkey_width, Py_BuildValue("f", dev_pathfactor * stroke->linewidth));
DICT_SETITEMSTR_DROP(dev_pathdict, "lineCap", Py_BuildValue("iii", stroke->start_cap, stroke->dash_cap, stroke->end_cap));
DICT_SETITEMSTR_DROP(dev_pathdict, "lineJoin", Py_BuildValue("f", dev_pathfactor * stroke->linejoin));
if (stroke->dash_len) {
fz_buffer *buff = fz_new_buffer(ctx, 50);
fz_append_string(ctx, buff, "[ ");
for (i = 0; i < stroke->dash_len; i++) {
fz_append_printf(ctx, buff, "%g ", dev_pathfactor * stroke->dash_list[i]);
}
fz_append_printf(ctx, buff, "] %g", dev_pathfactor * stroke->dash_phase);
DICT_SETITEMSTR_DROP(dev_pathdict, "dashes", JM_EscapeStrFromBuffer(ctx, buff));
fz_drop_buffer(ctx, buff);
} else {
DICT_SETITEMSTR_DROP(dev_pathdict, "dashes", PyUnicode_FromString("[] 0"));
}
DICT_SETITEM_DROP(dev_pathdict, dictkey_rect, JM_py_from_rect(dev_pathrect));
DICT_SETITEMSTR_DROP(dev_pathdict, "seqno", PyLong_FromSize_t(dev->seqno));
jm_append_merge(out);
dev->seqno += 1;
}
static void
jm_dev_linewidth(fz_context *ctx, fz_device *dev_, const fz_path *path, const fz_stroke_state *stroke, fz_matrix ctm, fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params)
{
dev_linewidth = stroke->linewidth;
jm_increase_seqno(ctx, dev_);
}
static void
jm_trace_text_span(fz_context *ctx, PyObject *out, fz_text_span *span, int type, fz_matrix ctm, fz_colorspace *colorspace, const float *color, float alpha, size_t seqno)
{
fz_font *out_font = NULL;
int i;
const char *fontname = JM_font_name(ctx, span->font);
float rgb[3];
PyObject *chars = PyTuple_New(span->len);
fz_matrix join = fz_concat(span->trm, ctm);
fz_point dir = fz_transform_vector(fz_make_point(1, 0), join);
double fsize = sqrt(fabs((double) join.a * (double) join.d));
double linewidth, adv, asc, dsc;
double space_adv = 0;
float x0, y0, x1, y1;
asc = (double) JM_font_ascender(ctx, span->font);
dsc = (double) JM_font_descender(ctx, span->font);
if (asc < 1e-3) { // probably Tesseract font
dsc = -0.1;
asc = 0.9;
}
double ascsize = asc * fsize / (asc - dsc);
double dscsize = dsc * fsize / (asc - dsc);
int fflags = 0;
int mono = fz_font_is_monospaced(ctx, span->font);
fflags += mono * TEXT_FONT_MONOSPACED;
fflags += fz_font_is_italic(ctx, span->font) * TEXT_FONT_ITALIC;
fflags += fz_font_is_serif(ctx, span->font) * TEXT_FONT_SERIFED;
fflags += fz_font_is_bold(ctx, span->font) * TEXT_FONT_BOLD;
fz_matrix mat = trace_device_ptm;
fz_matrix ctm_rot = fz_concat(ctm, trace_device_rot);
mat = fz_concat(mat, ctm_rot);
if (dev_linewidth > 0) {
linewidth = (double) dev_linewidth;
} else {
linewidth = fsize * 0.05;
}
fz_point char_orig;
double last_adv = 0;
// walk through characters of span
fz_rect span_bbox;
dir = fz_normalize_vector(dir);
fz_matrix rot = fz_make_matrix(dir.x, dir.y, -dir.y, dir.x, 0, 0);
if (dir.x == -1) { // left-right flip
rot.d = 1;
}
for (i = 0; i < span->len; i++) {
adv = 0;
if (span->items[i].gid >= 0) {
adv = (double) fz_advance_glyph(ctx, span->font, span->items[i].gid, span->wmode);
}
adv *= fsize;
last_adv = adv;
if (span->items[i].ucs == 32) {
space_adv = adv;
}
char_orig = fz_make_point(span->items[i].x, span->items[i].y);
char_orig.y = trace_device_ptm.f - char_orig.y;
char_orig = fz_transform_point(char_orig, mat);
fz_matrix m1 = fz_make_matrix(1, 0, 0, 1, -char_orig.x, -char_orig.y);
m1 = fz_concat(m1, rot);
m1 = fz_concat(m1, fz_make_matrix(1, 0, 0, 1, char_orig.x, char_orig.y));
x0 = char_orig.x;
x1 = x0 + adv;
if (dir.x == 1 && span->trm.d < 0) { // up-down flip
y0 = char_orig.y + dscsize;
y1 = char_orig.y + ascsize;
} else {
y0 = char_orig.y - ascsize;
y1 = char_orig.y - dscsize;
}
fz_rect char_bbox = fz_make_rect(x0, y0, x1, y1);
char_bbox = fz_transform_rect(char_bbox, m1);
PyTuple_SET_ITEM(chars, (Py_ssize_t) i, Py_BuildValue("ii(ff)(ffff)",
span->items[i].ucs, span->items[i].gid,
char_orig.x, char_orig.y, char_bbox.x0, char_bbox.y0, char_bbox.x1, char_bbox.y1));
if (i > 0) {
span_bbox = fz_union_rect(span_bbox, char_bbox);
} else {
span_bbox = char_bbox;
}
}
if (!space_adv) {
if (!mono) {
space_adv = fz_advance_glyph(ctx, span->font,
fz_encode_character_with_fallback(ctx, span->font, 32, 0, 0, &out_font),
span->wmode);
space_adv *= fsize;
if (!space_adv) {
space_adv = last_adv;
}
} else {
space_adv = last_adv; // for mono fonts this suffices
}
}
// make the span dictionary
PyObject *span_dict = PyDict_New();
DICT_SETITEMSTR_DROP(span_dict, "dir", JM_py_from_point(dir));
DICT_SETITEM_DROP(span_dict, dictkey_font, JM_EscapeStrFromStr(fontname));
DICT_SETITEM_DROP(span_dict, dictkey_wmode, PyLong_FromLong((long) span->wmode));
DICT_SETITEM_DROP(span_dict, dictkey_flags, PyLong_FromLong((long) fflags));
DICT_SETITEMSTR_DROP(span_dict, "bidi_lvl", PyLong_FromLong((long) span->bidi_level));
DICT_SETITEMSTR_DROP(span_dict, "bidi_dir", PyLong_FromLong((long) span->markup_dir));
DICT_SETITEM_DROP(span_dict, dictkey_ascender, PyFloat_FromDouble(asc));
DICT_SETITEM_DROP(span_dict, dictkey_descender, PyFloat_FromDouble(dsc));
if (colorspace) {
fz_convert_color(ctx, colorspace, color, fz_device_rgb(ctx),
rgb, NULL, fz_default_color_params);
DICT_SETITEM_DROP(span_dict, dictkey_colorspace, PyLong_FromLong(3));
DICT_SETITEM_DROP(span_dict, dictkey_color, Py_BuildValue("fff", rgb[0], rgb[1], rgb[2]));
} else {
DICT_SETITEM_DROP(span_dict, dictkey_colorspace, PyLong_FromLong(1));
DICT_SETITEM_DROP(span_dict, dictkey_color, PyFloat_FromDouble(1));
}
DICT_SETITEM_DROP(span_dict, dictkey_size, PyFloat_FromDouble(fsize));
DICT_SETITEMSTR_DROP(span_dict, "opacity", PyFloat_FromDouble((double) alpha));
DICT_SETITEMSTR_DROP(span_dict, "linewidth", PyFloat_FromDouble((double) linewidth));
DICT_SETITEMSTR_DROP(span_dict, "spacewidth", PyFloat_FromDouble(space_adv));
DICT_SETITEM_DROP(span_dict, dictkey_type, PyLong_FromLong((long) type));
DICT_SETITEM_DROP(span_dict, dictkey_chars, chars);
DICT_SETITEM_DROP(span_dict, dictkey_bbox, JM_py_from_rect(span_bbox));
DICT_SETITEMSTR_DROP(span_dict, "seqno", PyLong_FromSize_t(seqno));
LIST_APPEND_DROP(out, span_dict);
}
static void
jm_trace_text(fz_context *ctx, PyObject *out, const fz_text *text, int type, fz_matrix ctm, fz_colorspace *colorspace, const float *color, float alpha, size_t seqno)
{
fz_text_span *span;
for (span = text->head; span; span = span->next)
jm_trace_text_span(ctx, out, span, type, ctm, colorspace, color, alpha, seqno);
}
/*---------------------------------------------------------
There are 3 text trace types:
0 - fill text (PDF Tr 0)
1 - stroke text (PDF Tr 1)
3 - ignore text (PDF Tr 3)
---------------------------------------------------------*/
static void
jm_tracedraw_fill_text(fz_context *ctx, fz_device *dev_, const fz_text *text, fz_matrix ctm, fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params)
{
jm_tracedraw_device *dev = (jm_tracedraw_device *)dev_;
PyObject *out = dev->out;
jm_trace_text(ctx, out, text, 0, ctm, colorspace, color, alpha, dev->seqno);
dev->seqno += 1;
}
static void
jm_tracedraw_stroke_text(fz_context *ctx, fz_device *dev_, const fz_text *text, const fz_stroke_state *stroke, fz_matrix ctm, fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params)
{
jm_tracedraw_device *dev = (jm_tracedraw_device *)dev_;
PyObject *out = dev->out;
jm_trace_text(ctx, out, text, 1, ctm, colorspace, color, alpha, dev->seqno);
dev->seqno += 1;
}
static void
jm_tracedraw_ignore_text(fz_context *ctx, fz_device *dev_, const fz_text *text, fz_matrix ctm)
{
jm_tracedraw_device *dev = (jm_tracedraw_device *)dev_;
PyObject *out = dev->out;
jm_trace_text(ctx, out, text, 3, ctm, NULL, NULL, 1, dev->seqno);
dev->seqno += 1;
}
static void jm_tracedraw_drop_device(fz_context *ctx, fz_device *dev_)
{
jm_tracedraw_device *dev = (jm_tracedraw_device *)dev_;
Py_CLEAR(dev->out);
dev->out = NULL;
}
fz_device *JM_new_tracedraw_device(fz_context *ctx, PyObject *out)
{
jm_tracedraw_device *dev = fz_new_derived_device(ctx, jm_tracedraw_device);
dev->super.close_device = NULL;
dev->super.drop_device = jm_tracedraw_drop_device;
dev->super.fill_path = jm_tracedraw_fill_path;
dev->super.stroke_path = jm_tracedraw_stroke_path;
dev->super.clip_path = NULL;
dev->super.clip_stroke_path = NULL;
dev->super.fill_text = jm_increase_seqno;
dev->super.stroke_text = jm_increase_seqno;
dev->super.clip_text = NULL;
dev->super.clip_stroke_text = NULL;
dev->super.ignore_text = jm_increase_seqno;
dev->super.fill_shade = jm_increase_seqno;
dev->super.fill_image = jm_increase_seqno;
dev->super.fill_image_mask = jm_increase_seqno;
dev->super.clip_image_mask = NULL;
dev->super.pop_clip = NULL;
dev->super.begin_mask = NULL;
dev->super.end_mask = NULL;
dev->super.begin_group = NULL;
dev->super.end_group = NULL;
dev->super.begin_tile = NULL;
dev->super.end_tile = NULL;
dev->super.begin_layer = NULL;
dev->super.end_layer = NULL;
dev->super.render_flags = NULL;
dev->super.set_default_colorspaces = NULL;
Py_XINCREF(out);
dev->out = out;
dev->seqno = 0;
return (fz_device *)dev;
}
fz_device *JM_new_tracetext_device(fz_context *ctx, PyObject *out)
{
jm_tracedraw_device *dev = fz_new_derived_device(ctx, jm_tracedraw_device);
dev->super.close_device = NULL;
dev->super.drop_device = jm_tracedraw_drop_device;
dev->super.fill_path = jm_increase_seqno;
dev->super.stroke_path = jm_dev_linewidth;
dev->super.clip_path = NULL;
dev->super.clip_stroke_path = NULL;
dev->super.fill_text = jm_tracedraw_fill_text;
dev->super.stroke_text = jm_tracedraw_stroke_text;
dev->super.clip_text = NULL;
dev->super.clip_stroke_text = NULL;
dev->super.ignore_text = jm_tracedraw_ignore_text;
dev->super.fill_shade = jm_increase_seqno;
dev->super.fill_image = jm_increase_seqno;
dev->super.fill_image_mask = jm_increase_seqno;
dev->super.clip_image_mask = NULL;
dev->super.pop_clip = NULL;
dev->super.begin_mask = NULL;
dev->super.end_mask = NULL;
dev->super.begin_group = NULL;
dev->super.end_group = NULL;
dev->super.begin_tile = NULL;
dev->super.end_tile = NULL;
dev->super.begin_layer = NULL;
dev->super.end_layer = NULL;
dev->super.render_flags = NULL;
dev->super.set_default_colorspaces = NULL;
Py_XINCREF(out);
dev->out = out;
dev->seqno = 0;
return (fz_device *)dev;
}
typedef struct jm_bbox_device_s
{
fz_device super;
PyObject *result;
} jm_bbox_device;
static void
jm_bbox_add_rect(fz_context *ctx, fz_device *dev, fz_rect rect, char *code)
{
jm_bbox_device *bdev = (jm_bbox_device *)dev;
LIST_APPEND_DROP(bdev->result, Py_BuildValue("sN", code, JM_py_from_rect(rect)));
}
static void
jm_bbox_fill_path(fz_context *ctx, fz_device *dev, const fz_path *path, int even_odd, fz_matrix ctm,
fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params)
{
jm_bbox_add_rect(ctx, dev, fz_bound_path(ctx, path, NULL, ctm), "fill-path");
}
static void
jm_bbox_stroke_path(fz_context *ctx, fz_device *dev, const fz_path *path, const fz_stroke_state *stroke,
fz_matrix ctm, fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params)
{
jm_bbox_add_rect(ctx, dev, fz_bound_path(ctx, path, stroke, ctm), "stroke-path");
}
static void
jm_bbox_fill_text(fz_context *ctx, fz_device *dev, const fz_text *text, fz_matrix ctm, ...)
{
jm_bbox_add_rect(ctx, dev, fz_bound_text(ctx, text, NULL, ctm), "fill-text");
}
static void
jm_bbox_ignore_text(fz_context *ctx, fz_device *dev, const fz_text *text, fz_matrix ctm)
{
jm_bbox_add_rect(ctx, dev, fz_bound_text(ctx, text, NULL, ctm), "ignore-text");
}
static void
jm_bbox_stroke_text(fz_context *ctx, fz_device *dev, const fz_text *text, const fz_stroke_state *stroke, fz_matrix ctm, ...)
{
jm_bbox_add_rect(ctx, dev, fz_bound_text(ctx, text, stroke, ctm), "stroke-text");
}
static void
jm_bbox_fill_shade(fz_context *ctx, fz_device *dev, fz_shade *shade, fz_matrix ctm, float alpha, fz_color_params color_params)
{
jm_bbox_add_rect(ctx, dev, fz_bound_shade(ctx, shade, ctm), "fill-shade");
}
static void
jm_bbox_fill_image(fz_context *ctx, fz_device *dev, fz_image *image, fz_matrix ctm, float alpha, fz_color_params color_params)
{
jm_bbox_add_rect(ctx, dev, fz_transform_rect(fz_unit_rect, ctm), "fill-image");
}
static void
jm_bbox_fill_image_mask(fz_context *ctx, fz_device *dev, fz_image *image, fz_matrix ctm,
fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params)
{
jm_bbox_add_rect(ctx, dev, fz_transform_rect(fz_unit_rect, ctm), "fill-imgmask");
}
fz_device *
JM_new_bbox_device(fz_context *ctx, PyObject *result)
{
jm_bbox_device *dev = fz_new_derived_device(ctx, jm_bbox_device);
dev->super.fill_path = jm_bbox_fill_path;
dev->super.stroke_path = jm_bbox_stroke_path;
dev->super.clip_path = NULL;
dev->super.clip_stroke_path = NULL;
dev->super.fill_text = jm_bbox_fill_text;
dev->super.stroke_text = jm_bbox_stroke_text;
dev->super.clip_text = NULL;
dev->super.clip_stroke_text = NULL;
dev->super.ignore_text = jm_bbox_ignore_text;
dev->super.fill_shade = jm_bbox_fill_shade;
dev->super.fill_image = jm_bbox_fill_image;
dev->super.fill_image_mask = jm_bbox_fill_image_mask;
dev->super.clip_image_mask = NULL;
dev->super.pop_clip = NULL;
dev->super.begin_mask = NULL;
dev->super.end_mask = NULL;
dev->super.begin_group = NULL;
dev->super.end_group = NULL;
dev->super.begin_tile = NULL;
dev->super.end_tile = NULL;
dev->super.begin_layer = NULL;
dev->super.end_layer = NULL;
dev->super.render_flags = NULL;
dev->super.set_default_colorspaces = NULL;
dev->result = result;
return (fz_device *)dev;
}
%}