You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PCLRenderer.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.render.pcl;
  8. // FOP
  9. import org.apache.fop.render.PrintRenderer;
  10. import org.apache.fop.image.ImageArea;
  11. import org.apache.fop.image.FopImage;
  12. import org.apache.fop.apps.FOPException;
  13. import org.apache.fop.fo.properties.*;
  14. import org.apache.fop.datatypes.*;
  15. import org.apache.fop.pdf.PDFPathPaint;
  16. import org.apache.fop.pdf.PDFColor;
  17. import org.apache.fop.layout.*;
  18. import org.apache.fop.layout.inline.*;
  19. import org.apache.fop.image.*;
  20. import org.apache.fop.svg.SVGArea;
  21. import org.w3c.dom.svg.SVGSVGElement;
  22. import org.w3c.dom.svg.SVGDocument;
  23. // Java
  24. import java.io.IOException;
  25. import java.io.OutputStream;
  26. import java.util.Enumeration;
  27. /**
  28. * Renderer that renders areas to PCL
  29. * Created by Arthur E Welch III while at M&I EastPoint Technology
  30. * Donated by EastPoint to the Apache FOP project March 2, 2001.
  31. * Modified by Mark Lillywhite mark-fop@inomial.com to use the
  32. * new Renderer interface.
  33. */
  34. public class PCLRenderer extends PrintRenderer {
  35. /**
  36. * the current stream to add PCL commands to
  37. */
  38. public PCLStream currentStream;
  39. private int pageHeight = 7920;
  40. // These variables control the virtual paggination functionality.
  41. public int curdiv = 0;
  42. private int divisions = -1;
  43. public int paperheight = -1; // Paper height in decipoints?
  44. public int orientation =
  45. -1; // -1=default/unknown, 0=portrait, 1=landscape.
  46. public int topmargin = -1; // Top margin in decipoints?
  47. public int leftmargin = -1; // Left margin in decipoints?
  48. private int fullmargin = 0;
  49. private final boolean debug = false;
  50. private int xoffset =
  51. -180; // X Offset to allow for PCL implicit 1/4" left margin.
  52. private java.util.Hashtable options;
  53. /**
  54. * Create the PCL renderer
  55. */
  56. public PCLRenderer() {}
  57. /**
  58. * set up renderer options
  59. */
  60. public void setOptions(java.util.Hashtable options) {
  61. this.options = options;
  62. }
  63. /**
  64. * set the PCL document's producer
  65. *
  66. * @param producer string indicating application producing PCL
  67. */
  68. public void setProducer(String producer) {}
  69. /**
  70. * add a line to the current stream
  71. *
  72. * @param x1 the start x location in millipoints
  73. * @param y1 the start y location in millipoints
  74. * @param x2 the end x location in millipoints
  75. * @param y2 the end y location in millipoints
  76. * @param th the thickness in millipoints
  77. * @param stroke the line color
  78. */
  79. protected void addLine(int x1, int y1, int x2, int y2, int th,
  80. PDFPathPaint stroke) {
  81. if (x1 == x2)
  82. addRect(x1 - (th / 2), y1, th, y2 - y1 + 1, stroke, stroke);
  83. else if (y1 == y2)
  84. addRect(x1, y1 + (th / 2), x2 - x1 + 1, th, stroke, stroke);
  85. }
  86. /**
  87. * add a line to the current stream
  88. *
  89. * @param x1 the start x location in millipoints
  90. * @param y1 the start y location in millipoints
  91. * @param x2 the end x location in millipoints
  92. * @param y2 the end y location in millipoints
  93. * @param th the thickness in millipoints
  94. * @param rs the rule style
  95. * @param stroke the line color
  96. */
  97. protected void addLine(int x1, int y1, int x2, int y2, int th, int rs,
  98. PDFPathPaint stroke) {
  99. int dashon = 0;
  100. int dashoff = 0;
  101. // if ( rs != null && rs.length() > 5 && rs.charAt(0) == '[' && rs.charAt(1) != ']' && rs.charAt(4) == ']' )
  102. // {
  103. // dashon = rs.charAt(1) - '0';
  104. // dashoff = rs.charAt(3) - '0';
  105. // }
  106. switch (rs) {
  107. case org.apache.fop.fo.properties.RuleStyle.DASHED:
  108. dashon = 3;
  109. dashoff = 3;
  110. break;
  111. case org.apache.fop.fo.properties.RuleStyle.DOTTED:
  112. dashon = 1;
  113. dashoff = 3;
  114. break;
  115. }
  116. if (x1 == x2) {
  117. if (dashon > 0 && dashoff > 0) {
  118. int start = y1;
  119. int len = th * dashon;
  120. while (start < y2) {
  121. if (start + len > y2)
  122. len = y2 - start;
  123. addRect(x1 - (th / 2), start, th, len, stroke, stroke);
  124. start += (len + dashoff * th);
  125. }
  126. } else
  127. addRect(x1 - (th / 2), y1, th, y2 - y1 + 1, stroke, stroke);
  128. } else if (y1 == y2) {
  129. if (dashon > 0 && dashoff > 0) {
  130. int start = x1;
  131. int len = th * dashon;
  132. while (start < x2) {
  133. if (start + len > x2)
  134. len = x2 - start;
  135. addRect(start, y1 + (th / 2), len, th, stroke, stroke);
  136. start += (len + dashoff * th);
  137. }
  138. } else
  139. addRect(x1, y1 + (th / 2), x2 - x1 + 1, th, stroke, stroke);
  140. }
  141. }
  142. /**
  143. * add a rectangle to the current stream
  144. *
  145. * @param x the x position of left edge in millipoints
  146. * @param y the y position of top edge in millipoints
  147. * @param w the width in millipoints
  148. * @param h the height in millipoints
  149. * @param stroke the stroke color/gradient
  150. */
  151. protected void addRect(int x, int y, int w, int h, PDFPathPaint stroke) {
  152. if (h < 0)
  153. h *= -1;
  154. if (h < 720 || w < 720) {
  155. if (w < 720)
  156. w = 720;
  157. if (h < 720)
  158. h = 720;
  159. addRect(x, y, w, h, stroke, stroke);
  160. } else {
  161. addRect(x, y, w, 720, stroke, stroke);
  162. addRect(x, y, 720, h, stroke, stroke);
  163. addRect(x + w - 720, y, 720, h, stroke, stroke);
  164. addRect(x, y - h + 720, w, 720, stroke, stroke);
  165. }
  166. }
  167. /**
  168. * add a filled rectangle to the current stream
  169. *
  170. * @param x the x position of left edge in millipoints
  171. * @param y the y position of top edge in millipoints
  172. * @param w the width in millipoints
  173. * @param h the height in millipoints
  174. * @param fill the fill color/gradient
  175. * @param stroke the stroke color/gradient
  176. */
  177. protected void addRect(int x, int y, int w, int h, PDFPathPaint stroke,
  178. PDFPathPaint fill) {
  179. if ((w == 0) || (h == 0))
  180. return;
  181. if (h < 0)
  182. h *= -1;
  183. PDFColor sc = (PDFColor)stroke;
  184. PDFColor fc = (PDFColor)fill;
  185. sc.setColorSpace(ColorSpace.DEVICE_RGB);
  186. fc.setColorSpace(ColorSpace.DEVICE_RGB);
  187. int lineshade =
  188. (int)(100
  189. - ((0.3f * sc.red() + 0.59f * sc.green() + 0.11f * sc.blue())
  190. * 100f));
  191. int fillshade =
  192. (int)(100
  193. - ((0.3f * fc.red() + 0.59f * fc.green() + 0.11f * fc.blue())
  194. * 100f));
  195. int xpos = xoffset + (x / 100);
  196. if (xpos < 0) {
  197. xpos = 0;
  198. log.error("PCLRenderer.addRect() WARNING: Horizontal position out of bounds.");
  199. }
  200. currentStream.add("\033*v1O\033&a" + xpos + "h"
  201. + (pageHeight - (y / 100)) + "V" + "\033*c"
  202. + (w / 100) + "h" + (h / 100) + "V" + "\033*c"
  203. + lineshade + "G" + "\033*c2P");
  204. if (fillshade != lineshade && (w >= 720 || h >= 720)) {
  205. xpos = xoffset + ((x + 240) / 100);
  206. if (xpos < 0) {
  207. xpos = 0;
  208. log.error("PCLRenderer.addRect() WARNING: Horizontal position out of bounds.");
  209. }
  210. currentStream.add("\033&a" + xpos + "h"
  211. + (pageHeight - ((y + 240)) / 100) + "V"
  212. + "\033*c" + ((w - 480) / 100) + "h"
  213. + ((h - 480) / 100) + "V" + "\033*c"
  214. + fillshade + "G" + "\033*c2P");
  215. }
  216. // Reset pattern transparency mode.
  217. currentStream.add("\033*v0O");
  218. }
  219. boolean printBMP(FopImage img, int x, int y, int w,
  220. int h) throws FopImageException {
  221. // Print the passed image file in PCL.
  222. byte imgmap[] = img.getBitmaps();
  223. int ix = 0;
  224. int iy = 0;
  225. int indx = 0;
  226. int iw = img.getWidth();
  227. int ih = img.getHeight();
  228. int bytewidth = (iw / 8);
  229. if ((iw % 8) != 0)
  230. bytewidth++;
  231. byte ib;
  232. char ic[] = new char[bytewidth * 2];
  233. char icu[] = new char[bytewidth];
  234. int lastcount = -1;
  235. byte lastbyte = 0;
  236. int icwidth = 0;
  237. int cr = 0;
  238. int cg = 0;
  239. int cb = 0;
  240. int grey = 0;
  241. boolean iscolor = img.getColorSpace().getColorSpace()
  242. != ColorSpace.DEVICE_GRAY;
  243. int dcount = 0;
  244. int xres = (iw * 72000) / w;
  245. int yres = (ih * 72000) / h;
  246. int resolution = xres;
  247. if (yres > xres)
  248. resolution = yres;
  249. if (resolution > 300)
  250. resolution = 600;
  251. else if (resolution > 150)
  252. resolution = 300;
  253. else if (resolution > 100)
  254. resolution = 150;
  255. else if (resolution > 75)
  256. resolution = 100;
  257. else
  258. resolution = 75;
  259. if (debug)
  260. System.out.println("PCLRenderer.printBMP() iscolor = " + iscolor);
  261. // Setup for graphics
  262. currentStream.add("\033*t" + resolution + "R\033*r0F\033*r1A");
  263. // Transfer graphics data
  264. for (iy = 0; iy < ih; iy++) {
  265. ib = 0;
  266. // int padding = iw % 8;
  267. // if ( padding != 0 )
  268. // padding = 8 - padding;
  269. // padding = 0;
  270. // indx = (ih - iy - 1 + padding) * iw;
  271. indx = iy * iw;
  272. if (iscolor)
  273. indx *= 3;
  274. // currentStream.add("\033*b" + bytewidth + "W");
  275. for (ix = 0; ix < iw; ix++) {
  276. if (iscolor) {
  277. cr = imgmap[indx++] & 0xFF;
  278. cg = imgmap[indx++] & 0xFF;
  279. cb = imgmap[indx++] & 0xFF;
  280. grey = (cr * 30 + cg * 59 + cb * 11) / 100;
  281. } else
  282. grey = imgmap[indx++] & 0xFF;
  283. if (grey < 128)
  284. ib |= (1 << (7 - (ix % 8)));
  285. if ((ix % 8) == 7 || ((ix + 1) == iw)) {
  286. if (icwidth < bytewidth) {
  287. if (lastcount >= 0) {
  288. if (ib == lastbyte)
  289. lastcount++;
  290. else {
  291. ic[icwidth++] = (char)(lastcount & 0xFF);
  292. ic[icwidth++] = (char)lastbyte;
  293. lastbyte = ib;
  294. lastcount = 0;
  295. }
  296. } else {
  297. lastbyte = ib;
  298. lastcount = 0;
  299. }
  300. if (lastcount == 255 || ((ix + 1) == iw)) {
  301. ic[icwidth++] = (char)(lastcount & 0xFF);
  302. ic[icwidth++] = (char)lastbyte;
  303. lastbyte = 0;
  304. lastcount = -1;
  305. }
  306. }
  307. icu[ix / 8] = (char)ib;
  308. ib = 0;
  309. }
  310. }
  311. if (icwidth < bytewidth) {
  312. currentStream.add("\033*b1m" + icwidth + "W");
  313. currentStream.add(new String(ic, 0, icwidth));
  314. } else {
  315. currentStream.add("\033*b0m" + bytewidth + "W");
  316. currentStream.add(new String(icu));
  317. }
  318. lastcount = -1;
  319. icwidth = 0;
  320. }
  321. // End graphics
  322. currentStream.add("\033*rB");
  323. return (true);
  324. }
  325. /**
  326. * render image area to PCL
  327. *
  328. * @param area the image area to render
  329. */
  330. public void renderImageArea(ImageArea area) {
  331. int x = this.currentAreaContainerXPosition + area.getXOffset();
  332. int y = this.currentYPosition;
  333. int w = area.getContentWidth();
  334. int h = area.getHeight();
  335. this.currentYPosition -= h;
  336. FopImage img = area.getImage();
  337. int xpos = xoffset + (x / 100);
  338. if (xpos < 0) {
  339. xpos = 0;
  340. log.error("PCLRenderer.renderImageArea() WARNING: Horizontal position out of bounds.");
  341. }
  342. currentStream.add("\033&a" + xpos + "h" + (pageHeight - (y / 100))
  343. + "V");
  344. try {
  345. printBMP(img, x, y, w, h);
  346. } catch (FopImageException e) {
  347. // e.printStackTrace(System.out);
  348. log.error("PCLRenderer.renderImageArea() Error printing BMP ("
  349. + e.toString() + ")");
  350. }
  351. }
  352. /**
  353. * render a foreign object area
  354. */
  355. public void renderForeignObjectArea(ForeignObjectArea area) {
  356. // if necessary need to scale and align the content
  357. this.currentXPosition = this.currentXPosition + area.getXOffset();
  358. this.currentYPosition = this.currentYPosition;
  359. switch (area.getAlign()) {
  360. case TextAlign.START:
  361. break;
  362. case TextAlign.END:
  363. break;
  364. case TextAlign.CENTER:
  365. case TextAlign.JUSTIFY:
  366. break;
  367. }
  368. switch (area.getVerticalAlign()) {
  369. case VerticalAlign.BASELINE:
  370. break;
  371. case VerticalAlign.MIDDLE:
  372. break;
  373. case VerticalAlign.SUB:
  374. break;
  375. case VerticalAlign.SUPER:
  376. break;
  377. case VerticalAlign.TEXT_TOP:
  378. break;
  379. case VerticalAlign.TEXT_BOTTOM:
  380. break;
  381. case VerticalAlign.TOP:
  382. break;
  383. case VerticalAlign.BOTTOM:
  384. break;
  385. }
  386. // in general the content will not be text
  387. // align and scale
  388. switch (area.scalingMethod()) {
  389. case Scaling.UNIFORM:
  390. break;
  391. case Scaling.NON_UNIFORM:
  392. break;
  393. }
  394. // if the overflow is auto (default), scroll or visible
  395. // then the contents should not be clipped, since this
  396. // is considered a printing medium.
  397. switch (area.getOverflow()) {
  398. case Overflow.VISIBLE:
  399. case Overflow.SCROLL:
  400. case Overflow.AUTO:
  401. break;
  402. case Overflow.HIDDEN:
  403. break;
  404. }
  405. area.getObject().render(this);
  406. this.currentXPosition += area.getEffectiveWidth();
  407. // this.currentYPosition -= area.getEffectiveHeight();
  408. }
  409. /**
  410. * render SVG area to PCL
  411. *
  412. * @param area the SVG area to render
  413. */
  414. public void renderSVGArea(SVGArea area) {
  415. if (debug)
  416. System.out.println("PCLRenderer.renderSVGArea(" + area + ")");
  417. int x = this.currentXPosition;
  418. int y = this.currentYPosition;
  419. SVGSVGElement svg =
  420. ((SVGDocument)area.getSVGDocument()).getRootElement();
  421. int w = (int)(svg.getWidth().getBaseVal().getValue() * 1000);
  422. int h = (int)(svg.getHeight().getBaseVal().getValue() * 1000);
  423. /*
  424. * Clip to the svg area.
  425. * Note: To have the svg overlay (under) a text area then use
  426. * an fo:block-container
  427. */
  428. // TODO - translate and clip to viewbox
  429. // currentStream.add(svgRenderer.getString());
  430. // currentStream.add("Q\n");
  431. }
  432. public void setFont(String name, float size) {
  433. int fontcode = 0;
  434. if (name.length() > 1 && name.charAt(0) == 'F') {
  435. try {
  436. fontcode = Integer.parseInt(name.substring(1));
  437. } catch (Exception e) {
  438. e.printStackTrace();
  439. }
  440. }
  441. switch (fontcode) {
  442. case 1: // F1 = Helvetica
  443. // currentStream.add("\033(8U\033(s1p" + (size / 1000) + "v0s0b24580T");
  444. // Arial is more common among PCL5 printers than Helvetica - so use Arial
  445. currentStream.add("\033(0N\033(s1p" + (size / 1000)
  446. + "v0s0b16602T");
  447. break;
  448. case 2: // F2 = Helvetica Oblique
  449. currentStream.add("\033(0N\033(s1p" + (size / 1000)
  450. + "v1s0b16602T");
  451. break;
  452. case 3: // F3 = Helvetica Bold
  453. currentStream.add("\033(0N\033(s1p" + (size / 1000)
  454. + "v0s3b16602T");
  455. break;
  456. case 4: // F4 = Helvetica Bold Oblique
  457. currentStream.add("\033(0N\033(s1p" + (size / 1000)
  458. + "v1s3b16602T");
  459. break;
  460. case 5: // F5 = Times Roman
  461. // currentStream.add("\033(8U\033(s1p" + (size / 1000) + "v0s0b25093T");
  462. // Times New is more common among PCL5 printers than Times - so use Times New
  463. currentStream.add("\033(0N\033(s1p" + (size / 1000)
  464. + "v0s0b16901T");
  465. break;
  466. case 6: // F6 = Times Italic
  467. currentStream.add("\033(0N\033(s1p" + (size / 1000)
  468. + "v1s0b16901T");
  469. break;
  470. case 7: // F7 = Times Bold
  471. currentStream.add("\033(0N\033(s1p" + (size / 1000)
  472. + "v0s3b16901T");
  473. break;
  474. case 8: // F8 = Times Bold Italic
  475. currentStream.add("\033(0N\033(s1p" + (size / 1000)
  476. + "v1s3b16901T");
  477. break;
  478. case 9: // F9 = Courier
  479. currentStream.add("\033(0N\033(s0p"
  480. + (120.01f / (size / 1000.00f)) + "h0s0b4099T");
  481. break;
  482. case 10: // F10 = Courier Oblique
  483. currentStream.add("\033(0N\033(s0p"
  484. + (120.01f / (size / 1000.00f)) + "h1s0b4099T");
  485. break;
  486. case 11: // F11 = Courier Bold
  487. currentStream.add("\033(0N\033(s0p"
  488. + (120.01f / (size / 1000.00f)) + "h0s3b4099T");
  489. break;
  490. case 12: // F12 = Courier Bold Oblique
  491. currentStream.add("\033(0N\033(s0p"
  492. + (120.01f / (size / 1000.00f)) + "h1s3b4099T");
  493. break;
  494. case 13: // F13 = Symbol
  495. currentStream.add("\033(19M\033(s1p" + (size / 1000)
  496. + "v0s0b16686T");
  497. // currentStream.add("\033(9U\033(s1p" + (size / 1000) + "v0s0b25093T"); // ECMA Latin 1 Symbol Set in Times Roman???
  498. break;
  499. case 14: // F14 = Zapf Dingbats
  500. currentStream.add("\033(14L\033(s1p" + (size / 1000)
  501. + "v0s0b45101T");
  502. break;
  503. default:
  504. currentStream.add("\033(0N\033(s" + (size / 1000) + "V");
  505. break;
  506. }
  507. }
  508. /**
  509. * render inline area to PCL
  510. *
  511. * @param area inline area to render
  512. */
  513. public void renderWordArea(WordArea area) {
  514. String name = area.getFontState().getFontName();
  515. int size = area.getFontState().getFontSize();
  516. float red = area.getRed();
  517. float green = area.getGreen();
  518. float blue = area.getBlue();
  519. PDFColor theAreaColor = new PDFColor((double)area.getRed(),
  520. (double)area.getGreen(),
  521. (double)area.getBlue());
  522. // currentStream.add("\033*c" + (int)(100 - ((0.3f * red + 0.59f * green + 0.11f * blue) * 100f)) + "G\033*v2T");
  523. currentStream.add("\033*v1O\033*c"
  524. + (int)(100 - ((0.3f * red + 0.59f * green + 0.11f * blue) * 100f))
  525. + "G\033*v2T");
  526. if ((!name.equals(this.currentFontName))
  527. || (size != this.currentFontSize)) {
  528. this.currentFontName = name;
  529. this.currentFontSize = size;
  530. setFont(name, size);
  531. }
  532. this.currentFill = theAreaColor;
  533. int rx = this.currentXPosition;
  534. int bl = this.currentYPosition;
  535. String s;
  536. if (area.getPageNumberID() != null) {
  537. // this text is a page number, so resolve it
  538. s = idReferences.getPageNumber(area.getPageNumberID());
  539. if (s == null)
  540. s = "";
  541. } else {
  542. s = area.getText();
  543. }
  544. addWordLines(area, rx, bl, size, theAreaColor);
  545. int xpos = xoffset + (rx / 100);
  546. if (xpos < 0) {
  547. xpos = 0;
  548. log.error("PCLRenderer.renderWordArea() WARNING: Horizontal position out of bounds.");
  549. }
  550. currentStream.add("\033&a" + xpos + "h" + (pageHeight - (bl / 100))
  551. + "V" + s);
  552. this.currentXPosition += area.getContentWidth();
  553. }
  554. /**
  555. * render page into PCL
  556. *
  557. * @param page page to render
  558. */
  559. public void renderPage(Page page) {
  560. if (debug)
  561. System.out.println("PCLRenderer.renderPage() page.Height() = "
  562. + page.getHeight());
  563. BodyAreaContainer body;
  564. AreaContainer before, after, start, end;
  565. if (paperheight > 0 && divisions == -1)
  566. divisions = paperheight / (page.getHeight() / 100);
  567. if (debug)
  568. System.out.println("PCLRenderer.renderPage() paperheight="
  569. + paperheight + " divisions=" + divisions);
  570. // Set top margin.
  571. // float fullmargin = 0;
  572. if (divisions > 0)
  573. fullmargin = paperheight * curdiv / divisions;
  574. if (topmargin > 0)
  575. fullmargin += topmargin;
  576. if (debug)
  577. System.out.println("PCLRenderer.renderPage() curdiv=" + curdiv
  578. + " fullmargin=" + fullmargin);
  579. // if ( fullmargin > 0 )
  580. // currentStream.add("\033&l" + (fullmargin / 15f) + "c1e8C");
  581. // this.currentYPosition = fullmargin * 100;
  582. if (paperheight > 0)
  583. pageHeight = (paperheight / divisions) + fullmargin;
  584. else
  585. pageHeight = page.getHeight() / 100;
  586. if (debug)
  587. System.out.println("PCLRenderer.renderPage() Set currentYPosition="
  588. + this.currentYPosition);
  589. if (leftmargin > 0 && curdiv == 0)
  590. currentStream.add("\033&k" + (leftmargin / 6f)
  591. + "H\033&a1L\033&k12H");
  592. body = page.getBody();
  593. before = page.getBefore();
  594. after = page.getAfter();
  595. start = page.getStart();
  596. end = page.getEnd();
  597. this.currentFontName = "";
  598. this.currentFontSize = 0;
  599. renderBodyAreaContainer(body);
  600. if (before != null)
  601. renderAreaContainer(before);
  602. if (after != null)
  603. renderAreaContainer(after);
  604. if (start != null)
  605. renderAreaContainer(start);
  606. if (end != null)
  607. renderAreaContainer(end);
  608. // End page.
  609. if (++curdiv == divisions || divisions == -1) {
  610. curdiv = 0;
  611. currentStream.add("\f");
  612. }
  613. // Links, etc not implemented...
  614. /*
  615. * currentPage = this.pdfDoc.makePage(this.pdfResources, currentStream,
  616. * page.getWidth()/1000, page.getHeight()/1000, page);
  617. * if (page.hasLinks()) {
  618. * currentAnnotList = this.pdfDoc.makeAnnotList();
  619. * currentPage.setAnnotList(currentAnnotList);
  620. * Enumeration e = page.getLinkSets().elements();
  621. * while (e.hasMoreElements()) {
  622. * LinkSet linkSet = (LinkSet) e.nextElement();
  623. * linkSet.align();
  624. * String dest = linkSet.getDest();
  625. * int linkType = linkSet.getLinkType();
  626. * Enumeration f = linkSet.getRects().elements();
  627. * while (f.hasMoreElements()) {
  628. * LinkedRectangle lrect = (LinkedRectangle) f.nextElement();
  629. * currentAnnotList.addLink(
  630. * this.pdfDoc.makeLink(lrect.getRectangle(), dest, linkType));
  631. * }
  632. * }
  633. * } else {
  634. * // just to be on the safe side
  635. * currentAnnotList = null;
  636. * }
  637. */
  638. }
  639. public void startRenderer(OutputStream outputStream)
  640. throws IOException {
  641. log.info("rendering areas to PCL");
  642. currentStream = new PCLStream(outputStream);
  643. // Set orientation.
  644. if (orientation > -1)
  645. currentStream.add("\033&l" + orientation + "O");
  646. else
  647. currentStream.add("\033&l0O");
  648. if (orientation == 1 || orientation == 3)
  649. xoffset = -144;
  650. else
  651. xoffset = -180;
  652. // Reset the margins.
  653. currentStream.add("\033" + "9\033&l0E");
  654. }
  655. public void stopRenderer(OutputStream outputStream)
  656. throws IOException {
  657. log.info("writing out PCL");
  658. outputStream.flush();
  659. }
  660. public void render(Page page, OutputStream outputStream)
  661. throws IOException {
  662. idReferences = page.getIDReferences();
  663. this.renderPage(page);
  664. }
  665. }