PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/SpudSoft BIRT Excel Emitters/src/uk/co/spudsoft/birt/emitters/excel/StyleManagerHUtils.java

https://bitbucket.org/yaytay/spudsoft-birt-excel-emitters
Java | 258 lines | 187 code | 28 blank | 43 comment | 73 complexity | 8510175981f7653c68170cbdfb797646 MD5 | raw file
Possible License(s): EPL-1.0
  1. /*************************************************************************************
  2. * Copyright (c) 2011, 2012, 2013 James Talbut.
  3. * jim-emitters@spudsoft.co.uk
  4. *
  5. * All rights reserved. This program and the accompanying materials
  6. * are made available under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution, and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * James Talbut - Initial implementation.
  12. ************************************************************************************/
  13. package uk.co.spudsoft.birt.emitters.excel;
  14. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  15. import org.apache.poi.hssf.usermodel.HSSFFont;
  16. import org.apache.poi.hssf.usermodel.HSSFPalette;
  17. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  18. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  19. import org.apache.poi.hssf.util.HSSFColor;
  20. import org.apache.poi.ss.usermodel.CellStyle;
  21. import org.apache.poi.ss.usermodel.Font;
  22. import org.apache.poi.ss.usermodel.RichTextString;
  23. import org.apache.poi.ss.usermodel.Workbook;
  24. import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
  25. import org.eclipse.birt.report.engine.content.IStyle;
  26. import org.eclipse.birt.report.engine.css.dom.AreaStyle;
  27. import org.eclipse.birt.report.engine.ir.DimensionType;
  28. import org.eclipse.birt.report.model.api.util.ColorUtil;
  29. import org.w3c.dom.css.CSSValue;
  30. import uk.co.spudsoft.birt.emitters.excel.framework.Logger;
  31. /**
  32. * StyleManagerHUtils is an extension of the StyleManagerUtils to provide HSSFWorkbook specific functionality.
  33. * @author Jim Talbut
  34. *
  35. */
  36. public class StyleManagerHUtils extends StyleManagerUtils {
  37. private short paletteIndex = 64;
  38. private static short minPaletteIndex = 40;
  39. private static Factory factory = new StyleManagerUtils.Factory() {
  40. public StyleManagerUtils create(Logger log) {
  41. return new StyleManagerHUtils(log);
  42. }
  43. };
  44. public static Factory getFactory() {
  45. return factory;
  46. }
  47. /**
  48. * @param log
  49. * Logger used by StyleManagerHUtils to record anything of interest.
  50. */
  51. public StyleManagerHUtils(Logger log) {
  52. super(log);
  53. }
  54. @Override
  55. public RichTextString createRichTextString(String value) {
  56. return new HSSFRichTextString(value);
  57. }
  58. /**
  59. * Converts a BIRT border style into a POI border style (short constant defined in CellStyle).
  60. * @param birtBorder
  61. * The BIRT border style.
  62. * @param width
  63. * The width of the border as understood by BIRT.
  64. * @return
  65. * One of the CellStyle BORDER constants.
  66. */
  67. private short poiBorderStyleFromBirt( String birtBorder, String width ) {
  68. if( "none".equals(birtBorder) ) {
  69. return CellStyle.BORDER_NONE;
  70. }
  71. DimensionType dim = DimensionType.parserUnit( width );
  72. double pxWidth = 3.0;
  73. if( ( dim != null ) && ( "px".equals(dim.getUnits()) ) ){
  74. pxWidth = dim.getMeasure();
  75. }
  76. if( "solid".equals(birtBorder) ) {
  77. if( pxWidth < 2.9 ) {
  78. return CellStyle.BORDER_THIN;
  79. } else if( pxWidth < 3.1 ) {
  80. return CellStyle.BORDER_MEDIUM;
  81. } else {
  82. return CellStyle.BORDER_THICK;
  83. }
  84. } else if( "dashed".equals(birtBorder) ) {
  85. if( pxWidth < 2.9 ) {
  86. return CellStyle.BORDER_DASHED;
  87. } else {
  88. return CellStyle.BORDER_MEDIUM_DASHED;
  89. }
  90. } else if( "dotted".equals(birtBorder) ) {
  91. return CellStyle.BORDER_DOTTED;
  92. } else if( "double".equals(birtBorder) ) {
  93. return CellStyle.BORDER_DOUBLE;
  94. } else if( "none".equals(birtBorder) ) {
  95. return CellStyle.BORDER_NONE;
  96. }
  97. log.debug( "Border style \"", birtBorder, "\" is not recognised" );
  98. return CellStyle.BORDER_NONE;
  99. }
  100. /**
  101. * Get an HSSFPalette index for a workbook that closely approximates the passed in colour.
  102. * @param workbook
  103. * The workbook for which the colour is being sought.
  104. * @param colour
  105. * The colour, in the form "rgb(<i>r</i>, <i>g</i>, <i>b</i>)".
  106. * @return
  107. * The index into the HSSFPallete for the workbook for a colour that approximates the passed in colour.
  108. */
  109. private short getHColour( HSSFWorkbook workbook, String colour ) {
  110. int[] rgbInt = ColorUtil.getRGBs(colour);
  111. if( rgbInt == null ) {
  112. return 0;
  113. }
  114. byte[] rgbByte = new byte[] { (byte)rgbInt[0], (byte)rgbInt[1], (byte)rgbInt[2] };
  115. HSSFPalette palette = workbook.getCustomPalette();
  116. HSSFColor result = palette.findColor(rgbByte[0], rgbByte[1], rgbByte[2]);
  117. if( result == null) {
  118. if( paletteIndex > minPaletteIndex ) {
  119. --paletteIndex;
  120. palette.setColorAtIndex(paletteIndex, rgbByte[0], rgbByte[1], rgbByte[2]);
  121. return paletteIndex;
  122. } else {
  123. result = palette.findSimilarColor(rgbByte[0], rgbByte[1], rgbByte[2]);
  124. }
  125. }
  126. return result.getIndex();
  127. }
  128. @Override
  129. public void applyBorderStyle(Workbook workbook, CellStyle style, BorderSide side, CSSValue colour, CSSValue borderStyle, CSSValue width) {
  130. if( ( colour != null ) || ( borderStyle != null ) || ( width != null ) ) {
  131. String colourString = colour == null ? "rgb(0,0,0)" : colour.getCssText();
  132. String borderStyleString = borderStyle == null ? "solid" : borderStyle.getCssText();
  133. String widthString = width == null ? "medium" : width.getCssText();
  134. if( style instanceof HSSFCellStyle ) {
  135. HSSFCellStyle hStyle = (HSSFCellStyle)style;
  136. short hBorderStyle = poiBorderStyleFromBirt(borderStyleString, widthString);
  137. short colourIndex = getHColour((HSSFWorkbook)workbook, colourString);
  138. if( colourIndex > 0 ) {
  139. if(hBorderStyle != CellStyle.BORDER_NONE) {
  140. switch( side ) {
  141. case TOP:
  142. hStyle.setBorderTop(hBorderStyle);
  143. hStyle.setTopBorderColor(colourIndex);
  144. // log.debug( "Top border: " + xStyle.getBorderTop() + " / " + xStyle.getTopBorderXSSFColor().getARGBHex() );
  145. break;
  146. case LEFT:
  147. hStyle.setBorderLeft(hBorderStyle);
  148. hStyle.setLeftBorderColor(colourIndex);
  149. // log.debug( "Left border: " + xStyle.getBorderLeft() + " / " + xStyle.getLeftBorderXSSFColor().getARGBHex() );
  150. break;
  151. case RIGHT:
  152. hStyle.setBorderRight(hBorderStyle);
  153. hStyle.setRightBorderColor(colourIndex);
  154. // log.debug( "Right border: " + xStyle.getBorderRight() + " / " + xStyle.getRightBorderXSSFColor().getARGBHex() );
  155. break;
  156. case BOTTOM:
  157. hStyle.setBorderBottom(hBorderStyle);
  158. hStyle.setBottomBorderColor(colourIndex);
  159. // log.debug( "Bottom border: " + xStyle.getBorderBottom() + " / " + xStyle.getBottomBorderXSSFColor().getARGBHex() );
  160. break;
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }
  167. @Override
  168. public void addColourToFont(Workbook workbook, Font font, String colour) {
  169. if(colour == null) {
  170. return ;
  171. }
  172. if(IStyle.TRANSPARENT_VALUE.equals(colour)) {
  173. return ;
  174. }
  175. if(font instanceof HSSFFont) {
  176. HSSFFont hFont = (HSSFFont)font;
  177. short colourIndex = getHColour((HSSFWorkbook)workbook, colour);
  178. if( colourIndex > 0 ) {
  179. hFont.setColor(colourIndex);
  180. }
  181. }
  182. }
  183. @Override
  184. public void addBackgroundColourToStyle(Workbook workbook, CellStyle style, String colour) {
  185. if(colour == null) {
  186. return ;
  187. }
  188. if(IStyle.TRANSPARENT_VALUE.equals(colour)) {
  189. return ;
  190. }
  191. if(style instanceof HSSFCellStyle) {
  192. HSSFCellStyle cellStyle = (HSSFCellStyle)style;
  193. short colourIndex = getHColour((HSSFWorkbook)workbook, colour);
  194. if( colourIndex > 0 ) {
  195. cellStyle.setFillForegroundColor(colourIndex);
  196. cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  197. }
  198. }
  199. }
  200. @Override
  201. public Font correctFontColorIfBackground(FontManager fm, Workbook wb, BirtStyle birtStyle, Font font) {
  202. HSSFPalette palette = ((HSSFWorkbook)wb).getCustomPalette();
  203. CSSValue bgColour = birtStyle.getProperty( StylePropertyIndexes.STYLE_BACKGROUND_COLOR );
  204. int bgRgb[] = parseColour( bgColour == null ? null : bgColour.getCssText(), "white" );
  205. short fgRgb[] = HSSFColor.BLACK.triplet;
  206. if( ( font != null ) && ( font.getColor() != Short.MAX_VALUE ) ) {
  207. fgRgb = palette.getColor(font.getColor()).getTriplet();
  208. }
  209. if( ( fgRgb[0] == 255 ) && ( fgRgb[1] == 255 ) && ( fgRgb[2] == 255 ) ) {
  210. fgRgb[0]=fgRgb[1]=fgRgb[2]=0;
  211. } else if( ( fgRgb[0] == 0 ) && ( fgRgb[1] == 0 ) && ( fgRgb[2] == 0 ) ) {
  212. fgRgb[0]=fgRgb[1]=fgRgb[2]=255;
  213. }
  214. if( ( bgRgb[ 0 ] == fgRgb[ 0 ] ) && ( bgRgb[ 1 ] == fgRgb[ 1 ] ) && ( bgRgb[ 2 ] == fgRgb[ 2 ] ) ) {
  215. IStyle addedStyle = new AreaStyle( fm.getCssEngine() );
  216. addedStyle.setColor( contrastColour( bgRgb ) );
  217. return fm.getFontWithExtraStyle( font, addedStyle );
  218. } else {
  219. return font;
  220. }
  221. }
  222. @Override
  223. public int anchorDxFromMM( double widthMM, double colWidthMM ) {
  224. return (int)( 1023.0 * widthMM / colWidthMM );
  225. }
  226. @Override
  227. public int anchorDyFromPoints( float height, float rowHeight ) {
  228. return (int)( 255.0 * height / rowHeight );
  229. }
  230. }