Пример #1
0
 /**
  * Process the text so that it will render with a combination of fonts if needed.
  *
  * @param text the text
  * @return a <CODE>Phrase</CODE> with one or more chunks
  */
 public Phrase process(String text) {
   int fsize = fonts.size();
   if (fsize == 0) throw new IndexOutOfBoundsException("No font is defined.");
   char cc[] = text.toCharArray();
   int len = cc.length;
   StringBuffer sb = new StringBuffer();
   Font font = null;
   int lastidx = -1;
   Phrase ret = new Phrase();
   for (int k = 0; k < len; ++k) {
     char c = cc[k];
     if (c == '\n' || c == '\r') {
       sb.append(c);
       continue;
     }
     if (Utilities.isSurrogatePair(cc, k)) {
       int u = Utilities.convertToUtf32(cc, k);
       for (int f = 0; f < fsize; ++f) {
         font = (Font) fonts.get(f);
         if (font.getBaseFont().charExists(u)) {
           if (lastidx != f) {
             if (sb.length() > 0 && lastidx != -1) {
               Chunk ck = new Chunk(sb.toString(), (Font) fonts.get(lastidx));
               ret.add(ck);
               sb.setLength(0);
             }
             lastidx = f;
           }
           sb.append(c);
           sb.append(cc[++k]);
           break;
         }
       }
     } else {
       for (int f = 0; f < fsize; ++f) {
         font = (Font) fonts.get(f);
         if (font.getBaseFont().charExists(c)) {
           if (lastidx != f) {
             if (sb.length() > 0 && lastidx != -1) {
               Chunk ck = new Chunk(sb.toString(), (Font) fonts.get(lastidx));
               ret.add(ck);
               sb.setLength(0);
             }
             lastidx = f;
           }
           sb.append(c);
           break;
         }
       }
     }
   }
   if (sb.length() > 0) {
     Chunk ck = new Chunk(sb.toString(), (Font) fonts.get(lastidx == -1 ? 0 : lastidx));
     ret.add(ck);
   }
   return ret;
 }
Пример #2
0
 public int compareTo(Font font) {
   if (font == null) {
     return -1;
   }
   if (baseFont != null && !baseFont.equals(font.getBaseFont())) {
     return -2;
   }
   if (this.family != font.getFamily()) {
     return 1;
   }
   if (this.size != font.getSize()) {
     return 2;
   }
   if (this.style != font.getStyle()) {
     return 3;
   }
   if (this.color == null) {
     if (font.color == null) {
       return 0;
     }
     return 4;
   }
   if (font.color == null) {
     return 4;
   }
   if (this.color.equals(font.getColor())) {
     return 0;
   }
   return 4;
 }
Пример #3
0
 /**
  * Adds a <CODE>Font</CODE> to be searched for valid characters.
  *
  * @param font the <CODE>Font</CODE>
  */
 public void addFont(Font font) {
   System.out.println("i'm add font inside woowoo");
   if (font.getBaseFont() != null) {
     fonts.add(font);
     return;
   }
   BaseFont bf = font.getCalculatedBaseFont(true);
   Font f2 = new Font(bf, font.getSize(), font.getCalculatedStyle(), font.getColor());
   fonts.add(f2);
 }
Пример #4
0
 /**
  * Compares this <CODE>Font</CODE> with another
  *
  * @param object the other <CODE>Font</CODE>
  * @return a value
  */
 public int compareTo(Object object) {
   if (object == null) {
     return -1;
   }
   Font font;
   try {
     font = (Font) object;
     if (baseFont != null && !baseFont.equals(font.getBaseFont())) {
       return -2;
     }
     if (this.family != font.family()) {
       return 1;
     }
     if (this.size != font.size()) {
       return 2;
     }
     if (this.style != font.style()) {
       return 3;
     }
     if (this.color == null) {
       if (font.color == null) {
         return 0;
       }
       return 4;
     }
     if (font.color == null) {
       return 4;
     }
     if (this.color.equals(font.color())) {
       return 0;
     }
     return 4;
   } catch (ClassCastException cce) {
     return -3;
   }
 }
  /**
   * Constructs a <CODE>PdfChunk</CODE>-object.
   *
   * @param chunk the original <CODE>Chunk</CODE>-object
   * @param action the <CODE>PdfAction</CODE> if the <CODE>Chunk</CODE> comes from an <CODE>Anchor
   *     </CODE>
   */
  PdfChunk(Chunk chunk, PdfAction action) {
    thisChunk[0] = this;
    value = chunk.getContent();

    Font f = chunk.getFont();
    float size = f.getSize();
    if (size == Font.UNDEFINED) size = 12;
    baseFont = f.getBaseFont();
    int style = f.getStyle();
    if (style == Font.UNDEFINED) {
      style = Font.NORMAL;
    }
    if (baseFont == null) {
      // translation of the font-family to a PDF font-family
      baseFont = f.getCalculatedBaseFont(false);
    } else {
      // bold simulation
      if ((style & Font.BOLD) != 0)
        attributes.put(
            Chunk.TEXTRENDERMODE,
            new Object[] {
              new Integer(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE), new Float(size / 30f), null
            });
      // italic simulation
      if ((style & Font.ITALIC) != 0) attributes.put(Chunk.SKEW, new float[] {0, ITALIC_ANGLE});
    }
    font = new PdfFont(baseFont, size);
    // other style possibilities
    HashMap attr = chunk.getAttributes();
    if (attr != null) {
      for (Iterator i = attr.entrySet().iterator(); i.hasNext(); ) {
        Map.Entry entry = (Map.Entry) i.next();
        Object name = entry.getKey();
        if (keysAttributes.containsKey(name)) {
          attributes.put(name, entry.getValue());
        } else if (keysNoStroke.containsKey(name)) {
          noStroke.put(name, entry.getValue());
        }
      }
      if ("".equals(attr.get(Chunk.GENERICTAG))) {
        attributes.put(Chunk.GENERICTAG, chunk.getContent());
      }
    }
    if (f.isUnderlined()) {
      Object obj[] = {null, new float[] {0, 1f / 15, 0, -1f / 3, 0}};
      Object unders[][] = Utilities.addToArray((Object[][]) attributes.get(Chunk.UNDERLINE), obj);
      attributes.put(Chunk.UNDERLINE, unders);
    }
    if (f.isStrikethru()) {
      Object obj[] = {null, new float[] {0, 1f / 15, 0, 1f / 3, 0}};
      Object unders[][] = Utilities.addToArray((Object[][]) attributes.get(Chunk.UNDERLINE), obj);
      attributes.put(Chunk.UNDERLINE, unders);
    }
    if (action != null) attributes.put(Chunk.ACTION, action);
    // the color can't be stored in a PdfFont
    noStroke.put(Chunk.COLOR, f.getColor());
    noStroke.put(Chunk.ENCODING, font.getFont().getEncoding());
    Object obj[] = (Object[]) attributes.get(Chunk.IMAGE);
    if (obj == null) {
      image = null;
    } else {
      attributes.remove(Chunk.HSCALE); // images are scaled in other ways
      image = (Image) obj[0];
      offsetX = ((Float) obj[1]).floatValue();
      offsetY = ((Float) obj[2]).floatValue();
      changeLeading = ((Boolean) obj[3]).booleanValue();
    }
    font.setImage(image);
    Float hs = (Float) attributes.get(Chunk.HSCALE);
    if (hs != null) font.setHorizontalScaling(hs.floatValue());
    encoding = font.getFont().getEncoding();
    splitCharacter = (SplitCharacter) noStroke.get(Chunk.SPLITCHARACTER);
    if (splitCharacter == null) splitCharacter = DefaultSplitCharacter.DEFAULT;
  }