PageRenderTime 68ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/thirdparties-extension/org.apache.poi.xwpf.converter/src/main/java/org/apache/poi/xwpf/converter/internal/xhtml/XHTMLStyleUtil.java

https://code.google.com/
Java | 494 lines | 324 code | 57 blank | 113 comment | 85 complexity | dd6cf4bedbefe0b7e63404d0990f0fcc MD5 | raw file
  1. /**
  2. * Copyright (C) 2011 Angelo Zerr <angelo.zerr@gmail.com> and Pascal Leclercq <pascal.leclercq@gmail.com>
  3. *
  4. * All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. package org.apache.poi.xwpf.converter.internal.xhtml;
  26. import static org.apache.poi.xwpf.converter.internal.DxaUtil.dxa2points;
  27. import static org.apache.poi.xwpf.converter.internal.XWPFRunUtils.getFontColor;
  28. import static org.apache.poi.xwpf.converter.internal.XWPFRunUtils.getFontFamily;
  29. import static org.apache.poi.xwpf.converter.internal.XWPFRunUtils.isBold;
  30. import static org.apache.poi.xwpf.converter.internal.XWPFRunUtils.isItalic;
  31. import static org.apache.poi.xwpf.converter.internal.XWPFUtils.getRPr;
  32. import java.math.BigInteger;
  33. import org.apache.poi.xwpf.converter.internal.XWPFUtils;
  34. import org.apache.poi.xwpf.converter.internal.itext.TableWidth;
  35. import org.apache.poi.xwpf.converter.internal.itext.XWPFParagraphUtils;
  36. import org.apache.poi.xwpf.converter.internal.itext.XWPFTableUtil;
  37. import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
  38. import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
  39. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  40. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  41. import org.apache.poi.xwpf.usermodel.XWPFPicture;
  42. import org.apache.poi.xwpf.usermodel.XWPFRun;
  43. import org.apache.poi.xwpf.usermodel.XWPFStyle;
  44. import org.apache.poi.xwpf.usermodel.XWPFTableCell;
  45. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  46. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder;
  47. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocDefaults;
  48. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
  49. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;
  50. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageSz;
  51. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
  52. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
  53. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
  54. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle;
  55. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders;
  56. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
  57. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
  58. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
  59. import fr.opensagres.xdocreport.utils.BorderType;
  60. import fr.opensagres.xdocreport.utils.StringUtils;
  61. import fr.opensagres.xdocreport.xhtml.extension.CSSStylePropertyConstants;
  62. import fr.opensagres.xdocreport.xhtml.extension.XHTMLUtil;
  63. public class XHTMLStyleUtil
  64. implements CSSStylePropertyConstants
  65. {
  66. public static StringBuilder getStyle( XWPFDocument document, CTDocDefaults defaults )
  67. {
  68. StringBuilder htmlStyle = new StringBuilder();
  69. CTSectPr sectPr = document.getDocument().getBody().getSectPr();
  70. CTPageSz pageSize = sectPr.getPgSz();
  71. if ( pageSize != null )
  72. {
  73. // Width
  74. BigInteger width = pageSize.getW();
  75. float widthPt = dxa2points( width );
  76. XHTMLUtil.addHTMLStyle( htmlStyle, WIDTH, widthPt + "pt" );
  77. }
  78. CTPageMar pageMargin = sectPr.getPgMar();
  79. if ( pageMargin != null )
  80. {
  81. // margin bottom
  82. BigInteger marginBottom = pageMargin.getBottom();
  83. if ( marginBottom != null )
  84. {
  85. float marginBottomPt = dxa2points( marginBottom );
  86. XHTMLUtil.addHTMLStyle( htmlStyle, MARGIN_BOTTOM, marginBottomPt + "pt" );
  87. }
  88. // margin top
  89. BigInteger marginTop = pageMargin.getTop();
  90. if ( marginTop != null )
  91. {
  92. float marginTopPt = dxa2points( marginTop );
  93. XHTMLUtil.addHTMLStyle( htmlStyle, MARGIN_TOP, marginTopPt + "pt" );
  94. }
  95. // margin right
  96. BigInteger marginRight = pageMargin.getRight();
  97. if ( marginRight != null )
  98. {
  99. float marginRightPt = dxa2points( marginRight );
  100. XHTMLUtil.addHTMLStyle( htmlStyle, MARGIN_RIGHT, marginRightPt + "pt" );
  101. }
  102. // margin left
  103. BigInteger marginLeft = pageMargin.getLeft();
  104. if ( marginLeft != null )
  105. {
  106. float marginLeftPt = dxa2points( marginLeft );
  107. XHTMLUtil.addHTMLStyle( htmlStyle, MARGIN_LEFT, marginLeftPt + "pt" );
  108. }
  109. }
  110. return htmlStyle;
  111. }
  112. public static StringBuilder getStyle( XWPFParagraph paragraph, XWPFStyle style, CTDocDefaults defaults )
  113. {
  114. StringBuilder htmlStyle = new StringBuilder();
  115. float indentationLeft = -1;
  116. float indentationRight = -1;
  117. float firstLineIndent = -1;
  118. float spacingBefore = -1;
  119. float spacingAfter = -1;
  120. // // 1) From style
  121. // CTPPr ppr = getPPr(style);
  122. // if (ppr != null) {
  123. // // Indentation
  124. // CTInd ind = ppr.getInd();
  125. // if (ind != null) {
  126. //
  127. // // Left Indentation
  128. // BigInteger left = ind.getLeft();
  129. // if (left != null) {
  130. // indentationLeft = dxa2points(left);
  131. // }
  132. //
  133. // // Right Indentation
  134. // BigInteger right = ind.getRight();
  135. // if (right != null) {
  136. // indentationRight = dxa2points(right);
  137. // }
  138. //
  139. // // First line Indentation
  140. // BigInteger firstLine = ind.getFirstLine();
  141. // if (firstLine != null) {
  142. // firstLineIndent = dxa2points(firstLine);
  143. // }
  144. // }
  145. //
  146. // CTSpacing spacing = ppr.getSpacing();
  147. // if (spacing != null) {
  148. //
  149. // // Spacing before
  150. // BigInteger before = spacing.getBefore();
  151. // if (before != null) {
  152. // spacingBefore = dxa2points(before);
  153. // }
  154. //
  155. // // Spacing after
  156. // BigInteger after = spacing.getAfter();
  157. // if (after != null) {
  158. // spacingAfter = dxa2points(after);
  159. // }
  160. // }
  161. //
  162. // }
  163. // 2) From paragraph
  164. if ( indentationLeft == -1 && paragraph.getIndentationLeft() != -1 )
  165. {
  166. indentationLeft = dxa2points( paragraph.getIndentationLeft() );
  167. }
  168. if ( indentationRight == -1 && paragraph.getIndentationRight() != -1 )
  169. {
  170. indentationRight = dxa2points( paragraph.getIndentationRight() );
  171. }
  172. if ( firstLineIndent == -1 && paragraph.getIndentationFirstLine() != -1 )
  173. {
  174. firstLineIndent = dxa2points( paragraph.getIndentationFirstLine() );
  175. }
  176. if ( spacingBefore == -1 && paragraph.getSpacingBefore() != -1 )
  177. {
  178. spacingBefore = dxa2points( paragraph.getSpacingBefore() );
  179. }
  180. if ( spacingAfter == -1 && paragraph.getSpacingAfter() != -1 )
  181. {
  182. spacingAfter = dxa2points( paragraph.getSpacingAfter() );
  183. }
  184. // 3) From default
  185. // TODO
  186. // Apply
  187. if ( indentationLeft != -1 )
  188. {
  189. XHTMLUtil.addHTMLStyle( htmlStyle, TEXT_ALIGN, "left" );
  190. XHTMLUtil.addHTMLStyle( htmlStyle, TEXT_INDENT, indentationLeft + "pt" );
  191. }
  192. if ( indentationRight != -1 )
  193. {
  194. XHTMLUtil.addHTMLStyle( htmlStyle, TEXT_ALIGN, "right" );
  195. XHTMLUtil.addHTMLStyle( htmlStyle, TEXT_INDENT, indentationRight + "pt" );
  196. }
  197. if ( firstLineIndent != -1 )
  198. {
  199. XHTMLUtil.addHTMLStyle( htmlStyle, TEXT_INDENT, firstLineIndent + "pt" );
  200. }
  201. // Aligment
  202. ParagraphAlignment alignment = paragraph.getAlignment();
  203. switch ( alignment )
  204. {
  205. case LEFT:
  206. XHTMLUtil.addHTMLStyle( htmlStyle, TEXT_ALIGN, TEXT_ALIGN_LEFT );
  207. break;
  208. case RIGHT:
  209. XHTMLUtil.addHTMLStyle( htmlStyle, TEXT_ALIGN, TEXT_ALIGN_RIGHT );
  210. break;
  211. case CENTER:
  212. XHTMLUtil.addHTMLStyle( htmlStyle, TEXT_ALIGN, TEXT_ALIGN_CENTER );
  213. break;
  214. case BOTH:
  215. XHTMLUtil.addHTMLStyle( htmlStyle, TEXT_ALIGN, TEXT_ALIGN_JUSTIFIED );
  216. break;
  217. }
  218. // Margin bottom/top
  219. if ( spacingBefore != -1 )
  220. {
  221. XHTMLUtil.addHTMLStyle( htmlStyle, MARGIN_TOP, spacingBefore + "pt" );
  222. }
  223. else
  224. {
  225. // By default p element are no margin top/bottom
  226. // XHTMLUtil.addHTMLStyle(htmlStyle, MARGIN_TOP, "0");
  227. XHTMLUtil.addHTMLStyle( htmlStyle, MARGIN_TOP, "0" );
  228. }
  229. if ( spacingAfter != -1 )
  230. {
  231. XHTMLUtil.addHTMLStyle( htmlStyle, MARGIN_BOTTOM, spacingAfter + "pt" );
  232. }
  233. else
  234. {
  235. // By default p element are no margin top/bottom
  236. // XHTMLUtil.addHTMLStyle(htmlStyle, MARGIN_TOP, "0");
  237. XHTMLUtil.addHTMLStyle( htmlStyle, MARGIN_BOTTOM, "0" );
  238. }
  239. // Background-color
  240. String backgroundColor = XWPFParagraphUtils.getBackgroundColor( paragraph );
  241. if ( StringUtils.isNotEmpty( backgroundColor ) )
  242. {
  243. XHTMLUtil.addHTMLStyle( htmlStyle, BACKGROUND_COLOR, "#" + backgroundColor );
  244. }
  245. return htmlStyle;
  246. }
  247. public static StringBuilder getStyle( XWPFRun run, XWPFStyle runStyle, XWPFStyle style, CTDocDefaults defaults )
  248. {
  249. StringBuilder htmlStyle = new StringBuilder();
  250. // Get CTRPr from style+defaults
  251. CTRPr runRprStyle = getRPr( runStyle );
  252. CTRPr rprStyle = getRPr( style );
  253. CTRPr rprDefault = getRPr( defaults );
  254. // Font family
  255. String fontFamily = getFontFamily( run, rprStyle, rprDefault );
  256. if ( StringUtils.isNotEmpty( fontFamily ) )
  257. {
  258. XHTMLUtil.addHTMLStyle( htmlStyle, FONT_FAMILY, "'" + fontFamily + "'" );
  259. }
  260. // 2) Font size
  261. float fontSize = run.getFontSize();
  262. if ( fontSize == -1 )
  263. {
  264. XHTMLUtil.addHTMLStyle( htmlStyle, FONT_SIZE, fontSize + "pt" );
  265. }
  266. // Font Bold
  267. if ( isBold( run, runRprStyle, rprStyle, rprDefault ) )
  268. {
  269. XHTMLUtil.addHTMLStyle( htmlStyle, FONT_WEIGHT, "bold" );
  270. }
  271. // Font Italic
  272. if ( isItalic( run, runRprStyle, rprStyle, rprDefault ) )
  273. {
  274. XHTMLUtil.addHTMLStyle( htmlStyle, FONT_STYLE, "italic" );
  275. }
  276. // Font color
  277. String fontColor = getFontColor( run, runRprStyle, rprStyle, rprDefault );
  278. if ( StringUtils.isNotEmpty( fontColor ) )
  279. {
  280. XHTMLUtil.addHTMLStyle( htmlStyle, COLOR, "#" + fontColor );
  281. }
  282. UnderlinePatterns underlinePatterns = run.getUnderline();
  283. switch ( underlinePatterns )
  284. {
  285. case SINGLE:
  286. XHTMLUtil.addHTMLStyle( htmlStyle, TEXT_DECORATION, TEXT_DECORATION_UNDERLINE );
  287. break;
  288. default:
  289. break;
  290. }
  291. return htmlStyle;
  292. }
  293. // public static ComputedBorder computeBorder(XWPFTable table, XWPFStyle tableStyle,
  294. // CTDocDefaults defaults) {
  295. //
  296. // ComputedBorder computedBorder = new ComputedBorder();
  297. // CTTblBorders localTblBorders = null;
  298. // CTTblBorders styleTblBorders = null;
  299. //
  300. // if (tableStyle != null) {
  301. // styleTblBorders = tableStyle.getCTStyle().getTblPr()
  302. // .getTblBorders();
  303. // }
  304. //
  305. // localTblBorders = table.getCTTbl().getTblPr().getTblBorders();
  306. // computeBorder(localTblBorders, styleTblBorders, computedBorder);
  307. // return computedBorder;
  308. // }
  309. // private static void computeBorder(CTTblBorders localTblBorders,
  310. // CTTblBorders styleTblBorders, ComputedBorder computedBorder) {
  311. // // TODO Auto-generated method stub
  312. //
  313. // }
  314. private static void setBorders( CTTblBorders localTblBorders, CTTblBorders styleTblBorders, StringBuilder htmlStyle )
  315. {
  316. setBorder( localTblBorders != null ? localTblBorders.getTop() : null,
  317. styleTblBorders != null ? styleTblBorders.getTop() : null, htmlStyle, BorderType.TOP );
  318. }
  319. private static void setBorder( CTBorder localBorder, CTBorder styleBorder, StringBuilder htmlStyle,
  320. BorderType borderType )
  321. {
  322. boolean noBorder = false;
  323. float borderSize = -1;
  324. String borderColor = null;
  325. if ( localBorder != null )
  326. {
  327. noBorder = ( STBorder.NONE == localBorder.getVal() );
  328. if ( noBorder )
  329. {
  330. XHTMLUtil.addHTMLStyle( htmlStyle, BORDER, "none" );
  331. return;
  332. }
  333. BigInteger size = localBorder.getSz();
  334. if ( size != null )
  335. {
  336. borderSize = dxa2points( size );
  337. }
  338. borderColor = XWPFTableUtil.getBorderColor( localBorder );
  339. }
  340. if ( styleBorder != null )
  341. {
  342. noBorder = ( STBorder.NONE == styleBorder.getVal() );
  343. if ( noBorder )
  344. {
  345. XHTMLUtil.addHTMLStyle( htmlStyle, BORDER, "none" );
  346. return;
  347. }
  348. if ( borderSize == -1 )
  349. {
  350. BigInteger size = styleBorder.getSz();
  351. if ( size != null )
  352. {
  353. borderSize = dxa2points( size );
  354. }
  355. }
  356. if ( borderColor == null )
  357. {
  358. borderColor = XWPFTableUtil.getBorderColor( styleBorder );
  359. }
  360. }
  361. if ( borderSize != -1 )
  362. {
  363. switch ( borderType )
  364. {
  365. case TOP:
  366. XHTMLUtil.addHTMLStyle( htmlStyle, "border-top-width", borderSize + "pt" );
  367. break;
  368. }
  369. }
  370. if ( borderColor != null )
  371. {
  372. switch ( borderType )
  373. {
  374. case TOP:
  375. XHTMLUtil.addHTMLStyle( htmlStyle, "border-top-color", "#" + borderColor );
  376. break;
  377. }
  378. }
  379. }
  380. public static StringBuilder getStyle( XWPFTableCell tableCell, CTDocDefaults defaults )
  381. {
  382. StringBuilder htmlStyle = new StringBuilder();
  383. CTTcPr tcPr = tableCell.getCTTc().getTcPr();
  384. // Width
  385. CTTblWidth tblWidth = tcPr.getTcW();
  386. if ( tblWidth != null )
  387. {
  388. TableWidth tableWidth = XWPFTableUtil.getTableWidth( tableCell );
  389. boolean percentUnit = tableWidth.percentUnit;
  390. if ( percentUnit )
  391. {
  392. XHTMLUtil.addHTMLStyle( htmlStyle, WIDTH, tableWidth.width + "%" );
  393. }
  394. else
  395. {
  396. XHTMLUtil.addHTMLStyle( htmlStyle, WIDTH, tableWidth.width + "pt" );
  397. }
  398. }
  399. // Background Color
  400. CTShd shd = tcPr.getShd();
  401. if ( shd != null )
  402. {
  403. String backgroundColor = XWPFUtils.getColor( shd.xgetFill() );
  404. if ( StringUtils.isNotEmpty( backgroundColor ) )
  405. {
  406. XHTMLUtil.addHTMLStyle( htmlStyle, BACKGROUND_COLOR, "#" + backgroundColor );
  407. }
  408. }
  409. return htmlStyle;
  410. }
  411. public static CTPPr getPPr( XWPFStyle style )
  412. {
  413. if ( style == null )
  414. {
  415. return null;
  416. }
  417. CTStyle ctStyle = style.getCTStyle();
  418. if ( ctStyle == null )
  419. {
  420. return null;
  421. }
  422. return ctStyle.getPPr();
  423. }
  424. public static StringBuilder getStyle( XWPFPicture picture )
  425. {
  426. StringBuilder htmlStyle = new StringBuilder();
  427. // Position
  428. CTPositiveSize2D ext = picture.getCTPicture().getSpPr().getXfrm().getExt();
  429. long x = ext.getCx();
  430. long y = ext.getCy();
  431. float width = dxa2points( x ) / 635;
  432. float height = dxa2points( y ) / 635;
  433. XHTMLUtil.addHTMLStyle( htmlStyle, WIDTH, width + "pt" );
  434. XHTMLUtil.addHTMLStyle( htmlStyle, HEIGHT, height + "pt" );
  435. return htmlStyle;
  436. }
  437. }