Esempio n. 1
0
  private Chunk formatNumber(StyleListProperties listProperties, int value) {
    Chunk symbol = new Chunk("", getFont());

    StyleTextProperties textProperties = listProperties.getTextProperties();
    if (textProperties != null) {
      Font font = textProperties.getFont();
      if (font != null) {
        symbol.setFont(font);
      }
    }

    StyleNumFormat numFormat = listProperties.getNumFormat();
    if (numFormat != null) {
      StringBuilder sbuf = new StringBuilder();

      // num-prefix
      String numPrefix = listProperties.getNumPrefix();
      if (numPrefix != null) {
        sbuf.append(numPrefix);
      }

      // number
      if (numFormat.isAlphabetical()) {
        sbuf.append(RomanAlphabetFactory.getString(value, numFormat.isLowercase()));
      } else if (numFormat.isRoman()) {
        sbuf.append(RomanNumberFactory.getString(value, numFormat.isLowercase()));
      } else {
        sbuf.append(value);
      }

      // num-suffix
      String numSuffix = listProperties.getNumSuffix();
      if (numSuffix != null) {
        sbuf.append(numSuffix);
      }

      symbol.append(sbuf.toString());
    }
    return symbol;
  }
Esempio n. 2
0
  private static LinkedList<Chunk> splitScrambleToLineChunks(
      String paddedScramble, Font scrambleFont, float scrambleColumnWidth) {
    float availableScrambleWidth = scrambleColumnWidth - 2 * SCRAMBLE_PADDING_HORIZONTAL;

    int startIndex = 0;
    int endIndex = 0;
    LinkedList<Chunk> lineChunks = new LinkedList<Chunk>();
    while (startIndex < paddedScramble.length()) {
      // Walk forwards until we've grabbed the maximum number of characters
      // that fit in a line, we've run out of characters, or we hit a newline.
      float substringWidth;
      for (endIndex++; endIndex <= paddedScramble.length(); endIndex++) {
        if (paddedScramble.charAt(endIndex - 1) == '\n') {
          break;
        }
        String scrambleSubstring =
            NON_BREAKING_SPACE
                + paddedScramble.substring(startIndex, endIndex)
                + NON_BREAKING_SPACE;
        substringWidth =
            scrambleFont.getBaseFont().getWidthPoint(scrambleSubstring, scrambleFont.getSize());
        if (substringWidth > availableScrambleWidth) {
          break;
        }
      }
      // endIndex is one past the best fit, so remove one character and it should fit!
      endIndex--;

      // If we're not at the end of the scramble, make sure we're not cutting
      // a turn in half by walking backwards until we're right before a turn.
      // Any spaces added for padding after a turn are considered part of
      // that turn because they're actually NON_BREAKING_SPACE, not a ' '.
      int perfectFitEndIndex = endIndex;
      if (endIndex < paddedScramble.length()) {
        while (true) {
          if (endIndex < startIndex) {
            // We walked all the way to the beginning of the line
            // without finding a good breaking point. Give up and break
            // in the middle of a turn =(.
            endIndex = perfectFitEndIndex;
            break;
          }

          // Another dirty hack for sq1: turns only line up
          // nicely if every line starts with a (x,y). We ensure this
          // by forcing every line to end with a /.
          boolean isSquareOne = paddedScramble.indexOf('/') >= 0;
          if (isSquareOne) {
            char previousCharacter = paddedScramble.charAt(endIndex - 1);
            if (previousCharacter == '/') {
              break;
            }
          } else {
            char currentCharacter = paddedScramble.charAt(endIndex);
            boolean isTurnCharacter = currentCharacter != ' ';
            if (!isTurnCharacter || currentCharacter == '\n') {
              break;
            }
          }
          endIndex--;
        }
      }

      String scrambleSubstring =
          NON_BREAKING_SPACE + paddedScramble.substring(startIndex, endIndex) + NON_BREAKING_SPACE;

      // Add NON_BREAKING_SPACE until the scrambleSubstring takes up as much as
      // space as is available on a line.
      do {
        scrambleSubstring += NON_BREAKING_SPACE;
        substringWidth =
            scrambleFont.getBaseFont().getWidthPoint(scrambleSubstring, scrambleFont.getSize());
      } while (substringWidth <= availableScrambleWidth);
      // scrambleSubstring is now too big for our line, so remove the
      // last character.
      scrambleSubstring = scrambleSubstring.substring(0, scrambleSubstring.length() - 1);

      // Walk past all whitespace that comes immediately after the line wrap
      // we are about to insert.
      while (endIndex < paddedScramble.length()
          && (paddedScramble.charAt(endIndex) == ' ' || paddedScramble.charAt(endIndex) == '\n')) {
        endIndex++;
      }
      startIndex = endIndex;
      Chunk lineChunk = new Chunk(scrambleSubstring);
      lineChunks.add(lineChunk);
      lineChunk.setFont(scrambleFont);

      // Force a line wrap!
      lineChunk.append("\n");
    }

    return lineChunks;
  }