comparison mupdf-source/docs/examples/bbox-device.js @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 function BBoxDevice(bbox) {
2 function extend(x,y) {
3 if (x < bbox[0]) bbox[0] = x;
4 if (x > bbox[2]) bbox[2] = x;
5 if (y < bbox[1]) bbox[1] = y;
6 if (y > bbox[3]) bbox[3] = y;
7 }
8 function extendPoint(m, px, py) {
9 var x = px * m[0] + py * m[2] + m[4];
10 var y = px * m[1] + py * m[3] + m[5];
11 extend(x, y);
12 }
13 function extendRect(m, r) {
14 var x0 = r[0], y0 = r[1];
15 var x1 = r[2], y1 = r[3];
16 extendPoint(m, x0, y0);
17 extendPoint(m, x1, y0);
18 extendPoint(m, x0, y1);
19 extendPoint(m, x1, y1);
20 }
21 function PathBounder(ctm) {
22 return {
23 moveTo: function (x,y) { extendPoint(ctm, x, y) },
24 lineTo: function (x,y) { extendPoint(ctm, x, y) },
25 curveTo: function (x1,y1,x2,y2,x3,y3) {
26 extendPoint(ctm, x1, y1);
27 extendPoint(ctm, x2, y2);
28 extendPoint(ctm, x3, y3);
29 },
30 };
31 }
32 function TextBounder(ctm) {
33 return {
34 showGlyph: function (font,trm,gid,ucs,wmode,bidi) {
35 var bbox = [ 0, -0.2, font.advanceGlyph(gid, 0), 0.8 ];
36 extendRect(Matrix.concat(trm, ctm), bbox);
37 },
38 };
39 }
40 return {
41 fillPath: function (path, evenOdd, ctm, colorSpace, color, alpha) {
42 path.walk(new PathBounder(ctm));
43 },
44 clipPath: function (path, evenOdd, ctm) {
45 path.walk(new PathBounder(ctm));
46 },
47 strokePath: function (path, stroke, ctm, colorSpace, color, alpha) {
48 path.walk(new PathBounder(ctm));
49 },
50 clipStrokePath: function (path, stroke, ctm) {
51 path.walk(new PathBounder(ctm));
52 },
53 fillText: function (text, ctm, colorSpace, color, alpha) {
54 text.walk(new TextBounder(ctm));
55 },
56 clipText: function (text, ctm) {
57 text.walk(new TextBounder(ctm));
58 },
59 strokeText: function (text, stroke, ctm, colorSpace, color, alpha) {
60 text.walk(new TextBounder(ctm));
61 },
62 clipStrokeText: function (text, stroke, ctm) {
63 text.walk(new TextBounder(ctm));
64 },
65 ignoreText: function (text, ctm) {
66 text.walk(new TextBounder(ctm));
67 },
68 fillShade: function (shade, ctm, alpha) {
69 var bbox = shade.getBounds(ctm);
70 extend(bbox[0], bbox[1]);
71 extend(bbox[2], bbox[3]);
72 },
73 fillImage: function (image, ctm, alpha) {
74 extendRect(ctm, [0,0,1,1]);
75 },
76 fillImageMask: function (image, ctm, colorSpace, color, alpha) {
77 extendRect(ctm, [0,0,1,1]);
78 },
79 };
80 }
81
82 if (scriptArgs.length != 2)
83 print("usage: mutool run bbox-device.js document.pdf pageNumber")
84 else {
85 var doc = Document.openDocument(scriptArgs[0]);
86 var page = doc.loadPage(parseInt(scriptArgs[1])-1);
87 var bbox = [Infinity, Infinity, -Infinity, -Infinity];
88 page.run(new BBoxDevice(bbox), Matrix.identity);
89 print("original bbox:", page.getBounds());
90 print("computed bbox:", bbox.map(function (x) { return Math.round(x); }));
91 }