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

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