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