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.

PDFGraphics2D.java 69KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889
  1. /*
  2. * Copyright 1999-2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.svg;
  18. import org.apache.fop.pdf.PDFConformanceException;
  19. import org.apache.fop.pdf.PDFResourceContext;
  20. import org.apache.fop.pdf.PDFResources;
  21. import org.apache.fop.pdf.PDFGState;
  22. import org.apache.fop.pdf.PDFColorSpace;
  23. import org.apache.fop.pdf.PDFColor;
  24. import org.apache.fop.pdf.PDFState;
  25. import org.apache.fop.pdf.PDFNumber;
  26. import org.apache.fop.pdf.PDFText;
  27. import org.apache.fop.pdf.PDFXObject;
  28. import org.apache.fop.pdf.PDFPattern;
  29. import org.apache.fop.pdf.PDFDocument;
  30. import org.apache.fop.pdf.PDFLink;
  31. import org.apache.fop.pdf.PDFAnnotList;
  32. import org.apache.fop.pdf.BitmapImage;
  33. import org.apache.fop.fonts.FontInfo;
  34. import org.apache.fop.fonts.Font;
  35. import org.apache.fop.fonts.FontSetup;
  36. import org.apache.fop.fonts.FontTriplet;
  37. import org.apache.fop.fonts.LazyFont;
  38. import org.apache.fop.image.JpegImage;
  39. import org.apache.fop.fonts.CIDFont;
  40. import org.apache.fop.render.pdf.FopPDFImage;
  41. import org.apache.xmlgraphics.java2d.AbstractGraphics2D;
  42. import org.apache.xmlgraphics.java2d.GraphicContext;
  43. import org.apache.batik.ext.awt.RadialGradientPaint;
  44. import org.apache.batik.ext.awt.LinearGradientPaint;
  45. import org.apache.batik.ext.awt.MultipleGradientPaint;
  46. import org.apache.batik.ext.awt.RenderingHintsKeyExt;
  47. import org.apache.batik.gvt.PatternPaint;
  48. import org.apache.batik.gvt.GraphicsNode;
  49. import java.text.AttributedCharacterIterator;
  50. import java.text.CharacterIterator;
  51. import java.awt.Graphics;
  52. import java.awt.Graphics2D;
  53. import java.awt.Color;
  54. import java.awt.GraphicsConfiguration;
  55. /* java.awt.Font is not imported to avoid confusion with
  56. org.apache.fop.fonts.Font */
  57. import java.awt.GradientPaint;
  58. import java.awt.Image;
  59. import java.awt.Shape;
  60. import java.awt.Stroke;
  61. import java.awt.Paint;
  62. import java.awt.PaintContext;
  63. import java.awt.Rectangle;
  64. import java.awt.Dimension;
  65. import java.awt.BasicStroke;
  66. import java.awt.AlphaComposite;
  67. import java.awt.geom.AffineTransform;
  68. import java.awt.color.ColorSpace;
  69. import java.awt.image.BufferedImage;
  70. import java.awt.image.ColorModel;
  71. import java.awt.image.DirectColorModel;
  72. import java.awt.image.DataBuffer;
  73. import java.awt.image.DataBufferInt;
  74. import java.awt.image.ImageObserver;
  75. import java.awt.image.RenderedImage;
  76. import java.awt.image.Raster;
  77. import java.awt.image.WritableRaster;
  78. import java.awt.image.renderable.RenderableImage;
  79. import java.awt.geom.PathIterator;
  80. import java.awt.geom.Point2D;
  81. import java.awt.geom.Rectangle2D;
  82. import java.io.StringWriter;
  83. import java.io.IOException;
  84. import java.io.OutputStream;
  85. import java.util.Map;
  86. import java.util.List;
  87. /**
  88. * PDF Graphics 2D.
  89. * Used for drawing into a pdf document as if it is a graphics object.
  90. * This takes a pdf document and draws into it.
  91. *
  92. * @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a>
  93. * @version $Id$
  94. * @see org.apache.batik.ext.awt.g2d.AbstractGraphics2D
  95. */
  96. public class PDFGraphics2D extends AbstractGraphics2D {
  97. private static final AffineTransform IDENTITY_TRANSFORM = new AffineTransform();
  98. /** The number of decimal places. */
  99. private static final int DEC = 8;
  100. /**
  101. * the PDF Document being created
  102. */
  103. protected PDFDocument pdfDoc;
  104. /**
  105. * The current resource context for adding fonts, patterns etc.
  106. */
  107. protected PDFResourceContext resourceContext;
  108. /**
  109. * The PDF reference of the current page.
  110. */
  111. protected String pageRef;
  112. /**
  113. * the current state of the pdf graphics
  114. */
  115. protected PDFState graphicsState;
  116. /**
  117. * The PDF graphics state level that this svg is being drawn into.
  118. */
  119. protected int baseLevel = 0;
  120. /**
  121. * The count of JPEG images added to document so they recieve
  122. * unique keys.
  123. */
  124. protected int[] jpegCount = {0};
  125. /**
  126. * The current font information.
  127. */
  128. protected FontInfo fontInfo;
  129. /**
  130. * The override font state used when drawing text and the font cannot be
  131. * set using java fonts.
  132. */
  133. protected Font ovFontState = null;
  134. /**
  135. * the current stream to add PDF commands to
  136. */
  137. protected StringWriter currentStream = new StringWriter();
  138. /**
  139. * the current (internal) font name
  140. */
  141. protected String currentFontName;
  142. /**
  143. * the current font size in millipoints
  144. */
  145. protected float currentFontSize;
  146. /**
  147. * The output stream for the pdf document.
  148. * If this is set then it can progressively output
  149. * the pdf document objects to reduce memory.
  150. * Especially with images.
  151. */
  152. protected OutputStream outputStream = null;
  153. /**
  154. * Create a new PDFGraphics2D with the given pdf document info.
  155. * This is used to create a Graphics object for use inside an already
  156. * existing document.
  157. *
  158. * @param textAsShapes if true then draw text as shapes
  159. * @param fi the current font information
  160. * @param doc the pdf document for creating pdf objects
  161. * @param page the current resource context or page
  162. * @param pref the PDF reference of the current page
  163. * @param font the current font name
  164. * @param size the current font size
  165. */
  166. public PDFGraphics2D(boolean textAsShapes, FontInfo fi, PDFDocument doc,
  167. PDFResourceContext page, String pref, String font, float size) {
  168. this(textAsShapes);
  169. pdfDoc = doc;
  170. resourceContext = page;
  171. currentFontName = font;
  172. currentFontSize = size;
  173. fontInfo = fi;
  174. pageRef = pref;
  175. graphicsState = new PDFState();
  176. }
  177. /**
  178. * Create a new PDFGraphics2D.
  179. *
  180. * @param textAsShapes true if drawing text as shapes
  181. */
  182. protected PDFGraphics2D(boolean textAsShapes) {
  183. super(textAsShapes);
  184. }
  185. /**
  186. * This constructor supports the create method.
  187. * This is not implemented properly.
  188. *
  189. * @param g the PDF graphics to make a copy of
  190. */
  191. public PDFGraphics2D(PDFGraphics2D g) {
  192. super(g);
  193. this.pdfDoc = g.pdfDoc;
  194. this.resourceContext = g.resourceContext;
  195. this.currentFontName = g.currentFontName;
  196. this.currentFontSize = g.currentFontSize;
  197. this.fontInfo = g.fontInfo;
  198. this.pageRef = g.pageRef;
  199. this.graphicsState = g.graphicsState;
  200. this.currentStream = g.currentStream;
  201. this.jpegCount = g.jpegCount;
  202. this.outputStream = g.outputStream;
  203. this.ovFontState = g.ovFontState;
  204. }
  205. /**
  206. * Creates a new <code>Graphics</code> object that is
  207. * a copy of this <code>Graphics</code> object.
  208. * @return a new graphics context that is a copy of
  209. * this graphics context.
  210. */
  211. public Graphics create() {
  212. return new PDFGraphics2D(this);
  213. }
  214. /**
  215. * Central handler for IOExceptions for this class.
  216. * @param ioe IOException to handle
  217. */
  218. protected void handleIOException(IOException ioe) {
  219. //TODO Surely, there's a better way to do this.
  220. ioe.printStackTrace();
  221. }
  222. /**
  223. * This method is used by PDFDocumentGraphics2D to prepare a new page if
  224. * necessary.
  225. */
  226. protected void preparePainting() {
  227. //nop, used by PDFDocumentGraphics2D
  228. }
  229. /**
  230. * Set the PDF state to use when starting to draw
  231. * into the PDF graphics.
  232. *
  233. * @param state the PDF state
  234. */
  235. public void setPDFState(PDFState state) {
  236. graphicsState = state;
  237. baseLevel = graphicsState.getStackLevel();
  238. }
  239. /**
  240. * Set the output stream that this PDF document is
  241. * being drawn to. This is so that it can progressively
  242. * use the PDF document to output data such as images.
  243. * This results in a significant saving on memory.
  244. *
  245. * @param os the output stream that is being used for the PDF document
  246. */
  247. public void setOutputStream(OutputStream os) {
  248. outputStream = os;
  249. }
  250. /**
  251. * Get the string containing all the commands written into this
  252. * Grpahics.
  253. * @return the string containing the PDF markup
  254. */
  255. public String getString() {
  256. return currentStream.toString();
  257. }
  258. /**
  259. * Get the string buffer from the currentStream, containing all
  260. * the commands written into this Grpahics so far.
  261. * @return the StringBuffer containing the PDF markup
  262. */
  263. public StringBuffer getBuffer() {
  264. return currentStream.getBuffer();
  265. }
  266. /**
  267. * Set the Grpahics context.
  268. * @param c the graphics context to use
  269. */
  270. public void setGraphicContext(GraphicContext c) {
  271. gc = c;
  272. setPrivateHints();
  273. }
  274. private void setPrivateHints() {
  275. setRenderingHint(RenderingHintsKeyExt.KEY_AVOID_TILE_PAINTING,
  276. RenderingHintsKeyExt.VALUE_AVOID_TILE_PAINTING_ON);
  277. }
  278. /**
  279. * Set the override font state for drawing text.
  280. * This is used by the PDF text painter so that it can temporarily
  281. * set the font state when a java font cannot be used.
  282. * The next drawString will use this font state.
  283. *
  284. * @param infont the font state to use
  285. */
  286. public void setOverrideFontState(Font infont) {
  287. ovFontState = infont;
  288. }
  289. /**
  290. * Restore the PDF graphics state to the starting state level.
  291. */
  292. /* seems not to be used
  293. public void restorePDFState() {
  294. for (int count = graphicsState.getStackLevel(); count > baseLevel; count--) {
  295. currentStream.write("Q\n");
  296. }
  297. graphicsState.restoreLevel(baseLevel);
  298. }*/
  299. private void concatMatrix(double[] matrix) {
  300. currentStream.write(PDFNumber.doubleOut(matrix[0], DEC) + " "
  301. + PDFNumber.doubleOut(matrix[1], DEC) + " "
  302. + PDFNumber.doubleOut(matrix[2], DEC) + " "
  303. + PDFNumber.doubleOut(matrix[3], DEC) + " "
  304. + PDFNumber.doubleOut(matrix[4], DEC) + " "
  305. + PDFNumber.doubleOut(matrix[5], DEC) + " cm\n");
  306. }
  307. /**
  308. * This is a pdf specific method used to add a link to the
  309. * pdf document.
  310. *
  311. * @param bounds the bounds of the link in user coordinates
  312. * @param trans the transform of the current drawing position
  313. * @param dest the PDF destination
  314. * @param linkType the type of link, internal or external
  315. */
  316. public void addLink(Rectangle2D bounds, AffineTransform trans, String dest, int linkType) {
  317. preparePainting();
  318. AffineTransform at = getTransform();
  319. Shape b = at.createTransformedShape(bounds);
  320. b = trans.createTransformedShape(b);
  321. if (b != null) {
  322. Rectangle rect = b.getBounds();
  323. if (linkType != PDFLink.EXTERNAL) {
  324. String pdfdest = "/FitR " + dest;
  325. resourceContext.addAnnotation(
  326. pdfDoc.getFactory().makeLink(rect, pageRef, pdfdest));
  327. } else {
  328. resourceContext.addAnnotation(
  329. pdfDoc.getFactory().makeLink(rect, dest, linkType, 0));
  330. }
  331. }
  332. }
  333. /**
  334. * Add a JPEG image directly to the PDF document.
  335. * This is used by the PDFImageElementBridge to draw a JPEG
  336. * directly into the pdf document rather than converting the image into
  337. * a bitmap and increasing the size.
  338. *
  339. * @param jpeg the jpeg image to draw
  340. * @param x the x position
  341. * @param y the y position
  342. * @param width the width to draw the image
  343. * @param height the height to draw the image
  344. */
  345. public void addJpegImage(JpegImage jpeg, float x, float y,
  346. float width, float height) {
  347. preparePainting();
  348. // Need to include hash code as when invoked from FO you
  349. // may have several 'independent' PDFGraphics2D so the
  350. // count is not enough.
  351. String key = "__AddJPEG_" + hashCode() + "_" + jpegCount[0];
  352. jpegCount[0]++;
  353. FopPDFImage fopimage = new FopPDFImage(jpeg, key);
  354. int xObjectNum = this.pdfDoc.addImage(resourceContext,
  355. fopimage).getXNumber();
  356. AffineTransform at = getTransform();
  357. double[] matrix = new double[6];
  358. at.getMatrix(matrix);
  359. currentStream.write("q\n");
  360. if (!at.isIdentity()) {
  361. concatMatrix(matrix);
  362. }
  363. Shape imclip = getClip();
  364. writeClip(imclip);
  365. currentStream.write("" + width + " 0 0 "
  366. + (-height) + " "
  367. + x + " "
  368. + (y + height) + " cm\n" + "/Im"
  369. + xObjectNum + " Do\nQ\n");
  370. if (outputStream != null) {
  371. try {
  372. this.pdfDoc.output(outputStream);
  373. } catch (IOException ioe) {
  374. // ignore exception, will be thrown again later
  375. }
  376. }
  377. }
  378. /**
  379. * Draws as much of the specified image as is currently available.
  380. * The image is drawn with its top-left corner at
  381. * (<i>x</i>,&nbsp;<i>y</i>) in this graphics context's coordinate
  382. * space. Transparent pixels in the image do not affect whatever
  383. * pixels are already there.
  384. * <p>
  385. * This method returns immediately in all cases, even if the
  386. * complete image has not yet been loaded, and it has not been dithered
  387. * and converted for the current output device.
  388. * <p>
  389. * If the image has not yet been completely loaded, then
  390. * <code>drawImage</code> returns <code>false</code>. As more of
  391. * the image becomes available, the process that draws the image notifies
  392. * the specified image observer.
  393. * @param img the specified image to be drawn.
  394. * @param x the <i>x</i> coordinate.
  395. * @param y the <i>y</i> coordinate.
  396. * @param observer object to be notified as more of
  397. * the image is converted.
  398. * @return true if the image was drawn
  399. * @see java.awt.Image
  400. * @see java.awt.image.ImageObserver
  401. * @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
  402. */
  403. public boolean drawImage(Image img, int x, int y,
  404. ImageObserver observer) {
  405. preparePainting();
  406. int width = img.getWidth(observer);
  407. int height = img.getHeight(observer);
  408. if (width == -1 || height == -1) {
  409. return false;
  410. }
  411. return drawImage(img, x, y, width, height, observer);
  412. }
  413. private BufferedImage buildBufferedImage(Dimension size) {
  414. return new BufferedImage(size.width, size.height,
  415. BufferedImage.TYPE_INT_ARGB);
  416. }
  417. /**
  418. * Draws as much of the specified image as has already been scaled
  419. * to fit inside the specified rectangle.
  420. * <p>
  421. * The image is drawn inside the specified rectangle of this
  422. * graphics context's coordinate space, and is scaled if
  423. * necessary. Transparent pixels do not affect whatever pixels
  424. * are already there.
  425. * <p>
  426. * This method returns immediately in all cases, even if the
  427. * entire image has not yet been scaled, dithered, and converted
  428. * for the current output device.
  429. * If the current output representation is not yet complete, then
  430. * <code>drawImage</code> returns <code>false</code>. As more of
  431. * the image becomes available, the process that draws the image notifies
  432. * the image observer by calling its <code>imageUpdate</code> method.
  433. * <p>
  434. * A scaled version of an image will not necessarily be
  435. * available immediately just because an unscaled version of the
  436. * image has been constructed for this output device. Each size of
  437. * the image may be cached separately and generated from the original
  438. * data in a separate image production sequence.
  439. * @param img the specified image to be drawn.
  440. * @param x the <i>x</i> coordinate.
  441. * @param y the <i>y</i> coordinate.
  442. * @param width the width of the rectangle.
  443. * @param height the height of the rectangle.
  444. * @param observer object to be notified as more of
  445. * the image is converted.
  446. * @return true if the image was drawn
  447. * @see java.awt.Image
  448. * @see java.awt.image.ImageObserver
  449. * @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
  450. */
  451. public boolean drawImage(Image img, int x, int y, int width, int height,
  452. ImageObserver observer) {
  453. preparePainting();
  454. // first we look to see if we've already added this image to
  455. // the pdf document. If so, we just reuse the reference;
  456. // otherwise we have to build a FopImage and add it to the pdf
  457. // document
  458. PDFXObject imageInfo = pdfDoc.getImage("TempImage:" + img.toString());
  459. if (imageInfo == null) {
  460. // OK, have to build and add a PDF image
  461. Dimension size = new Dimension(width, height);
  462. BufferedImage buf = buildBufferedImage(size);
  463. java.awt.Graphics2D g = buf.createGraphics();
  464. g.setComposite(AlphaComposite.SrcOver);
  465. g.setBackground(new Color(1, 1, 1, 0));
  466. g.setPaint(new Color(1, 1, 1, 0));
  467. g.fillRect(0, 0, width, height);
  468. g.clip(new Rectangle(0, 0, buf.getWidth(), buf.getHeight()));
  469. g.setComposite(gc.getComposite());
  470. if (!g.drawImage(img, 0, 0, buf.getWidth(), buf.getHeight(), observer)) {
  471. return false;
  472. }
  473. g.dispose();
  474. final byte[] result = new byte[buf.getWidth() * buf.getHeight() * 3 /*for RGB*/];
  475. byte[] mask = new byte[buf.getWidth() * buf.getHeight()];
  476. boolean hasMask = false;
  477. //boolean binaryMask = true;
  478. Raster raster = buf.getData();
  479. DataBuffer bd = raster.getDataBuffer();
  480. int count = 0;
  481. int maskpos = 0;
  482. int[] iarray;
  483. int i, j, val, alpha;
  484. switch (bd.getDataType()) {
  485. case DataBuffer.TYPE_INT:
  486. int[][] idata = ((DataBufferInt)bd).getBankData();
  487. for (i = 0; i < idata.length; i++) {
  488. iarray = idata[i];
  489. for (j = 0; j < iarray.length; j++) {
  490. val = iarray[j];
  491. alpha = val >>> 24;
  492. mask[maskpos++] = (byte)(alpha & 0xFF);
  493. if (alpha != 255) {
  494. hasMask = true;
  495. }
  496. result[count++] = (byte)((val >> 16) & 0xFF);
  497. result[count++] = (byte)((val >> 8) & 0xFF);
  498. result[count++] = (byte)((val) & 0xFF);
  499. }
  500. }
  501. break;
  502. default:
  503. // error
  504. break;
  505. }
  506. String ref = null;
  507. if (hasMask) {
  508. // if the mask is binary then we could convert it into a bitmask
  509. BitmapImage fopimg = new BitmapImage("TempImageMask:"
  510. + img.toString(), buf.getWidth(),
  511. buf.getHeight(), mask, null);
  512. fopimg.setColorSpace(new PDFColorSpace(PDFColorSpace.DEVICE_GRAY));
  513. PDFXObject xobj = pdfDoc.addImage(resourceContext, fopimg);
  514. ref = xobj.referencePDF();
  515. if (outputStream != null) {
  516. try {
  517. this.pdfDoc.output(outputStream);
  518. } catch (IOException ioe) {
  519. // ignore exception, will be thrown again later
  520. }
  521. }
  522. } else {
  523. mask = null;
  524. }
  525. BitmapImage fopimg = new BitmapImage("TempImage:"
  526. + img.toString(), buf.getWidth(),
  527. buf.getHeight(), result, ref);
  528. imageInfo = pdfDoc.addImage(resourceContext, fopimg);
  529. //int xObjectNum = imageInfo.getXNumber();
  530. if (outputStream != null) {
  531. try {
  532. this.pdfDoc.output(outputStream);
  533. } catch (IOException ioe) {
  534. // ignore exception, will be thrown again later
  535. }
  536. }
  537. } else {
  538. resourceContext.getPDFResources().addXObject(imageInfo);
  539. }
  540. // now do any transformation required and add the actual image
  541. // placement instance
  542. AffineTransform at = getTransform();
  543. double[] matrix = new double[6];
  544. at.getMatrix(matrix);
  545. currentStream.write("q\n");
  546. if (!at.isIdentity()) {
  547. concatMatrix(matrix);
  548. }
  549. Shape imclip = getClip();
  550. writeClip(imclip);
  551. currentStream.write("" + width + " 0 0 " + (-height) + " " + x
  552. + " " + (y + height) + " cm\n" + "/Im"
  553. + imageInfo.getXNumber() + " Do\nQ\n");
  554. return true;
  555. }
  556. /**
  557. * Disposes of this graphics context and releases
  558. * any system resources that it is using.
  559. * A <code>Graphics</code> object cannot be used after
  560. * <code>dispose</code>has been called.
  561. * <p>
  562. * When a Java program runs, a large number of <code>Graphics</code>
  563. * objects can be created within a short time frame.
  564. * Although the finalization process of the garbage collector
  565. * also disposes of the same system resources, it is preferable
  566. * to manually free the associated resources by calling this
  567. * method rather than to rely on a finalization process which
  568. * may not run to completion for a long period of time.
  569. * <p>
  570. * Graphics objects which are provided as arguments to the
  571. * <code>paint</code> and <code>update</code> methods
  572. * of components are automatically released by the system when
  573. * those methods return. For efficiency, programmers should
  574. * call <code>dispose</code> when finished using
  575. * a <code>Graphics</code> object only if it was created
  576. * directly from a component or another <code>Graphics</code> object.
  577. * @see java.awt.Graphics#finalize
  578. * @see java.awt.Component#paint
  579. * @see java.awt.Component#update
  580. * @see java.awt.Component#getGraphics
  581. * @see java.awt.Graphics#create
  582. */
  583. public void dispose() {
  584. pdfDoc = null;
  585. fontInfo = null;
  586. currentStream = null;
  587. currentFontName = null;
  588. }
  589. /**
  590. * Strokes the outline of a <code>Shape</code> using the settings of the
  591. * current <code>Graphics2D</code> context. The rendering attributes
  592. * applied include the <code>Clip</code>, <code>Transform</code>,
  593. * <code>Paint</code>, <code>Composite</code> and
  594. * <code>Stroke</code> attributes.
  595. * @param s the <code>Shape</code> to be rendered
  596. * @see #setStroke
  597. * @see #setPaint
  598. * @see java.awt.Graphics#setColor
  599. * @see #transform
  600. * @see #setTransform
  601. * @see #clip
  602. * @see #setClip
  603. * @see #setComposite
  604. */
  605. public void draw(Shape s) {
  606. preparePainting();
  607. //Transparency shortcut
  608. Color c;
  609. c = getColor();
  610. if (c.getAlpha() == 0) {
  611. return;
  612. }
  613. AffineTransform trans = getTransform();
  614. double[] tranvals = new double[6];
  615. trans.getMatrix(tranvals);
  616. Shape imclip = getClip();
  617. boolean newClip = graphicsState.checkClip(imclip);
  618. boolean newTransform = graphicsState.checkTransform(trans)
  619. && !trans.isIdentity();
  620. if (newClip || newTransform) {
  621. currentStream.write("q\n");
  622. graphicsState.push();
  623. if (newTransform) {
  624. concatMatrix(tranvals);
  625. }
  626. if (newClip) {
  627. writeClip(imclip);
  628. }
  629. }
  630. if (c.getAlpha() != 255) {
  631. Map vals = new java.util.HashMap();
  632. vals.put(PDFGState.GSTATE_ALPHA_STROKE,
  633. new Float(c.getAlpha() / 255f));
  634. PDFGState gstate = pdfDoc.getFactory().makeGState(
  635. vals, graphicsState.getGState());
  636. //gstate.setAlpha(c.getAlpha() / 255f, false);
  637. resourceContext.addGState(gstate);
  638. currentStream.write("/" + gstate.getName() + " gs\n");
  639. }
  640. c = getColor();
  641. if (graphicsState.setColor(c)) {
  642. applyColor(c, false);
  643. }
  644. c = getBackground();
  645. if (graphicsState.setBackColor(c)) {
  646. applyColor(c, true);
  647. }
  648. Paint paint = getPaint();
  649. if (graphicsState.setPaint(paint)) {
  650. if (!applyPaint(paint, false)) {
  651. // Stroke the shape and use it to 'clip'
  652. // the paint contents.
  653. Shape ss = getStroke().createStrokedShape(s);
  654. applyUnknownPaint(paint, ss);
  655. if (newClip || newTransform) {
  656. currentStream.write("Q\n");
  657. graphicsState.pop();
  658. }
  659. return;
  660. }
  661. }
  662. applyStroke(getStroke());
  663. PathIterator iter = s.getPathIterator(IDENTITY_TRANSFORM);
  664. processPathIterator(iter);
  665. doDrawing(false, true, false);
  666. if (newClip || newTransform) {
  667. currentStream.write("Q\n");
  668. graphicsState.pop();
  669. }
  670. }
  671. /*
  672. // in theory we could set the clip using these methods
  673. // it doesn't seem to improve the file sizes much
  674. // and makes everything more complicated
  675. Shape lastClip = null;
  676. public void clip(Shape cl) {
  677. super.clip(cl);
  678. Shape newClip = getClip();
  679. if (newClip == null || lastClip == null
  680. || !(new Area(newClip).equals(new Area(lastClip)))) {
  681. graphicsState.setClip(newClip);
  682. writeClip(newClip);
  683. }
  684. lastClip = newClip;
  685. }
  686. public void setClip(Shape cl) {
  687. super.setClip(cl);
  688. Shape newClip = getClip();
  689. if (newClip == null || lastClip == null
  690. || !(new Area(newClip).equals(new Area(lastClip)))) {
  691. for (int count = graphicsState.getStackLevel(); count > baseLevel; count--) {
  692. currentStream.write("Q\n");
  693. }
  694. graphicsState.restoreLevel(baseLevel);
  695. currentStream.write("q\n");
  696. graphicsState.push();
  697. if (newClip != null) {
  698. graphicsState.setClip(newClip);
  699. }
  700. writeClip(newClip);
  701. }
  702. lastClip = newClip;
  703. }
  704. */
  705. /**
  706. * Set the clipping shape for future PDF drawing in the current graphics state.
  707. * This sets creates and writes a clipping shape that will apply
  708. * to future drawings in the current graphics state.
  709. *
  710. * @param s the clipping shape
  711. */
  712. protected void writeClip(Shape s) {
  713. if (s == null) {
  714. return;
  715. }
  716. preparePainting();
  717. PathIterator iter = s.getPathIterator(IDENTITY_TRANSFORM);
  718. processPathIterator(iter);
  719. // clip area
  720. currentStream.write("W\n");
  721. currentStream.write("n\n");
  722. }
  723. /**
  724. * Apply the java Color to PDF.
  725. * This converts the java colour to a PDF colour and
  726. * sets it for the next drawing.
  727. *
  728. * @param col the java colour
  729. * @param fill true if the colour will be used for filling
  730. */
  731. protected void applyColor(Color col, boolean fill) {
  732. preparePainting();
  733. Color c = col;
  734. if (c.getColorSpace().getType()
  735. == ColorSpace.TYPE_RGB) {
  736. PDFColor currentColour = new PDFColor(c.getRed(), c.getGreen(),
  737. c.getBlue());
  738. currentStream.write(currentColour.getColorSpaceOut(fill));
  739. } else if (c.getColorSpace().getType()
  740. == ColorSpace.TYPE_CMYK) {
  741. if (pdfDoc.getPDFAMode().isPDFA1LevelB()) {
  742. //See PDF/A-1, ISO 19005:1:2005(E), 6.2.3.3
  743. //FOP is currently restricted to DeviceRGB if PDF/A-1 is active.
  744. throw new PDFConformanceException(
  745. "PDF/A-1 does not allow mixing DeviceRGB and DeviceCMYK.");
  746. }
  747. float[] cComps = c.getColorComponents(new float[3]);
  748. double[] cmyk = new double[3];
  749. for (int i = 0; i < 3; i++) {
  750. // convert the float elements to doubles for pdf
  751. cmyk[i] = cComps[i];
  752. }
  753. PDFColor currentColour = new PDFColor(cmyk[0], cmyk[1], cmyk[2], cmyk[3]);
  754. currentStream.write(currentColour.getColorSpaceOut(fill));
  755. } else if (c.getColorSpace().getType()
  756. == ColorSpace.TYPE_2CLR) {
  757. // used for black/magenta
  758. float[] cComps = c.getColorComponents(new float[1]);
  759. double[] blackMagenta = new double[1];
  760. for (int i = 0; i < 1; i++) {
  761. blackMagenta[i] = cComps[i];
  762. }
  763. //PDFColor currentColour = new PDFColor(blackMagenta[0], blackMagenta[1]);
  764. //currentStream.write(currentColour.getColorSpaceOut(fill));
  765. } else {
  766. throw new UnsupportedOperationException(
  767. "Color Space not supported by PDFGraphics2D");
  768. }
  769. }
  770. /**
  771. * Apply the java paint to the PDF.
  772. * This takes the java paint sets up the appropraite PDF commands
  773. * for the drawing with that paint.
  774. * Currently this supports the gradients and patterns from batik.
  775. *
  776. * @param paint the paint to convert to PDF
  777. * @param fill true if the paint should be set for filling
  778. * @return true if the paint is handled natively, false if the paint should be rasterized
  779. */
  780. protected boolean applyPaint(Paint paint, boolean fill) {
  781. preparePainting();
  782. if (paint instanceof Color) {
  783. return true;
  784. }
  785. // convert java.awt.GradientPaint to LinearGradientPaint to avoid rasterization
  786. if (paint instanceof GradientPaint) {
  787. GradientPaint gpaint = (GradientPaint) paint;
  788. paint = new LinearGradientPaint(
  789. (float) gpaint.getPoint1().getX(),
  790. (float) gpaint.getPoint1().getY(),
  791. (float) gpaint.getPoint2().getX(),
  792. (float) gpaint.getPoint2().getY(),
  793. new float[] {0, 1},
  794. new Color[] {gpaint.getColor1(), gpaint.getColor2()},
  795. gpaint.isCyclic() ? LinearGradientPaint.REPEAT : LinearGradientPaint.NO_CYCLE);
  796. }
  797. if (paint instanceof LinearGradientPaint) {
  798. LinearGradientPaint gp = (LinearGradientPaint)paint;
  799. // This code currently doesn't support 'repeat'.
  800. // For linear gradients it is possible to construct
  801. // a 'tile' that is repeated with a PDF pattern, but
  802. // it would be very tricky as you would have to rotate
  803. // the coordinate system so the repeat was axially
  804. // aligned. At this point I'm just going to rasterize it.
  805. MultipleGradientPaint.CycleMethodEnum cycle = gp.getCycleMethod();
  806. if (cycle != MultipleGradientPaint.NO_CYCLE) {
  807. return false;
  808. }
  809. Color[] cols = gp.getColors();
  810. float[] fractions = gp.getFractions();
  811. // Build proper transform from gradient space to page space
  812. // ('Patterns' don't get userspace transform).
  813. AffineTransform transform;
  814. transform = new AffineTransform(graphicsState.getTransform());
  815. transform.concatenate(getTransform());
  816. transform.concatenate(gp.getTransform());
  817. List theMatrix = new java.util.ArrayList();
  818. double [] mat = new double[6];
  819. transform.getMatrix(mat);
  820. for (int idx = 0; idx < mat.length; idx++) {
  821. theMatrix.add(new Double(mat[idx]));
  822. }
  823. Point2D p1 = gp.getStartPoint();
  824. Point2D p2 = gp.getEndPoint();
  825. List theCoords = new java.util.ArrayList();
  826. theCoords.add(new Double(p1.getX()));
  827. theCoords.add(new Double(p1.getY()));
  828. theCoords.add(new Double(p2.getX()));
  829. theCoords.add(new Double(p2.getY()));
  830. List theExtend = new java.util.ArrayList();
  831. theExtend.add(new Boolean(true));
  832. theExtend.add(new Boolean(true));
  833. List theDomain = new java.util.ArrayList();
  834. theDomain.add(new Double(0));
  835. theDomain.add(new Double(1));
  836. List theEncode = new java.util.ArrayList();
  837. theEncode.add(new Double(0));
  838. theEncode.add(new Double(1));
  839. theEncode.add(new Double(0));
  840. theEncode.add(new Double(1));
  841. List theBounds = new java.util.ArrayList();
  842. List someColors = new java.util.ArrayList();
  843. for (int count = 0; count < cols.length; count++) {
  844. Color c1 = cols[count];
  845. if (c1.getAlpha() != 255) {
  846. return false; // PDF can't do alpha
  847. }
  848. PDFColor color1 = new PDFColor(c1.getRed(), c1.getGreen(),
  849. c1.getBlue());
  850. someColors.add(color1);
  851. if (count > 0 && count < cols.length - 1) {
  852. theBounds.add(new Double(fractions[count]));
  853. }
  854. }
  855. PDFColorSpace aColorSpace;
  856. aColorSpace = new PDFColorSpace(PDFColorSpace.DEVICE_RGB);
  857. PDFPattern myPat = pdfDoc.getFactory().makeGradient(
  858. resourceContext, false, aColorSpace,
  859. someColors, theBounds, theCoords, theMatrix);
  860. currentStream.write(myPat.getColorSpaceOut(fill));
  861. return true;
  862. }
  863. if (paint instanceof RadialGradientPaint) {
  864. RadialGradientPaint rgp = (RadialGradientPaint)paint;
  865. // There is essentially no way to support repeate
  866. // in PDF for radial gradients (the one option would
  867. // be to 'grow' the outer circle until it fully covered
  868. // the bounds and then grow the stops accordingly, the
  869. // problem is that this may require an extremely large
  870. // number of stops for cases where the focus is near
  871. // the edge of the outer circle). so we rasterize.
  872. MultipleGradientPaint.CycleMethodEnum cycle = rgp.getCycleMethod();
  873. if (cycle != MultipleGradientPaint.NO_CYCLE) {
  874. return false;
  875. }
  876. AffineTransform transform;
  877. transform = new AffineTransform(graphicsState.getTransform());
  878. transform.concatenate(getTransform());
  879. transform.concatenate(rgp.getTransform());
  880. List theMatrix = new java.util.ArrayList();
  881. double [] mat = new double[6];
  882. transform.getMatrix(mat);
  883. for (int idx = 0; idx < mat.length; idx++) {
  884. theMatrix.add(new Double(mat[idx]));
  885. }
  886. double ar = rgp.getRadius();
  887. Point2D ac = rgp.getCenterPoint();
  888. Point2D af = rgp.getFocusPoint();
  889. List theCoords = new java.util.ArrayList();
  890. double dx = af.getX() - ac.getX();
  891. double dy = af.getY() - ac.getY();
  892. double d = Math.sqrt(dx * dx + dy * dy);
  893. if (d > ar) {
  894. // the center point af must be within the circle with
  895. // radius ar centered at ac so limit it to that.
  896. double scale = (ar * .9999) / d;
  897. dx = dx * scale;
  898. dy = dy * scale;
  899. }
  900. theCoords.add(new Double(ac.getX() + dx)); // Fx
  901. theCoords.add(new Double(ac.getY() + dy)); // Fy
  902. theCoords.add(new Double(0));
  903. theCoords.add(new Double(ac.getX()));
  904. theCoords.add(new Double(ac.getY()));
  905. theCoords.add(new Double(ar));
  906. Color[] cols = rgp.getColors();
  907. List someColors = new java.util.ArrayList();
  908. for (int count = 0; count < cols.length; count++) {
  909. Color cc = cols[count];
  910. if (cc.getAlpha() != 255) {
  911. return false; // PDF can't do alpha
  912. }
  913. someColors.add(new PDFColor(cc.getRed(), cc.getGreen(),
  914. cc.getBlue()));
  915. }
  916. float[] fractions = rgp.getFractions();
  917. List theBounds = new java.util.ArrayList();
  918. for (int count = 1; count < fractions.length - 1; count++) {
  919. float offset = fractions[count];
  920. theBounds.add(new Double(offset));
  921. }
  922. PDFColorSpace colSpace;
  923. colSpace = new PDFColorSpace(PDFColorSpace.DEVICE_RGB);
  924. PDFPattern myPat = pdfDoc.getFactory().makeGradient
  925. (resourceContext, true, colSpace,
  926. someColors, theBounds, theCoords, theMatrix);
  927. currentStream.write(myPat.getColorSpaceOut(fill));
  928. return true;
  929. }
  930. if (paint instanceof PatternPaint) {
  931. PatternPaint pp = (PatternPaint)paint;
  932. return createPattern(pp, fill);
  933. }
  934. return false; // unknown paint
  935. }
  936. private boolean createPattern(PatternPaint pp, boolean fill) {
  937. preparePainting();
  938. FontInfo fontInfo = new FontInfo();
  939. FontSetup.setup(fontInfo, null, null);
  940. PDFResources res = pdfDoc.getFactory().makeResources();
  941. PDFResourceContext context = new PDFResourceContext(res);
  942. PDFGraphics2D pattGraphic = new PDFGraphics2D(textAsShapes, fontInfo,
  943. pdfDoc, context, pageRef,
  944. "", 0);
  945. pattGraphic.setGraphicContext(new GraphicContext());
  946. pattGraphic.gc.validateTransformStack();
  947. pattGraphic.setRenderingHints(this.getRenderingHints());
  948. pattGraphic.setOutputStream(outputStream);
  949. GraphicsNode gn = pp.getGraphicsNode();
  950. Rectangle2D gnBBox = gn.getBounds();
  951. Rectangle2D rect = pp.getPatternRect();
  952. // if (!pp.getOverflow()) {
  953. gn.paint(pattGraphic);
  954. // } else {
  955. // /* Commented out until SVN version of Batik is included */
  956. // // For overflow we need to paint the content from
  957. // // all the tiles who's overflow will intersect one
  958. // // tile (left->right, top->bottom). Then we can
  959. // // simply replicate that tile as normal.
  960. // double gnMinX = gnBBox.getX();
  961. // double gnMaxX = gnBBox.getX() + gnBBox.getWidth();
  962. // double gnMinY = gnBBox.getY();
  963. // double gnMaxY = gnBBox.getY() + gnBBox.getHeight();
  964. // double patMaxX = rect.getX() + rect.getWidth();
  965. // double patMaxY = rect.getY() + rect.getHeight();
  966. // double stepX = rect.getWidth();
  967. // double stepY = rect.getHeight();
  968. //
  969. // int startX = (int)((rect.getX() - gnMaxX)/stepX);
  970. // int startY = (int)((rect.getY() - gnMaxY)/stepY);
  971. //
  972. // int endX = (int)((patMaxX - gnMinX)/stepX);
  973. // int endY = (int)((patMaxY - gnMinY)/stepY);
  974. //
  975. // pattGraphic.translate(startX*stepX, startY*stepY);
  976. // for (int yIdx=startY; yIdx<=endY; yIdx++) {
  977. // for (int xIdx=startX; xIdx<=endX; xIdx++) {
  978. // gn.paint(pattGraphic);
  979. // pattGraphic.translate(stepX,0);
  980. // }
  981. // pattGraphic.translate(-(endX-startX+1)*stepX, stepY);
  982. // }
  983. // }
  984. List bbox = new java.util.ArrayList();
  985. bbox.add(new Double(rect.getX()));
  986. bbox.add(new Double(rect.getHeight() + rect.getY()));
  987. bbox.add(new Double(rect.getWidth() + rect.getX()));
  988. bbox.add(new Double(rect.getY()));
  989. AffineTransform transform;
  990. transform = new AffineTransform(graphicsState.getTransform());
  991. transform.concatenate(getTransform());
  992. transform.concatenate(pp.getPatternTransform());
  993. List theMatrix = new java.util.ArrayList();
  994. double [] mat = new double[6];
  995. transform.getMatrix(mat);
  996. for (int idx = 0; idx < mat.length; idx++) {
  997. theMatrix.add(new Double(mat[idx]));
  998. }
  999. /** @todo see if pdfDoc and res can be linked here,
  1000. (currently res <> PDFDocument's resources) so addFonts()
  1001. can be moved to PDFDocument class */
  1002. res.addFonts(pdfDoc, fontInfo);
  1003. PDFPattern myPat = pdfDoc.getFactory().makePattern(
  1004. resourceContext, 1, res, 1, 1, bbox,
  1005. rect.getWidth(), rect.getHeight(),
  1006. theMatrix, null,
  1007. pattGraphic.getBuffer());
  1008. currentStream.write(myPat.getColorSpaceOut(fill));
  1009. PDFAnnotList annots = context.getAnnotations();
  1010. if (annots != null) {
  1011. this.pdfDoc.addObject(annots);
  1012. }
  1013. if (outputStream != null) {
  1014. try {
  1015. this.pdfDoc.output(outputStream);
  1016. } catch (IOException ioe) {
  1017. // ignore exception, will be thrown again later
  1018. }
  1019. }
  1020. return true;
  1021. }
  1022. protected boolean applyUnknownPaint(Paint paint, Shape shape) {
  1023. preparePainting();
  1024. Shape clip = getClip();
  1025. Rectangle2D usrClipBounds, usrBounds;
  1026. usrBounds = shape.getBounds2D();
  1027. usrClipBounds = clip.getBounds2D();
  1028. if (!usrClipBounds.intersects(usrBounds)) {
  1029. return true;
  1030. }
  1031. Rectangle2D.intersect(usrBounds, usrClipBounds, usrBounds);
  1032. double usrX = usrBounds.getX();
  1033. double usrY = usrBounds.getY();
  1034. double usrW = usrBounds.getWidth();
  1035. double usrH = usrBounds.getHeight();
  1036. Rectangle devShapeBounds, devClipBounds, devBounds;
  1037. AffineTransform at = getTransform();
  1038. devShapeBounds = at.createTransformedShape(shape).getBounds();
  1039. devClipBounds = at.createTransformedShape(clip).getBounds();
  1040. if (!devClipBounds.intersects(devShapeBounds)) {
  1041. return true;
  1042. }
  1043. devBounds = devShapeBounds.intersection(devClipBounds);
  1044. int devX = devBounds.x;
  1045. int devY = devBounds.y;
  1046. int devW = devBounds.width;
  1047. int devH = devBounds.height;
  1048. ColorSpace rgbCS = ColorSpace.getInstance(ColorSpace.CS_sRGB);
  1049. ColorModel rgbCM = new DirectColorModel
  1050. (rgbCS, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000,
  1051. false, DataBuffer.TYPE_BYTE);
  1052. PaintContext pctx = paint.createContext(rgbCM, devBounds, usrBounds,
  1053. at, getRenderingHints());
  1054. PDFXObject imageInfo = pdfDoc.getImage
  1055. ("TempImage:" + pctx.toString());
  1056. if (imageInfo != null) {
  1057. resourceContext.getPDFResources().addXObject(imageInfo);
  1058. } else {
  1059. Raster r = pctx.getRaster(devX, devY, devW, devH);
  1060. WritableRaster wr = (WritableRaster)r;
  1061. wr = wr.createWritableTranslatedChild(0, 0);
  1062. ColorModel pcm = pctx.getColorModel();
  1063. BufferedImage bi = new BufferedImage
  1064. (pcm, wr, pcm.isAlphaPremultiplied(), null);
  1065. final byte[] rgb = new byte[devW * devH * 3];
  1066. final int[] line = new int[devW];
  1067. final byte[] mask;
  1068. int x, y, val, rgbIdx = 0;
  1069. if (pcm.hasAlpha()) {
  1070. mask = new byte[devW * devH];
  1071. int maskIdx = 0;
  1072. for (y = 0; y < devH; y++) {
  1073. bi.getRGB(0, y, devW, 1, line, 0, devW);
  1074. for (x = 0; x < devW; x++) {
  1075. val = line[x];
  1076. mask[maskIdx++] = (byte)(val >>> 24);
  1077. rgb[rgbIdx++] = (byte)((val >> 16) & 0x0FF);
  1078. rgb[rgbIdx++] = (byte)((val >> 8 ) & 0x0FF);
  1079. rgb[rgbIdx++] = (byte)((val ) & 0x0FF);
  1080. }
  1081. }
  1082. } else {
  1083. mask = null;
  1084. for (y = 0; y < devH; y++) {
  1085. bi.getRGB(0, y, devW, 1, line, 0, devW);
  1086. for (x = 0; x < devW; x++) {
  1087. val = line[x];
  1088. rgb[rgbIdx++] = (byte)((val >> 16) & 0x0FF);
  1089. rgb[rgbIdx++] = (byte)((val >> 8 ) & 0x0FF);
  1090. rgb[rgbIdx++] = (byte)((val ) & 0x0FF);
  1091. }
  1092. }
  1093. }
  1094. String maskRef = null;
  1095. if (mask != null) {
  1096. BitmapImage fopimg = new BitmapImage
  1097. ("TempImageMask:" + pctx.toString(), devW, devH, mask, null);
  1098. fopimg.setColorSpace(new PDFColorSpace(PDFColorSpace.DEVICE_GRAY));
  1099. PDFXObject xobj = pdfDoc.addImage(resourceContext, fopimg);
  1100. maskRef = xobj.referencePDF();
  1101. if (outputStream != null) {
  1102. try {
  1103. this.pdfDoc.output(outputStream);
  1104. } catch (IOException ioe) {
  1105. // ignore exception, will be thrown again later
  1106. }
  1107. }
  1108. }
  1109. BitmapImage fopimg;
  1110. fopimg = new BitmapImage("TempImage:" + pctx.toString(),
  1111. devW, devH, rgb, maskRef);
  1112. fopimg.setTransparent(new PDFColor(255, 255, 255));
  1113. imageInfo = pdfDoc.addImage(resourceContext, fopimg);
  1114. if (outputStream != null) {
  1115. try {
  1116. this.pdfDoc.output(outputStream);
  1117. } catch (IOException ioe) {
  1118. // ignore exception, will be thrown again later
  1119. }
  1120. }
  1121. }
  1122. currentStream.write("q\n");
  1123. writeClip(shape);
  1124. currentStream.write("" + usrW + " 0 0 " + (-usrH) + " " + usrX
  1125. + " " + (usrY + usrH) + " cm\n" + "/Im"
  1126. + imageInfo.getXNumber() + " Do\nQ\n");
  1127. return true;
  1128. }
  1129. /**
  1130. * Apply the stroke to the PDF.
  1131. * This takes the java stroke and outputs the appropriate settings
  1132. * to the PDF so that the stroke attributes are handled.
  1133. *
  1134. * @param stroke the java stroke
  1135. */
  1136. protected void applyStroke(Stroke stroke) {
  1137. preparePainting();
  1138. if (stroke instanceof BasicStroke) {
  1139. BasicStroke bs = (BasicStroke)stroke;
  1140. float[] da = bs.getDashArray();
  1141. if (da != null) {
  1142. currentStream.write("[");
  1143. for (int count = 0; count < da.length; count++) {
  1144. if (((int)da[count]) == 0) {
  1145. // the dasharray units in pdf are (whole) numbers
  1146. // in user space units, cannot be 0
  1147. currentStream.write("1");
  1148. } else {
  1149. currentStream.write("" + ((int)da[count]));
  1150. }
  1151. if (count < da.length - 1) {
  1152. currentStream.write(" ");
  1153. }
  1154. }
  1155. currentStream.write("] ");
  1156. float offset = bs.getDashPhase();
  1157. currentStream.write(((int)offset) + " d\n");
  1158. }
  1159. int ec = bs.getEndCap();
  1160. switch (ec) {
  1161. case BasicStroke.CAP_BUTT:
  1162. currentStream.write(0 + " J\n");
  1163. break;
  1164. case BasicStroke.CAP_ROUND:
  1165. currentStream.write(1 + " J\n");
  1166. break;
  1167. case BasicStroke.CAP_SQUARE:
  1168. currentStream.write(2 + " J\n");
  1169. break;
  1170. }
  1171. int lj = bs.getLineJoin();
  1172. switch (lj) {
  1173. case BasicStroke.JOIN_MITER:
  1174. currentStream.write(0 + " j\n");
  1175. break;
  1176. case BasicStroke.JOIN_ROUND:
  1177. currentStream.write(1 + " j\n");
  1178. break;
  1179. case BasicStroke.JOIN_BEVEL:
  1180. currentStream.write(2 + " j\n");
  1181. break;
  1182. }
  1183. float lw = bs.getLineWidth();
  1184. currentStream.write(PDFNumber.doubleOut(lw) + " w\n");
  1185. float ml = bs.getMiterLimit();
  1186. currentStream.write(PDFNumber.doubleOut(ml) + " M\n");
  1187. }
  1188. }
  1189. /**
  1190. * Renders a {@link RenderedImage},
  1191. * applying a transform from image
  1192. * space into user space before drawing.
  1193. * The transformation from user space into device space is done with
  1194. * the current <code>Transform</code> in the <code>Graphics2D</code>.
  1195. * The specified transformation is applied to the image before the
  1196. * transform attribute in the <code>Graphics2D</code> context is applied.
  1197. * The rendering attributes applied include the <code>Clip</code>,
  1198. * <code>Transform</code>, and <code>Composite</code> attributes. Note
  1199. * that no rendering is done if the specified transform is
  1200. * noninvertible.
  1201. * @param img the image to be rendered
  1202. * @param xform the transformation from image space into user space
  1203. * @see #transform
  1204. * @see #setTransform
  1205. * @see #setComposite
  1206. * @see #clip
  1207. * @see #setClip
  1208. */
  1209. public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
  1210. //NYI
  1211. }
  1212. /**
  1213. * Renders a
  1214. * {@link RenderableImage},
  1215. * applying a transform from image space into user space before drawing.
  1216. * The transformation from user space into device space is done with
  1217. * the current <code>Transform</code> in the <code>Graphics2D</code>.
  1218. * The specified transformation is applied to the image before the
  1219. * transform attribute in the <code>Graphics2D</code> context is applied.
  1220. * The rendering attributes applied include the <code>Clip</code>,
  1221. * <code>Transform</code>, and <code>Composite</code> attributes. Note
  1222. * that no rendering is done if the specified transform is
  1223. * noninvertible.
  1224. * <p>
  1225. * Rendering hints set on the <code>Graphics2D</code> object might
  1226. * be used in rendering the <code>RenderableImage</code>.
  1227. * If explicit control is required over specific hints recognized by a
  1228. * specific <code>RenderableImage</code>, or if knowledge of which hints
  1229. * are used is required, then a <code>RenderedImage</code> should be
  1230. * obtained directly from the <code>RenderableImage</code>
  1231. * and rendered using
  1232. * {@link #drawRenderedImage(RenderedImage, AffineTransform) drawRenderedImage}.
  1233. * @param img the image to be rendered
  1234. * @param xform the transformation from image space into user space
  1235. * @see #transform
  1236. * @see #setTransform
  1237. * @see #setComposite
  1238. * @see #clip
  1239. * @see #setClip
  1240. * @see #drawRenderedImage
  1241. */
  1242. public void drawRenderableImage(RenderableImage img,
  1243. AffineTransform xform) {
  1244. //NYI
  1245. }
  1246. /**
  1247. * Renders the text specified by the specified <code>String</code>,
  1248. * using the current <code>Font</code> and <code>Paint</code> attributes
  1249. * in the <code>Graphics2D</code> context.
  1250. * The baseline of the first character is at position
  1251. * (<i>x</i>,&nbsp;<i>y</i>) in the User Space.
  1252. * The rendering attributes applied include the <code>Clip</code>,
  1253. * <code>Transform</code>, <code>Paint</code>, <code>Font</code> and
  1254. * <code>Composite</code> attributes. For characters in script systems
  1255. * such as Hebrew and Arabic, the glyphs can be rendered from right to
  1256. * left, in which case the coordinate supplied is the location of the
  1257. * leftmost character on the baseline.
  1258. * @param s the <code>String</code> to be rendered
  1259. * @param x the coordinate where the <code>String</code>
  1260. * should be rendered
  1261. * @param y the coordinate where the <code>String</code>
  1262. * should be rendered
  1263. * @see #setPaint
  1264. * @see java.awt.Graphics#setColor
  1265. * @see java.awt.Graphics#setFont
  1266. * @see #setTransform
  1267. * @see #setComposite
  1268. * @see #setClip
  1269. */
  1270. public void drawString(String s, float x, float y) {
  1271. preparePainting();
  1272. Font fontState;
  1273. AffineTransform fontTransform = null;
  1274. if (ovFontState == null) {
  1275. java.awt.Font gFont = getFont();
  1276. fontTransform = gFont.getTransform();
  1277. String n = gFont.getFamily();
  1278. if (n.equals("sanserif")) {
  1279. n = "sans-serif";
  1280. }
  1281. float siz = gFont.getSize2D();
  1282. String style = gFont.isItalic() ? "italic" : "normal";
  1283. int weight = gFont.isBold() ? Font.BOLD : Font.NORMAL;
  1284. FontTriplet triplet = fontInfo.fontLookup(n, style, weight);
  1285. fontState = fontInfo.getFontInstance(triplet, (int)(siz * 1000 + 0.5));
  1286. } else {
  1287. fontState = fontInfo.getFontInstance(
  1288. ovFontState.getFontTriplet(), ovFontState.getFontSize());
  1289. ovFontState = null;
  1290. }
  1291. String name;
  1292. float size;
  1293. name = fontState.getFontName();
  1294. size = (float)fontState.getFontSize() / 1000f;
  1295. if ((!name.equals(this.currentFontName))
  1296. || (size != this.currentFontSize)) {
  1297. this.currentFontName = name;
  1298. this.currentFontSize = size;
  1299. currentStream.write("/" + name + " " + size + " Tf\n");
  1300. }
  1301. currentStream.write("q\n");
  1302. Color c = getColor();
  1303. applyColor(c, true);
  1304. applyPaint(getPaint(), true);
  1305. int salpha = c.getAlpha();
  1306. if (salpha != 255) {
  1307. Map vals = new java.util.HashMap();
  1308. vals.put(PDFGState.GSTATE_ALPHA_NONSTROKE, new Float(salpha / 255f));
  1309. PDFGState gstate = pdfDoc.getFactory().makeGState(
  1310. vals, graphicsState.getGState());
  1311. resourceContext.addGState(gstate);
  1312. currentStream.write("/" + gstate.getName() + " gs\n");
  1313. }
  1314. Map kerning = null;
  1315. boolean kerningAvailable = false;
  1316. kerning = fontState.getKerning();
  1317. if (kerning != null && !kerning.isEmpty()) {
  1318. kerningAvailable = true;
  1319. }
  1320. // This assumes that *all* CIDFonts use a /ToUnicode mapping
  1321. boolean useMultiByte = false;
  1322. org.apache.fop.fonts.Typeface f =
  1323. (org.apache.fop.fonts.Typeface)fontInfo.getFonts().get(name);
  1324. if (f instanceof LazyFont) {
  1325. if (((LazyFont) f).getRealFont() instanceof CIDFont) {
  1326. useMultiByte = true;
  1327. }
  1328. } else if (f instanceof CIDFont) {
  1329. useMultiByte = true;
  1330. }
  1331. // String startText = useMultiByte ? "<FEFF" : "(";
  1332. String startText = useMultiByte ? "<" : "(";
  1333. String endText = useMultiByte ? "> " : ") ";
  1334. AffineTransform trans = getTransform();
  1335. //trans.translate(x, y);
  1336. double[] vals = new double[6];
  1337. trans.getMatrix(vals);
  1338. concatMatrix(vals);
  1339. Shape imclip = getClip();
  1340. writeClip(imclip);
  1341. currentStream.write("BT\n");
  1342. AffineTransform localTransform = new AffineTransform();
  1343. localTransform.translate(x, y);
  1344. if (fontTransform != null) {
  1345. localTransform.concatenate(fontTransform);
  1346. }
  1347. localTransform.scale(1, -1);
  1348. double[] lt = new double[6];
  1349. localTransform.getMatrix(lt);
  1350. currentStream.write(PDFNumber.doubleOut(lt[0]) + " "
  1351. + PDFNumber.doubleOut(lt[1]) + " " + PDFNumber.doubleOut(lt[2]) + " "
  1352. + PDFNumber.doubleOut(lt[3]) + " " + PDFNumber.doubleOut(lt[4]) + " "
  1353. + PDFNumber.doubleOut(lt[5]) + " Tm [" + startText);
  1354. int l = s.length();
  1355. for (int i = 0; i < l; i++) {
  1356. char ch = fontState.mapChar(s.charAt(i));
  1357. if (!useMultiByte) {
  1358. if (ch > 127) {
  1359. currentStream.write("\\");
  1360. currentStream.write(Integer.toOctalString((int)ch));
  1361. } else {
  1362. switch (ch) {
  1363. case '(':
  1364. case ')':
  1365. case '\\':
  1366. currentStream.write("\\");
  1367. break;
  1368. }
  1369. currentStream.write(ch);
  1370. }
  1371. } else {
  1372. currentStream.write(PDFText.toUnicodeHex(ch));
  1373. }
  1374. if (kerningAvailable && (i + 1) < l) {
  1375. addKerning(currentStream, (new Integer((int)ch)),
  1376. (new Integer((int)fontState.mapChar(s.charAt(i + 1)))),
  1377. kerning, startText, endText);
  1378. }
  1379. }
  1380. currentStream.write(endText);
  1381. currentStream.write("] TJ\n");
  1382. currentStream.write("ET\n");
  1383. currentStream.write("Q\n");
  1384. }
  1385. private void addKerning(StringWriter buf, Integer ch1, Integer ch2,
  1386. Map kerning, String startText,
  1387. String endText) {
  1388. preparePainting();
  1389. Map kernPair = (Map)kerning.get(ch1);
  1390. if (kernPair != null) {
  1391. Integer width = (Integer)kernPair.get(ch2);
  1392. if (width != null) {
  1393. currentStream.write(endText + (-width.intValue()) + " " + startText);
  1394. }
  1395. }
  1396. }
  1397. /**
  1398. * Renders the text of the specified iterator, using the
  1399. * <code>Graphics2D</code> context's current <code>Paint</code>. The
  1400. * iterator must specify a font
  1401. * for each character. The baseline of the
  1402. * first character is at position (<i>x</i>,&nbsp;<i>y</i>) in the
  1403. * User Space.
  1404. * The rendering attributes applied include the <code>Clip</code>,
  1405. * <code>Transform</code>, <code>Paint</code>, and
  1406. * <code>Composite</code> attributes.
  1407. * For characters in script systems such as Hebrew and Arabic,
  1408. * the glyphs can be rendered from right to left, in which case the
  1409. * coordinate supplied is the location of the leftmost character
  1410. * on the baseline.
  1411. * @param iterator the iterator whose text is to be rendered
  1412. * @param x the coordinate where the iterator's text is to be
  1413. * rendered
  1414. * @param y the coordinate where the iterator's text is to be
  1415. * rendered
  1416. * @see #setPaint
  1417. * @see java.awt.Graphics#setColor
  1418. * @see #setTransform
  1419. * @see #setComposite
  1420. * @see #setClip
  1421. */
  1422. public void drawString(AttributedCharacterIterator iterator, float x,
  1423. float y) {
  1424. preparePainting();
  1425. Font fontState = null;
  1426. Shape imclip = getClip();
  1427. writeClip(imclip);
  1428. Color c = getColor();
  1429. applyColor(c, true);
  1430. applyPaint(getPaint(), true);
  1431. boolean fill = true;
  1432. boolean stroke = false;
  1433. if (true) {
  1434. Stroke currentStroke = getStroke();
  1435. stroke = true;
  1436. applyStroke(currentStroke);
  1437. applyColor(c, false);
  1438. applyPaint(getPaint(), false);
  1439. }
  1440. currentStream.write("BT\n");
  1441. // set text rendering mode:
  1442. // 0 - fill, 1 - stroke, 2 - fill then stroke
  1443. int textr = 0;
  1444. if (fill && stroke) {
  1445. textr = 2;
  1446. } else if (stroke) {
  1447. textr = 1;
  1448. }
  1449. currentStream.write(textr + " Tr\n");
  1450. AffineTransform trans = getTransform();
  1451. trans.translate(x, y);
  1452. double[] vals = new double[6];
  1453. trans.getMatrix(vals);
  1454. for (char ch = iterator.first(); ch != CharacterIterator.DONE;
  1455. ch = iterator.next()) {
  1456. //Map attr = iterator.getAttributes();
  1457. String name = fontState.getFontName();
  1458. int size = fontState.getFontSize();
  1459. if ((!name.equals(this.currentFontName))
  1460. || (size != this.currentFontSize)) {
  1461. this.currentFontName = name;
  1462. this.currentFontSize = size;
  1463. currentStream.write("/" + name + " " + (size / 1000)
  1464. + " Tf\n");
  1465. }
  1466. currentStream.write(PDFNumber.doubleOut(vals[0], DEC) + " "
  1467. + PDFNumber.doubleOut(vals[1], DEC) + " "
  1468. + PDFNumber.doubleOut(vals[2], DEC) + " "
  1469. + PDFNumber.doubleOut(vals[3], DEC) + " "
  1470. + PDFNumber.doubleOut(vals[4], DEC) + " "
  1471. + PDFNumber.doubleOut(vals[5], DEC) + " Tm (" + ch
  1472. + ") Tj\n");
  1473. }
  1474. currentStream.write("ET\n");
  1475. }
  1476. /**
  1477. * Fills the interior of a <code>Shape</code> using the settings of the
  1478. * <code>Graphics2D</code> context. The rendering attributes applied
  1479. * include the <code>Clip</code>, <code>Transform</code>,
  1480. * <code>Paint</code>, and <code>Composite</code>.
  1481. * @param s the <code>Shape</code> to be filled
  1482. * @see #setPaint
  1483. * @see java.awt.Graphics#setColor
  1484. * @see #transform
  1485. * @see #setTransform
  1486. * @see #setComposite
  1487. * @see #clip
  1488. * @see #setClip
  1489. */
  1490. public void fill(Shape s) {
  1491. preparePainting();
  1492. //Transparency shortcut
  1493. Color c;
  1494. c = getBackground();
  1495. if (c.getAlpha() == 0) {
  1496. c = getColor();
  1497. if (c.getAlpha() == 0) {
  1498. return;
  1499. }
  1500. }
  1501. AffineTransform trans = getTransform();
  1502. double[] tranvals = new double[6];
  1503. trans.getMatrix(tranvals);
  1504. Shape imclip = getClip();
  1505. boolean newClip = graphicsState.checkClip(imclip);
  1506. boolean newTransform = graphicsState.checkTransform(trans)
  1507. && !trans.isIdentity();
  1508. if (newClip || newTransform) {
  1509. currentStream.write("q\n");
  1510. graphicsState.push();
  1511. if (newTransform) {
  1512. concatMatrix(tranvals);
  1513. }
  1514. if (newClip) {
  1515. writeClip(imclip);
  1516. }
  1517. }
  1518. if (c.getAlpha() != 255) {
  1519. Map vals = new java.util.HashMap();
  1520. vals.put(PDFGState.GSTATE_ALPHA_NONSTROKE,
  1521. new Float(c.getAlpha() / 255f));
  1522. PDFGState gstate = pdfDoc.getFactory().makeGState(
  1523. vals, graphicsState.getGState());
  1524. resourceContext.addGState(gstate);
  1525. currentStream.write("/" + gstate.getName() + " gs\n");
  1526. }
  1527. c = getColor();
  1528. if (graphicsState.setColor(c)) {
  1529. applyColor(c, true);
  1530. }
  1531. c = getBackground();
  1532. if (graphicsState.setBackColor(c)) {
  1533. applyColor(c, false);
  1534. }
  1535. Paint paint = getPaint();
  1536. if (graphicsState.setPaint(paint)) {
  1537. if (!applyPaint(paint, true)) {
  1538. // Use the shape to 'clip' the paint contents.
  1539. applyUnknownPaint(paint, s);
  1540. if (newClip || newTransform) {
  1541. currentStream.write("Q\n");
  1542. graphicsState.pop();
  1543. }
  1544. return;
  1545. }
  1546. }
  1547. //PathIterator iter = s.getPathIterator(getTransform());
  1548. PathIterator iter = s.getPathIterator(IDENTITY_TRANSFORM);
  1549. processPathIterator(iter);
  1550. doDrawing(true, false,
  1551. iter.getWindingRule() == PathIterator.WIND_EVEN_ODD);
  1552. if (newClip || newTransform) {
  1553. currentStream.write("Q\n");
  1554. graphicsState.pop();
  1555. }
  1556. }
  1557. /**
  1558. * Processes a path iterator generating the necessary painting operations.
  1559. * @param iter PathIterator to process
  1560. */
  1561. public void processPathIterator(PathIterator iter) {
  1562. while (!iter.isDone()) {
  1563. double[] vals = new double[6];
  1564. int type = iter.currentSegment(vals);
  1565. switch (type) {
  1566. case PathIterator.SEG_CUBICTO:
  1567. currentStream.write(PDFNumber.doubleOut(vals[0], DEC) + " "
  1568. + PDFNumber.doubleOut(vals[1], DEC) + " "
  1569. + PDFNumber.doubleOut(vals[2], DEC) + " "
  1570. + PDFNumber.doubleOut(vals[3], DEC) + " "
  1571. + PDFNumber.doubleOut(vals[4], DEC) + " "
  1572. + PDFNumber.doubleOut(vals[5], DEC) + " c\n");
  1573. break;
  1574. case PathIterator.SEG_LINETO:
  1575. currentStream.write(PDFNumber.doubleOut(vals[0], DEC) + " "
  1576. + PDFNumber.doubleOut(vals[1], DEC) + " l\n");
  1577. break;
  1578. case PathIterator.SEG_MOVETO:
  1579. currentStream.write(PDFNumber.doubleOut(vals[0], DEC) + " "
  1580. + PDFNumber.doubleOut(vals[1], DEC) + " m\n");
  1581. break;
  1582. case PathIterator.SEG_QUADTO:
  1583. currentStream.write(PDFNumber.doubleOut(vals[0], DEC) + " "
  1584. + PDFNumber.doubleOut(vals[1], DEC) + " "
  1585. + PDFNumber.doubleOut(vals[2], DEC) + " "
  1586. + PDFNumber.doubleOut(vals[3], DEC) + " y\n");
  1587. break;
  1588. case PathIterator.SEG_CLOSE:
  1589. currentStream.write("h\n");
  1590. break;
  1591. default:
  1592. break;
  1593. }
  1594. iter.next();
  1595. }
  1596. }
  1597. /**
  1598. * Do the PDF drawing command.
  1599. * This does the PDF drawing command according to fill
  1600. * stroke and winding rule.
  1601. *
  1602. * @param fill true if filling the path
  1603. * @param stroke true if stroking the path
  1604. * @param nonzero true if using the non-zero winding rule
  1605. */
  1606. protected void doDrawing(boolean fill, boolean stroke, boolean nonzero) {
  1607. preparePainting();
  1608. if (fill) {
  1609. if (stroke) {
  1610. if (nonzero) {
  1611. currentStream.write("B*\n");
  1612. } else {
  1613. currentStream.write("B\n");
  1614. }
  1615. } else {
  1616. if (nonzero) {
  1617. currentStream.write("f*\n");
  1618. } else {
  1619. currentStream.write("f\n");
  1620. }
  1621. }
  1622. } else {
  1623. // if (stroke)
  1624. currentStream.write("S\n");
  1625. }
  1626. }
  1627. /**
  1628. * Returns the device configuration associated with this
  1629. * <code>Graphics2D</code>.
  1630. *
  1631. * @return the PDF graphics configuration
  1632. */
  1633. public GraphicsConfiguration getDeviceConfiguration() {
  1634. return new PDFGraphicsConfiguration();
  1635. }
  1636. /**
  1637. * Used to create proper font metrics
  1638. */
  1639. private Graphics2D fmg;
  1640. {
  1641. BufferedImage bi = new BufferedImage(1, 1,
  1642. BufferedImage.TYPE_INT_ARGB);
  1643. fmg = bi.createGraphics();
  1644. }
  1645. /**
  1646. * Gets the font metrics for the specified font.
  1647. * @return the font metrics for the specified font.
  1648. * @param f the specified font
  1649. * @see java.awt.Graphics#getFont
  1650. * @see java.awt.FontMetrics
  1651. * @see java.awt.Graphics#getFontMetrics()
  1652. */
  1653. public java.awt.FontMetrics getFontMetrics(java.awt.Font f) {
  1654. return fmg.getFontMetrics(f);
  1655. }
  1656. /**
  1657. * Sets the paint mode of this graphics context to alternate between
  1658. * this graphics context's current color and the new specified color.
  1659. * This specifies that logical pixel operations are performed in the
  1660. * XOR mode, which alternates pixels between the current color and
  1661. * a specified XOR color.
  1662. * <p>
  1663. * When drawing operations are performed, pixels which are the
  1664. * current color are changed to the specified color, and vice versa.
  1665. * <p>
  1666. * Pixels that are of colors other than those two colors are changed
  1667. * in an unpredictable but reversible manner; if the same figure is
  1668. * drawn twice, then all pixels are restored to their original values.
  1669. * @param c1 the XOR alternation color
  1670. */
  1671. public void setXORMode(Color c1) {
  1672. //NYI
  1673. }
  1674. /**
  1675. * Copies an area of the component by a distance specified by
  1676. * <code>dx</code> and <code>dy</code>. From the point specified
  1677. * by <code>x</code> and <code>y</code>, this method
  1678. * copies downwards and to the right. To copy an area of the
  1679. * component to the left or upwards, specify a negative value for
  1680. * <code>dx</code> or <code>dy</code>.
  1681. * If a portion of the source rectangle lies outside the bounds
  1682. * of the component, or is obscured by another window or component,
  1683. * <code>copyArea</code> will be unable to copy the associated
  1684. * pixels. The area that is omitted can be refreshed by calling
  1685. * the component's <code>paint</code> method.
  1686. * @param x the <i>x</i> coordinate of the source rectangle.
  1687. * @param y the <i>y</i> coordinate of the source rectangle.
  1688. * @param width the width of the source rectangle.
  1689. * @param height the height of the source rectangle.
  1690. * @param dx the horizontal distance to copy the pixels.
  1691. * @param dy the vertical distance to copy the pixels.
  1692. */
  1693. public void copyArea(int x, int y, int width, int height, int dx,
  1694. int dy) {
  1695. //NYI
  1696. }
  1697. }