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

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

https://bitbucket.org/xunchangguo/spudsoft-birt-excel-emitters
Java | 280 lines | 209 code | 26 blank | 45 comment | 88 complexity | 9bae33ba2ae2b3659c034710c785370e 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.ss.usermodel.BorderStyle;
  25. import org.apache.poi.ss.usermodel.CellStyle;
  26. import org.apache.poi.ss.usermodel.FillPatternType;
  27. import org.apache.poi.ss.usermodel.Font;
  28. import org.apache.poi.ss.usermodel.RichTextString;
  29. import org.apache.poi.ss.usermodel.Sheet;
  30. import org.apache.poi.ss.usermodel.Workbook;
  31. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  32. import org.apache.poi.xssf.usermodel.XSSFColor;
  33. import org.apache.poi.xssf.usermodel.XSSFFont;
  34. import org.apache.poi.xssf.usermodel.XSSFRichTextString;
  35. import org.apache.poi.xssf.usermodel.XSSFShape;
  36. import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
  37. import org.eclipse.birt.report.engine.content.IPageContent;
  38. import org.eclipse.birt.report.engine.content.IStyle;
  39. import org.eclipse.birt.report.engine.css.dom.AreaStyle;
  40. import org.eclipse.birt.report.engine.css.engine.StyleConstants;
  41. import org.eclipse.birt.report.engine.css.engine.value.css.CSSConstants;
  42. import org.eclipse.birt.report.engine.ir.DimensionType;
  43. import org.eclipse.birt.report.model.api.util.ColorUtil;
  44. import org.w3c.dom.css.CSSValue;
  45. import uk.co.spudsoft.birt.emitters.excel.framework.Logger;
  46. /**
  47. * StyleManagerXUtils is an extension of the StyleManagerUtils to provide XSSFWorkbook specific functionality.
  48. * @author Jim Talbut
  49. *
  50. */
  51. public class StyleManagerXUtils extends StyleManagerUtils {
  52. private static Factory factory = new StyleManagerUtils.Factory() {
  53. public StyleManagerUtils create(Logger log) {
  54. return new StyleManagerXUtils(log);
  55. }
  56. };
  57. public static Factory getFactory() {
  58. return factory;
  59. }
  60. /**
  61. * @param log
  62. * Logger used by StyleManagerXUtils to record anything of interest.
  63. */
  64. public StyleManagerXUtils(Logger log) {
  65. super(log);
  66. }
  67. @Override
  68. public RichTextString createRichTextString(String value) {
  69. XSSFRichTextString result = new XSSFRichTextString(value);
  70. return result;
  71. }
  72. /**
  73. * Converts a BIRT border style into a POI BorderStyle.
  74. * @param birtBorder
  75. * The BIRT border style.
  76. * @param width
  77. * The width of the border as understood by BIRT.
  78. * @return
  79. * A POI BorderStyle object.
  80. */
  81. private BorderStyle poiBorderStyleFromBirt( String birtBorder, String width ) {
  82. if( "none".equals(birtBorder) ) {
  83. return BorderStyle.NONE;
  84. }
  85. double pxWidth = 3.0;
  86. if( CSSConstants.CSS_THIN_VALUE.equals( width ) ) {
  87. pxWidth = 1.0;
  88. } else if( CSSConstants.CSS_MEDIUM_VALUE.equals( width ) ) {
  89. pxWidth = 3.0;
  90. } else if( CSSConstants.CSS_THICK_VALUE.equals( width ) ) {
  91. pxWidth = 4.0;
  92. } else {
  93. DimensionType dim = DimensionType.parserUnit( width );
  94. if( dim != null ) {
  95. if( "px".equals(dim.getUnits()) ) {
  96. pxWidth = dim.getMeasure();
  97. }
  98. }
  99. }
  100. if( "solid".equals(birtBorder) ) {
  101. if( pxWidth < 2.9 ) {
  102. return BorderStyle.THIN;
  103. } else if( pxWidth < 3.1 ) {
  104. return BorderStyle.MEDIUM;
  105. } else {
  106. return BorderStyle.THICK;
  107. }
  108. } else if( "dashed".equals(birtBorder) ) {
  109. if( pxWidth < 2.9 ) {
  110. return BorderStyle.DASHED;
  111. } else {
  112. return BorderStyle.MEDIUM_DASHED;
  113. }
  114. } else if( "dotted".equals(birtBorder) ) {
  115. return BorderStyle.DOTTED;
  116. } else if( "double".equals(birtBorder) ) {
  117. return BorderStyle.DOUBLE;
  118. }
  119. log.debug( "Border style \"", birtBorder, "\" is not recognised." );
  120. return BorderStyle.NONE;
  121. }
  122. @Override
  123. public void applyBorderStyle(Workbook workbook, CellStyle style, BorderSide side, CSSValue colour, CSSValue borderStyle, CSSValue width) {
  124. if( ( colour != null ) || ( borderStyle != null ) || ( width != null ) ) {
  125. String colourString = colour == null ? "rgb(0,0,0)" : colour.getCssText();
  126. String borderStyleString = borderStyle == null ? "solid" : borderStyle.getCssText();
  127. String widthString = width == null ? "medium" : width.getCssText();
  128. if( style instanceof XSSFCellStyle ) {
  129. XSSFCellStyle xStyle = (XSSFCellStyle)style;
  130. BorderStyle xBorderStyle = poiBorderStyleFromBirt(borderStyleString, widthString);
  131. XSSFColor xBorderColour = getXColour(colourString);
  132. if(xBorderStyle != BorderStyle.NONE) {
  133. switch( side ) {
  134. case TOP:
  135. xStyle.setBorderTop(xBorderStyle);
  136. xStyle.setTopBorderColor(xBorderColour);
  137. // log.debug( "Top border: " + xStyle.getBorderTop() + " / " + xStyle.getTopBorderXSSFColor().getARGBHex() );
  138. break;
  139. case LEFT:
  140. xStyle.setBorderLeft(xBorderStyle);
  141. xStyle.setLeftBorderColor(xBorderColour);
  142. // log.debug( "Left border: " + xStyle.getBorderLeft() + " / " + xStyle.getLeftBorderXSSFColor().getARGBHex() );
  143. break;
  144. case RIGHT:
  145. xStyle.setBorderRight(xBorderStyle);
  146. xStyle.setRightBorderColor(xBorderColour);
  147. // log.debug( "Right border: " + xStyle.getBorderRight() + " / " + xStyle.getRightBorderXSSFColor().getARGBHex() );
  148. break;
  149. case BOTTOM:
  150. xStyle.setBorderBottom(xBorderStyle);
  151. xStyle.setBottomBorderColor(xBorderColour);
  152. // log.debug( "Bottom border: " + xStyle.getBorderBottom() + " / " + xStyle.getBottomBorderXSSFColor().getARGBHex() );
  153. break;
  154. }
  155. }
  156. }
  157. }
  158. }
  159. private XSSFColor getXColour(String colour) {
  160. int[] rgbInt = ColorUtil.getRGBs(colour);
  161. if( rgbInt == null ) {
  162. return null;
  163. }
  164. byte[] rgbByte = { (byte)-1, (byte)rgbInt[0], (byte)rgbInt[1], (byte)rgbInt[2] };
  165. // System.out.println( "The X colour for " + colour + " is [ " + rgbByte[0] + "," + rgbByte[1] + "," + rgbByte[2] + "," + rgbByte[3] + "]" );
  166. XSSFColor result = new XSSFColor( rgbByte );
  167. return result;
  168. }
  169. @Override
  170. public void addColourToFont(Workbook workbook, Font font, String colour) {
  171. if(colour == null) {
  172. return ;
  173. }
  174. if(IStyle.TRANSPARENT_VALUE.equals(colour)) {
  175. return ;
  176. }
  177. if(font instanceof XSSFFont) {
  178. XSSFFont xFont = (XSSFFont)font;
  179. XSSFColor xColour = getXColour(colour);
  180. if(xColour != null) {
  181. xFont.setColor(xColour);
  182. }
  183. }
  184. }
  185. @Override
  186. public void addBackgroundColourToStyle(Workbook workbook, CellStyle style, String colour) {
  187. if(colour == null) {
  188. return ;
  189. }
  190. if(IStyle.TRANSPARENT_VALUE.equals(colour)) {
  191. return ;
  192. }
  193. if(style instanceof XSSFCellStyle) {
  194. XSSFCellStyle cellStyle = (XSSFCellStyle)style;
  195. XSSFColor xColour = getXColour(colour);
  196. if(xColour != null) {
  197. cellStyle.setFillForegroundColor(xColour);
  198. cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  199. }
  200. }
  201. }
  202. @Override
  203. public Font correctFontColorIfBackground( FontManager fm, Workbook wb, BirtStyle birtStyle, Font font ) {
  204. CSSValue bgColour = birtStyle.getProperty( StyleConstants.STYLE_BACKGROUND_COLOR );
  205. int bgRgb[] = parseColour( bgColour == null ? null : bgColour.getCssText(), "white" );
  206. XSSFColor colour = ((XSSFFont)font).getXSSFColor();
  207. int fgRgb[] = rgbOnly( colour.getARgb() );
  208. if( ( fgRgb[0] == 255 ) && ( fgRgb[1] == 255 ) && ( fgRgb[2] == 255 ) ) {
  209. fgRgb[0]=fgRgb[1]=fgRgb[2]=0;
  210. } else if( ( fgRgb[0] == 0 ) && ( fgRgb[1] == 0 ) && ( fgRgb[2] == 0 ) ) {
  211. fgRgb[0]=fgRgb[1]=fgRgb[2]=255;
  212. }
  213. if( ( bgRgb[ 0 ] == fgRgb[ 0 ] ) && ( bgRgb[ 1 ] == fgRgb[ 1 ] ) && ( bgRgb[ 2 ] == fgRgb[ 2 ] ) ) {
  214. IStyle addedStyle = new AreaStyle( fm.getCssEngine() );
  215. addedStyle.setColor( contrastColour( bgRgb ) );
  216. return fm.getFontWithExtraStyle( font, addedStyle );
  217. } else {
  218. return font;
  219. }
  220. }
  221. @Override
  222. public int anchorDxFromMM( double widthMM, double colWidthMM ) {
  223. return (int)(widthMM * 36000);
  224. }
  225. @Override
  226. public int anchorDyFromPoints( float height, float rowHeight ) {
  227. return (int)( height * XSSFShape.EMU_PER_POINT );
  228. }
  229. @Override
  230. public void prepareMarginDimensions(Sheet sheet, IPageContent page) {
  231. double headerHeight = 0.5;
  232. double footerHeight = 0.5;
  233. if( ( page.getHeaderHeight() != null ) && isAbsolute( page.getHeaderHeight() ) ) {
  234. headerHeight = page.getHeaderHeight().convertTo(DimensionType.UNITS_IN);
  235. sheet.setMargin(Sheet.HeaderMargin, headerHeight);
  236. }
  237. if( ( page.getFooterHeight() != null ) && isAbsolute( page.getFooterHeight() ) ) {
  238. footerHeight = page.getFooterHeight().convertTo(DimensionType.UNITS_IN);
  239. sheet.setMargin(Sheet.FooterMargin, footerHeight);
  240. }
  241. if( ( page.getMarginBottom() != null ) && isAbsolute( page.getMarginBottom() ) ) {
  242. sheet.setMargin(Sheet.BottomMargin, footerHeight + page.getMarginBottom().convertTo(DimensionType.UNITS_IN));
  243. }
  244. if( ( page.getMarginLeft() != null ) && isAbsolute( page.getMarginLeft() ) ) {
  245. sheet.setMargin(Sheet.LeftMargin, page.getMarginLeft().convertTo(DimensionType.UNITS_IN));
  246. }
  247. if( ( page.getMarginRight() != null ) && isAbsolute( page.getMarginRight() ) ) {
  248. sheet.setMargin(Sheet.RightMargin, page.getMarginRight().convertTo(DimensionType.UNITS_IN));
  249. }
  250. if( ( page.getMarginTop() != null ) && isAbsolute( page.getMarginTop() ) ) {
  251. sheet.setMargin(Sheet.TopMargin, headerHeight + page.getMarginTop().convertTo(DimensionType.UNITS_IN));
  252. }
  253. }
  254. }