PageRenderTime 86ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/ppt/poi/org/apache/poi/hssf/usermodel/EscherGraphics2d.java

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