pdf-trace.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. function showString(str) {
  2. var out, c, i, n;
  3. n = str.length;
  4. if (Array.isArray(str)) {
  5. out = '<';
  6. for (i = 0; i < n; ++i) {
  7. c = str[i].toString(16);
  8. if (c.length == 1)
  9. out += '0';
  10. out += c;
  11. }
  12. out += '>';
  13. } else {
  14. out = '(';
  15. for (i = 0; i < n; ++i) {
  16. c = str[i];
  17. if (c == '(' || c == ')' || c == '\\')
  18. out += '\\';
  19. out += c;
  20. }
  21. out += ')';
  22. }
  23. return out;
  24. }
  25. function showArray(arr) {
  26. return arr.join(" ");
  27. }
  28. var traceProcessor = {
  29. op_w: function (a) { print(a, "w"); },
  30. op_j: function (a) { print(a, "j"); },
  31. op_J: function (a) { print(a, "J"); },
  32. op_M: function (a) { print(a, "M"); },
  33. op_ri: function (a) { print(a, "ri"); },
  34. op_i: function (a) { print(a, "i"); },
  35. op_d: function (array, phase) { print("[" + showArray(array) + "]", phase, "d"); },
  36. op_gs: function (name, dict) {
  37. print("/"+name, "gs");
  38. dict.forEach(function(v,k) {
  39. print("%", k, v);
  40. });
  41. },
  42. op_q: function () { print("q"); },
  43. op_Q: function () { print("Q"); },
  44. op_cm: function (a,b,c,d,e,f) { print(a,b,c,d,e,f, "cm"); },
  45. op_m: function (x,y) { print(x, y, "m"); },
  46. op_l: function (x,y) { print(x, y, "l"); },
  47. op_c: function (x1,y1,x2,y2,x3,y3) { print(x1,y1,x2,y2,x3,y3, "c"); },
  48. op_v: function (x2,y2,x3,y3) { print(x2,y2,x3,y3, "v"); },
  49. op_y: function (x1,y1,x3,y3) { print(x1,y1,x3,y3, "y"); },
  50. op_h: function () { print("h"); },
  51. op_re: function (x,y,w,h) { print(x,y,w,h, "re"); },
  52. op_S: function () { print("S"); },
  53. op_s: function () { print("s"); },
  54. op_F: function () { print("F"); },
  55. op_f: function () { print("f"); },
  56. op_fstar: function () { print("f*"); },
  57. op_B: function () { print("B"); },
  58. op_Bstar: function () { print("B*"); },
  59. op_b: function () { print("b"); },
  60. op_bstar: function () { print("b*"); },
  61. op_n: function () { print("n"); },
  62. op_W: function () { print("W"); },
  63. op_Wstar: function () { print("W*"); },
  64. op_BT: function () { print("BT"); },
  65. op_ET: function () { print("ET"); },
  66. op_Tc: function (charspace) { print(charspace, "Tc"); },
  67. op_Tw: function (wordspace) { print(wordspace, "Tw"); },
  68. op_Tz: function (scale) { print(scale, "Tz"); },
  69. op_TL: function (leading) { print(leading, "TL"); },
  70. op_Tr: function (render) { print(render, "Tr"); },
  71. op_Ts: function (rise) { print(rise, "Ts"); },
  72. op_Tf: function (name,size) { print("/"+name, size, "Tf"); },
  73. op_Td: function (x,y) { print(x,y, "Td"); },
  74. op_TD: function (x,y) { print(x,y, "TD"); },
  75. op_Tm: function (a,b,c,d,e,f) { print(a,b,c,d,e,f, "Tm"); },
  76. op_Tstar: function () { print("T*"); },
  77. op_TJ: function (text) {
  78. text = text.map(function (x) {
  79. if (typeof x != 'number')
  80. return showString(x);
  81. return x;
  82. });
  83. print("[" + showArray(text) + "] TJ")
  84. },
  85. op_Tj: function (text) { print(showString(text), "Tj"); },
  86. op_squote: function (text) { print(showString(text), "'"); },
  87. op_dquote: function (aw,ac,text) { print(aw, ac, showString(text), "\""); },
  88. op_d0: function (wx,wy) { print(wx,wy, "d0"); },
  89. op_d1: function (wx,wy,llx,lly,urx,ury) { print(wx,wy,llx,lly,urx,ury, "d1"); },
  90. op_CS: function (name,colorspace) { print("/"+name, colorspace, "CS"); },
  91. op_cs: function (name,colorspace) { print("/"+name, colorspace, "cs"); },
  92. op_SC_pattern: function (name,pattern,color) { print("/"+name, showArray(color), "SC%pattern"); },
  93. op_sc_pattern: function (name,pattern,color) { print("/"+name, showArray(color), "sc%pattern"); },
  94. op_SC_shade: function (name,shade) { print("/"+name, "SC%shade"); },
  95. op_sc_shade: function (name,shade) { print("/"+name, "sc%shade"); },
  96. op_SC_color: function (color) { print(showArray(color), "SC%color"); },
  97. op_sc_color: function (color) { print(showArray(color), "sc%color"); },
  98. op_G: function (g) { print(g, "G"); },
  99. op_g: function (g) { print(g, "g"); },
  100. op_RG: function (r,g,b) { print(r,g,b, "RG"); },
  101. op_rg: function (r,g,b) { print(r,g,b, "rg"); },
  102. op_K: function (c,m,y,k) { print(c,m,y,k, "K"); },
  103. op_k: function (c,m,y,k) { print(c,m,y,k, "k"); },
  104. op_BI: function (image) { print("% BI ... ID ... EI"); },
  105. op_sh: function (name,shade) { print("/"+name, "sh"); },
  106. op_Do_image: function (name,image) { print("/"+name, "Do%image"); },
  107. op_Do_form: function (name,form,page_res) { print("/"+name, "Do%form"); },
  108. op_MP: function (tag) { print("/"+tag, "MP"); },
  109. op_DP: function (tag,dict) { print("/"+tag, dict, "DP"); },
  110. op_BMC: function (tag) { print("/"+tag, "BMC"); },
  111. op_BDC: function (tag,dict) { print("/"+tag, dict, "BDC"); },
  112. op_EMC: function () { print("EMC"); },
  113. op_BX: function () { print("BX"); },
  114. op_EX: function () { print("EX"); },
  115. };
  116. var doc = Document.openDocument(scriptArgs[0]);
  117. var page = doc.loadPage(scriptArgs[1]-1);
  118. page.process(traceProcessor);