trace-device.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. var Q = JSON.stringify
  2. var pathPrinter = {
  3. moveTo: function (x,y) { print("moveTo", x, y) },
  4. lineTo: function (x,y) { print("lineTo", x, y) },
  5. curveTo: function (x1,y1,x2,y2,x3,y3) { print("curveTo", x1, y1, x2, y2, x3, y3) },
  6. closePath: function () { print("closePath") },
  7. }
  8. var textPrinter = {
  9. beginSpan: function (f,m,wmode, bidi, dir, lang) {
  10. print("beginSpan",f,m,wmode,bidi,dir,repr(lang));
  11. },
  12. showGlyph: function (f,m,g,u,v,b) { print("glyph",f,m,g,u,v,b) },
  13. endSpan: function () { print("endSpan"); }
  14. }
  15. var traceDevice = {
  16. fillPath: function (path, evenOdd, ctm, colorSpace, color, alpha) {
  17. print("fillPath", evenOdd, ctm, colorSpace, color, alpha)
  18. path.walk(pathPrinter)
  19. },
  20. clipPath: function (path, evenOdd, ctm) {
  21. print("clipPath", evenOdd, ctm)
  22. path.walk(pathPrinter)
  23. },
  24. strokePath: function (path, stroke, ctm, colorSpace, color, alpha) {
  25. print("strokePath", Q(stroke), ctm, colorSpace, color, alpha)
  26. path.walk(pathPrinter)
  27. },
  28. clipStrokePath: function (path, stroke, ctm) {
  29. print("clipStrokePath", Q(stroke), ctm)
  30. path.walk(pathPrinter)
  31. },
  32. fillText: function (text, ctm, colorSpace, color, alpha) {
  33. print("fillText", ctm, colorSpace, color, alpha)
  34. text.walk(textPrinter)
  35. },
  36. clipText: function (text, ctm) {
  37. print("clipText", ctm)
  38. text.walk(textPrinter)
  39. },
  40. strokeText: function (text, stroke, ctm, colorSpace, color, alpha) {
  41. print("strokeText", Q(stroke), ctm, colorSpace, color, alpha)
  42. text.walk(textPrinter)
  43. },
  44. clipStrokeText: function (text, stroke, ctm) {
  45. print("clipStrokeText", Q(stroke), ctm)
  46. text.walk(textPrinter)
  47. },
  48. ignoreText: function (text, ctm) {
  49. print("ignoreText", ctm)
  50. text.walk(textPrinter)
  51. },
  52. fillShade: function (shade, ctm, alpha) {
  53. print("fillShade", shade, ctm, alpha)
  54. },
  55. fillImage: function (image, ctm, alpha) {
  56. print("fillImage", image, ctm, alpha)
  57. },
  58. fillImageMask: function (image, ctm, colorSpace, color, alpha) {
  59. print("fillImageMask", image, ctm, colorSpace, color, alpha)
  60. },
  61. clipImageMask: function (image, ctm) {
  62. print("clipImageMask", image, ctm)
  63. },
  64. beginMask: function (area, luminosity, colorspace, color) {
  65. print("beginMask", area, luminosity, colorspace, color)
  66. },
  67. endMask: function () {
  68. print("endMask")
  69. },
  70. popClip: function () {
  71. print("popClip")
  72. },
  73. beginGroup: function (area, isolated, knockout, blendmode, alpha) {
  74. print("beginGroup", area, isolated, knockout, blendmode, alpha)
  75. },
  76. endGroup: function () {
  77. print("endGroup")
  78. },
  79. beginTile: function (area, view, xstep, ystep, ctm, id) {
  80. print("beginTile", area, view, xstep, ystep, ctm, id)
  81. return 0
  82. },
  83. endTile: function () {
  84. print("endTile")
  85. },
  86. beginLayer: function (name) {
  87. print("beginLayer", name)
  88. },
  89. endLayer: function () {
  90. print("endLayer")
  91. },
  92. beginStructure: function (structure, raw, uid) {
  93. print("beginStructure", structure, raw, uiw)
  94. },
  95. endStructure: function () {
  96. print("endStructure")
  97. },
  98. beginMetatext: function (meta, metatext) {
  99. print("beginMetatext", meta, metatext)
  100. },
  101. endMetatext: function () {
  102. print("endMetatext")
  103. },
  104. renderFlags: function (set, clear) {
  105. print("renderFlags", set, clear)
  106. },
  107. setDefaultColorSpaces: function (colorSpaces) {
  108. print("setDefaultColorSpaces", colorSpaces.getDefaultGray(),
  109. colorSpaces.getDefaultRGB(), colorSpaces.getDefaultCMYK(),
  110. colorSpaces.getOutputIntent())
  111. },
  112. close: function () {
  113. print("close")
  114. },
  115. }
  116. if (scriptArgs.length != 2)
  117. print("usage: mutool run trace-device.js document.pdf pageNumber")
  118. else {
  119. var doc = Document.openDocument(scriptArgs[0]);
  120. var page = doc.loadPage(parseInt(scriptArgs[1])-1);
  121. page.run(traceDevice, Matrix.identity);
  122. }