2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009-2010 - 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-en.txt
12 package org.scilab.modules.renderer.JoGLView.axes.ruler;
14 import org.scilab.forge.scirenderer.ruler.RulerSpriteFactory;
15 import org.scilab.forge.scirenderer.texture.TextEntity;
16 import org.scilab.forge.scirenderer.texture.Texture;
17 import org.scilab.forge.scirenderer.texture.TextureDrawer;
18 import org.scilab.forge.scirenderer.texture.TextureDrawingTools;
19 import org.scilab.forge.scirenderer.texture.TextureManager;
20 import org.scilab.modules.graphic_objects.axes.Axes;
21 import org.scilab.modules.graphic_objects.axes.AxisProperty;
22 import org.scilab.modules.graphic_objects.figure.ColorMap;
23 import org.scilab.modules.graphic_objects.figure.Figure;
24 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
25 import org.scilab.modules.graphic_objects.textObject.FormattedText;
26 import org.scilab.modules.renderer.JoGLView.util.ColorFactory;
27 import org.scilab.modules.renderer.JoGLView.util.FormattedTextSpriteDrawer;
28 import org.scilab.modules.renderer.utils.textRendering.FontManager;
30 import java.awt.Dimension;
32 import java.text.DecimalFormat;
33 import java.text.DecimalFormatSymbols;
36 * This implementation of {@see RulerSpriteFactory} create ruler labels for the given {@see Axes}.
38 * @author Pierre Lando
40 class AxesRulerSpriteFactory implements RulerSpriteFactory {
42 * The symbol used for ticks label in log and auto ticks mode.
44 private static final String MULTIPLICATION_SYMBOL = "x";
47 * The exponent size is smaller than the mantissa size.
49 private static final float EXPONENT_SIZE_RATIO = 0.4f;
52 * This factory create ruler label for this {@see Axis}.
54 private final AxisProperty axisProperty;
57 * The current colormap.
59 private final ColorMap colorMap;
62 * Default constructor.
63 * @param axes This factory create ruler label for one axis of this given {@see Axes}.
64 * @param axisId the id of the managed axis.
66 public AxesRulerSpriteFactory(Axes axes, int axisId) {
67 this.axisProperty = axes.getAxes()[axisId];
68 ColorMap figureColorMap;
70 GraphicController controller = GraphicController.getController();
71 Figure parentFigure = (Figure) controller.getObjectFromId(axes.getParentFigure());
72 figureColorMap = parentFigure.getColorMap();
73 } catch (NullPointerException e) {
74 figureColorMap = null;
76 this.colorMap = figureColorMap;
80 public Texture create(double value, DecimalFormat adaptedFormat, TextureManager textureManager) {
81 // Simple ack to avoid ticks with "-0" as label.
86 if (axisProperty.getAutoTicks()) {
87 setScilabStyle(adaptedFormat);
88 if (axisProperty.getLogFlag()) {
89 return createScientificStyleSprite(value, textureManager);
91 return createSimpleSprite(adaptedFormat.format(value), textureManager);
94 FormattedText formattedText = getTextAtValue(value);
95 FormattedTextSpriteDrawer textObjectSpriteDrawer = new FormattedTextSpriteDrawer(colorMap, formattedText);
96 Texture texture = textureManager.createTexture();
97 texture.setDrawer(textObjectSpriteDrawer);
103 * Set the given {@see DecimalFormat} to scilab style.
104 * @param format the given {@see DecimalFormat}.
106 public static void setScilabStyle(DecimalFormat format) {
107 DecimalFormatSymbols decimalFormatSymbols = format.getDecimalFormatSymbols();
108 decimalFormatSymbols.setDecimalSeparator('.');
109 decimalFormatSymbols.setExponentSeparator("e");
110 decimalFormatSymbols.setGroupingSeparator('\u00A0');
111 format.setDecimalFormatSymbols(decimalFormatSymbols);
115 * Create and return a texture representing the given value.|| index >= axisProperty.getTicksLabels().size()
116 * The returned sprites will look like "5x10^2"
117 * @param value, the given value.
118 * @param textureManager used texture manager.
119 * @return a simple texture representing the given value with the adapted format.
121 private Texture createScientificStyleSprite(double value, TextureManager textureManager) {
122 Integer exponent = (int) Math.floor(Math.log10(value));
123 Double mantissa = value / Math.pow(10, exponent);
128 final TextEntity mantissaTextEntity;
130 mantissaTextEntity = new TextEntity(mantissa.toString() + MULTIPLICATION_SYMBOL + "10");
132 mantissaTextEntity = new TextEntity("10");
135 Font mantissaFont = FontManager.getSciFontManager().getFontFromIndex(axisProperty.getFontStyle(), axisProperty.getFontSize());
136 mantissaTextEntity.setTextAntiAliased(true);
137 mantissaTextEntity.setTextUseFractionalMetrics(axisProperty.getFontFractional());
138 mantissaTextEntity.setTextColor(ColorFactory.createColor(colorMap, axisProperty.getFontColor()));
139 mantissaTextEntity.setFont(mantissaFont);
140 final Dimension mantissaSize = mantissaTextEntity.getSize();
145 final TextEntity exponentTextEntity = new TextEntity(exponent.toString());
146 Font exponentFont = FontManager.getSciFontManager().getFontFromIndex(axisProperty.getFontStyle(), axisProperty.getFontSize() * EXPONENT_SIZE_RATIO);
147 exponentTextEntity.setTextAntiAliased(true);
148 exponentTextEntity.setTextUseFractionalMetrics(axisProperty.getFontFractional());
149 exponentTextEntity.setTextColor(ColorFactory.createColor(colorMap, axisProperty.getFontColor()));
150 exponentTextEntity.setFont(exponentFont);
151 final Dimension exponentSize = exponentTextEntity.getSize();
153 Texture texture = textureManager.createTexture();
155 texture.setDrawer(new TextureDrawer() {
158 public void draw(TextureDrawingTools drawingTools) {
159 drawingTools.draw(mantissaTextEntity, 0, exponentSize.height);
160 drawingTools.draw(exponentTextEntity, mantissaSize.width, 0);
164 public Dimension getTextureSize() {
165 return new Dimension(
166 exponentSize.width + mantissaSize.width,
167 exponentSize.height + mantissaSize.height
172 public TextureDrawer.OriginPosition getOriginPosition() {
173 return TextureDrawer.OriginPosition.UPPER_LEFT;
181 * Create and return a simple texture representing the given value.
182 * @param text the formatted string representing the value.
183 * @param textureManager used texture manager.
184 * @return a simple texture representing the given value with the adapted format.
186 private Texture createSimpleSprite(String text, TextureManager textureManager) {
187 Font font = FontManager.getSciFontManager().getFontFromIndex(axisProperty.getFontStyle(), axisProperty.getFontSize());
188 final TextEntity textEntity = new TextEntity(text);
189 textEntity.setTextAntiAliased(true);
190 textEntity.setTextUseFractionalMetrics(axisProperty.getFontFractional());
191 textEntity.setTextColor(ColorFactory.createColor(colorMap, axisProperty.getFontColor()));
192 textEntity.setFont(font);
194 Texture texture = textureManager.createTexture();
195 texture.setDrawer(new TextureDrawer() {
198 public void draw(TextureDrawingTools drawingTools) {
199 drawingTools.draw(textEntity, 0, 0);
203 public Dimension getTextureSize() {
204 return textEntity.getSize();
208 public TextureDrawer.OriginPosition getOriginPosition() {
209 return TextureDrawer.OriginPosition.UPPER_LEFT;
217 * Return the user defined {@see FormattedText} ticks label corresponding to the given value.
218 * @param value the given value.
219 * @return the user defined {@see FormattedText} ticks label corresponding to the given value.
221 private FormattedText getTextAtValue(double value) {
222 Double[] locations = axisProperty.getTicksLocations();
224 for (int i = 0 ; i < locations.length ; i++) {
225 if (locations[i] == value) {
230 if (index == -1 || index >= axisProperty.getTicksLabels().size()) {
233 return axisProperty.getTicksLabels().get(index);