1 17 18 19 20 package org.apache.fop.render.rtf.rtflib.rtfdoc; 21 22 import java.io.IOException ; 24 import java.io.Writer ; 25 import java.util.List ; 26 import java.util.Iterator ; 27 import java.util.ListIterator ; 28 29 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfExternalGraphic; 31 32 37 public class RtfTextrun extends RtfContainer { 38 private boolean bSuppressLastPar = false; 39 private RtfListItem rtfListItem; 40 41 42 private RtfSpaceManager rtfSpaceManager = new RtfSpaceManager(); 43 44 45 private class RtfOpenGroupMark extends RtfElement { 46 47 RtfOpenGroupMark(RtfContainer parent, Writer w, RtfAttributes attr) 48 throws IOException { 49 super(parent, w, attr); 50 } 51 52 55 public boolean isEmpty() { 56 return false; 57 } 58 59 63 protected void writeRtfContent() throws IOException { 64 writeGroupMark(true); 65 writeAttributes(getRtfAttributes(), null); 66 } 67 } 68 69 70 private class RtfCloseGroupMark extends RtfElement { 71 72 RtfCloseGroupMark(RtfContainer parent, Writer w) 73 throws IOException { 74 super(parent, w); 75 } 76 77 80 public boolean isEmpty() { 81 return false; 82 } 83 84 88 protected void writeRtfContent() throws IOException { 89 writeGroupMark(false); 90 } 91 } 92 93 94 private class RtfParagraphBreak extends RtfElement { 95 96 RtfParagraphBreak(RtfContainer parent, Writer w) 97 throws IOException { 98 super(parent, w); 99 } 100 101 104 public boolean isEmpty() { 105 return false; 106 } 107 108 112 protected void writeRtfContent() throws IOException { 113 writeControlWord("par"); 114 } 115 } 116 117 118 RtfTextrun(RtfContainer parent, Writer w, RtfAttributes attrs) throws IOException { 119 super(parent, w, attrs); 120 } 121 122 123 129 private void addOpenGroupMark(RtfAttributes attrs) throws IOException { 130 RtfOpenGroupMark r = new RtfOpenGroupMark(this, writer, attrs); 131 } 132 133 138 private void addCloseGroupMark() throws IOException { 139 RtfCloseGroupMark r = new RtfCloseGroupMark(this, writer); 140 } 141 142 149 public void pushBlockAttributes(RtfAttributes attrs) throws IOException { 150 rtfSpaceManager.stopUpdatingSpaceBefore(); 151 RtfSpaceSplitter splitter = rtfSpaceManager.pushRtfSpaceSplitter(attrs); 152 addOpenGroupMark(splitter.getCommonAttributes()); 153 } 154 155 161 public void popBlockAttributes() throws IOException { 162 rtfSpaceManager.popRtfSpaceSplitter(); 163 rtfSpaceManager.stopUpdatingSpaceBefore(); 164 addCloseGroupMark(); 165 } 166 167 173 public void pushInlineAttributes(RtfAttributes attrs) throws IOException { 174 rtfSpaceManager.pushInlineAttributes(attrs); 175 addOpenGroupMark(attrs); 176 } 177 178 183 public void popInlineAttributes() throws IOException { 184 rtfSpaceManager.popInlineAttributes(); 185 addCloseGroupMark(); 186 } 187 188 194 public void addString(String s) throws IOException { 195 if (s.equals("")) { 196 return; 197 } 198 RtfAttributes attrs = rtfSpaceManager.getLastInlineAttribute(); 199 rtfSpaceManager.pushRtfSpaceSplitter(attrs); 201 rtfSpaceManager.setCandidate(attrs); 202 RtfString r = new RtfString(this, writer, s); 203 rtfSpaceManager.popRtfSpaceSplitter(); 204 } 205 206 212 public RtfFootnote addFootnote() throws IOException { 213 return new RtfFootnote(this, writer); 214 } 215 216 221 public void addParagraphBreak() throws IOException { 222 List children = getChildren(); 224 225 int deletedCloseGroupCount = 0; 227 228 ListIterator lit = children.listIterator(children.size()); 229 while (lit.hasPrevious() 230 && (lit.previous() instanceof RtfCloseGroupMark)) { 231 lit.remove(); 232 deletedCloseGroupCount++; 233 } 234 235 if (children.size() != 0) { 236 setChildren(children); 238 new RtfParagraphBreak(this, writer); 239 for (int i = 0; i < deletedCloseGroupCount; i++) { 240 addCloseGroupMark(); 241 } 242 } 243 } 244 245 250 public void addPageNumber(RtfAttributes attr) throws IOException { 251 RtfPageNumber r = new RtfPageNumber(this, writer, attr); 252 } 253 254 260 public RtfHyperLink addHyperlink(RtfAttributes attr) throws IOException { 261 return new RtfHyperLink(this, writer, attr); 262 } 263 264 269 public void addBookmark(String id) throws IOException { 270 if (id != "") { 271 new RtfBookmark(this, writer, id); 273 } 274 } 275 276 281 public RtfExternalGraphic newImage() throws IOException { 282 return new RtfExternalGraphic(this, writer); 283 } 284 285 293 public static RtfTextrun getTextrun(RtfContainer container, Writer writer, RtfAttributes attrs) 294 throws IOException { 295 296 List list = container.getChildren(); 297 298 if (list.size() == 0) { 299 RtfTextrun textrun = new RtfTextrun(container, writer, attrs); 301 list.add(textrun); 302 303 return textrun; 304 } 305 306 Object obj = list.get(list.size() - 1); 307 308 if (obj instanceof RtfTextrun) { 309 return (RtfTextrun) obj; 311 } 312 313 RtfTextrun textrun = new RtfTextrun(container, writer, attrs); 315 list.add(textrun); 316 317 return textrun; 318 } 319 320 324 public void setSuppressLastPar(boolean bSuppress) { 325 bSuppressLastPar = bSuppress; 326 } 327 328 332 protected void writeRtfContent() throws IOException { 333 339 340 boolean bHasTableCellParent = 341 this.getParentOfClass(RtfTableCell.class) != null; 342 RtfAttributes attrBlockLevel = new RtfAttributes(); 343 344 boolean bLast = false; 346 for (Iterator it = parent.getChildren().iterator(); it.hasNext();) { 347 if (it.next() == this) { 348 bLast = !it.hasNext(); 349 break; 350 } 351 } 352 353 RtfParagraphBreak lastParagraphBreak = null; 355 if (bLast) { 356 for (Iterator it = getChildren().iterator(); it.hasNext();) { 357 final RtfElement e = (RtfElement)it.next(); 358 if (e instanceof RtfParagraphBreak) { 359 lastParagraphBreak = (RtfParagraphBreak)e; 360 } else { 361 if (!(e instanceof RtfOpenGroupMark) 362 && !(e instanceof RtfCloseGroupMark) 363 && e.isEmpty()) { 364 lastParagraphBreak = null; 365 } 366 } 367 } 368 } 369 370 writeAttributes(attrib, null); 372 373 if (rtfListItem != null) { 374 rtfListItem.getRtfListStyle().writeParagraphPrefix(this); 375 } 376 377 boolean bPrevPar = false; 379 boolean bFirst = true; 380 for (Iterator it = getChildren().iterator(); it.hasNext();) { 381 final RtfElement e = (RtfElement)it.next(); 382 final boolean bRtfParagraphBreak = (e instanceof RtfParagraphBreak); 383 384 if (bHasTableCellParent) { 385 attrBlockLevel.set(e.getRtfAttributes()); 386 } 387 388 389 397 boolean bHide = false; 398 bHide = bRtfParagraphBreak; 399 bHide = bHide 400 && (bPrevPar 401 || bFirst 402 || (bSuppressLastPar && bLast && lastParagraphBreak != null 403 && e == lastParagraphBreak)); 404 405 if (!bHide) { 406 newLine(); 407 e.writeRtf(); 408 409 if (rtfListItem != null && e instanceof RtfParagraphBreak) { 410 rtfListItem.getRtfListStyle().writeParagraphPrefix(this); 411 } 412 } 413 414 if (e instanceof RtfParagraphBreak) { 415 bPrevPar = true; 416 } else if (e instanceof RtfCloseGroupMark) { 417 } else if (e instanceof RtfOpenGroupMark) { 419 } else { 421 bPrevPar = bPrevPar && e.isEmpty(); 422 bFirst = bFirst && e.isEmpty(); 423 } 424 } 426 if (bHasTableCellParent) { 428 writeAttributes(attrBlockLevel, null); 429 } 430 431 } 432 433 438 public void setRtfListItem(RtfListItem listItem) { 439 rtfListItem = listItem; 440 } 441 442 447 public RtfListItem getRtfListItem() { 448 return rtfListItem; 449 } 450 } 451 452 | Popular Tags |