您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RtfTextrun.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * ============================================================================
  3. * The Apache Software License, Version 1.1
  4. * ============================================================================
  5. *
  6. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without modifica-
  9. * tion, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. *
  18. * 3. The end-user documentation included with the redistribution, if any, must
  19. * include the following acknowledgment: "This product includes software
  20. * developed by the Apache Software Foundation (http://www.apache.org/)."
  21. * Alternately, this acknowledgment may appear in the software itself, if
  22. * and wherever such third-party acknowledgments normally appear.
  23. *
  24. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  25. * endorse or promote products derived from this software without prior
  26. * written permission. For written permission, please contact
  27. * apache@apache.org.
  28. *
  29. * 5. Products derived from this software may not be called "Apache", nor may
  30. * "Apache" appear in their name, without prior written permission of the
  31. * Apache Software Foundation.
  32. *
  33. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  35. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  36. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  37. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  38. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  39. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  40. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  41. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  42. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. * ============================================================================
  44. *
  45. * This software consists of voluntary contributions made by many individuals
  46. * on behalf of the Apache Software Foundation and was originally created by
  47. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  48. * Software Foundation, please see <http://www.apache.org/>.
  49. */
  50. /*
  51. * This file is part of the RTF library of the FOP project.
  52. */
  53. package org.apache.fop.render.rtf.rtflib.rtfdoc;
  54. import java.io.IOException;
  55. import java.io.Writer;
  56. import java.util.LinkedList;
  57. import java.util.List;
  58. import java.util.Iterator;
  59. import java.io.IOException;
  60. import org.apache.fop.render.rtf.rtflib.exceptions.RtfStructureException;
  61. /** Class which contains a linear text run. It has methods to add attributes, text, paragraph breaks....
  62. * @author Peter Herweg, pherweg@web.de
  63. */
  64. public class RtfTextrun extends RtfContainer {
  65. /** Class which represents the opening of a RTF group mark.*/
  66. private class RtfOpenGroupMark extends RtfElement
  67. {
  68. RtfOpenGroupMark(RtfContainer parent, Writer w, RtfAttributes attr)
  69. throws IOException {
  70. super(parent, w, attr);
  71. }
  72. /**
  73. * @return true if this element would generate no "useful" RTF content
  74. */
  75. public boolean isEmpty() {
  76. return false;
  77. }
  78. /**
  79. * write RTF code of all our children
  80. * @throws IOException for I/O problems
  81. */
  82. protected void writeRtfContent() throws IOException {
  83. writeGroupMark(true);
  84. writeAttributes(getRtfAttributes(), null);
  85. }
  86. }
  87. /** Class which represents the closing of a RTF group mark.*/
  88. private class RtfCloseGroupMark extends RtfElement
  89. {
  90. RtfCloseGroupMark(RtfContainer parent, Writer w)
  91. throws IOException {
  92. super(parent, w);
  93. }
  94. /**
  95. * @return true if this element would generate no "useful" RTF content
  96. */
  97. public boolean isEmpty() {
  98. return false;
  99. }
  100. /**
  101. * write RTF code of all our children
  102. * @throws IOException for I/O problems
  103. */
  104. protected void writeRtfContent() throws IOException {
  105. writeGroupMark(false);
  106. }
  107. }
  108. /** Class which represents a paragraph break.*/
  109. private class RtfParagraphBreak extends RtfElement
  110. {
  111. RtfParagraphBreak(RtfContainer parent, Writer w)
  112. throws IOException {
  113. super(parent, w);
  114. }
  115. /**
  116. * @return true if this element would generate no "useful" RTF content
  117. */
  118. public boolean isEmpty() {
  119. return false;
  120. }
  121. /**
  122. * write RTF code of all our children
  123. * @throws IOException for I/O problems
  124. */
  125. protected void writeRtfContent() throws IOException {
  126. writeControlWord("par");
  127. }
  128. }
  129. /** Create an RTF container as a child of given container */
  130. RtfTextrun(RtfContainer parent, Writer w, RtfAttributes attrs) throws IOException {
  131. super(parent, w, attrs);
  132. }
  133. public void pushAttributes(RtfAttributes attrs) throws IOException {
  134. RtfOpenGroupMark r=new RtfOpenGroupMark(this, writer, attrs);
  135. }
  136. public void popAttributes() throws IOException {
  137. RtfCloseGroupMark r=new RtfCloseGroupMark(this, writer);
  138. }
  139. public void addString(String s) throws IOException {
  140. RtfString r=new RtfString(this, writer, s);
  141. }
  142. public void addParagraphBreak() throws IOException {
  143. RtfParagraphBreak r=new RtfParagraphBreak(this, writer);
  144. }
  145. public void addPageNumber(RtfAttributes attr) throws IOException {
  146. RtfPageNumber r=new RtfPageNumber(this, writer, attr);
  147. }
  148. /**
  149. * Adds a new RtfTextrun to the given container if necessary, and returns it.
  150. * @param container RtfContainer, which is the parent of the returned RtfTextrun
  151. * @param writer Writer of the given RtfContainer
  152. * @param attrs RtfAttributes which are to write at the beginning of the RtfTextrun
  153. * @throws IOException for I/O problems
  154. */
  155. public static RtfTextrun getTextrun(RtfContainer container, Writer writer, RtfAttributes attrs)
  156. throws IOException {
  157. Object obj;
  158. List list=container.getChildren();
  159. if(list.size()==0) {
  160. //add a new RtfTextrun
  161. RtfTextrun textrun=new RtfTextrun(container, writer, attrs);
  162. list.add(textrun);
  163. return textrun;
  164. } else if ((obj=list.get(list.size()-1)) instanceof RtfTextrun ) {
  165. //if the last child is a RtfTextrun, return it
  166. return (RtfTextrun)obj;
  167. }
  168. //add a new RtfTextrun as the last child
  169. RtfTextrun textrun=new RtfTextrun(container, writer, attrs);
  170. list.add(textrun);
  171. return textrun;
  172. }
  173. /**
  174. * write RTF code of all our children
  175. * @throws IOException for I/O problems
  176. */
  177. protected void writeRtfContent() throws IOException {
  178. /**
  179. *TODO: The textrun's children are iterated threetimes:
  180. * 1. In WhitespaceCollapser
  181. * 2. To determine the last RtfParagraphBreak
  182. * 3. To write the children
  183. * Maybe this can be done more efficient.
  184. */
  185. if (attrib != null && attrib.isSet("WhiteSpaceFalse")) {
  186. attrib.unset("WhiteSpaceFalse");
  187. } else {
  188. new WhitespaceCollapser(this);
  189. }
  190. //determine, if this RtfTextrun is the last child of its parent
  191. boolean bLast=false;
  192. for (Iterator it = parent.getChildren().iterator(); it.hasNext();) {
  193. if(it.next() == this) {
  194. bLast=!it.hasNext();
  195. break;
  196. }
  197. }
  198. //get last RtfParagraphBreak, which is not followed by any visible child
  199. RtfParagraphBreak lastParagraphBreak=null;
  200. if(bLast) {
  201. for (Iterator it = getChildren().iterator(); it.hasNext();) {
  202. final RtfElement e = (RtfElement)it.next();
  203. if(e instanceof RtfParagraphBreak) {
  204. lastParagraphBreak=(RtfParagraphBreak)e;
  205. } else {
  206. if(!(e instanceof RtfOpenGroupMark)
  207. && !(e instanceof RtfCloseGroupMark)
  208. && e.isEmpty()) {
  209. lastParagraphBreak=null;
  210. }
  211. }
  212. }
  213. }
  214. //may contain for example \intbl
  215. writeAttributes(attrib, null);
  216. //write all children
  217. boolean bPrevPar = false;
  218. boolean bFirst = true;
  219. for (Iterator it = getChildren().iterator(); it.hasNext();) {
  220. final RtfElement e = (RtfElement)it.next();
  221. final boolean bRtfParagraphBreak = (e instanceof RtfParagraphBreak);
  222. /**
  223. * -Write RtfParagraphBreak only, if the previous visible child
  224. * was't also a RtfParagraphBreak.
  225. * -Write RtfParagraphBreak only, if it is not the first visible
  226. * child.
  227. * -If the RtfTextrun is the last child of its parent, write a
  228. * RtfParagraphBreak only, if it is not the last child.
  229. */
  230. boolean bHide=false;
  231. bHide=bRtfParagraphBreak;
  232. bHide=bHide &&
  233. (bPrevPar || bFirst || (bLast && lastParagraphBreak!=null && e==lastParagraphBreak) );
  234. if( !bHide) {
  235. e.writeRtf();
  236. }
  237. if(e instanceof RtfParagraphBreak) {
  238. bPrevPar=true;
  239. } else if(e instanceof RtfCloseGroupMark) {
  240. //do nothing
  241. } else if(e instanceof RtfOpenGroupMark) {
  242. //do nothing
  243. } else {
  244. bPrevPar=bPrevPar && e.isEmpty();
  245. bFirst=bFirst && e.isEmpty();
  246. }
  247. }
  248. }
  249. }