2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009-2012 - DIGITEO - Pierre Lando
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
12 package org.scilab.forge.scirenderer.texture;
14 import org.scilab.forge.scirenderer.shapes.appearance.Color;
16 import java.awt.Dimension;
18 import java.awt.font.FontRenderContext;
19 import java.awt.font.TextLayout;
20 import java.awt.geom.Rectangle2D;
23 * @author Pierre Lando
25 public class TextEntity {
28 * The default text color.
30 public static final Color DEFAULT_TEXT_COLOR = new Color(0.f, 0.f, 0.f);
33 * The default text anti-aliased status.
35 public static final boolean DEFAULT_TEXT_ANTI_ALIASED = true;
38 * The default text use fractional matrix status.
40 public static final boolean DEFAULT_TEXT_USE_FRACTIONAL_METRICS = true;
45 private static final Font DEFAULT_FONT = new Font(null);
48 * The current text color.
50 private Color textColor = DEFAULT_TEXT_COLOR;
53 * The current text anti-aliased status.
55 private boolean textAntiAliased = DEFAULT_TEXT_ANTI_ALIASED;
58 * The current text use fractional matrix status.
60 private boolean textUseFractionalMetrics = DEFAULT_TEXT_USE_FRACTIONAL_METRICS;
63 * The text content of this object.
68 * The font used by this text entity.
72 private TextLayout layout;
75 * Default constructor.
76 * @param text the text content.
78 public TextEntity(String text) {
80 this.font = DEFAULT_FONT;
84 * Return the text content.
85 * @return the text content.
87 public String getText() {
92 * Set the text content.
93 * @param text the new text content.
95 public void setText(String text) {
101 * Return the text font.
102 * @return the text font.
104 public Font getFont() {
110 * @param font the new text font.
112 public void setFont(Font font) {
118 * Return the text color.
119 * @return the text color.
121 public Color getTextColor() {
126 * Set the text color.
127 * @param textColor the new text color.
129 public void setTextColor(Color textColor) {
130 this.textColor = textColor;
134 * Return the text anti-aliased status.
135 * @return the text anti-aliased status.
137 public boolean isTextAntiAliased() {
138 return textAntiAliased;
142 * Set the text anti-aliased status.
143 * @param textAntiAliased the new text anti-aliased status.
145 public void setTextAntiAliased(boolean textAntiAliased) {
146 this.textAntiAliased = textAntiAliased;
151 * Return the text use fractional metrics status.
152 * @return the text use fractional metrics status.
154 public boolean isTextUseFractionalMetrics() {
155 return textUseFractionalMetrics;
159 * Set the text use fractional metrics status.
160 * @param textUseFractionalMetrics the new text use fractional metrics status.
162 public void setTextUseFractionalMetrics(boolean textUseFractionalMetrics) {
163 this.textUseFractionalMetrics = textUseFractionalMetrics;
168 * TextEntity validity getter.
169 * @return true if the text entity is valid.
171 public boolean isValid() {
172 return ((getFont() != null)
173 && (getText() != null)
174 && (getText().length() > 0)
178 public TextLayout getLayout() {
179 if (layout == null) {
180 FontRenderContext frc = new FontRenderContext(null, isTextAntiAliased(), isTextUseFractionalMetrics());
181 layout = new TextLayout(getText(), getFont(), frc);
188 * Return the dimension in pixel of the text entity.
189 * @return the dimension in pixel of the text entity.
191 public Dimension getSize() {
193 TextLayout textLayout = getLayout();
194 Dimension dimension = new Dimension();
195 Rectangle2D r = textLayout.getBounds();
196 /* +1 added to fix rendering of ticks labels, a pixel row/column was missing */
197 dimension.setSize(r.getWidth() + 2, textLayout.getAscent() + textLayout.getDescent() + 1);
200 return new Dimension(0, 0);