PageRenderTime 61ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/components/forks/poi/src/loci/poi/hssf/usermodel/EscherGraphics2d.java

http://github.com/openmicroscopy/bioformats
Java | 634 lines | 436 code | 89 blank | 109 comment | 23 complexity | 3992ca0664b8dfcbb274eac5ed29285e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.hssf.usermodel;
  38. import loci.poi.util.POILogFactory;
  39. import loci.poi.util.POILogger;
  40. import java.awt.*;
  41. import java.awt.font.FontRenderContext;
  42. import java.awt.font.GlyphVector;
  43. import java.awt.font.TextLayout;
  44. import java.awt.geom.AffineTransform;
  45. import java.awt.geom.Area;
  46. import java.awt.geom.GeneralPath;
  47. import java.awt.geom.Line2D;
  48. import java.awt.image.BufferedImage;
  49. import java.awt.image.BufferedImageOp;
  50. import java.awt.image.ImageObserver;
  51. import java.awt.image.RenderedImage;
  52. import java.awt.image.renderable.RenderableImage;
  53. import java.text.AttributedCharacterIterator;
  54. import java.util.Map;
  55. /**
  56. * Translates Graphics2d calls into escher calls. The translation is lossy so
  57. * many features are not supported and some just aren't implemented yet. If
  58. * in doubt test the specific calls you wish to make. Graphics calls are
  59. * always drawn into an EscherGroup so one will need to be created.
  60. * <p>
  61. * <b>Important:</b>
  62. * <blockquote>
  63. * One important concept worth considering is that of font size. One of the
  64. * difficulties in converting Graphics calls into escher drawing calls is that
  65. * Excel does not have the concept of absolute pixel positions. It measures
  66. * it's cell widths in 'characters' and the cell heights in points.
  67. * Unfortunately it's not defined exactly what a type of character it's
  68. * measuring. Presumably this is due to the fact that the Excel will be
  69. * using different fonts on different platforms or even within the same
  70. * platform.
  71. * <p>
  72. * Because of this constraint you have to calculate the verticalPointsPerPixel.
  73. * This the amount the font should be scaled by when
  74. * you issue commands such as drawString(). A good way to calculate this
  75. * is to use the follow formula:
  76. * <p>
  77. * <pre>
  78. * multipler = groupHeightInPoints / heightOfGroup
  79. * </pre>
  80. * <p>
  81. * The height of the group is calculated fairly simply by calculating the
  82. * difference between the y coordinates of the bounding box of the shape. The
  83. * height of the group can be calculated by using a convenience called
  84. * <code>HSSFClientAnchor.getAnchorHeightInPoints()</code>.
  85. * </blockquote>
  86. *
  87. * @author Glen Stampoultzis (glens at apache.org)
  88. */
  89. public class EscherGraphics2d extends Graphics2D
  90. {
  91. private EscherGraphics escherGraphics;
  92. private BufferedImage img;
  93. private AffineTransform trans;
  94. private Stroke stroke;
  95. private Paint paint;
  96. private Shape deviceclip;
  97. private POILogger logger = POILogFactory.getLogger(getClass());
  98. /**
  99. * Constructs one escher graphics object from an escher graphics object.
  100. *
  101. * @param escherGraphics the original EscherGraphics2d object to copy
  102. */
  103. public EscherGraphics2d(EscherGraphics escherGraphics)
  104. {
  105. this.escherGraphics = escherGraphics;
  106. setImg( new BufferedImage(1, 1, 2) );
  107. setColor(Color.black);
  108. }
  109. public void addRenderingHints(Map map)
  110. {
  111. getG2D().addRenderingHints(map);
  112. }
  113. public void clearRect(int i, int j, int k, int l)
  114. {
  115. Paint paint1 = getPaint();
  116. setColor(getBackground());
  117. fillRect(i, j, k, l);
  118. setPaint(paint1);
  119. }
  120. public void clip(Shape shape)
  121. {
  122. if(getDeviceclip() != null)
  123. {
  124. Area area = new Area(getClip());
  125. if(shape != null)
  126. area.intersect(new Area(shape));
  127. shape = area;
  128. }
  129. setClip(shape);
  130. }
  131. public void clipRect(int x, int y, int width, int height)
  132. {
  133. clip(new Rectangle(x,y,width,height));
  134. }
  135. public void copyArea(int x, int y, int width, int height,
  136. int dx, int dy)
  137. {
  138. getG2D().copyArea(x,y,width,height,dx,dy);
  139. }
  140. public Graphics create()
  141. {
  142. EscherGraphics2d g2d = new EscherGraphics2d(escherGraphics);
  143. return g2d;
  144. }
  145. public void dispose()
  146. {
  147. getEscherGraphics().dispose();
  148. getG2D().dispose();
  149. getImg().flush();
  150. }
  151. public void draw(Shape shape)
  152. {
  153. if (shape instanceof Line2D)
  154. {
  155. Line2D shape2d = (Line2D) shape;
  156. int width = 0;
  157. if (stroke != null && stroke instanceof BasicStroke) {
  158. width = (int) ((BasicStroke)stroke).getLineWidth() * 12700;
  159. }
  160. drawLine((int)shape2d.getX1(), (int)shape2d.getY1(), (int)shape2d.getX2(), (int)shape2d.getY2(), width);
  161. }
  162. else
  163. {
  164. if (logger.check(POILogger.WARN))
  165. logger.log(POILogger.WARN, "draw not fully supported");
  166. }
  167. }
  168. public void drawArc(int x, int y, int width, int height,
  169. int startAngle, int arcAngle)
  170. {
  171. draw(new java.awt.geom.Arc2D.Float(x, y, width, height, startAngle, arcAngle, 0));
  172. }
  173. public void drawGlyphVector(GlyphVector g, float x, float y)
  174. {
  175. fill(g.getOutline(x, y));
  176. }
  177. public boolean drawImage(Image image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1,
  178. int sx2, int sy2, Color bgColor, ImageObserver imageobserver)
  179. {
  180. if (logger.check( POILogger.WARN ))
  181. logger.log(POILogger.WARN,"drawImage() not supported");
  182. return true;
  183. }
  184. public boolean drawImage(Image image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1,
  185. int sx2, int sy2, ImageObserver imageobserver)
  186. {
  187. if (logger.check( POILogger.WARN ))
  188. logger.log(POILogger.WARN,"drawImage() not supported");
  189. return drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null, imageobserver);
  190. }
  191. public boolean drawImage(Image image, int dx1, int dy1, int dx2, int dy2, Color bgColor, ImageObserver imageobserver)
  192. {
  193. if (logger.check( POILogger.WARN ))
  194. logger.log(POILogger.WARN,"drawImage() not supported");
  195. return true;
  196. }
  197. public boolean drawImage(Image img, int x, int y,
  198. int width, int height,
  199. ImageObserver observer)
  200. {
  201. return drawImage(img, x,y,width,height, null, observer);
  202. }
  203. public boolean drawImage(Image image, int x, int y, Color bgColor, ImageObserver imageobserver)
  204. {
  205. return drawImage(image, x, y, image.getWidth(imageobserver), image.getHeight(imageobserver), bgColor, imageobserver);
  206. }
  207. public boolean drawImage(Image image, int x, int y, ImageObserver imageobserver)
  208. {
  209. return drawImage(image, x, y, image.getWidth(imageobserver), image.getHeight(imageobserver), imageobserver);
  210. }
  211. public boolean drawImage(Image image, AffineTransform affinetransform, ImageObserver imageobserver)
  212. {
  213. AffineTransform affinetransform1 = (AffineTransform)getTrans().clone();
  214. getTrans().concatenate(affinetransform);
  215. drawImage(image, 0, 0, imageobserver);
  216. setTrans( affinetransform1 );
  217. return true;
  218. }
  219. public void drawImage(BufferedImage bufferedimage, BufferedImageOp op, int x, int y)
  220. {
  221. BufferedImage img = op.filter(bufferedimage, null);
  222. drawImage(((Image) (img)), new AffineTransform(1.0F, 0.0F, 0.0F, 1.0F, x, y), null);
  223. }
  224. public void drawLine(int x1, int y1, int x2, int y2, int width)
  225. {
  226. getEscherGraphics().drawLine(x1,y1,x2,y2, width);
  227. }
  228. public void drawLine(int x1, int y1, int x2, int y2)
  229. {
  230. int width = 0;
  231. if (stroke != null && stroke instanceof BasicStroke) {
  232. width = (int) ((BasicStroke)stroke).getLineWidth() * 12700;
  233. }
  234. getEscherGraphics().drawLine(x1,y1,x2,y2, width);
  235. // draw(new GeneralPath(new java.awt.geom.Line2D.Float(x1, y1, x2, y2)));
  236. }
  237. public void drawOval(int x, int y, int width, int height)
  238. {
  239. getEscherGraphics().drawOval(x,y,width,height);
  240. // draw(new java.awt.geom.Ellipse2D.Float(x, y, width, height));
  241. }
  242. public void drawPolygon(int xPoints[], int yPoints[],
  243. int nPoints)
  244. {
  245. getEscherGraphics().drawPolygon(xPoints, yPoints, nPoints);
  246. }
  247. public void drawPolyline(int xPoints[], int yPoints[], int nPoints)
  248. {
  249. if(nPoints > 0)
  250. {
  251. GeneralPath generalpath = new GeneralPath();
  252. generalpath.moveTo(xPoints[0], yPoints[0]);
  253. for(int j = 1; j < nPoints; j++)
  254. generalpath.lineTo(xPoints[j], yPoints[j]);
  255. draw(generalpath);
  256. }
  257. }
  258. public void drawRect(int x, int y, int width, int height)
  259. {
  260. escherGraphics.drawRect(x,y,width,height);
  261. }
  262. public void drawRenderableImage(RenderableImage renderableimage, AffineTransform affinetransform)
  263. {
  264. drawRenderedImage(renderableimage.createDefaultRendering(), affinetransform);
  265. }
  266. public void drawRenderedImage(RenderedImage renderedimage, AffineTransform affinetransform)
  267. {
  268. BufferedImage bufferedimage = new BufferedImage(renderedimage.getColorModel(), renderedimage.getData().createCompatibleWritableRaster(), false, null);
  269. bufferedimage.setData(renderedimage.getData());
  270. drawImage(bufferedimage, affinetransform, null);
  271. }
  272. public void drawRoundRect(int i, int j, int k, int l, int i1, int j1)
  273. {
  274. draw(new java.awt.geom.RoundRectangle2D.Float(i, j, k, l, i1, j1));
  275. }
  276. public void drawString(String string, float x, float y)
  277. {
  278. getEscherGraphics().drawString(string, (int)x, (int)y);
  279. }
  280. public void drawString(String string, int x, int y)
  281. {
  282. getEscherGraphics().drawString(string, x, y);
  283. }
  284. public void drawString(AttributedCharacterIterator attributedcharacteriterator, float x, float y)
  285. {
  286. TextLayout textlayout = new TextLayout(attributedcharacteriterator, getFontRenderContext());
  287. Paint paint1 = getPaint();
  288. setColor(getColor());
  289. fill(textlayout.getOutline(AffineTransform.getTranslateInstance(x, y)));
  290. setPaint(paint1);
  291. }
  292. public void drawString(AttributedCharacterIterator attributedcharacteriterator, int x, int y)
  293. {
  294. drawString(attributedcharacteriterator, x, y);
  295. }
  296. public void fill(Shape shape)
  297. {
  298. if (logger.check( POILogger.WARN ))
  299. logger.log(POILogger.WARN,"fill(Shape) not supported");
  300. }
  301. public void fillArc(int i, int j, int k, int l, int i1, int j1)
  302. {
  303. fill(new java.awt.geom.Arc2D.Float(i, j, k, l, i1, j1, 2));
  304. }
  305. public void fillOval(int x, int y, int width, int height)
  306. {
  307. escherGraphics.fillOval(x,y,width,height);
  308. }
  309. /**
  310. * Fills a (closed) polygon, as defined by a pair of arrays, which
  311. * hold the <i>x</i> and <i>y</i> coordinates.
  312. * <p>
  313. * This draws the polygon, with <code>nPoint</code> line segments.
  314. * The first <code>nPoint&nbsp;-&nbsp;1</code> line segments are
  315. * drawn between sequential points
  316. * (<code>xPoints[i],yPoints[i],xPoints[i+1],yPoints[i+1]</code>).
  317. * The final line segment is a closing one, from the last point to
  318. * the first (assuming they are different).
  319. * <p>
  320. * The area inside of the polygon is defined by using an
  321. * even-odd fill rule (also known as the alternating rule), and
  322. * the area inside of it is filled.
  323. * @param xPoints array of the <code>x</code> coordinates.
  324. * @param yPoints array of the <code>y</code> coordinates.
  325. * @param nPoints the total number of points in the polygon.
  326. * @see java.awt.Graphics#drawPolygon(int[], int[], int)
  327. */
  328. public void fillPolygon(int xPoints[], int yPoints[], int nPoints)
  329. {
  330. escherGraphics.fillPolygon(xPoints, yPoints, nPoints);
  331. }
  332. public void fillRect(int x, int y, int width, int height)
  333. {
  334. getEscherGraphics().fillRect(x,y,width,height);
  335. }
  336. public void fillRoundRect(int x, int y, int width, int height,
  337. int arcWidth, int arcHeight)
  338. {
  339. fill(new java.awt.geom.RoundRectangle2D.Float(x, y, width, height, arcWidth, arcHeight));
  340. }
  341. public Color getBackground()
  342. {
  343. return getEscherGraphics().getBackground();
  344. }
  345. public Shape getClip()
  346. {
  347. try
  348. {
  349. return getTrans().createInverse().createTransformedShape(getDeviceclip());
  350. }
  351. catch(Exception _ex)
  352. {
  353. return null;
  354. }
  355. }
  356. public Rectangle getClipBounds()
  357. {
  358. if(getDeviceclip() != null)
  359. return getClip().getBounds();
  360. else
  361. return null;
  362. }
  363. public Color getColor()
  364. {
  365. return escherGraphics.getColor();
  366. }
  367. public Composite getComposite()
  368. {
  369. return getG2D().getComposite();
  370. }
  371. public GraphicsConfiguration getDeviceConfiguration()
  372. {
  373. return getG2D().getDeviceConfiguration();
  374. }
  375. public Font getFont()
  376. {
  377. return getEscherGraphics().getFont();
  378. }
  379. public FontMetrics getFontMetrics(Font font)
  380. {
  381. return getEscherGraphics().getFontMetrics(font);
  382. }
  383. public FontRenderContext getFontRenderContext()
  384. {
  385. getG2D().setTransform(getTrans());
  386. return getG2D().getFontRenderContext();
  387. }
  388. public Paint getPaint()
  389. {
  390. return paint;
  391. }
  392. public Object getRenderingHint(java.awt.RenderingHints.Key key)
  393. {
  394. return getG2D().getRenderingHint(key);
  395. }
  396. public RenderingHints getRenderingHints()
  397. {
  398. return getG2D().getRenderingHints();
  399. }
  400. public Stroke getStroke()
  401. {
  402. return stroke;
  403. }
  404. public AffineTransform getTransform()
  405. {
  406. return (AffineTransform)getTrans().clone();
  407. }
  408. public boolean hit(Rectangle rectangle, Shape shape, boolean flag)
  409. {
  410. getG2D().setTransform(getTrans());
  411. getG2D().setStroke(getStroke());
  412. getG2D().setClip(getClip());
  413. return getG2D().hit(rectangle, shape, flag);
  414. }
  415. public void rotate(double d)
  416. {
  417. getTrans().rotate(d);
  418. }
  419. public void rotate(double d, double d1, double d2)
  420. {
  421. getTrans().rotate(d, d1, d2);
  422. }
  423. public void scale(double d, double d1)
  424. {
  425. getTrans().scale(d, d1);
  426. }
  427. public void setBackground(Color c)
  428. {
  429. getEscherGraphics().setBackground(c);
  430. }
  431. public void setClip(int i, int j, int k, int l)
  432. {
  433. setClip(((Shape) (new Rectangle(i, j, k, l))));
  434. }
  435. public void setClip(Shape shape)
  436. {
  437. setDeviceclip( getTrans().createTransformedShape(shape) );
  438. }
  439. public void setColor(Color c)
  440. {
  441. escherGraphics.setColor(c);
  442. }
  443. public void setComposite(Composite composite)
  444. {
  445. getG2D().setComposite(composite);
  446. }
  447. public void setFont(Font font)
  448. {
  449. getEscherGraphics().setFont(font);
  450. }
  451. public void setPaint(Paint paint1)
  452. {
  453. if(paint1 != null)
  454. {
  455. paint = paint1;
  456. if(paint1 instanceof Color)
  457. setColor( (Color)paint1 );
  458. }
  459. }
  460. public void setPaintMode()
  461. {
  462. getEscherGraphics().setPaintMode();
  463. }
  464. public void setRenderingHint(java.awt.RenderingHints.Key key, Object obj)
  465. {
  466. getG2D().setRenderingHint(key, obj);
  467. }
  468. public void setRenderingHints(Map map)
  469. {
  470. getG2D().setRenderingHints(map);
  471. }
  472. public void setStroke(Stroke s)
  473. {
  474. stroke = s;
  475. }
  476. public void setTransform(AffineTransform affinetransform)
  477. {
  478. setTrans( (AffineTransform)affinetransform.clone() );
  479. }
  480. public void setXORMode(Color color1)
  481. {
  482. getEscherGraphics().setXORMode(color1);
  483. }
  484. public void shear(double d, double d1)
  485. {
  486. getTrans().shear(d, d1);
  487. }
  488. public void transform(AffineTransform affinetransform)
  489. {
  490. getTrans().concatenate(affinetransform);
  491. }
  492. // Image transformImage(Image image, Rectangle rectangle, Rectangle rectangle1, ImageObserver imageobserver, Color color1)
  493. // {
  494. // logger.log(POILogger.WARN,"transformImage() not supported");
  495. // return null;
  496. // }
  497. //
  498. // Image transformImage(Image image, int ai[], Rectangle rectangle, ImageObserver imageobserver, Color color1)
  499. // {
  500. // logger.log(POILogger.WARN,"transformImage() not supported");
  501. // return null;
  502. // }
  503. public void translate(double d, double d1)
  504. {
  505. getTrans().translate(d, d1);
  506. }
  507. public void translate(int i, int j)
  508. {
  509. getTrans().translate(i, j);
  510. }
  511. private EscherGraphics getEscherGraphics()
  512. {
  513. return escherGraphics;
  514. }
  515. private BufferedImage getImg()
  516. {
  517. return img;
  518. }
  519. private void setImg( BufferedImage img )
  520. {
  521. this.img = img;
  522. }
  523. private Graphics2D getG2D()
  524. {
  525. return (Graphics2D) img.getGraphics();
  526. }
  527. private AffineTransform getTrans()
  528. {
  529. return trans;
  530. }
  531. private void setTrans( AffineTransform trans )
  532. {
  533. this.trans = trans;
  534. }
  535. private Shape getDeviceclip()
  536. {
  537. return deviceclip;
  538. }
  539. private void setDeviceclip( Shape deviceclip )
  540. {
  541. this.deviceclip = deviceclip;
  542. }
  543. }