View Javadoc

1   /*
2    * Copyright 2007 - 2007 JEuclid, http://jeuclid.sf.net
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* $Id: GraphicsSupport.java,v 86b5e673f9b8 2008/06/24 08:01:00 maxberger $ */
18  
19  package net.sourceforge.jeuclid.elements.support;
20  
21  import net.sourceforge.jeuclid.Constants;
22  import net.sourceforge.jeuclid.LayoutContext;
23  import net.sourceforge.jeuclid.context.Parameter;
24  
25  /**
26   * This class contains helper functions for graphical calculations.
27   * 
28   * @version $Revision: 86b5e673f9b8 $
29   */
30  public final class GraphicsSupport {
31  
32      /** Minimum line width. */
33      public static final float MIN_LINEWIDTH = 1.0f;
34  
35      private GraphicsSupport() {
36          // Empty on purpose.
37      }
38  
39      /**
40       * Gets the size of the actual font used (including scriptsizemultiplier).
41       * 
42       * @param context
43       *            Layout context to use.
44       * @return size of the current font.
45       */
46      public static float getFontsizeInPoint(final LayoutContext context) {
47          final float scriptMultiplier = (float) Math.pow((Float) context
48                  .getParameter(Parameter.SCRIPTSIZEMULTIPLIER),
49                  (Integer) context.getParameter(Parameter.SCRIPTLEVEL));
50          final float mathsize = (Float) context
51                  .getParameter(Parameter.MATHSIZE);
52          final float scriptminsize = (Float) context
53                  .getParameter(Parameter.SCRIPTMINSIZE);
54  
55          final float scriptsize = mathsize * scriptMultiplier;
56  
57          return Math.max(Math.min(scriptminsize, mathsize), scriptsize);
58      }
59  
60      /**
61       * Retrieve the width of a line that would be 1pt if unscaled.
62       * 
63       * @param context
64       *            Layout context to use.
65       * @return line width as float, at least {@link #MIN_LINEWIDTH}
66       */
67      public static float lineWidth(final LayoutContext context) {
68          float lineSize = GraphicsSupport.getFontsizeInPoint(context)
69                  / Constants.DEFAULT_FONTSIZE;
70          if (lineSize < GraphicsSupport.MIN_LINEWIDTH) {
71              lineSize = GraphicsSupport.MIN_LINEWIDTH;
72          }
73          return lineSize;
74      }
75  
76  }