예제 #1
0
파일: PdfChunk.java 프로젝트: fwenzel/pdftk
 boolean isExtSplitCharacter(int start, int current, int end, char[] cc, PdfChunk[] ck) {
   return splitCharacter.isSplitCharacter(start, current, end, cc, ck);
 }
예제 #2
0
파일: PdfChunk.java 프로젝트: fwenzel/pdftk
  /**
   * Splits this <CODE>PdfChunk</CODE> if it's too long for the given width.
   *
   * <p>Returns <VAR>null</VAR> if the <CODE>PdfChunk</CODE> wasn't truncated.
   *
   * @param width a given width
   * @return the <CODE>PdfChunk</CODE> that doesn't fit into the width.
   */
  PdfChunk split(float width) {
    newlineSplit = false;
    /* ssteward: dropped in 1.44
           if (image != null) {
               if (image.scaledWidth() > width) {
                   PdfChunk pc = new PdfChunk(Chunk.OBJECT_REPLACEMENT_CHARACTER, this);
                   value = "";
                   attributes = new HashMap();
                   image = null;
                   font = PdfFont.getDefaultFont();
                   return pc;
               }
               else
                   return null;
           }
    */
    HyphenationEvent hyphenationEvent = (HyphenationEvent) noStroke.get(Chunk.HYPHENATION);
    int currentPosition = 0;
    int splitPosition = -1;
    float currentWidth = 0;

    // loop over all the characters of a string
    // or until the totalWidth is reached
    int lastSpace = -1;
    float lastSpaceWidth = 0;
    int length = value.length();
    char valueArray[] = value.toCharArray();
    char character = 0;
    BaseFont ft = font.getFont();
    if (ft.getFontType() == BaseFont.FONT_TYPE_CJK && ft.getUnicodeEquivalent(' ') != ' ') {
      while (currentPosition < length) {
        // the width of every character is added to the currentWidth
        char cidChar = valueArray[currentPosition];
        character = ft.getUnicodeEquivalent(cidChar);
        // if a newLine or carriageReturn is encountered
        if (character == '\n') {
          newlineSplit = true;
          String returnValue = value.substring(currentPosition + 1);
          value = value.substring(0, currentPosition);
          if (value.length() < 1) {
            value = "\u0001";
          }
          PdfChunk pc = new PdfChunk(returnValue, this);
          return pc;
        }
        currentWidth += font.width(cidChar);
        if (character == ' ') {
          lastSpace = currentPosition + 1;
          lastSpaceWidth = currentWidth;
        }
        if (currentWidth > width) break;
        // if a split-character is encountered, the splitPosition is altered
        if (splitCharacter.isSplitCharacter(0, currentPosition, length, valueArray, thisChunk))
          splitPosition = currentPosition + 1;
        currentPosition++;
      }
    } else {
      while (currentPosition < length) {
        // the width of every character is added to the currentWidth
        character = valueArray[currentPosition];
        // if a newLine or carriageReturn is encountered
        if (character == '\r' || character == '\n') {
          newlineSplit = true;
          int inc = 1;
          if (character == '\r'
              && currentPosition + 1 < length
              && valueArray[currentPosition + 1] == '\n') inc = 2;
          String returnValue = value.substring(currentPosition + inc);
          value = value.substring(0, currentPosition);
          if (value.length() < 1) {
            value = " ";
          }
          PdfChunk pc = new PdfChunk(returnValue, this);
          return pc;
        }
        currentWidth += font.width(character);
        if (character == ' ') {
          lastSpace = currentPosition + 1;
          lastSpaceWidth = currentWidth;
        }
        if (currentWidth > width) break;
        // if a split-character is encountered, the splitPosition is altered
        if (splitCharacter.isSplitCharacter(0, currentPosition, length, valueArray, null))
          splitPosition = currentPosition + 1;
        currentPosition++;
      }
    }

    // if all the characters fit in the total width, null is returned (there is no overflow)
    if (currentPosition == length) {
      return null;
    }
    // otherwise, the string has to be truncated
    if (splitPosition < 0) {
      String returnValue = value;
      value = "";
      PdfChunk pc = new PdfChunk(returnValue, this);
      return pc;
    }
    if (lastSpace > splitPosition && splitCharacter.isSplitCharacter(0, 0, 1, singleSpace, null))
      splitPosition = lastSpace;
    if (hyphenationEvent != null && lastSpace < currentPosition) {
      int wordIdx = getWord(value, lastSpace);
      if (wordIdx > lastSpace) {
        String pre =
            hyphenationEvent.getHyphenatedWordPre(
                value.substring(lastSpace, wordIdx),
                font.getFont(),
                font.size(),
                width - lastSpaceWidth);
        String post = hyphenationEvent.getHyphenatedWordPost();
        if (pre.length() > 0) {
          String returnValue = post + value.substring(wordIdx);
          value = trim(value.substring(0, lastSpace) + pre);
          PdfChunk pc = new PdfChunk(returnValue, this);
          return pc;
        }
      }
    }
    String returnValue = value.substring(splitPosition);
    value = trim(value.substring(0, splitPosition));
    PdfChunk pc = new PdfChunk(returnValue, this);
    return pc;
  }