PageRenderTime 66ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/openmicroscopy/bioformats
Java | 530 lines | 349 code | 59 blank | 122 comment | 29 complexity | 90bc5f20331e7dcb9050bcbf7dd0d9c3 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.hssf.util.HSSFColor;
  39. import loci.poi.util.POILogFactory;
  40. import loci.poi.util.POILogger;
  41. import java.awt.*;
  42. import java.awt.image.ImageObserver;
  43. import java.text.AttributedCharacterIterator;
  44. /**
  45. * Translates Graphics calls into escher calls. The translation is lossy so
  46. * many features are not supported and some just aren't implemented yet. If
  47. * in doubt test the specific calls you wish to make. Graphics calls are
  48. * always performed into an EscherGroup so one will need to be created.
  49. * <p>
  50. * <b>Important:</b>
  51. * <blockquote>
  52. * One important concept worth considering is that of font size. One of the
  53. * difficulties in converting Graphics calls into escher drawing calls is that
  54. * Excel does not have the concept of absolute pixel positions. It measures
  55. * it's cell widths in 'characters' and the cell heights in points.
  56. * Unfortunately it's not defined exactly what a type of character it's
  57. * measuring. Presumably this is due to the fact that the Excel will be
  58. * using different fonts on different platforms or even within the same
  59. * platform.
  60. * <p>
  61. * Because of this constraint we've had to calculate the
  62. * verticalPointsPerPixel. This the amount the font should be scaled by when
  63. * you issue commands such as drawString(). A good way to calculate this
  64. * is to use the follow formula:
  65. * <p>
  66. * <pre>
  67. * multipler = groupHeightInPoints / heightOfGroup
  68. * </pre>
  69. * <p>
  70. * The height of the group is calculated fairly simply by calculating the
  71. * difference between the y coordinates of the bounding box of the shape. The
  72. * height of the group can be calculated by using a convenience called
  73. * <code>HSSFClientAnchor.getAnchorHeightInPoints()</code>.
  74. * </blockquote>
  75. *
  76. * @author Glen Stampoultzis (glens at apache.org)
  77. */
  78. public class EscherGraphics
  79. extends Graphics
  80. {
  81. private HSSFShapeGroup escherGroup;
  82. private HSSFWorkbook workbook;
  83. private float verticalPointsPerPixel = 1.0f;
  84. private float verticalPixelsPerPoint;
  85. private Color foreground;
  86. private Color background = Color.white;
  87. private Font font;
  88. private static POILogger logger = POILogFactory.getLogger(EscherGraphics.class);
  89. /**
  90. * Construct an escher graphics object.
  91. *
  92. * @param escherGroup The escher group to write the graphics calls into.
  93. * @param workbook The workbook we are using.
  94. * @param forecolor The foreground color to use as default.
  95. * @param verticalPointsPerPixel The font multiplier. (See class description for information on how this works.).
  96. */
  97. public EscherGraphics(HSSFShapeGroup escherGroup, HSSFWorkbook workbook, Color forecolor, float verticalPointsPerPixel )
  98. {
  99. this.escherGroup = escherGroup;
  100. this.workbook = workbook;
  101. this.verticalPointsPerPixel = verticalPointsPerPixel;
  102. this.verticalPixelsPerPoint = 1 / verticalPointsPerPixel;
  103. this.font = new Font("Arial", 0, 10);
  104. this.foreground = forecolor;
  105. // background = backcolor;
  106. }
  107. /**
  108. * Constructs an escher graphics object.
  109. *
  110. * @param escherGroup The escher group to write the graphics calls into.
  111. * @param workbook The workbook we are using.
  112. * @param foreground The foreground color to use as default.
  113. * @param verticalPointsPerPixel The font multiplier. (See class description for information on how this works.).
  114. * @param font The font to use.
  115. */
  116. EscherGraphics( HSSFShapeGroup escherGroup, HSSFWorkbook workbook, Color foreground, Font font, float verticalPointsPerPixel )
  117. {
  118. this.escherGroup = escherGroup;
  119. this.workbook = workbook;
  120. this.foreground = foreground;
  121. // this.background = background;
  122. this.font = font;
  123. this.verticalPointsPerPixel = verticalPointsPerPixel;
  124. this.verticalPixelsPerPoint = 1 / verticalPointsPerPixel;
  125. }
  126. // /**
  127. // * Constructs an escher graphics object.
  128. // *
  129. // * @param escherGroup The escher group to write the graphics calls into.
  130. // * @param workbook The workbook we are using.
  131. // * @param forecolor The default foreground color.
  132. // */
  133. // public EscherGraphics( HSSFShapeGroup escherGroup, HSSFWorkbook workbook, Color forecolor)
  134. // {
  135. // this(escherGroup, workbook, forecolor, 1.0f);
  136. // }
  137. public void clearRect(int x, int y, int width, int height)
  138. {
  139. Color color = foreground;
  140. setColor(background);
  141. fillRect(x,y,width,height);
  142. setColor(color);
  143. }
  144. public void clipRect(int x, int y, int width, int height)
  145. {
  146. if (logger.check( POILogger.WARN ))
  147. logger.log(POILogger.WARN,"clipRect not supported");
  148. }
  149. public void copyArea(int x, int y, int width, int height, int dx, int dy)
  150. {
  151. if (logger.check( POILogger.WARN ))
  152. logger.log(POILogger.WARN,"copyArea not supported");
  153. }
  154. public Graphics create()
  155. {
  156. EscherGraphics g = new EscherGraphics(escherGroup, workbook,
  157. foreground, font, verticalPointsPerPixel );
  158. return g;
  159. }
  160. public void dispose()
  161. {
  162. }
  163. public void drawArc(int x, int y, int width, int height,
  164. int startAngle, int arcAngle)
  165. {
  166. if (logger.check( POILogger.WARN ))
  167. logger.log(POILogger.WARN,"drawArc not supported");
  168. }
  169. public boolean drawImage(Image img,
  170. int dx1, int dy1, int dx2, int dy2,
  171. int sx1, int sy1, int sx2, int sy2,
  172. Color bgcolor,
  173. ImageObserver observer)
  174. {
  175. if (logger.check( POILogger.WARN ))
  176. logger.log(POILogger.WARN,"drawImage not supported");
  177. return true;
  178. }
  179. public boolean drawImage(Image img,
  180. int dx1, int dy1, int dx2, int dy2,
  181. int sx1, int sy1, int sx2, int sy2,
  182. ImageObserver observer)
  183. {
  184. if (logger.check( POILogger.WARN ))
  185. logger.log(POILogger.WARN,"drawImage not supported");
  186. return true;
  187. }
  188. public boolean drawImage(Image image, int i, int j, int k, int l, Color color, ImageObserver imageobserver)
  189. {
  190. return drawImage(image, i, j, i + k, j + l, 0, 0, image.getWidth(imageobserver), image.getHeight(imageobserver), color, imageobserver);
  191. }
  192. public boolean drawImage(Image image, int i, int j, int k, int l, ImageObserver imageobserver)
  193. {
  194. return drawImage(image, i, j, i + k, j + l, 0, 0, image.getWidth(imageobserver), image.getHeight(imageobserver), imageobserver);
  195. }
  196. public boolean drawImage(Image image, int i, int j, Color color, ImageObserver imageobserver)
  197. {
  198. return drawImage(image, i, j, image.getWidth(imageobserver), image.getHeight(imageobserver), color, imageobserver);
  199. }
  200. public boolean drawImage(Image image, int i, int j, ImageObserver imageobserver)
  201. {
  202. return drawImage(image, i, j, image.getWidth(imageobserver), image.getHeight(imageobserver), imageobserver);
  203. }
  204. public void drawLine(int x1, int y1, int x2, int y2)
  205. {
  206. drawLine(x1,y1,x2,y2,0);
  207. }
  208. public void drawLine(int x1, int y1, int x2, int y2, int width)
  209. {
  210. HSSFSimpleShape shape = escherGroup.createShape(new HSSFChildAnchor(x1, y1, x2, y2) );
  211. shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
  212. shape.setLineWidth(width);
  213. shape.setLineStyleColor(foreground.getRed(), foreground.getGreen(), foreground.getBlue());
  214. }
  215. public void drawOval(int x, int y, int width, int height)
  216. {
  217. HSSFSimpleShape shape = escherGroup.createShape(new HSSFChildAnchor(x,y,x+width,y+height) );
  218. shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
  219. shape.setLineWidth(0);
  220. shape.setLineStyleColor(foreground.getRed(), foreground.getGreen(), foreground.getBlue());
  221. shape.setNoFill(true);
  222. }
  223. public void drawPolygon(int xPoints[], int yPoints[],
  224. int nPoints)
  225. {
  226. int right = findBiggest(xPoints);
  227. int bottom = findBiggest(yPoints);
  228. int left = findSmallest(xPoints);
  229. int top = findSmallest(yPoints);
  230. HSSFPolygon shape = escherGroup.createPolygon(new HSSFChildAnchor(left,top,right,bottom) );
  231. shape.setPolygonDrawArea(right - left, bottom - top);
  232. shape.setPoints(addToAll(xPoints, -left), addToAll(yPoints, -top));
  233. shape.setLineStyleColor(foreground.getRed(), foreground.getGreen(), foreground.getBlue());
  234. shape.setLineWidth(0);
  235. shape.setNoFill(true);
  236. }
  237. private int[] addToAll( int[] values, int amount )
  238. {
  239. int[] result = new int[values.length];
  240. for ( int i = 0; i < values.length; i++ )
  241. result[i] = values[i] + amount;
  242. return result;
  243. }
  244. public void drawPolyline(int xPoints[], int yPoints[],
  245. int nPoints)
  246. {
  247. if (logger.check( POILogger.WARN ))
  248. logger.log(POILogger.WARN,"drawPolyline not supported");
  249. }
  250. public void drawRect(int x, int y, int width, int height)
  251. {
  252. if (logger.check( POILogger.WARN ))
  253. logger.log(POILogger.WARN,"drawRect not supported");
  254. }
  255. public void drawRoundRect(int x, int y, int width, int height,
  256. int arcWidth, int arcHeight)
  257. {
  258. if (logger.check( POILogger.WARN ))
  259. logger.log(POILogger.WARN,"drawRoundRect not supported");
  260. }
  261. public void drawString(String str, int x, int y)
  262. {
  263. if (str == null || str.equals(""))
  264. return;
  265. Font excelFont = font;
  266. if ( font.getName().equals( "SansSerif" ) )
  267. {
  268. excelFont = new Font( "Arial", font.getStyle(), (int) ( font.getSize() / verticalPixelsPerPoint ) );
  269. }
  270. else
  271. {
  272. excelFont = new Font( font.getName(), font.getStyle(), (int) ( font.getSize() / verticalPixelsPerPoint ));
  273. }
  274. FontDetails d = StaticFontMetrics.getFontDetails( excelFont );
  275. int width = (int) ( (d.getStringWidth( str ) * 8) + 12 );
  276. int height = (int) ( ( font.getSize() / verticalPixelsPerPoint ) + 6 ) * 2;
  277. y -= ( font.getSize() / verticalPixelsPerPoint ) + 2 * verticalPixelsPerPoint; // we want to draw the shape from the top-left
  278. HSSFTextbox textbox = escherGroup.createTextbox( new HSSFChildAnchor( x, y, x + width, y + height ) );
  279. textbox.setNoFill( true );
  280. textbox.setLineStyle( HSSFShape.LINESTYLE_NONE );
  281. HSSFRichTextString s = new HSSFRichTextString( str );
  282. HSSFFont hssfFont = matchFont( excelFont );
  283. s.applyFont( hssfFont );
  284. textbox.setString( s );
  285. }
  286. private HSSFFont matchFont( Font font )
  287. {
  288. HSSFColor hssfColor = workbook.getCustomPalette()
  289. .findColor((byte)foreground.getRed(), (byte)foreground.getGreen(), (byte)foreground.getBlue());
  290. if (hssfColor == null)
  291. hssfColor = workbook.getCustomPalette().findSimilarColor((byte)foreground.getRed(), (byte)foreground.getGreen(), (byte)foreground.getBlue());
  292. boolean bold = (font.getStyle() & Font.BOLD) != 0;
  293. boolean italic = (font.getStyle() & Font.ITALIC) != 0;
  294. HSSFFont hssfFont = workbook.findFont(bold ? HSSFFont.BOLDWEIGHT_BOLD : 0,
  295. hssfColor.getIndex(),
  296. (short)(font.getSize() * 20),
  297. font.getName(),
  298. italic,
  299. false,
  300. (short)0,
  301. (byte)0);
  302. if (hssfFont == null)
  303. {
  304. hssfFont = workbook.createFont();
  305. hssfFont.setBoldweight(bold ? HSSFFont.BOLDWEIGHT_BOLD : 0);
  306. hssfFont.setColor(hssfColor.getIndex());
  307. hssfFont.setFontHeight((short)(font.getSize() * 20));
  308. hssfFont.setFontName(font.getName());
  309. hssfFont.setItalic(italic);
  310. hssfFont.setStrikeout(false);
  311. hssfFont.setTypeOffset((short) 0);
  312. hssfFont.setUnderline((byte) 0);
  313. }
  314. return hssfFont;
  315. }
  316. public void drawString(AttributedCharacterIterator iterator,
  317. int x, int y)
  318. {
  319. if (logger.check( POILogger.WARN ))
  320. logger.log(POILogger.WARN,"drawString not supported");
  321. }
  322. public void fillArc(int x, int y, int width, int height,
  323. int startAngle, int arcAngle)
  324. {
  325. if (logger.check( POILogger.WARN ))
  326. logger.log(POILogger.WARN,"fillArc not supported");
  327. }
  328. public void fillOval(int x, int y, int width, int height)
  329. {
  330. HSSFSimpleShape shape = escherGroup.createShape(new HSSFChildAnchor( x, y, x + width, y + height ) );
  331. shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
  332. shape.setLineStyle(HSSFShape.LINESTYLE_NONE);
  333. shape.setFillColor(foreground.getRed(), foreground.getGreen(), foreground.getBlue());
  334. shape.setLineStyleColor(foreground.getRed(), foreground.getGreen(), foreground.getBlue());
  335. }
  336. /**
  337. * Fills a (closed) polygon, as defined by a pair of arrays, which
  338. * hold the <i>x</i> and <i>y</i> coordinates.
  339. * <p>
  340. * This draws the polygon, with <code>nPoint</code> line segments.
  341. * The first <code>nPoint&nbsp;-&nbsp;1</code> line segments are
  342. * drawn between sequential points
  343. * (<code>xPoints[i],yPoints[i],xPoints[i+1],yPoints[i+1]</code>).
  344. * The final line segment is a closing one, from the last point to
  345. * the first (assuming they are different).
  346. * <p>
  347. * The area inside of the polygon is defined by using an
  348. * even-odd fill rule (also known as the alternating rule), and
  349. * the area inside of it is filled.
  350. * @param xPoints array of the <code>x</code> coordinates.
  351. * @param yPoints array of the <code>y</code> coordinates.
  352. * @param nPoints the total number of points in the polygon.
  353. * @see java.awt.Graphics#drawPolygon(int[], int[], int)
  354. */
  355. public void fillPolygon(int xPoints[], int yPoints[],
  356. int nPoints)
  357. {
  358. int right = findBiggest(xPoints);
  359. int bottom = findBiggest(yPoints);
  360. int left = findSmallest(xPoints);
  361. int top = findSmallest(yPoints);
  362. HSSFPolygon shape = escherGroup.createPolygon(new HSSFChildAnchor(left,top,right,bottom) );
  363. shape.setPolygonDrawArea(right - left, bottom - top);
  364. shape.setPoints(addToAll(xPoints, -left), addToAll(yPoints, -top));
  365. shape.setLineStyleColor(foreground.getRed(), foreground.getGreen(), foreground.getBlue());
  366. shape.setFillColor(foreground.getRed(), foreground.getGreen(), foreground.getBlue());
  367. }
  368. private int findBiggest( int[] values )
  369. {
  370. int result = Integer.MIN_VALUE;
  371. for ( int i = 0; i < values.length; i++ )
  372. {
  373. if (values[i] > result)
  374. result = values[i];
  375. }
  376. return result;
  377. }
  378. private int findSmallest( int[] values )
  379. {
  380. int result = Integer.MAX_VALUE;
  381. for ( int i = 0; i < values.length; i++ )
  382. {
  383. if (values[i] < result)
  384. result = values[i];
  385. }
  386. return result;
  387. }
  388. public void fillRect(int x, int y, int width, int height)
  389. {
  390. HSSFSimpleShape shape = escherGroup.createShape(new HSSFChildAnchor( x, y, x + width, y + height ) );
  391. shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
  392. shape.setLineStyle(HSSFShape.LINESTYLE_NONE);
  393. shape.setFillColor(foreground.getRed(), foreground.getGreen(), foreground.getBlue());
  394. shape.setLineStyleColor(foreground.getRed(), foreground.getGreen(), foreground.getBlue());
  395. }
  396. public void fillRoundRect(int x, int y, int width, int height,
  397. int arcWidth, int arcHeight)
  398. {
  399. if (logger.check( POILogger.WARN ))
  400. logger.log(POILogger.WARN,"fillRoundRect not supported");
  401. }
  402. public Shape getClip()
  403. {
  404. return getClipBounds();
  405. }
  406. public Rectangle getClipBounds()
  407. {
  408. return null;
  409. }
  410. public Rectangle getClipRect()
  411. {
  412. return getClipBounds();
  413. }
  414. public Color getColor()
  415. {
  416. return foreground;
  417. }
  418. public Font getFont()
  419. {
  420. return font;
  421. }
  422. public FontMetrics getFontMetrics(Font f)
  423. {
  424. return Toolkit.getDefaultToolkit().getFontMetrics(f);
  425. }
  426. public void setClip(int x, int y, int width, int height)
  427. {
  428. setClip(((Shape) (new Rectangle(x,y,width,height))));
  429. }
  430. public void setClip(Shape shape)
  431. {
  432. // ignore... not implemented
  433. }
  434. public void setColor(Color color)
  435. {
  436. foreground = color;
  437. }
  438. public void setFont(Font f)
  439. {
  440. font = f;
  441. }
  442. public void setPaintMode()
  443. {
  444. if (logger.check( POILogger.WARN ))
  445. logger.log(POILogger.WARN,"setPaintMode not supported");
  446. }
  447. public void setXORMode(Color color)
  448. {
  449. if (logger.check( POILogger.WARN ))
  450. logger.log(POILogger.WARN,"setXORMode not supported");
  451. }
  452. public void translate(int x, int y)
  453. {
  454. if (logger.check( POILogger.WARN ))
  455. logger.log(POILogger.WARN,"translate not supported");
  456. }
  457. public Color getBackground()
  458. {
  459. return background;
  460. }
  461. public void setBackground( Color background )
  462. {
  463. this.background = background;
  464. }
  465. HSSFShapeGroup getEscherGraphics()
  466. {
  467. return escherGroup;
  468. }
  469. }