Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encapsulate the method's of rulings from class Page #514

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 12 additions & 66 deletions src/main/java/technology/tabula/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ public class Page extends Rectangle {
private List<TextElement> textElements;

// TODO: Create a class for 'List <Ruling>' that encapsulates all of these lists and their behaviors?
private List<Ruling> rulings,
cleanRulings = null,
verticalRulingLines = null,
horizontalRulingLines = null;

private Rulings rulings;
private PDPage pdPage;
private PDDocument pdDoc;

Expand All @@ -54,7 +50,7 @@ private Page(
this.pdPage = pdPage;
this.pdDoc = doc;
this.textElements = characters;
this.rulings = rulings;
this.rulings = new Rulings(rulings);
this.minCharWidth = minCharWidth;
this.minCharHeight = minCharHeight;
this.spatialIndex = index;
Expand All @@ -81,7 +77,7 @@ public Page(float top, float left, float width, float height, int rotation, int
List<TextElement> characters, List<Ruling> rulings) {
this(top, left, width, height, rotation, number, pdPage, doc);
this.textElements = characters;
this.rulings = rulings;
this.rulings = new Rulings(rulings);
}

/**
Expand Down Expand Up @@ -125,7 +121,7 @@ public Page getArea(Rectangle area) {
.withPdPage(pdPage)
.withPdDocument(pdDoc)
.withTextElements(areaTextElements)
.withRulings(Ruling.cropRulingsToArea(getRulings(), area))
.withRulings(Ruling.cropRulingsToArea(rulings.getRulings(), area))
.withMinCharWidth(minimumCharWidth)
.withMinCharHeight(minimumCharHeight)
.withIndex(spatialIndex)
Expand Down Expand Up @@ -238,78 +234,28 @@ public boolean hasText() {

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
public List<Ruling> getRulings() {
if (cleanRulings != null) {
return cleanRulings;
}

if (rulings == null || rulings.isEmpty()) {
verticalRulingLines = new ArrayList<>();
horizontalRulingLines = new ArrayList<>();
return new ArrayList<>();
}

// TODO: Move as a static method to the Ruling class?
Utils.snapPoints(rulings, minCharWidth, minCharHeight);

verticalRulingLines = getCollapsedVerticalRulings();
horizontalRulingLines = getCollapsedHorizontalRulings();

cleanRulings = new ArrayList<>(verticalRulingLines);
cleanRulings.addAll(horizontalRulingLines);

return cleanRulings;
}
List<Ruling> rulings = this.rulings.getRulings();

// TODO: Create a class for 'List <Ruling>' and encapsulate these behaviors within it?
private List<Ruling> getCollapsedVerticalRulings() {
List<Ruling> verticalRulings = new ArrayList<>();
for (Ruling ruling : rulings) {
if (ruling.vertical()) {
verticalRulings.add(ruling);
}
if (rulings != null && !rulings.isEmpty()) {
Utils.snapPoints(rulings, minCharWidth, minCharHeight);;
}
return Ruling.collapseOrientedRulings(verticalRulings);
}

private List<Ruling> getCollapsedHorizontalRulings() {
List<Ruling> horizontalRulings = new ArrayList<>();
for (Ruling ruling : rulings) {
if (ruling.horizontal()) {
horizontalRulings.add(ruling);
}
}
return Ruling.collapseOrientedRulings(horizontalRulings);
return rulings;
}

public List<Ruling> getVerticalRulings() {
if (verticalRulingLines != null) {
return verticalRulingLines;
}
getRulings();
return verticalRulingLines;
return rulings.getVerticalRulings();
}

public List<Ruling> getHorizontalRulings() {
if (horizontalRulingLines != null) {
return horizontalRulingLines;
}
getRulings();
return horizontalRulingLines;
return rulings.getHorizontalRulings();
}

public void addRuling(Ruling ruling) {
if (ruling.oblique()) {
throw new UnsupportedOperationException("Can't add an oblique ruling.");
}
rulings.add(ruling);
// Clear caches:
verticalRulingLines = null;
horizontalRulingLines = null;
cleanRulings = null;
rulings.addRuling(ruling);
}

public List<Ruling> getUnprocessedRulings() {
return rulings;
return rulings.getUnprocessedRulings();
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/technology/tabula/Rulings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package technology.tabula;

import java.util.ArrayList;
import java.util.List;

public class Rulings {
private List<Ruling> rulings = new ArrayList<>();
private List<Ruling> cleanRulings = new ArrayList<>();
// private float minCharWidth;
// private float minCharHeight;

public Rulings(List<Ruling> rulings) {
this.rulings = rulings;
// this.minCharWidth = minCharWidth;
// this.minCharHeight = minCharHeight;
}

public List<Ruling> getRulings() {
if (rulings == null || rulings.isEmpty()) {
return rulings;
}
List<Ruling> verticalRulingLines = getVerticalRulings();
List<Ruling> horizontalRulingLines = getHorizontalRulings();

cleanRulings = new ArrayList<>(verticalRulingLines);
cleanRulings.addAll(horizontalRulingLines);
return cleanRulings;
}

public List<Ruling> getVerticalRulings() {
List<Ruling> verticalRulings = new ArrayList<>();
for (Ruling ruling : rulings) {
if (ruling.vertical()) {
verticalRulings.add(ruling);
}
}
return Ruling.collapseOrientedRulings(verticalRulings);
}

public List<Ruling> getHorizontalRulings() {
List<Ruling> horizontalRulings = new ArrayList<>();
for (Ruling ruling : rulings) {
if (ruling.vertical()) {
horizontalRulings.add(ruling);
}
}
return Ruling.collapseOrientedRulings(horizontalRulings);
}

public void addRuling(Ruling ruling) {
if (ruling.oblique()) {
throw new UnsupportedOperationException("Can't add an oblique ruling.");
}
rulings.add(ruling);
}

public List<Ruling> getUnprocessedRulings() {
return rulings;
}

}