PageRenderTime 194ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/document/fr.opensagres.xdocreport.document.odt/src/main/java/fr/opensagres/xdocreport/document/odt/textstyling/ODTDefaultStylesGenerator.java

https://code.google.com/
Java | 282 lines | 192 code | 40 blank | 50 comment | 13 complexity | 422c97d487bfac7560aa6ad0af110c30 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 fr.opensagres.xdocreport.document.odt.textstyling;
  26. import java.text.DecimalFormat;
  27. import java.text.DecimalFormatSymbols;
  28. /**
  29. * Default implementation : - uses OOo default styles for headers - uses default but renamed styles for others
  30. *
  31. * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
  32. */
  33. public class ODTDefaultStylesGenerator
  34. implements IODTStylesGenerator
  35. {
  36. protected static final String HEADER_PREFIX = "Heading_20_";
  37. protected static final String[] BULLET_CHARS = { "\u2022", "\u25e6", "\u25aa" };
  38. protected static final String OL_STYLE_NAME = "XDocReport_OL";
  39. protected static final String UL_STYLE_NAME = "XDocReport_UL";
  40. protected static final String LIST_P_STYLE_NAME_SUFFIX = "_P";
  41. protected static final double BULLET_STEP = 0.635;
  42. protected static final String BOLD_STYLE_NAME = "XDocReport_Bold";
  43. protected static final String ITALIC_STYLE_NAME = "XDocReport_Italic";
  44. protected static final String BOLD_ITALIC_STYLE_NAME = "XDocReport_BoldItalic";
  45. // font size for the default OOo Headers
  46. protected static String[] TITLE_FONT_SIZE = { "115%", "14pt", "14pt", "85%", "85%", "75%" };
  47. /**
  48. * protected static String[] HEADING_STYLES = new String[] {
  49. * "<style:style style:name=\"Heading_20_1\" style:display-name=\"Heading 1\" style:family=\"paragraph\" style:parent-style-name=\"Heading\" style:next-style-name=\"Text_20_body\" style:default-outline-level=\"1\" style:class=\"text\"><style:text-properties "
  50. * +
  51. * "fo:font-size=\"115%\" fo:font-weight=\"bold\" style:font-size-asian=\"115%\" style:font-weight-asian=\"bold\" style:font-size-complex=\"115%\" style:font-weight-complex=\"bold\"/></style:style>"
  52. * ,
  53. * "<style:style style:name=\"Heading_20_2\" style:display-name=\"Heading 2\" style:family=\"paragraph\" style:parent-style-name=\"Heading\" style:next-style-name=\"Text_20_body\" style:default-outline-level=\"2\" style:class=\"text\"><style:text-properties fo:font-size=\"14pt\" fo:font-style=\"italic\" fo:font-weight=\"bold\" style:font-size-asian=\"14pt\" style:font-style-asian=\"italic\" style:font-weight-asian=\"bold\" style:font-size-complex=\"14pt\" style:font-style-complex=\"italic\" style:font-weight-complex=\"bold\"/></style:style>"
  54. * ,
  55. * "<style:style style:name=\"Heading_20_3\" style:display-name=\"Heading 3\" style:family=\"paragraph\" style:parent-style-name=\"Heading\" style:next-style-name=\"Text_20_body\" style:default-outline-level=\"3\" style:class=\"text\"><style:text-properties fo:font-size=\"14pt\" fo:font-weight=\"bold\" style:font-size-asian=\"14pt\" style:font-weight-asian=\"bold\" style:font-size-complex=\"14pt\" style:font-weight-complex=\"bold\"/></style:style>"
  56. * ,
  57. * "<style:style style:name=\"Heading_20_4\" style:display-name=\"Heading 4\" style:family=\"paragraph\" style:parent-style-name=\"Heading\" style:next-style-name=\"Text_20_body\" style:default-outline-level=\"4\" style:class=\"text\"><style:text-properties fo:font-size=\"85%\" fo:font-style=\"italic\" fo:font-weight=\"bold\" style:font-size-asian=\"85%\" style:font-style-asian=\"italic\" style:font-weight-asian=\"bold\" style:font-size-complex=\"85%\" style:font-style-complex=\"italic\" style:font-weight-complex=\"bold\"/></style:style>"
  58. * ,
  59. * "<style:style style:name=\"Heading_20_5\" style:display-name=\"Heading 5\" style:family=\"paragraph\" style:parent-style-name=\"Heading\" style:next-style-name=\"Text_20_body\" style:default-outline-level=\"5\" style:class=\"text\"><style:text-properties fo:font-size=\"85%\" fo:font-weight=\"bold\" style:font-size-asian=\"85%\" style:font-weight-asian=\"bold\" style:font-size-complex=\"85%\" style:font-weight-complex=\"bold\"/></style:style>"
  60. * ,
  61. * "<style:style style:name=\"Heading_20_6\" style:display-name=\"Heading 6\" style:family=\"paragraph\" style:parent-style-name=\"Heading\" style:next-style-name=\"Text_20_body\" style:default-outline-level=\"6\" style:class=\"text\"><style:text-properties fo:font-size=\"75%\" fo:font-weight=\"bold\" style:font-size-asian=\"75%\" style:font-weight-asian=\"bold\" style:font-size-complex=\"75%\" style:font-weight-complex=\"bold\"/></style:style>"
  62. * };
  63. */
  64. protected String getBulletChar( int level )
  65. {
  66. return BULLET_CHARS[( level - 1 ) % 3];
  67. }
  68. public String getHeaderStyleName( int level )
  69. {
  70. return HEADER_PREFIX + level;
  71. }
  72. public String generateHeaderStyle( int level )
  73. {
  74. StringBuilder style = new StringBuilder( "<style:style style:name=\"" );
  75. style.append( getHeaderStyleName( level ) );
  76. style.append( "\" style:display-name=\"" );
  77. style.append( "Heading " + level );
  78. style.append( "\" style:family=\"paragraph\" style:parent-style-name=\"Heading\" style:next-style-name=\"Text_20_body\" " );
  79. style.append( "style:default-outline-level=\"" );
  80. style.append( level + "\" " );
  81. style.append( "style:class=\"text\"><style:text-properties " );
  82. style.append( "fo:font-size=\"" );
  83. style.append( TITLE_FONT_SIZE[level - 1] );
  84. style.append( "\" fo:font-weight=\"bold\" " );
  85. if ( level % 2 == 0 )
  86. {
  87. style.append( " fo:font-style=\"italic\" " );
  88. }
  89. style.append( "/></style:style>" );
  90. return style.toString();
  91. }
  92. public String generateTextStyles()
  93. {
  94. StringBuilder region = new StringBuilder();
  95. region.append( generateStyle( BOLD_STYLE_NAME, true, false ) );
  96. region.append( generateStyle( ITALIC_STYLE_NAME, false, true ) );
  97. region.append( generateStyle( BOLD_ITALIC_STYLE_NAME, true, true ) );
  98. return region.toString();
  99. }
  100. private String generateStyle( String styleName, boolean bold, boolean italic )
  101. {
  102. StringBuilder region = new StringBuilder();
  103. region.append( "<style:style style:name=\"" );
  104. region.append( styleName );
  105. region.append( "\" style:family=\"text\">" );
  106. region.append( "<style:text-properties" );
  107. if ( bold )
  108. {
  109. region.append( " fo:font-weight=\"bold\"" );
  110. }
  111. if ( italic )
  112. {
  113. region.append( " fo:font-style=\"italic\"" );
  114. }
  115. region.append( "/></style:style>" );
  116. return region.toString();
  117. }
  118. public String generateListStyle()
  119. {
  120. StringBuilder region = new StringBuilder();
  121. region.append( generateListStyle( true ) );
  122. region.append( generateListStyle( false ) );
  123. return region.toString();
  124. }
  125. protected String generateListStyle( boolean ordered )
  126. {
  127. StringBuilder region = new StringBuilder();
  128. String styleName = UL_STYLE_NAME;
  129. if ( ordered )
  130. {
  131. styleName = OL_STYLE_NAME;
  132. }
  133. // generate Paragraph styles reference
  134. region.append( "<style:style style:name=\"" );
  135. region.append( styleName + LIST_P_STYLE_NAME_SUFFIX );
  136. region.append( "\" style:family=\"paragraph\" style:parent-style-name=\"Standard\" style:list-style-name=\"" );
  137. region.append( styleName );
  138. region.append( "\"/>" );
  139. // generate the list style
  140. region.append( "<text:list-style style:name=\"" );
  141. region.append( styleName );
  142. region.append( "\">" );
  143. // generate styles for 10 levels
  144. for ( int level = 1; level <= 10; level++ )
  145. {
  146. region.append( generateBulletStyle( level, ordered ) );
  147. }
  148. region.append( "</text:list-style>" );
  149. return region.toString();
  150. }
  151. protected String generateBulletStyle( Integer level, boolean ordered )
  152. {
  153. StringBuilder region = new StringBuilder();
  154. if ( ordered )
  155. {
  156. region.append( "<text:list-level-style-number text:level=\"" );
  157. }
  158. else
  159. {
  160. region.append( "<text:list-level-style-bullet text:level=\"" );
  161. }
  162. region.append( level.toString() );
  163. region.append( "\" text:style-name=\"" );
  164. if ( ordered )
  165. {
  166. region.append( "Numbering_20_Symbols" );
  167. }
  168. else
  169. {
  170. region.append( "Bullet_20_Symbols" );
  171. }
  172. region.append( "\" style:num-suffix=\".\" " );
  173. if ( ordered )
  174. {
  175. region.append( "style:num-format=\"1\">" );
  176. }
  177. else
  178. {
  179. region.append( "text:bullet-char=\"" + getBulletChar( level ) + "\">" );
  180. }
  181. region.append( "<style:list-level-properties text:list-level-position-and-space-mode=\"label-alignment\">" );
  182. region.append( "<style:list-level-label-alignment text:label-followed-by=\"listtab\" " );
  183. DecimalFormatSymbols decimalFormat = new DecimalFormatSymbols();
  184. decimalFormat.setDecimalSeparator( '.' );
  185. String offset = new DecimalFormat( "#.###", decimalFormat ).format( BULLET_STEP * ( level + 1 ) );
  186. region.append( " text:list-tab-stop-position=\"" );
  187. region.append( offset );
  188. region.append( "cm\" fo:text-indent=\"-0.635cm\" fo:margin-left=\"" );
  189. region.append( offset );
  190. region.append( "cm\"/>" );
  191. region.append( "</style:list-level-properties>" );
  192. if ( ordered )
  193. {
  194. region.append( "</text:list-level-style-number>" );
  195. }
  196. else
  197. {
  198. region.append( "</text:list-level-style-bullet>" );
  199. }
  200. return region.toString();
  201. }
  202. public int getHeaderStylesCount()
  203. {
  204. return 6;
  205. }
  206. public int getHeaderStyleNameLevel( String styleName )
  207. {
  208. if ( styleName != null && styleName.startsWith( HEADER_PREFIX ) )
  209. {
  210. String headerIdx = styleName.substring( 11 );
  211. return Integer.parseInt( headerIdx );
  212. }
  213. else
  214. {
  215. return -1;
  216. }
  217. }
  218. public String getOLStyleName()
  219. {
  220. return OL_STYLE_NAME;
  221. }
  222. public String getULStyleName()
  223. {
  224. return UL_STYLE_NAME;
  225. }
  226. public String getListItemParagraphStyleNameSuffix()
  227. {
  228. return LIST_P_STYLE_NAME_SUFFIX;
  229. }
  230. public String getBoldStyleName()
  231. {
  232. return BOLD_STYLE_NAME;
  233. }
  234. public String getItalicStyleName()
  235. {
  236. return ITALIC_STYLE_NAME;
  237. }
  238. public String getBoldItalicStyleName()
  239. {
  240. return BOLD_ITALIC_STYLE_NAME;
  241. }
  242. }