Package com.positive.charts.util

Source Code of com.positive.charts.util.TextFragment

/* ========================================================================
* JCommon : a free general purpose class library for the Java(tm) platform
* ========================================================================
*
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
*
* Project Info:  http://www.jfree.org/jcommon/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
* USA. 
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* -----------------
* TextFragment.java
* -----------------
* (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
*
* Original Author:  David Gilbert (for Object Refinery Limited);
* Contributor(s):   -;
*
* $Id: TextFragment.java,v 1.1 2007/08/24 12:43:41 slapukhov Exp $
*
* Changes
* -------
* 07-Nov-2003 : Version 1 (DG);
* 25-Nov-2003 : Fixed bug in the dimension calculation (DG);
* 22-Dec-2003 : Added workaround for Java bug 4245442 (DG);
* 29-Jan-2004 : Added paint attribute (DG);
* 22-Mar-2004 : Added equals() method and implemented Serializable (DG);
* 01-Apr-2004 : Changed .geom.Dimension2D to org.jfree.ui.Size2D
*               because of JDK bug 4976448 which persists on JDK 1.3.1 (DG);
* 30-Sep-2004 : Moved drawRotatedString() from RefineryUtilities
*               --> TextUtilities (DG);
*
*/

package com.positive.charts.util;

import java.io.Serializable;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;

/**
* A text item, with an associated font, that fits on a single line (see
* {@link TextLine}). Instances of the class are immutable.
*
* @author David Gilbert
*/
public class TextFragment implements Serializable {

  /** For serialization. */
  private static final long serialVersionUID = 4465945952903143262L;

  /** The text. */
  private final String text;

  /** The font. */
  private final Font font;

  /** The text color. */
  private final Color paint;

  /**
   * The baseline offset (can be used to simulate subscripts and
   * superscripts).
   */
  private final float baselineOffset;

  /**
   * Creates a new text fragment.
   *
   * @param text
   *            the text (<code>null</code> not permitted).
   * @param font
   *            the font (<code>null</code> not permitted).
   * @param paint
   *            the text color (<code>null</code> not permitted).
   */
  public TextFragment(final String text, final Font font, final Color paint) {
    this(text, font, paint, 0.0f);
  }

  /**
   * Creates a new text fragment.
   *
   * @param text
   *            the text (<code>null</code> not permitted).
   * @param font
   *            the font (<code>null</code> not permitted).
   * @param paint
   *            the text color (<code>null</code> not permitted).
   * @param baselineOffset
   *            the baseline offset.
   */
  public TextFragment(final String text, final Font font, final Color paint,
      final float baselineOffset) {
    if (text == null) {
      throw new IllegalArgumentException("Null 'text' argument.");
    }
    if (font == null) {
      throw new IllegalArgumentException("Null 'font' argument.");
    }
    if (paint == null) {
      throw new IllegalArgumentException("Null 'paint' argument.");
    }
    this.text = text;
    this.font = font;
    this.paint = paint;
    this.baselineOffset = baselineOffset;
  }

  /**
   * Calculates the vertical offset between the baseline and the specified
   * text anchor.
   *
   * @param g2
   *            the graphics device.
   * @param anchor
   *            the anchor.
   *
   * @return the offset.
   */
  public float calculateBaselineOffset(final GC g2, final TextAnchor anchor) {
    float result = 0.0f;
    g2.setFont(this.font);
    final FontMetrics fm = g2.getFontMetrics();
    if ((anchor == TextAnchor.TOP_LEFT)
        || (anchor == TextAnchor.TOP_CENTER)
        || (anchor == TextAnchor.TOP_RIGHT)) {
      result = fm.getAscent();
    } else if ((anchor == TextAnchor.BOTTOM_LEFT)
        || (anchor == TextAnchor.BOTTOM_CENTER)
        || (anchor == TextAnchor.BOTTOM_RIGHT)) {
      result = -fm.getDescent() - fm.getLeading();
    }
    return result;
  }

  /**
   * Calculates the dimensions of the text fragment.
   *
   * @param g2
   *            the graphics device.
   *
   * @return The width and height of the text.
   */
  public Rectangle calculateDimensions(final GC g2) {
    final Rectangle bounds = TextUtilities.getTextBounds(this.text, g2);
    return bounds;
  }

  /**
   * Draws the text fragment.
   *
   * @param g2
   *            the graphics device.
   * @param anchorX
   *            the x-coordinate of the anchor point.
   * @param anchorY
   *            the y-coordinate of the anchor point.
   * @param anchor
   *            the location of the text that is aligned to the anchor point.
   * @param rotateX
   *            the x-coordinate of the rotation point.
   * @param rotateY
   *            the y-coordinate of the rotation point.
   * @param angle
   *            the angle.
   */
  public void draw(final GC g2, final float anchorX, final float anchorY,
      final TextAnchor anchor, final float rotateX, final float rotateY,
      final double angle) {

    g2.setFont(this.font);
    g2.setForeground(this.paint);
    TextUtilities.drawRotatedString(this.text, g2, anchorX, anchorY
        + this.baselineOffset, anchor, angle, rotateX, rotateY);

  }

  /**
   * Tests this instance for equality with an arbitrary object.
   *
   * @param obj
   *            the object to test against (<code>null</code> permitted).
   *
   * @return A boolean.
   */
  public boolean equals(final Object obj) {
    if (obj == null) {
      return false;
    }
    if (obj == this) {
      return true;
    }
    if (obj instanceof TextFragment) {
      final TextFragment tf = (TextFragment) obj;
      if (!this.text.equals(tf.text)) {
        return false;
      }
      if (!this.font.equals(tf.font)) {
        return false;
      }
      if (!this.paint.equals(tf.paint)) {
        return false;
      }
      return true;
    }
    return false;
  }

  public float getBaselineOffset() {
    return this.baselineOffset;
  }

  /**
   * Returns the font.
   *
   * @return The font (never <code>null</code>).
   */
  public Font getFont() {
    return this.font;
  }

  /**
   * Returns the text paint.
   *
   * @return The text paint (never <code>null</code>).
   */
  public Color getPaint() {
    return this.paint;
  }

  /**
   * Returns the text.
   *
   * @return The text (possibly <code>null</code>).
   */
  public String getText() {
    return this.text;
  }

  /**
   * Returns a hash code for this object.
   *
   * @return A hash code.
   */
  public int hashCode() {
    int result;
    result = (this.text != null ? this.text.hashCode() : 0);
    result = 29 * result + (this.font != null ? this.font.hashCode() : 0);
    result = 29 * result + (this.paint != null ? this.paint.hashCode() : 0);
    return result;
  }
}
TOP

Related Classes of com.positive.charts.util.TextFragment

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.