com.formkiq.vision.predicate.DocumentBlockTopAndXContainsPredicate Maven / Gradle / Ivy
/*
* Copyright (C) 2018 FormKiQ Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formkiq.vision.predicate;
import org.apache.commons.lang3.Range;
import com.formkiq.vision.document.DocumentBlockRectangle;
/**
* Finds the text matches to the Top and X contains of
* {@link DocumentBlockRectangle}.
*
*/
public class DocumentBlockTopAndXContainsPredicate
implements DocumentBlockRectanglePredicate {
/** {@link DocumentBlockRectangle}. */
private DocumentBlockRectangle rectangle;
/**
* constructor.
* @param rect {@link DocumentBlockRectangle}
*/
public DocumentBlockTopAndXContainsPredicate(
final DocumentBlockRectangle rect) {
this.rectangle = rect;
}
@Override
public boolean test(final DocumentBlockRectangle rect) {
boolean found = !rect.equals(this.rectangle)
&& isTextAboveField(rect, this.rectangle)
&& isXPositionWithin(rect, this.rectangle);
return found;
}
/**
* Is X position within Field.
* @param textRect {@link DocumentBlockRectangle}
* @param rect {@link DocumentBlockRectangle}
* @return boolean
*/
private boolean isXPositionWithin(final DocumentBlockRectangle textRect,
final DocumentBlockRectangle rect) {
Range b0 = Range.between(Float.valueOf(rect.getLowerLeftX()),
Float.valueOf(rect.getUpperRightX()));
Range b1 = Range.between(Float.valueOf(textRect.getLowerLeftX()),
Float.valueOf(textRect.getUpperRightX()));
return b0.isOverlappedBy(b1) || b1.isOverlappedBy(b0);
}
/**
* Is Text {@link DocumentBlockRectangle} above Field
* {@link DocumentBlockRectangle}.
*
* @param textRect {@link DocumentBlockRectangle}
* @param fieldRect {@link DocumentBlockRectangle}
* @return boolean
*/
private boolean isTextAboveField(final DocumentBlockRectangle textRect,
final DocumentBlockRectangle fieldRect) {
return textRect.getLowerLeftY() > fieldRect.getUpperRightY();
}
@Override
public DocumentBlockRectangle getRectangle() {
return this.rectangle;
}
@Override
public void setRectangle(final DocumentBlockRectangle rect) {
this.rectangle = rect;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy