示例#1
0
  protected Dimension layout(
      String text,
      Locale locale,
      float maxWidth,
      String color,
      float fontSize,
      BufferedImage image,
      boolean draw) {

    Color fg = this.color;
    if (color != null) {
      try {
        fg = ColorUtils.parseColor(color);
      } catch (IllegalArgumentException e) {
        log.warn("Browser didn't send a valid color value, using default.");
      }
    }

    Font font = getFont(text, locale).deriveFont(fontSize);

    Graphics2D graphics = createGraphics(image);
    FontRenderContext fc = graphics.getFontRenderContext();

    HyphenatedLineBreakMeasurerer measurer = new HyphenatedLineBreakMeasurerer(text, font, fg, fc);
    int y = paddingTop;
    int maxX = 1;
    while (measurer.hasNext()) {
      TextLayout layout = measurer.nextLayout(maxWidth);
      y += layout.getAscent();
      int x = paddingLeft + (int) layout.getVisibleAdvance();
      if (draw) {
        layout.draw(graphics, paddingLeft, y);
      }
      y += layout.getDescent();
      maxX = Math.max(maxX, x + paddingRight);
      y += layout.getLeading();
      if (measurer.hasNext()) {
        y += lineSpacing;
      }
    }
    y += paddingBottom;
    graphics.dispose();
    return new Dimension(checkSize(maxX), checkSize(y));
  }
示例#2
0
 /**
  * Sets the text foreground color. Examples of supported formats:
  *
  * <pre>
  * #fff
  * #fffff
  * rgb(255,255,255)
  * rgb(100%, 100%, 100%)
  * </pre>
  */
 public void setColor(String color) {
   this.color = ColorUtils.parseColor(color);
 }