TraceDevice.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright (C) 2004-2023 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. package example;
  23. import com.artifex.mupdf.fitz.*;
  24. public class TraceDevice extends Device implements PathWalker, TextWalker
  25. {
  26. public String traceColor(ColorSpace cs, float[] color, float alpha) {
  27. String s = cs + " [";
  28. int i;
  29. for (i = 0; i < color.length; ++i) {
  30. if (i > 0) s += " ";
  31. s += color[i];
  32. }
  33. return s + "] " + alpha;
  34. }
  35. public String traceStroke(StrokeState stroke) {
  36. return "c=" + stroke.getStartCap() + "," + stroke.getDashCap() + "," + stroke.getEndCap() +
  37. " j=" + stroke.getLineJoin() +
  38. " m=" + stroke.getMiterLimit() +
  39. " l=" + stroke.getLineWidth();
  40. }
  41. public void moveTo(float x, float y) {
  42. System.out.println("moveto " + x + " " + y);
  43. }
  44. public void lineTo(float x, float y) {
  45. System.out.println("lineto " + x + " " + y);
  46. }
  47. public void curveTo(float cx1, float cy1, float cx2, float cy2, float ex, float ey) {
  48. System.out.println("curveto " + cx1 + " " + cy1 + " " + cx2 + " " + cy2 + " " + ex + " " + ey);
  49. }
  50. public void closePath() {
  51. System.out.println("closepath");
  52. }
  53. public void showGlyph(Font font, Matrix trm, int glyph, int unicode, boolean wmode) {
  54. System.out.println("glyph '" + (char)unicode + "' " + glyph + "\t" + font + " " + trm);
  55. }
  56. public void tracePath(Path path) {
  57. path.walk(this);
  58. }
  59. public void traceText(Text text) {
  60. text.walk(this);
  61. }
  62. public void close() {
  63. }
  64. public void fillPath(Path path, boolean evenOdd, Matrix ctm, ColorSpace cs, float[] color, float alpha, int cp) {
  65. System.out.println("fillPath " + evenOdd + " " + ctm + " " + traceColor(cs, color, alpha));
  66. tracePath(path);
  67. }
  68. public void strokePath(Path path, StrokeState stroke, Matrix ctm, ColorSpace cs, float[] color, float alpha, int cp) {
  69. System.out.println("strokePath " + traceStroke(stroke) + " " + ctm + " " + traceColor(cs, color, alpha));
  70. tracePath(path);
  71. }
  72. public void clipPath(Path path, boolean evenOdd, Matrix ctm) {
  73. System.out.println("clipPath " + evenOdd + " " + ctm);
  74. tracePath(path);
  75. }
  76. public void clipStrokePath(Path path, StrokeState stroke, Matrix ctm) {
  77. System.out.println("clipStrokePath " + traceStroke(stroke) + " " + ctm);
  78. tracePath(path);
  79. }
  80. public void fillText(Text text, Matrix ctm, ColorSpace cs, float[] color, float alpha, int cp) {
  81. System.out.println("fillText " + ctm + " " + traceColor(cs, color, alpha));
  82. traceText(text);
  83. }
  84. public void strokeText(Text text, StrokeState stroke, Matrix ctm, ColorSpace cs, float[] color, float alpha, int cp) {
  85. System.out.println("strokeText " + ctm + " " + traceStroke(stroke) + " " + traceColor(cs, color, alpha));
  86. traceText(text);
  87. }
  88. public void clipText(Text text, Matrix ctm) {
  89. System.out.println("clipText " + ctm);
  90. traceText(text);
  91. }
  92. public void clipStrokeText(Text text, StrokeState stroke, Matrix ctm) {
  93. System.out.println("clipStrokeText " + ctm + " " + traceStroke(stroke));
  94. traceText(text);
  95. }
  96. public void ignoreText(Text text, Matrix ctm) {
  97. System.out.println("ignoreText " + ctm);
  98. traceText(text);
  99. }
  100. public void fillShade(Shade shd, Matrix ctm, float alpha, int cp) {
  101. System.out.println("fillShade " + ctm + " " + alpha);
  102. }
  103. public void fillImage(Image img, Matrix ctm, float alpha, int cp) {
  104. System.out.println("fillImage " + ctm + " " + alpha);
  105. }
  106. public void fillImageMask(Image img, Matrix ctm, ColorSpace cs, float[] color, float alpha, int cp) {
  107. System.out.println("fillImageMask " + ctm + " " + traceColor(cs, color, alpha));
  108. }
  109. public void clipImageMask(Image img, Matrix ctm) {
  110. System.out.println("clipImageMask " + ctm);
  111. }
  112. public void popClip() {
  113. System.out.println("popClip");
  114. }
  115. public void beginMask(Rect rect, boolean luminosity, ColorSpace cs, float[] bc, int cp) {
  116. System.out.println("beginMask r=" + rect +
  117. " l=" + luminosity +
  118. " " + traceColor(cs, bc, 1));
  119. }
  120. public void endMask() {
  121. System.out.println("endMask");
  122. }
  123. public void beginGroup(Rect rect, ColorSpace cs, boolean isolated, boolean knockout, int blendmode, float alpha) {
  124. System.out.println("beginGroup r=" + rect +
  125. " i=" + isolated +
  126. " k=" + knockout +
  127. " bm=" + blendmode +
  128. " a=" + alpha);
  129. }
  130. public void endGroup() {
  131. System.out.println("endGroup");
  132. }
  133. public int beginTile(Rect area, Rect view, float xstep, float ystep, Matrix ctm, int id) {
  134. System.out.println("beginTile");
  135. return 0;
  136. }
  137. public void endTile() {
  138. System.out.println("endTile");
  139. }
  140. public void renderFlags(int set, int clear) {
  141. System.out.println("renderFlags set=" + set + " clear=" + clear);
  142. }
  143. public void setDefaultColorSpaces(DefaultColorSpaces dcs) {
  144. System.out.println("setDefaultColorSpaces" +
  145. " gray=" + dcs.getDefaultGray() +
  146. " rgb=" + dcs.getDefaultRGB() +
  147. " cmyk=" + dcs.getDefaultCMYK() +
  148. " outputIntent=" + dcs.getOutputIntent());
  149. }
  150. public void beginLayer(String name) {
  151. System.out.println("beginLayer");
  152. }
  153. public void endLayer() {
  154. System.out.println("endLayer");
  155. }
  156. public void beginStructure(int standard, String raw, int uid) {
  157. System.out.println("beginStructure standard=" + standard +
  158. " raw=" + raw +
  159. " uid=" + uid);
  160. }
  161. public void endStructure() {
  162. System.out.println("endStructure");
  163. }
  164. public void beginMetatext(int meta, String text) {
  165. System.out.println("beginMetatext type=" + meta +
  166. " text=" + text);
  167. }
  168. public void endMetatext() {
  169. System.out.println("endMetatext");
  170. }
  171. public static void main(String[] args) {
  172. Document doc = Document.openDocument("pdfref17.pdf");
  173. Page page = doc.loadPage(1144);
  174. TraceDevice dev = new TraceDevice();
  175. page.run(dev, new Matrix());
  176. }
  177. }