1   /*
2    * Copyright 2008 - 2009 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: RenderInfo.java,v 15e2a11c098a 2009/06/01 12:47:30 maxberger $ */
18  
19  package net.sourceforge.jeuclid.test.testsuite;
20  
21  import java.io.Serializable;
22  
23  /**
24   * Storable Render Information, to compare layout.
25   * 
26   * @version $Revision: 15e2a11c098a $
27   */
28  public class RenderInfo implements Serializable {
29      private static final float FUZZYNESS = 0.1f;
30  
31      /**
32       * 
33       */
34      private static final long serialVersionUID = 1L;
35  
36      private final String elementName;
37  
38      private final float ascent;
39  
40      private final float descent;
41  
42      private final float width;
43  
44      private final float posx;
45  
46      private final float posy;
47  
48      /**
49       * Default constructor.
50       * 
51       * @param element
52       *            Name of the element
53       * @param asc
54       *            Ascent height
55       * @param desc
56       *            Descent height
57       * @param wid
58       *            Width
59       * @param x
60       *            X-Position
61       * @param y
62       *            Y-Position.
63       */
64      public RenderInfo(final String element, final float asc, final float desc,
65              final float wid, final float x, final float y) {
66          super();
67          if (element == null) {
68              this.elementName = "";
69          } else {
70              this.elementName = element;
71          }
72          this.ascent = asc;
73          this.descent = desc;
74          this.width = wid;
75          this.posx = x;
76          this.posy = y;
77      }
78  
79      /**
80       * Checks if this render information is similar enough (within
81       * {@link #FUZZYNESS} to another RenderInfo object.
82       * 
83       * @param other
84       *            RenderInfo to compare to.
85       * @return true if both RenderInfos are similar.
86       */
87      public String checkSimilar(final RenderInfo other) {
88          final StringBuilder b = new StringBuilder();
89          if (!this.elementName.equals(other.elementName)) {
90              b.append(" name was: " + other.elementName);
91          }
92          if (!this.isClose(this.ascent, other.ascent)) {
93              b.append(" ascent " + this.ascent + " vs. " + other.ascent);
94          }
95          if (!this.isClose(this.descent, other.descent)) {
96              b.append(" descent " + this.descent + " vs. " + other.descent);
97          }
98          if (!this.isClose(this.width, other.width)) {
99              b.append(" width " + this.width + " vs. " + other.width);
100         }
101         if (!this.isClose(this.posx, other.posx)) {
102             b.append(" posx " + this.posx + " vs. " + other.posx);
103         }
104         if (!this.isClose(this.posy, other.posy)) {
105             b.append(" posy " + this.posy + " vs. " + other.posy);
106         }
107         return b.toString();
108     }
109 
110     private boolean isClose(final float should, final float is) {
111         final float maxdelta = Math.max(
112                 Math.abs(should * RenderInfo.FUZZYNESS), 0.1f);
113         return Math.abs(should - is) <= maxdelta;
114     }
115 
116     /**
117      * @return the Element Name.
118      */
119     public String getElementName() {
120         return this.elementName;
121     }
122 
123 }