StoryTest.java 10.0 KB

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