Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AFPRenderer.java 55KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  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.Leader;
  45. import org.apache.fop.area.inline.Image;
  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.AFPFontInfo;
  67. import org.apache.fop.render.afp.fonts.AFPFont;
  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 ", 1, new Helvetica());
  246. AFPFont bf = new OutlineFont("Helvetica", cs);
  247. this.fontInfo.addFontProperties("F" + num, "sans-serif", "normal", 400);
  248. this.fontInfo.addMetrics("F" + num, bf);
  249. num++;
  250. }
  251. if (this.fontInfo.fontLookup("serif", "normal", 400) == null) {
  252. CharacterSet cs = new FopCharacterSet("T1V10500", "Cp500", "CZN200 ", 1, new TimesRoman());
  253. AFPFont bf = new OutlineFont("Helvetica", cs);
  254. this.fontInfo.addFontProperties("F" + num, "serif", "normal", 400);
  255. this.fontInfo.addMetrics("F" + num, bf);
  256. num++;
  257. }
  258. if (this.fontInfo.fontLookup("monospace", "normal", 400) == null) {
  259. CharacterSet cs = new FopCharacterSet("T1V10500", "Cp500", "CZ4200 ", 1, new Courier());
  260. AFPFont bf = new OutlineFont("Helvetica", cs);
  261. this.fontInfo.addFontProperties("F" + num, "monospace", "normal", 400);
  262. this.fontInfo.addMetrics("F" + num, bf);
  263. num++;
  264. }
  265. if (this.fontInfo.fontLookup("any", "normal", 400) == null) {
  266. FontTriplet ft = this.fontInfo.fontLookup("sans-serif", "normal", 400);
  267. this.fontInfo.addFontProperties(this.fontInfo.getInternalFontKey(ft), "any", "normal", 400);
  268. }
  269. }
  270. /**
  271. * @see org.apache.fop.render.Renderer#setUserAgent(FOUserAgent)
  272. */
  273. public void setUserAgent(FOUserAgent agent) {
  274. super.setUserAgent(agent);
  275. }
  276. /**
  277. * @see org.apache.fop.render.Renderer#startRenderer(java.io.OutputStream)
  278. */
  279. public void startRenderer(OutputStream outputStream) throws IOException {
  280. _currentPageFonts = new HashMap();
  281. _currentColor = new Color(255, 255, 255);
  282. _afpDataStream = new AFPDataStream();
  283. _afpDataStream.setPortraitRotation(_portraitRotation);
  284. _afpDataStream.setLandscapeRotation(_landscapeRotation);
  285. _afpDataStream.startDocument(outputStream);
  286. }
  287. /**
  288. * @see org.apache.fop.render.Renderer#stopRenderer()
  289. */
  290. public void stopRenderer() throws IOException {
  291. _afpDataStream.endDocument();
  292. }
  293. /**
  294. * @see org.apache.fop.render.Renderer#supportsOutOfOrder()
  295. */
  296. public boolean supportsOutOfOrder() {
  297. //return false;
  298. return true;
  299. }
  300. /**
  301. * Prepare a page for rendering. This is called if the renderer supports
  302. * out of order rendering. The renderer should prepare the page so that a
  303. * page further on in the set of pages can be rendered. The body of the
  304. * page should not be rendered. The page will be rendered at a later time
  305. * by the call to render page.
  306. *
  307. * @see org.apache.fop.render.Renderer#preparePage(PageViewport)
  308. */
  309. public void preparePage(PageViewport page) {
  310. // initializeRootExtensions(page);
  311. _currentFontFamily = "";
  312. _currentFontSize = 0;
  313. _pageFontCounter = 0;
  314. _currentPageFonts.clear();
  315. _lineCache = new HashSet();
  316. Rectangle2D bounds = page.getViewArea();
  317. _pageWidth = mpts2units(bounds.getWidth());
  318. _pageHeight = mpts2units(bounds.getHeight());
  319. // renderPageGroupExtensions(page);
  320. _afpDataStream.startPage(_pageWidth, _pageHeight, 0);
  321. renderPageObjectExtensions(page);
  322. if (_pages == null) {
  323. _pages = new HashMap();
  324. }
  325. _pages.put(page, _afpDataStream.savePage());
  326. }
  327. /**
  328. * @see org.apache.fop.render.Renderer#processOffDocumentItem(OffDocumentItem)
  329. */
  330. public void processOffDocumentItem(OffDocumentItem odi) {
  331. // TODO
  332. }
  333. /** @see org.apache.fop.render.Renderer#getGraphics2DAdapter() */
  334. public Graphics2DAdapter getGraphics2DAdapter() {
  335. return new AFPGraphics2DAdapter();
  336. }
  337. /**
  338. * @see org.apache.fop.render.AbstractRenderer#startVParea(CTM, Rectangle2D)
  339. */
  340. public void startVParea(CTM ctm, Rectangle2D clippingRect) {
  341. // dummy not used
  342. }
  343. /**
  344. * @see org.apache.fop.render.AbstractRenderer#endVParea()
  345. */
  346. public void endVParea() {
  347. // dummy not used
  348. }
  349. /**
  350. * Renders a region viewport. <p>
  351. *
  352. * The region may clip the area and it establishes a position from where
  353. * the region is placed.</p>
  354. *
  355. * @param port The region viewport to be rendered
  356. */
  357. public void renderRegionViewport(RegionViewport port) {
  358. if (port != null) {
  359. Rectangle2D view = port.getViewArea();
  360. // The CTM will transform coordinates relative to
  361. // this region-reference area into page coords, so
  362. // set origin for the region to 0,0.
  363. currentBPPosition = 0;
  364. currentIPPosition = 0;
  365. RegionReference regionReference = port.getRegionReference();
  366. handleRegionTraits(port);
  367. /*
  368. _afpDataStream.startOverlay(mpts2units(view.getX())
  369. , mpts2units(view.getY())
  370. , mpts2units(view.getWidth())
  371. , mpts2units(view.getHeight())
  372. , rotation);
  373. */
  374. pushViewPortPos(new ViewPortPos(view, regionReference.getCTM()));
  375. if (regionReference.getRegionClass() == FO_REGION_BODY) {
  376. renderBodyRegion((BodyRegion) regionReference);
  377. } else {
  378. renderRegion(regionReference);
  379. }
  380. /*
  381. _afpDataStream.endOverlay();
  382. */
  383. popViewPortPos();
  384. }
  385. }
  386. /**
  387. * @see org.apache.fop.render.AbstractRenderer#renderBlockViewport(BlockViewport, List)
  388. */
  389. protected void renderBlockViewport(BlockViewport bv, List children) {
  390. // clip and position viewport if necessary
  391. // save positions
  392. int saveIP = currentIPPosition;
  393. int saveBP = currentBPPosition;
  394. //String saveFontName = currentFontName;
  395. CTM ctm = bv.getCTM();
  396. int borderPaddingStart = bv.getBorderAndPaddingWidthStart();
  397. int borderPaddingBefore = bv.getBorderAndPaddingWidthBefore();
  398. float x, y;
  399. x = (float)(bv.getXOffset() + containingIPPosition) / 1000f;
  400. y = (float)(bv.getYOffset() + containingBPPosition) / 1000f;
  401. //This is the content-rect
  402. float width = (float)bv.getIPD() / 1000f;
  403. float height = (float)bv.getBPD() / 1000f;
  404. if (bv.getPositioning() == Block.ABSOLUTE
  405. || bv.getPositioning() == Block.FIXED) {
  406. currentIPPosition = bv.getXOffset();
  407. currentBPPosition = bv.getYOffset();
  408. //For FIXED, we need to break out of the current viewports to the
  409. //one established by the page. We save the state stack for restoration
  410. //after the block-container has been painted. See below.
  411. List breakOutList = null;
  412. if (bv.getPositioning() == Block.FIXED) {
  413. breakOutList = breakOutOfStateStack();
  414. }
  415. CTM tempctm = new CTM(containingIPPosition, containingBPPosition);
  416. ctm = tempctm.multiply(ctm);
  417. //Adjust for spaces (from margin or indirectly by start-indent etc.
  418. x += bv.getSpaceStart() / 1000f;
  419. currentIPPosition += bv.getSpaceStart();
  420. y += bv.getSpaceBefore() / 1000f;
  421. currentBPPosition += bv.getSpaceBefore();
  422. float bpwidth = (borderPaddingStart + bv.getBorderAndPaddingWidthEnd()) / 1000f;
  423. float bpheight = (borderPaddingBefore + bv.getBorderAndPaddingWidthAfter()) / 1000f;
  424. drawBackAndBorders(bv, x, y, width + bpwidth, height + bpheight);
  425. //Now adjust for border/padding
  426. currentIPPosition += borderPaddingStart;
  427. currentBPPosition += borderPaddingBefore;
  428. Rectangle2D clippingRect = null;
  429. clippingRect = new Rectangle(currentIPPosition, currentBPPosition,
  430. bv.getIPD(), bv.getBPD());
  431. // startVParea(ctm, clippingRect);
  432. pushViewPortPos(new ViewPortPos(clippingRect, ctm));
  433. currentIPPosition = 0;
  434. currentBPPosition = 0;
  435. renderBlocks(bv, children);
  436. //endVParea();
  437. popViewPortPos();
  438. if (breakOutList != null) {
  439. restoreStateStackAfterBreakOut(breakOutList);
  440. }
  441. currentIPPosition = saveIP;
  442. currentBPPosition = saveBP;
  443. } else {
  444. currentBPPosition += bv.getSpaceBefore();
  445. //borders and background in the old coordinate system
  446. handleBlockTraits(bv);
  447. //Advance to start of content area
  448. currentIPPosition += bv.getStartIndent();
  449. CTM tempctm = new CTM(containingIPPosition, currentBPPosition);
  450. ctm = tempctm.multiply(ctm);
  451. //Now adjust for border/padding
  452. currentBPPosition += borderPaddingBefore;
  453. Rectangle2D clippingRect = null;
  454. clippingRect = new Rectangle(currentIPPosition, currentBPPosition,
  455. bv.getIPD(), bv.getBPD());
  456. //startVParea(ctm, clippingRect);
  457. pushViewPortPos(new ViewPortPos(clippingRect, ctm));
  458. currentIPPosition = 0;
  459. currentBPPosition = 0;
  460. renderBlocks(bv, children);
  461. //endVParea();
  462. popViewPortPos();
  463. currentIPPosition = saveIP;
  464. currentBPPosition = saveBP;
  465. currentBPPosition += (int)(bv.getAllocBPD());
  466. }
  467. //currentFontName = saveFontName;
  468. }
  469. /**
  470. * @see org.apache.fop.render.Renderer#renderPage(PageViewport)
  471. */
  472. public void renderPage(PageViewport page) {
  473. // initializeRootExtensions(page);
  474. _currentFontFamily = "";
  475. _currentFontSize = 0;
  476. _pageFontCounter = 0;
  477. _currentPageFonts.clear();
  478. _lineCache = new HashSet();
  479. Rectangle2D bounds = page.getViewArea();
  480. _pageWidth = mpts2units(bounds.getWidth());
  481. _pageHeight = mpts2units(bounds.getHeight());
  482. if (_pages != null && _pages.containsKey(page)) {
  483. _afpDataStream.restorePage((PageObject)_pages.remove(page));
  484. } else {
  485. // renderPageGroupExtensions(page);
  486. _afpDataStream.startPage(_pageWidth, _pageHeight, 0);
  487. renderPageObjectExtensions(page);
  488. }
  489. pushViewPortPos(new ViewPortPos());
  490. renderPageAreas(page.getPage());
  491. Iterator i = _currentPageFonts.values().iterator();
  492. while (i.hasNext()) {
  493. AFPFontAttributes afpFontAttributes = (AFPFontAttributes) i.next();
  494. _afpDataStream.createFont(
  495. afpFontAttributes.getFontReference(),
  496. afpFontAttributes.getFont(),
  497. afpFontAttributes.getPointSize());
  498. }
  499. try {
  500. _afpDataStream.endPage();
  501. } catch (IOException ioex) {
  502. // TODO What shall we do?
  503. }
  504. popViewPortPos();
  505. }
  506. /**
  507. * Clip using the current path.
  508. * @see org.apache.fop.render.AbstractPathOrientedRenderer#clip
  509. */
  510. public void clip() {
  511. // TODO
  512. }
  513. /**
  514. * Clip using a rectangular area.
  515. * @see org.apache.fop.render.AbstractPathOrientedRenderer#clipRect(float, float, float, float)
  516. */
  517. public void clipRect(float x, float y, float width, float height) {
  518. // TODO
  519. }
  520. /**
  521. * Moves the current point to (x, y), omitting any connecting line segment.
  522. * @see org.apache.fop.render.AbstractPathOrientedRenderer#moveTo(float, float)
  523. */
  524. public void moveTo(float x, float y) {
  525. // TODO
  526. }
  527. /**
  528. * Appends a straight line segment from the current point to (x, y). The
  529. * new current point is (x, y).
  530. * @see org.apache.fop.render.AbstractPathOrientedRenderer#lineTo(float, float)
  531. */
  532. public void lineTo(float x, float y) {
  533. // TODO
  534. }
  535. /**
  536. * Closes the current subpath by appending a straight line segment from
  537. * the current point to the starting point of the subpath.
  538. * @see org.apache.fop.render.AbstractPathOrientedRenderer#closePath
  539. */
  540. public void closePath() {
  541. // TODO
  542. }
  543. /**
  544. * Fill a rectangular area.
  545. * @see org.apache.fop.render.AbstractPathOrientedRenderer#fillRect(float, float, float, float)
  546. */
  547. public void fillRect(float x, float y, float width, float height) {
  548. /*
  549. _afpDataStream.createShading(
  550. pts2units(x),
  551. pts2units(y),
  552. pts2units(width),
  553. pts2units(height),
  554. _currentColor.getRed(),
  555. _currentColor.getGreen(),
  556. _currentColor.getBlue());
  557. */
  558. _afpDataStream.createLine(
  559. pts2units(x),
  560. pts2units(y),
  561. pts2units(x + width),
  562. pts2units(y),
  563. pts2units(height),
  564. _currentColor);
  565. }
  566. /**
  567. * Draw a border segment of an XSL-FO style border.
  568. * @see org.apache.fop.render.AbstractPathOrientedRenderer#drawBorderLine(float, float, float, float,
  569. * boolean, boolean, int, Color)
  570. */
  571. public void drawBorderLine(float x1, float y1, float x2, float y2,
  572. boolean horz, boolean startOrBefore, int style, Color col) {
  573. float w = x2 - x1;
  574. float h = y2 - y1;
  575. if ((w < 0) || (h < 0)) {
  576. log.error("Negative extent received. Border won't be painted.");
  577. return;
  578. }
  579. switch (style) {
  580. case Constants.EN_DOUBLE:
  581. if (horz) {
  582. float h3 = h / 3;
  583. float ym1 = y1;
  584. float ym2 = ym1 + h3 + h3;
  585. _afpDataStream.createLine(
  586. pts2units(x1),
  587. pts2units(ym1),
  588. pts2units(x2),
  589. pts2units(ym1),
  590. pts2units(h3),
  591. col
  592. );
  593. _afpDataStream.createLine(
  594. pts2units(x1),
  595. pts2units(ym2),
  596. pts2units(x2),
  597. pts2units(ym2),
  598. pts2units(h3),
  599. col
  600. );
  601. } else {
  602. float w3 = w / 3;
  603. float xm1 = x1;
  604. float xm2 = xm1 + w3 + w3;
  605. _afpDataStream.createLine(
  606. pts2units(xm1),
  607. pts2units(y1),
  608. pts2units(xm1),
  609. pts2units(y2),
  610. pts2units(w3),
  611. col
  612. );
  613. _afpDataStream.createLine(
  614. pts2units(xm2),
  615. pts2units(y1),
  616. pts2units(xm2),
  617. pts2units(y2),
  618. pts2units(w3),
  619. col
  620. );
  621. }
  622. break;
  623. case Constants.EN_DASHED:
  624. if (horz) {
  625. float w2 = 2 * h;
  626. while (x1 + w2 < x2) {
  627. _afpDataStream.createLine(
  628. pts2units(x1),
  629. pts2units(y1),
  630. pts2units(x1 + w2),
  631. pts2units(y1),
  632. pts2units(h),
  633. col
  634. );
  635. x1 += 2 * w2;
  636. }
  637. } else {
  638. float h2 = 2 * w;
  639. while (y1 + h2 < y2) {
  640. _afpDataStream.createLine(
  641. pts2units(x1),
  642. pts2units(y1),
  643. pts2units(x1),
  644. pts2units(y1 + h2),
  645. pts2units(w),
  646. col
  647. );
  648. y1 += 2 * h2;
  649. }
  650. }
  651. break;
  652. case Constants.EN_DOTTED:
  653. if (horz) {
  654. while (x1 + h < x2) {
  655. _afpDataStream.createLine(
  656. pts2units(x1),
  657. pts2units(y1),
  658. pts2units(x1 + h),
  659. pts2units(y1),
  660. pts2units(h),
  661. col
  662. );
  663. x1 += 2 * h;
  664. }
  665. } else {
  666. while (y1 + w < y2) {
  667. _afpDataStream.createLine(
  668. pts2units(x1),
  669. pts2units(y1),
  670. pts2units(x1),
  671. pts2units(y1 + w),
  672. pts2units(w),
  673. col
  674. );
  675. y1 += 2 * w;
  676. }
  677. }
  678. break;
  679. case Constants.EN_GROOVE:
  680. case Constants.EN_RIDGE:
  681. {
  682. float colFactor = (style == EN_GROOVE ? 0.4f : -0.4f);
  683. if (horz) {
  684. Color uppercol = lightenColor(col, -colFactor);
  685. Color lowercol = lightenColor(col, colFactor);
  686. float h3 = h / 3;
  687. float ym1 = y1;
  688. _afpDataStream.createLine(
  689. pts2units(x1),
  690. pts2units(ym1),
  691. pts2units(x2),
  692. pts2units(ym1),
  693. pts2units(h3),
  694. uppercol
  695. );
  696. _afpDataStream.createLine(
  697. pts2units(x1),
  698. pts2units(ym1 + h3),
  699. pts2units(x2),
  700. pts2units(ym1 + h3),
  701. pts2units(h3),
  702. col
  703. );
  704. _afpDataStream.createLine(
  705. pts2units(x1),
  706. pts2units(ym1 + h3 + h3),
  707. pts2units(x2),
  708. pts2units(ym1 + h3 + h3),
  709. pts2units(h3),
  710. lowercol
  711. );
  712. } else {
  713. Color leftcol = lightenColor(col, -colFactor);
  714. Color rightcol = lightenColor(col, colFactor);
  715. float w3 = w / 3;
  716. float xm1 = x1 + (w3 / 2);
  717. _afpDataStream.createLine(
  718. pts2units(xm1),
  719. pts2units(y1),
  720. pts2units(xm1),
  721. pts2units(y2),
  722. pts2units(w3),
  723. leftcol
  724. );
  725. _afpDataStream.createLine(
  726. pts2units(xm1 + w3),
  727. pts2units(y1),
  728. pts2units(xm1 + w3),
  729. pts2units(y2),
  730. pts2units(w3),
  731. col
  732. );
  733. _afpDataStream.createLine(
  734. pts2units(xm1 + w3 + w3),
  735. pts2units(y1),
  736. pts2units(xm1 + w3 + w3),
  737. pts2units(y2),
  738. pts2units(w3),
  739. rightcol
  740. );
  741. }
  742. break;
  743. }
  744. case Constants.EN_HIDDEN:
  745. break;
  746. case Constants.EN_INSET:
  747. case Constants.EN_OUTSET:
  748. default:
  749. _afpDataStream.createLine(
  750. pts2units(x1),
  751. pts2units(y1),
  752. pts2units(horz ? x2 : x1),
  753. pts2units(horz ? y1 : y2),
  754. pts2units(Math.abs(horz ? (y2 - y1) : (x2 - x1))),
  755. col
  756. );
  757. }
  758. }
  759. /**
  760. * @see org.apache.fop.render.PrintRenderer#createRendererContext(
  761. * int, int, int, int, java.util.Map)
  762. */
  763. protected RendererContext createRendererContext(int x, int y, int width, int height, Map foreignAttributes) {
  764. RendererContext context;
  765. context = super.createRendererContext(x, y, width, height, foreignAttributes);
  766. context.setProperty(AFPRendererContextConstants.AFP_GRAYSCALE,
  767. new Boolean(!this.colorImages));
  768. return context;
  769. }
  770. /**
  771. * Draw an image at the indicated location.
  772. * @see org.apache.fop.render.AbstractPathOrientedRenderer#drawImage(String, Rectangle2D, Map)
  773. */
  774. public void drawImage(String url, Rectangle2D pos, Map foreignAttributes) {
  775. String name = null;
  776. if (_pageSegmentsMap != null) {
  777. name = (String)_pageSegmentsMap.get(url);
  778. }
  779. if (name != null) {
  780. int x = mpts2units(pos.getX() + currentIPPosition);
  781. int y = mpts2units(pos.getY() + currentBPPosition);
  782. _afpDataStream.createIncludePageSegment(name, x, y);
  783. } else {
  784. url = ImageFactory.getURL(url);
  785. ImageFactory fact = userAgent.getFactory().getImageFactory();
  786. FopImage fopimage = fact.getImage(url, userAgent);
  787. if (fopimage == null) {
  788. return;
  789. }
  790. if (!fopimage.load(FopImage.DIMENSIONS)) {
  791. return;
  792. }
  793. String mime = fopimage.getMimeType();
  794. if ("text/xml".equals(mime) || MimeConstants.MIME_SVG.equals(mime)) {
  795. if (!fopimage.load(FopImage.ORIGINAL_DATA)) {
  796. return;
  797. }
  798. Document doc = ((XMLImage) fopimage).getDocument();
  799. String ns = ((XMLImage) fopimage).getNameSpace();
  800. renderDocument(doc, ns, pos, foreignAttributes);
  801. } else if (MimeConstants.MIME_EPS.equals(mime)) {
  802. log.warn("EPS images are not supported by this renderer");
  803. /*
  804. } else if (MimeConstants.MIME_JPEG.equals(mime)) {
  805. if (!fopimage.load(FopImage.ORIGINAL_DATA)) {
  806. return;
  807. }
  808. fact.releaseImage(url, userAgent);
  809. int x = mpts2units(pos.getX() + currentIPPosition);
  810. int y = mpts2units(pos.getY() + currentBPPosition);
  811. int w = mpts2units(pos.getWidth());
  812. int h = mpts2units(pos.getHeight());
  813. ImageObject io = _afpDataStream.getImageObject();
  814. io.setImageViewport(x, y, w, h);
  815. io.setImageParameters(
  816. (int)(fopimage.getHorizontalResolution() * 10),
  817. (int)(fopimage.getVerticalResolution() * 10),
  818. fopimage.getWidth(),
  819. fopimage.getHeight()
  820. );
  821. io.setImageIDESize((byte)fopimage.getBitsPerPixel());
  822. io.setImageEncoding((byte)0x83);
  823. io.setImageData(fopimage.getRessourceBytes());
  824. */
  825. } else if (MimeConstants.MIME_TIFF.equals(mime)
  826. && fopimage instanceof TIFFImage) {
  827. TIFFImage tiffImage = (TIFFImage) fopimage;
  828. int x = mpts2units(pos.getX() + currentIPPosition);
  829. int y = mpts2units(pos.getY() + currentBPPosition);
  830. int w = mpts2units(pos.getWidth());
  831. int h = mpts2units(pos.getHeight());
  832. ImageObject io = _afpDataStream.getImageObject(x, y, w, h);
  833. io.setImageParameters(
  834. (int)(fopimage.getHorizontalResolution() * 10),
  835. (int)(fopimage.getVerticalResolution() * 10),
  836. fopimage.getWidth(),
  837. fopimage.getHeight()
  838. );
  839. if (tiffImage.getStripCount() == 1) {
  840. int comp = tiffImage.getCompression();
  841. if (comp == 3) {
  842. if (!fopimage.load(FopImage.ORIGINAL_DATA)) {
  843. return;
  844. }
  845. io.setImageEncoding((byte)0x81);
  846. io.setImageData(fopimage.getRessourceBytes());
  847. } else if (comp == 4) {
  848. if (!fopimage.load(FopImage.ORIGINAL_DATA)) {
  849. return;
  850. }
  851. io.setImageEncoding((byte)0x82);
  852. io.setImageData(fopimage.getRessourceBytes());
  853. } else {
  854. if (!fopimage.load(FopImage.BITMAP)) {
  855. return;
  856. }
  857. convertToGrayScaleImage(io, fopimage.getBitmaps());
  858. }
  859. } else {
  860. if (!fopimage.load(FopImage.BITMAP)) {
  861. return;
  862. }
  863. convertToGrayScaleImage(io, fopimage.getBitmaps());
  864. }
  865. } else {
  866. if (!fopimage.load(FopImage.BITMAP)) {
  867. return;
  868. }
  869. fact.releaseImage(url, userAgent);
  870. int x = mpts2units(pos.getX() + currentIPPosition);
  871. int y = mpts2units(pos.getY() + currentBPPosition);
  872. int w = mpts2units(pos.getWidth());
  873. int h = mpts2units(pos.getHeight());
  874. ImageObject io = _afpDataStream.getImageObject(x, y, w, h);
  875. io.setImageParameters(
  876. (int)(fopimage.getHorizontalResolution() * 10),
  877. (int)(fopimage.getVerticalResolution() * 10),
  878. fopimage.getWidth(),
  879. fopimage.getHeight()
  880. );
  881. if (colorImages) {
  882. io.setImageIDESize((byte)24);
  883. io.setImageData(fopimage.getBitmaps());
  884. } else {
  885. convertToGrayScaleImage(io, fopimage.getBitmaps());
  886. }
  887. }
  888. }
  889. }
  890. /**
  891. * Writes a BufferedImage to an OutputStream as raw sRGB bitmaps.
  892. * @param img the BufferedImage
  893. * @param out the OutputStream
  894. * @throws IOException In case of an I/O error.
  895. */
  896. public static void writeImage(BufferedImage img, OutputStream out) throws IOException {
  897. int w = img.getWidth();
  898. int h = img.getHeight();
  899. int[] tmpMap = img.getRGB(0, 0, w, h, null, 0, w);
  900. for (int i = 0; i < h; i++) {
  901. for (int j = 0; j < w; j++) {
  902. int p = tmpMap[i * w + j];
  903. int r = (p >> 16) & 0xFF;
  904. int g = (p >> 8) & 0xFF;
  905. int b = (p) & 0xFF;
  906. out.write((byte)(r & 0xFF));
  907. out.write((byte)(g & 0xFF));
  908. out.write((byte)(b & 0xFF));
  909. }
  910. }
  911. }
  912. /**
  913. * Draws a BufferedImage to AFP.
  914. * @param bi the BufferedImage
  915. * @param resolution the resolution of the BufferedImage
  916. * @param x the x coordinate (in mpt)
  917. * @param y the y coordinate (in mpt)
  918. * @param w the width of the viewport (in mpt)
  919. * @param h the height of the viewport (in mpt)
  920. */
  921. public void drawBufferedImage(BufferedImage bi, int resolution, int x, int y, int w, int h) {
  922. int afpx = mpts2units(x);
  923. int afpy = mpts2units(y);
  924. int afpw = mpts2units(w);
  925. int afph = mpts2units(h);
  926. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  927. try {
  928. //Serialize image
  929. writeImage(bi, baout);
  930. byte[] buf = baout.toByteArray();
  931. //Generate image
  932. ImageObject io = _afpDataStream.getImageObject(afpx, afpy, afpw, afph);
  933. io.setImageParameters(
  934. resolution, resolution,
  935. bi.getWidth(),
  936. bi.getHeight()
  937. );
  938. if (colorImages) {
  939. io.setImageIDESize((byte)24);
  940. io.setImageData(buf);
  941. } else {
  942. //TODO Teach it how to handle grayscale BufferedImages directly
  943. //because this is pretty inefficient
  944. convertToGrayScaleImage(io, buf);
  945. }
  946. } catch (IOException ioe) {
  947. log.error("Error while serializing bitmap: " + ioe.getMessage(), ioe);
  948. }
  949. }
  950. /**
  951. * Establishes a new foreground or fill color.
  952. * @see org.apache.fop.render.AbstractPathOrientedRenderer#updateColor(Color, boolean)
  953. */
  954. public void updateColor(Color col, boolean fill) {
  955. if (fill) {
  956. _currentColor = col;
  957. }
  958. }
  959. /**
  960. * Restores the state stack after a break out.
  961. * @param breakOutList the state stack to restore.
  962. */
  963. public void restoreStateStackAfterBreakOut(List breakOutList) {
  964. }
  965. /**
  966. * Breaks out of the state stack to handle fixed block-containers.
  967. * @return the saved state stack to recreate later
  968. */
  969. public List breakOutOfStateStack() {
  970. return null;
  971. }
  972. /** Saves the graphics state of the rendering engine. */
  973. public void saveGraphicsState() {
  974. }
  975. /** Restores the last graphics state of the rendering engine. */
  976. public void restoreGraphicsState() {
  977. }
  978. /** Indicates the beginning of a text object. */
  979. public void beginTextObject() {
  980. }
  981. /** Indicates the end of a text object. */
  982. public void endTextObject() {
  983. }
  984. /**
  985. * @see org.apache.fop.render.AbstractRenderer#renderImage(Image, Rectangle2D)
  986. */
  987. public void renderImage(Image image, Rectangle2D pos) {
  988. String url = image.getURL();
  989. drawImage(url, pos);
  990. }
  991. /**
  992. * @see org.apache.fop.render.AbstractRenderer#renderText(TextArea)
  993. */
  994. public void renderText(TextArea text) {
  995. renderInlineAreaBackAndBorders(text);
  996. String name = getInternalFontNameForArea(text);
  997. _currentFontSize = ((Integer) text.getTrait(Trait.FONT_SIZE)).intValue();
  998. AFPFont tf = (AFPFont) fontInfo.getFonts().get(name);
  999. Color col = (Color) text.getTrait(Trait.COLOR);
  1000. int vsci = mpts2units(tf.getWidth(' ', _currentFontSize) / 1000
  1001. + text.getTextWordSpaceAdjust()
  1002. + text.getTextLetterSpaceAdjust());
  1003. // word.getOffset() = only height of text itself
  1004. // currentBlockIPPosition: 0 for beginning of line; nonzero
  1005. // where previous line area failed to take up entire allocated space
  1006. int rx = currentIPPosition + text.getBorderAndPaddingWidthStart();
  1007. int bl = currentBPPosition + text.getOffset() + text.getBaselineOffset();
  1008. // Set letterSpacing
  1009. //float ls = fs.getLetterSpacing() / this.currentFontSize;
  1010. String worddata = text.getText();
  1011. // Create an AFPFontAttributes object from the current font details
  1012. AFPFontAttributes afpFontAttributes =
  1013. new AFPFontAttributes(name, tf, _currentFontSize);
  1014. if (!_currentPageFonts.containsKey(afpFontAttributes.getFontKey())) {
  1015. // Font not found on current page, so add the new one
  1016. _pageFontCounter++;
  1017. afpFontAttributes.setFontReference(_pageFontCounter);
  1018. _currentPageFonts.put(
  1019. afpFontAttributes.getFontKey(),
  1020. afpFontAttributes);
  1021. } else {
  1022. // Use the previously stored font attributes
  1023. afpFontAttributes =
  1024. (AFPFontAttributes) _currentPageFonts.get(
  1025. afpFontAttributes.getFontKey());
  1026. }
  1027. // Try and get the encoding to use for the font
  1028. String encoding = null;
  1029. try {
  1030. encoding = tf.getCharacterSet(_currentFontSize).getEncoding();
  1031. } catch (Throwable ex) {
  1032. encoding = AFPConstants.EBCIDIC_ENCODING;
  1033. log.warn(
  1034. "renderText():: Error getting encoding for font "
  1035. + " - using default encoding "
  1036. + encoding);
  1037. }
  1038. try {
  1039. _afpDataStream.createText(
  1040. afpFontAttributes.getFontReference(),
  1041. mpts2units(rx),
  1042. mpts2units(bl),
  1043. col,
  1044. vsci,
  1045. mpts2units(text.getTextLetterSpaceAdjust()),
  1046. worddata.getBytes(encoding));
  1047. } catch (UnsupportedEncodingException usee) {
  1048. log.error(
  1049. "renderText:: Font "
  1050. + afpFontAttributes.getFontKey()
  1051. + " caused UnsupportedEncodingException");
  1052. }
  1053. super.renderText(text);
  1054. renderTextDecoration(tf, _currentFontSize, text, bl, rx);
  1055. }
  1056. /**
  1057. * @see org.apache.fop.render.AbstractRenderer#renderWord(WordArea)
  1058. */
  1059. public void renderWord(WordArea word) {
  1060. String name = getInternalFontNameForArea(word.getParentArea());
  1061. int size = ((Integer) word.getParentArea().getTrait(Trait.FONT_SIZE)).intValue();
  1062. AFPFont tf = (AFPFont) fontInfo.getFonts().get(name);
  1063. String s = word.getWord();
  1064. FontMetrics metrics = fontInfo.getMetricsFor(name);
  1065. super.renderWord(word);
  1066. }
  1067. /**
  1068. * @see org.apache.fop.render.AbstractRenderer#renderSpace(SpaceArea)
  1069. */
  1070. public void renderSpace(SpaceArea space) {
  1071. String name = getInternalFontNameForArea(space.getParentArea());
  1072. int size = ((Integer) space.getParentArea().getTrait(Trait.FONT_SIZE)).intValue();
  1073. AFPFont tf = (AFPFont) fontInfo.getFonts().get(name);
  1074. String s = space.getSpace();
  1075. FontMetrics metrics = fontInfo.getMetricsFor(name);
  1076. super.renderSpace(space);
  1077. }
  1078. /**
  1079. * Render leader area.
  1080. * This renders a leader area which is an area with a rule.
  1081. * @param area the leader area to render
  1082. */
  1083. public void renderLeader(Leader area) {
  1084. renderInlineAreaBackAndBorders(area);
  1085. int style = area.getRuleStyle();
  1086. float startx = (currentIPPosition + area.getBorderAndPaddingWidthStart()) / 1000f;
  1087. float starty = (currentBPPosition + area.getOffset()) / 1000f;
  1088. float endx = (currentIPPosition + area.getBorderAndPaddingWidthStart()
  1089. + area.getIPD()) / 1000f;
  1090. float ruleThickness = area.getRuleThickness() / 1000f;
  1091. Color col = (Color)area.getTrait(Trait.COLOR);
  1092. switch (style) {
  1093. case EN_SOLID:
  1094. case EN_DASHED:
  1095. case EN_DOUBLE:
  1096. case EN_DOTTED:
  1097. case EN_GROOVE:
  1098. case EN_RIDGE:
  1099. drawBorderLine(startx, starty, endx, starty + ruleThickness,
  1100. true, true, style, col);
  1101. break;
  1102. default:
  1103. throw new UnsupportedOperationException("rule style not supported");
  1104. }
  1105. super.renderLeader(area);
  1106. }
  1107. /**
  1108. * @see org.apache.fop.render.Renderer#setOptions(Map)
  1109. */
  1110. public void setOptions(Map options) {
  1111. _afpOptions = options;
  1112. }
  1113. /**
  1114. * Determines the orientation from the string representation, this method
  1115. * guarantees to return a value of either 0, 90, 180 or 270.
  1116. *
  1117. * @return the orientation
  1118. */
  1119. private int getOrientation(String orientationString) {
  1120. int orientation = 0;
  1121. if (orientationString != null && orientationString.length() > 0) {
  1122. try {
  1123. orientation = Integer.parseInt(orientationString);
  1124. } catch (NumberFormatException nfe) {
  1125. log.error(
  1126. "Cannot use orientation of "
  1127. + orientation
  1128. + " defaulting to zero.");
  1129. orientation = 0;
  1130. }
  1131. } else {
  1132. orientation = 0;
  1133. }
  1134. switch (orientation) {
  1135. case 0 :
  1136. break;
  1137. case 90 :
  1138. break;
  1139. case 180 :
  1140. break;
  1141. case 270 :
  1142. break;
  1143. default :
  1144. log.error(
  1145. "Cannot use orientation of "
  1146. + orientation
  1147. + " defaulting to zero.");
  1148. orientation = 0;
  1149. break;
  1150. }
  1151. return orientation;
  1152. }
  1153. /**
  1154. * Sets the rotation to be used for portrait pages, valid values are 0
  1155. * (default), 90, 180, 270.
  1156. *
  1157. * @param rotation
  1158. * The rotation in degrees.
  1159. */
  1160. public void setPortraitRotation(int rotation) {
  1161. if (rotation == 0
  1162. || rotation == 90
  1163. || rotation == 180
  1164. || rotation == 270) {
  1165. _portraitRotation = rotation;
  1166. } else {
  1167. throw new IllegalArgumentException("The portrait rotation must be one"
  1168. + " of the values 0, 90, 180, 270");
  1169. }
  1170. }
  1171. /**
  1172. * Sets the rotation to be used for landsacpe pages, valid values are 0, 90,
  1173. * 180, 270 (default).
  1174. *
  1175. * @param rotation
  1176. * The rotation in degrees.
  1177. */
  1178. public void setLandscapeRotation(int rotation) {
  1179. if (rotation == 0
  1180. || rotation == 90
  1181. || rotation == 180
  1182. || rotation == 270) {
  1183. _landscapeRotation = rotation;
  1184. } else {
  1185. throw new IllegalArgumentException("The landscape rotation must be one"
  1186. + " of the values 0, 90, 180, 270");
  1187. }
  1188. }
  1189. /**
  1190. * Get the MIME type of the renderer.
  1191. *
  1192. * @return The MIME type of the renderer
  1193. */
  1194. public String getMimeType() {
  1195. return MimeConstants.MIME_AFP;
  1196. }
  1197. /**
  1198. * Method to render the page extension.
  1199. * <p>
  1200. *
  1201. * @param page
  1202. * the page object
  1203. */
  1204. private void renderPageObjectExtensions(PageViewport page) {
  1205. _pageSegmentsMap = null;
  1206. if (page.getExtensionAttachments() != null
  1207. && page.getExtensionAttachments().size() > 0) {
  1208. //Extract all AFPPageSetup instances from the attachment list on the s-p-m
  1209. Iterator i = page.getExtensionAttachments().iterator();
  1210. while (i.hasNext()) {
  1211. ExtensionAttachment attachment = (ExtensionAttachment)i.next();
  1212. if (AFPPageSetup.CATEGORY.equals(attachment.getCategory())) {
  1213. AFPPageSetup aps = (AFPPageSetup)attachment;
  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. }
  1235. }
  1236. }
  1237. }
  1238. }
  1239. /**
  1240. * Converts FOP mpt measurement to afp measurement units
  1241. * @param mpt the millipoints value
  1242. */
  1243. private int mpts2units(int mpt) {
  1244. return mpts2units((double) mpt);
  1245. }
  1246. /**
  1247. * Converts FOP pt measurement to afp measurement units
  1248. * @param mpt the millipoints value
  1249. */
  1250. private int pts2units(float mpt) {
  1251. return mpts2units(mpt * 1000d);
  1252. }
  1253. /**
  1254. * Converts FOP mpt measurement to afp measurement units
  1255. * @param mpt the millipoints value
  1256. */
  1257. private int mpts2units(double mpt) {
  1258. return (int)Math.round(mpt / DPI_CONVERSION_FACTOR_240);
  1259. }
  1260. private void convertToGrayScaleImage(ImageObject io, byte raw[]) {
  1261. int pixelsPerByte = 8 / bitsPerPixel;
  1262. byte bw[] = new byte[raw.length / (3 * pixelsPerByte)];
  1263. int k = 0;
  1264. for (int i = 0, j = 0; i < raw.length; i += 3, j++) {
  1265. if (j == pixelsPerByte) {
  1266. j = 0;
  1267. k++;
  1268. }
  1269. // see http://www.jguru.com/faq/view.jsp?EID=221919
  1270. double greyVal = 0.212671d * ((int) raw[i] & 0xff)
  1271. + 0.715160d * ((int) raw[i + 1] & 0xff)
  1272. + 0.072169d * ((int) raw[i + 2] & 0xff);
  1273. switch (bitsPerPixel) {
  1274. case 1:
  1275. if (greyVal > 128) {
  1276. bw[k] |= (byte)(1 << j);
  1277. }
  1278. break;
  1279. case 4:
  1280. greyVal /= 16;
  1281. bw[k] |= (byte)((byte)greyVal << (j * 4));
  1282. break;
  1283. case 8:
  1284. bw[k] = (byte)greyVal;
  1285. break;
  1286. }
  1287. }
  1288. io.setImageIDESize((byte)bitsPerPixel);
  1289. io.setImageData(bw);
  1290. }
  1291. private class ViewPortPos {
  1292. int x = 0;
  1293. int y = 0;
  1294. int rot = 0;
  1295. ViewPortPos() {
  1296. }
  1297. ViewPortPos(Rectangle2D view, CTM ctm) {
  1298. ViewPortPos currentVP = (ViewPortPos)viewPortPositions.get(viewPortPositions.size() - 1);
  1299. int xOrigin;
  1300. int yOrigin;
  1301. int width;
  1302. int height;
  1303. switch (currentVP.rot) {
  1304. case 90:
  1305. width = mpts2units(view.getHeight());
  1306. height = mpts2units(view.getWidth());
  1307. xOrigin = _pageWidth - width - mpts2units(view.getY()) - currentVP.y;
  1308. yOrigin = mpts2units(view.getX()) + currentVP.x;
  1309. break;
  1310. case 180:
  1311. width = mpts2units(view.getWidth());
  1312. height = mpts2units(view.getHeight());
  1313. xOrigin = _pageWidth - width - mpts2units(view.getX()) - currentVP.x;
  1314. yOrigin = _pageHeight - height - mpts2units(view.getY()) - currentVP.y;
  1315. break;
  1316. case 270:
  1317. width = mpts2units(view.getHeight());
  1318. height = mpts2units(view.getWidth());
  1319. xOrigin = mpts2units(view.getY()) + currentVP.y;
  1320. yOrigin = _pageHeight - height - mpts2units(view.getX()) - currentVP.x;
  1321. break;
  1322. default:
  1323. xOrigin = mpts2units(view.getX()) + currentVP.x;
  1324. yOrigin = mpts2units(view.getY()) + currentVP.y;
  1325. width = mpts2units(view.getWidth());
  1326. height = mpts2units(view.getHeight());
  1327. break;
  1328. }
  1329. this.rot = currentVP.rot;
  1330. double ctmf[] = ctm.toArray();
  1331. if (ctmf[0] == 0.0d && ctmf[1] == -1.0d && ctmf[2] == 1.0d && ctmf[3] == 0.d) {
  1332. this.rot += 270;
  1333. } else if (ctmf[0] == -1.0d && ctmf[1] == 0.0d && ctmf[2] == 0.0d && ctmf[3] == -1.0d) {
  1334. this.rot += 180;
  1335. } else if (ctmf[0] == 0.0d && ctmf[1] == 1.0d && ctmf[2] == -1.0d && ctmf[3] == 0.0d) {
  1336. this.rot += 90;
  1337. }
  1338. this.rot %= 360;
  1339. switch (this.rot) {
  1340. /*
  1341. case 0:
  1342. this.x = mpts2units(view.getX()) + x;
  1343. this.y = mpts2units(view.getY()) + y;
  1344. break;
  1345. case 90:
  1346. this.x = mpts2units(view.getY()) + y;
  1347. this.y = _pageWidth - mpts2units(view.getX() + view.getWidth()) - x;
  1348. break;
  1349. case 180:
  1350. this.x = _pageWidth - mpts2units(view.getX() + view.getWidth()) - x;
  1351. this.y = _pageHeight - mpts2units(view.getY() + view.getHeight()) - y;
  1352. break;
  1353. case 270:
  1354. this.x = _pageHeight - mpts2units(view.getY() + view.getHeight()) - y;
  1355. this.y = mpts2units(view.getX()) + x;
  1356. break;
  1357. */
  1358. case 0:
  1359. this.x = xOrigin;
  1360. this.y = yOrigin;
  1361. break;
  1362. case 90:
  1363. this.x = yOrigin;
  1364. this.y = _pageWidth - width - xOrigin;
  1365. break;
  1366. case 180:
  1367. this.x = _pageWidth - width - xOrigin;
  1368. this.y = _pageHeight - height - yOrigin;
  1369. break;
  1370. case 270:
  1371. this.x = _pageHeight - height - yOrigin;
  1372. this.y = xOrigin;
  1373. break;
  1374. }
  1375. }
  1376. public String toString() {
  1377. return "x:" + x + " y:" + y + " rot:" + rot;
  1378. }
  1379. }
  1380. private List viewPortPositions = new ArrayList();
  1381. private void pushViewPortPos(ViewPortPos vpp) {
  1382. viewPortPositions.add(vpp);
  1383. _afpDataStream.setOffsets(vpp.x, vpp.y, vpp.rot);
  1384. }
  1385. private void popViewPortPos() {
  1386. viewPortPositions.remove(viewPortPositions.size() - 1);
  1387. if (viewPortPositions.size() > 0) {
  1388. ViewPortPos vpp = (ViewPortPos)viewPortPositions.get(viewPortPositions.size() - 1);
  1389. _afpDataStream.setOffsets(vpp.x, vpp.y, vpp.rot);
  1390. }
  1391. }
  1392. public void setBitsPerPixel(int bitsPerPixel) {
  1393. this.bitsPerPixel = bitsPerPixel;
  1394. switch (bitsPerPixel) {
  1395. case 1:
  1396. case 4:
  1397. case 8:
  1398. break;
  1399. default:
  1400. log.warn("Invalid bits_per_pixel value, must be 1, 4 or 8.");
  1401. bitsPerPixel = 8;
  1402. break;
  1403. }
  1404. }
  1405. public void setColorImages(boolean colorImages) {
  1406. this.colorImages = colorImages;
  1407. }
  1408. }