这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class PDFBoxDocument implements SearchableDocument, SelectableDocument {
private File contentFile;
private int numberOfPages;
private BitSet landscapeCache;
private SelectionExtractor textPositionExtractor = new SelectionExtractor(-1);
private SelectionExtractor textPositionExtractor = null;

public PDFBoxDocument(InputStream pdfInputStream) {
try {
Expand Down Expand Up @@ -204,7 +204,7 @@ private int calculateTextPositionStartIndex(String searchText, String snippetTex
// This method is synchronized to avoid multiple selection service calls at the same time
@Override
public synchronized Selection getSelection(int pageNumber, Point2D start, Point2D end, Selection.Mode mode) {
if (textPositionExtractor.getPageNumber() != pageNumber) {
if (textPositionExtractor == null || textPositionExtractor.getPageNumber() != pageNumber) {
textPositionExtractor = new SelectionExtractor(pageNumber);
try (PDDocument doc = createDocument()) { // TODO :: recreating document is expensive
textPositionExtractor.writeText(doc, Writer.nullWriter());
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions pdfviewfx/src/main/java/com/dlsc/pdfviewfx/impl/TextLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void collectSelection(double startx, double endx, Selection.Mode mode, List<Rect
}
int startIndex = getStartIndex(startx, mode);
int endIndex = getEndIndex(endx, mode);
if (startIndex != -1 && endIndex != -1) {
if (startIndex != -1 && endIndex != -1 && endIndex > startIndex) {
for (int idx = startIndex; idx <= endIndex; idx++) {
selectionText.append(textPositions.get(idx).getUnicode());
}
Expand Down Expand Up @@ -128,7 +128,7 @@ private void addPosition(TextPosition textPosition) {
float descenderHeight = Math.abs((fontDescriptor.getDescent() / 1000.0f) * fontSize);
float ascenderHeight = Math.abs((fontDescriptor.getAscent() / 1000.0f) * fontSize);

top = Math.min(top, textPosition.getYDirAdj() - ascenderHeight + descenderHeight);
top = Math.min(top, textPosition.getYDirAdj() - ascenderHeight);
bottom = Math.max(bottom, textPosition.getYDirAdj() + descenderHeight);
textPositions.add(textPosition);
}
Expand Down
31 changes: 27 additions & 4 deletions pdfviewfx/src/main/java/com/dlsc/pdfviewfx/skins/PDFViewSkin.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ protected List<SearchResult> call() throws Exception {
class SelectionService extends Service<Selection> {
private Point2D start, end;
private Selection.Mode mode;

private volatile boolean restartLater;

public void setStart(Point2D start) {
this.start = start;
this.end = null;
Expand All @@ -308,6 +309,23 @@ public void setMode(Selection.Mode mode) {
protected Task<Selection> createTask() {
return new SelectionTask(getSkinnable().getDocument(), getSkinnable().getPage(), start, end, mode);
}

public void restartLater() {
if (isRunning()) {
restartLater = true;
} else {
restart();
}
}

@Override
protected void succeeded() {
if (restartLater) {
restartLater = false;
restart();
}

}
}

static class SelectionTask extends Task<Selection> {
Expand All @@ -334,6 +352,11 @@ protected Selection call() throws Exception {
return new Selection(-1, List.of(), "");
}
}

@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return super.cancel(false); // #21: Must not interrupt pdfbox otherwise the PDDocument will no longer be able to render pages
}
}


Expand Down Expand Up @@ -789,7 +812,7 @@ private void ensureVisible(Node node) {
selectionService.setStart(getMouseEventPoint(evt));
selectionService.setEnd(getMouseEventPoint(evt));
selectionService.setMode(Selection.Mode.forClickCount(evt.getClickCount()));
selectionService.restart();
selectionService.restartLater();
evt.consume();
}
});
Expand All @@ -798,15 +821,15 @@ private void ensureVisible(Node node) {
if (evt.getButton() == MouseButton.PRIMARY) {
group.setCursor(Cursor.DEFAULT);
selectionService.setEnd(getMouseEventPoint(evt));
selectionService.restart();
selectionService.restartLater();
evt.consume();
}
});

group.addEventHandler(MouseEvent.MOUSE_DRAGGED, evt -> {
if (evt.getButton() == MouseButton.PRIMARY) {
selectionService.setEnd(getMouseEventPoint(evt));
selectionService.restart();
selectionService.restartLater();
evt.consume();
}
});
Expand Down
Loading