PageRenderTime 78ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/xunchangguo/spudsoft-birt-excel-emitters
Java | 297 lines | 215 code | 29 blank | 53 comment | 91 complexity | 4a93e7cc18198ee2f00f76297625128b MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. /*************************************************************
  2. * (C) Copyright 2011, 2012 James Talbut.
  3. * jim-emitters@spudsoft.co.uk
  4. *
  5. * This file is part of The SpudSoft BIRT Excel Emitters.
  6. * The SpudSoft BIRT Excel Emitters are free software: you can
  7. * redistribute them and/or modify them under the terms of the
  8. * GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * The SpudSoft BIRT Excel Emitters are distributed in the hope
  13. * that they will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with the SpudSoft BIRT Excel Emitters.
  20. * If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. *************************************************************/
  23. package uk.co.spudsoft.birt.emitters.excel;
  24. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  25. import org.apache.poi.hssf.usermodel.HSSFFont;
  26. import org.apache.poi.hssf.usermodel.HSSFPalette;
  27. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  28. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  29. import org.apache.poi.hssf.util.HSSFColor;
  30. import org.apache.poi.ss.usermodel.CellStyle;
  31. import org.apache.poi.ss.usermodel.Font;
  32. import org.apache.poi.ss.usermodel.RichTextString;
  33. import org.apache.poi.ss.usermodel.Sheet;
  34. import org.apache.poi.ss.usermodel.Workbook;
  35. import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
  36. import org.eclipse.birt.report.engine.content.IPageContent;
  37. import org.eclipse.birt.report.engine.content.IStyle;
  38. import org.eclipse.birt.report.engine.css.dom.AreaStyle;
  39. import org.eclipse.birt.report.engine.css.engine.StyleConstants;
  40. import org.eclipse.birt.report.engine.ir.DimensionType;
  41. import org.eclipse.birt.report.model.api.util.ColorUtil;
  42. import org.w3c.dom.css.CSSValue;
  43. import uk.co.spudsoft.birt.emitters.excel.framework.Logger;
  44. /**
  45. * StyleManagerHUtils is an extension of the StyleManagerUtils to provide HSSFWorkbook specific functionality.
  46. * @author Jim Talbut
  47. *
  48. */
  49. public class StyleManagerHUtils extends StyleManagerUtils {
  50. private short paletteIndex = 64;
  51. private static short minPaletteIndex = 40;
  52. private static Factory factory = new StyleManagerUtils.Factory() {
  53. public StyleManagerUtils create(Logger log) {
  54. return new StyleManagerHUtils(log);
  55. }
  56. };
  57. public static Factory getFactory() {
  58. return factory;
  59. }
  60. /**
  61. * @param log
  62. * Logger used by StyleManagerHUtils to record anything of interest.
  63. */
  64. public StyleManagerHUtils(Logger log) {
  65. super(log);
  66. }
  67. @Override
  68. public RichTextString createRichTextString(String value) {
  69. return new HSSFRichTextString(value);
  70. }
  71. /**
  72. * Converts a BIRT border style into a POI border style (short constant defined in CellStyle).
  73. * @param birtBorder
  74. * The BIRT border style.
  75. * @param width
  76. * The width of the border as understood by BIRT.
  77. * @return
  78. * One of the CellStyle BORDER constants.
  79. */
  80. private short poiBorderStyleFromBirt( String birtBorder, String width ) {
  81. if( "none".equals(birtBorder) ) {
  82. return CellStyle.BORDER_NONE;
  83. }
  84. DimensionType dim = DimensionType.parserUnit( width );
  85. double pxWidth = 3.0;
  86. if( ( dim != null ) && ( "px".equals(dim.getUnits()) ) ){
  87. pxWidth = dim.getMeasure();
  88. }
  89. if( "solid".equals(birtBorder) ) {
  90. if( pxWidth < 2.9 ) {
  91. return CellStyle.BORDER_THIN;
  92. } else if( pxWidth < 3.1 ) {
  93. return CellStyle.BORDER_MEDIUM;
  94. } else {
  95. return CellStyle.BORDER_THICK;
  96. }
  97. } else if( "dashed".equals(birtBorder) ) {
  98. if( pxWidth < 2.9 ) {
  99. return CellStyle.BORDER_DASHED;
  100. } else {
  101. return CellStyle.BORDER_MEDIUM_DASHED;
  102. }
  103. } else if( "dotted".equals(birtBorder) ) {
  104. return CellStyle.BORDER_DOTTED;
  105. } else if( "double".equals(birtBorder) ) {
  106. return CellStyle.BORDER_DOUBLE;
  107. } else if( "none".equals(birtBorder) ) {
  108. return CellStyle.BORDER_NONE;
  109. }
  110. log.debug( "Border style \"", birtBorder, "\" is not recognised" );
  111. return CellStyle.BORDER_NONE;
  112. }
  113. /**
  114. * Get an HSSFPalette index for a workbook that closely approximates the passed in colour.
  115. * @param workbook
  116. * The workbook for which the colour is being sought.
  117. * @param colour
  118. * The colour, in the form "rgb(<i>r</i>, <i>g</i>, <i>b</i>)".
  119. * @return
  120. * The index into the HSSFPallete for the workbook for a colour that approximates the passed in colour.
  121. */
  122. private short getHColour( HSSFWorkbook workbook, String colour ) {
  123. int[] rgbInt = ColorUtil.getRGBs(colour);
  124. if( rgbInt == null ) {
  125. return 0;
  126. }
  127. byte[] rgbByte = new byte[] { (byte)rgbInt[0], (byte)rgbInt[1], (byte)rgbInt[2] };
  128. HSSFPalette palette = workbook.getCustomPalette();
  129. HSSFColor result = palette.findColor(rgbByte[0], rgbByte[1], rgbByte[2]);
  130. if( result == null) {
  131. if( paletteIndex > minPaletteIndex ) {
  132. --paletteIndex;
  133. palette.setColorAtIndex(paletteIndex, rgbByte[0], rgbByte[1], rgbByte[2]);
  134. return paletteIndex;
  135. } else {
  136. result = palette.findSimilarColor(rgbByte[0], rgbByte[1], rgbByte[2]);
  137. }
  138. }
  139. return result.getIndex();
  140. }
  141. @Override
  142. public void applyBorderStyle(Workbook workbook, CellStyle style, BorderSide side, CSSValue colour, CSSValue borderStyle, CSSValue width) {
  143. if( ( colour != null ) || ( borderStyle != null ) || ( width != null ) ) {
  144. String colourString = colour == null ? "rgb(0,0,0)" : colour.getCssText();
  145. String borderStyleString = borderStyle == null ? "solid" : borderStyle.getCssText();
  146. String widthString = width == null ? "medium" : width.getCssText();
  147. if( style instanceof HSSFCellStyle ) {
  148. HSSFCellStyle hStyle = (HSSFCellStyle)style;
  149. short hBorderStyle = poiBorderStyleFromBirt(borderStyleString, widthString);
  150. short colourIndex = getHColour((HSSFWorkbook)workbook, colourString);
  151. if( colourIndex > 0 ) {
  152. if(hBorderStyle != CellStyle.BORDER_NONE) {
  153. switch( side ) {
  154. case TOP:
  155. hStyle.setBorderTop(hBorderStyle);
  156. hStyle.setTopBorderColor(colourIndex);
  157. // log.debug( "Top border: " + xStyle.getBorderTop() + " / " + xStyle.getTopBorderXSSFColor().getARGBHex() );
  158. break;
  159. case LEFT:
  160. hStyle.setBorderLeft(hBorderStyle);
  161. hStyle.setLeftBorderColor(colourIndex);
  162. // log.debug( "Left border: " + xStyle.getBorderLeft() + " / " + xStyle.getLeftBorderXSSFColor().getARGBHex() );
  163. break;
  164. case RIGHT:
  165. hStyle.setBorderRight(hBorderStyle);
  166. hStyle.setRightBorderColor(colourIndex);
  167. // log.debug( "Right border: " + xStyle.getBorderRight() + " / " + xStyle.getRightBorderXSSFColor().getARGBHex() );
  168. break;
  169. case BOTTOM:
  170. hStyle.setBorderBottom(hBorderStyle);
  171. hStyle.setBottomBorderColor(colourIndex);
  172. // log.debug( "Bottom border: " + xStyle.getBorderBottom() + " / " + xStyle.getBottomBorderXSSFColor().getARGBHex() );
  173. break;
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. @Override
  181. public void addColourToFont(Workbook workbook, Font font, String colour) {
  182. if(colour == null) {
  183. return ;
  184. }
  185. if(IStyle.TRANSPARENT_VALUE.equals(colour)) {
  186. return ;
  187. }
  188. if(font instanceof HSSFFont) {
  189. HSSFFont hFont = (HSSFFont)font;
  190. short colourIndex = getHColour((HSSFWorkbook)workbook, colour);
  191. if( colourIndex > 0 ) {
  192. hFont.setColor(colourIndex);
  193. }
  194. }
  195. }
  196. @Override
  197. public void addBackgroundColourToStyle(Workbook workbook, CellStyle style, String colour) {
  198. if(colour == null) {
  199. return ;
  200. }
  201. if(IStyle.TRANSPARENT_VALUE.equals(colour)) {
  202. return ;
  203. }
  204. if(style instanceof HSSFCellStyle) {
  205. HSSFCellStyle cellStyle = (HSSFCellStyle)style;
  206. short colourIndex = getHColour((HSSFWorkbook)workbook, colour);
  207. if( colourIndex > 0 ) {
  208. cellStyle.setFillForegroundColor(colourIndex);
  209. cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  210. }
  211. }
  212. }
  213. @Override
  214. public Font correctFontColorIfBackground(FontManager fm, Workbook wb, BirtStyle birtStyle, Font font) {
  215. HSSFPalette palette = ((HSSFWorkbook)wb).getCustomPalette();
  216. CSSValue bgColour = birtStyle.getProperty( StyleConstants.STYLE_BACKGROUND_COLOR );
  217. int bgRgb[] = parseColour( bgColour == null ? null : bgColour.getCssText(), "white" );
  218. short fgRgb[] = HSSFColor.BLACK.triplet;
  219. if( ( font != null ) && ( font.getColor() != Short.MAX_VALUE ) ) {
  220. fgRgb = palette.getColor(font.getColor()).getTriplet();
  221. }
  222. if( ( fgRgb[0] == 255 ) && ( fgRgb[1] == 255 ) && ( fgRgb[2] == 255 ) ) {
  223. fgRgb[0]=fgRgb[1]=fgRgb[2]=0;
  224. } else if( ( fgRgb[0] == 0 ) && ( fgRgb[1] == 0 ) && ( fgRgb[2] == 0 ) ) {
  225. fgRgb[0]=fgRgb[1]=fgRgb[2]=255;
  226. }
  227. if( ( bgRgb[ 0 ] == fgRgb[ 0 ] ) && ( bgRgb[ 1 ] == fgRgb[ 1 ] ) && ( bgRgb[ 2 ] == fgRgb[ 2 ] ) ) {
  228. IStyle addedStyle = new AreaStyle( fm.getCssEngine() );
  229. addedStyle.setColor( contrastColour( bgRgb ) );
  230. return fm.getFontWithExtraStyle( font, addedStyle );
  231. } else {
  232. return font;
  233. }
  234. }
  235. @Override
  236. public int anchorDxFromMM( double widthMM, double colWidthMM ) {
  237. return (int)( 1023.0 * widthMM / colWidthMM );
  238. }
  239. @Override
  240. public int anchorDyFromPoints( float height, float rowHeight ) {
  241. return (int)( 255.0 * height / rowHeight );
  242. }
  243. @Override
  244. public void prepareMarginDimensions(Sheet sheet, IPageContent page) {
  245. double headerHeight = 0.5;
  246. double footerHeight = 0.5;
  247. if( ( page.getHeaderHeight() != null ) && isAbsolute( page.getHeaderHeight() ) ) {
  248. headerHeight = page.getHeaderHeight().convertTo(DimensionType.UNITS_IN);
  249. sheet.getPrintSetup().setHeaderMargin(headerHeight);
  250. }
  251. if( ( page.getFooterHeight() != null ) && isAbsolute( page.getFooterHeight() ) ) {
  252. footerHeight = page.getFooterHeight().convertTo(DimensionType.UNITS_IN);
  253. sheet.getPrintSetup().setFooterMargin(footerHeight);
  254. }
  255. if( ( page.getMarginBottom() != null ) && isAbsolute( page.getMarginBottom() ) ) {
  256. sheet.setMargin(Sheet.BottomMargin, footerHeight + page.getMarginBottom().convertTo(DimensionType.UNITS_IN));
  257. }
  258. if( ( page.getMarginLeft() != null ) && isAbsolute( page.getMarginLeft() ) ) {
  259. sheet.setMargin(Sheet.LeftMargin, page.getMarginLeft().convertTo(DimensionType.UNITS_IN));
  260. }
  261. if( ( page.getMarginRight() != null ) && isAbsolute( page.getMarginRight() ) ) {
  262. sheet.setMargin(Sheet.RightMargin, page.getMarginRight().convertTo(DimensionType.UNITS_IN));
  263. }
  264. if( ( page.getMarginTop() != null ) && isAbsolute( page.getMarginTop() ) ) {
  265. sheet.setMargin(Sheet.TopMargin, headerHeight + page.getMarginTop().convertTo(DimensionType.UNITS_IN));
  266. }
  267. }
  268. }