示例#1
0
 /**
  * Adds a Chunk.
  *
  * <p>This method is a hack to solve a problem I had with phrases that were split between chunks
  * in the wrong place.
  *
  * @param chunk a Chunk to add to the Phrase
  * @return true if adding the Chunk succeeded
  */
 protected boolean addChunk(Chunk chunk) {
   Font f = chunk.getFont();
   String c = chunk.getContent();
   if (font != null && !font.isStandardFont()) {
     f = font.difference(chunk.getFont());
   }
   if (size() > 0 && !chunk.hasAttributes()) {
     try {
       Chunk previous = (Chunk) get(size() - 1);
       if (!previous.hasAttributes()
           && (f == null || f.compareTo(previous.getFont()) == 0)
           && !"".equals(previous.getContent().trim())
           && !"".equals(c.trim())) {
         previous.append(c);
         return true;
       }
     } catch (ClassCastException cce) {
     }
   }
   Chunk newChunk = new Chunk(c, f);
   newChunk.setAttributes(chunk.getAttributes());
   if (newChunk.getHyphenation() == null) {
     newChunk.setHyphenation(hyphenation);
   }
   return super.add(newChunk);
 }
示例#2
0
 /**
  * Adds a <CODE>Chunk</CODE>, an <CODE>Anchor</CODE> or another <CODE>Phrase</CODE> to this <CODE>
  * Phrase</CODE>.
  *
  * @param index index at which the specified element is to be inserted
  * @param o an object of type <CODE>Chunk</CODE>, <CODE>Anchor</CODE> or <CODE>Phrase</CODE>
  * @throws ClassCastException when you try to add something that isn't a <CODE>Chunk</CODE>,
  *     <CODE>Anchor</CODE> or <CODE>Phrase</CODE>
  */
 public void add(int index, Object o) {
   if (o == null) return;
   try {
     Element element = (Element) o;
     if (element.type() == Element.CHUNK) {
       Chunk chunk = (Chunk) element;
       if (!font.isStandardFont()) {
         chunk.setFont(font.difference(chunk.getFont()));
       }
       if (hyphenation != null) {
         chunk.setHyphenation(hyphenation);
       }
       super.add(index, chunk);
     } else if (element.type() == Element.PHRASE
         || element.type() == Element.ANCHOR
         || element.type() == Element.ANNOTATION
         || element.type() == Element.TABLE
         || // line added by David Freels
         element.type() == Element.YMARK
         || element.type() == Element.MARKED) {
       super.add(index, element);
     } else {
       throw new ClassCastException(String.valueOf(element.type()));
     }
   } catch (ClassCastException cce) {
     throw new ClassCastException("Insertion of illegal Element: " + cce.getMessage());
   }
 }