You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RtfText.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.rtf.rtflib.rtfdoc;
  19. /*
  20. * This file is part of the RTF library of the FOP project, which was originally
  21. * created by Bertrand Delacretaz bdelacretaz@codeconsult.ch and by other
  22. * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
  23. * the FOP project.
  24. */
  25. import java.io.IOException;
  26. import java.io.Writer;
  27. import org.apache.fop.apps.FOPException;
  28. /**
  29. * <p>Model of a text run (a piece of text with attributes) in an RTF document.</p>
  30. *
  31. * <p>This work was authored by Bertrand Delacretaz (bdelacretaz@codeconsult.ch).</p>
  32. */
  33. public class RtfText extends RtfElement {
  34. // char code for non-breakable space
  35. private static final int CHAR_NBSP = 160;
  36. private static final int CHAR_TAB = 137;
  37. private static final int CHAR_NEW_LINE = 141;
  38. /* these next two variables are used to encode bold formating in the
  39. * raw xml text. Usefull when specific words or phrases are to be bolded
  40. * but their placement and length change. Thus the bold formatting becomes
  41. * part of the data. The same method can be used for implementing other types
  42. * of raw text formatting.
  43. */
  44. private static final int CHAR_BOLD_START = 130;
  45. private static final int CHAR_BOLD_END = 131;
  46. /** members */
  47. private String text;
  48. private final RtfAttributes attr;
  49. /** RtfText attributes: attribute names are RTF control word names to avoid
  50. * additional mapping */
  51. /** constant for bold */
  52. public static final String ATTR_BOLD = "b";
  53. /** constant for italic */
  54. public static final String ATTR_ITALIC = "i";
  55. /** constant for underline */
  56. public static final String ATTR_UNDERLINE = "ul";
  57. /** constant for underline */
  58. public static final String ATTR_STRIKETHROUGH = "strike";
  59. /** constant for font size */
  60. public static final String ATTR_FONT_SIZE = "fs";
  61. /** constant for font family */
  62. public static final String ATTR_FONT_FAMILY = "f";
  63. /** constant for font color */
  64. public static final String ATTR_FONT_COLOR = "cf";
  65. /** constant for background color */
  66. public static final String ATTR_BACKGROUND_COLOR = "chcbpat"; // Added by Boris on 06/25//02
  67. /** constant for superscript */
  68. public static final String ATTR_SUPERSCRIPT = "super";
  69. /** constant for subscript */
  70. public static final String ATTR_SUBSCRIPT = "sub";
  71. /** RtfText attributes: paragraph shading attributes */
  72. /** Constant for the shading of the paragraph */
  73. public static final String SHADING = "shading";
  74. /** Constant for the document's color tableshading of the paragraph */
  75. public static final String SHADING_FRONT_COLOR = "cfpat";
  76. /** Constant for the 100% shading of the paragraph */
  77. public static final int FULL_SHADING = 10000;
  78. /** RtfText attributes: alignment attributes */
  79. /** constant for align center */
  80. public static final String ALIGN_CENTER = "qc";
  81. /** constant for align left */
  82. public static final String ALIGN_LEFT = "ql";
  83. /** constant for align right */
  84. public static final String ALIGN_RIGHT = "qr";
  85. /** constant for align justified */
  86. public static final String ALIGN_JUSTIFIED = "qj";
  87. /** constant for align distributed */
  88. public static final String ALIGN_DISTRIBUTED = "qd";
  89. /** RtfText attributes: border attributes */
  90. //added by Chris Scott
  91. /** constant for bottom single border */
  92. public static final String BDR_BOTTOM_SINGLE = "brdrb\\brsp40\\brdrs";
  93. /** constant for bottom double border */
  94. public static final String BDR_BOTTOM_DOUBLE = "brdrb\\brsp40\\brdrdb";
  95. /** constant for bottom embossed border */
  96. public static final String BDR_BOTTOM_EMBOSS = "brdrb\\brsp40\\brdremboss";
  97. /** constant for bottom dotted border */
  98. public static final String BDR_BOTTOM_DOTTED = "brdrb\\brsp40\\brdrdot";
  99. /** constant for bottom dashed border */
  100. public static final String BDR_BOTTOM_DASH = "brdrb\\brsp40\\brdrdash";
  101. /** RtfText attributes: fields */
  102. //must be carefull of group markings and star control
  103. //ie page field:
  104. // "{\field {\*\fldinst {PAGE}} {\fldrslt}}"
  105. /** constant for field */
  106. public static final String RTF_FIELD = "field";
  107. /** constant for field page */
  108. public static final String RTF_FIELD_PAGE = "fldinst { PAGE }";
  109. /** constant for field result */
  110. public static final String RTF_FIELD_RESULT = "fldrslt";
  111. /**RtfText attributes: indentation attributes */
  112. //added by Chris Scott
  113. /** constant for left indent body */
  114. public static final String LEFT_INDENT_BODY = "li";
  115. /** constant for left indent first */
  116. public static final String LEFT_INDENT_FIRST = "fi";
  117. /** constant for right indent body */
  118. public static final String RIGHT_INDENT_BODY = "ri";
  119. /** constant for center tab */
  120. public static final String TAB_CENTER = "tqc\\tx";
  121. /** constant for right tab */
  122. public static final String TAB_RIGHT = "tqr\\tx";
  123. /** constant for tab leader dots */
  124. public static final String TAB_LEADER_DOTS = "tldot";
  125. /** constant for tab leader hyphens */
  126. public static final String TAB_LEADER_HYPHEN = "tlhyph";
  127. /** constant for tab leader underscores */
  128. public static final String TAB_LEADER_UNDER = "tlul";
  129. /** constant for tab leader thick */
  130. public static final String TAB_LEADER_THICK = "tlth";
  131. /** constant for tab leader equals */
  132. public static final String TAB_LEADER_EQUALS = "tleq";
  133. /** Space before/after a paragraph */
  134. //these lines were added by Boris Pouderous
  135. public static final String SPACE_BEFORE = "sb";
  136. /** Space after a paragraph */
  137. public static final String SPACE_AFTER = "sa";
  138. /** RtfText attributes: this must contain all allignment attributes names */
  139. public static final String[] ALIGNMENT = new String []
  140. {
  141. ALIGN_CENTER, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_JUSTIFIED, ALIGN_DISTRIBUTED
  142. };
  143. /** RtfText attributes:: this must contain all border attribute names*/
  144. //this line added by Chris Scott, Westinghouse
  145. public static final String[] BORDER = new String []
  146. {
  147. BDR_BOTTOM_SINGLE, BDR_BOTTOM_DOUBLE, BDR_BOTTOM_EMBOSS, BDR_BOTTOM_DOTTED,
  148. BDR_BOTTOM_DASH
  149. };
  150. /** String array of indent constants */
  151. public static final String[] INDENT = new String []
  152. {
  153. LEFT_INDENT_BODY, LEFT_INDENT_FIRST
  154. };
  155. /** String array of tab constants */
  156. public static final String[] TABS = new String []
  157. {
  158. TAB_CENTER, TAB_RIGHT, TAB_LEADER_DOTS, TAB_LEADER_HYPHEN, TAB_LEADER_UNDER,
  159. TAB_LEADER_THICK, TAB_LEADER_EQUALS
  160. };
  161. /** RtfText attributes: this must contain all attribute names */
  162. public static final String [] ATTR_NAMES = {
  163. ATTR_BOLD,
  164. ATTR_ITALIC,
  165. ATTR_UNDERLINE,
  166. ATTR_FONT_SIZE,
  167. ATTR_FONT_FAMILY,
  168. ATTR_FONT_COLOR,
  169. ATTR_BACKGROUND_COLOR
  170. };
  171. /** Create an RtfText in given IRtfTextContainer.
  172. * @param str optional initial text content
  173. */
  174. RtfText(IRtfTextContainer parent, Writer w, String str, RtfAttributes attr)
  175. throws IOException {
  176. super((RtfContainer)parent, w);
  177. this.text = str;
  178. this.attr = attr;
  179. }
  180. /**
  181. * Write our text to the RTF stream
  182. * @throws IOException for I/O problems
  183. */
  184. public void writeRtfContent() throws IOException {
  185. writeChars: {
  186. //these lines were added by Boris Pouderous
  187. if (attr != null) {
  188. writeAttributes(attr, new String[] {RtfText.SPACE_BEFORE});
  189. writeAttributes(attr, new String[] {RtfText.SPACE_AFTER});
  190. }
  191. if (isTab()) {
  192. writeControlWord("tab");
  193. } else if (isNewLine()) {
  194. break writeChars;
  195. } else if (isBold(true)) {
  196. writeControlWord("b");
  197. } else if (isBold(false)) {
  198. writeControlWord("b0");
  199. // TODO not optimal, consecutive RtfText with same attributes
  200. // could be written without group marks
  201. } else {
  202. writeGroupMark(true);
  203. if (attr != null && mustWriteAttributes()) {
  204. writeAttributes(attr, RtfText.ATTR_NAMES);
  205. }
  206. RtfStringConverter.getInstance().writeRtfString(writer, text);
  207. writeGroupMark(false);
  208. }
  209. }
  210. }
  211. /** true if our text attributes must be written */
  212. private boolean mustWriteAttributes() {
  213. return !isEmpty() && !isNbsp();
  214. }
  215. /** IRtfTextContainer requirement:
  216. * @return a copy of our attributes
  217. * @throws FOPException if attributes cannot be cloned
  218. */
  219. public RtfAttributes getTextContainerAttributes() throws FOPException {
  220. if (attrib == null) {
  221. return null;
  222. }
  223. try {
  224. return (RtfAttributes)this.attrib.clone();
  225. } catch (CloneNotSupportedException e) {
  226. throw new FOPException(e);
  227. }
  228. }
  229. /** direct access to our text */
  230. String getText() {
  231. return text;
  232. }
  233. /** direct access to our text */
  234. void setText(String str) {
  235. text = str;
  236. }
  237. /**
  238. * Checks whether the text is empty.
  239. *
  240. * @return true If m_text is null\n
  241. * false m_text is set
  242. */
  243. public boolean isEmpty() {
  244. return text == null || text.trim().length() == 0;
  245. }
  246. /**
  247. * True if text contains a single non-breaking space (#160).
  248. * TODO make this more general and/or merge with isEmpty? -- what happen
  249. * with empty paragraphs, if they will be removed, than NO, else ok
  250. *
  251. * @return true If m_text is character 160\n
  252. * false m_text is not a nbsp
  253. */
  254. public boolean isNbsp() {
  255. if (!isEmpty()) {
  256. if (text.trim().length() == 1 && text.charAt(0) == CHAR_NBSP) {
  257. return true;
  258. }
  259. }
  260. return false;
  261. }
  262. /**
  263. * @return true if the text is a tab character
  264. */
  265. public boolean isTab() {
  266. return (text.trim().length() == 1 && text.charAt(0) == CHAR_TAB);
  267. }
  268. /**
  269. * @return true if text is a newline character
  270. */
  271. public boolean isNewLine() {
  272. return (text.trim().length() == 1 && text.charAt(0) == CHAR_NEW_LINE);
  273. }
  274. /**
  275. * @param isStart set to true if processing the start of the text (??)
  276. * @return true if text is bold
  277. */
  278. public boolean isBold(boolean isStart) {
  279. if (isStart) {
  280. return (text.trim().length() == 1 && text.charAt(0) == CHAR_BOLD_START);
  281. } else {
  282. return (text.trim().length() == 1 && text.charAt(0) == CHAR_BOLD_END);
  283. }
  284. }
  285. /** @return the attributes of our text */
  286. public RtfAttributes getTextAttributes() {
  287. return attr;
  288. }
  289. }