Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/platform/java/example/PDFTrace.java @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 // Copyright (C) 2004-2025 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 | |
| 23 package example; | |
| 24 | |
| 25 import com.artifex.mupdf.fitz.*; | |
| 26 | |
| 27 public class PDFTrace extends PDFProcessor | |
| 28 { | |
| 29 protected String formatFloatArray(float[] array, String suffix) { | |
| 30 StringBuilder sb = new StringBuilder("[ "); | |
| 31 int n = array.length; | |
| 32 for (int i = 0; i < n; i++) | |
| 33 sb.append(array[i] + " "); | |
| 34 sb.append(']'); | |
| 35 if (suffix != null) | |
| 36 sb.append(suffix); | |
| 37 return sb.toString(); | |
| 38 } | |
| 39 | |
| 40 protected String formatString(String s, String suffix) { | |
| 41 StringBuilder sb = new StringBuilder("("); | |
| 42 int n = s.length(); | |
| 43 for (int i = 0; i < n; i++) | |
| 44 { | |
| 45 char c = s.charAt(i); | |
| 46 if (c == '(' || c == ')' || c == '\\') | |
| 47 sb.append('\\'); | |
| 48 sb.append(c); | |
| 49 } | |
| 50 sb.append(')'); | |
| 51 if (suffix != null) | |
| 52 sb.append(suffix); | |
| 53 return sb.toString(); | |
| 54 } | |
| 55 | |
| 56 protected String formatByteArray(byte[] array, String suffix) { | |
| 57 StringBuilder sb = new StringBuilder("<"); | |
| 58 int n = array.length; | |
| 59 for (int i = 0; i < n; i++) | |
| 60 sb.append(String.format("%02x", array[i])); | |
| 61 sb.append('>'); | |
| 62 if (suffix != null) | |
| 63 sb.append(suffix); | |
| 64 return sb.toString(); | |
| 65 | |
| 66 } | |
| 67 | |
| 68 protected String formatObjectArray(Object[] array, String suffix) { | |
| 69 StringBuilder sb = new StringBuilder('['); | |
| 70 int n = array.length; | |
| 71 for (int i = 0; i < n; i++) | |
| 72 { | |
| 73 if (array[i] instanceof Float) | |
| 74 sb.append(((Float) array[i]).toString() + " "); | |
| 75 else if (array[i] instanceof String) | |
| 76 sb.append(formatString((String) array[i], " ")); | |
| 77 else | |
| 78 sb.append(formatByteArray((byte[]) array[i], " ")); | |
| 79 } | |
| 80 sb.append("]"); | |
| 81 if (suffix != null) | |
| 82 sb.append(suffix); | |
| 83 return sb.toString(); | |
| 84 } | |
| 85 | |
| 86 public void pushResources(PDFObject resources) { } | |
| 87 public void popResources() { } | |
| 88 public void op_gs(String name, PDFObject extgstate) { } | |
| 89 | |
| 90 public void op_w(float lineWidth) { System.out.println(lineWidth+ " w"); } | |
| 91 public void op_j(float lineJoin) { System.out.println(lineJoin + " j"); } | |
| 92 public void op_J(float lineCap) { System.out.println(lineCap + " J"); } | |
| 93 public void op_M(float miterLimit) { System.out.println(miterLimit + " M"); } | |
| 94 public void op_d(float[] array, float phase) { System.out.print("[ "); for (int i = 0; i < array.length; i++) System.out.print(array[i] + " "); System.out.println("] " + phase + " d"); } | |
| 95 public void op_ri(String intent) { System.out.println(intent + " ri"); } | |
| 96 public void op_i(float flatness) { System.out.println(flatness + "i"); } | |
| 97 | |
| 98 public void op_q() { System.out.println("q"); } | |
| 99 public void op_Q() { System.out.println("Q"); } | |
| 100 public void op_cm(float a, float b, float c, float d, float e, float f) { System.out.println(a + " " + b + " " + c + " " + d + " " + e + " " + f + " cm"); } | |
| 101 | |
| 102 public void op_m(float x, float y) { System.out.println(x + " " + y + " m"); } | |
| 103 public void op_l(float x, float y) { System.out.println(x + " " + y + " l"); } | |
| 104 public void op_c(float x1, float y1, float x2, float y2, float x3, float y3) { System.out.println(x1 + " " + y1 + " " + x2 + " " + y2 + " " + x3 + " " + y3 + " c"); } | |
| 105 public void op_v(float x2, float y2, float x3, float y3) { System.out.println(x2 + " " + y2 + " " + x3 + " " + y3 + " v"); } | |
| 106 public void op_y(float x1, float y1, float x3, float y3) { System.out.println(x1 + " " + y1 + " " + x3 + " " + y3 + " f"); } | |
| 107 public void op_h() { System.out.println("h"); } | |
| 108 public void op_re(float x, float y, float w, float h) { System.out.println(x + " " + y + " " + w + " " + h + " re"); } | |
| 109 | |
| 110 public void op_S() { System.out.println("S"); } | |
| 111 public void op_s() { System.out.println("s"); } | |
| 112 public void op_F() { System.out.println("F"); } | |
| 113 public void op_f() { System.out.println("f"); } | |
| 114 public void op_fstar() { System.out.println("f*"); } | |
| 115 public void op_B() { System.out.println("B"); } | |
| 116 public void op_Bstar() { System.out.println("B*"); } | |
| 117 public void op_b() { System.out.println("b"); } | |
| 118 public void op_bstar() { System.out.println("b*"); } | |
| 119 public void op_n() { System.out.println("n"); } | |
| 120 | |
| 121 public void op_W() { System.out.println("W"); } | |
| 122 public void op_Wstar() { System.out.println("W*"); } | |
| 123 | |
| 124 public void op_BT() { System.out.println("BT"); } | |
| 125 public void op_ET() { System.out.println("ET"); } | |
| 126 | |
| 127 public void op_Tc(float charspace) { System.out.println(charspace + " Tc"); } | |
| 128 public void op_Tw(float wordspace) { System.out.println(wordspace + " Tw"); } | |
| 129 public void op_Tz(float scale) { System.out.println(scale + " Tz"); } | |
| 130 public void op_TL(float leading) { System.out.println(leading + " TL"); } | |
| 131 public void op_Tf(String name, float size) { System.out.println("/" + name + " " + size + " Tf"); } | |
| 132 public void op_Tr(float render) { System.out.println(render + " Tr"); } | |
| 133 public void op_Ts(float rise) { System.out.println(rise + " Ts"); } | |
| 134 | |
| 135 public void op_Td(float tx, float ty) { System.out.println(tx + " " + ty + " Td"); } | |
| 136 public void op_TD(float tx, float ty) { System.out.println(tx + " " + ty + " TD"); } | |
| 137 public void op_Tm(float a, float b, float c, float d, float e, float f) { System.out.println(a + " " + b + " " + c + " " + d + " " + e + " " + f + " Tm"); } | |
| 138 public void op_Tstar() { System.out.println("T*"); } | |
| 139 | |
| 140 public void op_TJ(Object[] array) { System.out.println(formatObjectArray(array, " TJ")); } | |
| 141 public void op_Tj(byte[] array) { System.out.println(formatByteArray(array, " Tj")); } | |
| 142 public void op_Tj(String string) { System.out.println(formatString(string, " Tj")); } | |
| 143 public void op_squote(String text) { System.out.println(formatString(text, " '")); } | |
| 144 public void op_squote(byte[] array) { System.out.println(formatByteArray(array, " '")); } | |
| 145 public void op_dquote(float aw, float ac, String text) { System.out.println(aw + " " + ac + " " + formatString(text, " \"")); } | |
| 146 public void op_dquote(float aw, float ac, byte[] array) { System.out.println(aw + " " + ac + " " + " " + formatByteArray(array, " \"")); } | |
| 147 | |
| 148 public void op_d0(float wx, float wy) { System.out.println(wx + " " + wy + " d0"); } | |
| 149 public void op_d1(float wx, float wy, float llx, float lly, float urx, float ury) { System.out.println(wx + " " + wy + " " + llx + " " + lly + " " + urx + " " + ury + " d1"); } | |
| 150 | |
| 151 public void op_CS(String name, ColorSpace cs) { System.out.println("/" + name + " " + (cs != null ? cs.toString() : "null") + " CS"); } | |
| 152 public void op_cs(String name, ColorSpace cs) { System.out.println("/" + name + " " + (cs != null ? cs.toString() : "null") + " cs"); } | |
| 153 public void op_SC_color(float[] color) { for (int i = 0; i < color.length; i++) System.out.print(color[i] + " "); System.out.println("SC%color"); } | |
| 154 public void op_sc_color(float[] color) { for (int i = 0; i < color.length; i++) System.out.print(color[i] + " "); System.out.println("sc%color"); } | |
| 155 public void op_SC_pattern(String name, int idx, float[] color) { System.out.print("/" + name + " " + idx + " "); for (int i = 0; i < color.length; i++) System.out.print(color[i] + " "); System.out.println("SC%pattern"); } | |
| 156 public void op_sc_pattern(String name, int idx, float[] color) { System.out.print("/" + name + " " + idx + " "); for (int i = 0; i < color.length; i++) System.out.print(color[i] + " "); System.out.println("sc%pattern"); } | |
| 157 public void op_SC_shade(String name) { System.out.println("/" + name + " SC%shade"); } | |
| 158 public void op_sc_shade(String name) { System.out.println("/" + name + " sc%shade"); } | |
| 159 | |
| 160 public void op_G(float g) { System.out.println(g + " G"); } | |
| 161 public void op_g(float g) { System.out.println(g + " g"); } | |
| 162 public void op_RG(float r, float g, float b) { System.out.println(r + " " + g + " " + b + " RG"); } | |
| 163 public void op_rg(float r, float g, float b) { System.out.println(r + " " + g + " " + b + " rg"); } | |
| 164 public void op_K(float c, float m, float y, float k) { System.out.println(c + " " + m + " " + y + " " + k + " K"); } | |
| 165 public void op_k(float c, float m, float y, float k) { System.out.println(c + " " + m + " " + y + " " + k + " k"); } | |
| 166 | |
| 167 public void op_BI(Image image) { System.out.println("% BI ... ID ... EI"); } | |
| 168 public void op_sh(String name, Shade shade) { System.out.println("/" + name + " sh"); } | |
| 169 public void op_Do_image(String name, Image image) { System.out.println("/" + name + " Do%image"); } | |
| 170 public void op_Do_form(String name, PDFObject form, PDFObject pageResources) { System.out.println("/" + name + " Do%form"); } | |
| 171 | |
| 172 public void op_MP(String tag) { System.out.println(tag + " MP"); } | |
| 173 public void op_DP(String tag, PDFObject raw) { System.out.println(tag + " " + raw.toString() + " DP"); } | |
| 174 public void op_BMC(String tag) { System.out.println("/" + tag + " BMC"); } | |
| 175 public void op_BDC(String tag, PDFObject raw) { System.out.println("/" + tag + " " + raw.toString(true) + " BDC"); } | |
| 176 public void op_EMC() { System.out.println("EMC"); } | |
| 177 | |
| 178 public void op_BX() { System.out.println("BX"); } | |
| 179 public void op_EX() { System.out.println("EX"); } | |
| 180 | |
| 181 public static void main(String[] args) { | |
| 182 if (args.length < 1) | |
| 183 { | |
| 184 System.err.println("Usage: PDFTrace pdffile [pages...]"); | |
| 185 return; | |
| 186 } | |
| 187 | |
| 188 String filename = args[0]; | |
| 189 Document doc = Document.openDocument(args[0]); | |
| 190 if (!doc.isPDF()) | |
| 191 System.err.println(args[0] + ": only PDF documents can be traced"); | |
| 192 PDFDocument pdf = doc.asPDF(); | |
| 193 PDFProcessor proc = new PDFTrace(); | |
| 194 | |
| 195 if (args.length == 1) | |
| 196 { | |
| 197 int pageno = 1; | |
| 198 PDFPage page = (PDFPage) pdf.loadPage(pageno - 1); | |
| 199 System.out.println("--------PAGE " + pageno + " BEGIN--------"); | |
| 200 page.process(proc); | |
| 201 System.out.println("--------PAGE " + pageno + " END----------"); | |
| 202 } | |
| 203 else | |
| 204 { | |
| 205 for (int i = 1; i < args.length; i++) | |
| 206 { | |
| 207 int pageno = Integer.parseInt(args[i]); | |
| 208 PDFPage page = (PDFPage) pdf.loadPage(pageno - 1); | |
| 209 System.out.println("--------PAGE " + pageno + " BEGIN--------"); | |
| 210 page.process(proc); | |
| 211 System.out.println("--------PAGE " + pageno + " END----------"); | |
| 212 } | |
| 213 } | |
| 214 } | |
| 215 } |
