KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > util > TextPositionComparator


1 /**
2  * Copyright (c) 2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.util;
32
33 import java.util.Comparator JavaDoc;
34
35 import org.pdfbox.pdmodel.PDPage;
36
37 /**
38  * This class is a comparator for TextPosition operators.
39  *
40  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
41  * @version $Revision: 1.7 $
42  */

43 public class TextPositionComparator implements Comparator JavaDoc
44 {
45     private PDPage thePage = null;
46     
47     /**
48      * Constuctor, comparison of TextPosition depends on the rotation
49      * of the page.
50      * @param page The page that the text position is on.
51      */

52     public TextPositionComparator( PDPage page )
53     {
54         thePage = page;
55     }
56     
57     /**
58      * {@inheritDoc}
59      */

60     public int compare(Object JavaDoc o1, Object JavaDoc o2)
61     {
62         int retval = 0;
63         TextPosition pos1 = (TextPosition)o1;
64         TextPosition pos2 = (TextPosition)o2;
65         int rotation = thePage.findRotation();
66         float x1 = 0;
67         float x2 = 0;
68         float pos1YBottom = 0;
69         float pos2YBottom = 0;
70         if( rotation == 0 )
71         {
72             x1 = pos1.getX();
73             x2 = pos2.getX();
74             pos1YBottom = pos1.getY();
75             pos2YBottom = pos2.getY();
76         }
77         else if( rotation == 90 )
78         {
79             x1 = pos1.getY();
80             x2 = pos2.getX();
81             pos1YBottom = pos1.getX();
82             pos2YBottom = pos2.getY();
83         }
84         else if( rotation == 180 )
85         {
86             x1 = -pos1.getX();
87             x2 = -pos2.getX();
88             pos1YBottom = -pos1.getY();
89             pos2YBottom = -pos2.getY();
90         }
91         else if( rotation == 270 )
92         {
93             x1 = -pos1.getY();
94             x2 = -pos2.getY();
95             pos1YBottom = -pos1.getX();
96             pos2YBottom = -pos2.getX();
97         }
98         float pos1YTop = pos1YBottom - pos1.getHeight();
99         float pos2YTop = pos2YBottom - pos2.getHeight();
100
101         float yDifference = Math.abs( pos1YBottom-pos2YBottom);
102         //we will do a simple tolerance comparison.
103
if( yDifference < .1 ||
104             (pos2YBottom >= pos1YTop && pos2YBottom <= pos1YBottom) ||
105             (pos1YBottom >= pos2YTop && pos1YBottom <= pos2YBottom))
106         {
107             if( x1 < x2 )
108             {
109                 retval = -1;
110             }
111             else if( x1 > x2 )
112             {
113                 retval = 1;
114             }
115             else
116             {
117                 retval = 0;
118             }
119         }
120         else if( pos1YBottom < pos2YBottom )
121         {
122             retval = -1;
123         }
124         else
125         {
126             return 1;
127         }
128         
129         return retval;
130     }
131     
132 }
Popular Tags