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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * $Id$
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. /*
  52. * This file is part of the RTF library of the FOP project, which was originally
  53. * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
  54. * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
  55. * the FOP project.
  56. */
  57. package org.apache.fop.render.rtf.rtflib.rtfdoc;
  58. import java.io.IOException;
  59. import java.io.Writer;
  60. /** Model of a text run (a piece of text with attributes) in an RTF document
  61. * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
  62. */
  63. public class RtfText extends RtfElement {
  64. // char code for non-breakable space
  65. private static final int CHAR_NBSP = 160;
  66. private static final int CHAR_TAB = 137;
  67. private static final int CHAR_NEW_LINE = 141;
  68. /* these next two variables are used to encode bold formating in the
  69. * raw xml text. Usefull when specific words or phrases are to be bolded
  70. * but their placement and length change. Thus the bold formatting becomes
  71. * part of the data. The same method can be used for implementing other types
  72. * of raw text formatting.
  73. */
  74. private static final int CHAR_BOLD_START = 130;
  75. private static final int CHAR_BOLD_END = 131;
  76. /** members */
  77. private String text;
  78. private final RtfAttributes attr;
  79. /** RtfText attributes: attribute names are RTF control word names to avoid
  80. * additional mapping */
  81. /** constant for bold */
  82. public static final String ATTR_BOLD = "b";
  83. /** constant for italic */
  84. public static final String ATTR_ITALIC = "i";
  85. /** constant for underline */
  86. public static final String ATTR_UNDERLINE = "ul";
  87. /** constant for font size */
  88. public static final String ATTR_FONT_SIZE = "fs";
  89. /** constant for font family */
  90. public static final String ATTR_FONT_FAMILY = "f";
  91. /** constant for font color */
  92. public static final String ATTR_FONT_COLOR = "cf";
  93. /** constant for background color */
  94. public static final String ATTR_BACKGROUND_COLOR = "chcbpat"; // Added by Boris on 06/25//02
  95. /** RtfText attributes: alignment attributes */
  96. /** constant for align center */
  97. public static final String ALIGN_CENTER = "qc";
  98. /** constant for align left */
  99. public static final String ALIGN_LEFT = "ql";
  100. /** constant for align right */
  101. public static final String ALIGN_RIGHT = "qr";
  102. /** constant for align justified */
  103. public static final String ALIGN_JUSTIFIED = "qj";
  104. /** constant for align distributed */
  105. public static final String ALIGN_DISTRIBUTED = "qd";
  106. /** RtfText attributes: border attributes */
  107. //added by Chris Scott
  108. /** constant for bottom single border */
  109. public static final String BDR_BOTTOM_SINGLE = "brdrb\\brsp40\\brdrs";
  110. /** constant for bottom double border */
  111. public static final String BDR_BOTTOM_DOUBLE = "brdrb\\brsp40\\brdrdb";
  112. /** constant for bottom embossed border */
  113. public static final String BDR_BOTTOM_EMBOSS = "brdrb\\brsp40\\brdremboss";
  114. /** constant for bottom dotted border */
  115. public static final String BDR_BOTTOM_DOTTED = "brdrb\\brsp40\\brdrdot";
  116. /** constant for bottom dashed border */
  117. public static final String BDR_BOTTOM_DASH = "brdrb\\brsp40\\brdrdash";
  118. /** RtfText attributes: fields */
  119. //must be carefull of group markings and star control
  120. //ie page field:
  121. // "{\field {\*\fldinst {PAGE}} {\fldrslt}}"
  122. /** constant for field */
  123. public static final String RTF_FIELD = "field";
  124. /** constant for field page */
  125. public static final String RTF_FIELD_PAGE = "fldinst { PAGE }";
  126. /** constant for field result */
  127. public static final String RTF_FIELD_RESULT = "fldrslt";
  128. /**RtfText attributes: indentation attributes */
  129. //added by Chris Scott
  130. /** constant for left indent body */
  131. public static final String LEFT_INDENT_BODY = "li";
  132. /** constant for left indent first */
  133. public static final String LEFT_INDENT_FIRST = "fi-";
  134. /** constant for right indent body */
  135. public static final String RIGHT_INDENT_BODY = "ri";
  136. /** constant for center tab */
  137. public static final String TAB_CENTER = "tqc\\tx";
  138. /** constant for right tab */
  139. public static final String TAB_RIGHT = "tqr\\tx";
  140. /** constant for tab leader dots */
  141. public static final String TAB_LEADER_DOTS = "tldot";
  142. /** constant for tab leader hyphens */
  143. public static final String TAB_LEADER_HYPHEN = "tlhyph";
  144. /** constant for tab leader underscores */
  145. public static final String TAB_LEADER_UNDER = "tlul";
  146. /** constant for tab leader thick */
  147. public static final String TAB_LEADER_THICK = "tlth";
  148. /** constant for tab leader equals */
  149. public static final String TAB_LEADER_EQUALS = "tleq";
  150. /** Space before/after a paragraph */
  151. //these lines were added by Boris Pouderous
  152. public static final String SPACE_BEFORE = "sb";
  153. /** Space after a paragraph */
  154. public static final String SPACE_AFTER = "sa";
  155. /** RtfText attributes: this must contain all allignment attributes names */
  156. public static final String[] ALIGNMENT = new String []
  157. {
  158. ALIGN_CENTER, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_JUSTIFIED, ALIGN_DISTRIBUTED
  159. };
  160. /** RtfText attributes:: this must contain all border attribute names*/
  161. //this line added by Chris Scott, Westinghouse
  162. public static final String[] BORDER = new String []
  163. {
  164. BDR_BOTTOM_SINGLE, BDR_BOTTOM_DOUBLE, BDR_BOTTOM_EMBOSS, BDR_BOTTOM_DOTTED,
  165. BDR_BOTTOM_DASH
  166. };
  167. /** String array of indent constants */
  168. public static final String[] INDENT = new String []
  169. {
  170. LEFT_INDENT_BODY, LEFT_INDENT_FIRST
  171. };
  172. /** String array of tab constants */
  173. public static final String[] TABS = new String []
  174. {
  175. TAB_CENTER, TAB_RIGHT, TAB_LEADER_DOTS, TAB_LEADER_HYPHEN, TAB_LEADER_UNDER,
  176. TAB_LEADER_THICK, TAB_LEADER_EQUALS
  177. };
  178. /** RtfText attributes: this must contain all attribute names */
  179. public static final String [] ATTR_NAMES = {
  180. ATTR_BOLD,
  181. ATTR_ITALIC,
  182. ATTR_UNDERLINE,
  183. ATTR_FONT_SIZE,
  184. ATTR_FONT_FAMILY,
  185. ATTR_FONT_COLOR,
  186. ATTR_BACKGROUND_COLOR
  187. };
  188. /** Create an RtfText in given IRtfTextContainer.
  189. * @param str optional initial text content
  190. */
  191. RtfText(IRtfTextContainer parent, Writer w, String str, RtfAttributes attr)
  192. throws IOException {
  193. super((RtfContainer)parent, w);
  194. text = str;
  195. this.attr = attr;
  196. }
  197. /**
  198. * Write our text to the RTF stream
  199. * @throws IOException for I/O problems
  200. */
  201. public void writeRtfContent() throws IOException {
  202. writeChars: {
  203. //these lines were added by Boris Pouderous
  204. if (attr != null) {
  205. writeAttributes(attr, new String[] {RtfText.SPACE_BEFORE});
  206. writeAttributes(attr, new String[] {RtfText.SPACE_AFTER});
  207. }
  208. if (isTab()) {
  209. writeControlWord("tab");
  210. } else if (isNewLine()) {
  211. break writeChars;
  212. } else if (isBold(true)) {
  213. writeControlWord("b");
  214. } else if (isBold(false)) {
  215. writeControlWord("b0");
  216. // TODO not optimal, consecutive RtfText with same attributes
  217. // could be written without group marks
  218. } else {
  219. writeGroupMark(true);
  220. if (attr != null && mustWriteAttributes()) {
  221. writeAttributes(attr, RtfText.ATTR_NAMES);
  222. }
  223. RtfStringConverter.getInstance().writeRtfString(writer, text);
  224. writeGroupMark(false);
  225. }
  226. }
  227. }
  228. /** true if our text attributes must be written */
  229. private boolean mustWriteAttributes() {
  230. return !isEmpty() && !isNbsp();
  231. }
  232. /** IRtfTextContainer requirement:
  233. * @return a copy of our attributes */
  234. public RtfAttributes getTextContainerAttributes() {
  235. if (attrib == null) {
  236. return null;
  237. }
  238. return (RtfAttributes)this.attrib.clone();
  239. }
  240. /** direct access to our text */
  241. String getText() {
  242. return text;
  243. }
  244. /** direct access to our text */
  245. void setText(String str) {
  246. text = str;
  247. }
  248. /**
  249. * Checks whether the text is empty.
  250. *
  251. * @return true If m_text is null\n
  252. * false m_text is set
  253. */
  254. public boolean isEmpty () {
  255. return text == null || text.trim().length() == 0;
  256. }
  257. /**
  258. * True if text contains a single non-breaking space (#160).
  259. * TODO make this more general and/or merge with isEmpty? -- what happen
  260. * with empty paragraphs, if they will be removed, than NO, else ok
  261. *
  262. * @return true If m_text is character 160\n
  263. * false m_text is not a nbsp
  264. */
  265. public boolean isNbsp () {
  266. if (!isEmpty ()) {
  267. if (text.trim ().length () == 1 && text.charAt (0) == CHAR_NBSP) {
  268. return true;
  269. }
  270. }
  271. return false;
  272. }
  273. /**
  274. * @return true if the text is a tab character
  275. */
  276. public boolean isTab() {
  277. if (text.trim().length() == 1 && text.charAt(0) == CHAR_TAB) {
  278. return true;
  279. } else {
  280. return false;
  281. }
  282. }
  283. /**
  284. * @return true if text is a newline character
  285. */
  286. public boolean isNewLine() {
  287. if (text.trim().length() == 1 && text.charAt(0) == CHAR_NEW_LINE) {
  288. return true;
  289. } else {
  290. return false;
  291. }
  292. }
  293. /**
  294. * @param isStart set to true if processing the start of the text (??)
  295. * @return true if text is bold
  296. */
  297. public boolean isBold(boolean isStart) {
  298. if (isStart) {
  299. if (text.trim().length() == 1 && text.charAt(0) == CHAR_BOLD_START) {
  300. return true;
  301. }
  302. } else {
  303. if (text.trim().length() == 1 && text.charAt(0) == CHAR_BOLD_END) {
  304. return true;
  305. } else {
  306. return false;
  307. }
  308. }
  309. return false;
  310. }
  311. /** @return the attributes of our text */
  312. public RtfAttributes getTextAttributes() {
  313. return attr;
  314. }
  315. }