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.

AFPRenderer.java 55KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.afp;
  19. import java.awt.Color;
  20. import java.awt.Rectangle;
  21. import java.awt.geom.Rectangle2D;
  22. import java.awt.image.BufferedImage;
  23. import java.io.IOException;
  24. import java.io.OutputStream;
  25. import java.io.UnsupportedEncodingException;
  26. import java.util.ArrayList;
  27. import java.util.HashMap;
  28. import java.util.HashSet;
  29. import java.util.Iterator;
  30. import java.util.List;
  31. import java.util.Map;
  32. import org.apache.commons.io.output.ByteArrayOutputStream;
  33. import org.apache.fop.apps.FOUserAgent;
  34. import org.apache.fop.apps.MimeConstants;
  35. import org.apache.fop.area.Block;
  36. import org.apache.fop.area.BlockViewport;
  37. import org.apache.fop.area.BodyRegion;
  38. import org.apache.fop.area.CTM;
  39. import org.apache.fop.area.OffDocumentItem;
  40. import org.apache.fop.area.PageViewport;
  41. import org.apache.fop.area.RegionReference;
  42. import org.apache.fop.area.RegionViewport;
  43. import org.apache.fop.area.Trait;
  44. import org.apache.fop.area.inline.Image;
  45. import org.apache.fop.area.inline.Leader;
  46. import org.apache.fop.area.inline.SpaceArea;
  47. import org.apache.fop.area.inline.TextArea;
  48. import org.apache.fop.area.inline.WordArea;
  49. import org.apache.fop.fo.Constants;
  50. import org.apache.fop.fo.extensions.ExtensionAttachment;
  51. import org.apache.fop.fonts.FontInfo;
  52. import org.apache.fop.fonts.FontMetrics;
  53. import org.apache.fop.fonts.FontTriplet;
  54. import org.apache.fop.fonts.base14.Courier;
  55. import org.apache.fop.fonts.base14.Helvetica;
  56. import org.apache.fop.fonts.base14.TimesRoman;
  57. import org.apache.fop.image.FopImage;
  58. import org.apache.fop.image.ImageFactory;
  59. import org.apache.fop.image.TIFFImage;
  60. import org.apache.fop.image.XMLImage;
  61. import org.apache.fop.render.AbstractPathOrientedRenderer;
  62. import org.apache.fop.render.Graphics2DAdapter;
  63. import org.apache.fop.render.RendererContext;
  64. import org.apache.fop.render.afp.extensions.AFPElementMapping;
  65. import org.apache.fop.render.afp.extensions.AFPPageSetup;
  66. import org.apache.fop.render.afp.fonts.AFPFont;
  67. import org.apache.fop.render.afp.fonts.AFPFontInfo;
  68. import org.apache.fop.render.afp.fonts.CharacterSet;
  69. import org.apache.fop.render.afp.fonts.FopCharacterSet;
  70. import org.apache.fop.render.afp.fonts.OutlineFont;
  71. import org.apache.fop.render.afp.modca.AFPConstants;
  72. import org.apache.fop.render.afp.modca.AFPDataStream;
  73. import org.apache.fop.render.afp.modca.ImageObject;
  74. import org.apache.fop.render.afp.modca.PageObject;
  75. import org.w3c.dom.Document;
  76. /**
  77. * This is an implementation of a FOP Renderer that renders areas to AFP.
  78. * <p>
  79. * A renderer is primarily designed to convert a given area tree into the output
  80. * document format. It should be able to produce pages and fill the pages with
  81. * the text and graphical content. Usually the output is sent to an output
  82. * stream. Some output formats may support extra information that is not
  83. * available from the area tree or depends on the destination of the document.
  84. * Each renderer is given an area tree to render to its output format. The area
  85. * tree is simply a representation of the pages and the placement of text and
  86. * graphical objects on those pages.
  87. * </p>
  88. * <p>
  89. * The renderer will be given each page as it is ready and an output stream to
  90. * write the data out. All pages are supplied in the order they appear in the
  91. * document. In order to save memory it is possble to render the pages out of
  92. * order. Any page that is not ready to be rendered is setup by the renderer
  93. * first so that it can reserve a space or reference for when the page is ready
  94. * to be rendered.The renderer is responsible for managing the output format and
  95. * associated data and flow.
  96. * </p>
  97. * <p>
  98. * Each renderer is totally responsible for its output format. Because font
  99. * metrics (and therefore layout) are obtained in two different ways depending
  100. * on the renderer, the renderer actually sets up the fonts being used. The font
  101. * metrics are used during the layout process to determine the size of
  102. * characters.
  103. * </p>
  104. * <p>
  105. * The render context is used by handlers. It contains information about the
  106. * current state of the renderer, such as the page, the position, and any other
  107. * miscellanous objects that are required to draw into the page.
  108. * </p>
  109. * <p>
  110. * A renderer is created by implementing the Renderer interface. However, the
  111. * AbstractRenderer does most of what is needed, including iterating through the
  112. * tree parts, so it is this that is extended. This means that this object only
  113. * need to implement the basic functionality such as text, images, and lines.
  114. * AbstractRenderer's methods can easily be overridden to handle things in a
  115. * different way or do some extra processing.
  116. * </p>
  117. * <p>
  118. * The relevent AreaTree structures that will need to be rendered are Page,
  119. * Viewport, Region, Span, Block, Line, Inline. A renderer implementation
  120. * renders each individual page, clips and aligns child areas to a viewport,
  121. * handle all types of inline area, text, image etc and draws various lines and
  122. * rectangles.
  123. * </p>
  124. *
  125. * Note: There are specific extensions that have been added to the
  126. * FO. They are specific to their location within the FO and have to be
  127. * processed accordingly (ie. at the start or end of the page).
  128. *
  129. */
  130. public class AFPRenderer extends AbstractPathOrientedRenderer {
  131. /**
  132. * AFP factor for a 240 resolution = 72000/240 = 300
  133. */
  134. private static final int DPI_CONVERSION_FACTOR_240 = 300;
  135. /**
  136. * The afp data stream object responsible for generating afp data
  137. */
  138. private AFPDataStream _afpDataStream = null;
  139. /**
  140. * The map of afp root extensions
  141. */
  142. private HashMap _rootExtensionMap = null;
  143. /**
  144. * The map of page segments
  145. */
  146. private HashMap _pageSegmentsMap = null;
  147. /**
  148. * The fonts on the current page
  149. */
  150. private HashMap _currentPageFonts = null;
  151. /**
  152. * The current color object
  153. */
  154. private Color _currentColor = null;
  155. /**
  156. * The page font number counter, used to determine the next font reference
  157. */
  158. private int _pageFontCounter = 0;
  159. /**
  160. * The current font family
  161. */
  162. private String _currentFontFamily = "";
  163. /**
  164. * The current font size
  165. */
  166. private int _currentFontSize = 0;
  167. /**
  168. * The Options to be set on the AFPRenderer
  169. */
  170. private Map _afpOptions = null;
  171. /**
  172. * The page width
  173. */
  174. private int _pageWidth = 0;
  175. /**
  176. * The page height
  177. */
  178. private int _pageHeight = 0;
  179. /**
  180. * The current page sequence id
  181. */
  182. private String _pageSequenceId = null;
  183. /**
  184. * The portrait rotation
  185. */
  186. private int _portraitRotation = 0;
  187. /**
  188. * The landscape rotation
  189. */
  190. private int _landscapeRotation = 270;
  191. /**
  192. * The line cache, avoids drawing duplicate lines in tables.
  193. */
  194. private HashSet _lineCache = null;
  195. /**
  196. * The current x position for line drawing
  197. */
  198. private float _x;
  199. /**
  200. * The current y position for line drawing
  201. */
  202. private float _y;
  203. /**
  204. * The map of saved incomplete pages
  205. */
  206. private Map _pages = null;
  207. /**
  208. * Flag to the set the output object type for images
  209. */
  210. private boolean colorImages = false;
  211. /**
  212. * Default value for image depth
  213. */
  214. private int bitsPerPixel = 8;
  215. /**
  216. * Constructor for AFPRenderer.
  217. */
  218. public AFPRenderer() {
  219. super();
  220. }
  221. /**
  222. * Set up the font info
  223. *
  224. * @param inFontInfo font info to set up
  225. */
  226. public void setupFontInfo(FontInfo inFontInfo) {
  227. this.fontInfo = inFontInfo;
  228. int num = 1;
  229. if (this.fontList != null && this.fontList.size() > 0) {
  230. for (Iterator it = this.fontList.iterator(); it.hasNext();) {
  231. AFPFontInfo afi = (AFPFontInfo)it.next();
  232. AFPFont bf = (AFPFont)afi.getAFPFont();
  233. for (Iterator it2 = afi.getFontTriplets().iterator(); it2.hasNext();) {
  234. FontTriplet ft = (FontTriplet)it2.next();
  235. this.fontInfo.addFontProperties("F" + num, ft.getName()
  236. , ft.getStyle(), ft.getWeight());
  237. this.fontInfo.addMetrics("F" + num, bf);
  238. num++;
  239. }
  240. }
  241. } else {
  242. log.warn("No AFP fonts configured - using default setup");
  243. }
  244. if (this.fontInfo.fontLookup("sans-serif", "normal", 400) == null) {
  245. CharacterSet cs = new FopCharacterSet("T1V10500", "Cp500", "CZH200 ",
  246. 1, new Helvetica());
  247. AFPFont bf = new OutlineFont("Helvetica", cs);
  248. this.fontInfo.addFontProperties("F" + num, "sans-serif", "normal", 400);
  249. this.fontInfo.addMetrics("F" + num, bf);
  250. num++;
  251. }
  252. if (this.fontInfo.fontLookup("serif", "normal", 400) == null) {
  253. CharacterSet cs = new FopCharacterSet("T1V10500", "Cp500", "CZN200 ",
  254. 1, new TimesRoman());
  255. AFPFont bf = new OutlineFont("Helvetica", cs);
  256. this.fontInfo.addFontProperties("F" + num, "serif", "normal", 400);
  257. this.fontInfo.addMetrics("F" + num, bf);
  258. num++;
  259. }
  260. if (this.fontInfo.fontLookup("monospace", "normal", 400) == null) {
  261. CharacterSet cs = new FopCharacterSet("T1V10500", "Cp500", "CZ4200 ",
  262. 1, new Courier());
  263. AFPFont bf = new OutlineFont("Helvetica", cs);
  264. this.fontInfo.addFontProperties("F" + num, "monospace", "normal", 400);
  265. this.fontInfo.addMetrics("F" + num, bf);
  266. num++;
  267. }
  268. if (this.fontInfo.fontLookup("any", "normal", 400) == null) {
  269. FontTriplet ft = this.fontInfo.fontLookup("sans-serif", "normal", 400);
  270. this.fontInfo.addFontProperties(
  271. this.fontInfo.getInternalFontKey(ft), "any", "normal", 400);
  272. }
  273. }
  274. /**
  275. * {@inheritDoc}
  276. */
  277. public void setUserAgent(FOUserAgent agent) {
  278. super.setUserAgent(agent);
  279. }
  280. /**
  281. * {@inheritDoc}
  282. */
  283. public void startRenderer(OutputStream outputStream) throws IOException {
  284. _currentPageFonts = new HashMap();
  285. _currentColor = new Color(255, 255, 255);
  286. _afpDataStream = new AFPDataStream();
  287. _afpDataStream.setPortraitRotation(_portraitRotation);
  288. _afpDataStream.setLandscapeRotation(_landscapeRotation);
  289. _afpDataStream.startDocument(outputStream);
  290. }
  291. /**
  292. * {@inheritDoc}
  293. */
  294. public void stopRenderer() throws IOException {
  295. _afpDataStream.endDocument();
  296. }
  297. /**
  298. * {@inheritDoc}
  299. */
  300. public boolean supportsOutOfOrder() {
  301. //return false;
  302. return true;
  303. }
  304. /**
  305. * Prepare a page for rendering. This is called if the renderer supports
  306. * out of order rendering. The renderer should prepare the page so that a
  307. * page further on in the set of pages can be rendered. The body of the
  308. * page should not be rendered. The page will be rendered at a later time
  309. * by the call to render page.
  310. *
  311. * {@inheritDoc}
  312. */
  313. public void preparePage(PageViewport pageViewport) {
  314. // initializeRootExtensions(page);
  315. _currentFontFamily = "";
  316. _currentFontSize = 0;
  317. _pageFontCounter = 0;
  318. _currentPageFonts.clear();
  319. _lineCache = new HashSet();
  320. Rectangle2D bounds = pageViewport.getViewArea();
  321. _pageWidth = mpts2units(bounds.getWidth());
  322. _pageHeight = mpts2units(bounds.getHeight());
  323. // renderPageGroupExtensions(page);
  324. _afpDataStream.startPage(_pageWidth, _pageHeight, 0);
  325. renderPageObjectExtensions(pageViewport);
  326. if (_pages == null) {
  327. _pages = new HashMap();
  328. }
  329. _pages.put(pageViewport, _afpDataStream.savePage());
  330. }
  331. /**
  332. * {@inheritDoc}
  333. */
  334. public void processOffDocumentItem(OffDocumentItem odi) {
  335. // TODO
  336. }
  337. /** {@inheritDoc} */
  338. public Graphics2DAdapter getGraphics2DAdapter() {
  339. return new AFPGraphics2DAdapter();
  340. }
  341. /**
  342. * {@inheritDoc}
  343. */
  344. public void startVParea(CTM ctm, Rectangle2D clippingRect) {
  345. // dummy not used
  346. }
  347. /**
  348. * {@inheritDoc}
  349. */
  350. public void endVParea() {
  351. // dummy not used
  352. }
  353. /**
  354. * Renders a region viewport. <p>
  355. *
  356. * The region may clip the area and it establishes a position from where
  357. * the region is placed.</p>
  358. *
  359. * @param port The region viewport to be rendered
  360. */
  361. public void renderRegionViewport(RegionViewport port) {
  362. if (port != null) {
  363. Rectangle2D view = port.getViewArea();
  364. // The CTM will transform coordinates relative to
  365. // this region-reference area into page coords, so
  366. // set origin for the region to 0,0.
  367. currentBPPosition = 0;
  368. currentIPPosition = 0;
  369. RegionReference regionReference = port.getRegionReference();
  370. handleRegionTraits(port);
  371. /*
  372. _afpDataStream.startOverlay(mpts2units(view.getX())
  373. , mpts2units(view.getY())
  374. , mpts2units(view.getWidth())
  375. , mpts2units(view.getHeight())
  376. , rotation);
  377. */
  378. pushViewPortPos(new ViewPortPos(view, regionReference.getCTM()));
  379. if (regionReference.getRegionClass() == FO_REGION_BODY) {
  380. renderBodyRegion((BodyRegion) regionReference);
  381. } else {
  382. renderRegion(regionReference);
  383. }
  384. /*
  385. _afpDataStream.endOverlay();
  386. */
  387. popViewPortPos();
  388. }
  389. }
  390. /**
  391. * {@inheritDoc}
  392. */
  393. protected void renderBlockViewport(BlockViewport bv, List children) {
  394. // clip and position viewport if necessary
  395. // save positions
  396. int saveIP = currentIPPosition;
  397. int saveBP = currentBPPosition;
  398. //String saveFontName = currentFontName;
  399. CTM ctm = bv.getCTM();
  400. int borderPaddingStart = bv.getBorderAndPaddingWidthStart();
  401. int borderPaddingBefore = bv.getBorderAndPaddingWidthBefore();
  402. float x, y;
  403. x = (float)(bv.getXOffset() + containingIPPosition) / 1000f;
  404. y = (float)(bv.getYOffset() + containingBPPosition) / 1000f;
  405. //This is the content-rect
  406. float width = (float)bv.getIPD() / 1000f;
  407. float height = (float)bv.getBPD() / 1000f;
  408. if (bv.getPositioning() == Block.ABSOLUTE
  409. || bv.getPositioning() == Block.FIXED) {
  410. currentIPPosition = bv.getXOffset();
  411. currentBPPosition = bv.getYOffset();
  412. //For FIXED, we need to break out of the current viewports to the
  413. //one established by the page. We save the state stack for restoration
  414. //after the block-container has been painted. See below.
  415. List breakOutList = null;
  416. if (bv.getPositioning() == Block.FIXED) {
  417. breakOutList = breakOutOfStateStack();
  418. }
  419. CTM tempctm = new CTM(containingIPPosition, containingBPPosition);
  420. ctm = tempctm.multiply(ctm);
  421. //Adjust for spaces (from margin or indirectly by start-indent etc.
  422. x += bv.getSpaceStart() / 1000f;
  423. currentIPPosition += bv.getSpaceStart();
  424. y += bv.getSpaceBefore() / 1000f;
  425. currentBPPosition += bv.getSpaceBefore();
  426. float bpwidth = (borderPaddingStart + bv.getBorderAndPaddingWidthEnd()) / 1000f;
  427. float bpheight = (borderPaddingBefore + bv.getBorderAndPaddingWidthAfter()) / 1000f;
  428. drawBackAndBorders(bv, x, y, width + bpwidth, height + bpheight);
  429. //Now adjust for border/padding
  430. currentIPPosition += borderPaddingStart;
  431. currentBPPosition += borderPaddingBefore;
  432. Rectangle2D clippingRect = null;
  433. clippingRect = new Rectangle(currentIPPosition, currentBPPosition,
  434. bv.getIPD(), bv.getBPD());
  435. // startVParea(ctm, clippingRect);
  436. pushViewPortPos(new ViewPortPos(clippingRect, ctm));
  437. currentIPPosition = 0;
  438. currentBPPosition = 0;
  439. renderBlocks(bv, children);
  440. //endVParea();
  441. popViewPortPos();
  442. if (breakOutList != null) {
  443. restoreStateStackAfterBreakOut(breakOutList);
  444. }
  445. currentIPPosition = saveIP;
  446. currentBPPosition = saveBP;
  447. } else {
  448. currentBPPosition += bv.getSpaceBefore();
  449. //borders and background in the old coordinate system
  450. handleBlockTraits(bv);
  451. //Advance to start of content area
  452. currentIPPosition += bv.getStartIndent();
  453. CTM tempctm = new CTM(containingIPPosition, currentBPPosition);
  454. ctm = tempctm.multiply(ctm);
  455. //Now adjust for border/padding
  456. currentBPPosition += borderPaddingBefore;
  457. Rectangle2D clippingRect = null;
  458. clippingRect = new Rectangle(currentIPPosition, currentBPPosition,
  459. bv.getIPD(), bv.getBPD());
  460. //startVParea(ctm, clippingRect);
  461. pushViewPortPos(new ViewPortPos(clippingRect, ctm));
  462. currentIPPosition = 0;
  463. currentBPPosition = 0;
  464. renderBlocks(bv, children);
  465. //endVParea();
  466. popViewPortPos();
  467. currentIPPosition = saveIP;
  468. currentBPPosition = saveBP;
  469. currentBPPosition += (int)(bv.getAllocBPD());
  470. }
  471. //currentFontName = saveFontName;
  472. }
  473. /**
  474. * {@inheritDoc}
  475. */
  476. public void renderPage(PageViewport pageViewport) {
  477. // initializeRootExtensions(page);
  478. _currentFontFamily = "";
  479. _currentFontSize = 0;
  480. _pageFontCounter = 0;
  481. _currentPageFonts.clear();
  482. _lineCache = new HashSet();
  483. Rectangle2D bounds = pageViewport.getViewArea();
  484. _pageWidth = mpts2units(bounds.getWidth());
  485. _pageHeight = mpts2units(bounds.getHeight());
  486. if (_pages != null && _pages.containsKey(pageViewport)) {
  487. _afpDataStream.restorePage((PageObject)_pages.remove(pageViewport));
  488. } else {
  489. // renderPageGroupExtensions(page);
  490. _afpDataStream.startPage(_pageWidth, _pageHeight, 0);
  491. renderPageObjectExtensions(pageViewport);
  492. }
  493. pushViewPortPos(new ViewPortPos());
  494. renderPageAreas(pageViewport.getPage());
  495. Iterator i = _currentPageFonts.values().iterator();
  496. while (i.hasNext()) {
  497. AFPFontAttributes afpFontAttributes = (AFPFontAttributes) i.next();
  498. _afpDataStream.createFont(
  499. afpFontAttributes.getFontReference(),
  500. afpFontAttributes.getFont(),
  501. afpFontAttributes.getPointSize());
  502. }
  503. try {
  504. _afpDataStream.endPage();
  505. } catch (IOException ioex) {
  506. // TODO What shall we do?
  507. }
  508. popViewPortPos();
  509. }
  510. /**
  511. * {@inheritDoc}
  512. */
  513. public void clip() {
  514. // TODO
  515. }
  516. /**
  517. * {@inheritDoc}
  518. */
  519. public void clipRect(float x, float y, float width, float height) {
  520. // TODO
  521. }
  522. /**
  523. * {@inheritDoc}
  524. */
  525. public void moveTo(float x, float y) {
  526. // TODO
  527. }
  528. /**
  529. * {@inheritDoc}
  530. */
  531. public void lineTo(float x, float y) {
  532. // TODO
  533. }
  534. /**
  535. * {@inheritDoc}
  536. */
  537. public void closePath() {
  538. // TODO
  539. }
  540. /**
  541. * {@inheritDoc}
  542. */
  543. public void fillRect(float x, float y, float width, float height) {
  544. /*
  545. _afpDataStream.createShading(
  546. pts2units(x),
  547. pts2units(y),
  548. pts2units(width),
  549. pts2units(height),
  550. _currentColor.getRed(),
  551. _currentColor.getGreen(),
  552. _currentColor.getBlue());
  553. */
  554. _afpDataStream.createLine(
  555. pts2units(x),
  556. pts2units(y),
  557. pts2units(x + width),
  558. pts2units(y),
  559. pts2units(height),
  560. _currentColor);
  561. }
  562. /**
  563. * {@inheritDoc}
  564. */
  565. public void drawBorderLine(float x1, float y1, float x2, float y2,
  566. boolean horz, boolean startOrBefore, int style, Color col) {
  567. float w = x2 - x1;
  568. float h = y2 - y1;
  569. if ((w < 0) || (h < 0)) {
  570. log.error("Negative extent received. Border won't be painted.");
  571. return;
  572. }
  573. switch (style) {
  574. case Constants.EN_DOUBLE:
  575. if (horz) {
  576. float h3 = h / 3;
  577. float ym1 = y1;
  578. float ym2 = ym1 + h3 + h3;
  579. _afpDataStream.createLine(
  580. pts2units(x1),
  581. pts2units(ym1),
  582. pts2units(x2),
  583. pts2units(ym1),
  584. pts2units(h3),
  585. col
  586. );
  587. _afpDataStream.createLine(
  588. pts2units(x1),
  589. pts2units(ym2),
  590. pts2units(x2),
  591. pts2units(ym2),
  592. pts2units(h3),
  593. col
  594. );
  595. } else {
  596. float w3 = w / 3;
  597. float xm1 = x1;
  598. float xm2 = xm1 + w3 + w3;
  599. _afpDataStream.createLine(
  600. pts2units(xm1),
  601. pts2units(y1),
  602. pts2units(xm1),
  603. pts2units(y2),
  604. pts2units(w3),
  605. col
  606. );
  607. _afpDataStream.createLine(
  608. pts2units(xm2),
  609. pts2units(y1),
  610. pts2units(xm2),
  611. pts2units(y2),
  612. pts2units(w3),
  613. col
  614. );
  615. }
  616. break;
  617. case Constants.EN_DASHED:
  618. if (horz) {
  619. float w2 = 2 * h;
  620. while (x1 + w2 < x2) {
  621. _afpDataStream.createLine(
  622. pts2units(x1),
  623. pts2units(y1),
  624. pts2units(x1 + w2),
  625. pts2units(y1),
  626. pts2units(h),
  627. col
  628. );
  629. x1 += 2 * w2;
  630. }
  631. } else {
  632. float h2 = 2 * w;
  633. while (y1 + h2 < y2) {
  634. _afpDataStream.createLine(
  635. pts2units(x1),
  636. pts2units(y1),
  637. pts2units(x1),
  638. pts2units(y1 + h2),
  639. pts2units(w),
  640. col
  641. );
  642. y1 += 2 * h2;
  643. }
  644. }
  645. break;
  646. case Constants.EN_DOTTED:
  647. if (horz) {
  648. while (x1 + h < x2) {
  649. _afpDataStream.createLine(
  650. pts2units(x1),
  651. pts2units(y1),
  652. pts2units(x1 + h),
  653. pts2units(y1),
  654. pts2units(h),
  655. col
  656. );
  657. x1 += 2 * h;
  658. }
  659. } else {
  660. while (y1 + w < y2) {
  661. _afpDataStream.createLine(
  662. pts2units(x1),
  663. pts2units(y1),
  664. pts2units(x1),
  665. pts2units(y1 + w),
  666. pts2units(w),
  667. col
  668. );
  669. y1 += 2 * w;
  670. }
  671. }
  672. break;
  673. case Constants.EN_GROOVE:
  674. case Constants.EN_RIDGE:
  675. {
  676. float colFactor = (style == EN_GROOVE ? 0.4f : -0.4f);
  677. if (horz) {
  678. Color uppercol = lightenColor(col, -colFactor);
  679. Color lowercol = lightenColor(col, colFactor);
  680. float h3 = h / 3;
  681. float ym1 = y1;
  682. _afpDataStream.createLine(
  683. pts2units(x1),
  684. pts2units(ym1),
  685. pts2units(x2),
  686. pts2units(ym1),
  687. pts2units(h3),
  688. uppercol
  689. );
  690. _afpDataStream.createLine(
  691. pts2units(x1),
  692. pts2units(ym1 + h3),
  693. pts2units(x2),
  694. pts2units(ym1 + h3),
  695. pts2units(h3),
  696. col
  697. );
  698. _afpDataStream.createLine(
  699. pts2units(x1),
  700. pts2units(ym1 + h3 + h3),
  701. pts2units(x2),
  702. pts2units(ym1 + h3 + h3),
  703. pts2units(h3),
  704. lowercol
  705. );
  706. } else {
  707. Color leftcol = lightenColor(col, -colFactor);
  708. Color rightcol = lightenColor(col, colFactor);
  709. float w3 = w / 3;
  710. float xm1 = x1 + (w3 / 2);
  711. _afpDataStream.createLine(
  712. pts2units(xm1),
  713. pts2units(y1),
  714. pts2units(xm1),
  715. pts2units(y2),
  716. pts2units(w3),
  717. leftcol
  718. );
  719. _afpDataStream.createLine(
  720. pts2units(xm1 + w3),
  721. pts2units(y1),
  722. pts2units(xm1 + w3),
  723. pts2units(y2),
  724. pts2units(w3),
  725. col
  726. );
  727. _afpDataStream.createLine(
  728. pts2units(xm1 + w3 + w3),
  729. pts2units(y1),
  730. pts2units(xm1 + w3 + w3),
  731. pts2units(y2),
  732. pts2units(w3),
  733. rightcol
  734. );
  735. }
  736. break;
  737. }
  738. case Constants.EN_HIDDEN:
  739. break;
  740. case Constants.EN_INSET:
  741. case Constants.EN_OUTSET:
  742. default:
  743. _afpDataStream.createLine(
  744. pts2units(x1),
  745. pts2units(y1),
  746. pts2units(horz ? x2 : x1),
  747. pts2units(horz ? y1 : y2),
  748. pts2units(Math.abs(horz ? (y2 - y1) : (x2 - x1))),
  749. col
  750. );
  751. }
  752. }
  753. /**
  754. * {@inheritDoc}
  755. */
  756. protected RendererContext createRendererContext(int x, int y, int width, int height, Map foreignAttributes) {
  757. RendererContext context;
  758. context = super.createRendererContext(x, y, width, height, foreignAttributes);
  759. context.setProperty(AFPRendererContextConstants.AFP_GRAYSCALE,
  760. new Boolean(!this.colorImages));
  761. return context;
  762. }
  763. /**
  764. * {@inheritDoc}
  765. */
  766. public void drawImage(String url, Rectangle2D pos, Map foreignAttributes) {
  767. String name = null;
  768. if (_pageSegmentsMap != null) {
  769. name = (String)_pageSegmentsMap.get(url);
  770. }
  771. if (name != null) {
  772. int x = mpts2units(pos.getX() + currentIPPosition);
  773. int y = mpts2units(pos.getY() + currentBPPosition);
  774. _afpDataStream.createIncludePageSegment(name, x, y);
  775. } else {
  776. url = ImageFactory.getURL(url);
  777. ImageFactory fact = userAgent.getFactory().getImageFactory();
  778. FopImage fopimage = fact.getImage(url, userAgent);
  779. if (fopimage == null) {
  780. return;
  781. }
  782. if (!fopimage.load(FopImage.DIMENSIONS)) {
  783. return;
  784. }
  785. String mime = fopimage.getMimeType();
  786. if ("text/xml".equals(mime) || MimeConstants.MIME_SVG.equals(mime)) {
  787. if (!fopimage.load(FopImage.ORIGINAL_DATA)) {
  788. return;
  789. }
  790. Document doc = ((XMLImage) fopimage).getDocument();
  791. String ns = ((XMLImage) fopimage).getNameSpace();
  792. renderDocument(doc, ns, pos, foreignAttributes);
  793. } else if (MimeConstants.MIME_EPS.equals(mime)) {
  794. log.warn("EPS images are not supported by this renderer");
  795. /*
  796. } else if (MimeConstants.MIME_JPEG.equals(mime)) {
  797. if (!fopimage.load(FopImage.ORIGINAL_DATA)) {
  798. return;
  799. }
  800. fact.releaseImage(url, userAgent);
  801. int x = mpts2units(pos.getX() + currentIPPosition);
  802. int y = mpts2units(pos.getY() + currentBPPosition);
  803. int w = mpts2units(pos.getWidth());
  804. int h = mpts2units(pos.getHeight());
  805. ImageObject io = _afpDataStream.getImageObject();
  806. io.setImageViewport(x, y, w, h);
  807. io.setImageParameters(
  808. (int)(fopimage.getHorizontalResolution() * 10),
  809. (int)(fopimage.getVerticalResolution() * 10),
  810. fopimage.getWidth(),
  811. fopimage.getHeight()
  812. );
  813. io.setImageIDESize((byte)fopimage.getBitsPerPixel());
  814. io.setImageEncoding((byte)0x83);
  815. io.setImageData(fopimage.getRessourceBytes());
  816. */
  817. } else if (MimeConstants.MIME_TIFF.equals(mime)
  818. && fopimage instanceof TIFFImage) {
  819. TIFFImage tiffImage = (TIFFImage) fopimage;
  820. int x = mpts2units(pos.getX() + currentIPPosition);
  821. int y = mpts2units(pos.getY() + currentBPPosition);
  822. int w = mpts2units(pos.getWidth());
  823. int h = mpts2units(pos.getHeight());
  824. ImageObject io = _afpDataStream.getImageObject(x, y, w, h);
  825. io.setImageParameters(
  826. (int)(fopimage.getHorizontalResolution() * 10),
  827. (int)(fopimage.getVerticalResolution() * 10),
  828. fopimage.getWidth(),
  829. fopimage.getHeight()
  830. );
  831. if (tiffImage.getStripCount() == 1) {
  832. int comp = tiffImage.getCompression();
  833. if (comp == 3) {
  834. if (!fopimage.load(FopImage.ORIGINAL_DATA)) {
  835. return;
  836. }
  837. io.setImageEncoding((byte)0x81);
  838. io.setImageData(fopimage.getRessourceBytes());
  839. } else if (comp == 4) {
  840. if (!fopimage.load(FopImage.ORIGINAL_DATA)) {
  841. return;
  842. }
  843. io.setImageEncoding((byte)0x82);
  844. io.setImageData(fopimage.getRessourceBytes());
  845. } else {
  846. if (!fopimage.load(FopImage.BITMAP)) {
  847. return;
  848. }
  849. convertToGrayScaleImage(io, fopimage.getBitmaps(),
  850. fopimage.getWidth(), fopimage.getHeight());
  851. }
  852. } else {
  853. if (!fopimage.load(FopImage.BITMAP)) {
  854. return;
  855. }
  856. convertToGrayScaleImage(io, fopimage.getBitmaps(),
  857. fopimage.getWidth(), fopimage.getHeight());
  858. }
  859. } else {
  860. if (!fopimage.load(FopImage.BITMAP)) {
  861. return;
  862. }
  863. fact.releaseImage(url, userAgent);
  864. int x = mpts2units(pos.getX() + currentIPPosition);
  865. int y = mpts2units(pos.getY() + currentBPPosition);
  866. int w = mpts2units(pos.getWidth());
  867. int h = mpts2units(pos.getHeight());
  868. ImageObject io = _afpDataStream.getImageObject(x, y, w, h);
  869. io.setImageParameters(
  870. (int)(fopimage.getHorizontalResolution() * 10),
  871. (int)(fopimage.getVerticalResolution() * 10),
  872. fopimage.getWidth(),
  873. fopimage.getHeight()
  874. );
  875. if (colorImages) {
  876. io.setImageIDESize((byte)24);
  877. io.setImageData(fopimage.getBitmaps());
  878. } else {
  879. convertToGrayScaleImage(io, fopimage.getBitmaps(),
  880. fopimage.getWidth(), fopimage.getHeight());
  881. }
  882. }
  883. }
  884. }
  885. /**
  886. * Writes a BufferedImage to an OutputStream as raw sRGB bitmaps.
  887. * @param img the BufferedImage
  888. * @param out the OutputStream
  889. * @throws IOException In case of an I/O error.
  890. */
  891. public static void writeImage(BufferedImage img, OutputStream out) throws IOException {
  892. int w = img.getWidth();
  893. int h = img.getHeight();
  894. int[] tmpMap = img.getRGB(0, 0, w, h, null, 0, w);
  895. for (int i = 0; i < h; i++) {
  896. for (int j = 0; j < w; j++) {
  897. int p = tmpMap[i * w + j];
  898. int r = (p >> 16) & 0xFF;
  899. int g = (p >> 8) & 0xFF;
  900. int b = (p) & 0xFF;
  901. out.write((byte)(r & 0xFF));
  902. out.write((byte)(g & 0xFF));
  903. out.write((byte)(b & 0xFF));
  904. }
  905. }
  906. }
  907. /**
  908. * Draws a BufferedImage to AFP.
  909. * @param bi the BufferedImage
  910. * @param resolution the resolution of the BufferedImage
  911. * @param x the x coordinate (in mpt)
  912. * @param y the y coordinate (in mpt)
  913. * @param w the width of the viewport (in mpt)
  914. * @param h the height of the viewport (in mpt)
  915. */
  916. public void drawBufferedImage(BufferedImage bi, int resolution, int x, int y, int w, int h) {
  917. int afpx = mpts2units(x);
  918. int afpy = mpts2units(y);
  919. int afpw = mpts2units(w);
  920. int afph = mpts2units(h);
  921. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  922. try {
  923. //Serialize image
  924. writeImage(bi, baout);
  925. byte[] buf = baout.toByteArray();
  926. //Generate image
  927. ImageObject io = _afpDataStream.getImageObject(afpx, afpy, afpw, afph);
  928. io.setImageParameters(
  929. resolution, resolution,
  930. bi.getWidth(),
  931. bi.getHeight()
  932. );
  933. if (colorImages) {
  934. io.setImageIDESize((byte)24);
  935. io.setImageData(buf);
  936. } else {
  937. //TODO Teach it how to handle grayscale BufferedImages directly
  938. //because this is pretty inefficient
  939. convertToGrayScaleImage(io, buf, bi.getWidth(), bi.getHeight());
  940. }
  941. } catch (IOException ioe) {
  942. log.error("Error while serializing bitmap: " + ioe.getMessage(), ioe);
  943. }
  944. }
  945. /**
  946. * Establishes a new foreground or fill color.
  947. * {@inheritDoc}
  948. */
  949. public void updateColor(Color col, boolean fill) {
  950. if (fill) {
  951. _currentColor = col;
  952. }
  953. }
  954. /**
  955. * Restores the state stack after a break out.
  956. * @param breakOutList the state stack to restore.
  957. */
  958. public void restoreStateStackAfterBreakOut(List breakOutList) {
  959. }
  960. /**
  961. * Breaks out of the state stack to handle fixed block-containers.
  962. * @return the saved state stack to recreate later
  963. */
  964. public List breakOutOfStateStack() {
  965. return null;
  966. }
  967. /** Saves the graphics state of the rendering engine. */
  968. public void saveGraphicsState() {
  969. }
  970. /** Restores the last graphics state of the rendering engine. */
  971. public void restoreGraphicsState() {
  972. }
  973. /** Indicates the beginning of a text object. */
  974. public void beginTextObject() {
  975. }
  976. /** Indicates the end of a text object. */
  977. public void endTextObject() {
  978. }
  979. /**
  980. * {@inheritDoc}
  981. */
  982. public void renderImage(Image image, Rectangle2D pos) {
  983. String url = image.getURL();
  984. drawImage(url, pos);
  985. }
  986. /**
  987. * {@inheritDoc}
  988. */
  989. public void renderText(TextArea text) {
  990. renderInlineAreaBackAndBorders(text);
  991. String name = getInternalFontNameForArea(text);
  992. _currentFontSize = ((Integer) text.getTrait(Trait.FONT_SIZE)).intValue();
  993. AFPFont tf = (AFPFont) fontInfo.getFonts().get(name);
  994. Color col = (Color) text.getTrait(Trait.COLOR);
  995. int vsci = mpts2units(tf.getWidth(' ', _currentFontSize) / 1000
  996. + text.getTextWordSpaceAdjust()
  997. + text.getTextLetterSpaceAdjust());
  998. // word.getOffset() = only height of text itself
  999. // currentBlockIPPosition: 0 for beginning of line; nonzero
  1000. // where previous line area failed to take up entire allocated space
  1001. int rx = currentIPPosition + text.getBorderAndPaddingWidthStart();
  1002. int bl = currentBPPosition + text.getOffset() + text.getBaselineOffset();
  1003. // Set letterSpacing
  1004. //float ls = fs.getLetterSpacing() / this.currentFontSize;
  1005. String worddata = text.getText();
  1006. // Create an AFPFontAttributes object from the current font details
  1007. AFPFontAttributes afpFontAttributes =
  1008. new AFPFontAttributes(name, tf, _currentFontSize);
  1009. if (!_currentPageFonts.containsKey(afpFontAttributes.getFontKey())) {
  1010. // Font not found on current page, so add the new one
  1011. _pageFontCounter++;
  1012. afpFontAttributes.setFontReference(_pageFontCounter);
  1013. _currentPageFonts.put(
  1014. afpFontAttributes.getFontKey(),
  1015. afpFontAttributes);
  1016. } else {
  1017. // Use the previously stored font attributes
  1018. afpFontAttributes =
  1019. (AFPFontAttributes) _currentPageFonts.get(
  1020. afpFontAttributes.getFontKey());
  1021. }
  1022. // Try and get the encoding to use for the font
  1023. String encoding = null;
  1024. try {
  1025. encoding = tf.getCharacterSet(_currentFontSize).getEncoding();
  1026. } catch (Throwable ex) {
  1027. encoding = AFPConstants.EBCIDIC_ENCODING;
  1028. log.warn(
  1029. "renderText():: Error getting encoding for font "
  1030. + " - using default encoding "
  1031. + encoding);
  1032. }
  1033. try {
  1034. _afpDataStream.createText(
  1035. afpFontAttributes.getFontReference(),
  1036. mpts2units(rx),
  1037. mpts2units(bl),
  1038. col,
  1039. vsci,
  1040. mpts2units(text.getTextLetterSpaceAdjust()),
  1041. worddata.getBytes(encoding));
  1042. } catch (UnsupportedEncodingException usee) {
  1043. log.error(
  1044. "renderText:: Font "
  1045. + afpFontAttributes.getFontKey()
  1046. + " caused UnsupportedEncodingException");
  1047. }
  1048. super.renderText(text);
  1049. renderTextDecoration(tf, _currentFontSize, text, bl, rx);
  1050. }
  1051. /**
  1052. * {@inheritDoc}
  1053. */
  1054. public void renderWord(WordArea word) {
  1055. String name = getInternalFontNameForArea(word.getParentArea());
  1056. int size = ((Integer) word.getParentArea().getTrait(Trait.FONT_SIZE)).intValue();
  1057. AFPFont tf = (AFPFont) fontInfo.getFonts().get(name);
  1058. String s = word.getWord();
  1059. FontMetrics metrics = fontInfo.getMetricsFor(name);
  1060. super.renderWord(word);
  1061. }
  1062. /**
  1063. * {@inheritDoc}
  1064. */
  1065. public void renderSpace(SpaceArea space) {
  1066. String name = getInternalFontNameForArea(space.getParentArea());
  1067. int size = ((Integer) space.getParentArea().getTrait(Trait.FONT_SIZE)).intValue();
  1068. AFPFont tf = (AFPFont) fontInfo.getFonts().get(name);
  1069. String s = space.getSpace();
  1070. FontMetrics metrics = fontInfo.getMetricsFor(name);
  1071. super.renderSpace(space);
  1072. }
  1073. /**
  1074. * Render leader area.
  1075. * This renders a leader area which is an area with a rule.
  1076. * @param area the leader area to render
  1077. */
  1078. public void renderLeader(Leader area) {
  1079. renderInlineAreaBackAndBorders(area);
  1080. int style = area.getRuleStyle();
  1081. float startx = (currentIPPosition + area.getBorderAndPaddingWidthStart()) / 1000f;
  1082. float starty = (currentBPPosition + area.getOffset()) / 1000f;
  1083. float endx = (currentIPPosition + area.getBorderAndPaddingWidthStart()
  1084. + area.getIPD()) / 1000f;
  1085. float ruleThickness = area.getRuleThickness() / 1000f;
  1086. Color col = (Color)area.getTrait(Trait.COLOR);
  1087. switch (style) {
  1088. case EN_SOLID:
  1089. case EN_DASHED:
  1090. case EN_DOUBLE:
  1091. case EN_DOTTED:
  1092. case EN_GROOVE:
  1093. case EN_RIDGE:
  1094. drawBorderLine(startx, starty, endx, starty + ruleThickness,
  1095. true, true, style, col);
  1096. break;
  1097. default:
  1098. throw new UnsupportedOperationException("rule style not supported");
  1099. }
  1100. super.renderLeader(area);
  1101. }
  1102. /**
  1103. * Sets the AFPRenderer options
  1104. * @param options the <code>Map</code> containing the options
  1105. */
  1106. public void setOptions(Map options) {
  1107. _afpOptions = options;
  1108. }
  1109. /**
  1110. * Determines the orientation from the string representation, this method
  1111. * guarantees to return a value of either 0, 90, 180 or 270.
  1112. *
  1113. * @return the orientation
  1114. */
  1115. private int getOrientation(String orientationString) {
  1116. int orientation = 0;
  1117. if (orientationString != null && orientationString.length() > 0) {
  1118. try {
  1119. orientation = Integer.parseInt(orientationString);
  1120. } catch (NumberFormatException nfe) {
  1121. log.error(
  1122. "Cannot use orientation of "
  1123. + orientation
  1124. + " defaulting to zero.");
  1125. orientation = 0;
  1126. }
  1127. } else {
  1128. orientation = 0;
  1129. }
  1130. switch (orientation) {
  1131. case 0 :
  1132. break;
  1133. case 90 :
  1134. break;
  1135. case 180 :
  1136. break;
  1137. case 270 :
  1138. break;
  1139. default :
  1140. log.error(
  1141. "Cannot use orientation of "
  1142. + orientation
  1143. + " defaulting to zero.");
  1144. orientation = 0;
  1145. break;
  1146. }
  1147. return orientation;
  1148. }
  1149. /**
  1150. * Sets the rotation to be used for portrait pages, valid values are 0
  1151. * (default), 90, 180, 270.
  1152. *
  1153. * @param rotation
  1154. * The rotation in degrees.
  1155. */
  1156. public void setPortraitRotation(int rotation) {
  1157. if (rotation == 0
  1158. || rotation == 90
  1159. || rotation == 180
  1160. || rotation == 270) {
  1161. _portraitRotation = rotation;
  1162. } else {
  1163. throw new IllegalArgumentException("The portrait rotation must be one"
  1164. + " of the values 0, 90, 180, 270");
  1165. }
  1166. }
  1167. /**
  1168. * Sets the rotation to be used for landsacpe pages, valid values are 0, 90,
  1169. * 180, 270 (default).
  1170. *
  1171. * @param rotation
  1172. * The rotation in degrees.
  1173. */
  1174. public void setLandscapeRotation(int rotation) {
  1175. if (rotation == 0
  1176. || rotation == 90
  1177. || rotation == 180
  1178. || rotation == 270) {
  1179. _landscapeRotation = rotation;
  1180. } else {
  1181. throw new IllegalArgumentException("The landscape rotation must be one"
  1182. + " of the values 0, 90, 180, 270");
  1183. }
  1184. }
  1185. /**
  1186. * Get the MIME type of the renderer.
  1187. *
  1188. * @return The MIME type of the renderer
  1189. */
  1190. public String getMimeType() {
  1191. return MimeConstants.MIME_AFP;
  1192. }
  1193. /**
  1194. * Method to render the page extension.
  1195. * <p>
  1196. *
  1197. * @param pageViewport
  1198. * the page object
  1199. */
  1200. private void renderPageObjectExtensions(PageViewport pageViewport) {
  1201. _pageSegmentsMap = null;
  1202. if (pageViewport.getExtensionAttachments() != null
  1203. && pageViewport.getExtensionAttachments().size() > 0) {
  1204. //Extract all AFPPageSetup instances from the attachment list on the s-p-m
  1205. Iterator i = pageViewport.getExtensionAttachments().iterator();
  1206. while (i.hasNext()) {
  1207. ExtensionAttachment attachment = (ExtensionAttachment)i.next();
  1208. if (AFPPageSetup.CATEGORY.equals(attachment.getCategory())) {
  1209. if (attachment instanceof AFPPageSetup) {
  1210. AFPPageSetup aps = (AFPPageSetup)attachment;
  1211. if (log.isDebugEnabled()) {
  1212. log.debug(aps);
  1213. }
  1214. String element = aps.getElementName();
  1215. if (AFPElementMapping.INCLUDE_PAGE_OVERLAY.equals(element)) {
  1216. String overlay = aps.getName();
  1217. if (overlay != null) {
  1218. _afpDataStream.createIncludePageOverlay(overlay);
  1219. }
  1220. } else if (AFPElementMapping.INCLUDE_PAGE_SEGMENT.equals(element)) {
  1221. String name = aps.getName();
  1222. String source = aps.getValue();
  1223. if (_pageSegmentsMap == null) {
  1224. _pageSegmentsMap = new HashMap();
  1225. }
  1226. _pageSegmentsMap.put(source, name);
  1227. } else if (AFPElementMapping.TAG_LOGICAL_ELEMENT.equals(element)) {
  1228. String name = aps.getName();
  1229. String value = aps.getValue();
  1230. if (_pageSegmentsMap == null) {
  1231. _pageSegmentsMap = new HashMap();
  1232. }
  1233. _afpDataStream.createTagLogicalElement(name, value);
  1234. } else if (AFPElementMapping.NO_OPERATION.equals(element)) {
  1235. String content = aps.getContent();
  1236. if (content != null) {
  1237. _afpDataStream.createNoOperation(content);
  1238. }
  1239. }
  1240. }
  1241. }
  1242. }
  1243. }
  1244. }
  1245. /**
  1246. * Converts FOP mpt measurement to afp measurement units
  1247. * @param mpt the millipoints value
  1248. */
  1249. private int mpts2units(int mpt) {
  1250. return mpts2units((double) mpt);
  1251. }
  1252. /**
  1253. * Converts FOP pt measurement to afp measurement units
  1254. * @param mpt the millipoints value
  1255. */
  1256. private int pts2units(float mpt) {
  1257. return mpts2units(mpt * 1000d);
  1258. }
  1259. /**
  1260. * Converts FOP mpt measurement to afp measurement units
  1261. * @param mpt the millipoints value
  1262. */
  1263. private int mpts2units(double mpt) {
  1264. return (int)Math.round(mpt / DPI_CONVERSION_FACTOR_240);
  1265. }
  1266. /**
  1267. * Converts a byte array containing 24 bit RGB image data to a grayscale image.
  1268. * @param io the target image object
  1269. * @param raw the buffer containing the RGB image data
  1270. * @param width the width of the image in pixels
  1271. * @param height the height of the image in pixels
  1272. */
  1273. private void convertToGrayScaleImage(ImageObject io, byte[] raw, int width, int height) {
  1274. int pixelsPerByte = 8 / bitsPerPixel;
  1275. int bytewidth = (width / pixelsPerByte);
  1276. if ((width % pixelsPerByte) != 0) {
  1277. bytewidth++;
  1278. }
  1279. byte[] bw = new byte[height * bytewidth];
  1280. byte ib;
  1281. for (int y = 0; y < height; y++) {
  1282. ib = 0;
  1283. int i = 3 * y * width;
  1284. for (int x = 0; x < width; x++, i += 3) {
  1285. // see http://www.jguru.com/faq/view.jsp?EID=221919
  1286. double greyVal = 0.212671d * ((int) raw[i] & 0xff)
  1287. + 0.715160d * ((int) raw[i + 1] & 0xff)
  1288. + 0.072169d * ((int) raw[i + 2] & 0xff);
  1289. switch (bitsPerPixel) {
  1290. case 1:
  1291. if (greyVal < 128) {
  1292. ib |= (byte)(1 << (7 - (x % 8)));
  1293. }
  1294. break;
  1295. case 4:
  1296. greyVal /= 16;
  1297. ib |= (byte)((byte)greyVal << ((1 - (x % 2)) * 4));
  1298. break;
  1299. case 8:
  1300. ib = (byte)greyVal;
  1301. break;
  1302. default:
  1303. throw new UnsupportedOperationException(
  1304. "Unsupported bits per pixel: " + bitsPerPixel);
  1305. }
  1306. if ((x % pixelsPerByte) == (pixelsPerByte - 1) || ((x + 1) == width)) {
  1307. bw[(y * bytewidth) + (x / pixelsPerByte)] = ib;
  1308. ib = 0;
  1309. }
  1310. }
  1311. }
  1312. io.setImageIDESize((byte)bitsPerPixel);
  1313. io.setImageData(bw);
  1314. }
  1315. private class ViewPortPos {
  1316. int x = 0;
  1317. int y = 0;
  1318. int rot = 0;
  1319. ViewPortPos() {
  1320. }
  1321. ViewPortPos(Rectangle2D view, CTM ctm) {
  1322. ViewPortPos currentVP = (ViewPortPos)viewPortPositions.get(viewPortPositions.size() - 1);
  1323. int xOrigin;
  1324. int yOrigin;
  1325. int width;
  1326. int height;
  1327. switch (currentVP.rot) {
  1328. case 90:
  1329. width = mpts2units(view.getHeight());
  1330. height = mpts2units(view.getWidth());
  1331. xOrigin = _pageWidth - width - mpts2units(view.getY()) - currentVP.y;
  1332. yOrigin = mpts2units(view.getX()) + currentVP.x;
  1333. break;
  1334. case 180:
  1335. width = mpts2units(view.getWidth());
  1336. height = mpts2units(view.getHeight());
  1337. xOrigin = _pageWidth - width - mpts2units(view.getX()) - currentVP.x;
  1338. yOrigin = _pageHeight - height - mpts2units(view.getY()) - currentVP.y;
  1339. break;
  1340. case 270:
  1341. width = mpts2units(view.getHeight());
  1342. height = mpts2units(view.getWidth());
  1343. xOrigin = mpts2units(view.getY()) + currentVP.y;
  1344. yOrigin = _pageHeight - height - mpts2units(view.getX()) - currentVP.x;
  1345. break;
  1346. default:
  1347. xOrigin = mpts2units(view.getX()) + currentVP.x;
  1348. yOrigin = mpts2units(view.getY()) + currentVP.y;
  1349. width = mpts2units(view.getWidth());
  1350. height = mpts2units(view.getHeight());
  1351. break;
  1352. }
  1353. this.rot = currentVP.rot;
  1354. double ctmf[] = ctm.toArray();
  1355. if (ctmf[0] == 0.0d && ctmf[1] == -1.0d && ctmf[2] == 1.0d && ctmf[3] == 0.d) {
  1356. this.rot += 270;
  1357. } else if (ctmf[0] == -1.0d && ctmf[1] == 0.0d && ctmf[2] == 0.0d && ctmf[3] == -1.0d) {
  1358. this.rot += 180;
  1359. } else if (ctmf[0] == 0.0d && ctmf[1] == 1.0d && ctmf[2] == -1.0d && ctmf[3] == 0.0d) {
  1360. this.rot += 90;
  1361. }
  1362. this.rot %= 360;
  1363. switch (this.rot) {
  1364. /*
  1365. case 0:
  1366. this.x = mpts2units(view.getX()) + x;
  1367. this.y = mpts2units(view.getY()) + y;
  1368. break;
  1369. case 90:
  1370. this.x = mpts2units(view.getY()) + y;
  1371. this.y = _pageWidth - mpts2units(view.getX() + view.getWidth()) - x;
  1372. break;
  1373. case 180:
  1374. this.x = _pageWidth - mpts2units(view.getX() + view.getWidth()) - x;
  1375. this.y = _pageHeight - mpts2units(view.getY() + view.getHeight()) - y;
  1376. break;
  1377. case 270:
  1378. this.x = _pageHeight - mpts2units(view.getY() + view.getHeight()) - y;
  1379. this.y = mpts2units(view.getX()) + x;
  1380. break;
  1381. */
  1382. case 0:
  1383. this.x = xOrigin;
  1384. this.y = yOrigin;
  1385. break;
  1386. case 90:
  1387. this.x = yOrigin;
  1388. this.y = _pageWidth - width - xOrigin;
  1389. break;
  1390. case 180:
  1391. this.x = _pageWidth - width - xOrigin;
  1392. this.y = _pageHeight - height - yOrigin;
  1393. break;
  1394. case 270:
  1395. this.x = _pageHeight - height - yOrigin;
  1396. this.y = xOrigin;
  1397. break;
  1398. }
  1399. }
  1400. public String toString() {
  1401. return "x:" + x + " y:" + y + " rot:" + rot;
  1402. }
  1403. }
  1404. private List viewPortPositions = new ArrayList();
  1405. private void pushViewPortPos(ViewPortPos vpp) {
  1406. viewPortPositions.add(vpp);
  1407. _afpDataStream.setOffsets(vpp.x, vpp.y, vpp.rot);
  1408. }
  1409. private void popViewPortPos() {
  1410. viewPortPositions.remove(viewPortPositions.size() - 1);
  1411. if (viewPortPositions.size() > 0) {
  1412. ViewPortPos vpp = (ViewPortPos)viewPortPositions.get(viewPortPositions.size() - 1);
  1413. _afpDataStream.setOffsets(vpp.x, vpp.y, vpp.rot);
  1414. }
  1415. }
  1416. public void setBitsPerPixel(int bitsPerPixel) {
  1417. this.bitsPerPixel = bitsPerPixel;
  1418. switch (bitsPerPixel) {
  1419. case 1:
  1420. case 4:
  1421. case 8:
  1422. break;
  1423. default:
  1424. log.warn("Invalid bits_per_pixel value, must be 1, 4 or 8.");
  1425. bitsPerPixel = 8;
  1426. break;
  1427. }
  1428. }
  1429. public void setColorImages(boolean colorImages) {
  1430. this.colorImages = colorImages;
  1431. }
  1432. }