1 package example;
2 
3 import com.artifex.mupdf.fitz.*;
4 
5 public class TraceDevice extends Device implements PathWalker, TextWalker
6 {
traceColor(ColorSpace cs, float[] color, float alpha)7 	public String traceColor(ColorSpace cs, float[] color, float alpha) {
8 		String s = cs + " [";
9 		int i;
10 		for (i = 0; i < color.length; ++i) {
11 			if (i > 0) s += " ";
12 			s += color[i];
13 		}
14 		return s + "] " + alpha;
15 	}
traceStroke(StrokeState stroke)16 	public String traceStroke(StrokeState stroke) {
17 		return "c=" + stroke.getStartCap() + "," + stroke.getDashCap() + "," + stroke.getEndCap() +
18 			" j=" + stroke.getLineJoin() +
19 			" m=" + stroke.getMiterLimit() +
20 			" l=" + stroke.getLineWidth();
21 	}
22 
moveTo(float x, float y)23 	public void moveTo(float x, float y) {
24 		System.out.println("moveto " + x + " " + y);
25 	}
26 
lineTo(float x, float y)27 	public void lineTo(float x, float y) {
28 		System.out.println("lineto " + x + " " + y);
29 	}
30 
curveTo(float cx1, float cy1, float cx2, float cy2, float ex, float ey)31 	public void curveTo(float cx1, float cy1, float cx2, float cy2, float ex, float ey) {
32 		System.out.println("curveto " + cx1 + " " + cy1 + " " + cx2 + " " + cy2 + " " + ex + " " + ey);
33 	}
34 
closePath()35 	public void closePath() {
36 		System.out.println("closepath");
37 	}
38 
showGlyph(Font font, Matrix trm, int glyph, int unicode, boolean wmode)39 	public void showGlyph(Font font, Matrix trm, int glyph, int unicode, boolean wmode) {
40 		System.out.println("glyph '" + (char)unicode + "' " + glyph + "\t" + font + " " + trm);
41 	}
42 
tracePath(Path path)43 	public void tracePath(Path path) {
44 		path.walk(this);
45 	}
46 
traceText(Text text)47 	public void traceText(Text text) {
48 		text.walk(this);
49 	}
50 
close()51 	public void close() {
52 	}
53 
fillPath(Path path, boolean evenOdd, Matrix ctm, ColorSpace cs, float[] color, float alpha, int cp)54 	public void fillPath(Path path, boolean evenOdd, Matrix ctm, ColorSpace cs, float[] color, float alpha, int cp) {
55 		System.out.println("fillPath " + evenOdd + " " + ctm + " " + traceColor(cs, color, alpha));
56 		tracePath(path);
57 	}
58 
strokePath(Path path, StrokeState stroke, Matrix ctm, ColorSpace cs, float[] color, float alpha, int cp)59 	public void strokePath(Path path, StrokeState stroke, Matrix ctm, ColorSpace cs, float[] color, float alpha, int cp) {
60 		System.out.println("strokePath " + traceStroke(stroke) + " " + ctm + " " + traceColor(cs, color, alpha));
61 		tracePath(path);
62 	}
63 
clipPath(Path path, boolean evenOdd, Matrix ctm)64 	public void clipPath(Path path, boolean evenOdd, Matrix ctm) {
65 		System.out.println("clipPath " + evenOdd + " " + ctm);
66 		tracePath(path);
67 	}
68 
clipStrokePath(Path path, StrokeState stroke, Matrix ctm)69 	public void clipStrokePath(Path path, StrokeState stroke, Matrix ctm) {
70 		System.out.println("clipStrokePath " + traceStroke(stroke) + " " + ctm);
71 		tracePath(path);
72 	}
73 
fillText(Text text, Matrix ctm, ColorSpace cs, float[] color, float alpha, int cp)74 	public void fillText(Text text, Matrix ctm, ColorSpace cs, float[] color, float alpha, int cp) {
75 		System.out.println("fillText " + ctm + " " + traceColor(cs, color, alpha));
76 		traceText(text);
77 	}
78 
strokeText(Text text, StrokeState stroke, Matrix ctm, ColorSpace cs, float[] color, float alpha, int cp)79 	public void strokeText(Text text, StrokeState stroke, Matrix ctm, ColorSpace cs, float[] color, float alpha, int cp) {
80 		System.out.println("strokeText " + ctm + " " + traceStroke(stroke) + " " + traceColor(cs, color, alpha));
81 		traceText(text);
82 	}
83 
clipText(Text text, Matrix ctm)84 	public void clipText(Text text, Matrix ctm) {
85 		System.out.println("clipText " + ctm);
86 		traceText(text);
87 	}
88 
clipStrokeText(Text text, StrokeState stroke, Matrix ctm)89 	public void clipStrokeText(Text text, StrokeState stroke, Matrix ctm) {
90 		System.out.println("clipStrokeText " + ctm + " " + traceStroke(stroke));
91 		traceText(text);
92 	}
93 
ignoreText(Text text, Matrix ctm)94 	public void ignoreText(Text text, Matrix ctm) {
95 		System.out.println("ignoreText " + ctm);
96 		traceText(text);
97 	}
98 
fillShade(Shade shd, Matrix ctm, float alpha, int cp)99 	public void fillShade(Shade shd, Matrix ctm, float alpha, int cp) {
100 		System.out.println("fillShade " + ctm + " " + alpha);
101 	}
102 
fillImage(Image img, Matrix ctm, float alpha, int cp)103 	public void fillImage(Image img, Matrix ctm, float alpha, int cp) {
104 		System.out.println("fillImage " + ctm + " " + alpha);
105 	}
106 
fillImageMask(Image img, Matrix ctm, ColorSpace cs, float[] color, float alpha, int cp)107 	public void fillImageMask(Image img, Matrix ctm, ColorSpace cs, float[] color, float alpha, int cp) {
108 		System.out.println("fillImageMask " + ctm + " " + traceColor(cs, color, alpha));
109 	}
110 
clipImageMask(Image img, Matrix ctm)111 	public void clipImageMask(Image img, Matrix ctm) {
112 		System.out.println("clipImageMask " + ctm);
113 	}
114 
popClip()115 	public void popClip() {
116 		System.out.println("popClip");
117 	}
118 
beginMask(Rect rect, boolean luminosity, ColorSpace cs, float[] bc, int cp)119 	public void beginMask(Rect rect, boolean luminosity, ColorSpace cs, float[] bc, int cp) {
120 		System.out.println("beginMask r=" + rect +
121 				" l=" + luminosity +
122 				" " + traceColor(cs, bc, 1));
123 	}
124 
endMask()125 	public void endMask() {
126 		System.out.println("endMask");
127 	}
128 
beginGroup(Rect rect, ColorSpace cs, boolean isolated, boolean knockout, int blendmode, float alpha)129 	public void beginGroup(Rect rect, ColorSpace cs, boolean isolated, boolean knockout, int blendmode, float alpha) {
130 		System.out.println("beginGroup r=" + rect +
131 				" i=" + isolated +
132 				" k=" + knockout +
133 				" bm=" + blendmode +
134 				" a=" + alpha);
135 	}
136 
endGroup()137 	public void endGroup() {
138 		System.out.println("endGroup");
139 	}
140 
beginTile(Rect area, Rect view, float xstep, float ystep, Matrix ctm, int id)141 	public int beginTile(Rect area, Rect view, float xstep, float ystep, Matrix ctm, int id) {
142 		System.out.println("beginTile");
143 		return 0;
144 	}
145 
endTile()146 	public void endTile() {
147 		System.out.println("endTile");
148 	}
149 
beginLayer(String name)150 	public void beginLayer(String name) {
151 		System.out.println("beginLayer");
152 	}
153 
endLayer()154 	public void endLayer() {
155 		System.out.println("endLayer");
156 	}
157 
main(String[] args)158 	public static void main(String[] args) {
159 		Document doc = Document.openDocument("pdfref17.pdf");
160 		Page page = doc.loadPage(1144);
161 		TraceDevice dev = new TraceDevice();
162 		page.run(dev, new Matrix());
163 	}
164 }
165