Skip to content

Commit

Permalink
Merge pull request #7 from PrimosK/highlight-misplacement
Browse files Browse the repository at this point in the history
Highlights misalignment fix
  • Loading branch information
dlemmermann authored Apr 7, 2020
2 parents 3accd5a + d0c1b85 commit c3060d7
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions gemsfx/src/main/java/com/dlsc/gemsfx/skins/PDFViewSkin.java
Original file line number Diff line number Diff line change
Expand Up @@ -1006,9 +1006,6 @@ private void highlightSearchResults(int pageNumber, float scale, BufferedImage b
final List<SearchResult> searchResults = getSkinnable().getSearchResults().stream().filter(result -> result.getPageNumber() == pageNumber).collect(Collectors.toList());

if (!searchResults.isEmpty()) {
final PDDocument document = getSkinnable().getDocument();
final PDPage page = document.getPage(pageNumber);

final Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics();
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));

Expand All @@ -1019,16 +1016,17 @@ private void highlightSearchResults(int pageNumber, float scale, BufferedImage b

searchResults.forEach(result -> {
final String searchText = result.getSearchText();
final String snippetText = result.getText();
final List<TextPosition> textPositions = result.getTextPositions();
final int startIndex = result.getText().toLowerCase().indexOf(searchText.toLowerCase());
final int textPositionStartIndex = calculateTextPositionStartIndex(searchText, snippetText, textPositions);

float x1 = Float.MAX_VALUE;
float x2 = 0;
float y1 = Float.MAX_VALUE;
float y2 = 0;

for (int i = startIndex; i < startIndex + searchText.length(); i++) {
TextPosition position = textPositions.get(i);
for (int textPositionIndex = textPositionStartIndex; textPositionIndex < textPositionStartIndex + searchText.length(); textPositionIndex++) {
TextPosition position = textPositions.get(textPositionIndex);

x1 = Math.min(x1, position.getXDirAdj() * scale);
x2 = Math.max(x2, (position.getXDirAdj() + position.getWidth()) * scale);
Expand All @@ -1047,6 +1045,29 @@ private void highlightSearchResults(int pageNumber, float scale, BufferedImage b
});
}
}

/**
* Note that number of textPositions might not be equal to the length of the snippetText.
* so we need to account for that.
*
* See: org.apache.pdfbox.text.PDFTextStripper.WordWithTextPositions
*/
private int calculateTextPositionStartIndex(String searchText, String snippetText, List<TextPosition> textPositions) {

final int snippetTextStartIndex = snippetText.toLowerCase().indexOf(searchText.toLowerCase());

int startIndexDecreaseDelta = 0;

// If any TextPosition (up to the snippetTextStartIndex) contains more then one character, we have to account for that.
for (int i=0; i < snippetTextStartIndex; i++) {
int numberOfCharactersInTextPosition = textPositions.get(i).getUnicode().length();
if (numberOfCharactersInTextPosition > 1) {
startIndexDecreaseDelta = startIndexDecreaseDelta + (numberOfCharactersInTextPosition - 1);
}
}

return snippetTextStartIndex - startIndexDecreaseDelta;
}
}

private void updatePagesList() {
Expand Down

0 comments on commit c3060d7

Please sign in to comment.