bbox-device.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. if (scriptArgs.length != 2)
  82. print("usage: mutool run bbox-device.js document.pdf pageNumber")
  83. else {
  84. var doc = Document.openDocument(scriptArgs[0]);
  85. var page = doc.loadPage(parseInt(scriptArgs[1])-1);
  86. var bbox = [Infinity, Infinity, -Infinity, -Infinity];
  87. page.run(new BBoxDevice(bbox), Matrix.identity);
  88. print("original bbox:", page.getBounds());
  89. print("computed bbox:", bbox.map(function (x) { return Math.round(x); }));
  90. }