Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/platform/java/example/StoryTest.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) 2022 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 class StoryTest | |
| 28 { | |
| 29 private static String snark = | |
| 30 "<!DOCTYPE html>" | |
| 31 +"<style>" | |
| 32 +"#a { margin: 30px; }" | |
| 33 +"#b { margin: 20px; }" | |
| 34 +"#c { margin: 5px; }" | |
| 35 +"#a { border: 1px solid red; }" | |
| 36 +"#b { border: 1px solid green; }" | |
| 37 +"#c { border: 1px solid blue; }" | |
| 38 +"</style>" | |
| 39 +"<body>" | |
| 40 +"<div id=\"a\">" | |
| 41 +"A" | |
| 42 +"</div>" | |
| 43 +"<div id=\"b\">" | |
| 44 +"<div id=\"c\">" | |
| 45 +"C" | |
| 46 +"</div>" | |
| 47 +"</div>" | |
| 48 +"<p>\"Just the place for a Snark!\" the Bellman cried,<br>" | |
| 49 +"As he landed his crew with care;<br>" | |
| 50 +"Supporting each man on the top of the tide<br>" | |
| 51 +"By a finger entwined in his hair.</p>" | |
| 52 | |
| 53 +"<P>Just the place for a Snark! I have said it twice:<br>" | |
| 54 +"That alone should encourage the crew.<br>" | |
| 55 +"Just the place for a Snark! I have said it thrice:<br>" | |
| 56 +"What I tell you three times is true.</p>" | |
| 57 | |
| 58 +"<p>The crew was complete: it included a Boots-<br>" | |
| 59 +"A maker of Bonnets and Hoods-<br>" | |
| 60 +"A Barrister, brought to arrange their disputes-<br>" | |
| 61 +"And a Broker, to value their goods.</p>" | |
| 62 | |
| 63 +"<p>A Billiard-marker, whose skill was immense,<br>" | |
| 64 +"Might perhaps have won more than his share-<br>" | |
| 65 +"But a Banker, engaged at enormous expense,<br>" | |
| 66 +"Had the whole of their cash in his care.</p>" | |
| 67 | |
| 68 +"<p>There was also a Beaver, that paced on the deck,<br>" | |
| 69 +"Or would sit making lace in the bow:<br>" | |
| 70 +"And had often (the Bellman said) saved them from wreck,<br>" | |
| 71 +"Though none of the sailors knew how.</p>" | |
| 72 | |
| 73 +"<p>There was one who was famed for the number of things<br>" | |
| 74 +"He forgot when he entered the ship:<br>" | |
| 75 +"His umbrella, his watch, all his jewels and rings,<br>" | |
| 76 +"And the clothes he had bought for the trip.</p>" | |
| 77 +"<div id=\"a\">" | |
| 78 +"<p>He had forty-two boxes, all carefully packed,<br>" | |
| 79 +"With his name painted clearly on each:<br>" | |
| 80 +"But, since he omitted to mention the fact,<br>" | |
| 81 +"They were all left behind on the beach.</p>" | |
| 82 +"</div>" | |
| 83 | |
| 84 +"<p>The loss of his clothes hardly mattered, because<br>" | |
| 85 +"He had seven coats on when he came,<br>" | |
| 86 +"With three pair of boots-but the worst of it was,<br>" | |
| 87 +"He had wholly forgotten his name.</p>" | |
| 88 | |
| 89 +"<p>He would answer to \"Hi!\" or to any loud cry,<br>" | |
| 90 +"Such as \"Fry me!\" or \"Fritter my wig!\"<br>" | |
| 91 +"To \"What-you-may-call-um!\" or \"What-was-his-name!\"<br>" | |
| 92 +"But especially \"Thing-um-a-jig!\"</p>" | |
| 93 | |
| 94 +"<p>While, for those who preferred a more forcible word,<br>" | |
| 95 +"He had different names from these:<br>" | |
| 96 +"His intimate friends called him \"Candle-ends,\"<br>" | |
| 97 +"And his enemies \"Toasted-cheese.\"</p>" | |
| 98 | |
| 99 +"<p>\"His form is ungainly-his intellect small-\"<br>" | |
| 100 +"(So the Bellman would often remark)<br>" | |
| 101 +"\"But his courage is perfect! And that, after all,<br>" | |
| 102 +"Is the thing that one needs with a Snark.\"</p>" | |
| 103 | |
| 104 +"<p>He would joke with hyenas, returning their stare<br>" | |
| 105 +"With an impudent wag of the head:<br>" | |
| 106 +"And he once went a walk, paw-in-paw, with a bear,<br>" | |
| 107 +"\"Just to keep up its spirits,\" he said.</p>" | |
| 108 | |
| 109 +"<p>He came as a Baker: but owned, when too late-<br>" | |
| 110 +"And it drove the poor Bellman half-mad-<br>" | |
| 111 +"He could only bake Bride-cake-for which, I may state,<br>" | |
| 112 +"No materials were to be had.</p>" | |
| 113 | |
| 114 +"<p>The last of the crew needs especial remark,<br>" | |
| 115 +"Though he looked an incredible dunce:<br>" | |
| 116 +"He had just one idea-but, that one being \"Snark,\"<br>" | |
| 117 +"The good Bellman engaged him at once.</p>" | |
| 118 | |
| 119 +"<p>He came as a Butcher: but gravely declared,<br>" | |
| 120 +"When the ship had been sailing a week,<br>" | |
| 121 +"He could only kill Beavers. The Bellman looked scared,<br>" | |
| 122 +"And was almost too frightened to speak:</p>" | |
| 123 | |
| 124 +"<p>But at length he explained, in a tremulous tone,<br>" | |
| 125 +"There was only one Beaver on board;<br>" | |
| 126 +"And that was a tame one he had of his own,<br>" | |
| 127 +"Whose death would be deeply deplored.</p>" | |
| 128 | |
| 129 +"<div id=\"b\">" | |
| 130 +"<p>The Beaver, who happened to hear the remark,<br>" | |
| 131 +"Protested, with tears in its eyes,<br>" | |
| 132 +"That not even the rapture of hunting the Snark<br>" | |
| 133 +"Could atone for that dismal surprise!</p>" | |
| 134 +"</div>" | |
| 135 | |
| 136 +"<p style=\"-mupdf-leading:7pt;\">It strongly advised that the Butcher should be<br>" | |
| 137 +"Conveyed in a separate ship:<br>" | |
| 138 +"But the Bellman declared that would never agree<br>" | |
| 139 +"With the plans he had made for the trip:</p>" | |
| 140 | |
| 141 +"<p style=\"-mupdf-leading:11pt;\">Navigation was always a difficult art,<br>" | |
| 142 +"Though with only one ship and one bell:<br>" | |
| 143 +"And he feared he must really decline, for his part,<br>" | |
| 144 +"Undertaking another as well.</p>" | |
| 145 | |
| 146 +"<p style=\"-mupdf-leading:15pt;\">The Beaver's best course was, no doubt, to procure<br>" | |
| 147 +"A second-hand dagger-proof coat-<br>" | |
| 148 +"So the Baker advised it-and next, to insure<br>" | |
| 149 +"Its life in some Office of note:</p>" | |
| 150 | |
| 151 +"<p style=\"-mupdf-leading:20pt;\">This the Banker suggested, and offered for hire<br>" | |
| 152 +"(On moderate terms), or for sale,<br>" | |
| 153 +"Two excellent Policies, one Against Fire,<br>" | |
| 154 +"And one Against Damage From Hail.</p>" | |
| 155 | |
| 156 +"<p style=\"-mupdf-leading:30pt;\">Yet still, ever after that sorrowful day,<br>" | |
| 157 +"Whenever the Butcher was by,<br>" | |
| 158 +"The Beaver kept looking the opposite way,<br>" | |
| 159 +"And appeared unaccountably shy.</p>" | |
| 160 ; | |
| 161 | |
| 162 | |
| 163 private static String festival_template = | |
| 164 "<html><head><title>Why do we have a title? Why not?</title></head>" | |
| 165 +"<body><h1 style=\"text-align:center\">Hook Norton Film Festival</h1>" | |
| 166 +"<ol>" | |
| 167 +"<li id=\"filmtemplate\">" | |
| 168 +"<b id=\"filmtitle\"></b>" | |
| 169 +"<dl>" | |
| 170 +"<dt>Director<dd id=\"director\">" | |
| 171 +"<dt>Release Year<dd id=\"filmyear\">" | |
| 172 +"<dt>Cast<dd id=\"cast\">" | |
| 173 +"</dl>" | |
| 174 +"</li>" | |
| 175 +"<ul>" | |
| 176 +"</body></html"; | |
| 177 | |
| 178 private static class Film | |
| 179 { | |
| 180 String title; | |
| 181 String director; | |
| 182 String year; | |
| 183 String cast[]; | |
| 184 | |
| 185 Film(String t, String d, String y, String c[]) | |
| 186 { | |
| 187 title = t; | |
| 188 director = d; | |
| 189 year = y; | |
| 190 cast = c; | |
| 191 } | |
| 192 }; | |
| 193 | |
| 194 static Film films[] = { | |
| 195 new Film("Pulp Fiction", "Quentin Tarantino", "1994", | |
| 196 new String[] { "John Travolta", "Samuel L Jackson", "Uma Thurman", "Bruce Willis", "Ving Rhames", "Harvey Keitel", "Tim Roth", "Bridget Fonda"}), | |
| 197 new Film("The Usual Suspects", "Bryan Singer", "1995", | |
| 198 new String[] { "Kevin Spacey", "Gabriel Bryne", "Chazz Palminteri", "Benicio Del Toro", "Kevin Pollak", "Pete Postlethwaite", "Steven Baldwin"}), | |
| 199 new Film("Fight Club", "David Fincher", "1999", | |
| 200 new String[] { "Brad Pitt", "Edward Norton", "Helen Bonham Carter"}), | |
| 201 }; | |
| 202 | |
| 203 public static void main(String args[]) | |
| 204 { | |
| 205 Rect mediabox = new Rect(0, 0, 512, 640); | |
| 206 float margin = 10; | |
| 207 | |
| 208 /* First, one with precooked content. */ | |
| 209 DocumentWriter writer = new DocumentWriter("out.pdf", "PDF", ""); | |
| 210 | |
| 211 Story story = new Story(snark, "", 11); | |
| 212 | |
| 213 boolean more; | |
| 214 | |
| 215 do | |
| 216 { | |
| 217 Rect filled = new Rect(); | |
| 218 Rect where = new Rect(mediabox.x0 + margin, mediabox.y0 + margin, mediabox.x1 - margin, mediabox.y1 - margin); | |
| 219 | |
| 220 Device dev = writer.beginPage(mediabox); | |
| 221 | |
| 222 more = story.place(where, filled); | |
| 223 | |
| 224 story.draw(dev, Matrix.Identity()); | |
| 225 | |
| 226 writer.endPage(); | |
| 227 } | |
| 228 while (more); | |
| 229 | |
| 230 writer.close(); | |
| 231 writer.destroy(); | |
| 232 story.destroy(); | |
| 233 | |
| 234 /* Now one with programmatic content */ | |
| 235 writer = new DocumentWriter("out2.pdf", "PDF", ""); | |
| 236 | |
| 237 story = new Story("", "", 11); | |
| 238 | |
| 239 DOM dom = story.document(); | |
| 240 | |
| 241 DOM body = dom.body(); | |
| 242 | |
| 243 body.appendChild(dom.createTextNode("This is some text.")); | |
| 244 | |
| 245 DOM tmp = dom.createElement("b"); | |
| 246 body.appendChild(tmp); | |
| 247 | |
| 248 tmp.appendChild(dom.createTextNode("This is some bold text.")); | |
| 249 | |
| 250 body.appendChild(dom.createTextNode("This is some normal text.")); | |
| 251 | |
| 252 do | |
| 253 { | |
| 254 Rect filled = new Rect(); | |
| 255 Rect where = new Rect(mediabox.x0 + margin, mediabox.y0 + margin, mediabox.x1 - margin, mediabox.y1 - margin); | |
| 256 | |
| 257 Device dev = writer.beginPage(mediabox); | |
| 258 | |
| 259 more = story.place(where, filled); | |
| 260 | |
| 261 story.draw(dev, Matrix.Identity()); | |
| 262 | |
| 263 writer.endPage(); | |
| 264 } | |
| 265 while (more); | |
| 266 | |
| 267 writer.close(); | |
| 268 writer.destroy(); | |
| 269 story.destroy(); | |
| 270 | |
| 271 /* Now a combination of the two */ | |
| 272 writer = new DocumentWriter("out3.pdf", "PDF", ""); | |
| 273 | |
| 274 story = new Story(festival_template, "", 11); | |
| 275 | |
| 276 dom = story.document(); | |
| 277 | |
| 278 body = dom.body(); | |
| 279 | |
| 280 DOM template = body.find(null, "id", "filmtemplate"); | |
| 281 | |
| 282 for (Film film : films) | |
| 283 { | |
| 284 DOM clone = template.clone(); | |
| 285 | |
| 286 tmp = clone.find(null, "id", "filmtitle"); | |
| 287 tmp.appendChild(dom.createTextNode(film.title)); | |
| 288 | |
| 289 tmp = clone.find(null, "id", "director"); | |
| 290 tmp.appendChild(dom.createTextNode(film.director)); | |
| 291 | |
| 292 tmp = clone.find(null, "id", "filmyear"); | |
| 293 tmp.appendChild(dom.createTextNode(film.year)); | |
| 294 | |
| 295 for (String member : film.cast) | |
| 296 { | |
| 297 tmp = clone.find(null, "id", "cast"); | |
| 298 tmp.appendChild(dom.createTextNode(member)); | |
| 299 tmp.appendChild(dom.createElement("br")); | |
| 300 } | |
| 301 | |
| 302 template.parent().appendChild(clone); | |
| 303 } | |
| 304 | |
| 305 template.remove(); | |
| 306 | |
| 307 do | |
| 308 { | |
| 309 Rect filled = new Rect(); | |
| 310 Rect where = new Rect(mediabox.x0 + margin, mediabox.y0 + margin, mediabox.x1 - margin, mediabox.y1 - margin); | |
| 311 | |
| 312 Device dev = writer.beginPage(mediabox); | |
| 313 | |
| 314 more = story.place(where, filled); | |
| 315 | |
| 316 story.draw(dev, Matrix.Identity()); | |
| 317 | |
| 318 writer.endPage(); | |
| 319 } | |
| 320 while (more); | |
| 321 | |
| 322 writer.close(); | |
| 323 writer.destroy(); | |
| 324 story.destroy(); | |
| 325 | |
| 326 } | |
| 327 } |
