Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

RtfTextrun.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. // Java
  20. import java.io.IOException;
  21. import java.io.Writer;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.ListIterator;
  25. import java.util.Stack;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. /**
  29. * <p>Class which contains a linear text run. It has methods to add attributes,
  30. * text, paragraph breaks....</p>
  31. *
  32. * <p>This work was authored by Peter Herweg (pherweg@web.de).</p>
  33. */
  34. public class RtfTextrun extends RtfContainer {
  35. /** Constant for no page break */
  36. public static final int BREAK_NONE = 0;
  37. /** Constant for a normal page break */
  38. public static final int BREAK_PAGE = 1;
  39. /** Constant for a column break */
  40. public static final int BREAK_COLUMN = 2;
  41. /** Constant for a even page break */
  42. public static final int BREAK_EVEN_PAGE = 3;
  43. /** Constant for a odd page break */
  44. public static final int BREAK_ODD_PAGE = 4;
  45. private boolean bSuppressLastPar;
  46. private RtfListItem rtfListItem;
  47. /**
  48. * logging instance
  49. */
  50. protected static final Log log = LogFactory.getLog(RtfTextrun.class);
  51. /** Manager for handling space-* property. */
  52. private RtfSpaceManager rtfSpaceManager = new RtfSpaceManager();
  53. /** Class which represents the opening of a RTF group mark.*/
  54. private class RtfOpenGroupMark extends RtfElement {
  55. RtfOpenGroupMark(RtfContainer parent, Writer w, RtfAttributes attr)
  56. throws IOException {
  57. super(parent, w, attr);
  58. }
  59. /**
  60. * @return true if this element would generate no "useful" RTF content
  61. */
  62. public boolean isEmpty() {
  63. return false;
  64. }
  65. /**
  66. * write RTF code of all our children
  67. * @throws IOException for I/O problems
  68. */
  69. protected void writeRtfContent() throws IOException {
  70. writeGroupMark(true);
  71. writeAttributes(getRtfAttributes(), null);
  72. }
  73. }
  74. /** Class which represents the closing of a RTF group mark.*/
  75. private class RtfCloseGroupMark extends RtfElement {
  76. private int breakType = BREAK_NONE;
  77. RtfCloseGroupMark(RtfContainer parent, Writer w, int breakType)
  78. throws IOException {
  79. super(parent, w);
  80. this.breakType = breakType;
  81. }
  82. /**
  83. * @return true if this element would generate no "useful" RTF content
  84. */
  85. public boolean isEmpty() {
  86. return false;
  87. }
  88. /**
  89. * Returns the break type.
  90. * @return the break type (BREAK_* constants)
  91. */
  92. public int getBreakType() {
  93. return breakType;
  94. }
  95. /**
  96. * Write RTF code of all our children.
  97. * @throws IOException for I/O problems
  98. */
  99. protected void writeRtfContent() throws IOException {
  100. writeGroupMark(false);
  101. boolean bHasTableCellParent = this.getParentOfClass(RtfTableCell.class) != null;
  102. //Unknown behavior when a table starts a new section,
  103. //Word may crash
  104. if (breakType != BREAK_NONE) {
  105. if (!bHasTableCellParent) {
  106. writeControlWord("sect");
  107. /* The following modifiers don't seem to appear in the right place */
  108. switch (breakType) {
  109. case BREAK_EVEN_PAGE:
  110. writeControlWord("sbkeven");
  111. break;
  112. case BREAK_ODD_PAGE:
  113. writeControlWord("sbkodd");
  114. break;
  115. case BREAK_COLUMN:
  116. writeControlWord("sbkcol");
  117. break;
  118. default:
  119. writeControlWord("sbkpage");
  120. }
  121. } else {
  122. log.warn("Cannot create break-after for a paragraph inside a table.");
  123. }
  124. }
  125. }
  126. }
  127. /** Create an RTF container as a child of given container */
  128. RtfTextrun(RtfContainer parent, Writer w, RtfAttributes attrs) throws IOException {
  129. super(parent, w, attrs);
  130. }
  131. /**
  132. * Adds instance of <code>OpenGroupMark</code> as a child with attributes.
  133. *
  134. * @param attrs attributes to add
  135. * @throws IOException for I/O problems
  136. */
  137. private void addOpenGroupMark(RtfAttributes attrs) throws IOException {
  138. RtfOpenGroupMark r = new RtfOpenGroupMark(this, writer, attrs);
  139. }
  140. /**
  141. * Adds instance of <code>CloseGroupMark</code> as a child.
  142. *
  143. * @throws IOException for I/O problems
  144. */
  145. private void addCloseGroupMark(int breakType) throws IOException {
  146. RtfCloseGroupMark r = new RtfCloseGroupMark(this, writer, breakType);
  147. }
  148. /**
  149. * Adds instance of <code>CloseGroupMark</code> as a child, but without a break option.
  150. * Inline attributes do not need that for example
  151. *
  152. * @throws IOException for I/O problems
  153. */
  154. private void addCloseGroupMark() throws IOException {
  155. RtfCloseGroupMark r = new RtfCloseGroupMark(this, writer, BREAK_NONE);
  156. }
  157. /**
  158. * Pushes block attributes, notifies all opened blocks about pushing block
  159. * attributes, adds <code>OpenGroupMark</code> as a child.
  160. *
  161. * @param attrs the block attributes to push
  162. * @throws IOException for I/O problems
  163. */
  164. public void pushBlockAttributes(RtfAttributes attrs) throws IOException {
  165. rtfSpaceManager.stopUpdatingSpaceBefore();
  166. RtfSpaceSplitter splitter = rtfSpaceManager.pushRtfSpaceSplitter(attrs);
  167. addOpenGroupMark(splitter.getCommonAttributes());
  168. }
  169. /**
  170. * Pops block attributes, notifies all opened blocks about pushing block
  171. * attributes, adds <code>CloseGroupMark</code> as a child.
  172. * @param breakType the break type
  173. * @throws IOException for I/O problems
  174. */
  175. public void popBlockAttributes(int breakType) throws IOException {
  176. rtfSpaceManager.popRtfSpaceSplitter();
  177. rtfSpaceManager.stopUpdatingSpaceBefore();
  178. addCloseGroupMark(breakType);
  179. }
  180. /**
  181. * Pushes inline attributes.
  182. *
  183. * @param attrs the inline attributes to push
  184. * @throws IOException for I/O problems
  185. */
  186. public void pushInlineAttributes(RtfAttributes attrs) throws IOException {
  187. rtfSpaceManager.pushInlineAttributes(attrs);
  188. addOpenGroupMark(attrs);
  189. }
  190. /**
  191. * Inserts a page number citation.
  192. * @param refId the identifier being referenced
  193. * @throws IOException for I/O problems
  194. */
  195. public void addPageNumberCitation(String refId) throws IOException {
  196. RtfPageNumberCitation r = new RtfPageNumberCitation(this, writer, refId);
  197. }
  198. /**
  199. * Pop inline attributes.
  200. *
  201. * @throws IOException for I/O problems
  202. */
  203. public void popInlineAttributes() throws IOException {
  204. rtfSpaceManager.popInlineAttributes();
  205. addCloseGroupMark();
  206. }
  207. /**
  208. * Add string to children list.
  209. *
  210. * @param s string to add
  211. * @throws IOException for I/O problems
  212. */
  213. public void addString(String s) throws IOException {
  214. if (s.equals("")) {
  215. return;
  216. }
  217. RtfAttributes attrs = rtfSpaceManager.getLastInlineAttribute();
  218. //add RtfSpaceSplitter to inherit accumulated space
  219. rtfSpaceManager.pushRtfSpaceSplitter(attrs);
  220. rtfSpaceManager.setCandidate(attrs);
  221. // create a string and add it as a child
  222. new RtfString(this, writer, s);
  223. rtfSpaceManager.popRtfSpaceSplitter();
  224. }
  225. /**
  226. * Inserts a footnote.
  227. *
  228. * @return inserted footnote
  229. * @throws IOException for I/O problems
  230. */
  231. public RtfFootnote addFootnote() throws IOException {
  232. return new RtfFootnote(this, writer);
  233. }
  234. /**
  235. * Inserts paragraph break before all close group marks.
  236. *
  237. * @throws IOException for I/O problems
  238. * @return The paragraph break element
  239. */
  240. public RtfParagraphBreak addParagraphBreak() throws IOException {
  241. // get copy of children list
  242. List children = getChildren();
  243. Stack tmp = new Stack();
  244. RtfParagraphBreak par = null;
  245. // delete all previous CloseGroupMark
  246. int deletedCloseGroupCount = 0;
  247. ListIterator lit = children.listIterator(children.size());
  248. while (lit.hasPrevious()
  249. && (lit.previous() instanceof RtfCloseGroupMark)) {
  250. tmp.push(((RtfCloseGroupMark) lit.next()).getBreakType());
  251. lit.remove();
  252. deletedCloseGroupCount++;
  253. }
  254. if (children.size() != 0) {
  255. // add paragraph break and restore all deleted close group marks
  256. setChildren(children);
  257. par = new RtfParagraphBreak(this, writer);
  258. for (int i = 0; i < deletedCloseGroupCount; i++) {
  259. addCloseGroupMark((Integer) tmp.pop());
  260. }
  261. }
  262. return par;
  263. }
  264. /**
  265. * Inserts a leader.
  266. * @param attrs Attributes for the leader
  267. * @throws IOException for I/O problems
  268. */
  269. public void addLeader(RtfAttributes attrs) throws IOException {
  270. new RtfLeader(this, writer, attrs);
  271. }
  272. /**
  273. * Inserts a page number.
  274. * @param attr Attributes for the page number to insert.
  275. * @throws IOException for I/O problems
  276. */
  277. public void addPageNumber(RtfAttributes attr) throws IOException {
  278. RtfPageNumber r = new RtfPageNumber(this, writer, attr);
  279. }
  280. /**
  281. * Inserts a hyperlink.
  282. * @param attr Attributes for the hyperlink to insert.
  283. * @return inserted hyperlink
  284. * @throws IOException for I/O problems
  285. */
  286. public RtfHyperLink addHyperlink(RtfAttributes attr) throws IOException {
  287. return new RtfHyperLink(this, writer, attr);
  288. }
  289. /**
  290. * Inserts a bookmark.
  291. * @param id Id for the inserted bookmark
  292. * @throws IOException for I/O problems
  293. */
  294. public void addBookmark(String id) throws IOException {
  295. if (id.length() > 0) {
  296. // if id is not empty, add bookmark
  297. new RtfBookmark(this, writer, id);
  298. }
  299. }
  300. /**
  301. * Inserts an image.
  302. * @return inserted image
  303. * @throws IOException for I/O problems
  304. */
  305. public RtfExternalGraphic newImage() throws IOException {
  306. return new RtfExternalGraphic(this, writer);
  307. }
  308. /**
  309. * Adds a new RtfTextrun to the given container if necessary, and returns it.
  310. * @param container RtfContainer, which is the parent of the returned RtfTextrun
  311. * @param writer Writer of the given RtfContainer
  312. * @param attrs RtfAttributes which are to write at the beginning of the RtfTextrun
  313. * @return new or existing RtfTextrun object.
  314. * @throws IOException for I/O problems
  315. */
  316. public static RtfTextrun getTextrun(RtfContainer container, Writer writer, RtfAttributes attrs)
  317. throws IOException {
  318. List list = container.getChildren();
  319. if (list.size() == 0) {
  320. //add a new RtfTextrun
  321. RtfTextrun textrun = new RtfTextrun(container, writer, attrs);
  322. list.add(textrun);
  323. return textrun;
  324. }
  325. Object obj = list.get(list.size() - 1);
  326. if (obj instanceof RtfTextrun) {
  327. //if the last child is a RtfTextrun, return it
  328. return (RtfTextrun) obj;
  329. }
  330. //add a new RtfTextrun as the last child
  331. RtfTextrun textrun = new RtfTextrun(container, writer, attrs);
  332. list.add(textrun);
  333. return textrun;
  334. }
  335. /**
  336. * specify, if the last paragraph control word (\par) should be suppressed.
  337. * @param bSuppress true, if the last \par should be suppressed
  338. */
  339. public void setSuppressLastPar(boolean bSuppress) {
  340. bSuppressLastPar = bSuppress;
  341. }
  342. /**
  343. * write RTF code of all our children
  344. * @throws IOException for I/O problems
  345. */
  346. protected void writeRtfContent() throws IOException {
  347. /**
  348. *TODO: The textrun's children are iterated twice:
  349. * 1. To determine the last RtfParagraphBreak
  350. * 2. To write the children
  351. * Maybe this can be done more efficient.
  352. */
  353. boolean bHasTableCellParent
  354. = this.getParentOfClass(RtfTableCell.class) != null;
  355. RtfAttributes attrBlockLevel = new RtfAttributes();
  356. //determine, if this RtfTextrun is the last child of its parent
  357. boolean bLast = false;
  358. for (Iterator it = parent.getChildren().iterator(); it.hasNext();) {
  359. if (it.next() == this) {
  360. bLast = !it.hasNext();
  361. break;
  362. }
  363. }
  364. //get last RtfParagraphBreak, which is not followed by any visible child
  365. RtfParagraphBreak lastParagraphBreak = null;
  366. if (bLast) {
  367. RtfElement aBefore = null;
  368. for (Iterator it = getChildren().iterator(); it.hasNext();) {
  369. final RtfElement e = (RtfElement)it.next();
  370. if (e instanceof RtfParagraphBreak) {
  371. //If the element before was a paragraph break or a bookmark
  372. //they will be hidden and are therefore not considered as visible
  373. if (!(aBefore instanceof RtfParagraphBreak)
  374. && !(aBefore instanceof RtfBookmark)) {
  375. lastParagraphBreak = (RtfParagraphBreak)e;
  376. }
  377. } else {
  378. if (!(e instanceof RtfOpenGroupMark)
  379. && !(e instanceof RtfCloseGroupMark)
  380. && e.isEmpty()) {
  381. lastParagraphBreak = null;
  382. }
  383. }
  384. aBefore = e;
  385. }
  386. }
  387. //may contain for example \intbl
  388. writeAttributes(attrib, null);
  389. if (rtfListItem != null) {
  390. rtfListItem.getRtfListStyle().writeParagraphPrefix(this);
  391. }
  392. //write all children
  393. boolean bPrevPar = false;
  394. boolean bBookmark = false;
  395. boolean bFirst = true;
  396. for (Iterator it = getChildren().iterator(); it.hasNext();) {
  397. final RtfElement e = (RtfElement)it.next();
  398. final boolean bRtfParagraphBreak = (e instanceof RtfParagraphBreak);
  399. if (bHasTableCellParent) {
  400. attrBlockLevel.set(e.getRtfAttributes());
  401. }
  402. /**
  403. * -Write RtfParagraphBreak only, if the previous visible child
  404. * was't also a RtfParagraphBreak.
  405. * -Write RtfParagraphBreak only, if it is not the first visible
  406. * child.
  407. * -If the RtfTextrun is the last child of its parent, write a
  408. * RtfParagraphBreak only, if it is not the last child.
  409. * -If the RtfParagraphBreak can not be hidden (e.g. a table cell requires it)
  410. * it is also written
  411. */
  412. boolean bHide = false;
  413. bHide = bRtfParagraphBreak;
  414. bHide = bHide
  415. && (bPrevPar
  416. || bFirst
  417. || (bSuppressLastPar && bLast && lastParagraphBreak != null
  418. && e == lastParagraphBreak)
  419. || bBookmark)
  420. && ((RtfParagraphBreak)e).canHide();
  421. if (!bHide) {
  422. newLine();
  423. e.writeRtf();
  424. if (rtfListItem != null && e instanceof RtfParagraphBreak) {
  425. rtfListItem.getRtfListStyle().writeParagraphPrefix(this);
  426. }
  427. }
  428. if (e instanceof RtfParagraphBreak) {
  429. bPrevPar = true;
  430. } else if (e instanceof RtfBookmark) {
  431. bBookmark = true;
  432. } else if (e instanceof RtfCloseGroupMark) {
  433. //do nothing
  434. } else if (e instanceof RtfOpenGroupMark) {
  435. //do nothing
  436. } else {
  437. bPrevPar = bPrevPar && e.isEmpty();
  438. bFirst = bFirst && e.isEmpty();
  439. bBookmark = false;
  440. }
  441. } //for (Iterator it = ...)
  442. //
  443. if (bHasTableCellParent) {
  444. writeAttributes(attrBlockLevel, null);
  445. }
  446. }
  447. /**
  448. * Set the parent list-item of the textrun.
  449. *
  450. * @param listItem parent list-item of the textrun
  451. */
  452. public void setRtfListItem(RtfListItem listItem) {
  453. rtfListItem = listItem;
  454. }
  455. /**
  456. * Gets the parent list-item of the textrun.
  457. *
  458. * @return parent list-item of the textrun
  459. */
  460. public RtfListItem getRtfListItem() {
  461. return rtfListItem;
  462. }
  463. }