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 26KB

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