Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  1. /*
  2. * Copyright 1999-2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id: PDFRenderer.java,v 1.38 2004/04/07 14:24:17 cbowditch Exp $ */
  17. package org.apache.fop.render.pdf;
  18. // Java
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.awt.Color;
  22. import java.awt.geom.Rectangle2D;
  23. import java.awt.geom.AffineTransform;
  24. import java.util.Iterator;
  25. import java.util.Map;
  26. import java.util.List;
  27. // XML
  28. import org.w3c.dom.Document;
  29. // Avalon
  30. import org.apache.avalon.framework.configuration.Configuration;
  31. import org.apache.avalon.framework.configuration.ConfigurationException;
  32. // FOP
  33. import org.apache.fop.apps.FOPException;
  34. import org.apache.fop.apps.FOUserAgent;
  35. import org.apache.fop.area.Area;
  36. import org.apache.fop.area.Block;
  37. import org.apache.fop.area.BlockViewport;
  38. import org.apache.fop.area.CTM;
  39. import org.apache.fop.area.LineArea;
  40. import org.apache.fop.area.Page;
  41. import org.apache.fop.area.PageViewport;
  42. import org.apache.fop.area.RegionViewport;
  43. import org.apache.fop.area.Trait;
  44. import org.apache.fop.area.OffDocumentItem;
  45. import org.apache.fop.area.BookmarkData;
  46. import org.apache.fop.area.inline.Character;
  47. import org.apache.fop.area.inline.InlineArea;
  48. import org.apache.fop.area.inline.TextArea;
  49. import org.apache.fop.area.inline.Viewport;
  50. import org.apache.fop.area.inline.ForeignObject;
  51. import org.apache.fop.area.inline.Image;
  52. import org.apache.fop.area.inline.Leader;
  53. import org.apache.fop.area.inline.InlineParent;
  54. import org.apache.fop.datatypes.ColorType;
  55. import org.apache.fop.fonts.Typeface;
  56. import org.apache.fop.fonts.Font;
  57. import org.apache.fop.fonts.FontSetup;
  58. import org.apache.fop.fonts.FontMetrics;
  59. import org.apache.fop.image.FopImage;
  60. import org.apache.fop.image.ImageFactory;
  61. import org.apache.fop.image.XMLImage;
  62. import org.apache.fop.pdf.PDFAnnotList;
  63. import org.apache.fop.pdf.PDFColor;
  64. import org.apache.fop.pdf.PDFDocument;
  65. import org.apache.fop.pdf.PDFEncryptionManager;
  66. import org.apache.fop.pdf.PDFFilterList;
  67. import org.apache.fop.pdf.PDFInfo;
  68. import org.apache.fop.pdf.PDFLink;
  69. import org.apache.fop.pdf.PDFOutline;
  70. import org.apache.fop.pdf.PDFPage;
  71. import org.apache.fop.pdf.PDFResourceContext;
  72. import org.apache.fop.pdf.PDFResources;
  73. import org.apache.fop.pdf.PDFState;
  74. import org.apache.fop.pdf.PDFStream;
  75. import org.apache.fop.pdf.PDFText;
  76. import org.apache.fop.pdf.PDFXObject;
  77. import org.apache.fop.render.PrintRenderer;
  78. import org.apache.fop.render.RendererContext;
  79. import org.apache.fop.traits.BorderProps;
  80. import org.apache.fop.fo.Constants;
  81. /*
  82. todo:
  83. word rendering and optimistion
  84. pdf state optimisation
  85. line and border
  86. background pattern
  87. writing mode
  88. text decoration
  89. */
  90. /**
  91. * Renderer that renders areas to PDF
  92. *
  93. */
  94. public class PDFRenderer extends PrintRenderer {
  95. /**
  96. * The mime type for pdf
  97. */
  98. public static final String MIME_TYPE = "application/pdf";
  99. /** Controls whether comments are written to the PDF stream. */
  100. protected static final boolean WRITE_COMMENTS = true;
  101. /**
  102. * the PDF Document being created
  103. */
  104. protected PDFDocument pdfDoc;
  105. /**
  106. * Map of pages using the PageViewport as the key
  107. * this is used for prepared pages that cannot be immediately
  108. * rendered
  109. */
  110. protected Map pages = null;
  111. /**
  112. * Page references are stored using the PageViewport as the key
  113. * when a reference is made the PageViewport is used
  114. * for pdf this means we need the pdf page reference
  115. */
  116. protected Map pageReferences = new java.util.HashMap();
  117. /** Page viewport references */
  118. protected Map pvReferences = new java.util.HashMap();
  119. /**
  120. * The output stream to write the document to
  121. */
  122. protected OutputStream ostream;
  123. /**
  124. * the /Resources object of the PDF document being created
  125. */
  126. protected PDFResources pdfResources;
  127. /**
  128. * the current stream to add PDF commands to
  129. */
  130. protected PDFStream currentStream;
  131. /**
  132. * the current annotation list to add annotations to
  133. */
  134. protected PDFResourceContext currentContext = null;
  135. /**
  136. * the current page to add annotations to
  137. */
  138. protected PDFPage currentPage;
  139. protected AffineTransform currentBasicTransform;
  140. /** drawing state */
  141. protected PDFState currentState = null;
  142. /** Name of currently selected font */
  143. protected String currentFontName = "";
  144. /** Size of currently selected font */
  145. protected int currentFontSize = 0;
  146. /** page height */
  147. protected int pageHeight;
  148. /** Registry of PDF filters */
  149. protected Map filterMap;
  150. /**
  151. * true if a TJ command is left to be written
  152. */
  153. protected boolean textOpen = false;
  154. /**
  155. * true if a BT command has been written.
  156. */
  157. protected boolean inTextMode = false;
  158. /**
  159. * the previous Y coordinate of the last word written.
  160. * Used to decide if we can draw the next word on the same line.
  161. */
  162. protected int prevWordY = 0;
  163. /**
  164. * the previous X coordinate of the last word written.
  165. * used to calculate how much space between two words
  166. */
  167. protected int prevWordX = 0;
  168. /**
  169. * The width of the previous word. Used to calculate space between
  170. */
  171. protected int prevWordWidth = 0;
  172. /**
  173. * reusable word area string buffer to reduce memory usage
  174. */
  175. //private StringBuffer wordAreaPDF = new StringBuffer();
  176. /**
  177. * create the PDF renderer
  178. */
  179. public PDFRenderer() {
  180. }
  181. /**
  182. * Configure the PDF renderer.
  183. * Get the configuration to be used for pdf stream filters,
  184. * fonts etc.
  185. * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
  186. */
  187. public void configure(Configuration cfg) throws ConfigurationException {
  188. //PDF filters
  189. this.filterMap = PDFFilterList.buildFilterMapFromConfiguration(cfg);
  190. //Font configuration
  191. List cfgFonts = FontSetup.buildFontListFromConfiguration(cfg);
  192. if (this.fontList == null) {
  193. this.fontList = cfgFonts;
  194. } else {
  195. this.fontList.addAll(cfgFonts);
  196. }
  197. }
  198. /**
  199. * @see org.apache.fop.render.Renderer#setUserAgent(FOUserAgent)
  200. */
  201. public void setUserAgent(FOUserAgent agent) {
  202. super.setUserAgent(agent);
  203. PDFXMLHandler xmlHandler = new PDFXMLHandler();
  204. userAgent.getXMLHandlerRegistry().addXMLHandler(xmlHandler);
  205. }
  206. /**
  207. * @see org.apache.fop.render.Renderer#startRenderer(OutputStream)
  208. */
  209. public void startRenderer(OutputStream stream) throws IOException {
  210. if (userAgent == null) {
  211. throw new IllegalStateException("UserAgent must be set before starting the renderer");
  212. }
  213. ostream = stream;
  214. this.pdfDoc = new PDFDocument(
  215. userAgent.getProducer() != null ? userAgent.getProducer() : "");
  216. this.pdfDoc.setCreator(userAgent.getCreator());
  217. this.pdfDoc.setCreationDate(userAgent.getCreationDate());
  218. this.pdfDoc.getInfo().setAuthor(userAgent.getAuthor());
  219. this.pdfDoc.getInfo().setTitle(userAgent.getTitle());
  220. this.pdfDoc.getInfo().setKeywords(userAgent.getKeywords());
  221. this.pdfDoc.setFilterMap(filterMap);
  222. this.pdfDoc.outputHeader(stream);
  223. //Setup encryption if necessary
  224. PDFEncryptionManager.setupPDFEncryption(
  225. userAgent.getPDFEncryptionParams(), this.pdfDoc);
  226. }
  227. /**
  228. * @see org.apache.fop.render.Renderer#stopRenderer()
  229. */
  230. public void stopRenderer() throws IOException {
  231. pdfDoc.getResources().addFonts(pdfDoc, fontInfo);
  232. pdfDoc.outputTrailer(ostream);
  233. this.pdfDoc = null;
  234. ostream = null;
  235. pages = null;
  236. pageReferences.clear();
  237. pvReferences.clear();
  238. pdfResources = null;
  239. currentStream = null;
  240. currentContext = null;
  241. currentPage = null;
  242. currentState = null;
  243. currentFontName = "";
  244. }
  245. /**
  246. * @see org.apache.fop.render.Renderer#supportsOutOfOrder()
  247. */
  248. public boolean supportsOutOfOrder() {
  249. return false;
  250. }
  251. /**
  252. * @see org.apache.fop.render.Renderer#processOffDocumentItem(OffDocumentItem)
  253. */
  254. public void processOffDocumentItem(OffDocumentItem odi) {
  255. // render Bookmark-Tree
  256. if (odi instanceof BookmarkData) {
  257. renderBookmarkTree((BookmarkData) odi);
  258. }
  259. }
  260. /**
  261. * Renders a Bookmark-Tree object
  262. * @param bookmarks the BookmarkData object containing all the Bookmark-Items
  263. */
  264. protected void renderBookmarkTree(BookmarkData bookmarks) {
  265. for (int i = 0; i < bookmarks.getCount(); i++) {
  266. BookmarkData ext = bookmarks.getSubData(i);
  267. renderBookmarkItem(ext, null);
  268. }
  269. }
  270. private void renderBookmarkItem(BookmarkData bookmarkItem,
  271. PDFOutline parentBookmarkItem) {
  272. PDFOutline pdfOutline = null;
  273. PageViewport pv = bookmarkItem.getPageViewport();
  274. if (pv != null) {
  275. Rectangle2D bounds = pv.getViewArea();
  276. double h = bounds.getHeight();
  277. float yoffset = (float)h / 1000f;
  278. String intDest = (String)pageReferences.get(pv.getKey());
  279. if (parentBookmarkItem == null) {
  280. PDFOutline outlineRoot = pdfDoc.getOutlineRoot();
  281. pdfOutline = pdfDoc.getFactory().makeOutline(outlineRoot,
  282. bookmarkItem.getBookmarkTitle(),
  283. intDest, yoffset,
  284. bookmarkItem.showChildItems());
  285. } else {
  286. pdfOutline = pdfDoc.getFactory().makeOutline(parentBookmarkItem,
  287. bookmarkItem.getBookmarkTitle(),
  288. intDest, yoffset,
  289. bookmarkItem.showChildItems());
  290. }
  291. }
  292. for (int i = 0; i < bookmarkItem.getCount(); i++) {
  293. renderBookmarkItem(bookmarkItem.getSubData(i), pdfOutline);
  294. }
  295. }
  296. /**
  297. * writes out a comment.
  298. * @param text text for the comment
  299. */
  300. protected void comment(String text) {
  301. if (WRITE_COMMENTS) {
  302. currentStream.add("% " + text + "\n");
  303. }
  304. }
  305. /** Saves the graphics state of the rendering engine. */
  306. protected void saveGraphicsState() {
  307. endTextObject();
  308. currentStream.add("q\n");
  309. }
  310. /** Restores the last graphics state of the rendering engine. */
  311. protected void restoreGraphicsState() {
  312. endTextObject();
  313. currentStream.add("Q\n");
  314. }
  315. /** Indicates the beginning of a text object. */
  316. protected void beginTextObject() {
  317. if (!inTextMode) {
  318. currentStream.add("BT\n");
  319. inTextMode = true;
  320. }
  321. }
  322. /** Indicates the end of a text object. */
  323. protected void endTextObject() {
  324. closeText();
  325. if (inTextMode) {
  326. currentStream.add("ET\n");
  327. inTextMode = false;
  328. }
  329. }
  330. /**
  331. * Start the next page sequence.
  332. * For the pdf renderer there is no concept of page sequences
  333. * but it uses the first available page sequence title to set
  334. * as the title of the pdf document.
  335. *
  336. * @param seqTitle the title of the page sequence
  337. */
  338. public void startPageSequence(LineArea seqTitle) {
  339. if (seqTitle != null) {
  340. String str = convertTitleToString(seqTitle);
  341. PDFInfo info = this.pdfDoc.getInfo();
  342. if (info.getTitle() == null) {
  343. info.setTitle(str);
  344. }
  345. }
  346. }
  347. /**
  348. * The pdf page is prepared by making the page.
  349. * The page is made in the pdf document without any contents
  350. * and then stored to add the contents later.
  351. * The page objects is stored using the area tree PageViewport
  352. * as a key.
  353. *
  354. * @param page the page to prepare
  355. */
  356. public void preparePage(PageViewport page) {
  357. this.pdfResources = this.pdfDoc.getResources();
  358. Rectangle2D bounds = page.getViewArea();
  359. double w = bounds.getWidth();
  360. double h = bounds.getHeight();
  361. currentPage = this.pdfDoc.getFactory().makePage(
  362. this.pdfResources,
  363. (int) Math.round(w / 1000), (int) Math.round(h / 1000));
  364. if (pages == null) {
  365. pages = new java.util.HashMap();
  366. }
  367. pages.put(page, currentPage);
  368. pageReferences.put(page.getKey(), currentPage.referencePDF());
  369. pvReferences.put(page.getKey(), page);
  370. }
  371. /**
  372. * This method creates a pdf stream for the current page
  373. * uses it as the contents of a new page. The page is written
  374. * immediately to the output stream.
  375. * @see org.apache.fop.render.Renderer#renderPage(PageViewport)
  376. */
  377. public void renderPage(PageViewport page)
  378. throws IOException, FOPException {
  379. if (pages != null
  380. && (currentPage = (PDFPage) pages.get(page)) != null) {
  381. pages.remove(page);
  382. Rectangle2D bounds = page.getViewArea();
  383. double h = bounds.getHeight();
  384. pageHeight = (int) h;
  385. } else {
  386. this.pdfResources = this.pdfDoc.getResources();
  387. Rectangle2D bounds = page.getViewArea();
  388. double w = bounds.getWidth();
  389. double h = bounds.getHeight();
  390. pageHeight = (int) h;
  391. currentPage = this.pdfDoc.getFactory().makePage(
  392. this.pdfResources,
  393. (int) Math.round(w / 1000), (int) Math.round(h / 1000));
  394. pageReferences.put(page.getKey(), currentPage.referencePDF());
  395. pvReferences.put(page.getKey(), page);
  396. }
  397. currentStream = this.pdfDoc.getFactory()
  398. .makeStream(PDFFilterList.CONTENT_FILTER, false);
  399. currentState = new PDFState();
  400. /* This transform shouldn't affect PDFState as it only sets the basic
  401. * coordinate system for the rendering process.
  402. *
  403. currentState.setTransform(new AffineTransform(1, 0, 0, -1, 0,
  404. (int) Math.round(pageHeight / 1000)));
  405. */
  406. // Transform origin at top left to origin at bottom left
  407. currentStream.add("1 0 0 -1 0 "
  408. + (int) Math.round(pageHeight / 1000) + " cm\n");
  409. currentBasicTransform = new AffineTransform(1, 0, 0, -1, 0,
  410. (int) Math.round(pageHeight / 1000));
  411. currentFontName = "";
  412. Page p = page.getPage();
  413. renderPageAreas(p);
  414. this.pdfDoc.registerObject(currentStream);
  415. currentPage.setContents(currentStream);
  416. PDFAnnotList annots = currentPage.getAnnotations();
  417. if (annots != null) {
  418. this.pdfDoc.addObject(annots);
  419. }
  420. this.pdfDoc.addObject(currentPage);
  421. this.pdfDoc.output(ostream);
  422. }
  423. /**
  424. * @see org.apache.fop.render.AbstractRenderer#startVParea(CTM)
  425. */
  426. protected void startVParea(CTM ctm) {
  427. // Set the given CTM in the graphics state
  428. currentState.push();
  429. currentState.setTransform(
  430. new AffineTransform(CTMHelper.toPDFArray(ctm)));
  431. saveGraphicsState();
  432. // multiply with current CTM
  433. currentStream.add(CTMHelper.toPDFString(ctm) + " cm\n");
  434. // Set clip?
  435. }
  436. /**
  437. * @see org.apache.fop.render.AbstractRenderer#endVParea()
  438. */
  439. protected void endVParea() {
  440. restoreGraphicsState();
  441. currentState.pop();
  442. }
  443. /**
  444. * Handle the traits for a region
  445. * This is used to draw the traits for the given page region.
  446. * (See Sect. 6.4.1.2 of XSL-FO spec.)
  447. * @param region the RegionViewport whose region is to be drawn
  448. */
  449. protected void handleRegionTraits(RegionViewport region) {
  450. currentFontName = "";
  451. Rectangle2D viewArea = region.getViewArea();
  452. float startx = (float)(viewArea.getX() / 1000f);
  453. float starty = (float)(viewArea.getY() / 1000f);
  454. float width = (float)(viewArea.getWidth() / 1000f);
  455. float height = (float)(viewArea.getHeight() / 1000f);
  456. if (region.getRegionReference().getRegionClass() == FO_REGION_BODY) {
  457. currentBPPosition = region.getBorderAndPaddingWidthBefore();
  458. currentIPPosition = region.getBorderAndPaddingWidthStart();
  459. }
  460. drawBackAndBorders(region, startx, starty, width, height);
  461. }
  462. /**
  463. * Handle block traits.
  464. * The block could be any sort of block with any positioning
  465. * so this should render the traits such as border and background
  466. * in its position.
  467. *
  468. * @param block the block to render the traits
  469. */
  470. protected void handleBlockTraits(Block block) {
  471. int borderPaddingStart = block.getBorderAndPaddingWidthStart();
  472. int borderPaddingBefore = block.getBorderAndPaddingWidthBefore();
  473. float startx = currentIPPosition / 1000f;
  474. float starty = currentBPPosition / 1000f;
  475. float width = block.getIPD() / 1000f;
  476. float height = block.getBPD() / 1000f;
  477. /* using start-indent now
  478. Integer spaceStart = (Integer) block.getTrait(Trait.SPACE_START);
  479. if (spaceStart != null) {
  480. startx += spaceStart.floatValue() / 1000f;
  481. }*/
  482. startx += block.getStartIndent() / 1000f;
  483. startx -= block.getBorderAndPaddingWidthStart() / 1000f;
  484. width += borderPaddingStart / 1000f;
  485. width += block.getBorderAndPaddingWidthEnd() / 1000f;
  486. height += borderPaddingBefore / 1000f;
  487. height += block.getBorderAndPaddingWidthAfter() / 1000f;
  488. drawBackAndBorders(block, startx, starty,
  489. width, height);
  490. }
  491. /**
  492. * Draw the background and borders.
  493. * This draws the background and border traits for an area given
  494. * the position.
  495. *
  496. * @param area the area to get the traits from
  497. * @param startx the start x position
  498. * @param starty the start y position
  499. * @param width the width of the area
  500. * @param height the height of the area
  501. */
  502. protected void drawBackAndBorders(Area area,
  503. float startx, float starty,
  504. float width, float height) {
  505. // draw background then border
  506. BorderProps bpsBefore = (BorderProps)area.getTrait(Trait.BORDER_BEFORE);
  507. BorderProps bpsAfter = (BorderProps)area.getTrait(Trait.BORDER_AFTER);
  508. BorderProps bpsStart = (BorderProps)area.getTrait(Trait.BORDER_START);
  509. BorderProps bpsEnd = (BorderProps)area.getTrait(Trait.BORDER_END);
  510. Trait.Background back;
  511. back = (Trait.Background)area.getTrait(Trait.BACKGROUND);
  512. if (back != null) {
  513. endTextObject();
  514. //Calculate padding rectangle
  515. float sx = startx;
  516. float sy = starty;
  517. float paddRectWidth = width;
  518. float paddRectHeight = height;
  519. if (bpsStart != null) {
  520. sx += bpsStart.width / 1000f;
  521. paddRectWidth -= bpsStart.width / 1000f;
  522. }
  523. if (bpsBefore != null) {
  524. sy += bpsBefore.width / 1000f;
  525. paddRectHeight -= bpsBefore.width / 1000f;
  526. }
  527. if (bpsEnd != null) {
  528. paddRectWidth -= bpsEnd.width / 1000f;
  529. }
  530. if (bpsAfter != null) {
  531. paddRectHeight -= bpsAfter.width / 1000f;
  532. }
  533. if (back.getColor() != null) {
  534. updateColor(back.getColor(), true, null);
  535. currentStream.add(sx + " " + sy + " "
  536. + paddRectWidth + " " + paddRectHeight + " re\n");
  537. currentStream.add("f\n");
  538. }
  539. if (back.getFopImage() != null) {
  540. FopImage fopimage = back.getFopImage();
  541. if (fopimage != null && fopimage.load(FopImage.DIMENSIONS)) {
  542. saveGraphicsState();
  543. clip(sx, sy, paddRectWidth, paddRectHeight);
  544. int horzCount = (int)((paddRectWidth
  545. * 1000 / fopimage.getIntrinsicWidth()) + 1.0f);
  546. int vertCount = (int)((paddRectHeight
  547. * 1000 / fopimage.getIntrinsicHeight()) + 1.0f);
  548. if (back.getRepeat() == EN_NOREPEAT) {
  549. horzCount = 1;
  550. vertCount = 1;
  551. } else if (back.getRepeat() == EN_REPEATX) {
  552. vertCount = 1;
  553. } else if (back.getRepeat() == EN_REPEATY) {
  554. horzCount = 1;
  555. }
  556. //change from points to millipoints
  557. sx *= 1000;
  558. sy *= 1000;
  559. if (horzCount == 1) {
  560. sx += back.getHoriz();
  561. }
  562. if (vertCount == 1) {
  563. sy += back.getVertical();
  564. }
  565. for (int x = 0; x < horzCount; x++) {
  566. for (int y = 0; y < vertCount; y++) {
  567. // place once
  568. Rectangle2D pos;
  569. pos = new Rectangle2D.Float(sx + (x * fopimage.getIntrinsicWidth()),
  570. sy + (y * fopimage.getIntrinsicHeight()),
  571. fopimage.getIntrinsicWidth(),
  572. fopimage.getIntrinsicHeight());
  573. putImage(back.getURL(), pos);
  574. }
  575. }
  576. restoreGraphicsState();
  577. } else {
  578. log.warn("Can't find background image: " + back.getURL());
  579. }
  580. }
  581. }
  582. boolean[] b = new boolean[] {
  583. (bpsBefore != null), (bpsEnd != null),
  584. (bpsAfter != null), (bpsStart != null)};
  585. if (!b[0] && !b[1] && !b[2] && !b[3]) {
  586. return;
  587. }
  588. float[] bw = new float[] {
  589. (b[0] ? bpsBefore.width / 1000f : 0.0f),
  590. (b[1] ? bpsEnd.width / 1000f : 0.0f),
  591. (b[2] ? bpsAfter.width / 1000f : 0.0f),
  592. (b[3] ? bpsStart.width / 1000f : 0.0f)};
  593. float[] clipw = new float[] {
  594. BorderProps.getClippedWidth(bpsBefore) / 1000f,
  595. BorderProps.getClippedWidth(bpsEnd) / 1000f,
  596. BorderProps.getClippedWidth(bpsAfter) / 1000f,
  597. BorderProps.getClippedWidth(bpsStart) / 1000f};
  598. starty += clipw[0];
  599. height -= clipw[0];
  600. height -= clipw[2];
  601. startx += clipw[3];
  602. width -= clipw[3];
  603. width -= clipw[1];
  604. boolean[] slant = new boolean[] {
  605. (b[3] && b[0]), (b[0] && b[1]), (b[1] && b[2]), (b[2] && b[3])};
  606. if (bpsBefore != null) {
  607. endTextObject();
  608. float sx1 = startx;
  609. float sx2 = (slant[0] ? sx1 + bw[3] - clipw[3] : sx1);
  610. float ex1 = startx + width;
  611. float ex2 = (slant[1] ? ex1 - bw[1] + clipw[1] : ex1);
  612. float outery = starty - clipw[0];
  613. float clipy = outery + clipw[0];
  614. float innery = outery + bw[0];
  615. saveGraphicsState();
  616. moveTo(sx1, clipy);
  617. float sx1a = sx1;
  618. float ex1a = ex1;
  619. if (bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
  620. if (bpsStart != null && bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
  621. sx1a -= clipw[3];
  622. }
  623. if (bpsEnd != null && bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
  624. ex1a += clipw[1];
  625. }
  626. lineTo(sx1a, outery);
  627. lineTo(ex1a, outery);
  628. }
  629. lineTo(ex1, clipy);
  630. lineTo(ex2, innery);
  631. lineTo(sx2, innery);
  632. closePath();
  633. clip();
  634. drawBorderLine(sx1a, outery, ex1a, innery, true, true,
  635. bpsBefore.style, bpsBefore.color);
  636. restoreGraphicsState();
  637. }
  638. if (bpsEnd != null) {
  639. endTextObject();
  640. float sy1 = starty;
  641. float sy2 = (slant[1] ? sy1 + bw[0] - clipw[0] : sy1);
  642. float ey1 = starty + height;
  643. float ey2 = (slant[2] ? ey1 - bw[2] + clipw[2] : ey1);
  644. float outerx = startx + width + clipw[1];
  645. float clipx = outerx - clipw[1];
  646. float innerx = outerx - bw[1];
  647. saveGraphicsState();
  648. moveTo(clipx, sy1);
  649. float sy1a = sy1;
  650. float ey1a = ey1;
  651. if (bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
  652. if (bpsBefore != null && bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
  653. sy1a -= clipw[0];
  654. }
  655. if (bpsAfter != null && bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
  656. ey1a += clipw[2];
  657. }
  658. lineTo(outerx, sy1a);
  659. lineTo(outerx, ey1a);
  660. }
  661. lineTo(clipx, ey1);
  662. lineTo(innerx, ey2);
  663. lineTo(innerx, sy2);
  664. closePath();
  665. clip();
  666. drawBorderLine(innerx, sy1a, outerx, ey1a, false, false, bpsEnd.style, bpsEnd.color);
  667. restoreGraphicsState();
  668. }
  669. if (bpsAfter != null) {
  670. endTextObject();
  671. float sx1 = startx;
  672. float sx2 = (slant[3] ? sx1 + bw[3] - clipw[3] : sx1);
  673. float ex1 = startx + width;
  674. float ex2 = (slant[2] ? ex1 - bw[1] + clipw[1] : ex1);
  675. float outery = starty + height + clipw[2];
  676. float clipy = outery - clipw[2];
  677. float innery = outery - bw[2];
  678. saveGraphicsState();
  679. moveTo(ex1, clipy);
  680. float sx1a = sx1;
  681. float ex1a = ex1;
  682. if (bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
  683. if (bpsStart != null && bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
  684. sx1a -= clipw[3];
  685. }
  686. if (bpsEnd != null && bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
  687. ex1a += clipw[1];
  688. }
  689. lineTo(ex1a, outery);
  690. lineTo(sx1a, outery);
  691. }
  692. lineTo(sx1, clipy);
  693. lineTo(sx2, innery);
  694. lineTo(ex2, innery);
  695. closePath();
  696. clip();
  697. drawBorderLine(sx1a, innery, ex1a, outery, true, false, bpsAfter.style, bpsAfter.color);
  698. restoreGraphicsState();
  699. }
  700. if (bpsStart != null) {
  701. endTextObject();
  702. float sy1 = starty;
  703. float sy2 = (slant[0] ? sy1 + bw[0] - clipw[0] : sy1);
  704. float ey1 = sy1 + height;
  705. float ey2 = (slant[3] ? ey1 - bw[2] + clipw[2] : ey1);
  706. float outerx = startx - clipw[3];
  707. float clipx = outerx + clipw[3];
  708. float innerx = outerx + bw[3];
  709. saveGraphicsState();
  710. moveTo(clipx, ey1);
  711. float sy1a = sy1;
  712. float ey1a = ey1;
  713. if (bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
  714. if (bpsBefore != null && bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
  715. sy1a -= clipw[0];
  716. }
  717. if (bpsAfter != null && bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
  718. ey1a += clipw[2];
  719. }
  720. lineTo(outerx, ey1a);
  721. lineTo(outerx, sy1a);
  722. }
  723. lineTo(clipx, sy1);
  724. lineTo(innerx, sy2);
  725. lineTo(innerx, ey2);
  726. closePath();
  727. clip();
  728. drawBorderLine(outerx, sy1a, innerx, ey1a, false, true, bpsStart.style, bpsStart.color);
  729. restoreGraphicsState();
  730. }
  731. }
  732. private Color lightenColor(Color col, float factor) {
  733. float[] cols = new float[3];
  734. cols = col.getColorComponents(cols);
  735. if (factor > 0) {
  736. cols[0] += (1.0 - cols[0]) * factor;
  737. cols[1] += (1.0 - cols[1]) * factor;
  738. cols[2] += (1.0 - cols[2]) * factor;
  739. } else {
  740. cols[0] -= cols[0] * -factor;
  741. cols[1] -= cols[1] * -factor;
  742. cols[2] -= cols[2] * -factor;
  743. }
  744. return new Color(cols[0], cols[1], cols[2]);
  745. }
  746. private void drawBorderLine(float x1, float y1, float x2, float y2,
  747. boolean horz, boolean startOrBefore, int style, ColorType col) {
  748. float w = x2 - x1;
  749. float h = y2 - y1;
  750. if ((w < 0) || (h < 0)) {
  751. log.error("Negative extent received. Border won't be painted.");
  752. return;
  753. }
  754. switch (style) {
  755. case Constants.EN_DASHED:
  756. setColor(toColor(col), false, null);
  757. if (horz) {
  758. float unit = Math.abs(2 * h);
  759. int rep = (int)(w / unit);
  760. if (rep % 2 == 0) {
  761. rep++;
  762. }
  763. unit = w / rep;
  764. currentStream.add("[" + unit + "] 0 d ");
  765. currentStream.add(h + " w\n");
  766. float ym = y1 + (h / 2);
  767. currentStream.add(x1 + " " + ym + " m " + x2 + " " + ym + " l S\n");
  768. } else {
  769. float unit = Math.abs(2 * w);
  770. int rep = (int)(h / unit);
  771. if (rep % 2 == 0) {
  772. rep++;
  773. }
  774. unit = h / rep;
  775. currentStream.add("[" + unit + "] 0 d ");
  776. currentStream.add(w + " w\n");
  777. float xm = x1 + (w / 2);
  778. currentStream.add(xm + " " + y1 + " m " + xm + " " + y2 + " l S\n");
  779. }
  780. break;
  781. case Constants.EN_DOTTED:
  782. setColor(toColor(col), false, null);
  783. currentStream.add("1 J ");
  784. if (horz) {
  785. float unit = Math.abs(2 * h);
  786. int rep = (int)(w / unit);
  787. if (rep % 2 == 0) {
  788. rep++;
  789. }
  790. unit = w / rep;
  791. currentStream.add("[0 " + unit + "] 0 d ");
  792. currentStream.add(h + " w\n");
  793. float ym = y1 + (h / 2);
  794. currentStream.add(x1 + " " + ym + " m " + x2 + " " + ym + " l S\n");
  795. } else {
  796. float unit = Math.abs(2 * w);
  797. int rep = (int)(h / unit);
  798. if (rep % 2 == 0) {
  799. rep++;
  800. }
  801. unit = h / rep;
  802. currentStream.add("[0 " + unit + " ] 0 d ");
  803. currentStream.add(w + " w\n");
  804. float xm = x1 + (w / 2);
  805. currentStream.add(xm + " " + y1 + " m " + xm + " " + y2 + " l S\n");
  806. }
  807. break;
  808. case Constants.EN_DOUBLE:
  809. setColor(toColor(col), false, null);
  810. currentStream.add("[] 0 d ");
  811. if (horz) {
  812. float h3 = h / 3;
  813. currentStream.add(h3 + " w\n");
  814. float ym1 = y1 + (h3 / 2);
  815. float ym2 = ym1 + h3 + h3;
  816. currentStream.add(x1 + " " + ym1 + " m " + x2 + " " + ym1 + " l S\n");
  817. currentStream.add(x1 + " " + ym2 + " m " + x2 + " " + ym2 + " l S\n");
  818. } else {
  819. float w3 = w / 3;
  820. currentStream.add(w3 + " w\n");
  821. float xm1 = x1 + (w3 / 2);
  822. float xm2 = xm1 + w3 + w3;
  823. currentStream.add(xm1 + " " + y1 + " m " + xm1 + " " + y2 + " l S\n");
  824. currentStream.add(xm2 + " " + y1 + " m " + xm2 + " " + y2 + " l S\n");
  825. }
  826. break;
  827. case Constants.EN_GROOVE:
  828. case Constants.EN_RIDGE:
  829. {
  830. float colFactor = (style == EN_GROOVE ? 0.4f : -0.4f);
  831. currentStream.add("[] 0 d ");
  832. Color c = toColor(col);
  833. if (horz) {
  834. Color uppercol = lightenColor(c, -colFactor);
  835. Color lowercol = lightenColor(c, colFactor);
  836. float h3 = h / 3;
  837. currentStream.add(h3 + " w\n");
  838. float ym1 = y1 + (h3 / 2);
  839. setColor(uppercol, false, null);
  840. currentStream.add(x1 + " " + ym1 + " m " + x2 + " " + ym1 + " l S\n");
  841. setColor(c, false, null);
  842. currentStream.add(x1 + " " + (ym1 + h3) + " m " + x2 + " " + (ym1 + h3) + " l S\n");
  843. setColor(lowercol, false, null);
  844. currentStream.add(x1 + " " + (ym1 + h3 + h3) + " m " + x2 + " " + (ym1 + h3 + h3) + " l S\n");
  845. } else {
  846. Color leftcol = lightenColor(c, -colFactor);
  847. Color rightcol = lightenColor(c, colFactor);
  848. float w3 = w / 3;
  849. currentStream.add(w3 + " w\n");
  850. float xm1 = x1 + (w3 / 2);
  851. setColor(leftcol, false, null);
  852. currentStream.add(xm1 + " " + y1 + " m " + xm1 + " " + y2 + " l S\n");
  853. setColor(c, false, null);
  854. currentStream.add((xm1 + w3) + " " + y1 + " m " + (xm1 + w3) + " " + y2 + " l S\n");
  855. setColor(rightcol, false, null);
  856. currentStream.add((xm1 + w3 + w3) + " " + y1 + " m " + (xm1 + w3 + w3) + " " + y2 + " l S\n");
  857. }
  858. break;
  859. }
  860. case Constants.EN_INSET:
  861. case Constants.EN_OUTSET:
  862. {
  863. float colFactor = (style == EN_OUTSET ? 0.4f : -0.4f);
  864. currentStream.add("[] 0 d ");
  865. Color c = toColor(col);
  866. if (horz) {
  867. c = lightenColor(c, (startOrBefore ? 1 : -1) * colFactor);
  868. currentStream.add(h + " w\n");
  869. float ym1 = y1 + (h / 2);
  870. setColor(c, false, null);
  871. currentStream.add(x1 + " " + ym1 + " m " + x2 + " " + ym1 + " l S\n");
  872. } else {
  873. c = lightenColor(c, (startOrBefore ? 1 : -1) * colFactor);
  874. currentStream.add(w + " w\n");
  875. float xm1 = x1 + (w / 2);
  876. setColor(c, false, null);
  877. currentStream.add(xm1 + " " + y1 + " m " + xm1 + " " + y2 + " l S\n");
  878. }
  879. break;
  880. }
  881. case Constants.EN_HIDDEN:
  882. break;
  883. default:
  884. setColor(toColor(col), false, null);
  885. currentStream.add("[] 0 d ");
  886. if (horz) {
  887. currentStream.add(h + " w\n");
  888. float ym = y1 + (h / 2);
  889. currentStream.add(x1 + " " + ym + " m " + x2 + " " + ym + " l S\n");
  890. } else {
  891. currentStream.add(w + " w\n");
  892. float xm = x1 + (w / 2);
  893. currentStream.add(xm + " " + y1 + " m " + xm + " " + y2 + " l S\n");
  894. }
  895. }
  896. }
  897. /**
  898. * Sets the current line width in points.
  899. * @param width line width in points
  900. */
  901. private void updateLineWidth(float width) {
  902. if (currentState.setLineWidth(width)) {
  903. //Only write if value has changed WRT the current line width
  904. currentStream.add(width + " w\n");
  905. }
  906. }
  907. private void updateLineStyle(int style) {
  908. switch (style) {
  909. case Constants.EN_DASHED:
  910. currentStream.add("[3] 0 d\n");
  911. break;
  912. case Constants.EN_DOTTED:
  913. currentStream.add("[1 7] 0 d\n");
  914. break;
  915. default:
  916. // solid
  917. currentStream.add("[] 0 d\n");
  918. break;
  919. }
  920. }
  921. /**
  922. * Moves the current point to (x, y), omitting any connecting line segment.
  923. * @param x x coordinate
  924. * @param y y coordinate
  925. */
  926. private void moveTo(float x, float y) {
  927. currentStream.add(x + " " + y + " m ");
  928. }
  929. /**
  930. * Appends a straight line segment from the current point to (x, y). The
  931. * new current point is (x, y).
  932. * @param x x coordinate
  933. * @param y y coordinate
  934. */
  935. private void lineTo(float x, float y) {
  936. currentStream.add(x + " " + y + " l ");
  937. }
  938. /**
  939. * Closes the current subpath by appending a straight line segment from
  940. * the current point to the starting point of the subpath.
  941. */
  942. private void closePath() {
  943. currentStream.add("h ");
  944. }
  945. /**
  946. * Draw a line.
  947. *
  948. * @param startx the start x position
  949. * @param starty the start y position
  950. * @param endx the x end position
  951. * @param endy the y end position
  952. */
  953. private void drawLine(float startx, float starty, float endx, float endy) {
  954. currentStream.add(startx + " " + starty + " m ");
  955. currentStream.add(endx + " " + endy + " l S\n");
  956. }
  957. /**
  958. * @see org.apache.fop.render.AbstractRenderer#renderBlockViewport(BlockViewport, List)
  959. */
  960. protected void renderBlockViewport(BlockViewport bv, List children) {
  961. // clip and position viewport if necessary
  962. // save positions
  963. int saveIP = currentIPPosition;
  964. int saveBP = currentBPPosition;
  965. String saveFontName = currentFontName;
  966. CTM ctm = bv.getCTM();
  967. int borderPaddingStart = bv.getBorderAndPaddingWidthStart();
  968. int borderPaddingBefore = bv.getBorderAndPaddingWidthBefore();
  969. float x, y;
  970. x = (float)(bv.getXOffset() + containingIPPosition) / 1000f;
  971. y = (float)(bv.getYOffset() + containingBPPosition) / 1000f;
  972. if (bv.getPositioning() == Block.ABSOLUTE
  973. || bv.getPositioning() == Block.FIXED) {
  974. //For FIXED, we need to break out of the current viewports to the
  975. //one established by the page. We save the state stack for restoration
  976. //after the block-container has been painted. See below.
  977. List breakOutList = null;
  978. if (bv.getPositioning() == Block.FIXED) {
  979. //break out
  980. breakOutList = new java.util.ArrayList();
  981. PDFState.Data data;
  982. while (true) {
  983. data = currentState.getData();
  984. if (currentState.pop() == null) {
  985. break;
  986. }
  987. if (breakOutList.size() == 0) {
  988. comment("------ break out!");
  989. }
  990. breakOutList.add(0, data); //Insert because of stack-popping
  991. //getLogger().debug("Adding to break out list: " + data);
  992. restoreGraphicsState();
  993. }
  994. }
  995. CTM tempctm = new CTM(containingIPPosition, containingBPPosition);
  996. ctm = tempctm.multiply(ctm);
  997. //This is the content-rect
  998. float width = (float)bv.getIPD() / 1000f;
  999. float height = (float)bv.getBPD() / 1000f;
  1000. //Adjust for spaces (from margin or indirectly by start-indent etc.
  1001. Integer spaceStart = (Integer) bv.getTrait(Trait.SPACE_START);
  1002. if (spaceStart != null) {
  1003. x += spaceStart.floatValue() / 1000;
  1004. }
  1005. Integer spaceBefore = (Integer) bv.getTrait(Trait.SPACE_BEFORE);
  1006. if (spaceBefore != null) {
  1007. y += spaceBefore.floatValue() / 1000;
  1008. }
  1009. float bpwidth = (borderPaddingStart + bv.getBorderAndPaddingWidthEnd()) / 1000f;
  1010. float bpheight = (borderPaddingBefore + bv.getBorderAndPaddingWidthAfter()) / 1000f;
  1011. drawBackAndBorders(bv, x, y, width + bpwidth, height + bpheight);
  1012. //Now adjust for border/padding
  1013. x += borderPaddingStart / 1000f;
  1014. y += borderPaddingBefore / 1000f;
  1015. if (bv.getClip()) {
  1016. saveGraphicsState();
  1017. clip(x, y, width, height);
  1018. }
  1019. startVParea(ctm);
  1020. currentIPPosition = 0;
  1021. currentBPPosition = 0;
  1022. renderBlocks(bv, children);
  1023. endVParea();
  1024. if (bv.getClip()) {
  1025. restoreGraphicsState();
  1026. }
  1027. // clip if necessary
  1028. if (breakOutList != null) {
  1029. comment("------ restoring context after break-out...");
  1030. PDFState.Data data;
  1031. Iterator i = breakOutList.iterator();
  1032. while (i.hasNext()) {
  1033. data = (PDFState.Data)i.next();
  1034. //getLogger().debug("Restoring: " + data);
  1035. currentState.push();
  1036. saveGraphicsState();
  1037. if (data.concatenations != null) {
  1038. Iterator tr = data.concatenations.iterator();
  1039. while (tr.hasNext()) {
  1040. AffineTransform at = (AffineTransform)tr.next();
  1041. currentState.setTransform(at);
  1042. double[] matrix = new double[6];
  1043. at.getMatrix(matrix);
  1044. tempctm = new CTM(matrix[0], matrix[1], matrix[2], matrix[3],
  1045. matrix[4] * 1000, matrix[5] * 1000);
  1046. currentStream.add(CTMHelper.toPDFString(tempctm) + " cm\n");
  1047. }
  1048. }
  1049. //TODO Break-out: Also restore items such as line width and color
  1050. //Left out for now because all this painting stuff is very
  1051. //inconsistent. Some values go over PDFState, some don't.
  1052. }
  1053. comment("------ done.");
  1054. }
  1055. currentIPPosition = saveIP;
  1056. currentBPPosition = saveBP;
  1057. } else {
  1058. Integer spaceBefore = (Integer)bv.getTrait(Trait.SPACE_BEFORE);
  1059. if (spaceBefore != null) {
  1060. currentBPPosition += spaceBefore.intValue();
  1061. }
  1062. //borders and background in the old coordinate system
  1063. handleBlockTraits(bv);
  1064. CTM tempctm = new CTM(containingIPPosition, currentBPPosition);
  1065. ctm = tempctm.multiply(ctm);
  1066. //Now adjust for border/padding
  1067. x += borderPaddingStart / 1000f;
  1068. y += borderPaddingBefore / 1000f;
  1069. // clip if necessary
  1070. if (bv.getClip()) {
  1071. saveGraphicsState();
  1072. float width = (float)bv.getIPD() / 1000f;
  1073. float height = (float)bv.getBPD() / 1000f;
  1074. clip(x, y, width, height);
  1075. }
  1076. if (ctm != null) {
  1077. startVParea(ctm);
  1078. currentIPPosition = 0;
  1079. currentBPPosition = 0;
  1080. }
  1081. renderBlocks(bv, children);
  1082. if (ctm != null) {
  1083. endVParea();
  1084. }
  1085. if (bv.getClip()) {
  1086. restoreGraphicsState();
  1087. }
  1088. currentIPPosition = saveIP;
  1089. currentBPPosition = saveBP;
  1090. //Adjust BP position (alloc BPD + spaces)
  1091. if (spaceBefore != null) {
  1092. currentBPPosition += spaceBefore.intValue();
  1093. }
  1094. currentBPPosition += (int)(bv.getAllocBPD());
  1095. Integer spaceAfter = (Integer)bv.getTrait(Trait.SPACE_AFTER);
  1096. if (spaceAfter != null) {
  1097. currentBPPosition += spaceAfter.intValue();
  1098. }
  1099. }
  1100. currentFontName = saveFontName;
  1101. }
  1102. /**
  1103. * Clip an area.
  1104. * write a clipping operation given coordinates in the current
  1105. * transform.
  1106. * @param x the x coordinate
  1107. * @param y the y coordinate
  1108. * @param width the width of the area
  1109. * @param height the height of the area
  1110. */
  1111. protected void clip(float x, float y, float width, float height) {
  1112. currentStream.add(x + " " + y + " " + width + " " + height + " re ");
  1113. clip();
  1114. }
  1115. /**
  1116. * Clip an area.
  1117. */
  1118. protected void clip() {
  1119. currentStream.add("W\n");
  1120. currentStream.add("n\n");
  1121. }
  1122. /**
  1123. * @see org.apache.fop.render.AbstractRenderer#renderLineArea(LineArea)
  1124. */
  1125. protected void renderLineArea(LineArea line) {
  1126. super.renderLineArea(line);
  1127. closeText();
  1128. }
  1129. /**
  1130. * Render inline parent area.
  1131. * For pdf this handles the inline parent area traits such as
  1132. * links, border, background.
  1133. * @param ip the inline parent area
  1134. */
  1135. public void renderInlineParent(InlineParent ip) {
  1136. float start = currentIPPosition / 1000f;
  1137. float top = (ip.getOffset() + currentBPPosition) / 1000f;
  1138. float width = ip.getIPD() / 1000f;
  1139. float height = ip.getBPD() / 1000f;
  1140. drawBackAndBorders(ip, start, top, width, height);
  1141. // render contents
  1142. super.renderInlineParent(ip);
  1143. // place the link over the top
  1144. Object tr = ip.getTrait(Trait.INTERNAL_LINK);
  1145. boolean internal = false;
  1146. String dest = null;
  1147. float yoffset = 0;
  1148. if (tr == null) {
  1149. dest = (String)ip.getTrait(Trait.EXTERNAL_LINK);
  1150. } else {
  1151. String pvKey = (String)tr;
  1152. dest = (String)pageReferences.get(pvKey);
  1153. if (dest != null) {
  1154. PageViewport pv = (PageViewport)pvReferences.get(pvKey);
  1155. Rectangle2D bounds = pv.getViewArea();
  1156. double h = bounds.getHeight();
  1157. yoffset = (float)h / 1000f;
  1158. internal = true;
  1159. }
  1160. }
  1161. if (dest != null) {
  1162. // add link to pdf document
  1163. Rectangle2D rect = new Rectangle2D.Float(start, top, width, height);
  1164. // transform rect to absolute coords
  1165. AffineTransform transform = currentState.getTransform();
  1166. rect = transform.createTransformedShape(rect).getBounds2D();
  1167. rect = currentBasicTransform.createTransformedShape(rect).getBounds2D();
  1168. int type = internal ? PDFLink.INTERNAL : PDFLink.EXTERNAL;
  1169. PDFLink pdflink = pdfDoc.getFactory().makeLink(
  1170. rect, dest, type, yoffset);
  1171. currentPage.addAnnotation(pdflink);
  1172. }
  1173. }
  1174. /**
  1175. * @see org.apache.fop.render.Renderer#renderCharacter(Character)
  1176. */
  1177. public void renderCharacter(Character ch) {
  1178. StringBuffer pdf = new StringBuffer();
  1179. String name = (String) ch.getTrait(Trait.FONT_NAME);
  1180. int size = ((Integer) ch.getTrait(Trait.FONT_SIZE)).intValue();
  1181. // This assumes that *all* CIDFonts use a /ToUnicode mapping
  1182. Typeface f = (Typeface) fontInfo.getFonts().get(name);
  1183. boolean useMultiByte = f.isMultiByte();
  1184. // String startText = useMultiByte ? "<FEFF" : "(";
  1185. String startText = useMultiByte ? "<" : "(";
  1186. String endText = useMultiByte ? "> " : ") ";
  1187. updateFont(name, size, pdf);
  1188. ColorType ct = (ColorType) ch.getTrait(Trait.COLOR);
  1189. if (ct != null) {
  1190. updateColor(ct, true, pdf);
  1191. }
  1192. // word.getOffset() = only height of text itself
  1193. // currentBlockIPPosition: 0 for beginning of line; nonzero
  1194. // where previous line area failed to take up entire allocated space
  1195. int rx = currentIPPosition;
  1196. int bl = currentBPPosition + ch.getOffset();
  1197. /* System.out.println("Text = " + ch.getTextArea() +
  1198. "; text width: " + ch.getWidth() +
  1199. "; BlockIP Position: " + currentBlockIPPosition +
  1200. "; currentBPPosition: " + currentBPPosition +
  1201. "; offset: " + ch.getOffset());
  1202. */
  1203. // Set letterSpacing
  1204. //float ls = fs.getLetterSpacing() / this.currentFontSize;
  1205. //pdf.append(ls).append(" Tc\n");
  1206. if (!textOpen || bl != prevWordY) {
  1207. closeText();
  1208. pdf.append("1 0 0 -1 " + (rx / 1000f) + " " + (bl / 1000f) + " Tm "
  1209. + (ch.getTextLetterSpaceAdjust() / 1000f) + " Tc "
  1210. + (ch.getTextWordSpaceAdjust() / 1000f) + " Tw [" + startText);
  1211. prevWordY = bl;
  1212. textOpen = true;
  1213. } else {
  1214. closeText();
  1215. pdf.append("1 0 0 -1 " + (rx / 1000f) + " " + (bl / 1000f) + " Tm "
  1216. + (ch.getTextLetterSpaceAdjust() / 1000f) + " Tc "
  1217. + (ch.getTextWordSpaceAdjust() / 1000f) + " Tw [" + startText);
  1218. textOpen = true;
  1219. }
  1220. prevWordWidth = ch.getIPD();
  1221. prevWordX = rx;
  1222. String s = ch.getChar();
  1223. FontMetrics metrics = fontInfo.getMetricsFor(name);
  1224. Font fs = new Font(name, metrics, size);
  1225. escapeText(s, fs, useMultiByte, pdf);
  1226. pdf.append(endText);
  1227. currentStream.add(pdf.toString());
  1228. renderTextDecoration(fs, ch, bl, rx);
  1229. super.renderCharacter(ch);
  1230. }
  1231. /**
  1232. * @see org.apache.fop.render.Renderer#renderText(TextArea)
  1233. */
  1234. public void renderText(TextArea text) {
  1235. beginTextObject();
  1236. StringBuffer pdf = new StringBuffer();
  1237. String name = (String) text.getTrait(Trait.FONT_NAME);
  1238. int size = ((Integer) text.getTrait(Trait.FONT_SIZE)).intValue();
  1239. // This assumes that *all* CIDFonts use a /ToUnicode mapping
  1240. Typeface f = (Typeface) fontInfo.getFonts().get(name);
  1241. boolean useMultiByte = f.isMultiByte();
  1242. // String startText = useMultiByte ? "<FEFF" : "(";
  1243. String startText = useMultiByte ? "<" : "(";
  1244. String endText = useMultiByte ? "> " : ") ";
  1245. updateFont(name, size, pdf);
  1246. ColorType ct = (ColorType) text.getTrait(Trait.COLOR);
  1247. updateColor(ct, true, pdf);
  1248. // word.getOffset() = only height of text itself
  1249. // currentBlockIPPosition: 0 for beginning of line; nonzero
  1250. // where previous line area failed to take up entire allocated space
  1251. int rx = currentIPPosition;
  1252. int bl = currentBPPosition + text.getOffset();
  1253. /* System.out.println("Text = " + text.getTextArea() +
  1254. "; text width: " + text.getWidth() +
  1255. "; BlockIP Position: " + currentBlockIPPosition +
  1256. "; currentBPPosition: " + currentBPPosition +
  1257. "; offset: " + text.getOffset());
  1258. */
  1259. // Set letterSpacing
  1260. //float ls = fs.getLetterSpacing() / this.currentFontSize;
  1261. //pdf.append(ls).append(" Tc\n");
  1262. if (!textOpen || bl != prevWordY) {
  1263. closeText();
  1264. pdf.append("1 0 0 -1 " + (rx / 1000f) + " " + (bl / 1000f) + " Tm "
  1265. + (text.getTextLetterSpaceAdjust() / 1000f) + " Tc "
  1266. + (text.getTextWordSpaceAdjust() / 1000f) + " Tw [" + startText);
  1267. prevWordY = bl;
  1268. textOpen = true;
  1269. } else {
  1270. closeText();
  1271. pdf.append("1 0 0 -1 " + (rx / 1000f) + " " + (bl / 1000f) + " Tm "
  1272. + (text.getTextLetterSpaceAdjust() / 1000f) + " Tc "
  1273. + (text.getTextWordSpaceAdjust() / 1000f) + " Tw [" + startText);
  1274. textOpen = true;
  1275. }
  1276. prevWordWidth = text.getIPD();
  1277. prevWordX = rx;
  1278. String s = text.getTextArea();
  1279. FontMetrics metrics = fontInfo.getMetricsFor(name);
  1280. Font fs = new Font(name, metrics, size);
  1281. escapeText(s, fs, useMultiByte, pdf);
  1282. pdf.append(endText);
  1283. currentStream.add(pdf.toString());
  1284. renderTextDecoration(fs, text, bl, rx);
  1285. super.renderText(text);
  1286. }
  1287. /**
  1288. * Paints the text decoration marks.
  1289. * @param fs Current font
  1290. * @param inline inline area to paint the marks for
  1291. * @param baseline position of the baseline
  1292. * @param startx start IPD
  1293. */
  1294. protected void renderTextDecoration(Font fs, InlineArea inline,
  1295. int baseline, int startx) {
  1296. boolean hasTextDeco = inline.hasUnderline()
  1297. || inline.hasOverline()
  1298. || inline.hasLineThrough();
  1299. if (hasTextDeco) {
  1300. endTextObject();
  1301. updateLineStyle(Constants.EN_SOLID);
  1302. updateLineWidth(fs.getDescender() / -8 / 1000f);
  1303. float endx = (startx + inline.getIPD()) / 1000f;
  1304. if (inline.hasUnderline()) {
  1305. ColorType ct = (ColorType) inline.getTrait(Trait.UNDERLINE_COLOR);
  1306. updateColor(ct, false, null);
  1307. float y = baseline - fs.getDescender() / 2;
  1308. drawLine(startx / 1000f, y / 1000f, endx, y / 1000f);
  1309. }
  1310. if (inline.hasOverline()) {
  1311. ColorType ct = (ColorType) inline.getTrait(Trait.OVERLINE_COLOR);
  1312. updateColor(ct, false, null);
  1313. float y = (float)(baseline - (1.1 * fs.getCapHeight()));
  1314. drawLine(startx / 1000f, y / 1000f, endx, y / 1000f);
  1315. }
  1316. if (inline.hasLineThrough()) {
  1317. ColorType ct = (ColorType) inline.getTrait(Trait.LINETHROUGH_COLOR);
  1318. updateColor(ct, false, null);
  1319. float y = (float)(baseline - (0.45 * fs.getCapHeight()));
  1320. drawLine(startx / 1000f, y / 1000f, endx, y / 1000f);
  1321. }
  1322. }
  1323. }
  1324. /**
  1325. * Escapes text according to PDF rules.
  1326. * @param s Text to escape
  1327. * @param fs Font state
  1328. * @param useMultiByte Indicates the use of multi byte convention
  1329. * @param pdf target buffer for the escaped text
  1330. */
  1331. public void escapeText(String s, Font fs,
  1332. boolean useMultiByte, StringBuffer pdf) {
  1333. String startText = useMultiByte ? "<" : "(";
  1334. String endText = useMultiByte ? "> " : ") ";
  1335. boolean kerningAvailable = false;
  1336. Map kerning = fs.getKerning();
  1337. if (kerning != null && !kerning.isEmpty()) {
  1338. kerningAvailable = true;
  1339. }
  1340. int l = s.length();
  1341. for (int i = 0; i < l; i++) {
  1342. char ch = fs.mapChar(s.charAt(i));
  1343. if (!useMultiByte) {
  1344. if (ch > 127) {
  1345. pdf.append("\\");
  1346. pdf.append(Integer.toOctalString((int) ch));
  1347. } else {
  1348. switch (ch) {
  1349. case '(':
  1350. case ')':
  1351. case '\\':
  1352. pdf.append("\\");
  1353. break;
  1354. }
  1355. pdf.append(ch);
  1356. }
  1357. } else {
  1358. pdf.append(PDFText.toUnicodeHex(ch));
  1359. }
  1360. if (kerningAvailable && (i + 1) < l) {
  1361. addKerning(pdf, (new Integer((int) ch)),
  1362. (new Integer((int) fs.mapChar(s.charAt(i + 1)))
  1363. ), kerning, startText, endText);
  1364. }
  1365. }
  1366. }
  1367. private void addKerning(StringBuffer buf, Integer ch1, Integer ch2,
  1368. Map kerning, String startText, String endText) {
  1369. Map kernPair = (Map) kerning.get(ch1);
  1370. if (kernPair != null) {
  1371. Integer width = (Integer) kernPair.get(ch2);
  1372. if (width != null) {
  1373. buf.append(endText).append(-width.intValue());
  1374. buf.append(' ').append(startText);
  1375. }
  1376. }
  1377. }
  1378. /**
  1379. * Checks to see if we have some text rendering commands open
  1380. * still and writes out the TJ command to the stream if we do
  1381. */
  1382. protected void closeText() {
  1383. if (textOpen) {
  1384. currentStream.add("] TJ\n");
  1385. textOpen = false;
  1386. prevWordX = 0;
  1387. prevWordY = 0;
  1388. currentFontName = "";
  1389. }
  1390. }
  1391. /**
  1392. * Establishes a new foreground or fill color. In contrast to updateColor
  1393. * this method does not check the PDFState for optimization possibilities.
  1394. * @param col the color to apply
  1395. * @param fill true to set the fill color, false for the foreground color
  1396. * @param pdf StringBuffer to write the PDF code to, if null, the code is
  1397. * written to the current stream.
  1398. */
  1399. protected void setColor(Color col, boolean fill, StringBuffer pdf) {
  1400. PDFColor color = new PDFColor(col);
  1401. closeText();
  1402. if (pdf != null) {
  1403. pdf.append(color.getColorSpaceOut(fill));
  1404. } else {
  1405. currentStream.add(color.getColorSpaceOut(fill));
  1406. }
  1407. }
  1408. /**
  1409. * Converts a ColorType to a java.awt.Color (sRGB).
  1410. * @param col the color
  1411. * @return the converted color
  1412. */
  1413. private Color toColor(ColorType col) {
  1414. return new Color(col.getRed(), col.getGreen(), col.getBlue());
  1415. }
  1416. /**
  1417. * Establishes a new foreground or fill color.
  1418. * @param col the color to apply (null skips this operation)
  1419. * @param fill true to set the fill color, false for the foreground color
  1420. * @param pdf StringBuffer to write the PDF code to, if null, the code is
  1421. * written to the current stream.
  1422. */
  1423. private void updateColor(ColorType col, boolean fill, StringBuffer pdf) {
  1424. if (col == null) {
  1425. return;
  1426. }
  1427. Color newCol = toColor(col);
  1428. boolean update = false;
  1429. if (fill) {
  1430. update = currentState.setBackColor(newCol);
  1431. } else {
  1432. update = currentState.setColor(newCol);
  1433. }
  1434. if (update) {
  1435. setColor(newCol, fill, pdf);
  1436. }
  1437. }
  1438. private void updateFont(String name, int size, StringBuffer pdf) {
  1439. if ((!name.equals(this.currentFontName))
  1440. || (size != this.currentFontSize)) {
  1441. closeText();
  1442. this.currentFontName = name;
  1443. this.currentFontSize = size;
  1444. pdf = pdf.append("/" + name + " " + ((float) size / 1000f)
  1445. + " Tf\n");
  1446. }
  1447. }
  1448. /**
  1449. * @see org.apache.fop.render.AbstractRenderer#renderImage(Image, Rectangle2D)
  1450. */
  1451. public void renderImage(Image image, Rectangle2D pos) {
  1452. endTextObject();
  1453. String url = image.getURL();
  1454. putImage(url, pos);
  1455. }
  1456. /**
  1457. * Adds a PDF XObject (a bitmap) to the PDF that will later be referenced.
  1458. * @param url URL of the bitmap
  1459. * @param pos Position of the bitmap
  1460. */
  1461. protected void putImage(String url, Rectangle2D pos) {
  1462. PDFXObject xobject = pdfDoc.getImage(url);
  1463. if (xobject != null) {
  1464. float w = (float) pos.getWidth() / 1000f;
  1465. float h = (float) pos.getHeight() / 1000f;
  1466. placeImage((float)pos.getX() / 1000f,
  1467. (float)pos.getY() / 1000f, w, h, xobject.getXNumber());
  1468. return;
  1469. }
  1470. url = ImageFactory.getURL(url);
  1471. ImageFactory fact = ImageFactory.getInstance();
  1472. FopImage fopimage = fact.getImage(url, userAgent);
  1473. if (fopimage == null) {
  1474. return;
  1475. }
  1476. if (!fopimage.load(FopImage.DIMENSIONS)) {
  1477. return;
  1478. }
  1479. String mime = fopimage.getMimeType();
  1480. if ("text/xml".equals(mime)) {
  1481. if (!fopimage.load(FopImage.ORIGINAL_DATA)) {
  1482. return;
  1483. }
  1484. Document doc = ((XMLImage) fopimage).getDocument();
  1485. String ns = ((XMLImage) fopimage).getNameSpace();
  1486. renderDocument(doc, ns, pos);
  1487. } else if ("image/svg+xml".equals(mime)) {
  1488. if (!fopimage.load(FopImage.ORIGINAL_DATA)) {
  1489. return;
  1490. }
  1491. Document doc = ((XMLImage) fopimage).getDocument();
  1492. String ns = ((XMLImage) fopimage).getNameSpace();
  1493. renderDocument(doc, ns, pos);
  1494. } else if ("image/eps".equals(mime)) {
  1495. if (!fopimage.load(FopImage.ORIGINAL_DATA)) {
  1496. return;
  1497. }
  1498. FopPDFImage pdfimage = new FopPDFImage(fopimage, url);
  1499. int xobj = pdfDoc.addImage(currentContext, pdfimage).getXNumber();
  1500. fact.releaseImage(url, userAgent);
  1501. } else if ("image/jpeg".equals(mime)) {
  1502. if (!fopimage.load(FopImage.ORIGINAL_DATA)) {
  1503. return;
  1504. }
  1505. FopPDFImage pdfimage = new FopPDFImage(fopimage, url);
  1506. int xobj = pdfDoc.addImage(currentContext, pdfimage).getXNumber();
  1507. fact.releaseImage(url, userAgent);
  1508. float w = (float)pos.getWidth() / 1000f;
  1509. float h = (float)pos.getHeight() / 1000f;
  1510. placeImage((float) pos.getX() / 1000,
  1511. (float) pos.getY() / 1000, w, h, xobj);
  1512. } else {
  1513. if (!fopimage.load(FopImage.BITMAP)) {
  1514. return;
  1515. }
  1516. FopPDFImage pdfimage = new FopPDFImage(fopimage, url);
  1517. int xobj = pdfDoc.addImage(currentContext, pdfimage).getXNumber();
  1518. fact.releaseImage(url, userAgent);
  1519. float w = (float) pos.getWidth() / 1000f;
  1520. float h = (float) pos.getHeight() / 1000f;
  1521. placeImage((float) pos.getX() / 1000f,
  1522. (float) pos.getY() / 1000f, w, h, xobj);
  1523. }
  1524. // output new data
  1525. try {
  1526. this.pdfDoc.output(ostream);
  1527. } catch (IOException ioe) {
  1528. // ioexception will be caught later
  1529. }
  1530. }
  1531. /**
  1532. * Places a previously registered image at a certain place on the page.
  1533. * @param x X coordinate
  1534. * @param y Y coordinate
  1535. * @param w width for image
  1536. * @param h height for image
  1537. * @param xobj object number of the referenced image
  1538. */
  1539. protected void placeImage(float x, float y, float w, float h, int xobj) {
  1540. saveGraphicsState();
  1541. currentStream.add(w + " 0 0 "
  1542. + -h + " "
  1543. + (currentIPPosition / 1000f + x) + " "
  1544. + (currentBPPosition / 1000f + h + y)
  1545. + " cm\n" + "/Im" + xobj + " Do\n");
  1546. restoreGraphicsState();
  1547. }
  1548. /**
  1549. * @see org.apache.fop.render.AbstractRenderer#renderForeignObject(ForeignObject, Rectangle2D)
  1550. */
  1551. public void renderForeignObject(ForeignObject fo, Rectangle2D pos) {
  1552. endTextObject();
  1553. Document doc = fo.getDocument();
  1554. String ns = fo.getNameSpace();
  1555. renderDocument(doc, ns, pos);
  1556. }
  1557. /**
  1558. * Renders an XML document (SVG for example).
  1559. * @param doc DOM document representing the XML document
  1560. * @param ns Namespace for the document
  1561. * @param pos Position on the page
  1562. */
  1563. public void renderDocument(Document doc, String ns, Rectangle2D pos) {
  1564. RendererContext context;
  1565. context = new RendererContext(this, MIME_TYPE);
  1566. context.setUserAgent(userAgent);
  1567. context.setProperty(PDFXMLHandler.PDF_DOCUMENT, pdfDoc);
  1568. context.setProperty(PDFXMLHandler.OUTPUT_STREAM, ostream);
  1569. context.setProperty(PDFXMLHandler.PDF_STATE, currentState);
  1570. context.setProperty(PDFXMLHandler.PDF_PAGE, currentPage);
  1571. context.setProperty(PDFXMLHandler.PDF_CONTEXT,
  1572. currentContext == null ? currentPage : currentContext);
  1573. context.setProperty(PDFXMLHandler.PDF_CONTEXT, currentContext);
  1574. context.setProperty(PDFXMLHandler.PDF_STREAM, currentStream);
  1575. context.setProperty(PDFXMLHandler.PDF_XPOS,
  1576. new Integer(currentIPPosition + (int) pos.getX()));
  1577. context.setProperty(PDFXMLHandler.PDF_YPOS,
  1578. new Integer(currentBPPosition + (int) pos.getY()));
  1579. context.setProperty(PDFXMLHandler.PDF_FONT_INFO, fontInfo);
  1580. context.setProperty(PDFXMLHandler.PDF_FONT_NAME, currentFontName);
  1581. context.setProperty(PDFXMLHandler.PDF_FONT_SIZE,
  1582. new Integer(currentFontSize));
  1583. context.setProperty(PDFXMLHandler.PDF_WIDTH,
  1584. new Integer((int) pos.getWidth()));
  1585. context.setProperty(PDFXMLHandler.PDF_HEIGHT,
  1586. new Integer((int) pos.getHeight()));
  1587. renderXML(context, doc, ns);
  1588. }
  1589. /**
  1590. * Render an inline viewport.
  1591. * This renders an inline viewport by clipping if necessary.
  1592. * @param viewport the viewport to handle
  1593. */
  1594. public void renderViewport(Viewport viewport) {
  1595. float x = currentIPPosition / 1000f;
  1596. float y = (currentBPPosition + viewport.getOffset()) / 1000f;
  1597. float width = viewport.getIPD() / 1000f;
  1598. float height = viewport.getBPD() / 1000f;
  1599. // TODO: Calculate the border rect correctly.
  1600. drawBackAndBorders(viewport, x, y, width, height);
  1601. if (viewport.getClip()) {
  1602. saveGraphicsState();
  1603. clip(x, y, width, height);
  1604. }
  1605. super.renderViewport(viewport);
  1606. if (viewport.getClip()) {
  1607. restoreGraphicsState();
  1608. }
  1609. }
  1610. /**
  1611. * Render leader area.
  1612. * This renders a leader area which is an area with a rule.
  1613. * @param area the leader area to render
  1614. */
  1615. public void renderLeader(Leader area) {
  1616. saveGraphicsState();
  1617. int style = area.getRuleStyle();
  1618. boolean alt = false;
  1619. switch(style) {
  1620. case EN_SOLID:
  1621. currentStream.add("[] 0 d\n");
  1622. break;
  1623. case EN_DOTTED:
  1624. currentStream.add("[2] 0 d\n");
  1625. break;
  1626. case EN_DASHED:
  1627. currentStream.add("[6 4] 0 d\n");
  1628. break;
  1629. case EN_DOUBLE:
  1630. case EN_GROOVE:
  1631. case EN_RIDGE:
  1632. alt = true;
  1633. break;
  1634. }
  1635. float startx = ((float) currentIPPosition) / 1000f;
  1636. float starty = ((currentBPPosition + area.getOffset()) / 1000f);
  1637. float endx = (currentIPPosition + area.getIPD()) / 1000f;
  1638. if (!alt) {
  1639. updateLineWidth(area.getRuleThickness() / 1000f);
  1640. drawLine(startx, starty, endx, starty);
  1641. } else {
  1642. if (style == EN_DOUBLE) {
  1643. float third = area.getRuleThickness() / 3000f;
  1644. updateLineWidth(third);
  1645. drawLine(startx, starty, endx, starty);
  1646. drawLine(startx, (starty + 2 * third), endx, (starty + 2 * third));
  1647. } else {
  1648. float half = area.getRuleThickness() / 2000f;
  1649. currentStream.add("1 g\n");
  1650. currentStream.add(startx + " " + starty + " m\n");
  1651. currentStream.add(endx + " " + starty + " l\n");
  1652. currentStream.add(endx + " " + (starty + 2 * half) + " l\n");
  1653. currentStream.add(startx + " " + (starty + 2 * half) + " l\n");
  1654. currentStream.add("h\n");
  1655. currentStream.add("f\n");
  1656. if (style == EN_GROOVE) {
  1657. currentStream.add("0 g\n");
  1658. currentStream.add(startx + " " + starty + " m\n");
  1659. currentStream.add(endx + " " + starty + " l\n");
  1660. currentStream.add(endx + " " + (starty + half) + " l\n");
  1661. currentStream.add((startx + half) + " " + (starty + half) + " l\n");
  1662. currentStream.add(startx + " " + (starty + 2 * half) + " l\n");
  1663. } else {
  1664. currentStream.add("0 g\n");
  1665. currentStream.add(endx + " " + starty + " m\n");
  1666. currentStream.add(endx + " " + (starty + 2 * half) + " l\n");
  1667. currentStream.add(startx + " " + (starty + 2 * half) + " l\n");
  1668. currentStream.add(startx + " " + (starty + half) + " l\n");
  1669. currentStream.add((endx - half) + " " + (starty + half) + " l\n");
  1670. }
  1671. currentStream.add("h\n");
  1672. currentStream.add("f\n");
  1673. }
  1674. }
  1675. restoreGraphicsState();
  1676. beginTextObject();
  1677. super.renderLeader(area);
  1678. }
  1679. /** @see org.apache.fop.render.AbstractRenderer */
  1680. public String getMimeType() {
  1681. return MIME_TYPE;
  1682. }
  1683. }