Esempio n. 1
0
  /**
   * Compute the bounds of the text, if necessary.
   *
   * @param dc the current DrawContext.
   */
  protected void computeBoundsIfNeeded(DrawContext dc) {
    // Do not compute bounds if they are available. Computing text bounds is expensive, so only do
    // this
    // calculation if necessary.
    if (this.bounds != null) return;

    TextRenderer textRenderer =
        OGLTextRenderer.getOrCreateTextRenderer(dc.getTextRendererCache(), this.getFont());

    int width = 0;
    int maxLineHeight = 0;
    this.lineBounds = new Rectangle2D[this.lines.length];

    for (int i = 0; i < this.lines.length; i++) {
      Rectangle2D lineBounds = textRenderer.getBounds(lines[i]);
      width = (int) Math.max(lineBounds.getWidth(), width);

      double thisLineHeight = Math.abs(lineBounds.getY());
      maxLineHeight = (int) Math.max(thisLineHeight, maxLineHeight);

      this.lineBounds[i] = lineBounds;
    }
    this.lineHeight = maxLineHeight;

    // Compute final height using maxLineHeight and number of lines
    this.bounds =
        new Rectangle(
            this.lines.length,
            maxLineHeight,
            width,
            this.lines.length * maxLineHeight + this.lines.length * this.lineSpacing);
  }