001: /**
002: * Copyright (c) 2005, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.util;
031:
032: import java.util.Comparator;
033:
034: import org.pdfbox.pdmodel.PDPage;
035:
036: /**
037: * This class is a comparator for TextPosition operators.
038: *
039: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
040: * @version $Revision: 1.7 $
041: */
042: public class TextPositionComparator implements Comparator {
043: private PDPage thePage = null;
044:
045: /**
046: * Constuctor, comparison of TextPosition depends on the rotation
047: * of the page.
048: * @param page The page that the text position is on.
049: */
050: public TextPositionComparator(PDPage page) {
051: thePage = page;
052: }
053:
054: /**
055: * {@inheritDoc}
056: */
057: public int compare(Object o1, Object o2) {
058: int retval = 0;
059: TextPosition pos1 = (TextPosition) o1;
060: TextPosition pos2 = (TextPosition) o2;
061: int rotation = thePage.findRotation();
062: float x1 = 0;
063: float x2 = 0;
064: float pos1YBottom = 0;
065: float pos2YBottom = 0;
066: if (rotation == 0) {
067: x1 = pos1.getX();
068: x2 = pos2.getX();
069: pos1YBottom = pos1.getY();
070: pos2YBottom = pos2.getY();
071: } else if (rotation == 90) {
072: x1 = pos1.getY();
073: x2 = pos2.getX();
074: pos1YBottom = pos1.getX();
075: pos2YBottom = pos2.getY();
076: } else if (rotation == 180) {
077: x1 = -pos1.getX();
078: x2 = -pos2.getX();
079: pos1YBottom = -pos1.getY();
080: pos2YBottom = -pos2.getY();
081: } else if (rotation == 270) {
082: x1 = -pos1.getY();
083: x2 = -pos2.getY();
084: pos1YBottom = -pos1.getX();
085: pos2YBottom = -pos2.getX();
086: }
087: float pos1YTop = pos1YBottom - pos1.getHeight();
088: float pos2YTop = pos2YBottom - pos2.getHeight();
089:
090: float yDifference = Math.abs(pos1YBottom - pos2YBottom);
091: //we will do a simple tolerance comparison.
092: if (yDifference < .1
093: || (pos2YBottom >= pos1YTop && pos2YBottom <= pos1YBottom)
094: || (pos1YBottom >= pos2YTop && pos1YBottom <= pos2YBottom)) {
095: if (x1 < x2) {
096: retval = -1;
097: } else if (x1 > x2) {
098: retval = 1;
099: } else {
100: retval = 0;
101: }
102: } else if (pos1YBottom < pos2YBottom) {
103: retval = -1;
104: } else {
105: return 1;
106: }
107:
108: return retval;
109: }
110:
111: }
|